blob: acb683e5b011104364f9316ec0021a2c032c6965 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===----- CGOpenMPRuntime.cpp - Interface to OpenMP Runtimes -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides a class for OpenMP runtime code generation.
11//
12//===----------------------------------------------------------------------===//
13
Samuel Antaoee8fb302016-01-06 13:42:12 +000014#include "CGCXXABI.h"
15#include "CGCleanup.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "CGOpenMPRuntime.h"
17#include "CodeGenFunction.h"
John McCall5ad74072017-03-02 20:04:19 +000018#include "clang/CodeGen/ConstantInitBuilder.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000019#include "clang/AST/Decl.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000020#include "clang/AST/StmtOpenMP.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000021#include "llvm/ADT/ArrayRef.h"
Alexey Bataev0f87dbe2017-08-14 17:56:13 +000022#include "llvm/ADT/BitmaskEnum.h"
Teresa Johnsonffc4e242016-11-11 05:35:12 +000023#include "llvm/Bitcode/BitcodeReader.h"
Alexey Bataevd74d0602014-10-13 06:02:40 +000024#include "llvm/IR/CallSite.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000025#include "llvm/IR/DerivedTypes.h"
26#include "llvm/IR/GlobalValue.h"
27#include "llvm/IR/Value.h"
Samuel Antaoee8fb302016-01-06 13:42:12 +000028#include "llvm/Support/Format.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000029#include "llvm/Support/raw_ostream.h"
Alexey Bataev23b69422014-06-18 07:08:49 +000030#include <cassert>
Alexey Bataev9959db52014-05-06 10:08:46 +000031
32using namespace clang;
33using namespace CodeGen;
34
Benjamin Kramerc52193f2014-10-10 13:57:57 +000035namespace {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000036/// \brief Base class for handling code generation inside OpenMP regions.
Alexey Bataev18095712014-10-10 12:19:54 +000037class CGOpenMPRegionInfo : public CodeGenFunction::CGCapturedStmtInfo {
38public:
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000039 /// \brief Kinds of OpenMP regions used in codegen.
40 enum CGOpenMPRegionKind {
41 /// \brief Region with outlined function for standalone 'parallel'
42 /// directive.
43 ParallelOutlinedRegion,
44 /// \brief Region with outlined function for standalone 'task' directive.
45 TaskOutlinedRegion,
46 /// \brief Region for constructs that do not require function outlining,
47 /// like 'for', 'sections', 'atomic' etc. directives.
48 InlinedRegion,
Samuel Antaobed3c462015-10-02 16:14:20 +000049 /// \brief Region with outlined function for standalone 'target' directive.
50 TargetRegion,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000051 };
Alexey Bataev18095712014-10-10 12:19:54 +000052
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000053 CGOpenMPRegionInfo(const CapturedStmt &CS,
54 const CGOpenMPRegionKind RegionKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +000055 const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind,
56 bool HasCancel)
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000057 : CGCapturedStmtInfo(CS, CR_OpenMP), RegionKind(RegionKind),
Alexey Bataev25e5b442015-09-15 12:52:43 +000058 CodeGen(CodeGen), Kind(Kind), HasCancel(HasCancel) {}
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000059
60 CGOpenMPRegionInfo(const CGOpenMPRegionKind RegionKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +000061 const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind,
62 bool HasCancel)
Alexey Bataev81c7ea02015-07-03 09:56:58 +000063 : CGCapturedStmtInfo(CR_OpenMP), RegionKind(RegionKind), CodeGen(CodeGen),
Alexey Bataev25e5b442015-09-15 12:52:43 +000064 Kind(Kind), HasCancel(HasCancel) {}
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000065
66 /// \brief Get a variable or parameter for storing global thread id
Alexey Bataev18095712014-10-10 12:19:54 +000067 /// inside OpenMP construct.
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000068 virtual const VarDecl *getThreadIDVariable() const = 0;
Alexey Bataev18095712014-10-10 12:19:54 +000069
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000070 /// \brief Emit the captured statement body.
Hans Wennborg7eb54642015-09-10 17:07:54 +000071 void EmitBody(CodeGenFunction &CGF, const Stmt *S) override;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000072
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000073 /// \brief Get an LValue for the current ThreadID variable.
Alexey Bataev62b63b12015-03-10 07:28:44 +000074 /// \return LValue for thread id variable. This LValue always has type int32*.
75 virtual LValue getThreadIDVariableLValue(CodeGenFunction &CGF);
Alexey Bataev18095712014-10-10 12:19:54 +000076
Alexey Bataev48591dd2016-04-20 04:01:36 +000077 virtual void emitUntiedSwitch(CodeGenFunction & /*CGF*/) {}
78
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000079 CGOpenMPRegionKind getRegionKind() const { return RegionKind; }
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000080
Alexey Bataev81c7ea02015-07-03 09:56:58 +000081 OpenMPDirectiveKind getDirectiveKind() const { return Kind; }
82
Alexey Bataev25e5b442015-09-15 12:52:43 +000083 bool hasCancel() const { return HasCancel; }
84
Alexey Bataev18095712014-10-10 12:19:54 +000085 static bool classof(const CGCapturedStmtInfo *Info) {
86 return Info->getKind() == CR_OpenMP;
87 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000088
Alexey Bataev48591dd2016-04-20 04:01:36 +000089 ~CGOpenMPRegionInfo() override = default;
90
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000091protected:
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000092 CGOpenMPRegionKind RegionKind;
Hans Wennborg45c74392016-01-12 20:54:36 +000093 RegionCodeGenTy CodeGen;
Alexey Bataev81c7ea02015-07-03 09:56:58 +000094 OpenMPDirectiveKind Kind;
Alexey Bataev25e5b442015-09-15 12:52:43 +000095 bool HasCancel;
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000096};
Alexey Bataev18095712014-10-10 12:19:54 +000097
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000098/// \brief API for captured statement code generation in OpenMP constructs.
Alexey Bataev48591dd2016-04-20 04:01:36 +000099class CGOpenMPOutlinedRegionInfo final : public CGOpenMPRegionInfo {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000100public:
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000101 CGOpenMPOutlinedRegionInfo(const CapturedStmt &CS, const VarDecl *ThreadIDVar,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000102 const RegionCodeGenTy &CodeGen,
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000103 OpenMPDirectiveKind Kind, bool HasCancel,
104 StringRef HelperName)
Alexey Bataev25e5b442015-09-15 12:52:43 +0000105 : CGOpenMPRegionInfo(CS, ParallelOutlinedRegion, CodeGen, Kind,
106 HasCancel),
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000107 ThreadIDVar(ThreadIDVar), HelperName(HelperName) {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000108 assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region.");
109 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000110
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000111 /// \brief Get a variable or parameter for storing global thread id
112 /// inside OpenMP construct.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000113 const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000114
Alexey Bataev18095712014-10-10 12:19:54 +0000115 /// \brief Get the name of the capture helper.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000116 StringRef getHelperName() const override { return HelperName; }
Alexey Bataev18095712014-10-10 12:19:54 +0000117
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000118 static bool classof(const CGCapturedStmtInfo *Info) {
119 return CGOpenMPRegionInfo::classof(Info) &&
120 cast<CGOpenMPRegionInfo>(Info)->getRegionKind() ==
121 ParallelOutlinedRegion;
122 }
123
Alexey Bataev18095712014-10-10 12:19:54 +0000124private:
125 /// \brief A variable or parameter storing global thread id for OpenMP
126 /// constructs.
127 const VarDecl *ThreadIDVar;
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000128 StringRef HelperName;
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000129};
130
Alexey Bataev62b63b12015-03-10 07:28:44 +0000131/// \brief API for captured statement code generation in OpenMP constructs.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000132class CGOpenMPTaskOutlinedRegionInfo final : public CGOpenMPRegionInfo {
Alexey Bataev62b63b12015-03-10 07:28:44 +0000133public:
Alexey Bataev48591dd2016-04-20 04:01:36 +0000134 class UntiedTaskActionTy final : public PrePostActionTy {
135 bool Untied;
136 const VarDecl *PartIDVar;
137 const RegionCodeGenTy UntiedCodeGen;
138 llvm::SwitchInst *UntiedSwitch = nullptr;
139
140 public:
141 UntiedTaskActionTy(bool Tied, const VarDecl *PartIDVar,
142 const RegionCodeGenTy &UntiedCodeGen)
143 : Untied(!Tied), PartIDVar(PartIDVar), UntiedCodeGen(UntiedCodeGen) {}
144 void Enter(CodeGenFunction &CGF) override {
145 if (Untied) {
146 // Emit task switching point.
147 auto PartIdLVal = CGF.EmitLoadOfPointerLValue(
148 CGF.GetAddrOfLocalVar(PartIDVar),
149 PartIDVar->getType()->castAs<PointerType>());
Alexey Bataeva9b9cc02018-01-23 18:12:38 +0000150 auto *Res = CGF.EmitLoadOfScalar(PartIdLVal, PartIDVar->getLocation());
Alexey Bataev48591dd2016-04-20 04:01:36 +0000151 auto *DoneBB = CGF.createBasicBlock(".untied.done.");
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) {
163 auto PartIdLVal = CGF.EmitLoadOfPointerLValue(
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 Bataev6f1ffc02015-04-10 04:50:10 +0000181 CGOpenMPTaskOutlinedRegionInfo(const CapturedStmt &CS,
Alexey Bataev62b63b12015-03-10 07:28:44 +0000182 const VarDecl *ThreadIDVar,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000183 const RegionCodeGenTy &CodeGen,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000184 OpenMPDirectiveKind Kind, bool HasCancel,
185 const UntiedTaskActionTy &Action)
Alexey Bataev25e5b442015-09-15 12:52:43 +0000186 : CGOpenMPRegionInfo(CS, TaskOutlinedRegion, CodeGen, Kind, HasCancel),
Alexey Bataev48591dd2016-04-20 04:01:36 +0000187 ThreadIDVar(ThreadIDVar), Action(Action) {
Alexey Bataev62b63b12015-03-10 07:28:44 +0000188 assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region.");
189 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000190
Alexey Bataev62b63b12015-03-10 07:28:44 +0000191 /// \brief Get a variable or parameter for storing global thread id
192 /// inside OpenMP construct.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000193 const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; }
Alexey Bataev62b63b12015-03-10 07:28:44 +0000194
195 /// \brief Get an LValue for the current ThreadID variable.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000196 LValue getThreadIDVariableLValue(CodeGenFunction &CGF) override;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000197
Alexey Bataev62b63b12015-03-10 07:28:44 +0000198 /// \brief Get the name of the capture helper.
199 StringRef getHelperName() const override { return ".omp_outlined."; }
200
Alexey Bataev48591dd2016-04-20 04:01:36 +0000201 void emitUntiedSwitch(CodeGenFunction &CGF) override {
202 Action.emitUntiedSwitch(CGF);
203 }
204
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000205 static bool classof(const CGCapturedStmtInfo *Info) {
206 return CGOpenMPRegionInfo::classof(Info) &&
207 cast<CGOpenMPRegionInfo>(Info)->getRegionKind() ==
208 TaskOutlinedRegion;
209 }
210
Alexey Bataev62b63b12015-03-10 07:28:44 +0000211private:
212 /// \brief A variable or parameter storing global thread id for OpenMP
213 /// constructs.
214 const VarDecl *ThreadIDVar;
Alexey Bataev48591dd2016-04-20 04:01:36 +0000215 /// Action for emitting code for untied tasks.
216 const UntiedTaskActionTy &Action;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000217};
218
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000219/// \brief API for inlined captured statement code generation in OpenMP
220/// constructs.
221class CGOpenMPInlinedRegionInfo : public CGOpenMPRegionInfo {
222public:
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000223 CGOpenMPInlinedRegionInfo(CodeGenFunction::CGCapturedStmtInfo *OldCSI,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000224 const RegionCodeGenTy &CodeGen,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000225 OpenMPDirectiveKind Kind, bool HasCancel)
226 : CGOpenMPRegionInfo(InlinedRegion, CodeGen, Kind, HasCancel),
227 OldCSI(OldCSI),
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000228 OuterRegionInfo(dyn_cast_or_null<CGOpenMPRegionInfo>(OldCSI)) {}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000229
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000230 // \brief Retrieve the value of the context parameter.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000231 llvm::Value *getContextValue() const override {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000232 if (OuterRegionInfo)
233 return OuterRegionInfo->getContextValue();
234 llvm_unreachable("No context value for inlined OpenMP region");
235 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000236
Hans Wennborg7eb54642015-09-10 17:07:54 +0000237 void setContextValue(llvm::Value *V) override {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000238 if (OuterRegionInfo) {
239 OuterRegionInfo->setContextValue(V);
240 return;
241 }
242 llvm_unreachable("No context value for inlined OpenMP region");
243 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000244
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000245 /// \brief Lookup the captured field decl for a variable.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000246 const FieldDecl *lookup(const VarDecl *VD) const override {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000247 if (OuterRegionInfo)
248 return OuterRegionInfo->lookup(VD);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000249 // 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 Bataev8cbe0a62015-02-26 10:27:34 +0000252 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000253
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000254 FieldDecl *getThisFieldDecl() const override {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000255 if (OuterRegionInfo)
256 return OuterRegionInfo->getThisFieldDecl();
257 return nullptr;
258 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000259
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000260 /// \brief Get a variable or parameter for storing global thread id
261 /// inside OpenMP construct.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000262 const VarDecl *getThreadIDVariable() const override {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000263 if (OuterRegionInfo)
264 return OuterRegionInfo->getThreadIDVariable();
265 return nullptr;
266 }
Alexey Bataev62b63b12015-03-10 07:28:44 +0000267
Alexey Bataev311a9282017-10-12 13:51:32 +0000268 /// \brief Get an LValue for the current ThreadID variable.
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
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000275 /// \brief Get the name of the capture helper.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000276 StringRef getHelperName() const override {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000277 if (auto *OuterRegionInfo = getOldCSI())
278 return OuterRegionInfo->getHelperName();
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000279 llvm_unreachable("No helper name for inlined OpenMP construct");
280 }
281
Alexey Bataev48591dd2016-04-20 04:01:36 +0000282 void emitUntiedSwitch(CodeGenFunction &CGF) override {
283 if (OuterRegionInfo)
284 OuterRegionInfo->emitUntiedSwitch(CGF);
285 }
286
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000287 CodeGenFunction::CGCapturedStmtInfo *getOldCSI() const { return OldCSI; }
288
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000289 static bool classof(const CGCapturedStmtInfo *Info) {
290 return CGOpenMPRegionInfo::classof(Info) &&
291 cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == InlinedRegion;
292 }
293
Alexey Bataev48591dd2016-04-20 04:01:36 +0000294 ~CGOpenMPInlinedRegionInfo() override = default;
295
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000296private:
297 /// \brief CodeGen info about outer OpenMP region.
298 CodeGenFunction::CGCapturedStmtInfo *OldCSI;
299 CGOpenMPRegionInfo *OuterRegionInfo;
Alexey Bataev18095712014-10-10 12:19:54 +0000300};
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000301
Samuel Antaobed3c462015-10-02 16:14:20 +0000302/// \brief API for captured statement code generation in OpenMP target
303/// constructs. For this captures, implicit parameters are used instead of the
Samuel Antaoee8fb302016-01-06 13:42:12 +0000304/// 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 Bataev48591dd2016-04-20 04:01:36 +0000307class CGOpenMPTargetRegionInfo final : public CGOpenMPRegionInfo {
Samuel Antaobed3c462015-10-02 16:14:20 +0000308public:
309 CGOpenMPTargetRegionInfo(const CapturedStmt &CS,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000310 const RegionCodeGenTy &CodeGen, StringRef HelperName)
Samuel Antaobed3c462015-10-02 16:14:20 +0000311 : CGOpenMPRegionInfo(CS, TargetRegion, CodeGen, OMPD_target,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000312 /*HasCancel=*/false),
313 HelperName(HelperName) {}
Samuel Antaobed3c462015-10-02 16:14:20 +0000314
315 /// \brief This is unused for target regions because each starts executing
316 /// with a single thread.
317 const VarDecl *getThreadIDVariable() const override { return nullptr; }
318
319 /// \brief Get the name of the capture helper.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000320 StringRef getHelperName() const override { return HelperName; }
Samuel Antaobed3c462015-10-02 16:14:20 +0000321
322 static bool classof(const CGCapturedStmtInfo *Info) {
323 return CGOpenMPRegionInfo::classof(Info) &&
324 cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == TargetRegion;
325 }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000326
327private:
328 StringRef HelperName;
Samuel Antaobed3c462015-10-02 16:14:20 +0000329};
330
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000331static void EmptyCodeGen(CodeGenFunction &, PrePostActionTy &) {
Samuel Antaob68e2db2016-03-03 16:20:23 +0000332 llvm_unreachable("No codegen for expressions");
333}
334/// \brief API for generation of expressions captured in a innermost OpenMP
335/// region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000336class CGOpenMPInnerExprInfo final : public CGOpenMPInlinedRegionInfo {
Samuel Antaob68e2db2016-03-03 16:20:23 +0000337public:
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.
346 for (auto &C : CS.captures()) {
347 if (!C.capturesVariable() && !C.capturesVariableByCopy())
348 continue;
349
350 const VarDecl *VD = C.getCapturedVar();
351 if (VD->isLocalVarDeclOrParm())
352 continue;
353
354 DeclRefExpr DRE(const_cast<VarDecl *>(VD),
355 /*RefersToEnclosingVariableOrCapture=*/false,
356 VD->getType().getNonReferenceType(), VK_LValue,
Alexey Bataeva9b9cc02018-01-23 18:12:38 +0000357 C.getLocation());
Samuel Antaob68e2db2016-03-03 16:20:23 +0000358 PrivScope.addPrivate(VD, [&CGF, &DRE]() -> Address {
359 return CGF.EmitLValue(&DRE).getAddress();
360 });
361 }
362 (void)PrivScope.Privatize();
363 }
364
365 /// \brief Lookup the captured field decl for a variable.
366 const FieldDecl *lookup(const VarDecl *VD) const override {
367 if (auto *FD = CGOpenMPInlinedRegionInfo::lookup(VD))
368 return FD;
369 return nullptr;
370 }
371
372 /// \brief Emit the captured statement body.
373 void EmitBody(CodeGenFunction &CGF, const Stmt *S) override {
374 llvm_unreachable("No body for expressions");
375 }
376
377 /// \brief Get a variable or parameter for storing global thread id
378 /// inside OpenMP construct.
379 const VarDecl *getThreadIDVariable() const override {
380 llvm_unreachable("No thread id for expressions");
381 }
382
383 /// \brief Get the name of the capture helper.
384 StringRef getHelperName() const override {
385 llvm_unreachable("No helper name for expressions");
386 }
387
388 static bool classof(const CGCapturedStmtInfo *Info) { return false; }
389
390private:
391 /// Private scope to capture global variables.
392 CodeGenFunction::OMPPrivateScope PrivScope;
393};
394
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000395/// \brief RAII for emitting code of OpenMP constructs.
396class InlinedOpenMPRegionRAII {
397 CodeGenFunction &CGF;
Alexey Bataev4ba78a42016-04-27 07:56:03 +0000398 llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
399 FieldDecl *LambdaThisCaptureField = nullptr;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +0000400 const CodeGen::CGBlockInfo *BlockInfo = nullptr;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000401
402public:
403 /// \brief Constructs region for combined constructs.
404 /// \param CodeGen Code generation sequence for combined directives. Includes
405 /// a list of functions used for code generation of implicitly inlined
406 /// regions.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000407 InlinedOpenMPRegionRAII(CodeGenFunction &CGF, const RegionCodeGenTy &CodeGen,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000408 OpenMPDirectiveKind Kind, bool HasCancel)
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000409 : CGF(CGF) {
410 // Start emission for the construct.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000411 CGF.CapturedStmtInfo = new CGOpenMPInlinedRegionInfo(
412 CGF.CapturedStmtInfo, CodeGen, Kind, HasCancel);
Alexey Bataev4ba78a42016-04-27 07:56:03 +0000413 std::swap(CGF.LambdaCaptureFields, LambdaCaptureFields);
414 LambdaThisCaptureField = CGF.LambdaThisCaptureField;
415 CGF.LambdaThisCaptureField = nullptr;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +0000416 BlockInfo = CGF.BlockInfo;
417 CGF.BlockInfo = nullptr;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000418 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000419
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000420 ~InlinedOpenMPRegionRAII() {
421 // Restore original CapturedStmtInfo only if we're done with code emission.
422 auto *OldCSI =
423 cast<CGOpenMPInlinedRegionInfo>(CGF.CapturedStmtInfo)->getOldCSI();
424 delete CGF.CapturedStmtInfo;
425 CGF.CapturedStmtInfo = OldCSI;
Alexey Bataev4ba78a42016-04-27 07:56:03 +0000426 std::swap(CGF.LambdaCaptureFields, LambdaCaptureFields);
427 CGF.LambdaThisCaptureField = LambdaThisCaptureField;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +0000428 CGF.BlockInfo = BlockInfo;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000429 }
430};
431
Alexey Bataev50b3c952016-02-19 10:38:26 +0000432/// \brief Values for bit flags used in the ident_t to describe the fields.
433/// All enumeric elements are named and described in accordance with the code
434/// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000435enum OpenMPLocationFlags : unsigned {
Alexey Bataev50b3c952016-02-19 10:38:26 +0000436 /// \brief Use trampoline for internal microtask.
437 OMP_IDENT_IMD = 0x01,
438 /// \brief Use c-style ident structure.
439 OMP_IDENT_KMPC = 0x02,
440 /// \brief Atomic reduction option for kmpc_reduce.
441 OMP_ATOMIC_REDUCE = 0x10,
442 /// \brief Explicit 'barrier' directive.
443 OMP_IDENT_BARRIER_EXPL = 0x20,
444 /// \brief Implicit barrier in code.
445 OMP_IDENT_BARRIER_IMPL = 0x40,
446 /// \brief Implicit barrier in 'for' directive.
447 OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
448 /// \brief Implicit barrier in 'sections' directive.
449 OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
450 /// \brief Implicit barrier in 'single' directive.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000451 OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140,
452 /// Call of __kmp_for_static_init for static loop.
453 OMP_IDENT_WORK_LOOP = 0x200,
454 /// Call of __kmp_for_static_init for sections.
455 OMP_IDENT_WORK_SECTIONS = 0x400,
456 /// Call of __kmp_for_static_init for distribute.
457 OMP_IDENT_WORK_DISTRIBUTE = 0x800,
458 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/OMP_IDENT_WORK_DISTRIBUTE)
Alexey Bataev50b3c952016-02-19 10:38:26 +0000459};
460
461/// \brief Describes ident structure that describes a source location.
462/// All descriptions are taken from
463/// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
464/// Original structure:
465/// typedef struct ident {
466/// kmp_int32 reserved_1; /**< might be used in Fortran;
467/// see above */
468/// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
469/// KMP_IDENT_KMPC identifies this union
470/// member */
471/// kmp_int32 reserved_2; /**< not really used in Fortran any more;
472/// see above */
473///#if USE_ITT_BUILD
474/// /* but currently used for storing
475/// region-specific ITT */
476/// /* contextual information. */
477///#endif /* USE_ITT_BUILD */
478/// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
479/// C++ */
480/// char const *psource; /**< String describing the source location.
481/// The string is composed of semi-colon separated
482// fields which describe the source file,
483/// the function and a pair of line numbers that
484/// delimit the construct.
485/// */
486/// } ident_t;
487enum IdentFieldIndex {
488 /// \brief might be used in Fortran
489 IdentField_Reserved_1,
490 /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
491 IdentField_Flags,
492 /// \brief Not really used in Fortran any more
493 IdentField_Reserved_2,
494 /// \brief Source[4] in Fortran, do not use for C++
495 IdentField_Reserved_3,
496 /// \brief String describing the source location. The string is composed of
497 /// semi-colon separated fields which describe the source file, the function
498 /// and a pair of line numbers that delimit the construct.
499 IdentField_PSource
500};
501
502/// \brief Schedule types for 'omp for' loops (these enumerators are taken from
503/// the enum sched_type in kmp.h).
504enum OpenMPSchedType {
505 /// \brief Lower bound for default (unordered) versions.
506 OMP_sch_lower = 32,
507 OMP_sch_static_chunked = 33,
508 OMP_sch_static = 34,
509 OMP_sch_dynamic_chunked = 35,
510 OMP_sch_guided_chunked = 36,
511 OMP_sch_runtime = 37,
512 OMP_sch_auto = 38,
Alexey Bataev6cff6242016-05-30 13:05:14 +0000513 /// static with chunk adjustment (e.g., simd)
Samuel Antao4c8035b2016-12-12 18:00:20 +0000514 OMP_sch_static_balanced_chunked = 45,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000515 /// \brief Lower bound for 'ordered' versions.
516 OMP_ord_lower = 64,
517 OMP_ord_static_chunked = 65,
518 OMP_ord_static = 66,
519 OMP_ord_dynamic_chunked = 67,
520 OMP_ord_guided_chunked = 68,
521 OMP_ord_runtime = 69,
522 OMP_ord_auto = 70,
523 OMP_sch_default = OMP_sch_static,
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000524 /// \brief dist_schedule types
525 OMP_dist_sch_static_chunked = 91,
526 OMP_dist_sch_static = 92,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000527 /// Support for OpenMP 4.5 monotonic and nonmonotonic schedule modifiers.
528 /// Set if the monotonic schedule modifier was present.
529 OMP_sch_modifier_monotonic = (1 << 29),
530 /// Set if the nonmonotonic schedule modifier was present.
531 OMP_sch_modifier_nonmonotonic = (1 << 30),
Alexey Bataev50b3c952016-02-19 10:38:26 +0000532};
533
534enum OpenMPRTLFunction {
535 /// \brief Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc,
536 /// kmpc_micro microtask, ...);
537 OMPRTL__kmpc_fork_call,
538 /// \brief Call to void *__kmpc_threadprivate_cached(ident_t *loc,
539 /// kmp_int32 global_tid, void *data, size_t size, void ***cache);
540 OMPRTL__kmpc_threadprivate_cached,
541 /// \brief Call to void __kmpc_threadprivate_register( ident_t *,
542 /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
543 OMPRTL__kmpc_threadprivate_register,
544 // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
545 OMPRTL__kmpc_global_thread_num,
546 // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
547 // kmp_critical_name *crit);
548 OMPRTL__kmpc_critical,
549 // Call to void __kmpc_critical_with_hint(ident_t *loc, kmp_int32
550 // global_tid, kmp_critical_name *crit, uintptr_t hint);
551 OMPRTL__kmpc_critical_with_hint,
552 // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
553 // kmp_critical_name *crit);
554 OMPRTL__kmpc_end_critical,
555 // Call to kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32
556 // global_tid);
557 OMPRTL__kmpc_cancel_barrier,
558 // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
559 OMPRTL__kmpc_barrier,
560 // Call to void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
561 OMPRTL__kmpc_for_static_fini,
562 // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
563 // global_tid);
564 OMPRTL__kmpc_serialized_parallel,
565 // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
566 // global_tid);
567 OMPRTL__kmpc_end_serialized_parallel,
568 // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
569 // kmp_int32 num_threads);
570 OMPRTL__kmpc_push_num_threads,
571 // Call to void __kmpc_flush(ident_t *loc);
572 OMPRTL__kmpc_flush,
573 // Call to kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid);
574 OMPRTL__kmpc_master,
575 // Call to void __kmpc_end_master(ident_t *, kmp_int32 global_tid);
576 OMPRTL__kmpc_end_master,
577 // Call to kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid,
578 // int end_part);
579 OMPRTL__kmpc_omp_taskyield,
580 // Call to kmp_int32 __kmpc_single(ident_t *, kmp_int32 global_tid);
581 OMPRTL__kmpc_single,
582 // Call to void __kmpc_end_single(ident_t *, kmp_int32 global_tid);
583 OMPRTL__kmpc_end_single,
584 // Call to kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
585 // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
586 // kmp_routine_entry_t *task_entry);
587 OMPRTL__kmpc_omp_task_alloc,
588 // Call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t *
589 // new_task);
590 OMPRTL__kmpc_omp_task,
591 // Call to void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid,
592 // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *),
593 // kmp_int32 didit);
594 OMPRTL__kmpc_copyprivate,
595 // Call to kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid,
596 // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void
597 // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck);
598 OMPRTL__kmpc_reduce,
599 // Call to kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32
600 // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data,
601 // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name
602 // *lck);
603 OMPRTL__kmpc_reduce_nowait,
604 // Call to void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
605 // kmp_critical_name *lck);
606 OMPRTL__kmpc_end_reduce,
607 // Call to void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
608 // kmp_critical_name *lck);
609 OMPRTL__kmpc_end_reduce_nowait,
610 // Call to void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid,
611 // kmp_task_t * new_task);
612 OMPRTL__kmpc_omp_task_begin_if0,
613 // Call to void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid,
614 // kmp_task_t * new_task);
615 OMPRTL__kmpc_omp_task_complete_if0,
616 // Call to void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid);
617 OMPRTL__kmpc_ordered,
618 // Call to void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid);
619 OMPRTL__kmpc_end_ordered,
620 // Call to kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32
621 // global_tid);
622 OMPRTL__kmpc_omp_taskwait,
623 // Call to void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid);
624 OMPRTL__kmpc_taskgroup,
625 // Call to void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid);
626 OMPRTL__kmpc_end_taskgroup,
627 // Call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid,
628 // int proc_bind);
629 OMPRTL__kmpc_push_proc_bind,
630 // Call to kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32
631 // gtid, kmp_task_t * new_task, kmp_int32 ndeps, kmp_depend_info_t
632 // *dep_list, kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
633 OMPRTL__kmpc_omp_task_with_deps,
634 // Call to void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32
635 // gtid, kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32
636 // ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
637 OMPRTL__kmpc_omp_wait_deps,
638 // Call to kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32
639 // global_tid, kmp_int32 cncl_kind);
640 OMPRTL__kmpc_cancellationpoint,
641 // Call to kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid,
642 // kmp_int32 cncl_kind);
643 OMPRTL__kmpc_cancel,
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000644 // Call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32 global_tid,
645 // kmp_int32 num_teams, kmp_int32 thread_limit);
646 OMPRTL__kmpc_push_num_teams,
Alexey Bataev7292c292016-04-25 12:22:29 +0000647 // Call to void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro
648 // microtask, ...);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000649 OMPRTL__kmpc_fork_teams,
Alexey Bataev7292c292016-04-25 12:22:29 +0000650 // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int
651 // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int
652 // sched, kmp_uint64 grainsize, void *task_dup);
653 OMPRTL__kmpc_taskloop,
Alexey Bataev8b427062016-05-25 12:36:08 +0000654 // Call to void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32
655 // num_dims, struct kmp_dim *dims);
656 OMPRTL__kmpc_doacross_init,
657 // Call to void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid);
658 OMPRTL__kmpc_doacross_fini,
659 // Call to void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64
660 // *vec);
661 OMPRTL__kmpc_doacross_post,
662 // Call to void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64
663 // *vec);
664 OMPRTL__kmpc_doacross_wait,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000665 // Call to void *__kmpc_task_reduction_init(int gtid, int num_data, void
666 // *data);
667 OMPRTL__kmpc_task_reduction_init,
668 // Call to void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void
669 // *d);
670 OMPRTL__kmpc_task_reduction_get_th_data,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000671
672 //
673 // Offloading related calls
674 //
George Rokos63bc9d62017-11-21 18:25:12 +0000675 // Call to int32_t __tgt_target(int64_t device_id, void *host_ptr, int32_t
676 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
Alexey Bataev50b3c952016-02-19 10:38:26 +0000677 // *arg_types);
678 OMPRTL__tgt_target,
Alexey Bataeva9f77c62017-12-13 21:04:20 +0000679 // Call to int32_t __tgt_target_nowait(int64_t device_id, void *host_ptr,
680 // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
681 // *arg_types);
682 OMPRTL__tgt_target_nowait,
George Rokos63bc9d62017-11-21 18:25:12 +0000683 // Call to int32_t __tgt_target_teams(int64_t device_id, void *host_ptr,
684 // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
685 // *arg_types, int32_t num_teams, int32_t thread_limit);
Samuel Antaob68e2db2016-03-03 16:20:23 +0000686 OMPRTL__tgt_target_teams,
Alexey Bataeva9f77c62017-12-13 21:04:20 +0000687 // Call to int32_t __tgt_target_teams_nowait(int64_t device_id, void
688 // *host_ptr, int32_t arg_num, void** args_base, void **args, size_t
689 // *arg_sizes, int64_t *arg_types, int32_t num_teams, int32_t thread_limit);
690 OMPRTL__tgt_target_teams_nowait,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000691 // Call to void __tgt_register_lib(__tgt_bin_desc *desc);
692 OMPRTL__tgt_register_lib,
693 // Call to void __tgt_unregister_lib(__tgt_bin_desc *desc);
694 OMPRTL__tgt_unregister_lib,
George Rokos63bc9d62017-11-21 18:25:12 +0000695 // Call to void __tgt_target_data_begin(int64_t device_id, int32_t arg_num,
696 // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types);
Samuel Antaodf158d52016-04-27 22:58:19 +0000697 OMPRTL__tgt_target_data_begin,
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +0000698 // Call to void __tgt_target_data_begin_nowait(int64_t device_id, int32_t
699 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
700 // *arg_types);
701 OMPRTL__tgt_target_data_begin_nowait,
George Rokos63bc9d62017-11-21 18:25:12 +0000702 // Call to void __tgt_target_data_end(int64_t device_id, int32_t arg_num,
703 // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types);
Samuel Antaodf158d52016-04-27 22:58:19 +0000704 OMPRTL__tgt_target_data_end,
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +0000705 // Call to void __tgt_target_data_end_nowait(int64_t device_id, int32_t
706 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
707 // *arg_types);
708 OMPRTL__tgt_target_data_end_nowait,
George Rokos63bc9d62017-11-21 18:25:12 +0000709 // Call to void __tgt_target_data_update(int64_t device_id, int32_t arg_num,
710 // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types);
Samuel Antao8d2d7302016-05-26 18:30:22 +0000711 OMPRTL__tgt_target_data_update,
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +0000712 // Call to void __tgt_target_data_update_nowait(int64_t device_id, int32_t
713 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
714 // *arg_types);
715 OMPRTL__tgt_target_data_update_nowait,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000716};
717
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000718/// A basic class for pre|post-action for advanced codegen sequence for OpenMP
719/// region.
720class CleanupTy final : public EHScopeStack::Cleanup {
721 PrePostActionTy *Action;
722
723public:
724 explicit CleanupTy(PrePostActionTy *Action) : Action(Action) {}
725 void Emit(CodeGenFunction &CGF, Flags /*flags*/) override {
726 if (!CGF.HaveInsertPoint())
727 return;
728 Action->Exit(CGF);
729 }
730};
731
Hans Wennborg7eb54642015-09-10 17:07:54 +0000732} // anonymous namespace
Alexey Bataev18095712014-10-10 12:19:54 +0000733
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000734void RegionCodeGenTy::operator()(CodeGenFunction &CGF) const {
735 CodeGenFunction::RunCleanupsScope Scope(CGF);
736 if (PrePostAction) {
737 CGF.EHStack.pushCleanup<CleanupTy>(NormalAndEHCleanup, PrePostAction);
738 Callback(CodeGen, CGF, *PrePostAction);
739 } else {
740 PrePostActionTy Action;
741 Callback(CodeGen, CGF, Action);
742 }
743}
744
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000745/// Check if the combiner is a call to UDR combiner and if it is so return the
746/// UDR decl used for reduction.
747static const OMPDeclareReductionDecl *
748getReductionInit(const Expr *ReductionOp) {
749 if (auto *CE = dyn_cast<CallExpr>(ReductionOp))
750 if (auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee()))
751 if (auto *DRE =
752 dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts()))
753 if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl()))
754 return DRD;
755 return nullptr;
756}
757
758static void emitInitWithReductionInitializer(CodeGenFunction &CGF,
759 const OMPDeclareReductionDecl *DRD,
760 const Expr *InitOp,
761 Address Private, Address Original,
762 QualType Ty) {
763 if (DRD->getInitializer()) {
764 std::pair<llvm::Function *, llvm::Function *> Reduction =
765 CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD);
766 auto *CE = cast<CallExpr>(InitOp);
767 auto *OVE = cast<OpaqueValueExpr>(CE->getCallee());
768 const Expr *LHS = CE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
769 const Expr *RHS = CE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
770 auto *LHSDRE = cast<DeclRefExpr>(cast<UnaryOperator>(LHS)->getSubExpr());
771 auto *RHSDRE = cast<DeclRefExpr>(cast<UnaryOperator>(RHS)->getSubExpr());
772 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
773 PrivateScope.addPrivate(cast<VarDecl>(LHSDRE->getDecl()),
774 [=]() -> Address { return Private; });
775 PrivateScope.addPrivate(cast<VarDecl>(RHSDRE->getDecl()),
776 [=]() -> Address { return Original; });
777 (void)PrivateScope.Privatize();
778 RValue Func = RValue::get(Reduction.second);
779 CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func);
780 CGF.EmitIgnoredExpr(InitOp);
781 } else {
782 llvm::Constant *Init = CGF.CGM.EmitNullConstant(Ty);
783 auto *GV = new llvm::GlobalVariable(
784 CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true,
785 llvm::GlobalValue::PrivateLinkage, Init, ".init");
786 LValue LV = CGF.MakeNaturalAlignAddrLValue(GV, Ty);
787 RValue InitRVal;
788 switch (CGF.getEvaluationKind(Ty)) {
789 case TEK_Scalar:
Alexey Bataeva9b9cc02018-01-23 18:12:38 +0000790 InitRVal = CGF.EmitLoadOfLValue(LV, DRD->getLocation());
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000791 break;
792 case TEK_Complex:
793 InitRVal =
Alexey Bataeva9b9cc02018-01-23 18:12:38 +0000794 RValue::getComplex(CGF.EmitLoadOfComplex(LV, DRD->getLocation()));
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000795 break;
796 case TEK_Aggregate:
797 InitRVal = RValue::getAggregate(LV.getAddress());
798 break;
799 }
Alexey Bataeva9b9cc02018-01-23 18:12:38 +0000800 OpaqueValueExpr OVE(DRD->getLocation(), Ty, VK_RValue);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000801 CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE, InitRVal);
802 CGF.EmitAnyExprToMem(&OVE, Private, Ty.getQualifiers(),
803 /*IsInitializer=*/false);
804 }
805}
806
807/// \brief Emit initialization of arrays of complex types.
808/// \param DestAddr Address of the array.
809/// \param Type Type of array.
810/// \param Init Initial expression of array.
811/// \param SrcAddr Address of the original array.
812static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr,
Alexey Bataeva7b19152017-10-12 20:03:39 +0000813 QualType Type, bool EmitDeclareReductionInit,
814 const Expr *Init,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000815 const OMPDeclareReductionDecl *DRD,
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000816 Address SrcAddr = Address::invalid()) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000817 // Perform element-by-element initialization.
818 QualType ElementTy;
819
820 // Drill down to the base element type on both arrays.
821 auto ArrayTy = Type->getAsArrayTypeUnsafe();
822 auto NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr);
823 DestAddr =
824 CGF.Builder.CreateElementBitCast(DestAddr, DestAddr.getElementType());
825 if (DRD)
826 SrcAddr =
827 CGF.Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
828
829 llvm::Value *SrcBegin = nullptr;
830 if (DRD)
831 SrcBegin = SrcAddr.getPointer();
832 auto DestBegin = DestAddr.getPointer();
833 // Cast from pointer to array type to pointer to single element.
834 auto DestEnd = CGF.Builder.CreateGEP(DestBegin, NumElements);
835 // The basic structure here is a while-do loop.
836 auto BodyBB = CGF.createBasicBlock("omp.arrayinit.body");
837 auto DoneBB = CGF.createBasicBlock("omp.arrayinit.done");
838 auto IsEmpty =
839 CGF.Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arrayinit.isempty");
840 CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
841
842 // Enter the loop body, making that address the current address.
843 auto EntryBB = CGF.Builder.GetInsertBlock();
844 CGF.EmitBlock(BodyBB);
845
846 CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy);
847
848 llvm::PHINode *SrcElementPHI = nullptr;
849 Address SrcElementCurrent = Address::invalid();
850 if (DRD) {
851 SrcElementPHI = CGF.Builder.CreatePHI(SrcBegin->getType(), 2,
852 "omp.arraycpy.srcElementPast");
853 SrcElementPHI->addIncoming(SrcBegin, EntryBB);
854 SrcElementCurrent =
855 Address(SrcElementPHI,
856 SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize));
857 }
858 llvm::PHINode *DestElementPHI = CGF.Builder.CreatePHI(
859 DestBegin->getType(), 2, "omp.arraycpy.destElementPast");
860 DestElementPHI->addIncoming(DestBegin, EntryBB);
861 Address DestElementCurrent =
862 Address(DestElementPHI,
863 DestAddr.getAlignment().alignmentOfArrayElement(ElementSize));
864
865 // Emit copy.
866 {
867 CodeGenFunction::RunCleanupsScope InitScope(CGF);
Alexey Bataeva7b19152017-10-12 20:03:39 +0000868 if (EmitDeclareReductionInit) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000869 emitInitWithReductionInitializer(CGF, DRD, Init, DestElementCurrent,
870 SrcElementCurrent, ElementTy);
871 } else
872 CGF.EmitAnyExprToMem(Init, DestElementCurrent, ElementTy.getQualifiers(),
873 /*IsInitializer=*/false);
874 }
875
876 if (DRD) {
877 // Shift the address forward by one element.
878 auto SrcElementNext = CGF.Builder.CreateConstGEP1_32(
879 SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
880 SrcElementPHI->addIncoming(SrcElementNext, CGF.Builder.GetInsertBlock());
881 }
882
883 // Shift the address forward by one element.
884 auto DestElementNext = CGF.Builder.CreateConstGEP1_32(
885 DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
886 // Check whether we've reached the end.
887 auto Done =
888 CGF.Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
889 CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB);
890 DestElementPHI->addIncoming(DestElementNext, CGF.Builder.GetInsertBlock());
891
892 // Done.
893 CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
894}
895
Alexey Bataev92327c52018-03-26 16:40:55 +0000896static llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy>
897isDeclareTargetDeclaration(const ValueDecl *VD) {
898 for (const auto *D : VD->redecls()) {
899 if (!D->hasAttrs())
900 continue;
901 if (const auto *Attr = D->getAttr<OMPDeclareTargetDeclAttr>())
902 return Attr->getMapType();
903 }
904 return llvm::None;
905}
906
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000907LValue ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, const Expr *E) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +0000908 return CGF.EmitOMPSharedLValue(E);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000909}
910
911LValue ReductionCodeGen::emitSharedLValueUB(CodeGenFunction &CGF,
912 const Expr *E) {
913 if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(E))
914 return CGF.EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false);
915 return LValue();
916}
917
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000918void ReductionCodeGen::emitAggregateInitialization(
919 CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal,
920 const OMPDeclareReductionDecl *DRD) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000921 // Emit VarDecl with copy init for arrays.
922 // Get the address of the original variable captured in current
923 // captured region.
924 auto *PrivateVD =
925 cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
Alexey Bataeva7b19152017-10-12 20:03:39 +0000926 bool EmitDeclareReductionInit =
927 DRD && (DRD->getInitializer() || !PrivateVD->hasInit());
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000928 EmitOMPAggregateInit(CGF, PrivateAddr, PrivateVD->getType(),
Alexey Bataeva7b19152017-10-12 20:03:39 +0000929 EmitDeclareReductionInit,
930 EmitDeclareReductionInit ? ClausesData[N].ReductionOp
931 : PrivateVD->getInit(),
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000932 DRD, SharedLVal.getAddress());
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000933}
934
935ReductionCodeGen::ReductionCodeGen(ArrayRef<const Expr *> Shareds,
936 ArrayRef<const Expr *> Privates,
937 ArrayRef<const Expr *> ReductionOps) {
938 ClausesData.reserve(Shareds.size());
939 SharedAddresses.reserve(Shareds.size());
940 Sizes.reserve(Shareds.size());
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000941 BaseDecls.reserve(Shareds.size());
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000942 auto IPriv = Privates.begin();
943 auto IRed = ReductionOps.begin();
944 for (const auto *Ref : Shareds) {
945 ClausesData.emplace_back(Ref, *IPriv, *IRed);
946 std::advance(IPriv, 1);
947 std::advance(IRed, 1);
948 }
949}
950
951void ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, unsigned N) {
952 assert(SharedAddresses.size() == N &&
953 "Number of generated lvalues must be exactly N.");
Jonas Hahnfeld4525c822017-10-23 19:01:35 +0000954 LValue First = emitSharedLValue(CGF, ClausesData[N].Ref);
955 LValue Second = emitSharedLValueUB(CGF, ClausesData[N].Ref);
956 SharedAddresses.emplace_back(First, Second);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000957}
958
959void ReductionCodeGen::emitAggregateType(CodeGenFunction &CGF, unsigned N) {
960 auto *PrivateVD =
961 cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
962 QualType PrivateType = PrivateVD->getType();
963 bool AsArraySection = isa<OMPArraySectionExpr>(ClausesData[N].Ref);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +0000964 if (!PrivateType->isVariablyModifiedType()) {
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000965 Sizes.emplace_back(
966 CGF.getTypeSize(
967 SharedAddresses[N].first.getType().getNonReferenceType()),
968 nullptr);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000969 return;
970 }
971 llvm::Value *Size;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000972 llvm::Value *SizeInChars;
973 llvm::Type *ElemType =
974 cast<llvm::PointerType>(SharedAddresses[N].first.getPointer()->getType())
975 ->getElementType();
976 auto *ElemSizeOf = llvm::ConstantExpr::getSizeOf(ElemType);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000977 if (AsArraySection) {
978 Size = CGF.Builder.CreatePtrDiff(SharedAddresses[N].second.getPointer(),
979 SharedAddresses[N].first.getPointer());
980 Size = CGF.Builder.CreateNUWAdd(
981 Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1));
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000982 SizeInChars = CGF.Builder.CreateNUWMul(Size, ElemSizeOf);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000983 } else {
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000984 SizeInChars = CGF.getTypeSize(
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000985 SharedAddresses[N].first.getType().getNonReferenceType());
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000986 Size = CGF.Builder.CreateExactUDiv(SizeInChars, ElemSizeOf);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000987 }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000988 Sizes.emplace_back(SizeInChars, Size);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000989 CodeGenFunction::OpaqueValueMapping OpaqueMap(
990 CGF,
991 cast<OpaqueValueExpr>(
992 CGF.getContext().getAsVariableArrayType(PrivateType)->getSizeExpr()),
993 RValue::get(Size));
994 CGF.EmitVariablyModifiedType(PrivateType);
995}
996
997void ReductionCodeGen::emitAggregateType(CodeGenFunction &CGF, unsigned N,
998 llvm::Value *Size) {
999 auto *PrivateVD =
1000 cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
1001 QualType PrivateType = PrivateVD->getType();
Jonas Hahnfeld4525c822017-10-23 19:01:35 +00001002 if (!PrivateType->isVariablyModifiedType()) {
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001003 assert(!Size && !Sizes[N].second &&
Jonas Hahnfeld4525c822017-10-23 19:01:35 +00001004 "Size should be nullptr for non-variably modified reduction "
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001005 "items.");
1006 return;
1007 }
1008 CodeGenFunction::OpaqueValueMapping OpaqueMap(
1009 CGF,
1010 cast<OpaqueValueExpr>(
1011 CGF.getContext().getAsVariableArrayType(PrivateType)->getSizeExpr()),
1012 RValue::get(Size));
1013 CGF.EmitVariablyModifiedType(PrivateType);
1014}
1015
1016void ReductionCodeGen::emitInitialization(
1017 CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal,
1018 llvm::function_ref<bool(CodeGenFunction &)> DefaultInit) {
1019 assert(SharedAddresses.size() > N && "No variable was generated");
1020 auto *PrivateVD =
1021 cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
1022 auto *DRD = getReductionInit(ClausesData[N].ReductionOp);
1023 QualType PrivateType = PrivateVD->getType();
1024 PrivateAddr = CGF.Builder.CreateElementBitCast(
1025 PrivateAddr, CGF.ConvertTypeForMem(PrivateType));
1026 QualType SharedType = SharedAddresses[N].first.getType();
1027 SharedLVal = CGF.MakeAddrLValue(
1028 CGF.Builder.CreateElementBitCast(SharedLVal.getAddress(),
1029 CGF.ConvertTypeForMem(SharedType)),
Ivan A. Kosarevf5f20462017-10-12 11:29:46 +00001030 SharedType, SharedAddresses[N].first.getBaseInfo(),
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +00001031 CGF.CGM.getTBAAInfoForSubobject(SharedAddresses[N].first, SharedType));
Jonas Hahnfeld4525c822017-10-23 19:01:35 +00001032 if (CGF.getContext().getAsArrayType(PrivateVD->getType())) {
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001033 emitAggregateInitialization(CGF, N, PrivateAddr, SharedLVal, DRD);
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001034 } else if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) {
1035 emitInitWithReductionInitializer(CGF, DRD, ClausesData[N].ReductionOp,
1036 PrivateAddr, SharedLVal.getAddress(),
1037 SharedLVal.getType());
1038 } else if (!DefaultInit(CGF) && PrivateVD->hasInit() &&
1039 !CGF.isTrivialInitializer(PrivateVD->getInit())) {
1040 CGF.EmitAnyExprToMem(PrivateVD->getInit(), PrivateAddr,
1041 PrivateVD->getType().getQualifiers(),
1042 /*IsInitializer=*/false);
1043 }
1044}
1045
1046bool ReductionCodeGen::needCleanups(unsigned N) {
1047 auto *PrivateVD =
1048 cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
1049 QualType PrivateType = PrivateVD->getType();
1050 QualType::DestructionKind DTorKind = PrivateType.isDestructedType();
1051 return DTorKind != QualType::DK_none;
1052}
1053
1054void ReductionCodeGen::emitCleanups(CodeGenFunction &CGF, unsigned N,
1055 Address PrivateAddr) {
1056 auto *PrivateVD =
1057 cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
1058 QualType PrivateType = PrivateVD->getType();
1059 QualType::DestructionKind DTorKind = PrivateType.isDestructedType();
1060 if (needCleanups(N)) {
1061 PrivateAddr = CGF.Builder.CreateElementBitCast(
1062 PrivateAddr, CGF.ConvertTypeForMem(PrivateType));
1063 CGF.pushDestroy(DTorKind, PrivateAddr, PrivateType);
1064 }
1065}
1066
1067static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
1068 LValue BaseLV) {
1069 BaseTy = BaseTy.getNonReferenceType();
1070 while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) &&
1071 !CGF.getContext().hasSameType(BaseTy, ElTy)) {
1072 if (auto *PtrTy = BaseTy->getAs<PointerType>())
1073 BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy);
1074 else {
Ivan A. Kosarev9f9d1572017-10-30 11:49:31 +00001075 LValue RefLVal = CGF.MakeAddrLValue(BaseLV.getAddress(), BaseTy);
1076 BaseLV = CGF.EmitLoadOfReferenceLValue(RefLVal);
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001077 }
1078 BaseTy = BaseTy->getPointeeType();
1079 }
1080 return CGF.MakeAddrLValue(
1081 CGF.Builder.CreateElementBitCast(BaseLV.getAddress(),
1082 CGF.ConvertTypeForMem(ElTy)),
Ivan A. Kosarevf5f20462017-10-12 11:29:46 +00001083 BaseLV.getType(), BaseLV.getBaseInfo(),
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +00001084 CGF.CGM.getTBAAInfoForSubobject(BaseLV, BaseLV.getType()));
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001085}
1086
1087static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
1088 llvm::Type *BaseLVType, CharUnits BaseLVAlignment,
1089 llvm::Value *Addr) {
1090 Address Tmp = Address::invalid();
1091 Address TopTmp = Address::invalid();
1092 Address MostTopTmp = Address::invalid();
1093 BaseTy = BaseTy.getNonReferenceType();
1094 while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) &&
1095 !CGF.getContext().hasSameType(BaseTy, ElTy)) {
1096 Tmp = CGF.CreateMemTemp(BaseTy);
1097 if (TopTmp.isValid())
1098 CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp);
1099 else
1100 MostTopTmp = Tmp;
1101 TopTmp = Tmp;
1102 BaseTy = BaseTy->getPointeeType();
1103 }
1104 llvm::Type *Ty = BaseLVType;
1105 if (Tmp.isValid())
1106 Ty = Tmp.getElementType();
1107 Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty);
1108 if (Tmp.isValid()) {
1109 CGF.Builder.CreateStore(Addr, Tmp);
1110 return MostTopTmp;
1111 }
1112 return Address(Addr, BaseLVAlignment);
1113}
1114
Alexey Bataev1c44e152018-03-06 18:59:43 +00001115static const VarDecl *getBaseDecl(const Expr *Ref, const DeclRefExpr *&DE) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001116 const VarDecl *OrigVD = nullptr;
Alexey Bataev1c44e152018-03-06 18:59:43 +00001117 if (auto *OASE = dyn_cast<OMPArraySectionExpr>(Ref)) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001118 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
1119 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
1120 Base = TempOASE->getBase()->IgnoreParenImpCasts();
1121 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
1122 Base = TempASE->getBase()->IgnoreParenImpCasts();
1123 DE = cast<DeclRefExpr>(Base);
1124 OrigVD = cast<VarDecl>(DE->getDecl());
Alexey Bataev1c44e152018-03-06 18:59:43 +00001125 } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Ref)) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001126 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
1127 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
1128 Base = TempASE->getBase()->IgnoreParenImpCasts();
1129 DE = cast<DeclRefExpr>(Base);
1130 OrigVD = cast<VarDecl>(DE->getDecl());
1131 }
Alexey Bataev1c44e152018-03-06 18:59:43 +00001132 return OrigVD;
1133}
1134
1135Address ReductionCodeGen::adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
1136 Address PrivateAddr) {
1137 const DeclRefExpr *DE;
1138 if (const VarDecl *OrigVD = ::getBaseDecl(ClausesData[N].Ref, DE)) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001139 BaseDecls.emplace_back(OrigVD);
1140 auto OriginalBaseLValue = CGF.EmitLValue(DE);
1141 LValue BaseLValue =
1142 loadToBegin(CGF, OrigVD->getType(), SharedAddresses[N].first.getType(),
1143 OriginalBaseLValue);
1144 llvm::Value *Adjustment = CGF.Builder.CreatePtrDiff(
1145 BaseLValue.getPointer(), SharedAddresses[N].first.getPointer());
Jonas Hahnfeld273d2612017-12-06 19:15:28 +00001146 llvm::Value *PrivatePointer =
1147 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
1148 PrivateAddr.getPointer(),
1149 SharedAddresses[N].first.getAddress().getType());
1150 llvm::Value *Ptr = CGF.Builder.CreateGEP(PrivatePointer, Adjustment);
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001151 return castToBase(CGF, OrigVD->getType(),
1152 SharedAddresses[N].first.getType(),
Jonas Hahnfeld273d2612017-12-06 19:15:28 +00001153 OriginalBaseLValue.getAddress().getType(),
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001154 OriginalBaseLValue.getAlignment(), Ptr);
1155 }
1156 BaseDecls.emplace_back(
1157 cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Ref)->getDecl()));
1158 return PrivateAddr;
1159}
1160
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001161bool ReductionCodeGen::usesReductionInitializer(unsigned N) const {
1162 auto *DRD = getReductionInit(ClausesData[N].ReductionOp);
1163 return DRD && DRD->getInitializer();
1164}
1165
Alexey Bataev18095712014-10-10 12:19:54 +00001166LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) {
Alexey Bataev31300ed2016-02-04 11:27:03 +00001167 return CGF.EmitLoadOfPointerLValue(
1168 CGF.GetAddrOfLocalVar(getThreadIDVariable()),
1169 getThreadIDVariable()->getType()->castAs<PointerType>());
Alexey Bataev18095712014-10-10 12:19:54 +00001170}
1171
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001172void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, const Stmt * /*S*/) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001173 if (!CGF.HaveInsertPoint())
1174 return;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001175 // 1.2.2 OpenMP Language Terminology
1176 // Structured block - An executable statement with a single entry at the
1177 // top and a single exit at the bottom.
1178 // The point of exit cannot be a branch out of the structured block.
1179 // longjmp() and throw() must not violate the entry/exit criteria.
1180 CGF.EHStack.pushTerminate();
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001181 CodeGen(CGF);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001182 CGF.EHStack.popTerminate();
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001183}
1184
Alexey Bataev62b63b12015-03-10 07:28:44 +00001185LValue CGOpenMPTaskOutlinedRegionInfo::getThreadIDVariableLValue(
1186 CodeGenFunction &CGF) {
Alexey Bataev2377fe92015-09-10 08:12:02 +00001187 return CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(getThreadIDVariable()),
1188 getThreadIDVariable()->getType(),
Ivan A. Kosarev5f8c0ca2017-10-10 09:39:32 +00001189 AlignmentSource::Decl);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001190}
1191
Alexey Bataev9959db52014-05-06 10:08:46 +00001192CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM)
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001193 : CGM(CGM), OffloadEntriesInfoManager(CGM) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001194 IdentTy = llvm::StructType::create(
1195 "ident_t", CGM.Int32Ty /* reserved_1 */, CGM.Int32Ty /* flags */,
1196 CGM.Int32Ty /* reserved_2 */, CGM.Int32Ty /* reserved_3 */,
Serge Guelton1d993272017-05-09 19:31:30 +00001197 CGM.Int8PtrTy /* psource */);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001198 KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001199
1200 loadOffloadInfoMetadata();
Alexey Bataev9959db52014-05-06 10:08:46 +00001201}
1202
Alexey Bataev91797552015-03-18 04:13:55 +00001203void CGOpenMPRuntime::clear() {
1204 InternalVars.clear();
1205}
1206
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001207static llvm::Function *
1208emitCombinerOrInitializer(CodeGenModule &CGM, QualType Ty,
1209 const Expr *CombinerInitializer, const VarDecl *In,
1210 const VarDecl *Out, bool IsCombiner) {
1211 // void .omp_combiner.(Ty *in, Ty *out);
1212 auto &C = CGM.getContext();
1213 QualType PtrTy = C.getPointerType(Ty).withRestrict();
1214 FunctionArgList Args;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001215 ImplicitParamDecl OmpOutParm(C, /*DC=*/nullptr, Out->getLocation(),
Alexey Bataev56223232017-06-09 13:40:18 +00001216 /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001217 ImplicitParamDecl OmpInParm(C, /*DC=*/nullptr, In->getLocation(),
Alexey Bataev56223232017-06-09 13:40:18 +00001218 /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other);
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001219 Args.push_back(&OmpOutParm);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001220 Args.push_back(&OmpInParm);
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001221 auto &FnInfo =
John McCallc56a8b32016-03-11 04:30:31 +00001222 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001223 auto *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
1224 auto *Fn = llvm::Function::Create(
1225 FnTy, llvm::GlobalValue::InternalLinkage,
1226 IsCombiner ? ".omp_combiner." : ".omp_initializer.", &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00001227 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo);
Chandler Carruthfcd33142016-12-23 01:24:49 +00001228 Fn->removeFnAttr(llvm::Attribute::NoInline);
Mehdi Amini6aa9e9b2017-05-29 05:38:20 +00001229 Fn->removeFnAttr(llvm::Attribute::OptimizeNone);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001230 Fn->addFnAttr(llvm::Attribute::AlwaysInline);
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001231 CodeGenFunction CGF(CGM);
1232 // Map "T omp_in;" variable to "*omp_in_parm" value in all expressions.
1233 // Map "T omp_out;" variable to "*omp_out_parm" value in all expressions.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00001234 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, In->getLocation(),
1235 Out->getLocation());
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001236 CodeGenFunction::OMPPrivateScope Scope(CGF);
1237 Address AddrIn = CGF.GetAddrOfLocalVar(&OmpInParm);
1238 Scope.addPrivate(In, [&CGF, AddrIn, PtrTy]() -> Address {
1239 return CGF.EmitLoadOfPointerLValue(AddrIn, PtrTy->castAs<PointerType>())
1240 .getAddress();
1241 });
1242 Address AddrOut = CGF.GetAddrOfLocalVar(&OmpOutParm);
1243 Scope.addPrivate(Out, [&CGF, AddrOut, PtrTy]() -> Address {
1244 return CGF.EmitLoadOfPointerLValue(AddrOut, PtrTy->castAs<PointerType>())
1245 .getAddress();
1246 });
1247 (void)Scope.Privatize();
Alexey Bataev070f43a2017-09-06 14:49:58 +00001248 if (!IsCombiner && Out->hasInit() &&
1249 !CGF.isTrivialInitializer(Out->getInit())) {
1250 CGF.EmitAnyExprToMem(Out->getInit(), CGF.GetAddrOfLocalVar(Out),
1251 Out->getType().getQualifiers(),
1252 /*IsInitializer=*/true);
1253 }
1254 if (CombinerInitializer)
1255 CGF.EmitIgnoredExpr(CombinerInitializer);
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001256 Scope.ForceCleanup();
1257 CGF.FinishFunction();
1258 return Fn;
1259}
1260
1261void CGOpenMPRuntime::emitUserDefinedReduction(
1262 CodeGenFunction *CGF, const OMPDeclareReductionDecl *D) {
1263 if (UDRMap.count(D) > 0)
1264 return;
1265 auto &C = CGM.getContext();
1266 if (!In || !Out) {
1267 In = &C.Idents.get("omp_in");
1268 Out = &C.Idents.get("omp_out");
1269 }
1270 llvm::Function *Combiner = emitCombinerOrInitializer(
1271 CGM, D->getType(), D->getCombiner(), cast<VarDecl>(D->lookup(In).front()),
1272 cast<VarDecl>(D->lookup(Out).front()),
1273 /*IsCombiner=*/true);
1274 llvm::Function *Initializer = nullptr;
1275 if (auto *Init = D->getInitializer()) {
1276 if (!Priv || !Orig) {
1277 Priv = &C.Idents.get("omp_priv");
1278 Orig = &C.Idents.get("omp_orig");
1279 }
1280 Initializer = emitCombinerOrInitializer(
Alexey Bataev070f43a2017-09-06 14:49:58 +00001281 CGM, D->getType(),
1282 D->getInitializerKind() == OMPDeclareReductionDecl::CallInit ? Init
1283 : nullptr,
1284 cast<VarDecl>(D->lookup(Orig).front()),
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001285 cast<VarDecl>(D->lookup(Priv).front()),
1286 /*IsCombiner=*/false);
1287 }
1288 UDRMap.insert(std::make_pair(D, std::make_pair(Combiner, Initializer)));
1289 if (CGF) {
1290 auto &Decls = FunctionUDRMap.FindAndConstruct(CGF->CurFn);
1291 Decls.second.push_back(D);
1292 }
1293}
1294
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001295std::pair<llvm::Function *, llvm::Function *>
1296CGOpenMPRuntime::getUserDefinedReduction(const OMPDeclareReductionDecl *D) {
1297 auto I = UDRMap.find(D);
1298 if (I != UDRMap.end())
1299 return I->second;
1300 emitUserDefinedReduction(/*CGF=*/nullptr, D);
1301 return UDRMap.lookup(D);
1302}
1303
John McCall7f416cc2015-09-08 08:05:57 +00001304// Layout information for ident_t.
1305static CharUnits getIdentAlign(CodeGenModule &CGM) {
1306 return CGM.getPointerAlign();
1307}
1308static CharUnits getIdentSize(CodeGenModule &CGM) {
1309 assert((4 * CGM.getPointerSize()).isMultipleOf(CGM.getPointerAlign()));
1310 return CharUnits::fromQuantity(16) + CGM.getPointerSize();
1311}
Alexey Bataev50b3c952016-02-19 10:38:26 +00001312static CharUnits getOffsetOfIdentField(IdentFieldIndex Field) {
John McCall7f416cc2015-09-08 08:05:57 +00001313 // All the fields except the last are i32, so this works beautifully.
1314 return unsigned(Field) * CharUnits::fromQuantity(4);
1315}
1316static Address createIdentFieldGEP(CodeGenFunction &CGF, Address Addr,
Alexey Bataev50b3c952016-02-19 10:38:26 +00001317 IdentFieldIndex Field,
John McCall7f416cc2015-09-08 08:05:57 +00001318 const llvm::Twine &Name = "") {
1319 auto Offset = getOffsetOfIdentField(Field);
1320 return CGF.Builder.CreateStructGEP(Addr, Field, Offset, Name);
1321}
1322
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00001323static llvm::Value *emitParallelOrTeamsOutlinedFunction(
1324 CodeGenModule &CGM, const OMPExecutableDirective &D, const CapturedStmt *CS,
1325 const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind,
1326 const StringRef OutlinedHelperName, const RegionCodeGenTy &CodeGen) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001327 assert(ThreadIDVar->getType()->isPointerType() &&
1328 "thread id variable must be of type kmp_int32 *");
Alexey Bataev18095712014-10-10 12:19:54 +00001329 CodeGenFunction CGF(CGM, true);
Alexey Bataev25e5b442015-09-15 12:52:43 +00001330 bool HasCancel = false;
1331 if (auto *OPD = dyn_cast<OMPParallelDirective>(&D))
1332 HasCancel = OPD->hasCancel();
1333 else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&D))
1334 HasCancel = OPSD->hasCancel();
1335 else if (auto *OPFD = dyn_cast<OMPParallelForDirective>(&D))
1336 HasCancel = OPFD->hasCancel();
Alexey Bataev2139ed62017-11-16 18:20:21 +00001337 else if (auto *OPFD = dyn_cast<OMPTargetParallelForDirective>(&D))
1338 HasCancel = OPFD->hasCancel();
Alexey Bataev10a54312017-11-27 16:54:08 +00001339 else if (auto *OPFD = dyn_cast<OMPDistributeParallelForDirective>(&D))
1340 HasCancel = OPFD->hasCancel();
1341 else if (auto *OPFD = dyn_cast<OMPTeamsDistributeParallelForDirective>(&D))
1342 HasCancel = OPFD->hasCancel();
1343 else if (auto *OPFD =
1344 dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&D))
1345 HasCancel = OPFD->hasCancel();
Alexey Bataev25e5b442015-09-15 12:52:43 +00001346 CGOpenMPOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, InnermostKind,
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00001347 HasCancel, OutlinedHelperName);
Alexey Bataevd157d472015-06-24 03:35:38 +00001348 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001349 return CGF.GenerateOpenMPCapturedStmtFunction(*CS);
Alexey Bataev18095712014-10-10 12:19:54 +00001350}
1351
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00001352llvm::Value *CGOpenMPRuntime::emitParallelOutlinedFunction(
1353 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1354 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
1355 const CapturedStmt *CS = D.getCapturedStmt(OMPD_parallel);
1356 return emitParallelOrTeamsOutlinedFunction(
1357 CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen);
1358}
1359
1360llvm::Value *CGOpenMPRuntime::emitTeamsOutlinedFunction(
1361 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1362 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
1363 const CapturedStmt *CS = D.getCapturedStmt(OMPD_teams);
1364 return emitParallelOrTeamsOutlinedFunction(
1365 CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen);
1366}
1367
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001368llvm::Value *CGOpenMPRuntime::emitTaskOutlinedFunction(
1369 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +00001370 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1371 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1372 bool Tied, unsigned &NumberOfParts) {
1373 auto &&UntiedCodeGen = [this, &D, TaskTVar](CodeGenFunction &CGF,
1374 PrePostActionTy &) {
1375 auto *ThreadID = getThreadID(CGF, D.getLocStart());
1376 auto *UpLoc = emitUpdateLocation(CGF, D.getLocStart());
1377 llvm::Value *TaskArgs[] = {
1378 UpLoc, ThreadID,
1379 CGF.EmitLoadOfPointerLValue(CGF.GetAddrOfLocalVar(TaskTVar),
1380 TaskTVar->getType()->castAs<PointerType>())
1381 .getPointer()};
1382 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), TaskArgs);
1383 };
1384 CGOpenMPTaskOutlinedRegionInfo::UntiedTaskActionTy Action(Tied, PartIDVar,
1385 UntiedCodeGen);
1386 CodeGen.setAction(Action);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001387 assert(!ThreadIDVar->getType()->isPointerType() &&
1388 "thread id variable must be of type kmp_int32 for tasks");
Alexey Bataev475a7442018-01-12 19:39:11 +00001389 const OpenMPDirectiveKind Region =
1390 isOpenMPTaskLoopDirective(D.getDirectiveKind()) ? OMPD_taskloop
1391 : OMPD_task;
1392 auto *CS = D.getCapturedStmt(Region);
Alexey Bataev7292c292016-04-25 12:22:29 +00001393 auto *TD = dyn_cast<OMPTaskDirective>(&D);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001394 CodeGenFunction CGF(CGM, true);
Alexey Bataev7292c292016-04-25 12:22:29 +00001395 CGOpenMPTaskOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen,
1396 InnermostKind,
1397 TD ? TD->hasCancel() : false, Action);
Alexey Bataevd157d472015-06-24 03:35:38 +00001398 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
Alexey Bataev48591dd2016-04-20 04:01:36 +00001399 auto *Res = CGF.GenerateCapturedStmtFunction(*CS);
1400 if (!Tied)
1401 NumberOfParts = Action.getNumberOfParts();
1402 return Res;
Alexey Bataev62b63b12015-03-10 07:28:44 +00001403}
1404
Alexey Bataev50b3c952016-02-19 10:38:26 +00001405Address CGOpenMPRuntime::getOrCreateDefaultLocation(unsigned Flags) {
John McCall7f416cc2015-09-08 08:05:57 +00001406 CharUnits Align = getIdentAlign(CGM);
Alexey Bataev15007ba2014-05-07 06:18:01 +00001407 llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags);
Alexey Bataev9959db52014-05-06 10:08:46 +00001408 if (!Entry) {
1409 if (!DefaultOpenMPPSource) {
1410 // Initialize default location for psource field of ident_t structure of
1411 // all ident_t objects. Format is ";file;function;line;column;;".
1412 // Taken from
1413 // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp_str.c
1414 DefaultOpenMPPSource =
John McCall7f416cc2015-09-08 08:05:57 +00001415 CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;").getPointer();
Alexey Bataev9959db52014-05-06 10:08:46 +00001416 DefaultOpenMPPSource =
1417 llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy);
1418 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001419
John McCall23c9dc62016-11-28 22:18:27 +00001420 ConstantInitBuilder builder(CGM);
John McCall6c9f1fdb2016-11-19 08:17:24 +00001421 auto fields = builder.beginStruct(IdentTy);
1422 fields.addInt(CGM.Int32Ty, 0);
1423 fields.addInt(CGM.Int32Ty, Flags);
1424 fields.addInt(CGM.Int32Ty, 0);
1425 fields.addInt(CGM.Int32Ty, 0);
1426 fields.add(DefaultOpenMPPSource);
1427 auto DefaultOpenMPLocation =
1428 fields.finishAndCreateGlobal("", Align, /*isConstant*/ true,
1429 llvm::GlobalValue::PrivateLinkage);
1430 DefaultOpenMPLocation->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1431
John McCall7f416cc2015-09-08 08:05:57 +00001432 OpenMPDefaultLocMap[Flags] = Entry = DefaultOpenMPLocation;
Alexey Bataev9959db52014-05-06 10:08:46 +00001433 }
John McCall7f416cc2015-09-08 08:05:57 +00001434 return Address(Entry, Align);
Alexey Bataev9959db52014-05-06 10:08:46 +00001435}
1436
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001437llvm::Value *CGOpenMPRuntime::emitUpdateLocation(CodeGenFunction &CGF,
1438 SourceLocation Loc,
Alexey Bataev50b3c952016-02-19 10:38:26 +00001439 unsigned Flags) {
1440 Flags |= OMP_IDENT_KMPC;
Alexey Bataev9959db52014-05-06 10:08:46 +00001441 // If no debug info is generated - return global default location.
Benjamin Kramer8c305922016-02-02 11:06:51 +00001442 if (CGM.getCodeGenOpts().getDebugInfo() == codegenoptions::NoDebugInfo ||
Alexey Bataev9959db52014-05-06 10:08:46 +00001443 Loc.isInvalid())
John McCall7f416cc2015-09-08 08:05:57 +00001444 return getOrCreateDefaultLocation(Flags).getPointer();
Alexey Bataev9959db52014-05-06 10:08:46 +00001445
1446 assert(CGF.CurFn && "No function in current CodeGenFunction.");
1447
John McCall7f416cc2015-09-08 08:05:57 +00001448 Address LocValue = Address::invalid();
Alexey Bataev1e4b7132014-12-03 12:11:24 +00001449 auto I = OpenMPLocThreadIDMap.find(CGF.CurFn);
1450 if (I != OpenMPLocThreadIDMap.end())
John McCall7f416cc2015-09-08 08:05:57 +00001451 LocValue = Address(I->second.DebugLoc, getIdentAlign(CGF.CGM));
1452
Alexander Musmanc6388682014-12-15 07:07:06 +00001453 // OpenMPLocThreadIDMap may have null DebugLoc and non-null ThreadID, if
1454 // GetOpenMPThreadID was called before this routine.
John McCall7f416cc2015-09-08 08:05:57 +00001455 if (!LocValue.isValid()) {
Alexey Bataev15007ba2014-05-07 06:18:01 +00001456 // Generate "ident_t .kmpc_loc.addr;"
John McCall7f416cc2015-09-08 08:05:57 +00001457 Address AI = CGF.CreateTempAlloca(IdentTy, getIdentAlign(CGF.CGM),
1458 ".kmpc_loc.addr");
Alexey Bataev18095712014-10-10 12:19:54 +00001459 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
John McCall7f416cc2015-09-08 08:05:57 +00001460 Elem.second.DebugLoc = AI.getPointer();
Alexey Bataev9959db52014-05-06 10:08:46 +00001461 LocValue = AI;
1462
1463 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
1464 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001465 CGF.Builder.CreateMemCpy(LocValue, getOrCreateDefaultLocation(Flags),
John McCall7f416cc2015-09-08 08:05:57 +00001466 CGM.getSize(getIdentSize(CGF.CGM)));
Alexey Bataev9959db52014-05-06 10:08:46 +00001467 }
1468
1469 // char **psource = &.kmpc_loc_<flags>.addr.psource;
John McCall7f416cc2015-09-08 08:05:57 +00001470 Address PSource = createIdentFieldGEP(CGF, LocValue, IdentField_PSource);
Alexey Bataev9959db52014-05-06 10:08:46 +00001471
Alexey Bataevf002aca2014-05-30 05:48:40 +00001472 auto OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding());
1473 if (OMPDebugLoc == nullptr) {
1474 SmallString<128> Buffer2;
1475 llvm::raw_svector_ostream OS2(Buffer2);
1476 // Build debug location
1477 PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc);
1478 OS2 << ";" << PLoc.getFilename() << ";";
1479 if (const FunctionDecl *FD =
1480 dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) {
1481 OS2 << FD->getQualifiedNameAsString();
1482 }
1483 OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;";
1484 OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str());
1485 OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc;
Alexey Bataev9959db52014-05-06 10:08:46 +00001486 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001487 // *psource = ";<File>;<Function>;<Line>;<Column>;;";
Alexey Bataevf002aca2014-05-30 05:48:40 +00001488 CGF.Builder.CreateStore(OMPDebugLoc, PSource);
1489
John McCall7f416cc2015-09-08 08:05:57 +00001490 // Our callers always pass this to a runtime function, so for
1491 // convenience, go ahead and return a naked pointer.
1492 return LocValue.getPointer();
Alexey Bataev9959db52014-05-06 10:08:46 +00001493}
1494
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001495llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF,
1496 SourceLocation Loc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001497 assert(CGF.CurFn && "No function in current CodeGenFunction.");
1498
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001499 llvm::Value *ThreadID = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +00001500 // Check whether we've already cached a load of the thread id in this
1501 // function.
Alexey Bataev1e4b7132014-12-03 12:11:24 +00001502 auto I = OpenMPLocThreadIDMap.find(CGF.CurFn);
Alexey Bataev18095712014-10-10 12:19:54 +00001503 if (I != OpenMPLocThreadIDMap.end()) {
1504 ThreadID = I->second.ThreadID;
Alexey Bataev03b340a2014-10-21 03:16:40 +00001505 if (ThreadID != nullptr)
1506 return ThreadID;
1507 }
Alexey Bataevaee18552017-08-16 14:01:00 +00001508 // If exceptions are enabled, do not use parameter to avoid possible crash.
Alexey Bataev5d2c9a42017-11-02 18:55:05 +00001509 if (!CGF.EHStack.requiresLandingPad() || !CGF.getLangOpts().Exceptions ||
1510 !CGF.getLangOpts().CXXExceptions ||
Alexey Bataev0e1b4582017-11-02 14:25:34 +00001511 CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) {
Alexey Bataevaee18552017-08-16 14:01:00 +00001512 if (auto *OMPRegionInfo =
1513 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
1514 if (OMPRegionInfo->getThreadIDVariable()) {
1515 // Check if this an outlined function with thread id passed as argument.
1516 auto LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF);
Alexey Bataev1e491372018-01-23 18:44:14 +00001517 ThreadID = CGF.EmitLoadOfScalar(LVal, Loc);
Alexey Bataevaee18552017-08-16 14:01:00 +00001518 // If value loaded in entry block, cache it and use it everywhere in
1519 // function.
1520 if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) {
1521 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
1522 Elem.second.ThreadID = ThreadID;
1523 }
1524 return ThreadID;
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001525 }
Alexey Bataevd6c57552014-07-25 07:55:17 +00001526 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001527 }
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001528
1529 // This is not an outlined function region - need to call __kmpc_int32
1530 // kmpc_global_thread_num(ident_t *loc).
1531 // Generate thread id value and cache this value for use across the
1532 // function.
1533 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
1534 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
Alexey Bataev0e1b4582017-11-02 14:25:34 +00001535 auto *Call = CGF.Builder.CreateCall(
1536 createRuntimeFunction(OMPRTL__kmpc_global_thread_num),
1537 emitUpdateLocation(CGF, Loc));
1538 Call->setCallingConv(CGF.getRuntimeCC());
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001539 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
Alexey Bataev0e1b4582017-11-02 14:25:34 +00001540 Elem.second.ThreadID = Call;
1541 return Call;
Alexey Bataev9959db52014-05-06 10:08:46 +00001542}
1543
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001544void CGOpenMPRuntime::functionFinished(CodeGenFunction &CGF) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001545 assert(CGF.CurFn && "No function in current CodeGenFunction.");
Alexey Bataev03b340a2014-10-21 03:16:40 +00001546 if (OpenMPLocThreadIDMap.count(CGF.CurFn))
1547 OpenMPLocThreadIDMap.erase(CGF.CurFn);
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001548 if (FunctionUDRMap.count(CGF.CurFn) > 0) {
1549 for(auto *D : FunctionUDRMap[CGF.CurFn]) {
1550 UDRMap.erase(D);
1551 }
1552 FunctionUDRMap.erase(CGF.CurFn);
1553 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001554}
1555
1556llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001557 if (!IdentTy) {
1558 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001559 return llvm::PointerType::getUnqual(IdentTy);
1560}
1561
1562llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001563 if (!Kmpc_MicroTy) {
1564 // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...)
1565 llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty),
1566 llvm::PointerType::getUnqual(CGM.Int32Ty)};
1567 Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true);
1568 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001569 return llvm::PointerType::getUnqual(Kmpc_MicroTy);
1570}
1571
1572llvm::Constant *
Alexey Bataev50b3c952016-02-19 10:38:26 +00001573CGOpenMPRuntime::createRuntimeFunction(unsigned Function) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001574 llvm::Constant *RTLFn = nullptr;
Alexey Bataev50b3c952016-02-19 10:38:26 +00001575 switch (static_cast<OpenMPRTLFunction>(Function)) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001576 case OMPRTL__kmpc_fork_call: {
1577 // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro
1578 // microtask, ...);
Alexey Bataev23b69422014-06-18 07:08:49 +00001579 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1580 getKmpc_MicroPointerTy()};
Alexey Bataev9959db52014-05-06 10:08:46 +00001581 llvm::FunctionType *FnTy =
Alexey Bataevd74d0602014-10-13 06:02:40 +00001582 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true);
Alexey Bataev9959db52014-05-06 10:08:46 +00001583 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call");
1584 break;
1585 }
1586 case OMPRTL__kmpc_global_thread_num: {
1587 // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc);
Alexey Bataev23b69422014-06-18 07:08:49 +00001588 llvm::Type *TypeParams[] = {getIdentTyPointerTy()};
Alexey Bataev9959db52014-05-06 10:08:46 +00001589 llvm::FunctionType *FnTy =
Alexey Bataevd74d0602014-10-13 06:02:40 +00001590 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
Alexey Bataev9959db52014-05-06 10:08:46 +00001591 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num");
1592 break;
1593 }
Alexey Bataev97720002014-11-11 04:05:39 +00001594 case OMPRTL__kmpc_threadprivate_cached: {
1595 // Build void *__kmpc_threadprivate_cached(ident_t *loc,
1596 // kmp_int32 global_tid, void *data, size_t size, void ***cache);
1597 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1598 CGM.VoidPtrTy, CGM.SizeTy,
1599 CGM.VoidPtrTy->getPointerTo()->getPointerTo()};
1600 llvm::FunctionType *FnTy =
1601 llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg*/ false);
1602 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_cached");
1603 break;
1604 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001605 case OMPRTL__kmpc_critical: {
Alexey Bataevf9472182014-09-22 12:32:31 +00001606 // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
1607 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001608 llvm::Type *TypeParams[] = {
1609 getIdentTyPointerTy(), CGM.Int32Ty,
1610 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
1611 llvm::FunctionType *FnTy =
1612 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1613 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical");
1614 break;
1615 }
Alexey Bataevfc57d162015-12-15 10:55:09 +00001616 case OMPRTL__kmpc_critical_with_hint: {
1617 // Build void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 global_tid,
1618 // kmp_critical_name *crit, uintptr_t hint);
1619 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1620 llvm::PointerType::getUnqual(KmpCriticalNameTy),
1621 CGM.IntPtrTy};
1622 llvm::FunctionType *FnTy =
1623 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1624 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical_with_hint");
1625 break;
1626 }
Alexey Bataev97720002014-11-11 04:05:39 +00001627 case OMPRTL__kmpc_threadprivate_register: {
1628 // Build void __kmpc_threadprivate_register(ident_t *, void *data,
1629 // kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
1630 // typedef void *(*kmpc_ctor)(void *);
1631 auto KmpcCtorTy =
1632 llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy,
1633 /*isVarArg*/ false)->getPointerTo();
1634 // typedef void *(*kmpc_cctor)(void *, void *);
1635 llvm::Type *KmpcCopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
1636 auto KmpcCopyCtorTy =
1637 llvm::FunctionType::get(CGM.VoidPtrTy, KmpcCopyCtorTyArgs,
1638 /*isVarArg*/ false)->getPointerTo();
1639 // typedef void (*kmpc_dtor)(void *);
1640 auto KmpcDtorTy =
1641 llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, /*isVarArg*/ false)
1642 ->getPointerTo();
1643 llvm::Type *FnTyArgs[] = {getIdentTyPointerTy(), CGM.VoidPtrTy, KmpcCtorTy,
1644 KmpcCopyCtorTy, KmpcDtorTy};
1645 auto FnTy = llvm::FunctionType::get(CGM.VoidTy, FnTyArgs,
1646 /*isVarArg*/ false);
1647 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_register");
1648 break;
1649 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001650 case OMPRTL__kmpc_end_critical: {
Alexey Bataevf9472182014-09-22 12:32:31 +00001651 // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
1652 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001653 llvm::Type *TypeParams[] = {
1654 getIdentTyPointerTy(), CGM.Int32Ty,
1655 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
1656 llvm::FunctionType *FnTy =
1657 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1658 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical");
1659 break;
1660 }
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00001661 case OMPRTL__kmpc_cancel_barrier: {
1662 // Build kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32
1663 // global_tid);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001664 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1665 llvm::FunctionType *FnTy =
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00001666 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1667 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_cancel_barrier");
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001668 break;
1669 }
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001670 case OMPRTL__kmpc_barrier: {
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001671 // Build void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001672 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1673 llvm::FunctionType *FnTy =
1674 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1675 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_barrier");
1676 break;
1677 }
Alexander Musmanc6388682014-12-15 07:07:06 +00001678 case OMPRTL__kmpc_for_static_fini: {
1679 // Build void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
1680 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1681 llvm::FunctionType *FnTy =
1682 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1683 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_fini");
1684 break;
1685 }
Alexey Bataevb2059782014-10-13 08:23:51 +00001686 case OMPRTL__kmpc_push_num_threads: {
1687 // Build void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
1688 // kmp_int32 num_threads)
1689 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1690 CGM.Int32Ty};
1691 llvm::FunctionType *FnTy =
1692 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1693 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_threads");
1694 break;
1695 }
Alexey Bataevd74d0602014-10-13 06:02:40 +00001696 case OMPRTL__kmpc_serialized_parallel: {
1697 // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
1698 // global_tid);
1699 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1700 llvm::FunctionType *FnTy =
1701 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1702 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel");
1703 break;
1704 }
1705 case OMPRTL__kmpc_end_serialized_parallel: {
1706 // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
1707 // global_tid);
1708 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1709 llvm::FunctionType *FnTy =
1710 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1711 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel");
1712 break;
1713 }
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001714 case OMPRTL__kmpc_flush: {
Alexey Bataevd76df6d2015-02-24 12:55:09 +00001715 // Build void __kmpc_flush(ident_t *loc);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001716 llvm::Type *TypeParams[] = {getIdentTyPointerTy()};
1717 llvm::FunctionType *FnTy =
Alexey Bataevd76df6d2015-02-24 12:55:09 +00001718 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001719 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_flush");
1720 break;
1721 }
Alexey Bataev8d690652014-12-04 07:23:53 +00001722 case OMPRTL__kmpc_master: {
1723 // Build kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid);
1724 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1725 llvm::FunctionType *FnTy =
1726 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1727 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_master");
1728 break;
1729 }
1730 case OMPRTL__kmpc_end_master: {
1731 // Build void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid);
1732 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1733 llvm::FunctionType *FnTy =
1734 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1735 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_master");
1736 break;
1737 }
Alexey Bataev9f797f32015-02-05 05:57:51 +00001738 case OMPRTL__kmpc_omp_taskyield: {
1739 // Build kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid,
1740 // int end_part);
1741 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy};
1742 llvm::FunctionType *FnTy =
1743 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1744 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_taskyield");
1745 break;
1746 }
Alexey Bataev6956e2e2015-02-05 06:35:41 +00001747 case OMPRTL__kmpc_single: {
1748 // Build kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid);
1749 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1750 llvm::FunctionType *FnTy =
1751 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1752 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_single");
1753 break;
1754 }
1755 case OMPRTL__kmpc_end_single: {
1756 // Build void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid);
1757 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1758 llvm::FunctionType *FnTy =
1759 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1760 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_single");
1761 break;
1762 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00001763 case OMPRTL__kmpc_omp_task_alloc: {
1764 // Build kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
1765 // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1766 // kmp_routine_entry_t *task_entry);
1767 assert(KmpRoutineEntryPtrTy != nullptr &&
1768 "Type kmp_routine_entry_t must be created.");
1769 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty,
1770 CGM.SizeTy, CGM.SizeTy, KmpRoutineEntryPtrTy};
1771 // Return void * and then cast to particular kmp_task_t type.
1772 llvm::FunctionType *FnTy =
1773 llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false);
1774 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_alloc");
1775 break;
1776 }
1777 case OMPRTL__kmpc_omp_task: {
1778 // Build kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t
1779 // *new_task);
1780 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1781 CGM.VoidPtrTy};
1782 llvm::FunctionType *FnTy =
1783 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1784 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task");
1785 break;
1786 }
Alexey Bataeva63048e2015-03-23 06:18:07 +00001787 case OMPRTL__kmpc_copyprivate: {
1788 // Build void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid,
Alexey Bataev66beaa92015-04-30 03:47:32 +00001789 // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *),
Alexey Bataeva63048e2015-03-23 06:18:07 +00001790 // kmp_int32 didit);
1791 llvm::Type *CpyTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
1792 auto *CpyFnTy =
1793 llvm::FunctionType::get(CGM.VoidTy, CpyTypeParams, /*isVarArg=*/false);
Alexey Bataev66beaa92015-04-30 03:47:32 +00001794 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.SizeTy,
Alexey Bataeva63048e2015-03-23 06:18:07 +00001795 CGM.VoidPtrTy, CpyFnTy->getPointerTo(),
1796 CGM.Int32Ty};
1797 llvm::FunctionType *FnTy =
1798 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1799 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_copyprivate");
1800 break;
1801 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001802 case OMPRTL__kmpc_reduce: {
1803 // Build kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid,
1804 // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void
1805 // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck);
1806 llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
1807 auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams,
1808 /*isVarArg=*/false);
1809 llvm::Type *TypeParams[] = {
1810 getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy,
1811 CGM.VoidPtrTy, ReduceFnTy->getPointerTo(),
1812 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
1813 llvm::FunctionType *FnTy =
1814 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1815 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce");
1816 break;
1817 }
1818 case OMPRTL__kmpc_reduce_nowait: {
1819 // Build kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32
1820 // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data,
1821 // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name
1822 // *lck);
1823 llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
1824 auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams,
1825 /*isVarArg=*/false);
1826 llvm::Type *TypeParams[] = {
1827 getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy,
1828 CGM.VoidPtrTy, ReduceFnTy->getPointerTo(),
1829 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
1830 llvm::FunctionType *FnTy =
1831 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1832 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce_nowait");
1833 break;
1834 }
1835 case OMPRTL__kmpc_end_reduce: {
1836 // Build void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
1837 // kmp_critical_name *lck);
1838 llvm::Type *TypeParams[] = {
1839 getIdentTyPointerTy(), CGM.Int32Ty,
1840 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
1841 llvm::FunctionType *FnTy =
1842 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1843 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce");
1844 break;
1845 }
1846 case OMPRTL__kmpc_end_reduce_nowait: {
1847 // Build __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
1848 // kmp_critical_name *lck);
1849 llvm::Type *TypeParams[] = {
1850 getIdentTyPointerTy(), CGM.Int32Ty,
1851 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
1852 llvm::FunctionType *FnTy =
1853 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1854 RTLFn =
1855 CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce_nowait");
1856 break;
1857 }
Alexey Bataev1d677132015-04-22 13:57:31 +00001858 case OMPRTL__kmpc_omp_task_begin_if0: {
1859 // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t
1860 // *new_task);
1861 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1862 CGM.VoidPtrTy};
1863 llvm::FunctionType *FnTy =
1864 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1865 RTLFn =
1866 CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_begin_if0");
1867 break;
1868 }
1869 case OMPRTL__kmpc_omp_task_complete_if0: {
1870 // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t
1871 // *new_task);
1872 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1873 CGM.VoidPtrTy};
1874 llvm::FunctionType *FnTy =
1875 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1876 RTLFn = CGM.CreateRuntimeFunction(FnTy,
1877 /*Name=*/"__kmpc_omp_task_complete_if0");
1878 break;
1879 }
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001880 case OMPRTL__kmpc_ordered: {
1881 // Build void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid);
1882 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1883 llvm::FunctionType *FnTy =
1884 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1885 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_ordered");
1886 break;
1887 }
1888 case OMPRTL__kmpc_end_ordered: {
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001889 // Build void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001890 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1891 llvm::FunctionType *FnTy =
1892 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1893 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_ordered");
1894 break;
1895 }
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001896 case OMPRTL__kmpc_omp_taskwait: {
1897 // Build kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 global_tid);
1898 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1899 llvm::FunctionType *FnTy =
1900 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1901 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_omp_taskwait");
1902 break;
1903 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001904 case OMPRTL__kmpc_taskgroup: {
1905 // Build void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid);
1906 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1907 llvm::FunctionType *FnTy =
1908 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1909 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_taskgroup");
1910 break;
1911 }
1912 case OMPRTL__kmpc_end_taskgroup: {
1913 // Build void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid);
1914 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1915 llvm::FunctionType *FnTy =
1916 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1917 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_taskgroup");
1918 break;
1919 }
Alexey Bataev7f210c62015-06-18 13:40:03 +00001920 case OMPRTL__kmpc_push_proc_bind: {
1921 // Build void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid,
1922 // int proc_bind)
1923 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy};
1924 llvm::FunctionType *FnTy =
1925 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1926 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_proc_bind");
1927 break;
1928 }
Alexey Bataev1d2353d2015-06-24 11:01:36 +00001929 case OMPRTL__kmpc_omp_task_with_deps: {
1930 // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid,
1931 // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list,
1932 // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
1933 llvm::Type *TypeParams[] = {
1934 getIdentTyPointerTy(), CGM.Int32Ty, CGM.VoidPtrTy, CGM.Int32Ty,
1935 CGM.VoidPtrTy, CGM.Int32Ty, CGM.VoidPtrTy};
1936 llvm::FunctionType *FnTy =
1937 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1938 RTLFn =
1939 CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_with_deps");
1940 break;
1941 }
1942 case OMPRTL__kmpc_omp_wait_deps: {
1943 // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid,
1944 // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias,
1945 // kmp_depend_info_t *noalias_dep_list);
1946 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1947 CGM.Int32Ty, CGM.VoidPtrTy,
1948 CGM.Int32Ty, CGM.VoidPtrTy};
1949 llvm::FunctionType *FnTy =
1950 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1951 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_wait_deps");
1952 break;
1953 }
Alexey Bataev0f34da12015-07-02 04:17:07 +00001954 case OMPRTL__kmpc_cancellationpoint: {
1955 // Build kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32
1956 // global_tid, kmp_int32 cncl_kind)
1957 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy};
1958 llvm::FunctionType *FnTy =
1959 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1960 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancellationpoint");
1961 break;
1962 }
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001963 case OMPRTL__kmpc_cancel: {
1964 // Build kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid,
1965 // kmp_int32 cncl_kind)
1966 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy};
1967 llvm::FunctionType *FnTy =
1968 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1969 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancel");
1970 break;
1971 }
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001972 case OMPRTL__kmpc_push_num_teams: {
1973 // Build void kmpc_push_num_teams (ident_t loc, kmp_int32 global_tid,
1974 // kmp_int32 num_teams, kmp_int32 num_threads)
1975 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty,
1976 CGM.Int32Ty};
1977 llvm::FunctionType *FnTy =
1978 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1979 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_teams");
1980 break;
1981 }
1982 case OMPRTL__kmpc_fork_teams: {
1983 // Build void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro
1984 // microtask, ...);
1985 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1986 getKmpc_MicroPointerTy()};
1987 llvm::FunctionType *FnTy =
1988 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true);
1989 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_teams");
1990 break;
1991 }
Alexey Bataev7292c292016-04-25 12:22:29 +00001992 case OMPRTL__kmpc_taskloop: {
1993 // Build void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int
1994 // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int
1995 // sched, kmp_uint64 grainsize, void *task_dup);
1996 llvm::Type *TypeParams[] = {getIdentTyPointerTy(),
1997 CGM.IntTy,
1998 CGM.VoidPtrTy,
1999 CGM.IntTy,
2000 CGM.Int64Ty->getPointerTo(),
2001 CGM.Int64Ty->getPointerTo(),
2002 CGM.Int64Ty,
2003 CGM.IntTy,
2004 CGM.IntTy,
2005 CGM.Int64Ty,
2006 CGM.VoidPtrTy};
2007 llvm::FunctionType *FnTy =
2008 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2009 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_taskloop");
2010 break;
2011 }
Alexey Bataev8b427062016-05-25 12:36:08 +00002012 case OMPRTL__kmpc_doacross_init: {
2013 // Build void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32
2014 // num_dims, struct kmp_dim *dims);
2015 llvm::Type *TypeParams[] = {getIdentTyPointerTy(),
2016 CGM.Int32Ty,
2017 CGM.Int32Ty,
2018 CGM.VoidPtrTy};
2019 llvm::FunctionType *FnTy =
2020 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2021 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_init");
2022 break;
2023 }
2024 case OMPRTL__kmpc_doacross_fini: {
2025 // Build void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid);
2026 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
2027 llvm::FunctionType *FnTy =
2028 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2029 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_fini");
2030 break;
2031 }
2032 case OMPRTL__kmpc_doacross_post: {
2033 // Build void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64
2034 // *vec);
2035 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
2036 CGM.Int64Ty->getPointerTo()};
2037 llvm::FunctionType *FnTy =
2038 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2039 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_post");
2040 break;
2041 }
2042 case OMPRTL__kmpc_doacross_wait: {
2043 // Build void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64
2044 // *vec);
2045 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
2046 CGM.Int64Ty->getPointerTo()};
2047 llvm::FunctionType *FnTy =
2048 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2049 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_wait");
2050 break;
2051 }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002052 case OMPRTL__kmpc_task_reduction_init: {
2053 // Build void *__kmpc_task_reduction_init(int gtid, int num_data, void
2054 // *data);
2055 llvm::Type *TypeParams[] = {CGM.IntTy, CGM.IntTy, CGM.VoidPtrTy};
2056 llvm::FunctionType *FnTy =
2057 llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false);
2058 RTLFn =
2059 CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_task_reduction_init");
2060 break;
2061 }
2062 case OMPRTL__kmpc_task_reduction_get_th_data: {
2063 // Build void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void
2064 // *d);
2065 llvm::Type *TypeParams[] = {CGM.IntTy, CGM.VoidPtrTy, CGM.VoidPtrTy};
2066 llvm::FunctionType *FnTy =
2067 llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false);
2068 RTLFn = CGM.CreateRuntimeFunction(
2069 FnTy, /*Name=*/"__kmpc_task_reduction_get_th_data");
2070 break;
2071 }
Samuel Antaobed3c462015-10-02 16:14:20 +00002072 case OMPRTL__tgt_target: {
George Rokos63bc9d62017-11-21 18:25:12 +00002073 // Build int32_t __tgt_target(int64_t device_id, void *host_ptr, int32_t
2074 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
Samuel Antaobed3c462015-10-02 16:14:20 +00002075 // *arg_types);
George Rokos63bc9d62017-11-21 18:25:12 +00002076 llvm::Type *TypeParams[] = {CGM.Int64Ty,
Samuel Antaobed3c462015-10-02 16:14:20 +00002077 CGM.VoidPtrTy,
2078 CGM.Int32Ty,
2079 CGM.VoidPtrPtrTy,
2080 CGM.VoidPtrPtrTy,
2081 CGM.SizeTy->getPointerTo(),
George Rokos63bc9d62017-11-21 18:25:12 +00002082 CGM.Int64Ty->getPointerTo()};
Samuel Antaobed3c462015-10-02 16:14:20 +00002083 llvm::FunctionType *FnTy =
2084 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2085 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target");
2086 break;
2087 }
Alexey Bataeva9f77c62017-12-13 21:04:20 +00002088 case OMPRTL__tgt_target_nowait: {
2089 // Build int32_t __tgt_target_nowait(int64_t device_id, void *host_ptr,
2090 // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes,
2091 // int64_t *arg_types);
2092 llvm::Type *TypeParams[] = {CGM.Int64Ty,
2093 CGM.VoidPtrTy,
2094 CGM.Int32Ty,
2095 CGM.VoidPtrPtrTy,
2096 CGM.VoidPtrPtrTy,
2097 CGM.SizeTy->getPointerTo(),
2098 CGM.Int64Ty->getPointerTo()};
2099 llvm::FunctionType *FnTy =
2100 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2101 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_nowait");
2102 break;
2103 }
Samuel Antaob68e2db2016-03-03 16:20:23 +00002104 case OMPRTL__tgt_target_teams: {
George Rokos63bc9d62017-11-21 18:25:12 +00002105 // Build int32_t __tgt_target_teams(int64_t device_id, void *host_ptr,
Samuel Antaob68e2db2016-03-03 16:20:23 +00002106 // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes,
George Rokos63bc9d62017-11-21 18:25:12 +00002107 // int64_t *arg_types, int32_t num_teams, int32_t thread_limit);
2108 llvm::Type *TypeParams[] = {CGM.Int64Ty,
Samuel Antaob68e2db2016-03-03 16:20:23 +00002109 CGM.VoidPtrTy,
2110 CGM.Int32Ty,
2111 CGM.VoidPtrPtrTy,
2112 CGM.VoidPtrPtrTy,
2113 CGM.SizeTy->getPointerTo(),
George Rokos63bc9d62017-11-21 18:25:12 +00002114 CGM.Int64Ty->getPointerTo(),
Samuel Antaob68e2db2016-03-03 16:20:23 +00002115 CGM.Int32Ty,
2116 CGM.Int32Ty};
2117 llvm::FunctionType *FnTy =
2118 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2119 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams");
2120 break;
2121 }
Alexey Bataeva9f77c62017-12-13 21:04:20 +00002122 case OMPRTL__tgt_target_teams_nowait: {
2123 // Build int32_t __tgt_target_teams_nowait(int64_t device_id, void
2124 // *host_ptr, int32_t arg_num, void** args_base, void **args, size_t
2125 // *arg_sizes, int64_t *arg_types, int32_t num_teams, int32_t thread_limit);
2126 llvm::Type *TypeParams[] = {CGM.Int64Ty,
2127 CGM.VoidPtrTy,
2128 CGM.Int32Ty,
2129 CGM.VoidPtrPtrTy,
2130 CGM.VoidPtrPtrTy,
2131 CGM.SizeTy->getPointerTo(),
2132 CGM.Int64Ty->getPointerTo(),
2133 CGM.Int32Ty,
2134 CGM.Int32Ty};
2135 llvm::FunctionType *FnTy =
2136 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2137 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams_nowait");
2138 break;
2139 }
Samuel Antaoee8fb302016-01-06 13:42:12 +00002140 case OMPRTL__tgt_register_lib: {
2141 // Build void __tgt_register_lib(__tgt_bin_desc *desc);
2142 QualType ParamTy =
2143 CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy());
2144 llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)};
2145 llvm::FunctionType *FnTy =
2146 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2147 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_register_lib");
2148 break;
2149 }
2150 case OMPRTL__tgt_unregister_lib: {
2151 // Build void __tgt_unregister_lib(__tgt_bin_desc *desc);
2152 QualType ParamTy =
2153 CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy());
2154 llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)};
2155 llvm::FunctionType *FnTy =
2156 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2157 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_unregister_lib");
2158 break;
2159 }
Samuel Antaodf158d52016-04-27 22:58:19 +00002160 case OMPRTL__tgt_target_data_begin: {
George Rokos63bc9d62017-11-21 18:25:12 +00002161 // Build void __tgt_target_data_begin(int64_t device_id, int32_t arg_num,
2162 // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types);
2163 llvm::Type *TypeParams[] = {CGM.Int64Ty,
Samuel Antaodf158d52016-04-27 22:58:19 +00002164 CGM.Int32Ty,
2165 CGM.VoidPtrPtrTy,
2166 CGM.VoidPtrPtrTy,
2167 CGM.SizeTy->getPointerTo(),
George Rokos63bc9d62017-11-21 18:25:12 +00002168 CGM.Int64Ty->getPointerTo()};
Samuel Antaodf158d52016-04-27 22:58:19 +00002169 llvm::FunctionType *FnTy =
2170 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
2171 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin");
2172 break;
2173 }
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +00002174 case OMPRTL__tgt_target_data_begin_nowait: {
2175 // Build void __tgt_target_data_begin_nowait(int64_t device_id, int32_t
2176 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
2177 // *arg_types);
2178 llvm::Type *TypeParams[] = {CGM.Int64Ty,
2179 CGM.Int32Ty,
2180 CGM.VoidPtrPtrTy,
2181 CGM.VoidPtrPtrTy,
2182 CGM.SizeTy->getPointerTo(),
2183 CGM.Int64Ty->getPointerTo()};
2184 auto *FnTy =
2185 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2186 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin_nowait");
2187 break;
2188 }
Samuel Antaodf158d52016-04-27 22:58:19 +00002189 case OMPRTL__tgt_target_data_end: {
George Rokos63bc9d62017-11-21 18:25:12 +00002190 // Build void __tgt_target_data_end(int64_t device_id, int32_t arg_num,
2191 // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types);
2192 llvm::Type *TypeParams[] = {CGM.Int64Ty,
Samuel Antaodf158d52016-04-27 22:58:19 +00002193 CGM.Int32Ty,
2194 CGM.VoidPtrPtrTy,
2195 CGM.VoidPtrPtrTy,
2196 CGM.SizeTy->getPointerTo(),
George Rokos63bc9d62017-11-21 18:25:12 +00002197 CGM.Int64Ty->getPointerTo()};
Samuel Antaodf158d52016-04-27 22:58:19 +00002198 llvm::FunctionType *FnTy =
2199 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
2200 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end");
2201 break;
2202 }
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +00002203 case OMPRTL__tgt_target_data_end_nowait: {
2204 // Build void __tgt_target_data_end_nowait(int64_t device_id, int32_t
2205 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
2206 // *arg_types);
2207 llvm::Type *TypeParams[] = {CGM.Int64Ty,
2208 CGM.Int32Ty,
2209 CGM.VoidPtrPtrTy,
2210 CGM.VoidPtrPtrTy,
2211 CGM.SizeTy->getPointerTo(),
2212 CGM.Int64Ty->getPointerTo()};
2213 auto *FnTy =
2214 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2215 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end_nowait");
2216 break;
2217 }
Samuel Antao8d2d7302016-05-26 18:30:22 +00002218 case OMPRTL__tgt_target_data_update: {
George Rokos63bc9d62017-11-21 18:25:12 +00002219 // Build void __tgt_target_data_update(int64_t device_id, int32_t arg_num,
2220 // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types);
2221 llvm::Type *TypeParams[] = {CGM.Int64Ty,
Samuel Antao8d2d7302016-05-26 18:30:22 +00002222 CGM.Int32Ty,
2223 CGM.VoidPtrPtrTy,
2224 CGM.VoidPtrPtrTy,
2225 CGM.SizeTy->getPointerTo(),
George Rokos63bc9d62017-11-21 18:25:12 +00002226 CGM.Int64Ty->getPointerTo()};
Samuel Antao8d2d7302016-05-26 18:30:22 +00002227 llvm::FunctionType *FnTy =
2228 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
2229 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update");
2230 break;
2231 }
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +00002232 case OMPRTL__tgt_target_data_update_nowait: {
2233 // Build void __tgt_target_data_update_nowait(int64_t device_id, int32_t
2234 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
2235 // *arg_types);
2236 llvm::Type *TypeParams[] = {CGM.Int64Ty,
2237 CGM.Int32Ty,
2238 CGM.VoidPtrPtrTy,
2239 CGM.VoidPtrPtrTy,
2240 CGM.SizeTy->getPointerTo(),
2241 CGM.Int64Ty->getPointerTo()};
2242 auto *FnTy =
2243 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2244 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update_nowait");
2245 break;
2246 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002247 }
Alexey Bataev50b3c952016-02-19 10:38:26 +00002248 assert(RTLFn && "Unable to find OpenMP runtime function");
Alexey Bataev9959db52014-05-06 10:08:46 +00002249 return RTLFn;
2250}
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002251
Alexander Musman21212e42015-03-13 10:38:23 +00002252llvm::Constant *CGOpenMPRuntime::createForStaticInitFunction(unsigned IVSize,
2253 bool IVSigned) {
2254 assert((IVSize == 32 || IVSize == 64) &&
2255 "IV size is not compatible with the omp runtime");
2256 auto Name = IVSize == 32 ? (IVSigned ? "__kmpc_for_static_init_4"
2257 : "__kmpc_for_static_init_4u")
2258 : (IVSigned ? "__kmpc_for_static_init_8"
2259 : "__kmpc_for_static_init_8u");
2260 auto ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty;
2261 auto PtrTy = llvm::PointerType::getUnqual(ITy);
2262 llvm::Type *TypeParams[] = {
2263 getIdentTyPointerTy(), // loc
2264 CGM.Int32Ty, // tid
2265 CGM.Int32Ty, // schedtype
2266 llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
2267 PtrTy, // p_lower
2268 PtrTy, // p_upper
2269 PtrTy, // p_stride
2270 ITy, // incr
2271 ITy // chunk
2272 };
2273 llvm::FunctionType *FnTy =
2274 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
2275 return CGM.CreateRuntimeFunction(FnTy, Name);
2276}
2277
Alexander Musman92bdaab2015-03-12 13:37:50 +00002278llvm::Constant *CGOpenMPRuntime::createDispatchInitFunction(unsigned IVSize,
2279 bool IVSigned) {
2280 assert((IVSize == 32 || IVSize == 64) &&
2281 "IV size is not compatible with the omp runtime");
2282 auto Name =
2283 IVSize == 32
2284 ? (IVSigned ? "__kmpc_dispatch_init_4" : "__kmpc_dispatch_init_4u")
2285 : (IVSigned ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_8u");
2286 auto ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty;
2287 llvm::Type *TypeParams[] = { getIdentTyPointerTy(), // loc
2288 CGM.Int32Ty, // tid
2289 CGM.Int32Ty, // schedtype
2290 ITy, // lower
2291 ITy, // upper
2292 ITy, // stride
2293 ITy // chunk
2294 };
2295 llvm::FunctionType *FnTy =
2296 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
2297 return CGM.CreateRuntimeFunction(FnTy, Name);
2298}
2299
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002300llvm::Constant *CGOpenMPRuntime::createDispatchFiniFunction(unsigned IVSize,
2301 bool IVSigned) {
2302 assert((IVSize == 32 || IVSize == 64) &&
2303 "IV size is not compatible with the omp runtime");
2304 auto Name =
2305 IVSize == 32
2306 ? (IVSigned ? "__kmpc_dispatch_fini_4" : "__kmpc_dispatch_fini_4u")
2307 : (IVSigned ? "__kmpc_dispatch_fini_8" : "__kmpc_dispatch_fini_8u");
2308 llvm::Type *TypeParams[] = {
2309 getIdentTyPointerTy(), // loc
2310 CGM.Int32Ty, // tid
2311 };
2312 llvm::FunctionType *FnTy =
2313 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2314 return CGM.CreateRuntimeFunction(FnTy, Name);
2315}
2316
Alexander Musman92bdaab2015-03-12 13:37:50 +00002317llvm::Constant *CGOpenMPRuntime::createDispatchNextFunction(unsigned IVSize,
2318 bool IVSigned) {
2319 assert((IVSize == 32 || IVSize == 64) &&
2320 "IV size is not compatible with the omp runtime");
2321 auto Name =
2322 IVSize == 32
2323 ? (IVSigned ? "__kmpc_dispatch_next_4" : "__kmpc_dispatch_next_4u")
2324 : (IVSigned ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_8u");
2325 auto ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty;
2326 auto PtrTy = llvm::PointerType::getUnqual(ITy);
2327 llvm::Type *TypeParams[] = {
2328 getIdentTyPointerTy(), // loc
2329 CGM.Int32Ty, // tid
2330 llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
2331 PtrTy, // p_lower
2332 PtrTy, // p_upper
2333 PtrTy // p_stride
2334 };
2335 llvm::FunctionType *FnTy =
2336 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2337 return CGM.CreateRuntimeFunction(FnTy, Name);
2338}
2339
Alexey Bataev03f270c2018-03-30 18:31:07 +00002340Address CGOpenMPRuntime::getAddrOfDeclareTargetLink(const VarDecl *VD) {
2341 if (CGM.getLangOpts().OpenMPSimd)
2342 return Address::invalid();
Alexey Bataev92327c52018-03-26 16:40:55 +00002343 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
2344 isDeclareTargetDeclaration(VD);
2345 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
2346 SmallString<64> PtrName;
2347 {
2348 llvm::raw_svector_ostream OS(PtrName);
2349 OS << CGM.getMangledName(GlobalDecl(VD)) << "_decl_tgt_link_ptr";
2350 }
2351 llvm::Value *Ptr = CGM.getModule().getNamedValue(PtrName);
2352 if (!Ptr) {
2353 QualType PtrTy = CGM.getContext().getPointerType(VD->getType());
2354 Ptr = getOrCreateInternalVariable(CGM.getTypes().ConvertTypeForMem(PtrTy),
2355 PtrName);
Alexey Bataev03f270c2018-03-30 18:31:07 +00002356 if (!CGM.getLangOpts().OpenMPIsDevice) {
2357 auto *GV = cast<llvm::GlobalVariable>(Ptr);
2358 GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
2359 GV->setInitializer(CGM.GetAddrOfGlobal(VD));
2360 }
2361 CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ptr));
2362 registerTargetGlobalVariable(VD, cast<llvm::Constant>(Ptr));
Alexey Bataev92327c52018-03-26 16:40:55 +00002363 }
2364 return Address(Ptr, CGM.getContext().getDeclAlign(VD));
2365 }
2366 return Address::invalid();
2367}
2368
Alexey Bataev97720002014-11-11 04:05:39 +00002369llvm::Constant *
2370CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) {
Samuel Antaof8b50122015-07-13 22:54:53 +00002371 assert(!CGM.getLangOpts().OpenMPUseTLS ||
2372 !CGM.getContext().getTargetInfo().isTLSSupported());
Alexey Bataev97720002014-11-11 04:05:39 +00002373 // Lookup the entry, lazily creating it if necessary.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002374 return getOrCreateInternalVariable(CGM.Int8PtrPtrTy,
Alexey Bataev97720002014-11-11 04:05:39 +00002375 Twine(CGM.getMangledName(VD)) + ".cache.");
2376}
2377
John McCall7f416cc2015-09-08 08:05:57 +00002378Address CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF,
2379 const VarDecl *VD,
2380 Address VDAddr,
2381 SourceLocation Loc) {
Samuel Antaof8b50122015-07-13 22:54:53 +00002382 if (CGM.getLangOpts().OpenMPUseTLS &&
2383 CGM.getContext().getTargetInfo().isTLSSupported())
2384 return VDAddr;
2385
John McCall7f416cc2015-09-08 08:05:57 +00002386 auto VarTy = VDAddr.getElementType();
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002387 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
John McCall7f416cc2015-09-08 08:05:57 +00002388 CGF.Builder.CreatePointerCast(VDAddr.getPointer(),
2389 CGM.Int8PtrTy),
Alexey Bataev97720002014-11-11 04:05:39 +00002390 CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)),
2391 getOrCreateThreadPrivateCache(VD)};
John McCall7f416cc2015-09-08 08:05:57 +00002392 return Address(CGF.EmitRuntimeCall(
2393 createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args),
2394 VDAddr.getAlignment());
Alexey Bataev97720002014-11-11 04:05:39 +00002395}
2396
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002397void CGOpenMPRuntime::emitThreadPrivateVarInit(
John McCall7f416cc2015-09-08 08:05:57 +00002398 CodeGenFunction &CGF, Address VDAddr, llvm::Value *Ctor,
Alexey Bataev97720002014-11-11 04:05:39 +00002399 llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) {
2400 // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime
2401 // library.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002402 auto OMPLoc = emitUpdateLocation(CGF, Loc);
2403 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num),
Alexey Bataev97720002014-11-11 04:05:39 +00002404 OMPLoc);
2405 // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor)
2406 // to register constructor/destructor for variable.
2407 llvm::Value *Args[] = {OMPLoc,
John McCall7f416cc2015-09-08 08:05:57 +00002408 CGF.Builder.CreatePointerCast(VDAddr.getPointer(),
2409 CGM.VoidPtrTy),
Alexey Bataev97720002014-11-11 04:05:39 +00002410 Ctor, CopyCtor, Dtor};
Alexey Bataev1e4b7132014-12-03 12:11:24 +00002411 CGF.EmitRuntimeCall(
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002412 createRuntimeFunction(OMPRTL__kmpc_threadprivate_register), Args);
Alexey Bataev97720002014-11-11 04:05:39 +00002413}
2414
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002415llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition(
John McCall7f416cc2015-09-08 08:05:57 +00002416 const VarDecl *VD, Address VDAddr, SourceLocation Loc,
Alexey Bataev97720002014-11-11 04:05:39 +00002417 bool PerformInit, CodeGenFunction *CGF) {
Samuel Antaof8b50122015-07-13 22:54:53 +00002418 if (CGM.getLangOpts().OpenMPUseTLS &&
2419 CGM.getContext().getTargetInfo().isTLSSupported())
2420 return nullptr;
2421
Alexey Bataev97720002014-11-11 04:05:39 +00002422 VD = VD->getDefinition(CGM.getContext());
2423 if (VD && ThreadPrivateWithDefinition.count(VD) == 0) {
2424 ThreadPrivateWithDefinition.insert(VD);
2425 QualType ASTTy = VD->getType();
2426
2427 llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr;
2428 auto Init = VD->getAnyInitializer();
2429 if (CGM.getLangOpts().CPlusPlus && PerformInit) {
2430 // Generate function that re-emits the declaration's initializer into the
2431 // threadprivate copy of the variable VD
2432 CodeGenFunction CtorCGF(CGM);
2433 FunctionArgList Args;
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002434 ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc,
2435 /*Id=*/nullptr, CGM.getContext().VoidPtrTy,
Alexey Bataev56223232017-06-09 13:40:18 +00002436 ImplicitParamDecl::Other);
Alexey Bataev97720002014-11-11 04:05:39 +00002437 Args.push_back(&Dst);
2438
John McCallc56a8b32016-03-11 04:30:31 +00002439 auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(
2440 CGM.getContext().VoidPtrTy, Args);
Alexey Bataev97720002014-11-11 04:05:39 +00002441 auto FTy = CGM.getTypes().GetFunctionType(FI);
2442 auto Fn = CGM.CreateGlobalInitOrDestructFunction(
Akira Hatanaka7791f1a42015-10-31 01:28:07 +00002443 FTy, ".__kmpc_global_ctor_.", FI, Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00002444 CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI,
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002445 Args, Loc, Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00002446 auto ArgVal = CtorCGF.EmitLoadOfScalar(
John McCall7f416cc2015-09-08 08:05:57 +00002447 CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false,
Alexey Bataev97720002014-11-11 04:05:39 +00002448 CGM.getContext().VoidPtrTy, Dst.getLocation());
John McCall7f416cc2015-09-08 08:05:57 +00002449 Address Arg = Address(ArgVal, VDAddr.getAlignment());
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002450 Arg = CtorCGF.Builder.CreateElementBitCast(
2451 Arg, CtorCGF.ConvertTypeForMem(ASTTy));
Alexey Bataev97720002014-11-11 04:05:39 +00002452 CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(),
2453 /*IsInitializer=*/true);
2454 ArgVal = CtorCGF.EmitLoadOfScalar(
John McCall7f416cc2015-09-08 08:05:57 +00002455 CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false,
Alexey Bataev97720002014-11-11 04:05:39 +00002456 CGM.getContext().VoidPtrTy, Dst.getLocation());
2457 CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue);
2458 CtorCGF.FinishFunction();
2459 Ctor = Fn;
2460 }
2461 if (VD->getType().isDestructedType() != QualType::DK_none) {
2462 // Generate function that emits destructor call for the threadprivate copy
2463 // of the variable VD
2464 CodeGenFunction DtorCGF(CGM);
2465 FunctionArgList Args;
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002466 ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc,
2467 /*Id=*/nullptr, CGM.getContext().VoidPtrTy,
Alexey Bataev56223232017-06-09 13:40:18 +00002468 ImplicitParamDecl::Other);
Alexey Bataev97720002014-11-11 04:05:39 +00002469 Args.push_back(&Dst);
2470
John McCallc56a8b32016-03-11 04:30:31 +00002471 auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(
2472 CGM.getContext().VoidTy, Args);
Alexey Bataev97720002014-11-11 04:05:39 +00002473 auto FTy = CGM.getTypes().GetFunctionType(FI);
2474 auto Fn = CGM.CreateGlobalInitOrDestructFunction(
Akira Hatanaka7791f1a42015-10-31 01:28:07 +00002475 FTy, ".__kmpc_global_dtor_.", FI, Loc);
Adrian Prantl1858c662016-04-24 22:22:29 +00002476 auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF);
Alexey Bataev97720002014-11-11 04:05:39 +00002477 DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args,
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002478 Loc, Loc);
Adrian Prantl1858c662016-04-24 22:22:29 +00002479 // Create a scope with an artificial location for the body of this function.
2480 auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF);
Alexey Bataev97720002014-11-11 04:05:39 +00002481 auto ArgVal = DtorCGF.EmitLoadOfScalar(
2482 DtorCGF.GetAddrOfLocalVar(&Dst),
John McCall7f416cc2015-09-08 08:05:57 +00002483 /*Volatile=*/false, CGM.getContext().VoidPtrTy, Dst.getLocation());
2484 DtorCGF.emitDestroy(Address(ArgVal, VDAddr.getAlignment()), ASTTy,
Alexey Bataev97720002014-11-11 04:05:39 +00002485 DtorCGF.getDestroyer(ASTTy.isDestructedType()),
2486 DtorCGF.needsEHCleanup(ASTTy.isDestructedType()));
2487 DtorCGF.FinishFunction();
2488 Dtor = Fn;
2489 }
2490 // Do not emit init function if it is not required.
2491 if (!Ctor && !Dtor)
2492 return nullptr;
2493
2494 llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
2495 auto CopyCtorTy =
2496 llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs,
2497 /*isVarArg=*/false)->getPointerTo();
2498 // Copying constructor for the threadprivate variable.
2499 // Must be NULL - reserved by runtime, but currently it requires that this
2500 // parameter is always NULL. Otherwise it fires assertion.
2501 CopyCtor = llvm::Constant::getNullValue(CopyCtorTy);
2502 if (Ctor == nullptr) {
2503 auto CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy,
2504 /*isVarArg=*/false)->getPointerTo();
2505 Ctor = llvm::Constant::getNullValue(CtorTy);
2506 }
2507 if (Dtor == nullptr) {
2508 auto DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy,
2509 /*isVarArg=*/false)->getPointerTo();
2510 Dtor = llvm::Constant::getNullValue(DtorTy);
2511 }
2512 if (!CGF) {
2513 auto InitFunctionTy =
2514 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false);
2515 auto InitFunction = CGM.CreateGlobalInitOrDestructFunction(
Akira Hatanaka7791f1a42015-10-31 01:28:07 +00002516 InitFunctionTy, ".__omp_threadprivate_init_.",
2517 CGM.getTypes().arrangeNullaryFunction());
Alexey Bataev97720002014-11-11 04:05:39 +00002518 CodeGenFunction InitCGF(CGM);
2519 FunctionArgList ArgList;
2520 InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction,
2521 CGM.getTypes().arrangeNullaryFunction(), ArgList,
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002522 Loc, Loc);
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002523 emitThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00002524 InitCGF.FinishFunction();
2525 return InitFunction;
2526 }
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002527 emitThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00002528 }
2529 return nullptr;
2530}
2531
Alexey Bataev34f8a702018-03-28 14:28:54 +00002532/// \brief Obtain information that uniquely identifies a target entry. This
2533/// consists of the file and device IDs as well as line number associated with
2534/// the relevant entry source location.
2535static void getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc,
2536 unsigned &DeviceID, unsigned &FileID,
2537 unsigned &LineNum) {
2538
2539 auto &SM = C.getSourceManager();
2540
2541 // The loc should be always valid and have a file ID (the user cannot use
2542 // #pragma directives in macros)
2543
2544 assert(Loc.isValid() && "Source location is expected to be always valid.");
2545 assert(Loc.isFileID() && "Source location is expected to refer to a file.");
2546
2547 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
2548 assert(PLoc.isValid() && "Source location is expected to be always valid.");
2549
2550 llvm::sys::fs::UniqueID ID;
2551 if (llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID))
2552 llvm_unreachable("Source file with target region no longer exists!");
2553
2554 DeviceID = ID.getDevice();
2555 FileID = ID.getFile();
2556 LineNum = PLoc.getLine();
2557}
2558
2559bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD,
2560 llvm::GlobalVariable *Addr,
2561 bool PerformInit) {
2562 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
2563 isDeclareTargetDeclaration(VD);
2564 if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link)
2565 return false;
2566 VD = VD->getDefinition(CGM.getContext());
2567 if (VD && !DeclareTargetWithDefinition.insert(VD).second)
2568 return CGM.getLangOpts().OpenMPIsDevice;
2569
2570 QualType ASTTy = VD->getType();
2571
2572 SourceLocation Loc = VD->getCanonicalDecl()->getLocStart();
2573 // Produce the unique prefix to identify the new target regions. We use
2574 // the source location of the variable declaration which we know to not
2575 // conflict with any target region.
2576 unsigned DeviceID;
2577 unsigned FileID;
2578 unsigned Line;
2579 getTargetEntryUniqueInfo(CGM.getContext(), Loc, DeviceID, FileID, Line);
2580 SmallString<128> Buffer, Out;
2581 {
2582 llvm::raw_svector_ostream OS(Buffer);
2583 OS << "__omp_offloading_" << llvm::format("_%x", DeviceID)
2584 << llvm::format("_%x_", FileID) << VD->getName() << "_l" << Line;
2585 }
2586
2587 const Expr *Init = VD->getAnyInitializer();
2588 if (CGM.getLangOpts().CPlusPlus && PerformInit) {
2589 llvm::Constant *Ctor;
2590 llvm::Constant *ID;
2591 if (CGM.getLangOpts().OpenMPIsDevice) {
2592 // Generate function that re-emits the declaration's initializer into
2593 // the threadprivate copy of the variable VD
2594 CodeGenFunction CtorCGF(CGM);
2595
2596 const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
2597 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
2598 llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction(
2599 FTy, Twine(Buffer, "_ctor"), FI, Loc);
2600 auto NL = ApplyDebugLocation::CreateEmpty(CtorCGF);
2601 CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI,
2602 FunctionArgList(), Loc, Loc);
2603 auto AL = ApplyDebugLocation::CreateArtificial(CtorCGF);
2604 CtorCGF.EmitAnyExprToMem(Init,
2605 Address(Addr, CGM.getContext().getDeclAlign(VD)),
2606 Init->getType().getQualifiers(),
2607 /*IsInitializer=*/true);
2608 CtorCGF.FinishFunction();
2609 Ctor = Fn;
2610 ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy);
2611 } else {
2612 Ctor = new llvm::GlobalVariable(
2613 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
2614 llvm::GlobalValue::PrivateLinkage,
2615 llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_ctor"));
2616 ID = Ctor;
2617 }
2618
2619 // Register the information for the entry associated with the constructor.
2620 Out.clear();
2621 OffloadEntriesInfoManager.registerTargetRegionEntryInfo(
2622 DeviceID, FileID, Twine(Buffer, "_ctor").toStringRef(Out), Line, Ctor,
Alexey Bataev03f270c2018-03-30 18:31:07 +00002623 ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryCtor);
Alexey Bataev34f8a702018-03-28 14:28:54 +00002624 }
2625 if (VD->getType().isDestructedType() != QualType::DK_none) {
2626 llvm::Constant *Dtor;
2627 llvm::Constant *ID;
2628 if (CGM.getLangOpts().OpenMPIsDevice) {
2629 // Generate function that emits destructor call for the threadprivate
2630 // copy of the variable VD
2631 CodeGenFunction DtorCGF(CGM);
2632
2633 const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
2634 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
2635 llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction(
2636 FTy, Twine(Buffer, "_dtor"), FI, Loc);
2637 auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF);
2638 DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI,
2639 FunctionArgList(), Loc, Loc);
2640 // Create a scope with an artificial location for the body of this
2641 // function.
2642 auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF);
2643 DtorCGF.emitDestroy(Address(Addr, CGM.getContext().getDeclAlign(VD)),
2644 ASTTy, DtorCGF.getDestroyer(ASTTy.isDestructedType()),
2645 DtorCGF.needsEHCleanup(ASTTy.isDestructedType()));
2646 DtorCGF.FinishFunction();
2647 Dtor = Fn;
2648 ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy);
2649 } else {
2650 Dtor = new llvm::GlobalVariable(
2651 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
2652 llvm::GlobalValue::PrivateLinkage,
2653 llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_dtor"));
2654 ID = Dtor;
2655 }
2656 // Register the information for the entry associated with the destructor.
2657 Out.clear();
2658 OffloadEntriesInfoManager.registerTargetRegionEntryInfo(
2659 DeviceID, FileID, Twine(Buffer, "_dtor").toStringRef(Out), Line, Dtor,
Alexey Bataev03f270c2018-03-30 18:31:07 +00002660 ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryDtor);
Alexey Bataev34f8a702018-03-28 14:28:54 +00002661 }
2662 return CGM.getLangOpts().OpenMPIsDevice;
2663}
2664
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002665Address CGOpenMPRuntime::getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
2666 QualType VarType,
2667 StringRef Name) {
2668 llvm::Twine VarName(Name, ".artificial.");
2669 llvm::Type *VarLVType = CGF.ConvertTypeForMem(VarType);
2670 llvm::Value *GAddr = getOrCreateInternalVariable(VarLVType, VarName);
2671 llvm::Value *Args[] = {
2672 emitUpdateLocation(CGF, SourceLocation()),
2673 getThreadID(CGF, SourceLocation()),
2674 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(GAddr, CGM.VoidPtrTy),
2675 CGF.Builder.CreateIntCast(CGF.getTypeSize(VarType), CGM.SizeTy,
2676 /*IsSigned=*/false),
2677 getOrCreateInternalVariable(CGM.VoidPtrPtrTy, VarName + ".cache.")};
2678 return Address(
2679 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
2680 CGF.EmitRuntimeCall(
2681 createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args),
2682 VarLVType->getPointerTo(/*AddrSpace=*/0)),
2683 CGM.getPointerAlign());
2684}
2685
Alexey Bataev1d677132015-04-22 13:57:31 +00002686/// \brief Emits code for OpenMP 'if' clause using specified \a CodeGen
2687/// function. Here is the logic:
2688/// if (Cond) {
2689/// ThenGen();
2690/// } else {
2691/// ElseGen();
2692/// }
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00002693void CGOpenMPRuntime::emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond,
2694 const RegionCodeGenTy &ThenGen,
2695 const RegionCodeGenTy &ElseGen) {
Alexey Bataev1d677132015-04-22 13:57:31 +00002696 CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange());
2697
2698 // If the condition constant folds and can be elided, try to avoid emitting
2699 // the condition and the dead arm of the if/else.
2700 bool CondConstant;
2701 if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002702 if (CondConstant)
Alexey Bataev1d677132015-04-22 13:57:31 +00002703 ThenGen(CGF);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002704 else
Alexey Bataev1d677132015-04-22 13:57:31 +00002705 ElseGen(CGF);
Alexey Bataev1d677132015-04-22 13:57:31 +00002706 return;
2707 }
2708
2709 // Otherwise, the condition did not fold, or we couldn't elide it. Just
2710 // emit the conditional branch.
2711 auto ThenBlock = CGF.createBasicBlock("omp_if.then");
2712 auto ElseBlock = CGF.createBasicBlock("omp_if.else");
2713 auto ContBlock = CGF.createBasicBlock("omp_if.end");
2714 CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount=*/0);
2715
2716 // Emit the 'then' code.
2717 CGF.EmitBlock(ThenBlock);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002718 ThenGen(CGF);
Alexey Bataev1d677132015-04-22 13:57:31 +00002719 CGF.EmitBranch(ContBlock);
2720 // Emit the 'else' code if present.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002721 // There is no need to emit line number for unconditional branch.
2722 (void)ApplyDebugLocation::CreateEmpty(CGF);
2723 CGF.EmitBlock(ElseBlock);
2724 ElseGen(CGF);
2725 // There is no need to emit line number for unconditional branch.
2726 (void)ApplyDebugLocation::CreateEmpty(CGF);
2727 CGF.EmitBranch(ContBlock);
Alexey Bataev1d677132015-04-22 13:57:31 +00002728 // Emit the continuation block for code after the if.
2729 CGF.EmitBlock(ContBlock, /*IsFinished=*/true);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00002730}
2731
Alexey Bataev1d677132015-04-22 13:57:31 +00002732void CGOpenMPRuntime::emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
2733 llvm::Value *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +00002734 ArrayRef<llvm::Value *> CapturedVars,
Alexey Bataev1d677132015-04-22 13:57:31 +00002735 const Expr *IfCond) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002736 if (!CGF.HaveInsertPoint())
2737 return;
Alexey Bataev1d677132015-04-22 13:57:31 +00002738 auto *RTLoc = emitUpdateLocation(CGF, Loc);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002739 auto &&ThenGen = [OutlinedFn, CapturedVars, RTLoc](CodeGenFunction &CGF,
2740 PrePostActionTy &) {
Alexey Bataev2377fe92015-09-10 08:12:02 +00002741 // Build call __kmpc_fork_call(loc, n, microtask, var1, .., varn);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002742 auto &RT = CGF.CGM.getOpenMPRuntime();
Alexey Bataev2377fe92015-09-10 08:12:02 +00002743 llvm::Value *Args[] = {
2744 RTLoc,
2745 CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002746 CGF.Builder.CreateBitCast(OutlinedFn, RT.getKmpc_MicroPointerTy())};
Alexey Bataev2377fe92015-09-10 08:12:02 +00002747 llvm::SmallVector<llvm::Value *, 16> RealArgs;
2748 RealArgs.append(std::begin(Args), std::end(Args));
2749 RealArgs.append(CapturedVars.begin(), CapturedVars.end());
2750
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002751 auto RTLFn = RT.createRuntimeFunction(OMPRTL__kmpc_fork_call);
Alexey Bataev2377fe92015-09-10 08:12:02 +00002752 CGF.EmitRuntimeCall(RTLFn, RealArgs);
2753 };
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002754 auto &&ElseGen = [OutlinedFn, CapturedVars, RTLoc, Loc](CodeGenFunction &CGF,
2755 PrePostActionTy &) {
2756 auto &RT = CGF.CGM.getOpenMPRuntime();
2757 auto ThreadID = RT.getThreadID(CGF, Loc);
Alexey Bataev1d677132015-04-22 13:57:31 +00002758 // Build calls:
2759 // __kmpc_serialized_parallel(&Loc, GTid);
2760 llvm::Value *Args[] = {RTLoc, ThreadID};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002761 CGF.EmitRuntimeCall(
2762 RT.createRuntimeFunction(OMPRTL__kmpc_serialized_parallel), Args);
Alexey Bataevd74d0602014-10-13 06:02:40 +00002763
Alexey Bataev1d677132015-04-22 13:57:31 +00002764 // OutlinedFn(&GTid, &zero, CapturedStruct);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002765 auto ThreadIDAddr = RT.emitThreadIDAddress(CGF, Loc);
John McCall7f416cc2015-09-08 08:05:57 +00002766 Address ZeroAddr =
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002767 CGF.CreateTempAlloca(CGF.Int32Ty, CharUnits::fromQuantity(4),
2768 /*Name*/ ".zero.addr");
Alexey Bataev1d677132015-04-22 13:57:31 +00002769 CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
Alexey Bataev2377fe92015-09-10 08:12:02 +00002770 llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs;
2771 OutlinedFnArgs.push_back(ThreadIDAddr.getPointer());
2772 OutlinedFnArgs.push_back(ZeroAddr.getPointer());
2773 OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end());
Alexey Bataev3c595a62017-08-14 15:01:03 +00002774 RT.emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs);
Alexey Bataevd74d0602014-10-13 06:02:40 +00002775
Alexey Bataev1d677132015-04-22 13:57:31 +00002776 // __kmpc_end_serialized_parallel(&Loc, GTid);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002777 llvm::Value *EndArgs[] = {RT.emitUpdateLocation(CGF, Loc), ThreadID};
Alexey Bataev1d677132015-04-22 13:57:31 +00002778 CGF.EmitRuntimeCall(
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002779 RT.createRuntimeFunction(OMPRTL__kmpc_end_serialized_parallel),
2780 EndArgs);
Alexey Bataev1d677132015-04-22 13:57:31 +00002781 };
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002782 if (IfCond)
Alexey Bataev1d677132015-04-22 13:57:31 +00002783 emitOMPIfClause(CGF, IfCond, ThenGen, ElseGen);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002784 else {
2785 RegionCodeGenTy ThenRCG(ThenGen);
2786 ThenRCG(CGF);
Alexey Bataevf539faa2016-03-28 12:58:34 +00002787 }
Alexey Bataevd74d0602014-10-13 06:02:40 +00002788}
2789
NAKAMURA Takumi59c74b222014-10-27 08:08:18 +00002790// If we're inside an (outlined) parallel region, use the region info's
Alexey Bataevd74d0602014-10-13 06:02:40 +00002791// thread-ID variable (it is passed in a first argument of the outlined function
2792// as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in
2793// regular serial code region, get thread ID by calling kmp_int32
2794// kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and
2795// return the address of that temp.
John McCall7f416cc2015-09-08 08:05:57 +00002796Address CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF,
2797 SourceLocation Loc) {
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002798 if (auto *OMPRegionInfo =
Alexey Bataevd74d0602014-10-13 06:02:40 +00002799 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00002800 if (OMPRegionInfo->getThreadIDVariable())
Alexey Bataev62b63b12015-03-10 07:28:44 +00002801 return OMPRegionInfo->getThreadIDVariableLValue(CGF).getAddress();
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00002802
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002803 auto ThreadID = getThreadID(CGF, Loc);
Alexey Bataevd74d0602014-10-13 06:02:40 +00002804 auto Int32Ty =
2805 CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true);
2806 auto ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp.");
2807 CGF.EmitStoreOfScalar(ThreadID,
John McCall7f416cc2015-09-08 08:05:57 +00002808 CGF.MakeAddrLValue(ThreadIDTemp, Int32Ty));
Alexey Bataevd74d0602014-10-13 06:02:40 +00002809
2810 return ThreadIDTemp;
2811}
2812
Alexey Bataev97720002014-11-11 04:05:39 +00002813llvm::Constant *
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002814CGOpenMPRuntime::getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +00002815 const llvm::Twine &Name) {
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002816 SmallString<256> Buffer;
2817 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev97720002014-11-11 04:05:39 +00002818 Out << Name;
2819 auto RuntimeName = Out.str();
David Blaikie13156b62014-11-19 03:06:06 +00002820 auto &Elem = *InternalVars.insert(std::make_pair(RuntimeName, nullptr)).first;
2821 if (Elem.second) {
2822 assert(Elem.second->getType()->getPointerElementType() == Ty &&
Alexey Bataev97720002014-11-11 04:05:39 +00002823 "OMP internal variable has different type than requested");
David Blaikie13156b62014-11-19 03:06:06 +00002824 return &*Elem.second;
Alexey Bataev97720002014-11-11 04:05:39 +00002825 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002826
David Blaikie13156b62014-11-19 03:06:06 +00002827 return Elem.second = new llvm::GlobalVariable(
2828 CGM.getModule(), Ty, /*IsConstant*/ false,
2829 llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty),
2830 Elem.first());
Alexey Bataev97720002014-11-11 04:05:39 +00002831}
2832
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002833llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) {
Alexey Bataev97720002014-11-11 04:05:39 +00002834 llvm::Twine Name(".gomp_critical_user_", CriticalName);
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002835 return getOrCreateInternalVariable(KmpCriticalNameTy, Name.concat(".var"));
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002836}
2837
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002838namespace {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002839/// Common pre(post)-action for different OpenMP constructs.
2840class CommonActionTy final : public PrePostActionTy {
2841 llvm::Value *EnterCallee;
2842 ArrayRef<llvm::Value *> EnterArgs;
2843 llvm::Value *ExitCallee;
2844 ArrayRef<llvm::Value *> ExitArgs;
2845 bool Conditional;
2846 llvm::BasicBlock *ContBlock = nullptr;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002847
2848public:
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002849 CommonActionTy(llvm::Value *EnterCallee, ArrayRef<llvm::Value *> EnterArgs,
2850 llvm::Value *ExitCallee, ArrayRef<llvm::Value *> ExitArgs,
2851 bool Conditional = false)
2852 : EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee),
2853 ExitArgs(ExitArgs), Conditional(Conditional) {}
2854 void Enter(CodeGenFunction &CGF) override {
2855 llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs);
2856 if (Conditional) {
2857 llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes);
2858 auto *ThenBlock = CGF.createBasicBlock("omp_if.then");
2859 ContBlock = CGF.createBasicBlock("omp_if.end");
2860 // Generate the branch (If-stmt)
2861 CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock);
2862 CGF.EmitBlock(ThenBlock);
2863 }
Alexey Bataeva744ff52015-05-05 09:24:37 +00002864 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002865 void Done(CodeGenFunction &CGF) {
2866 // Emit the rest of blocks/branches
2867 CGF.EmitBranch(ContBlock);
2868 CGF.EmitBlock(ContBlock, true);
2869 }
2870 void Exit(CodeGenFunction &CGF) override {
2871 CGF.EmitRuntimeCall(ExitCallee, ExitArgs);
Alexey Bataev3e6124b2015-04-10 07:48:12 +00002872 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002873};
Hans Wennborg7eb54642015-09-10 17:07:54 +00002874} // anonymous namespace
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002875
2876void CGOpenMPRuntime::emitCriticalRegion(CodeGenFunction &CGF,
2877 StringRef CriticalName,
2878 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +00002879 SourceLocation Loc, const Expr *Hint) {
2880 // __kmpc_critical[_with_hint](ident_t *, gtid, Lock[, hint]);
Alexey Bataev75ddfab2014-12-01 11:32:38 +00002881 // CriticalOpGen();
2882 // __kmpc_end_critical(ident_t *, gtid, Lock);
2883 // Prepare arguments and build a call to __kmpc_critical
Alexey Bataev8ef31412015-12-18 07:58:25 +00002884 if (!CGF.HaveInsertPoint())
2885 return;
Alexey Bataevfc57d162015-12-15 10:55:09 +00002886 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
2887 getCriticalRegionLock(CriticalName)};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002888 llvm::SmallVector<llvm::Value *, 4> EnterArgs(std::begin(Args),
2889 std::end(Args));
Alexey Bataevfc57d162015-12-15 10:55:09 +00002890 if (Hint) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002891 EnterArgs.push_back(CGF.Builder.CreateIntCast(
2892 CGF.EmitScalarExpr(Hint), CGM.IntPtrTy, /*isSigned=*/false));
2893 }
2894 CommonActionTy Action(
2895 createRuntimeFunction(Hint ? OMPRTL__kmpc_critical_with_hint
2896 : OMPRTL__kmpc_critical),
2897 EnterArgs, createRuntimeFunction(OMPRTL__kmpc_end_critical), Args);
2898 CriticalOpGen.setAction(Action);
Alexey Bataevfc57d162015-12-15 10:55:09 +00002899 emitInlinedDirective(CGF, OMPD_critical, CriticalOpGen);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002900}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00002901
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002902void CGOpenMPRuntime::emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002903 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002904 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002905 if (!CGF.HaveInsertPoint())
2906 return;
Alexey Bataev8d690652014-12-04 07:23:53 +00002907 // if(__kmpc_master(ident_t *, gtid)) {
2908 // MasterOpGen();
2909 // __kmpc_end_master(ident_t *, gtid);
2910 // }
2911 // Prepare arguments and build a call to __kmpc_master
Alexey Bataevd7614fb2015-04-10 06:33:45 +00002912 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002913 CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_master), Args,
2914 createRuntimeFunction(OMPRTL__kmpc_end_master), Args,
2915 /*Conditional=*/true);
2916 MasterOpGen.setAction(Action);
2917 emitInlinedDirective(CGF, OMPD_master, MasterOpGen);
2918 Action.Done(CGF);
Alexey Bataev8d690652014-12-04 07:23:53 +00002919}
2920
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002921void CGOpenMPRuntime::emitTaskyieldCall(CodeGenFunction &CGF,
2922 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002923 if (!CGF.HaveInsertPoint())
2924 return;
Alexey Bataev9f797f32015-02-05 05:57:51 +00002925 // Build call __kmpc_omp_taskyield(loc, thread_id, 0);
2926 llvm::Value *Args[] = {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002927 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
Alexey Bataev9f797f32015-02-05 05:57:51 +00002928 llvm::ConstantInt::get(CGM.IntTy, /*V=*/0, /*isSigned=*/true)};
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002929 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskyield), Args);
Alexey Bataev48591dd2016-04-20 04:01:36 +00002930 if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
2931 Region->emitUntiedSwitch(CGF);
Alexey Bataev9f797f32015-02-05 05:57:51 +00002932}
2933
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002934void CGOpenMPRuntime::emitTaskgroupRegion(CodeGenFunction &CGF,
2935 const RegionCodeGenTy &TaskgroupOpGen,
2936 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002937 if (!CGF.HaveInsertPoint())
2938 return;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002939 // __kmpc_taskgroup(ident_t *, gtid);
2940 // TaskgroupOpGen();
2941 // __kmpc_end_taskgroup(ident_t *, gtid);
2942 // Prepare arguments and build a call to __kmpc_taskgroup
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002943 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
2944 CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_taskgroup), Args,
2945 createRuntimeFunction(OMPRTL__kmpc_end_taskgroup),
2946 Args);
2947 TaskgroupOpGen.setAction(Action);
2948 emitInlinedDirective(CGF, OMPD_taskgroup, TaskgroupOpGen);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002949}
2950
John McCall7f416cc2015-09-08 08:05:57 +00002951/// Given an array of pointers to variables, project the address of a
2952/// given variable.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00002953static Address emitAddrOfVarFromArray(CodeGenFunction &CGF, Address Array,
2954 unsigned Index, const VarDecl *Var) {
John McCall7f416cc2015-09-08 08:05:57 +00002955 // Pull out the pointer to the variable.
2956 Address PtrAddr =
Alexey Bataevf24e7b12015-10-08 09:10:53 +00002957 CGF.Builder.CreateConstArrayGEP(Array, Index, CGF.getPointerSize());
John McCall7f416cc2015-09-08 08:05:57 +00002958 llvm::Value *Ptr = CGF.Builder.CreateLoad(PtrAddr);
2959
2960 Address Addr = Address(Ptr, CGF.getContext().getDeclAlign(Var));
Alexey Bataevf24e7b12015-10-08 09:10:53 +00002961 Addr = CGF.Builder.CreateElementBitCast(
2962 Addr, CGF.ConvertTypeForMem(Var->getType()));
John McCall7f416cc2015-09-08 08:05:57 +00002963 return Addr;
2964}
2965
Alexey Bataeva63048e2015-03-23 06:18:07 +00002966static llvm::Value *emitCopyprivateCopyFunction(
Alexey Bataev420d45b2015-04-14 05:11:24 +00002967 CodeGenModule &CGM, llvm::Type *ArgsType,
2968 ArrayRef<const Expr *> CopyprivateVars, ArrayRef<const Expr *> DestExprs,
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002969 ArrayRef<const Expr *> SrcExprs, ArrayRef<const Expr *> AssignmentOps,
2970 SourceLocation Loc) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00002971 auto &C = CGM.getContext();
2972 // void copy_func(void *LHSArg, void *RHSArg);
2973 FunctionArgList Args;
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002974 ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
2975 ImplicitParamDecl::Other);
2976 ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
2977 ImplicitParamDecl::Other);
Alexey Bataeva63048e2015-03-23 06:18:07 +00002978 Args.push_back(&LHSArg);
2979 Args.push_back(&RHSArg);
John McCallc56a8b32016-03-11 04:30:31 +00002980 auto &CGFI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataeva63048e2015-03-23 06:18:07 +00002981 auto *Fn = llvm::Function::Create(
2982 CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
2983 ".omp.copyprivate.copy_func", &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00002984 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
Alexey Bataeva63048e2015-03-23 06:18:07 +00002985 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002986 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00002987 // Dest = (void*[n])(LHSArg);
Alexey Bataeva63048e2015-03-23 06:18:07 +00002988 // Src = (void*[n])(RHSArg);
John McCall7f416cc2015-09-08 08:05:57 +00002989 Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
2990 CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)),
2991 ArgsType), CGF.getPointerAlign());
2992 Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
2993 CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)),
2994 ArgsType), CGF.getPointerAlign());
Alexey Bataeva63048e2015-03-23 06:18:07 +00002995 // *(Type0*)Dst[0] = *(Type0*)Src[0];
2996 // *(Type1*)Dst[1] = *(Type1*)Src[1];
2997 // ...
2998 // *(Typen*)Dst[n] = *(Typen*)Src[n];
Alexey Bataeva63048e2015-03-23 06:18:07 +00002999 for (unsigned I = 0, E = AssignmentOps.size(); I < E; ++I) {
John McCall7f416cc2015-09-08 08:05:57 +00003000 auto DestVar = cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl());
3001 Address DestAddr = emitAddrOfVarFromArray(CGF, LHS, I, DestVar);
3002
3003 auto SrcVar = cast<VarDecl>(cast<DeclRefExpr>(SrcExprs[I])->getDecl());
3004 Address SrcAddr = emitAddrOfVarFromArray(CGF, RHS, I, SrcVar);
3005
Alexey Bataev1d9c15c2015-05-19 12:31:28 +00003006 auto *VD = cast<DeclRefExpr>(CopyprivateVars[I])->getDecl();
3007 QualType Type = VD->getType();
John McCall7f416cc2015-09-08 08:05:57 +00003008 CGF.EmitOMPCopy(Type, DestAddr, SrcAddr, DestVar, SrcVar, AssignmentOps[I]);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003009 }
Alexey Bataeva63048e2015-03-23 06:18:07 +00003010 CGF.FinishFunction();
3011 return Fn;
3012}
3013
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003014void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00003015 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +00003016 SourceLocation Loc,
3017 ArrayRef<const Expr *> CopyprivateVars,
3018 ArrayRef<const Expr *> SrcExprs,
3019 ArrayRef<const Expr *> DstExprs,
3020 ArrayRef<const Expr *> AssignmentOps) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003021 if (!CGF.HaveInsertPoint())
3022 return;
Alexey Bataeva63048e2015-03-23 06:18:07 +00003023 assert(CopyprivateVars.size() == SrcExprs.size() &&
3024 CopyprivateVars.size() == DstExprs.size() &&
3025 CopyprivateVars.size() == AssignmentOps.size());
3026 auto &C = CGM.getContext();
3027 // int32 did_it = 0;
Alexey Bataev6956e2e2015-02-05 06:35:41 +00003028 // if(__kmpc_single(ident_t *, gtid)) {
3029 // SingleOpGen();
3030 // __kmpc_end_single(ident_t *, gtid);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003031 // did_it = 1;
Alexey Bataev6956e2e2015-02-05 06:35:41 +00003032 // }
Alexey Bataeva63048e2015-03-23 06:18:07 +00003033 // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>,
3034 // <copy_func>, did_it);
3035
John McCall7f416cc2015-09-08 08:05:57 +00003036 Address DidIt = Address::invalid();
Alexey Bataeva63048e2015-03-23 06:18:07 +00003037 if (!CopyprivateVars.empty()) {
3038 // int32 did_it = 0;
3039 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
3040 DidIt = CGF.CreateMemTemp(KmpInt32Ty, ".omp.copyprivate.did_it");
John McCall7f416cc2015-09-08 08:05:57 +00003041 CGF.Builder.CreateStore(CGF.Builder.getInt32(0), DidIt);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003042 }
Alexey Bataev6956e2e2015-02-05 06:35:41 +00003043 // Prepare arguments and build a call to __kmpc_single
Alexey Bataevd7614fb2015-04-10 06:33:45 +00003044 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003045 CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_single), Args,
3046 createRuntimeFunction(OMPRTL__kmpc_end_single), Args,
3047 /*Conditional=*/true);
3048 SingleOpGen.setAction(Action);
3049 emitInlinedDirective(CGF, OMPD_single, SingleOpGen);
3050 if (DidIt.isValid()) {
3051 // did_it = 1;
3052 CGF.Builder.CreateStore(CGF.Builder.getInt32(1), DidIt);
3053 }
3054 Action.Done(CGF);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003055 // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>,
3056 // <copy_func>, did_it);
John McCall7f416cc2015-09-08 08:05:57 +00003057 if (DidIt.isValid()) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00003058 llvm::APInt ArraySize(/*unsigned int numBits=*/32, CopyprivateVars.size());
3059 auto CopyprivateArrayTy =
3060 C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal,
3061 /*IndexTypeQuals=*/0);
3062 // Create a list of all private variables for copyprivate.
John McCall7f416cc2015-09-08 08:05:57 +00003063 Address CopyprivateList =
Alexey Bataeva63048e2015-03-23 06:18:07 +00003064 CGF.CreateMemTemp(CopyprivateArrayTy, ".omp.copyprivate.cpr_list");
3065 for (unsigned I = 0, E = CopyprivateVars.size(); I < E; ++I) {
John McCall7f416cc2015-09-08 08:05:57 +00003066 Address Elem = CGF.Builder.CreateConstArrayGEP(
3067 CopyprivateList, I, CGF.getPointerSize());
3068 CGF.Builder.CreateStore(
Alexey Bataeva63048e2015-03-23 06:18:07 +00003069 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
John McCall7f416cc2015-09-08 08:05:57 +00003070 CGF.EmitLValue(CopyprivateVars[I]).getPointer(), CGF.VoidPtrTy),
3071 Elem);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003072 }
3073 // Build function that copies private values from single region to all other
3074 // threads in the corresponding parallel region.
3075 auto *CpyFn = emitCopyprivateCopyFunction(
3076 CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy)->getPointerTo(),
Alexey Bataev7cae94e2018-01-04 19:45:16 +00003077 CopyprivateVars, SrcExprs, DstExprs, AssignmentOps, Loc);
Alexey Bataev1189bd02016-01-26 12:20:39 +00003078 auto *BufSize = CGF.getTypeSize(CopyprivateArrayTy);
John McCall7f416cc2015-09-08 08:05:57 +00003079 Address CL =
3080 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(CopyprivateList,
3081 CGF.VoidPtrTy);
3082 auto *DidItVal = CGF.Builder.CreateLoad(DidIt);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003083 llvm::Value *Args[] = {
3084 emitUpdateLocation(CGF, Loc), // ident_t *<loc>
3085 getThreadID(CGF, Loc), // i32 <gtid>
Alexey Bataev66beaa92015-04-30 03:47:32 +00003086 BufSize, // size_t <buf_size>
John McCall7f416cc2015-09-08 08:05:57 +00003087 CL.getPointer(), // void *<copyprivate list>
Alexey Bataeva63048e2015-03-23 06:18:07 +00003088 CpyFn, // void (*) (void *, void *) <copy_func>
3089 DidItVal // i32 did_it
3090 };
3091 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_copyprivate), Args);
3092 }
Alexey Bataev6956e2e2015-02-05 06:35:41 +00003093}
3094
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003095void CGOpenMPRuntime::emitOrderedRegion(CodeGenFunction &CGF,
3096 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +00003097 SourceLocation Loc, bool IsThreads) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003098 if (!CGF.HaveInsertPoint())
3099 return;
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003100 // __kmpc_ordered(ident_t *, gtid);
3101 // OrderedOpGen();
3102 // __kmpc_end_ordered(ident_t *, gtid);
3103 // Prepare arguments and build a call to __kmpc_ordered
Alexey Bataev5f600d62015-09-29 03:48:57 +00003104 if (IsThreads) {
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003105 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003106 CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_ordered), Args,
3107 createRuntimeFunction(OMPRTL__kmpc_end_ordered),
3108 Args);
3109 OrderedOpGen.setAction(Action);
3110 emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen);
3111 return;
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003112 }
Alexey Bataev5f600d62015-09-29 03:48:57 +00003113 emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003114}
3115
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003116void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00003117 OpenMPDirectiveKind Kind, bool EmitChecks,
3118 bool ForceSimpleCall) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003119 if (!CGF.HaveInsertPoint())
3120 return;
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00003121 // Build call __kmpc_cancel_barrier(loc, thread_id);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003122 // Build call __kmpc_barrier(loc, thread_id);
Alexey Bataev50b3c952016-02-19 10:38:26 +00003123 unsigned Flags;
3124 if (Kind == OMPD_for)
3125 Flags = OMP_IDENT_BARRIER_IMPL_FOR;
3126 else if (Kind == OMPD_sections)
3127 Flags = OMP_IDENT_BARRIER_IMPL_SECTIONS;
3128 else if (Kind == OMPD_single)
3129 Flags = OMP_IDENT_BARRIER_IMPL_SINGLE;
3130 else if (Kind == OMPD_barrier)
3131 Flags = OMP_IDENT_BARRIER_EXPL;
3132 else
3133 Flags = OMP_IDENT_BARRIER_IMPL;
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003134 // Build call __kmpc_cancel_barrier(loc, thread_id) or __kmpc_barrier(loc,
3135 // thread_id);
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003136 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, Flags),
3137 getThreadID(CGF, Loc)};
Alexey Bataev3015bcc2016-01-22 08:56:50 +00003138 if (auto *OMPRegionInfo =
3139 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
Alexey Bataev25e5b442015-09-15 12:52:43 +00003140 if (!ForceSimpleCall && OMPRegionInfo->hasCancel()) {
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003141 auto *Result = CGF.EmitRuntimeCall(
3142 createRuntimeFunction(OMPRTL__kmpc_cancel_barrier), Args);
Alexey Bataev25e5b442015-09-15 12:52:43 +00003143 if (EmitChecks) {
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003144 // if (__kmpc_cancel_barrier()) {
3145 // exit from construct;
3146 // }
3147 auto *ExitBB = CGF.createBasicBlock(".cancel.exit");
3148 auto *ContBB = CGF.createBasicBlock(".cancel.continue");
3149 auto *Cmp = CGF.Builder.CreateIsNotNull(Result);
3150 CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB);
3151 CGF.EmitBlock(ExitBB);
3152 // exit from construct;
Alexey Bataev25e5b442015-09-15 12:52:43 +00003153 auto CancelDestination =
3154 CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind());
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003155 CGF.EmitBranchThroughCleanup(CancelDestination);
3156 CGF.EmitBlock(ContBB, /*IsFinished=*/true);
3157 }
3158 return;
3159 }
3160 }
3161 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_barrier), Args);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00003162}
3163
Alexander Musmanc6388682014-12-15 07:07:06 +00003164/// \brief Map the OpenMP loop schedule to the runtime enumeration.
3165static OpenMPSchedType getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003166 bool Chunked, bool Ordered) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003167 switch (ScheduleKind) {
3168 case OMPC_SCHEDULE_static:
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003169 return Chunked ? (Ordered ? OMP_ord_static_chunked : OMP_sch_static_chunked)
3170 : (Ordered ? OMP_ord_static : OMP_sch_static);
Alexander Musmanc6388682014-12-15 07:07:06 +00003171 case OMPC_SCHEDULE_dynamic:
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003172 return Ordered ? OMP_ord_dynamic_chunked : OMP_sch_dynamic_chunked;
Alexander Musmanc6388682014-12-15 07:07:06 +00003173 case OMPC_SCHEDULE_guided:
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003174 return Ordered ? OMP_ord_guided_chunked : OMP_sch_guided_chunked;
Alexander Musmanc6388682014-12-15 07:07:06 +00003175 case OMPC_SCHEDULE_runtime:
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003176 return Ordered ? OMP_ord_runtime : OMP_sch_runtime;
3177 case OMPC_SCHEDULE_auto:
3178 return Ordered ? OMP_ord_auto : OMP_sch_auto;
Alexander Musmanc6388682014-12-15 07:07:06 +00003179 case OMPC_SCHEDULE_unknown:
3180 assert(!Chunked && "chunk was specified but schedule kind not known");
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003181 return Ordered ? OMP_ord_static : OMP_sch_static;
Alexander Musmanc6388682014-12-15 07:07:06 +00003182 }
3183 llvm_unreachable("Unexpected runtime schedule");
3184}
3185
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003186/// \brief Map the OpenMP distribute schedule to the runtime enumeration.
3187static OpenMPSchedType
3188getRuntimeSchedule(OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) {
3189 // only static is allowed for dist_schedule
3190 return Chunked ? OMP_dist_sch_static_chunked : OMP_dist_sch_static;
3191}
3192
Alexander Musmanc6388682014-12-15 07:07:06 +00003193bool CGOpenMPRuntime::isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
3194 bool Chunked) const {
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003195 auto Schedule = getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false);
Alexander Musmanc6388682014-12-15 07:07:06 +00003196 return Schedule == OMP_sch_static;
3197}
3198
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003199bool CGOpenMPRuntime::isStaticNonchunked(
3200 OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const {
3201 auto Schedule = getRuntimeSchedule(ScheduleKind, Chunked);
3202 return Schedule == OMP_dist_sch_static;
3203}
3204
3205
Alexander Musmandf7a8e22015-01-22 08:49:35 +00003206bool CGOpenMPRuntime::isDynamic(OpenMPScheduleClauseKind ScheduleKind) const {
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003207 auto Schedule =
3208 getRuntimeSchedule(ScheduleKind, /*Chunked=*/false, /*Ordered=*/false);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00003209 assert(Schedule != OMP_sch_static_chunked && "cannot be chunked here");
3210 return Schedule != OMP_sch_static;
3211}
3212
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003213static int addMonoNonMonoModifier(OpenMPSchedType Schedule,
3214 OpenMPScheduleClauseModifier M1,
3215 OpenMPScheduleClauseModifier M2) {
Alexey Bataev6cff6242016-05-30 13:05:14 +00003216 int Modifier = 0;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003217 switch (M1) {
3218 case OMPC_SCHEDULE_MODIFIER_monotonic:
Alexey Bataev6cff6242016-05-30 13:05:14 +00003219 Modifier = OMP_sch_modifier_monotonic;
3220 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003221 case OMPC_SCHEDULE_MODIFIER_nonmonotonic:
Alexey Bataev6cff6242016-05-30 13:05:14 +00003222 Modifier = OMP_sch_modifier_nonmonotonic;
3223 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003224 case OMPC_SCHEDULE_MODIFIER_simd:
Alexey Bataev6cff6242016-05-30 13:05:14 +00003225 if (Schedule == OMP_sch_static_chunked)
3226 Schedule = OMP_sch_static_balanced_chunked;
3227 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003228 case OMPC_SCHEDULE_MODIFIER_last:
3229 case OMPC_SCHEDULE_MODIFIER_unknown:
3230 break;
3231 }
3232 switch (M2) {
3233 case OMPC_SCHEDULE_MODIFIER_monotonic:
Alexey Bataev6cff6242016-05-30 13:05:14 +00003234 Modifier = OMP_sch_modifier_monotonic;
3235 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003236 case OMPC_SCHEDULE_MODIFIER_nonmonotonic:
Alexey Bataev6cff6242016-05-30 13:05:14 +00003237 Modifier = OMP_sch_modifier_nonmonotonic;
3238 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003239 case OMPC_SCHEDULE_MODIFIER_simd:
Alexey Bataev6cff6242016-05-30 13:05:14 +00003240 if (Schedule == OMP_sch_static_chunked)
3241 Schedule = OMP_sch_static_balanced_chunked;
3242 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003243 case OMPC_SCHEDULE_MODIFIER_last:
3244 case OMPC_SCHEDULE_MODIFIER_unknown:
3245 break;
3246 }
Alexey Bataev6cff6242016-05-30 13:05:14 +00003247 return Schedule | Modifier;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003248}
3249
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003250void CGOpenMPRuntime::emitForDispatchInit(
3251 CodeGenFunction &CGF, SourceLocation Loc,
3252 const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned,
3253 bool Ordered, const DispatchRTInput &DispatchValues) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003254 if (!CGF.HaveInsertPoint())
3255 return;
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003256 OpenMPSchedType Schedule = getRuntimeSchedule(
3257 ScheduleKind.Schedule, DispatchValues.Chunk != nullptr, Ordered);
John McCall7f416cc2015-09-08 08:05:57 +00003258 assert(Ordered ||
3259 (Schedule != OMP_sch_static && Schedule != OMP_sch_static_chunked &&
Alexey Bataev6cff6242016-05-30 13:05:14 +00003260 Schedule != OMP_ord_static && Schedule != OMP_ord_static_chunked &&
3261 Schedule != OMP_sch_static_balanced_chunked));
John McCall7f416cc2015-09-08 08:05:57 +00003262 // Call __kmpc_dispatch_init(
3263 // ident_t *loc, kmp_int32 tid, kmp_int32 schedule,
3264 // kmp_int[32|64] lower, kmp_int[32|64] upper,
3265 // kmp_int[32|64] stride, kmp_int[32|64] chunk);
Alexander Musmanc6388682014-12-15 07:07:06 +00003266
John McCall7f416cc2015-09-08 08:05:57 +00003267 // If the Chunk was not specified in the clause - use default value 1.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003268 llvm::Value *Chunk = DispatchValues.Chunk ? DispatchValues.Chunk
3269 : CGF.Builder.getIntN(IVSize, 1);
John McCall7f416cc2015-09-08 08:05:57 +00003270 llvm::Value *Args[] = {
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003271 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
3272 CGF.Builder.getInt32(addMonoNonMonoModifier(
3273 Schedule, ScheduleKind.M1, ScheduleKind.M2)), // Schedule type
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003274 DispatchValues.LB, // Lower
3275 DispatchValues.UB, // Upper
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003276 CGF.Builder.getIntN(IVSize, 1), // Stride
3277 Chunk // Chunk
John McCall7f416cc2015-09-08 08:05:57 +00003278 };
3279 CGF.EmitRuntimeCall(createDispatchInitFunction(IVSize, IVSigned), Args);
3280}
3281
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003282static void emitForStaticInitCall(
3283 CodeGenFunction &CGF, llvm::Value *UpdateLocation, llvm::Value *ThreadId,
3284 llvm::Constant *ForStaticInitFunction, OpenMPSchedType Schedule,
3285 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003286 const CGOpenMPRuntime::StaticRTInput &Values) {
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003287 if (!CGF.HaveInsertPoint())
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003288 return;
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003289
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003290 assert(!Values.Ordered);
3291 assert(Schedule == OMP_sch_static || Schedule == OMP_sch_static_chunked ||
3292 Schedule == OMP_sch_static_balanced_chunked ||
3293 Schedule == OMP_ord_static || Schedule == OMP_ord_static_chunked ||
3294 Schedule == OMP_dist_sch_static ||
3295 Schedule == OMP_dist_sch_static_chunked);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003296
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003297 // Call __kmpc_for_static_init(
3298 // ident_t *loc, kmp_int32 tid, kmp_int32 schedtype,
3299 // kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower,
3300 // kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride,
3301 // kmp_int[32|64] incr, kmp_int[32|64] chunk);
3302 llvm::Value *Chunk = Values.Chunk;
3303 if (Chunk == nullptr) {
3304 assert((Schedule == OMP_sch_static || Schedule == OMP_ord_static ||
3305 Schedule == OMP_dist_sch_static) &&
3306 "expected static non-chunked schedule");
3307 // If the Chunk was not specified in the clause - use default value 1.
3308 Chunk = CGF.Builder.getIntN(Values.IVSize, 1);
3309 } else {
3310 assert((Schedule == OMP_sch_static_chunked ||
3311 Schedule == OMP_sch_static_balanced_chunked ||
3312 Schedule == OMP_ord_static_chunked ||
3313 Schedule == OMP_dist_sch_static_chunked) &&
3314 "expected static chunked schedule");
3315 }
3316 llvm::Value *Args[] = {
3317 UpdateLocation,
3318 ThreadId,
3319 CGF.Builder.getInt32(addMonoNonMonoModifier(Schedule, M1,
3320 M2)), // Schedule type
3321 Values.IL.getPointer(), // &isLastIter
3322 Values.LB.getPointer(), // &LB
3323 Values.UB.getPointer(), // &UB
3324 Values.ST.getPointer(), // &Stride
3325 CGF.Builder.getIntN(Values.IVSize, 1), // Incr
3326 Chunk // Chunk
3327 };
3328 CGF.EmitRuntimeCall(ForStaticInitFunction, Args);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003329}
3330
John McCall7f416cc2015-09-08 08:05:57 +00003331void CGOpenMPRuntime::emitForStaticInit(CodeGenFunction &CGF,
3332 SourceLocation Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003333 OpenMPDirectiveKind DKind,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003334 const OpenMPScheduleTy &ScheduleKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003335 const StaticRTInput &Values) {
3336 OpenMPSchedType ScheduleNum = getRuntimeSchedule(
3337 ScheduleKind.Schedule, Values.Chunk != nullptr, Values.Ordered);
3338 assert(isOpenMPWorksharingDirective(DKind) &&
3339 "Expected loop-based or sections-based directive.");
3340 auto *UpdatedLocation = emitUpdateLocation(CGF, Loc,
3341 isOpenMPLoopDirective(DKind)
3342 ? OMP_IDENT_WORK_LOOP
3343 : OMP_IDENT_WORK_SECTIONS);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003344 auto *ThreadId = getThreadID(CGF, Loc);
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003345 auto *StaticInitFunction =
3346 createForStaticInitFunction(Values.IVSize, Values.IVSigned);
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003347 emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003348 ScheduleNum, ScheduleKind.M1, ScheduleKind.M2, Values);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003349}
John McCall7f416cc2015-09-08 08:05:57 +00003350
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003351void CGOpenMPRuntime::emitDistributeStaticInit(
3352 CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003353 OpenMPDistScheduleClauseKind SchedKind,
3354 const CGOpenMPRuntime::StaticRTInput &Values) {
3355 OpenMPSchedType ScheduleNum =
3356 getRuntimeSchedule(SchedKind, Values.Chunk != nullptr);
3357 auto *UpdatedLocation =
3358 emitUpdateLocation(CGF, Loc, OMP_IDENT_WORK_DISTRIBUTE);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003359 auto *ThreadId = getThreadID(CGF, Loc);
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003360 auto *StaticInitFunction =
3361 createForStaticInitFunction(Values.IVSize, Values.IVSigned);
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003362 emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction,
3363 ScheduleNum, OMPC_SCHEDULE_MODIFIER_unknown,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003364 OMPC_SCHEDULE_MODIFIER_unknown, Values);
Alexander Musmanc6388682014-12-15 07:07:06 +00003365}
3366
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003367void CGOpenMPRuntime::emitForStaticFinish(CodeGenFunction &CGF,
Alexey Bataevf43f7142017-09-06 16:17:35 +00003368 SourceLocation Loc,
3369 OpenMPDirectiveKind DKind) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003370 if (!CGF.HaveInsertPoint())
3371 return;
Alexander Musmanc6388682014-12-15 07:07:06 +00003372 // Call __kmpc_for_static_fini(ident_t *loc, kmp_int32 tid);
Alexey Bataevf43f7142017-09-06 16:17:35 +00003373 llvm::Value *Args[] = {
3374 emitUpdateLocation(CGF, Loc,
3375 isOpenMPDistributeDirective(DKind)
3376 ? OMP_IDENT_WORK_DISTRIBUTE
3377 : isOpenMPLoopDirective(DKind)
3378 ? OMP_IDENT_WORK_LOOP
3379 : OMP_IDENT_WORK_SECTIONS),
3380 getThreadID(CGF, Loc)};
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003381 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_for_static_fini),
3382 Args);
Alexander Musmanc6388682014-12-15 07:07:06 +00003383}
3384
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003385void CGOpenMPRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF,
3386 SourceLocation Loc,
3387 unsigned IVSize,
3388 bool IVSigned) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003389 if (!CGF.HaveInsertPoint())
3390 return;
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003391 // Call __kmpc_for_dynamic_fini_(4|8)[u](ident_t *loc, kmp_int32 tid);
Alexey Bataev50b3c952016-02-19 10:38:26 +00003392 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003393 CGF.EmitRuntimeCall(createDispatchFiniFunction(IVSize, IVSigned), Args);
3394}
3395
Alexander Musman92bdaab2015-03-12 13:37:50 +00003396llvm::Value *CGOpenMPRuntime::emitForNext(CodeGenFunction &CGF,
3397 SourceLocation Loc, unsigned IVSize,
John McCall7f416cc2015-09-08 08:05:57 +00003398 bool IVSigned, Address IL,
3399 Address LB, Address UB,
3400 Address ST) {
Alexander Musman92bdaab2015-03-12 13:37:50 +00003401 // Call __kmpc_dispatch_next(
3402 // ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
3403 // kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
3404 // kmp_int[32|64] *p_stride);
3405 llvm::Value *Args[] = {
Alexey Bataev50b3c952016-02-19 10:38:26 +00003406 emitUpdateLocation(CGF, Loc),
3407 getThreadID(CGF, Loc),
John McCall7f416cc2015-09-08 08:05:57 +00003408 IL.getPointer(), // &isLastIter
3409 LB.getPointer(), // &Lower
3410 UB.getPointer(), // &Upper
3411 ST.getPointer() // &Stride
Alexander Musman92bdaab2015-03-12 13:37:50 +00003412 };
3413 llvm::Value *Call =
3414 CGF.EmitRuntimeCall(createDispatchNextFunction(IVSize, IVSigned), Args);
3415 return CGF.EmitScalarConversion(
3416 Call, CGF.getContext().getIntTypeForBitwidth(32, /* Signed */ true),
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003417 CGF.getContext().BoolTy, Loc);
Alexander Musman92bdaab2015-03-12 13:37:50 +00003418}
3419
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003420void CGOpenMPRuntime::emitNumThreadsClause(CodeGenFunction &CGF,
3421 llvm::Value *NumThreads,
3422 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003423 if (!CGF.HaveInsertPoint())
3424 return;
Alexey Bataevb2059782014-10-13 08:23:51 +00003425 // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads)
3426 llvm::Value *Args[] = {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003427 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
Alexey Bataevb2059782014-10-13 08:23:51 +00003428 CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)};
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003429 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_threads),
3430 Args);
Alexey Bataevb2059782014-10-13 08:23:51 +00003431}
3432
Alexey Bataev7f210c62015-06-18 13:40:03 +00003433void CGOpenMPRuntime::emitProcBindClause(CodeGenFunction &CGF,
3434 OpenMPProcBindClauseKind ProcBind,
3435 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003436 if (!CGF.HaveInsertPoint())
3437 return;
Alexey Bataev7f210c62015-06-18 13:40:03 +00003438 // Constants for proc bind value accepted by the runtime.
3439 enum ProcBindTy {
3440 ProcBindFalse = 0,
3441 ProcBindTrue,
3442 ProcBindMaster,
3443 ProcBindClose,
3444 ProcBindSpread,
3445 ProcBindIntel,
3446 ProcBindDefault
3447 } RuntimeProcBind;
3448 switch (ProcBind) {
3449 case OMPC_PROC_BIND_master:
3450 RuntimeProcBind = ProcBindMaster;
3451 break;
3452 case OMPC_PROC_BIND_close:
3453 RuntimeProcBind = ProcBindClose;
3454 break;
3455 case OMPC_PROC_BIND_spread:
3456 RuntimeProcBind = ProcBindSpread;
3457 break;
3458 case OMPC_PROC_BIND_unknown:
3459 llvm_unreachable("Unsupported proc_bind value.");
3460 }
3461 // Build call __kmpc_push_proc_bind(&loc, global_tid, proc_bind)
3462 llvm::Value *Args[] = {
3463 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
3464 llvm::ConstantInt::get(CGM.IntTy, RuntimeProcBind, /*isSigned=*/true)};
3465 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_proc_bind), Args);
3466}
3467
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003468void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>,
3469 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003470 if (!CGF.HaveInsertPoint())
3471 return;
Alexey Bataevd76df6d2015-02-24 12:55:09 +00003472 // Build call void __kmpc_flush(ident_t *loc)
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003473 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_flush),
3474 emitUpdateLocation(CGF, Loc));
Alexey Bataevcc37cc12014-11-20 04:34:54 +00003475}
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003476
Alexey Bataev62b63b12015-03-10 07:28:44 +00003477namespace {
3478/// \brief Indexes of fields for type kmp_task_t.
3479enum KmpTaskTFields {
3480 /// \brief List of shared variables.
3481 KmpTaskTShareds,
3482 /// \brief Task routine.
3483 KmpTaskTRoutine,
3484 /// \brief Partition id for the untied tasks.
3485 KmpTaskTPartId,
Alexey Bataevad537bb2016-05-30 09:06:50 +00003486 /// Function with call of destructors for private variables.
3487 Data1,
3488 /// Task priority.
3489 Data2,
Alexey Bataev7292c292016-04-25 12:22:29 +00003490 /// (Taskloops only) Lower bound.
3491 KmpTaskTLowerBound,
3492 /// (Taskloops only) Upper bound.
3493 KmpTaskTUpperBound,
3494 /// (Taskloops only) Stride.
3495 KmpTaskTStride,
3496 /// (Taskloops only) Is last iteration flag.
3497 KmpTaskTLastIter,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00003498 /// (Taskloops only) Reduction data.
3499 KmpTaskTReductions,
Alexey Bataev62b63b12015-03-10 07:28:44 +00003500};
Hans Wennborg7eb54642015-09-10 17:07:54 +00003501} // anonymous namespace
Alexey Bataev62b63b12015-03-10 07:28:44 +00003502
Samuel Antaoee8fb302016-01-06 13:42:12 +00003503bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::empty() const {
Alexey Bataev03f270c2018-03-30 18:31:07 +00003504 return OffloadEntriesTargetRegion.empty() &&
3505 OffloadEntriesDeviceGlobalVar.empty();
Samuel Antaoee8fb302016-01-06 13:42:12 +00003506}
3507
3508/// \brief Initialize target region entry.
3509void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
3510 initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
3511 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +00003512 unsigned Order) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003513 assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is "
3514 "only required for the device "
3515 "code generation.");
Samuel Antao2de62b02016-02-13 23:35:10 +00003516 OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] =
Samuel Antaof83efdb2017-01-05 16:02:49 +00003517 OffloadEntryInfoTargetRegion(Order, /*Addr=*/nullptr, /*ID=*/nullptr,
Alexey Bataev34f8a702018-03-28 14:28:54 +00003518 OMPTargetRegionEntryTargetRegion);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003519 ++OffloadingEntriesNum;
3520}
3521
3522void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
3523 registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
3524 StringRef ParentName, unsigned LineNum,
Samuel Antaof83efdb2017-01-05 16:02:49 +00003525 llvm::Constant *Addr, llvm::Constant *ID,
Alexey Bataev34f8a702018-03-28 14:28:54 +00003526 OMPTargetRegionEntryKind Flags) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003527 // If we are emitting code for a target, the entry is already initialized,
3528 // only has to be registered.
3529 if (CGM.getLangOpts().OpenMPIsDevice) {
Samuel Antao2de62b02016-02-13 23:35:10 +00003530 assert(hasTargetRegionEntryInfo(DeviceID, FileID, ParentName, LineNum) &&
Samuel Antaoee8fb302016-01-06 13:42:12 +00003531 "Entry must exist.");
Samuel Antao2de62b02016-02-13 23:35:10 +00003532 auto &Entry =
3533 OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum];
Samuel Antaoee8fb302016-01-06 13:42:12 +00003534 assert(Entry.isValid() && "Entry not initialized!");
3535 Entry.setAddress(Addr);
3536 Entry.setID(ID);
Samuel Antaof83efdb2017-01-05 16:02:49 +00003537 Entry.setFlags(Flags);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003538 } else {
Alexey Bataev03f270c2018-03-30 18:31:07 +00003539 OffloadEntryInfoTargetRegion Entry(OffloadingEntriesNum, Addr, ID, Flags);
Samuel Antao2de62b02016-02-13 23:35:10 +00003540 OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = Entry;
Alexey Bataev03f270c2018-03-30 18:31:07 +00003541 ++OffloadingEntriesNum;
Samuel Antaoee8fb302016-01-06 13:42:12 +00003542 }
3543}
3544
3545bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::hasTargetRegionEntryInfo(
Samuel Antao2de62b02016-02-13 23:35:10 +00003546 unsigned DeviceID, unsigned FileID, StringRef ParentName,
3547 unsigned LineNum) const {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003548 auto PerDevice = OffloadEntriesTargetRegion.find(DeviceID);
3549 if (PerDevice == OffloadEntriesTargetRegion.end())
3550 return false;
3551 auto PerFile = PerDevice->second.find(FileID);
3552 if (PerFile == PerDevice->second.end())
3553 return false;
3554 auto PerParentName = PerFile->second.find(ParentName);
3555 if (PerParentName == PerFile->second.end())
3556 return false;
3557 auto PerLine = PerParentName->second.find(LineNum);
3558 if (PerLine == PerParentName->second.end())
3559 return false;
Samuel Antaoee8fb302016-01-06 13:42:12 +00003560 // Fail if this entry is already registered.
Samuel Antao2de62b02016-02-13 23:35:10 +00003561 if (PerLine->second.getAddress() || PerLine->second.getID())
Samuel Antaoee8fb302016-01-06 13:42:12 +00003562 return false;
3563 return true;
3564}
3565
3566void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::actOnTargetRegionEntriesInfo(
3567 const OffloadTargetRegionEntryInfoActTy &Action) {
3568 // Scan all target region entries and perform the provided action.
Alexey Bataev03f270c2018-03-30 18:31:07 +00003569 for (const auto &D : OffloadEntriesTargetRegion)
3570 for (const auto &F : D.second)
3571 for (const auto &P : F.second)
3572 for (const auto &L : P.second)
Samuel Antao2de62b02016-02-13 23:35:10 +00003573 Action(D.first, F.first, P.first(), L.first, L.second);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003574}
3575
Alexey Bataev03f270c2018-03-30 18:31:07 +00003576void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
3577 initializeDeviceGlobalVarEntryInfo(StringRef Name,
3578 OMPTargetGlobalVarEntryKind Flags,
3579 unsigned Order) {
3580 assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is "
3581 "only required for the device "
3582 "code generation.");
3583 OffloadEntriesDeviceGlobalVar.try_emplace(Name, Order, Flags);
3584 ++OffloadingEntriesNum;
3585}
Samuel Antaoee8fb302016-01-06 13:42:12 +00003586
Alexey Bataev03f270c2018-03-30 18:31:07 +00003587void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
3588 registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
3589 CharUnits VarSize,
3590 OMPTargetGlobalVarEntryKind Flags,
3591 llvm::GlobalValue::LinkageTypes Linkage) {
3592 if (CGM.getLangOpts().OpenMPIsDevice) {
3593 auto &Entry = OffloadEntriesDeviceGlobalVar[VarName];
3594 assert(Entry.isValid() && Entry.getFlags() == Flags &&
3595 "Entry not initialized!");
3596 assert((!Entry.getAddress() || Entry.getAddress() == Addr) &&
3597 "Resetting with the new address.");
3598 if (Entry.getAddress() && hasDeviceGlobalVarEntryInfo(VarName))
3599 return;
3600 Entry.setAddress(Addr);
3601 Entry.setVarSize(VarSize);
3602 Entry.setLinkage(Linkage);
3603 } else {
3604 if (hasDeviceGlobalVarEntryInfo(VarName))
3605 return;
3606 OffloadEntriesDeviceGlobalVar.try_emplace(
3607 VarName, OffloadingEntriesNum, Addr, VarSize, Flags, Linkage);
3608 ++OffloadingEntriesNum;
3609 }
3610}
3611
3612void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
3613 actOnDeviceGlobalVarEntriesInfo(
3614 const OffloadDeviceGlobalVarEntryInfoActTy &Action) {
3615 // Scan all target region entries and perform the provided action.
3616 for (const auto &E : OffloadEntriesDeviceGlobalVar)
3617 Action(E.getKey(), E.getValue());
Samuel Antaoee8fb302016-01-06 13:42:12 +00003618}
3619
3620llvm::Function *
3621CGOpenMPRuntime::createOffloadingBinaryDescriptorRegistration() {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003622 // If we don't have entries or if we are emitting code for the device, we
3623 // don't need to do anything.
3624 if (CGM.getLangOpts().OpenMPIsDevice || OffloadEntriesInfoManager.empty())
3625 return nullptr;
3626
3627 auto &M = CGM.getModule();
3628 auto &C = CGM.getContext();
3629
3630 // Get list of devices we care about
3631 auto &Devices = CGM.getLangOpts().OMPTargetTriples;
3632
3633 // We should be creating an offloading descriptor only if there are devices
3634 // specified.
3635 assert(!Devices.empty() && "No OpenMP offloading devices??");
3636
3637 // Create the external variables that will point to the begin and end of the
3638 // host entries section. These will be defined by the linker.
3639 auto *OffloadEntryTy =
3640 CGM.getTypes().ConvertTypeForMem(getTgtOffloadEntryQTy());
3641 llvm::GlobalVariable *HostEntriesBegin = new llvm::GlobalVariable(
3642 M, OffloadEntryTy, /*isConstant=*/true,
Eugene Zelenko1660a5d2016-01-26 19:01:06 +00003643 llvm::GlobalValue::ExternalLinkage, /*Initializer=*/nullptr,
Samuel Antaoee8fb302016-01-06 13:42:12 +00003644 ".omp_offloading.entries_begin");
3645 llvm::GlobalVariable *HostEntriesEnd = new llvm::GlobalVariable(
3646 M, OffloadEntryTy, /*isConstant=*/true,
Eugene Zelenko1660a5d2016-01-26 19:01:06 +00003647 llvm::GlobalValue::ExternalLinkage, /*Initializer=*/nullptr,
Samuel Antaoee8fb302016-01-06 13:42:12 +00003648 ".omp_offloading.entries_end");
3649
3650 // Create all device images
Samuel Antaoee8fb302016-01-06 13:42:12 +00003651 auto *DeviceImageTy = cast<llvm::StructType>(
3652 CGM.getTypes().ConvertTypeForMem(getTgtDeviceImageQTy()));
John McCall23c9dc62016-11-28 22:18:27 +00003653 ConstantInitBuilder DeviceImagesBuilder(CGM);
John McCall6c9f1fdb2016-11-19 08:17:24 +00003654 auto DeviceImagesEntries = DeviceImagesBuilder.beginArray(DeviceImageTy);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003655
Alexey Bataev03f270c2018-03-30 18:31:07 +00003656 for (llvm::Triple Device : Devices) {
3657 StringRef T = Device.getTriple();
Samuel Antaoee8fb302016-01-06 13:42:12 +00003658 auto *ImgBegin = new llvm::GlobalVariable(
3659 M, CGM.Int8Ty, /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage,
Eugene Zelenko1660a5d2016-01-26 19:01:06 +00003660 /*Initializer=*/nullptr,
3661 Twine(".omp_offloading.img_start.") + Twine(T));
Samuel Antaoee8fb302016-01-06 13:42:12 +00003662 auto *ImgEnd = new llvm::GlobalVariable(
3663 M, CGM.Int8Ty, /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage,
Eugene Zelenko1660a5d2016-01-26 19:01:06 +00003664 /*Initializer=*/nullptr, Twine(".omp_offloading.img_end.") + Twine(T));
Samuel Antaoee8fb302016-01-06 13:42:12 +00003665
John McCall6c9f1fdb2016-11-19 08:17:24 +00003666 auto Dev = DeviceImagesEntries.beginStruct(DeviceImageTy);
3667 Dev.add(ImgBegin);
3668 Dev.add(ImgEnd);
3669 Dev.add(HostEntriesBegin);
3670 Dev.add(HostEntriesEnd);
John McCallf1788632016-11-28 22:18:30 +00003671 Dev.finishAndAddTo(DeviceImagesEntries);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003672 }
3673
3674 // Create device images global array.
John McCall6c9f1fdb2016-11-19 08:17:24 +00003675 llvm::GlobalVariable *DeviceImages =
3676 DeviceImagesEntries.finishAndCreateGlobal(".omp_offloading.device_images",
3677 CGM.getPointerAlign(),
3678 /*isConstant=*/true);
Peter Collingbournebcf909d2016-06-14 21:02:05 +00003679 DeviceImages->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003680
3681 // This is a Zero array to be used in the creation of the constant expressions
3682 llvm::Constant *Index[] = {llvm::Constant::getNullValue(CGM.Int32Ty),
3683 llvm::Constant::getNullValue(CGM.Int32Ty)};
3684
3685 // Create the target region descriptor.
3686 auto *BinaryDescriptorTy = cast<llvm::StructType>(
3687 CGM.getTypes().ConvertTypeForMem(getTgtBinaryDescriptorQTy()));
John McCall23c9dc62016-11-28 22:18:27 +00003688 ConstantInitBuilder DescBuilder(CGM);
John McCall6c9f1fdb2016-11-19 08:17:24 +00003689 auto DescInit = DescBuilder.beginStruct(BinaryDescriptorTy);
3690 DescInit.addInt(CGM.Int32Ty, Devices.size());
3691 DescInit.add(llvm::ConstantExpr::getGetElementPtr(DeviceImages->getValueType(),
3692 DeviceImages,
3693 Index));
3694 DescInit.add(HostEntriesBegin);
3695 DescInit.add(HostEntriesEnd);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003696
John McCall6c9f1fdb2016-11-19 08:17:24 +00003697 auto *Desc = DescInit.finishAndCreateGlobal(".omp_offloading.descriptor",
3698 CGM.getPointerAlign(),
3699 /*isConstant=*/true);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003700
3701 // Emit code to register or unregister the descriptor at execution
3702 // startup or closing, respectively.
3703
Alexey Bataev03f270c2018-03-30 18:31:07 +00003704 llvm::Function *UnRegFn;
3705 {
3706 FunctionArgList Args;
3707 ImplicitParamDecl DummyPtr(C, C.VoidPtrTy, ImplicitParamDecl::Other);
3708 Args.push_back(&DummyPtr);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003709
Alexey Bataev03f270c2018-03-30 18:31:07 +00003710 CodeGenFunction CGF(CGM);
3711 // Disable debug info for global (de-)initializer because they are not part
3712 // of some particular construct.
3713 CGF.disableDebugInfo();
3714 auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
3715 auto FTy = CGM.getTypes().GetFunctionType(FI);
3716 UnRegFn = CGM.CreateGlobalInitOrDestructFunction(
3717 FTy, ".omp_offloading.descriptor_unreg", FI);
3718 CGF.StartFunction(GlobalDecl(), C.VoidTy, UnRegFn, FI, Args);
3719 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_unregister_lib),
3720 Desc);
3721 CGF.FinishFunction();
3722 }
3723 llvm::Function *RegFn;
3724 {
3725 CodeGenFunction CGF(CGM);
3726 // Disable debug info for global (de-)initializer because they are not part
3727 // of some particular construct.
3728 CGF.disableDebugInfo();
3729 auto &FI = CGM.getTypes().arrangeNullaryFunction();
3730 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
3731 RegFn = CGM.CreateGlobalInitOrDestructFunction(
3732 FTy, ".omp_offloading.descriptor_reg", FI);
3733 CGF.StartFunction(GlobalDecl(), C.VoidTy, RegFn, FI, FunctionArgList());
3734 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_register_lib), Desc);
3735 // Create a variable to drive the registration and unregistration of the
3736 // descriptor, so we can reuse the logic that emits Ctors and Dtors.
3737 ImplicitParamDecl RegUnregVar(C, C.getTranslationUnitDecl(),
3738 SourceLocation(), nullptr, C.CharTy,
3739 ImplicitParamDecl::Other);
3740 CGM.getCXXABI().registerGlobalDtor(CGF, RegUnregVar, UnRegFn, Desc);
3741 CGF.FinishFunction();
3742 }
George Rokos29d0f002017-05-27 03:03:13 +00003743 if (CGM.supportsCOMDAT()) {
3744 // It is sufficient to call registration function only once, so create a
3745 // COMDAT group for registration/unregistration functions and associated
3746 // data. That would reduce startup time and code size. Registration
3747 // function serves as a COMDAT group key.
3748 auto ComdatKey = M.getOrInsertComdat(RegFn->getName());
3749 RegFn->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage);
3750 RegFn->setVisibility(llvm::GlobalValue::HiddenVisibility);
3751 RegFn->setComdat(ComdatKey);
3752 UnRegFn->setComdat(ComdatKey);
3753 DeviceImages->setComdat(ComdatKey);
3754 Desc->setComdat(ComdatKey);
3755 }
Samuel Antaoee8fb302016-01-06 13:42:12 +00003756 return RegFn;
3757}
3758
Alexey Bataev03f270c2018-03-30 18:31:07 +00003759void CGOpenMPRuntime::createOffloadEntry(
3760 llvm::Constant *ID, llvm::Constant *Addr, uint64_t Size, int32_t Flags,
3761 llvm::GlobalValue::LinkageTypes Linkage) {
Samuel Antao2de62b02016-02-13 23:35:10 +00003762 StringRef Name = Addr->getName();
Samuel Antaoee8fb302016-01-06 13:42:12 +00003763 auto *TgtOffloadEntryType = cast<llvm::StructType>(
3764 CGM.getTypes().ConvertTypeForMem(getTgtOffloadEntryQTy()));
3765 llvm::LLVMContext &C = CGM.getModule().getContext();
3766 llvm::Module &M = CGM.getModule();
3767
3768 // Make sure the address has the right type.
Samuel Antao2de62b02016-02-13 23:35:10 +00003769 llvm::Constant *AddrPtr = llvm::ConstantExpr::getBitCast(ID, CGM.VoidPtrTy);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003770
3771 // Create constant string with the name.
3772 llvm::Constant *StrPtrInit = llvm::ConstantDataArray::getString(C, Name);
3773
3774 llvm::GlobalVariable *Str =
3775 new llvm::GlobalVariable(M, StrPtrInit->getType(), /*isConstant=*/true,
3776 llvm::GlobalValue::InternalLinkage, StrPtrInit,
3777 ".omp_offloading.entry_name");
Peter Collingbournebcf909d2016-06-14 21:02:05 +00003778 Str->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003779 llvm::Constant *StrPtr = llvm::ConstantExpr::getBitCast(Str, CGM.Int8PtrTy);
3780
John McCall6c9f1fdb2016-11-19 08:17:24 +00003781 // We can't have any padding between symbols, so we need to have 1-byte
3782 // alignment.
3783 auto Align = CharUnits::fromQuantity(1);
3784
Samuel Antaoee8fb302016-01-06 13:42:12 +00003785 // Create the entry struct.
John McCall23c9dc62016-11-28 22:18:27 +00003786 ConstantInitBuilder EntryBuilder(CGM);
John McCall6c9f1fdb2016-11-19 08:17:24 +00003787 auto EntryInit = EntryBuilder.beginStruct(TgtOffloadEntryType);
3788 EntryInit.add(AddrPtr);
3789 EntryInit.add(StrPtr);
3790 EntryInit.addInt(CGM.SizeTy, Size);
Samuel Antaof83efdb2017-01-05 16:02:49 +00003791 EntryInit.addInt(CGM.Int32Ty, Flags);
3792 EntryInit.addInt(CGM.Int32Ty, 0);
Jonas Hahnfeld5e4df282018-01-18 15:38:03 +00003793 llvm::GlobalVariable *Entry = EntryInit.finishAndCreateGlobal(
Alexey Bataev03f270c2018-03-30 18:31:07 +00003794 Twine(".omp_offloading.entry.", Name), Align,
3795 /*Constant=*/true, Linkage);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003796
3797 // The entry has to be created in the section the linker expects it to be.
3798 Entry->setSection(".omp_offloading.entries");
Samuel Antaoee8fb302016-01-06 13:42:12 +00003799}
3800
3801void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() {
3802 // Emit the offloading entries and metadata so that the device codegen side
Samuel Antao4c8035b2016-12-12 18:00:20 +00003803 // can easily figure out what to emit. The produced metadata looks like
3804 // this:
Samuel Antaoee8fb302016-01-06 13:42:12 +00003805 //
3806 // !omp_offload.info = !{!1, ...}
3807 //
3808 // Right now we only generate metadata for function that contain target
3809 // regions.
3810
3811 // If we do not have entries, we dont need to do anything.
3812 if (OffloadEntriesInfoManager.empty())
3813 return;
3814
3815 llvm::Module &M = CGM.getModule();
3816 llvm::LLVMContext &C = M.getContext();
Alexey Bataev03f270c2018-03-30 18:31:07 +00003817 SmallVector<const OffloadEntriesInfoManagerTy::OffloadEntryInfo *, 16>
Samuel Antaoee8fb302016-01-06 13:42:12 +00003818 OrderedEntries(OffloadEntriesInfoManager.size());
3819
Simon Pilgrim2c518802017-03-30 14:13:19 +00003820 // Auxiliary methods to create metadata values and strings.
Alexey Bataev03f270c2018-03-30 18:31:07 +00003821 auto &&GetMDInt = [&C](unsigned V) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003822 return llvm::ConstantAsMetadata::get(
Alexey Bataev34f8a702018-03-28 14:28:54 +00003823 llvm::ConstantInt::get(llvm::Type::getInt32Ty(C), V));
Samuel Antaoee8fb302016-01-06 13:42:12 +00003824 };
3825
Alexey Bataev03f270c2018-03-30 18:31:07 +00003826 auto &&GetMDString = [&C](StringRef V) { return llvm::MDString::get(C, V); };
3827
3828 // Create the offloading info metadata node.
3829 llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("omp_offload.info");
Samuel Antaoee8fb302016-01-06 13:42:12 +00003830
3831 // Create function that emits metadata for each target region entry;
Alexey Bataev03f270c2018-03-30 18:31:07 +00003832 auto &&TargetRegionMetadataEmitter =
3833 [&C, MD, &OrderedEntries, &GetMDInt, &GetMDString](
3834 unsigned DeviceID, unsigned FileID, StringRef ParentName,
3835 unsigned Line,
3836 const OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion &E) {
3837 // Generate metadata for target regions. Each entry of this metadata
3838 // contains:
3839 // - Entry 0 -> Kind of this type of metadata (0).
3840 // - Entry 1 -> Device ID of the file where the entry was identified.
3841 // - Entry 2 -> File ID of the file where the entry was identified.
3842 // - Entry 3 -> Mangled name of the function where the entry was
3843 // identified.
3844 // - Entry 4 -> Line in the file where the entry was identified.
3845 // - Entry 5 -> Order the entry was created.
3846 // The first element of the metadata node is the kind.
3847 llvm::Metadata *Ops[] = {GetMDInt(E.getKind()), GetMDInt(DeviceID),
3848 GetMDInt(FileID), GetMDString(ParentName),
3849 GetMDInt(Line), GetMDInt(E.getOrder())};
Samuel Antaoee8fb302016-01-06 13:42:12 +00003850
Alexey Bataev03f270c2018-03-30 18:31:07 +00003851 // Save this entry in the right position of the ordered entries array.
3852 OrderedEntries[E.getOrder()] = &E;
Samuel Antaoee8fb302016-01-06 13:42:12 +00003853
Alexey Bataev03f270c2018-03-30 18:31:07 +00003854 // Add metadata to the named metadata node.
3855 MD->addOperand(llvm::MDNode::get(C, Ops));
3856 };
Samuel Antaoee8fb302016-01-06 13:42:12 +00003857
3858 OffloadEntriesInfoManager.actOnTargetRegionEntriesInfo(
3859 TargetRegionMetadataEmitter);
3860
Alexey Bataev03f270c2018-03-30 18:31:07 +00003861 // Create function that emits metadata for each device global variable entry;
3862 auto &&DeviceGlobalVarMetadataEmitter =
3863 [&C, &OrderedEntries, &GetMDInt, &GetMDString,
3864 MD](StringRef MangledName,
3865 const OffloadEntriesInfoManagerTy::OffloadEntryInfoDeviceGlobalVar
3866 &E) {
3867 // Generate metadata for global variables. Each entry of this metadata
3868 // contains:
3869 // - Entry 0 -> Kind of this type of metadata (1).
3870 // - Entry 1 -> Mangled name of the variable.
3871 // - Entry 2 -> Declare target kind.
3872 // - Entry 3 -> Order the entry was created.
3873 // The first element of the metadata node is the kind.
3874 llvm::Metadata *Ops[] = {
3875 GetMDInt(E.getKind()), GetMDString(MangledName),
3876 GetMDInt(E.getFlags()), GetMDInt(E.getOrder())};
3877
3878 // Save this entry in the right position of the ordered entries array.
3879 OrderedEntries[E.getOrder()] = &E;
3880
3881 // Add metadata to the named metadata node.
3882 MD->addOperand(llvm::MDNode::get(C, Ops));
3883 };
3884
3885 OffloadEntriesInfoManager.actOnDeviceGlobalVarEntriesInfo(
3886 DeviceGlobalVarMetadataEmitter);
3887
3888 for (const auto *E : OrderedEntries) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003889 assert(E && "All ordered entries must exist!");
Alexey Bataev03f270c2018-03-30 18:31:07 +00003890 if (const auto *CE =
Samuel Antaoee8fb302016-01-06 13:42:12 +00003891 dyn_cast<OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion>(
3892 E)) {
3893 assert(CE->getID() && CE->getAddress() &&
3894 "Entry ID and Addr are invalid!");
Alexey Bataev34f8a702018-03-28 14:28:54 +00003895 createOffloadEntry(CE->getID(), CE->getAddress(), /*Size=*/0,
Alexey Bataev03f270c2018-03-30 18:31:07 +00003896 CE->getFlags(), llvm::GlobalValue::WeakAnyLinkage);
3897 } else if (const auto *CE =
3898 dyn_cast<OffloadEntriesInfoManagerTy::
3899 OffloadEntryInfoDeviceGlobalVar>(E)) {
3900 assert(CE->getAddress() && "Entry Addr is invalid!");
3901 createOffloadEntry(CE->getAddress(), CE->getAddress(),
3902 CE->getVarSize().getQuantity(), CE->getFlags(),
3903 CE->getLinkage());
3904 } else {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003905 llvm_unreachable("Unsupported entry kind.");
Alexey Bataev03f270c2018-03-30 18:31:07 +00003906 }
Samuel Antaoee8fb302016-01-06 13:42:12 +00003907 }
3908}
3909
3910/// \brief Loads all the offload entries information from the host IR
3911/// metadata.
3912void CGOpenMPRuntime::loadOffloadInfoMetadata() {
3913 // If we are in target mode, load the metadata from the host IR. This code has
3914 // to match the metadaata creation in createOffloadEntriesAndInfoMetadata().
3915
3916 if (!CGM.getLangOpts().OpenMPIsDevice)
3917 return;
3918
3919 if (CGM.getLangOpts().OMPHostIRFile.empty())
3920 return;
3921
3922 auto Buf = llvm::MemoryBuffer::getFile(CGM.getLangOpts().OMPHostIRFile);
3923 if (Buf.getError())
3924 return;
3925
3926 llvm::LLVMContext C;
Peter Collingbourned9445c42016-11-13 07:00:17 +00003927 auto ME = expectedToErrorOrAndEmitErrors(
3928 C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C));
Samuel Antaoee8fb302016-01-06 13:42:12 +00003929
3930 if (ME.getError())
3931 return;
3932
3933 llvm::NamedMDNode *MD = ME.get()->getNamedMetadata("omp_offload.info");
3934 if (!MD)
3935 return;
3936
George Burgess IV00f70bd2018-03-01 05:43:23 +00003937 for (llvm::MDNode *MN : MD->operands()) {
Alexey Bataev03f270c2018-03-30 18:31:07 +00003938 auto GetMDInt = [MN](unsigned Idx) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003939 llvm::ConstantAsMetadata *V =
3940 cast<llvm::ConstantAsMetadata>(MN->getOperand(Idx));
3941 return cast<llvm::ConstantInt>(V->getValue())->getZExtValue();
3942 };
3943
Alexey Bataev03f270c2018-03-30 18:31:07 +00003944 auto GetMDString = [MN](unsigned Idx) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003945 llvm::MDString *V = cast<llvm::MDString>(MN->getOperand(Idx));
3946 return V->getString();
3947 };
3948
Alexey Bataev03f270c2018-03-30 18:31:07 +00003949 switch (GetMDInt(0)) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003950 default:
3951 llvm_unreachable("Unexpected metadata!");
3952 break;
3953 case OffloadEntriesInfoManagerTy::OffloadEntryInfo::
Alexey Bataev34f8a702018-03-28 14:28:54 +00003954 OffloadingEntryInfoTargetRegion:
Samuel Antaoee8fb302016-01-06 13:42:12 +00003955 OffloadEntriesInfoManager.initializeTargetRegionEntryInfo(
Alexey Bataev03f270c2018-03-30 18:31:07 +00003956 /*DeviceID=*/GetMDInt(1), /*FileID=*/GetMDInt(2),
3957 /*ParentName=*/GetMDString(3), /*Line=*/GetMDInt(4),
3958 /*Order=*/GetMDInt(5));
3959 break;
3960 case OffloadEntriesInfoManagerTy::OffloadEntryInfo::
3961 OffloadingEntryInfoDeviceGlobalVar:
3962 OffloadEntriesInfoManager.initializeDeviceGlobalVarEntryInfo(
3963 /*MangledName=*/GetMDString(1),
3964 static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>(
3965 /*Flags=*/GetMDInt(2)),
3966 /*Order=*/GetMDInt(3));
Samuel Antaoee8fb302016-01-06 13:42:12 +00003967 break;
3968 }
3969 }
3970}
3971
Alexey Bataev62b63b12015-03-10 07:28:44 +00003972void CGOpenMPRuntime::emitKmpRoutineEntryT(QualType KmpInt32Ty) {
3973 if (!KmpRoutineEntryPtrTy) {
3974 // Build typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); type.
3975 auto &C = CGM.getContext();
3976 QualType KmpRoutineEntryTyArgs[] = {KmpInt32Ty, C.VoidPtrTy};
3977 FunctionProtoType::ExtProtoInfo EPI;
3978 KmpRoutineEntryPtrQTy = C.getPointerType(
3979 C.getFunctionType(KmpInt32Ty, KmpRoutineEntryTyArgs, EPI));
3980 KmpRoutineEntryPtrTy = CGM.getTypes().ConvertType(KmpRoutineEntryPtrQTy);
3981 }
3982}
3983
Alexey Bataevc71a4092015-09-11 10:29:41 +00003984static FieldDecl *addFieldToRecordDecl(ASTContext &C, DeclContext *DC,
3985 QualType FieldTy) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00003986 auto *Field = FieldDecl::Create(
3987 C, DC, SourceLocation(), SourceLocation(), /*Id=*/nullptr, FieldTy,
3988 C.getTrivialTypeSourceInfo(FieldTy, SourceLocation()),
3989 /*BW=*/nullptr, /*Mutable=*/false, /*InitStyle=*/ICIS_NoInit);
3990 Field->setAccess(AS_public);
3991 DC->addDecl(Field);
Alexey Bataevc71a4092015-09-11 10:29:41 +00003992 return Field;
Alexey Bataev62b63b12015-03-10 07:28:44 +00003993}
3994
Samuel Antaoee8fb302016-01-06 13:42:12 +00003995QualType CGOpenMPRuntime::getTgtOffloadEntryQTy() {
3996
3997 // Make sure the type of the entry is already created. This is the type we
3998 // have to create:
3999 // struct __tgt_offload_entry{
4000 // void *addr; // Pointer to the offload entry info.
4001 // // (function or global)
4002 // char *name; // Name of the function or global.
4003 // size_t size; // Size of the entry info (0 if it a function).
Samuel Antaof83efdb2017-01-05 16:02:49 +00004004 // int32_t flags; // Flags associated with the entry, e.g. 'link'.
4005 // int32_t reserved; // Reserved, to use by the runtime library.
Samuel Antaoee8fb302016-01-06 13:42:12 +00004006 // };
4007 if (TgtOffloadEntryQTy.isNull()) {
4008 ASTContext &C = CGM.getContext();
4009 auto *RD = C.buildImplicitRecord("__tgt_offload_entry");
4010 RD->startDefinition();
4011 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
4012 addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy));
4013 addFieldToRecordDecl(C, RD, C.getSizeType());
Samuel Antaof83efdb2017-01-05 16:02:49 +00004014 addFieldToRecordDecl(
4015 C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true));
4016 addFieldToRecordDecl(
4017 C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true));
Samuel Antaoee8fb302016-01-06 13:42:12 +00004018 RD->completeDefinition();
Jonas Hahnfeld5e4df282018-01-18 15:38:03 +00004019 RD->addAttr(PackedAttr::CreateImplicit(C));
Samuel Antaoee8fb302016-01-06 13:42:12 +00004020 TgtOffloadEntryQTy = C.getRecordType(RD);
4021 }
4022 return TgtOffloadEntryQTy;
4023}
4024
4025QualType CGOpenMPRuntime::getTgtDeviceImageQTy() {
4026 // These are the types we need to build:
4027 // struct __tgt_device_image{
4028 // void *ImageStart; // Pointer to the target code start.
4029 // void *ImageEnd; // Pointer to the target code end.
4030 // // We also add the host entries to the device image, as it may be useful
4031 // // for the target runtime to have access to that information.
4032 // __tgt_offload_entry *EntriesBegin; // Begin of the table with all
4033 // // the entries.
4034 // __tgt_offload_entry *EntriesEnd; // End of the table with all the
4035 // // entries (non inclusive).
4036 // };
4037 if (TgtDeviceImageQTy.isNull()) {
4038 ASTContext &C = CGM.getContext();
4039 auto *RD = C.buildImplicitRecord("__tgt_device_image");
4040 RD->startDefinition();
4041 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
4042 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
4043 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy()));
4044 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy()));
4045 RD->completeDefinition();
4046 TgtDeviceImageQTy = C.getRecordType(RD);
4047 }
4048 return TgtDeviceImageQTy;
4049}
4050
4051QualType CGOpenMPRuntime::getTgtBinaryDescriptorQTy() {
4052 // struct __tgt_bin_desc{
4053 // int32_t NumDevices; // Number of devices supported.
4054 // __tgt_device_image *DeviceImages; // Arrays of device images
4055 // // (one per device).
4056 // __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
4057 // // entries.
4058 // __tgt_offload_entry *EntriesEnd; // End of the table with all the
4059 // // entries (non inclusive).
4060 // };
4061 if (TgtBinaryDescriptorQTy.isNull()) {
4062 ASTContext &C = CGM.getContext();
4063 auto *RD = C.buildImplicitRecord("__tgt_bin_desc");
4064 RD->startDefinition();
4065 addFieldToRecordDecl(
4066 C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true));
4067 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtDeviceImageQTy()));
4068 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy()));
4069 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy()));
4070 RD->completeDefinition();
4071 TgtBinaryDescriptorQTy = C.getRecordType(RD);
4072 }
4073 return TgtBinaryDescriptorQTy;
4074}
4075
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004076namespace {
Alexey Bataev9e034042015-05-05 04:05:12 +00004077struct PrivateHelpersTy {
4078 PrivateHelpersTy(const VarDecl *Original, const VarDecl *PrivateCopy,
4079 const VarDecl *PrivateElemInit)
4080 : Original(Original), PrivateCopy(PrivateCopy),
4081 PrivateElemInit(PrivateElemInit) {}
4082 const VarDecl *Original;
4083 const VarDecl *PrivateCopy;
4084 const VarDecl *PrivateElemInit;
4085};
4086typedef std::pair<CharUnits /*Align*/, PrivateHelpersTy> PrivateDataTy;
Hans Wennborg7eb54642015-09-10 17:07:54 +00004087} // anonymous namespace
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004088
Alexey Bataev9e034042015-05-05 04:05:12 +00004089static RecordDecl *
Craig Topper8674c5c2015-09-29 04:30:07 +00004090createPrivatesRecordDecl(CodeGenModule &CGM, ArrayRef<PrivateDataTy> Privates) {
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004091 if (!Privates.empty()) {
4092 auto &C = CGM.getContext();
4093 // Build struct .kmp_privates_t. {
4094 // /* private vars */
4095 // };
4096 auto *RD = C.buildImplicitRecord(".kmp_privates.t");
4097 RD->startDefinition();
4098 for (auto &&Pair : Privates) {
Alexey Bataevc71a4092015-09-11 10:29:41 +00004099 auto *VD = Pair.second.Original;
4100 auto Type = VD->getType();
Alexey Bataev1d9c15c2015-05-19 12:31:28 +00004101 Type = Type.getNonReferenceType();
Alexey Bataevc71a4092015-09-11 10:29:41 +00004102 auto *FD = addFieldToRecordDecl(C, RD, Type);
4103 if (VD->hasAttrs()) {
4104 for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()),
4105 E(VD->getAttrs().end());
4106 I != E; ++I)
4107 FD->addAttr(*I);
4108 }
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004109 }
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004110 RD->completeDefinition();
4111 return RD;
4112 }
4113 return nullptr;
4114}
4115
Alexey Bataev9e034042015-05-05 04:05:12 +00004116static RecordDecl *
Alexey Bataev7292c292016-04-25 12:22:29 +00004117createKmpTaskTRecordDecl(CodeGenModule &CGM, OpenMPDirectiveKind Kind,
4118 QualType KmpInt32Ty,
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004119 QualType KmpRoutineEntryPointerQTy) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00004120 auto &C = CGM.getContext();
4121 // Build struct kmp_task_t {
4122 // void * shareds;
4123 // kmp_routine_entry_t routine;
4124 // kmp_int32 part_id;
Alexey Bataevad537bb2016-05-30 09:06:50 +00004125 // kmp_cmplrdata_t data1;
4126 // kmp_cmplrdata_t data2;
Alexey Bataev7292c292016-04-25 12:22:29 +00004127 // For taskloops additional fields:
4128 // kmp_uint64 lb;
4129 // kmp_uint64 ub;
4130 // kmp_int64 st;
4131 // kmp_int32 liter;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00004132 // void * reductions;
Alexey Bataev62b63b12015-03-10 07:28:44 +00004133 // };
Alexey Bataevad537bb2016-05-30 09:06:50 +00004134 auto *UD = C.buildImplicitRecord("kmp_cmplrdata_t", TTK_Union);
4135 UD->startDefinition();
4136 addFieldToRecordDecl(C, UD, KmpInt32Ty);
4137 addFieldToRecordDecl(C, UD, KmpRoutineEntryPointerQTy);
4138 UD->completeDefinition();
4139 QualType KmpCmplrdataTy = C.getRecordType(UD);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004140 auto *RD = C.buildImplicitRecord("kmp_task_t");
4141 RD->startDefinition();
4142 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
4143 addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy);
4144 addFieldToRecordDecl(C, RD, KmpInt32Ty);
Alexey Bataevad537bb2016-05-30 09:06:50 +00004145 addFieldToRecordDecl(C, RD, KmpCmplrdataTy);
4146 addFieldToRecordDecl(C, RD, KmpCmplrdataTy);
Alexey Bataev7292c292016-04-25 12:22:29 +00004147 if (isOpenMPTaskLoopDirective(Kind)) {
4148 QualType KmpUInt64Ty =
4149 CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4150 QualType KmpInt64Ty =
4151 CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4152 addFieldToRecordDecl(C, RD, KmpUInt64Ty);
4153 addFieldToRecordDecl(C, RD, KmpUInt64Ty);
4154 addFieldToRecordDecl(C, RD, KmpInt64Ty);
4155 addFieldToRecordDecl(C, RD, KmpInt32Ty);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00004156 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
Alexey Bataev7292c292016-04-25 12:22:29 +00004157 }
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004158 RD->completeDefinition();
4159 return RD;
4160}
4161
4162static RecordDecl *
4163createKmpTaskTWithPrivatesRecordDecl(CodeGenModule &CGM, QualType KmpTaskTQTy,
Craig Topper8674c5c2015-09-29 04:30:07 +00004164 ArrayRef<PrivateDataTy> Privates) {
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004165 auto &C = CGM.getContext();
4166 // Build struct kmp_task_t_with_privates {
4167 // kmp_task_t task_data;
4168 // .kmp_privates_t. privates;
4169 // };
4170 auto *RD = C.buildImplicitRecord("kmp_task_t_with_privates");
4171 RD->startDefinition();
4172 addFieldToRecordDecl(C, RD, KmpTaskTQTy);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004173 if (auto *PrivateRD = createPrivatesRecordDecl(CGM, Privates)) {
4174 addFieldToRecordDecl(C, RD, C.getRecordType(PrivateRD));
4175 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00004176 RD->completeDefinition();
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004177 return RD;
Alexey Bataev62b63b12015-03-10 07:28:44 +00004178}
4179
4180/// \brief Emit a proxy function which accepts kmp_task_t as the second
4181/// argument.
4182/// \code
4183/// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00004184/// TaskFunction(gtid, tt->part_id, &tt->privates, task_privates_map, tt,
Alexey Bataev7292c292016-04-25 12:22:29 +00004185/// For taskloops:
4186/// tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00004187/// tt->reductions, tt->shareds);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004188/// return 0;
4189/// }
4190/// \endcode
4191static llvm::Value *
4192emitProxyTaskFunction(CodeGenModule &CGM, SourceLocation Loc,
Alexey Bataev7292c292016-04-25 12:22:29 +00004193 OpenMPDirectiveKind Kind, QualType KmpInt32Ty,
4194 QualType KmpTaskTWithPrivatesPtrQTy,
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004195 QualType KmpTaskTWithPrivatesQTy, QualType KmpTaskTQTy,
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004196 QualType SharedsPtrTy, llvm::Value *TaskFunction,
4197 llvm::Value *TaskPrivatesMap) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00004198 auto &C = CGM.getContext();
4199 FunctionArgList Args;
Alexey Bataev56223232017-06-09 13:40:18 +00004200 ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty,
4201 ImplicitParamDecl::Other);
4202 ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
4203 KmpTaskTWithPrivatesPtrQTy.withRestrict(),
4204 ImplicitParamDecl::Other);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004205 Args.push_back(&GtidArg);
4206 Args.push_back(&TaskTypeArg);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004207 auto &TaskEntryFnInfo =
John McCallc56a8b32016-03-11 04:30:31 +00004208 CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004209 auto *TaskEntryTy = CGM.getTypes().GetFunctionType(TaskEntryFnInfo);
4210 auto *TaskEntry =
4211 llvm::Function::Create(TaskEntryTy, llvm::GlobalValue::InternalLinkage,
4212 ".omp_task_entry.", &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00004213 CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskEntry, TaskEntryFnInfo);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004214 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00004215 CGF.StartFunction(GlobalDecl(), KmpInt32Ty, TaskEntry, TaskEntryFnInfo, Args,
4216 Loc, Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004217
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004218 // TaskFunction(gtid, tt->task_data.part_id, &tt->privates, task_privates_map,
Alexey Bataev7292c292016-04-25 12:22:29 +00004219 // tt,
4220 // For taskloops:
4221 // tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter,
4222 // tt->task_data.shareds);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004223 auto *GtidParam = CGF.EmitLoadOfScalar(
John McCall7f416cc2015-09-08 08:05:57 +00004224 CGF.GetAddrOfLocalVar(&GtidArg), /*Volatile=*/false, KmpInt32Ty, Loc);
Alexey Bataev31300ed2016-02-04 11:27:03 +00004225 LValue TDBase = CGF.EmitLoadOfPointerLValue(
4226 CGF.GetAddrOfLocalVar(&TaskTypeArg),
4227 KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004228 auto *KmpTaskTWithPrivatesQTyRD =
4229 cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004230 LValue Base =
4231 CGF.EmitLValueForField(TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin());
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004232 auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl());
4233 auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId);
4234 auto PartIdLVal = CGF.EmitLValueForField(Base, *PartIdFI);
Alexey Bataev48591dd2016-04-20 04:01:36 +00004235 auto *PartidParam = PartIdLVal.getPointer();
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004236
4237 auto SharedsFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTShareds);
4238 auto SharedsLVal = CGF.EmitLValueForField(Base, *SharedsFI);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004239 auto *SharedsParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
Alexey Bataev1e491372018-01-23 18:44:14 +00004240 CGF.EmitLoadOfScalar(SharedsLVal, Loc),
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004241 CGF.ConvertTypeForMem(SharedsPtrTy));
4242
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004243 auto PrivatesFI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1);
4244 llvm::Value *PrivatesParam;
4245 if (PrivatesFI != KmpTaskTWithPrivatesQTyRD->field_end()) {
4246 auto PrivatesLVal = CGF.EmitLValueForField(TDBase, *PrivatesFI);
4247 PrivatesParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
John McCall7f416cc2015-09-08 08:05:57 +00004248 PrivatesLVal.getPointer(), CGF.VoidPtrTy);
Alexey Bataev7292c292016-04-25 12:22:29 +00004249 } else
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004250 PrivatesParam = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004251
Alexey Bataev7292c292016-04-25 12:22:29 +00004252 llvm::Value *CommonArgs[] = {GtidParam, PartidParam, PrivatesParam,
4253 TaskPrivatesMap,
4254 CGF.Builder
4255 .CreatePointerBitCastOrAddrSpaceCast(
4256 TDBase.getAddress(), CGF.VoidPtrTy)
4257 .getPointer()};
4258 SmallVector<llvm::Value *, 16> CallArgs(std::begin(CommonArgs),
4259 std::end(CommonArgs));
4260 if (isOpenMPTaskLoopDirective(Kind)) {
4261 auto LBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound);
4262 auto LBLVal = CGF.EmitLValueForField(Base, *LBFI);
Alexey Bataev1e491372018-01-23 18:44:14 +00004263 auto *LBParam = CGF.EmitLoadOfScalar(LBLVal, Loc);
Alexey Bataev7292c292016-04-25 12:22:29 +00004264 auto UBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound);
4265 auto UBLVal = CGF.EmitLValueForField(Base, *UBFI);
Alexey Bataev1e491372018-01-23 18:44:14 +00004266 auto *UBParam = CGF.EmitLoadOfScalar(UBLVal, Loc);
Alexey Bataev7292c292016-04-25 12:22:29 +00004267 auto StFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTStride);
4268 auto StLVal = CGF.EmitLValueForField(Base, *StFI);
Alexey Bataev1e491372018-01-23 18:44:14 +00004269 auto *StParam = CGF.EmitLoadOfScalar(StLVal, Loc);
Alexey Bataev7292c292016-04-25 12:22:29 +00004270 auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter);
4271 auto LILVal = CGF.EmitLValueForField(Base, *LIFI);
Alexey Bataev1e491372018-01-23 18:44:14 +00004272 auto *LIParam = CGF.EmitLoadOfScalar(LILVal, Loc);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00004273 auto RFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTReductions);
4274 auto RLVal = CGF.EmitLValueForField(Base, *RFI);
Alexey Bataev1e491372018-01-23 18:44:14 +00004275 auto *RParam = CGF.EmitLoadOfScalar(RLVal, Loc);
Alexey Bataev7292c292016-04-25 12:22:29 +00004276 CallArgs.push_back(LBParam);
4277 CallArgs.push_back(UBParam);
4278 CallArgs.push_back(StParam);
4279 CallArgs.push_back(LIParam);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00004280 CallArgs.push_back(RParam);
Alexey Bataev7292c292016-04-25 12:22:29 +00004281 }
4282 CallArgs.push_back(SharedsParam);
4283
Alexey Bataev3c595a62017-08-14 15:01:03 +00004284 CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskFunction,
4285 CallArgs);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004286 CGF.EmitStoreThroughLValue(
4287 RValue::get(CGF.Builder.getInt32(/*C=*/0)),
John McCall7f416cc2015-09-08 08:05:57 +00004288 CGF.MakeAddrLValue(CGF.ReturnValue, KmpInt32Ty));
Alexey Bataev62b63b12015-03-10 07:28:44 +00004289 CGF.FinishFunction();
4290 return TaskEntry;
4291}
4292
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004293static llvm::Value *emitDestructorsFunction(CodeGenModule &CGM,
4294 SourceLocation Loc,
4295 QualType KmpInt32Ty,
4296 QualType KmpTaskTWithPrivatesPtrQTy,
4297 QualType KmpTaskTWithPrivatesQTy) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00004298 auto &C = CGM.getContext();
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004299 FunctionArgList Args;
Alexey Bataev56223232017-06-09 13:40:18 +00004300 ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty,
4301 ImplicitParamDecl::Other);
4302 ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
4303 KmpTaskTWithPrivatesPtrQTy.withRestrict(),
4304 ImplicitParamDecl::Other);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004305 Args.push_back(&GtidArg);
4306 Args.push_back(&TaskTypeArg);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004307 auto &DestructorFnInfo =
John McCallc56a8b32016-03-11 04:30:31 +00004308 CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004309 auto *DestructorFnTy = CGM.getTypes().GetFunctionType(DestructorFnInfo);
4310 auto *DestructorFn =
4311 llvm::Function::Create(DestructorFnTy, llvm::GlobalValue::InternalLinkage,
4312 ".omp_task_destructor.", &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00004313 CGM.SetInternalFunctionAttributes(GlobalDecl(), DestructorFn,
Akira Hatanaka44a59f82015-10-28 02:30:47 +00004314 DestructorFnInfo);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004315 CodeGenFunction CGF(CGM);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004316 CGF.StartFunction(GlobalDecl(), KmpInt32Ty, DestructorFn, DestructorFnInfo,
Alexey Bataev7cae94e2018-01-04 19:45:16 +00004317 Args, Loc, Loc);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004318
Alexey Bataev31300ed2016-02-04 11:27:03 +00004319 LValue Base = CGF.EmitLoadOfPointerLValue(
4320 CGF.GetAddrOfLocalVar(&TaskTypeArg),
4321 KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004322 auto *KmpTaskTWithPrivatesQTyRD =
4323 cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl());
4324 auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin());
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004325 Base = CGF.EmitLValueForField(Base, *FI);
4326 for (auto *Field :
4327 cast<RecordDecl>(FI->getType()->getAsTagDecl())->fields()) {
4328 if (auto DtorKind = Field->getType().isDestructedType()) {
4329 auto FieldLValue = CGF.EmitLValueForField(Base, Field);
4330 CGF.pushDestroy(DtorKind, FieldLValue.getAddress(), Field->getType());
4331 }
4332 }
4333 CGF.FinishFunction();
4334 return DestructorFn;
4335}
4336
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004337/// \brief Emit a privates mapping function for correct handling of private and
4338/// firstprivate variables.
4339/// \code
4340/// void .omp_task_privates_map.(const .privates. *noalias privs, <ty1>
4341/// **noalias priv1,..., <tyn> **noalias privn) {
4342/// *priv1 = &.privates.priv1;
4343/// ...;
4344/// *privn = &.privates.privn;
4345/// }
4346/// \endcode
4347static llvm::Value *
4348emitTaskPrivateMappingFunction(CodeGenModule &CGM, SourceLocation Loc,
Craig Topper8674c5c2015-09-29 04:30:07 +00004349 ArrayRef<const Expr *> PrivateVars,
4350 ArrayRef<const Expr *> FirstprivateVars,
Alexey Bataevf93095a2016-05-05 08:46:22 +00004351 ArrayRef<const Expr *> LastprivateVars,
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004352 QualType PrivatesQTy,
Craig Topper8674c5c2015-09-29 04:30:07 +00004353 ArrayRef<PrivateDataTy> Privates) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004354 auto &C = CGM.getContext();
4355 FunctionArgList Args;
4356 ImplicitParamDecl TaskPrivatesArg(
4357 C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
Alexey Bataev56223232017-06-09 13:40:18 +00004358 C.getPointerType(PrivatesQTy).withConst().withRestrict(),
4359 ImplicitParamDecl::Other);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004360 Args.push_back(&TaskPrivatesArg);
4361 llvm::DenseMap<const VarDecl *, unsigned> PrivateVarsPos;
4362 unsigned Counter = 1;
4363 for (auto *E: PrivateVars) {
4364 Args.push_back(ImplicitParamDecl::Create(
Alexey Bataev56223232017-06-09 13:40:18 +00004365 C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
4366 C.getPointerType(C.getPointerType(E->getType()))
4367 .withConst()
4368 .withRestrict(),
4369 ImplicitParamDecl::Other));
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004370 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
4371 PrivateVarsPos[VD] = Counter;
4372 ++Counter;
4373 }
4374 for (auto *E : FirstprivateVars) {
4375 Args.push_back(ImplicitParamDecl::Create(
Alexey Bataev56223232017-06-09 13:40:18 +00004376 C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
4377 C.getPointerType(C.getPointerType(E->getType()))
4378 .withConst()
4379 .withRestrict(),
4380 ImplicitParamDecl::Other));
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004381 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
4382 PrivateVarsPos[VD] = Counter;
4383 ++Counter;
4384 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004385 for (auto *E: LastprivateVars) {
4386 Args.push_back(ImplicitParamDecl::Create(
Alexey Bataev56223232017-06-09 13:40:18 +00004387 C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
4388 C.getPointerType(C.getPointerType(E->getType()))
4389 .withConst()
4390 .withRestrict(),
4391 ImplicitParamDecl::Other));
Alexey Bataevf93095a2016-05-05 08:46:22 +00004392 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
4393 PrivateVarsPos[VD] = Counter;
4394 ++Counter;
4395 }
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004396 auto &TaskPrivatesMapFnInfo =
John McCallc56a8b32016-03-11 04:30:31 +00004397 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004398 auto *TaskPrivatesMapTy =
4399 CGM.getTypes().GetFunctionType(TaskPrivatesMapFnInfo);
4400 auto *TaskPrivatesMap = llvm::Function::Create(
4401 TaskPrivatesMapTy, llvm::GlobalValue::InternalLinkage,
4402 ".omp_task_privates_map.", &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00004403 CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskPrivatesMap,
Akira Hatanaka44a59f82015-10-28 02:30:47 +00004404 TaskPrivatesMapFnInfo);
Chandler Carruthfcd33142016-12-23 01:24:49 +00004405 TaskPrivatesMap->removeFnAttr(llvm::Attribute::NoInline);
Mehdi Amini6aa9e9b2017-05-29 05:38:20 +00004406 TaskPrivatesMap->removeFnAttr(llvm::Attribute::OptimizeNone);
Evgeniy Stepanov6b2a61d2015-09-14 21:35:16 +00004407 TaskPrivatesMap->addFnAttr(llvm::Attribute::AlwaysInline);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004408 CodeGenFunction CGF(CGM);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004409 CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskPrivatesMap,
Alexey Bataev7cae94e2018-01-04 19:45:16 +00004410 TaskPrivatesMapFnInfo, Args, Loc, Loc);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004411
4412 // *privi = &.privates.privi;
Alexey Bataev31300ed2016-02-04 11:27:03 +00004413 LValue Base = CGF.EmitLoadOfPointerLValue(
4414 CGF.GetAddrOfLocalVar(&TaskPrivatesArg),
4415 TaskPrivatesArg.getType()->castAs<PointerType>());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004416 auto *PrivatesQTyRD = cast<RecordDecl>(PrivatesQTy->getAsTagDecl());
4417 Counter = 0;
4418 for (auto *Field : PrivatesQTyRD->fields()) {
4419 auto FieldLVal = CGF.EmitLValueForField(Base, Field);
4420 auto *VD = Args[PrivateVarsPos[Privates[Counter].second.Original]];
John McCall7f416cc2015-09-08 08:05:57 +00004421 auto RefLVal = CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType());
Alexey Bataev31300ed2016-02-04 11:27:03 +00004422 auto RefLoadLVal = CGF.EmitLoadOfPointerLValue(
4423 RefLVal.getAddress(), RefLVal.getType()->castAs<PointerType>());
Alexey Bataev2377fe92015-09-10 08:12:02 +00004424 CGF.EmitStoreOfScalar(FieldLVal.getPointer(), RefLoadLVal);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004425 ++Counter;
4426 }
4427 CGF.FinishFunction();
4428 return TaskPrivatesMap;
4429}
4430
Mandeep Singh Grangb14fb6a22017-11-28 20:41:13 +00004431static bool stable_sort_comparator(const PrivateDataTy P1,
4432 const PrivateDataTy P2) {
4433 return P1.first > P2.first;
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004434}
4435
Alexey Bataevf93095a2016-05-05 08:46:22 +00004436/// Emit initialization for private variables in task-based directives.
Alexey Bataev8a831592016-05-10 10:36:51 +00004437static void emitPrivatesInit(CodeGenFunction &CGF,
Alexey Bataevf93095a2016-05-05 08:46:22 +00004438 const OMPExecutableDirective &D,
4439 Address KmpTaskSharedsPtr, LValue TDBase,
4440 const RecordDecl *KmpTaskTWithPrivatesQTyRD,
4441 QualType SharedsTy, QualType SharedsPtrTy,
4442 const OMPTaskDataTy &Data,
4443 ArrayRef<PrivateDataTy> Privates, bool ForDup) {
4444 auto &C = CGF.getContext();
Alexey Bataevf93095a2016-05-05 08:46:22 +00004445 auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin());
4446 LValue PrivatesBase = CGF.EmitLValueForField(TDBase, *FI);
Alexey Bataev8451efa2018-01-15 19:06:12 +00004447 OpenMPDirectiveKind Kind = isOpenMPTaskLoopDirective(D.getDirectiveKind())
4448 ? OMPD_taskloop
4449 : OMPD_task;
4450 const CapturedStmt &CS = *D.getCapturedStmt(Kind);
4451 CodeGenFunction::CGCapturedStmtInfo CapturesInfo(CS);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004452 LValue SrcBase;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00004453 bool IsTargetTask =
4454 isOpenMPTargetDataManagementDirective(D.getDirectiveKind()) ||
4455 isOpenMPTargetExecutionDirective(D.getDirectiveKind());
4456 // For target-based directives skip 3 firstprivate arrays BasePointersArray,
4457 // PointersArray and SizesArray. The original variables for these arrays are
4458 // not captured and we get their addresses explicitly.
4459 if ((!IsTargetTask && !Data.FirstprivateVars.empty()) ||
Alexey Bataev8451efa2018-01-15 19:06:12 +00004460 (IsTargetTask && KmpTaskSharedsPtr.isValid())) {
Alexey Bataevf93095a2016-05-05 08:46:22 +00004461 SrcBase = CGF.MakeAddrLValue(
4462 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4463 KmpTaskSharedsPtr, CGF.ConvertTypeForMem(SharedsPtrTy)),
4464 SharedsTy);
4465 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004466 FI = cast<RecordDecl>(FI->getType()->getAsTagDecl())->field_begin();
4467 for (auto &&Pair : Privates) {
4468 auto *VD = Pair.second.PrivateCopy;
4469 auto *Init = VD->getAnyInitializer();
Alexey Bataevf93095a2016-05-05 08:46:22 +00004470 if (Init && (!ForDup || (isa<CXXConstructExpr>(Init) &&
4471 !CGF.isTrivialInitializer(Init)))) {
Alexey Bataev8a831592016-05-10 10:36:51 +00004472 LValue PrivateLValue = CGF.EmitLValueForField(PrivatesBase, *FI);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004473 if (auto *Elem = Pair.second.PrivateElemInit) {
4474 auto *OriginalVD = Pair.second.Original;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00004475 // Check if the variable is the target-based BasePointersArray,
4476 // PointersArray or SizesArray.
4477 LValue SharedRefLValue;
Alexey Bataevf93095a2016-05-05 08:46:22 +00004478 QualType Type = OriginalVD->getType();
Alexey Bataev8451efa2018-01-15 19:06:12 +00004479 auto *SharedField = CapturesInfo.lookup(OriginalVD);
4480 if (IsTargetTask && !SharedField) {
4481 assert(isa<ImplicitParamDecl>(OriginalVD) &&
4482 isa<CapturedDecl>(OriginalVD->getDeclContext()) &&
4483 cast<CapturedDecl>(OriginalVD->getDeclContext())
4484 ->getNumParams() == 0 &&
4485 isa<TranslationUnitDecl>(
4486 cast<CapturedDecl>(OriginalVD->getDeclContext())
4487 ->getDeclContext()) &&
4488 "Expected artificial target data variable.");
Alexey Bataevd2202ca2017-12-27 17:58:32 +00004489 SharedRefLValue =
4490 CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(OriginalVD), Type);
4491 } else {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00004492 SharedRefLValue = CGF.EmitLValueForField(SrcBase, SharedField);
4493 SharedRefLValue = CGF.MakeAddrLValue(
4494 Address(SharedRefLValue.getPointer(), C.getDeclAlign(OriginalVD)),
4495 SharedRefLValue.getType(), LValueBaseInfo(AlignmentSource::Decl),
4496 SharedRefLValue.getTBAAInfo());
4497 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004498 if (Type->isArrayType()) {
4499 // Initialize firstprivate array.
4500 if (!isa<CXXConstructExpr>(Init) || CGF.isTrivialInitializer(Init)) {
4501 // Perform simple memcpy.
Ivan A. Kosarev1860b522018-01-25 14:21:55 +00004502 CGF.EmitAggregateAssign(PrivateLValue, SharedRefLValue, Type);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004503 } else {
4504 // Initialize firstprivate array using element-by-element
Simon Pilgrim2c518802017-03-30 14:13:19 +00004505 // initialization.
Alexey Bataevf93095a2016-05-05 08:46:22 +00004506 CGF.EmitOMPAggregateAssign(
4507 PrivateLValue.getAddress(), SharedRefLValue.getAddress(), Type,
4508 [&CGF, Elem, Init, &CapturesInfo](Address DestElement,
4509 Address SrcElement) {
4510 // Clean up any temporaries needed by the initialization.
4511 CodeGenFunction::OMPPrivateScope InitScope(CGF);
4512 InitScope.addPrivate(
4513 Elem, [SrcElement]() -> Address { return SrcElement; });
4514 (void)InitScope.Privatize();
4515 // Emit initialization for single element.
4516 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(
4517 CGF, &CapturesInfo);
4518 CGF.EmitAnyExprToMem(Init, DestElement,
4519 Init->getType().getQualifiers(),
4520 /*IsInitializer=*/false);
4521 });
4522 }
4523 } else {
4524 CodeGenFunction::OMPPrivateScope InitScope(CGF);
4525 InitScope.addPrivate(Elem, [SharedRefLValue]() -> Address {
4526 return SharedRefLValue.getAddress();
4527 });
4528 (void)InitScope.Privatize();
4529 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CapturesInfo);
4530 CGF.EmitExprAsInit(Init, VD, PrivateLValue,
4531 /*capturedByInit=*/false);
4532 }
4533 } else
4534 CGF.EmitExprAsInit(Init, VD, PrivateLValue, /*capturedByInit=*/false);
4535 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004536 ++FI;
4537 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004538}
4539
4540/// Check if duplication function is required for taskloops.
4541static bool checkInitIsRequired(CodeGenFunction &CGF,
4542 ArrayRef<PrivateDataTy> Privates) {
4543 bool InitRequired = false;
4544 for (auto &&Pair : Privates) {
4545 auto *VD = Pair.second.PrivateCopy;
4546 auto *Init = VD->getAnyInitializer();
4547 InitRequired = InitRequired || (Init && isa<CXXConstructExpr>(Init) &&
4548 !CGF.isTrivialInitializer(Init));
4549 }
4550 return InitRequired;
4551}
4552
4553
4554/// Emit task_dup function (for initialization of
4555/// private/firstprivate/lastprivate vars and last_iter flag)
4556/// \code
4557/// void __task_dup_entry(kmp_task_t *task_dst, const kmp_task_t *task_src, int
4558/// lastpriv) {
4559/// // setup lastprivate flag
4560/// task_dst->last = lastpriv;
4561/// // could be constructor calls here...
4562/// }
4563/// \endcode
4564static llvm::Value *
4565emitTaskDupFunction(CodeGenModule &CGM, SourceLocation Loc,
4566 const OMPExecutableDirective &D,
4567 QualType KmpTaskTWithPrivatesPtrQTy,
4568 const RecordDecl *KmpTaskTWithPrivatesQTyRD,
4569 const RecordDecl *KmpTaskTQTyRD, QualType SharedsTy,
4570 QualType SharedsPtrTy, const OMPTaskDataTy &Data,
4571 ArrayRef<PrivateDataTy> Privates, bool WithLastIter) {
4572 auto &C = CGM.getContext();
4573 FunctionArgList Args;
Alexey Bataev56223232017-06-09 13:40:18 +00004574 ImplicitParamDecl DstArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
4575 KmpTaskTWithPrivatesPtrQTy,
4576 ImplicitParamDecl::Other);
4577 ImplicitParamDecl SrcArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
4578 KmpTaskTWithPrivatesPtrQTy,
4579 ImplicitParamDecl::Other);
4580 ImplicitParamDecl LastprivArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.IntTy,
4581 ImplicitParamDecl::Other);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004582 Args.push_back(&DstArg);
4583 Args.push_back(&SrcArg);
4584 Args.push_back(&LastprivArg);
4585 auto &TaskDupFnInfo =
4586 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
4587 auto *TaskDupTy = CGM.getTypes().GetFunctionType(TaskDupFnInfo);
4588 auto *TaskDup =
4589 llvm::Function::Create(TaskDupTy, llvm::GlobalValue::InternalLinkage,
4590 ".omp_task_dup.", &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00004591 CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskDup, TaskDupFnInfo);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004592 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00004593 CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskDup, TaskDupFnInfo, Args, Loc,
4594 Loc);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004595
4596 LValue TDBase = CGF.EmitLoadOfPointerLValue(
4597 CGF.GetAddrOfLocalVar(&DstArg),
4598 KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
4599 // task_dst->liter = lastpriv;
4600 if (WithLastIter) {
4601 auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter);
4602 LValue Base = CGF.EmitLValueForField(
4603 TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin());
4604 LValue LILVal = CGF.EmitLValueForField(Base, *LIFI);
4605 llvm::Value *Lastpriv = CGF.EmitLoadOfScalar(
4606 CGF.GetAddrOfLocalVar(&LastprivArg), /*Volatile=*/false, C.IntTy, Loc);
4607 CGF.EmitStoreOfScalar(Lastpriv, LILVal);
4608 }
4609
4610 // Emit initial values for private copies (if any).
4611 assert(!Privates.empty());
4612 Address KmpTaskSharedsPtr = Address::invalid();
4613 if (!Data.FirstprivateVars.empty()) {
4614 LValue TDBase = CGF.EmitLoadOfPointerLValue(
4615 CGF.GetAddrOfLocalVar(&SrcArg),
4616 KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
4617 LValue Base = CGF.EmitLValueForField(
4618 TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin());
4619 KmpTaskSharedsPtr = Address(
4620 CGF.EmitLoadOfScalar(CGF.EmitLValueForField(
4621 Base, *std::next(KmpTaskTQTyRD->field_begin(),
4622 KmpTaskTShareds)),
4623 Loc),
4624 CGF.getNaturalTypeAlignment(SharedsTy));
4625 }
Alexey Bataev8a831592016-05-10 10:36:51 +00004626 emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, TDBase, KmpTaskTWithPrivatesQTyRD,
4627 SharedsTy, SharedsPtrTy, Data, Privates, /*ForDup=*/true);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004628 CGF.FinishFunction();
4629 return TaskDup;
4630}
4631
Alexey Bataev8a831592016-05-10 10:36:51 +00004632/// Checks if destructor function is required to be generated.
4633/// \return true if cleanups are required, false otherwise.
4634static bool
4635checkDestructorsRequired(const RecordDecl *KmpTaskTWithPrivatesQTyRD) {
4636 bool NeedsCleanup = false;
4637 auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin());
4638 auto *PrivateRD = cast<RecordDecl>(FI->getType()->getAsTagDecl());
4639 for (auto *FD : PrivateRD->fields()) {
4640 NeedsCleanup = NeedsCleanup || FD->getType().isDestructedType();
4641 if (NeedsCleanup)
4642 break;
4643 }
4644 return NeedsCleanup;
4645}
4646
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004647CGOpenMPRuntime::TaskResultTy
4648CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
4649 const OMPExecutableDirective &D,
4650 llvm::Value *TaskFunction, QualType SharedsTy,
4651 Address Shareds, const OMPTaskDataTy &Data) {
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004652 auto &C = CGM.getContext();
Alexey Bataev7292c292016-04-25 12:22:29 +00004653 llvm::SmallVector<PrivateDataTy, 4> Privates;
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004654 // Aggregate privates and sort them by the alignment.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004655 auto I = Data.PrivateCopies.begin();
4656 for (auto *E : Data.PrivateVars) {
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004657 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
4658 Privates.push_back(std::make_pair(
Alexey Bataevc71a4092015-09-11 10:29:41 +00004659 C.getDeclAlign(VD),
Alexey Bataev9e034042015-05-05 04:05:12 +00004660 PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()),
4661 /*PrivateElemInit=*/nullptr)));
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004662 ++I;
4663 }
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004664 I = Data.FirstprivateCopies.begin();
4665 auto IElemInitRef = Data.FirstprivateInits.begin();
4666 for (auto *E : Data.FirstprivateVars) {
Alexey Bataev9e034042015-05-05 04:05:12 +00004667 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
4668 Privates.push_back(std::make_pair(
Alexey Bataevc71a4092015-09-11 10:29:41 +00004669 C.getDeclAlign(VD),
Alexey Bataev9e034042015-05-05 04:05:12 +00004670 PrivateHelpersTy(
4671 VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()),
4672 cast<VarDecl>(cast<DeclRefExpr>(*IElemInitRef)->getDecl()))));
Richard Trieucc3949d2016-02-18 22:34:54 +00004673 ++I;
4674 ++IElemInitRef;
Alexey Bataev9e034042015-05-05 04:05:12 +00004675 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004676 I = Data.LastprivateCopies.begin();
4677 for (auto *E : Data.LastprivateVars) {
4678 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
4679 Privates.push_back(std::make_pair(
4680 C.getDeclAlign(VD),
4681 PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()),
4682 /*PrivateElemInit=*/nullptr)));
4683 ++I;
4684 }
Mandeep Singh Grangb14fb6a22017-11-28 20:41:13 +00004685 std::stable_sort(Privates.begin(), Privates.end(), stable_sort_comparator);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004686 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
4687 // Build type kmp_routine_entry_t (if not built yet).
4688 emitKmpRoutineEntryT(KmpInt32Ty);
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004689 // Build type kmp_task_t (if not built yet).
Alexey Bataeve213f3e2017-10-11 15:29:40 +00004690 if (isOpenMPTaskLoopDirective(D.getDirectiveKind())) {
4691 if (SavedKmpTaskloopTQTy.isNull()) {
4692 SavedKmpTaskloopTQTy = C.getRecordType(createKmpTaskTRecordDecl(
4693 CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy));
4694 }
4695 KmpTaskTQTy = SavedKmpTaskloopTQTy;
Alexey Bataev3a03a7f2017-10-11 15:56:38 +00004696 } else {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00004697 assert((D.getDirectiveKind() == OMPD_task ||
4698 isOpenMPTargetExecutionDirective(D.getDirectiveKind()) ||
4699 isOpenMPTargetDataManagementDirective(D.getDirectiveKind())) &&
4700 "Expected taskloop, task or target directive");
Alexey Bataeve213f3e2017-10-11 15:29:40 +00004701 if (SavedKmpTaskTQTy.isNull()) {
4702 SavedKmpTaskTQTy = C.getRecordType(createKmpTaskTRecordDecl(
4703 CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy));
4704 }
4705 KmpTaskTQTy = SavedKmpTaskTQTy;
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004706 }
4707 auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl());
Alexey Bataev62b63b12015-03-10 07:28:44 +00004708 // Build particular struct kmp_task_t for the given task.
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004709 auto *KmpTaskTWithPrivatesQTyRD =
4710 createKmpTaskTWithPrivatesRecordDecl(CGM, KmpTaskTQTy, Privates);
4711 auto KmpTaskTWithPrivatesQTy = C.getRecordType(KmpTaskTWithPrivatesQTyRD);
4712 QualType KmpTaskTWithPrivatesPtrQTy =
4713 C.getPointerType(KmpTaskTWithPrivatesQTy);
4714 auto *KmpTaskTWithPrivatesTy = CGF.ConvertType(KmpTaskTWithPrivatesQTy);
4715 auto *KmpTaskTWithPrivatesPtrTy = KmpTaskTWithPrivatesTy->getPointerTo();
Alexey Bataev1189bd02016-01-26 12:20:39 +00004716 auto *KmpTaskTWithPrivatesTySize = CGF.getTypeSize(KmpTaskTWithPrivatesQTy);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004717 QualType SharedsPtrTy = C.getPointerType(SharedsTy);
4718
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004719 // Emit initial values for private copies (if any).
4720 llvm::Value *TaskPrivatesMap = nullptr;
4721 auto *TaskPrivatesMapTy =
Reid Klecknere258c442017-03-16 18:55:46 +00004722 std::next(cast<llvm::Function>(TaskFunction)->arg_begin(), 3)->getType();
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004723 if (!Privates.empty()) {
4724 auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin());
Alexey Bataevf93095a2016-05-05 08:46:22 +00004725 TaskPrivatesMap = emitTaskPrivateMappingFunction(
4726 CGM, Loc, Data.PrivateVars, Data.FirstprivateVars, Data.LastprivateVars,
4727 FI->getType(), Privates);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004728 TaskPrivatesMap = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4729 TaskPrivatesMap, TaskPrivatesMapTy);
4730 } else {
4731 TaskPrivatesMap = llvm::ConstantPointerNull::get(
4732 cast<llvm::PointerType>(TaskPrivatesMapTy));
4733 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00004734 // Build a proxy function kmp_int32 .omp_task_entry.(kmp_int32 gtid,
4735 // kmp_task_t *tt);
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004736 auto *TaskEntry = emitProxyTaskFunction(
Alexey Bataev7292c292016-04-25 12:22:29 +00004737 CGM, Loc, D.getDirectiveKind(), KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy,
4738 KmpTaskTWithPrivatesQTy, KmpTaskTQTy, SharedsPtrTy, TaskFunction,
4739 TaskPrivatesMap);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004740
4741 // Build call kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
4742 // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
4743 // kmp_routine_entry_t *task_entry);
4744 // Task flags. Format is taken from
4745 // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h,
4746 // description of kmp_tasking_flags struct.
Alexey Bataev1e1e2862016-05-10 12:21:02 +00004747 enum {
4748 TiedFlag = 0x1,
4749 FinalFlag = 0x2,
4750 DestructorsFlag = 0x8,
4751 PriorityFlag = 0x20
4752 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004753 unsigned Flags = Data.Tied ? TiedFlag : 0;
Alexey Bataev8a831592016-05-10 10:36:51 +00004754 bool NeedsCleanup = false;
4755 if (!Privates.empty()) {
4756 NeedsCleanup = checkDestructorsRequired(KmpTaskTWithPrivatesQTyRD);
4757 if (NeedsCleanup)
4758 Flags = Flags | DestructorsFlag;
4759 }
Alexey Bataev1e1e2862016-05-10 12:21:02 +00004760 if (Data.Priority.getInt())
4761 Flags = Flags | PriorityFlag;
Alexey Bataev62b63b12015-03-10 07:28:44 +00004762 auto *TaskFlags =
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004763 Data.Final.getPointer()
4764 ? CGF.Builder.CreateSelect(Data.Final.getPointer(),
Alexey Bataev62b63b12015-03-10 07:28:44 +00004765 CGF.Builder.getInt32(FinalFlag),
4766 CGF.Builder.getInt32(/*C=*/0))
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004767 : CGF.Builder.getInt32(Data.Final.getInt() ? FinalFlag : 0);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004768 TaskFlags = CGF.Builder.CreateOr(TaskFlags, CGF.Builder.getInt32(Flags));
Alexey Bataev40e36f12015-11-24 13:01:44 +00004769 auto *SharedsSize = CGM.getSize(C.getTypeSizeInChars(SharedsTy));
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004770 llvm::Value *AllocArgs[] = {emitUpdateLocation(CGF, Loc),
4771 getThreadID(CGF, Loc), TaskFlags,
4772 KmpTaskTWithPrivatesTySize, SharedsSize,
4773 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4774 TaskEntry, KmpRoutineEntryPtrTy)};
Alexey Bataev62b63b12015-03-10 07:28:44 +00004775 auto *NewTask = CGF.EmitRuntimeCall(
4776 createRuntimeFunction(OMPRTL__kmpc_omp_task_alloc), AllocArgs);
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004777 auto *NewTaskNewTaskTTy = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4778 NewTask, KmpTaskTWithPrivatesPtrTy);
4779 LValue Base = CGF.MakeNaturalAlignAddrLValue(NewTaskNewTaskTTy,
4780 KmpTaskTWithPrivatesQTy);
4781 LValue TDBase =
4782 CGF.EmitLValueForField(Base, *KmpTaskTWithPrivatesQTyRD->field_begin());
Alexey Bataev62b63b12015-03-10 07:28:44 +00004783 // Fill the data in the resulting kmp_task_t record.
4784 // Copy shareds if there are any.
John McCall7f416cc2015-09-08 08:05:57 +00004785 Address KmpTaskSharedsPtr = Address::invalid();
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004786 if (!SharedsTy->getAsStructureType()->getDecl()->field_empty()) {
Alexey Bataev2377fe92015-09-10 08:12:02 +00004787 KmpTaskSharedsPtr =
4788 Address(CGF.EmitLoadOfScalar(
4789 CGF.EmitLValueForField(
4790 TDBase, *std::next(KmpTaskTQTyRD->field_begin(),
4791 KmpTaskTShareds)),
4792 Loc),
4793 CGF.getNaturalTypeAlignment(SharedsTy));
Ivan A. Kosarev1860b522018-01-25 14:21:55 +00004794 LValue Dest = CGF.MakeAddrLValue(KmpTaskSharedsPtr, SharedsTy);
4795 LValue Src = CGF.MakeAddrLValue(Shareds, SharedsTy);
4796 CGF.EmitAggregateCopy(Dest, Src, SharedsTy);
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004797 }
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004798 // Emit initial values for private copies (if any).
Alexey Bataevf93095a2016-05-05 08:46:22 +00004799 TaskResultTy Result;
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004800 if (!Privates.empty()) {
Alexey Bataev8a831592016-05-10 10:36:51 +00004801 emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, Base, KmpTaskTWithPrivatesQTyRD,
4802 SharedsTy, SharedsPtrTy, Data, Privates,
4803 /*ForDup=*/false);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004804 if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) &&
4805 (!Data.LastprivateVars.empty() || checkInitIsRequired(CGF, Privates))) {
4806 Result.TaskDupFn = emitTaskDupFunction(
4807 CGM, Loc, D, KmpTaskTWithPrivatesPtrQTy, KmpTaskTWithPrivatesQTyRD,
4808 KmpTaskTQTyRD, SharedsTy, SharedsPtrTy, Data, Privates,
4809 /*WithLastIter=*/!Data.LastprivateVars.empty());
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004810 }
4811 }
Alexey Bataevad537bb2016-05-30 09:06:50 +00004812 // Fields of union "kmp_cmplrdata_t" for destructors and priority.
4813 enum { Priority = 0, Destructors = 1 };
Alexey Bataev62b63b12015-03-10 07:28:44 +00004814 // Provide pointer to function with destructors for privates.
Alexey Bataevad537bb2016-05-30 09:06:50 +00004815 auto FI = std::next(KmpTaskTQTyRD->field_begin(), Data1);
4816 auto *KmpCmplrdataUD = (*FI)->getType()->getAsUnionType()->getDecl();
4817 if (NeedsCleanup) {
4818 llvm::Value *DestructorFn = emitDestructorsFunction(
4819 CGM, Loc, KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy,
4820 KmpTaskTWithPrivatesQTy);
4821 LValue Data1LV = CGF.EmitLValueForField(TDBase, *FI);
4822 LValue DestructorsLV = CGF.EmitLValueForField(
4823 Data1LV, *std::next(KmpCmplrdataUD->field_begin(), Destructors));
4824 CGF.EmitStoreOfScalar(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4825 DestructorFn, KmpRoutineEntryPtrTy),
4826 DestructorsLV);
4827 }
4828 // Set priority.
4829 if (Data.Priority.getInt()) {
4830 LValue Data2LV = CGF.EmitLValueForField(
4831 TDBase, *std::next(KmpTaskTQTyRD->field_begin(), Data2));
4832 LValue PriorityLV = CGF.EmitLValueForField(
4833 Data2LV, *std::next(KmpCmplrdataUD->field_begin(), Priority));
4834 CGF.EmitStoreOfScalar(Data.Priority.getPointer(), PriorityLV);
4835 }
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004836 Result.NewTask = NewTask;
4837 Result.TaskEntry = TaskEntry;
4838 Result.NewTaskNewTaskTTy = NewTaskNewTaskTTy;
4839 Result.TDBase = TDBase;
4840 Result.KmpTaskTQTyRD = KmpTaskTQTyRD;
4841 return Result;
Alexey Bataev7292c292016-04-25 12:22:29 +00004842}
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004843
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004844void CGOpenMPRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
4845 const OMPExecutableDirective &D,
4846 llvm::Value *TaskFunction,
4847 QualType SharedsTy, Address Shareds,
4848 const Expr *IfCond,
4849 const OMPTaskDataTy &Data) {
Alexey Bataev7292c292016-04-25 12:22:29 +00004850 if (!CGF.HaveInsertPoint())
4851 return;
4852
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004853 TaskResultTy Result =
4854 emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data);
4855 llvm::Value *NewTask = Result.NewTask;
4856 llvm::Value *TaskEntry = Result.TaskEntry;
4857 llvm::Value *NewTaskNewTaskTTy = Result.NewTaskNewTaskTTy;
4858 LValue TDBase = Result.TDBase;
4859 RecordDecl *KmpTaskTQTyRD = Result.KmpTaskTQTyRD;
Alexey Bataev7292c292016-04-25 12:22:29 +00004860 auto &C = CGM.getContext();
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004861 // Process list of dependences.
John McCall7f416cc2015-09-08 08:05:57 +00004862 Address DependenciesArray = Address::invalid();
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004863 unsigned NumDependencies = Data.Dependences.size();
John McCall7f416cc2015-09-08 08:05:57 +00004864 if (NumDependencies) {
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004865 // Dependence kind for RTL.
Alexey Bataev92e82f92015-11-23 13:33:42 +00004866 enum RTLDependenceKindTy { DepIn = 0x01, DepInOut = 0x3 };
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004867 enum RTLDependInfoFieldsTy { BaseAddr, Len, Flags };
4868 RecordDecl *KmpDependInfoRD;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004869 QualType FlagsTy =
4870 C.getIntTypeForBitwidth(C.getTypeSize(C.BoolTy), /*Signed=*/false);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004871 llvm::Type *LLVMFlagsTy = CGF.ConvertTypeForMem(FlagsTy);
4872 if (KmpDependInfoTy.isNull()) {
4873 KmpDependInfoRD = C.buildImplicitRecord("kmp_depend_info");
4874 KmpDependInfoRD->startDefinition();
4875 addFieldToRecordDecl(C, KmpDependInfoRD, C.getIntPtrType());
4876 addFieldToRecordDecl(C, KmpDependInfoRD, C.getSizeType());
4877 addFieldToRecordDecl(C, KmpDependInfoRD, FlagsTy);
4878 KmpDependInfoRD->completeDefinition();
4879 KmpDependInfoTy = C.getRecordType(KmpDependInfoRD);
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004880 } else
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004881 KmpDependInfoRD = cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl());
John McCall7f416cc2015-09-08 08:05:57 +00004882 CharUnits DependencySize = C.getTypeSizeInChars(KmpDependInfoTy);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004883 // Define type kmp_depend_info[<Dependences.size()>];
4884 QualType KmpDependInfoArrayTy = C.getConstantArrayType(
John McCall7f416cc2015-09-08 08:05:57 +00004885 KmpDependInfoTy, llvm::APInt(/*numBits=*/64, NumDependencies),
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004886 ArrayType::Normal, /*IndexTypeQuals=*/0);
4887 // kmp_depend_info[<Dependences.size()>] deps;
Alexey Bataev48591dd2016-04-20 04:01:36 +00004888 DependenciesArray =
4889 CGF.CreateMemTemp(KmpDependInfoArrayTy, ".dep.arr.addr");
John McCall7f416cc2015-09-08 08:05:57 +00004890 for (unsigned i = 0; i < NumDependencies; ++i) {
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004891 const Expr *E = Data.Dependences[i].second;
John McCall7f416cc2015-09-08 08:05:57 +00004892 auto Addr = CGF.EmitLValue(E);
Alexey Bataevd6fdc8b2015-08-31 07:32:19 +00004893 llvm::Value *Size;
4894 QualType Ty = E->getType();
Alexey Bataevd6fdc8b2015-08-31 07:32:19 +00004895 if (auto *ASE = dyn_cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts())) {
4896 LValue UpAddrLVal =
4897 CGF.EmitOMPArraySectionExpr(ASE, /*LowerBound=*/false);
4898 llvm::Value *UpAddr =
John McCall7f416cc2015-09-08 08:05:57 +00004899 CGF.Builder.CreateConstGEP1_32(UpAddrLVal.getPointer(), /*Idx0=*/1);
Alexey Bataevd6fdc8b2015-08-31 07:32:19 +00004900 llvm::Value *LowIntPtr =
John McCall7f416cc2015-09-08 08:05:57 +00004901 CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGM.SizeTy);
Alexey Bataevd6fdc8b2015-08-31 07:32:19 +00004902 llvm::Value *UpIntPtr = CGF.Builder.CreatePtrToInt(UpAddr, CGM.SizeTy);
4903 Size = CGF.Builder.CreateNUWSub(UpIntPtr, LowIntPtr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004904 } else
Alexey Bataev1189bd02016-01-26 12:20:39 +00004905 Size = CGF.getTypeSize(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00004906 auto Base = CGF.MakeAddrLValue(
4907 CGF.Builder.CreateConstArrayGEP(DependenciesArray, i, DependencySize),
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004908 KmpDependInfoTy);
4909 // deps[i].base_addr = &<Dependences[i].second>;
4910 auto BaseAddrLVal = CGF.EmitLValueForField(
4911 Base, *std::next(KmpDependInfoRD->field_begin(), BaseAddr));
John McCall7f416cc2015-09-08 08:05:57 +00004912 CGF.EmitStoreOfScalar(
4913 CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGF.IntPtrTy),
4914 BaseAddrLVal);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004915 // deps[i].len = sizeof(<Dependences[i].second>);
4916 auto LenLVal = CGF.EmitLValueForField(
4917 Base, *std::next(KmpDependInfoRD->field_begin(), Len));
4918 CGF.EmitStoreOfScalar(Size, LenLVal);
4919 // deps[i].flags = <Dependences[i].first>;
4920 RTLDependenceKindTy DepKind;
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004921 switch (Data.Dependences[i].first) {
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004922 case OMPC_DEPEND_in:
4923 DepKind = DepIn;
4924 break;
Alexey Bataev92e82f92015-11-23 13:33:42 +00004925 // Out and InOut dependencies must use the same code.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004926 case OMPC_DEPEND_out:
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004927 case OMPC_DEPEND_inout:
4928 DepKind = DepInOut;
4929 break;
Alexey Bataeveb482352015-12-18 05:05:56 +00004930 case OMPC_DEPEND_source:
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004931 case OMPC_DEPEND_sink:
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004932 case OMPC_DEPEND_unknown:
4933 llvm_unreachable("Unknown task dependence type");
4934 }
4935 auto FlagsLVal = CGF.EmitLValueForField(
4936 Base, *std::next(KmpDependInfoRD->field_begin(), Flags));
4937 CGF.EmitStoreOfScalar(llvm::ConstantInt::get(LLVMFlagsTy, DepKind),
4938 FlagsLVal);
4939 }
John McCall7f416cc2015-09-08 08:05:57 +00004940 DependenciesArray = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4941 CGF.Builder.CreateStructGEP(DependenciesArray, 0, CharUnits::Zero()),
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004942 CGF.VoidPtrTy);
4943 }
4944
Alexey Bataev62b63b12015-03-10 07:28:44 +00004945 // NOTE: routine and part_id fields are intialized by __kmpc_omp_task_alloc()
4946 // libcall.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004947 // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid,
4948 // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list,
4949 // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list) if dependence
4950 // list is not empty
Alexey Bataev1d677132015-04-22 13:57:31 +00004951 auto *ThreadID = getThreadID(CGF, Loc);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004952 auto *UpLoc = emitUpdateLocation(CGF, Loc);
John McCall7f416cc2015-09-08 08:05:57 +00004953 llvm::Value *TaskArgs[] = { UpLoc, ThreadID, NewTask };
4954 llvm::Value *DepTaskArgs[7];
4955 if (NumDependencies) {
4956 DepTaskArgs[0] = UpLoc;
4957 DepTaskArgs[1] = ThreadID;
4958 DepTaskArgs[2] = NewTask;
4959 DepTaskArgs[3] = CGF.Builder.getInt32(NumDependencies);
4960 DepTaskArgs[4] = DependenciesArray.getPointer();
4961 DepTaskArgs[5] = CGF.Builder.getInt32(0);
4962 DepTaskArgs[6] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
4963 }
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00004964 auto &&ThenCodeGen = [this, &Data, TDBase, KmpTaskTQTyRD, NumDependencies,
4965 &TaskArgs,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004966 &DepTaskArgs](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004967 if (!Data.Tied) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00004968 auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId);
4969 auto PartIdLVal = CGF.EmitLValueForField(TDBase, *PartIdFI);
4970 CGF.EmitStoreOfScalar(CGF.Builder.getInt32(0), PartIdLVal);
4971 }
John McCall7f416cc2015-09-08 08:05:57 +00004972 if (NumDependencies) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004973 CGF.EmitRuntimeCall(
Alexey Bataev48591dd2016-04-20 04:01:36 +00004974 createRuntimeFunction(OMPRTL__kmpc_omp_task_with_deps), DepTaskArgs);
John McCall7f416cc2015-09-08 08:05:57 +00004975 } else {
Alexey Bataev48591dd2016-04-20 04:01:36 +00004976 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task),
John McCall7f416cc2015-09-08 08:05:57 +00004977 TaskArgs);
4978 }
Alexey Bataev48591dd2016-04-20 04:01:36 +00004979 // Check if parent region is untied and build return for untied task;
4980 if (auto *Region =
4981 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
4982 Region->emitUntiedSwitch(CGF);
Alexey Bataev1d677132015-04-22 13:57:31 +00004983 };
John McCall7f416cc2015-09-08 08:05:57 +00004984
4985 llvm::Value *DepWaitTaskArgs[6];
4986 if (NumDependencies) {
4987 DepWaitTaskArgs[0] = UpLoc;
4988 DepWaitTaskArgs[1] = ThreadID;
4989 DepWaitTaskArgs[2] = CGF.Builder.getInt32(NumDependencies);
4990 DepWaitTaskArgs[3] = DependenciesArray.getPointer();
4991 DepWaitTaskArgs[4] = CGF.Builder.getInt32(0);
4992 DepWaitTaskArgs[5] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
4993 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004994 auto &&ElseCodeGen = [&TaskArgs, ThreadID, NewTaskNewTaskTTy, TaskEntry,
Alexey Bataev3c595a62017-08-14 15:01:03 +00004995 NumDependencies, &DepWaitTaskArgs,
4996 Loc](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004997 auto &RT = CGF.CGM.getOpenMPRuntime();
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004998 CodeGenFunction::RunCleanupsScope LocalScope(CGF);
4999 // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid,
5000 // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32
5001 // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); if dependence info
5002 // is specified.
John McCall7f416cc2015-09-08 08:05:57 +00005003 if (NumDependencies)
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005004 CGF.EmitRuntimeCall(RT.createRuntimeFunction(OMPRTL__kmpc_omp_wait_deps),
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005005 DepWaitTaskArgs);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005006 // Call proxy_task_entry(gtid, new_task);
Alexey Bataev3c595a62017-08-14 15:01:03 +00005007 auto &&CodeGen = [TaskEntry, ThreadID, NewTaskNewTaskTTy,
5008 Loc](CodeGenFunction &CGF, PrePostActionTy &Action) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005009 Action.Enter(CGF);
5010 llvm::Value *OutlinedFnArgs[] = {ThreadID, NewTaskNewTaskTTy};
Alexey Bataev3c595a62017-08-14 15:01:03 +00005011 CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskEntry,
Alexey Bataev2c7eee52017-08-04 19:10:54 +00005012 OutlinedFnArgs);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005013 };
5014
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005015 // Build void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid,
5016 // kmp_task_t *new_task);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005017 // Build void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid,
5018 // kmp_task_t *new_task);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005019 RegionCodeGenTy RCG(CodeGen);
5020 CommonActionTy Action(
5021 RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_begin_if0), TaskArgs,
5022 RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_complete_if0), TaskArgs);
5023 RCG.setAction(Action);
5024 RCG(CGF);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005025 };
John McCall7f416cc2015-09-08 08:05:57 +00005026
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005027 if (IfCond)
Alexey Bataev1d677132015-04-22 13:57:31 +00005028 emitOMPIfClause(CGF, IfCond, ThenCodeGen, ElseCodeGen);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005029 else {
5030 RegionCodeGenTy ThenRCG(ThenCodeGen);
5031 ThenRCG(CGF);
Alexey Bataevf539faa2016-03-28 12:58:34 +00005032 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00005033}
5034
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005035void CGOpenMPRuntime::emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
5036 const OMPLoopDirective &D,
5037 llvm::Value *TaskFunction,
5038 QualType SharedsTy, Address Shareds,
5039 const Expr *IfCond,
5040 const OMPTaskDataTy &Data) {
Alexey Bataev7292c292016-04-25 12:22:29 +00005041 if (!CGF.HaveInsertPoint())
5042 return;
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005043 TaskResultTy Result =
5044 emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00005045 // NOTE: routine and part_id fields are intialized by __kmpc_omp_task_alloc()
5046 // libcall.
5047 // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int
5048 // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int
5049 // sched, kmp_uint64 grainsize, void *task_dup);
5050 llvm::Value *ThreadID = getThreadID(CGF, Loc);
5051 llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc);
5052 llvm::Value *IfVal;
5053 if (IfCond) {
5054 IfVal = CGF.Builder.CreateIntCast(CGF.EvaluateExprAsBool(IfCond), CGF.IntTy,
5055 /*isSigned=*/true);
5056 } else
5057 IfVal = llvm::ConstantInt::getSigned(CGF.IntTy, /*V=*/1);
5058
5059 LValue LBLVal = CGF.EmitLValueForField(
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005060 Result.TDBase,
5061 *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound));
Alexey Bataev7292c292016-04-25 12:22:29 +00005062 auto *LBVar =
5063 cast<VarDecl>(cast<DeclRefExpr>(D.getLowerBoundVariable())->getDecl());
5064 CGF.EmitAnyExprToMem(LBVar->getInit(), LBLVal.getAddress(), LBLVal.getQuals(),
5065 /*IsInitializer=*/true);
5066 LValue UBLVal = CGF.EmitLValueForField(
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005067 Result.TDBase,
5068 *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound));
Alexey Bataev7292c292016-04-25 12:22:29 +00005069 auto *UBVar =
5070 cast<VarDecl>(cast<DeclRefExpr>(D.getUpperBoundVariable())->getDecl());
5071 CGF.EmitAnyExprToMem(UBVar->getInit(), UBLVal.getAddress(), UBLVal.getQuals(),
5072 /*IsInitializer=*/true);
5073 LValue StLVal = CGF.EmitLValueForField(
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005074 Result.TDBase,
5075 *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTStride));
Alexey Bataev7292c292016-04-25 12:22:29 +00005076 auto *StVar =
5077 cast<VarDecl>(cast<DeclRefExpr>(D.getStrideVariable())->getDecl());
5078 CGF.EmitAnyExprToMem(StVar->getInit(), StLVal.getAddress(), StLVal.getQuals(),
5079 /*IsInitializer=*/true);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005080 // Store reductions address.
5081 LValue RedLVal = CGF.EmitLValueForField(
5082 Result.TDBase,
5083 *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTReductions));
5084 if (Data.Reductions)
5085 CGF.EmitStoreOfScalar(Data.Reductions, RedLVal);
5086 else {
5087 CGF.EmitNullInitialization(RedLVal.getAddress(),
5088 CGF.getContext().VoidPtrTy);
5089 }
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00005090 enum { NoSchedule = 0, Grainsize = 1, NumTasks = 2 };
Alexey Bataev7292c292016-04-25 12:22:29 +00005091 llvm::Value *TaskArgs[] = {
Alexey Bataev33446032017-07-12 18:09:32 +00005092 UpLoc,
5093 ThreadID,
5094 Result.NewTask,
5095 IfVal,
5096 LBLVal.getPointer(),
5097 UBLVal.getPointer(),
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00005098 CGF.EmitLoadOfScalar(StLVal, Loc),
Alexey Bataev33446032017-07-12 18:09:32 +00005099 llvm::ConstantInt::getNullValue(
5100 CGF.IntTy), // Always 0 because taskgroup emitted by the compiler
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00005101 llvm::ConstantInt::getSigned(
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005102 CGF.IntTy, Data.Schedule.getPointer()
5103 ? Data.Schedule.getInt() ? NumTasks : Grainsize
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00005104 : NoSchedule),
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005105 Data.Schedule.getPointer()
5106 ? CGF.Builder.CreateIntCast(Data.Schedule.getPointer(), CGF.Int64Ty,
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00005107 /*isSigned=*/false)
5108 : llvm::ConstantInt::get(CGF.Int64Ty, /*V=*/0),
Alexey Bataev33446032017-07-12 18:09:32 +00005109 Result.TaskDupFn ? CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5110 Result.TaskDupFn, CGF.VoidPtrTy)
5111 : llvm::ConstantPointerNull::get(CGF.VoidPtrTy)};
Alexey Bataev7292c292016-04-25 12:22:29 +00005112 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_taskloop), TaskArgs);
5113}
5114
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005115/// \brief Emit reduction operation for each element of array (required for
5116/// array sections) LHS op = RHS.
5117/// \param Type Type of array.
5118/// \param LHSVar Variable on the left side of the reduction operation
5119/// (references element of array in original variable).
5120/// \param RHSVar Variable on the right side of the reduction operation
5121/// (references element of array in original variable).
5122/// \param RedOpGen Generator of reduction operation with use of LHSVar and
5123/// RHSVar.
Benjamin Kramere003ca22015-10-28 13:54:16 +00005124static void EmitOMPAggregateReduction(
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005125 CodeGenFunction &CGF, QualType Type, const VarDecl *LHSVar,
5126 const VarDecl *RHSVar,
5127 const llvm::function_ref<void(CodeGenFunction &CGF, const Expr *,
5128 const Expr *, const Expr *)> &RedOpGen,
5129 const Expr *XExpr = nullptr, const Expr *EExpr = nullptr,
5130 const Expr *UpExpr = nullptr) {
5131 // Perform element-by-element initialization.
5132 QualType ElementTy;
5133 Address LHSAddr = CGF.GetAddrOfLocalVar(LHSVar);
5134 Address RHSAddr = CGF.GetAddrOfLocalVar(RHSVar);
5135
5136 // Drill down to the base element type on both arrays.
5137 auto ArrayTy = Type->getAsArrayTypeUnsafe();
5138 auto NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, LHSAddr);
5139
5140 auto RHSBegin = RHSAddr.getPointer();
5141 auto LHSBegin = LHSAddr.getPointer();
5142 // Cast from pointer to array type to pointer to single element.
5143 auto LHSEnd = CGF.Builder.CreateGEP(LHSBegin, NumElements);
5144 // The basic structure here is a while-do loop.
5145 auto BodyBB = CGF.createBasicBlock("omp.arraycpy.body");
5146 auto DoneBB = CGF.createBasicBlock("omp.arraycpy.done");
5147 auto IsEmpty =
5148 CGF.Builder.CreateICmpEQ(LHSBegin, LHSEnd, "omp.arraycpy.isempty");
5149 CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
5150
5151 // Enter the loop body, making that address the current address.
5152 auto EntryBB = CGF.Builder.GetInsertBlock();
5153 CGF.EmitBlock(BodyBB);
5154
5155 CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy);
5156
5157 llvm::PHINode *RHSElementPHI = CGF.Builder.CreatePHI(
5158 RHSBegin->getType(), 2, "omp.arraycpy.srcElementPast");
5159 RHSElementPHI->addIncoming(RHSBegin, EntryBB);
5160 Address RHSElementCurrent =
5161 Address(RHSElementPHI,
5162 RHSAddr.getAlignment().alignmentOfArrayElement(ElementSize));
5163
5164 llvm::PHINode *LHSElementPHI = CGF.Builder.CreatePHI(
5165 LHSBegin->getType(), 2, "omp.arraycpy.destElementPast");
5166 LHSElementPHI->addIncoming(LHSBegin, EntryBB);
5167 Address LHSElementCurrent =
5168 Address(LHSElementPHI,
5169 LHSAddr.getAlignment().alignmentOfArrayElement(ElementSize));
5170
5171 // Emit copy.
5172 CodeGenFunction::OMPPrivateScope Scope(CGF);
5173 Scope.addPrivate(LHSVar, [=]() -> Address { return LHSElementCurrent; });
5174 Scope.addPrivate(RHSVar, [=]() -> Address { return RHSElementCurrent; });
5175 Scope.Privatize();
5176 RedOpGen(CGF, XExpr, EExpr, UpExpr);
5177 Scope.ForceCleanup();
5178
5179 // Shift the address forward by one element.
5180 auto LHSElementNext = CGF.Builder.CreateConstGEP1_32(
5181 LHSElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
5182 auto RHSElementNext = CGF.Builder.CreateConstGEP1_32(
5183 RHSElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element");
5184 // Check whether we've reached the end.
5185 auto Done =
5186 CGF.Builder.CreateICmpEQ(LHSElementNext, LHSEnd, "omp.arraycpy.done");
5187 CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB);
5188 LHSElementPHI->addIncoming(LHSElementNext, CGF.Builder.GetInsertBlock());
5189 RHSElementPHI->addIncoming(RHSElementNext, CGF.Builder.GetInsertBlock());
5190
5191 // Done.
5192 CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
5193}
5194
Alexey Bataeva839ddd2016-03-17 10:19:46 +00005195/// Emit reduction combiner. If the combiner is a simple expression emit it as
5196/// is, otherwise consider it as combiner of UDR decl and emit it as a call of
5197/// UDR combiner function.
5198static void emitReductionCombiner(CodeGenFunction &CGF,
5199 const Expr *ReductionOp) {
5200 if (auto *CE = dyn_cast<CallExpr>(ReductionOp))
5201 if (auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee()))
5202 if (auto *DRE =
5203 dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts()))
5204 if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) {
5205 std::pair<llvm::Function *, llvm::Function *> Reduction =
5206 CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD);
5207 RValue Func = RValue::get(Reduction.first);
5208 CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func);
5209 CGF.EmitIgnoredExpr(ReductionOp);
5210 return;
5211 }
5212 CGF.EmitIgnoredExpr(ReductionOp);
5213}
5214
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00005215llvm::Value *CGOpenMPRuntime::emitReductionFunction(
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005216 CodeGenModule &CGM, SourceLocation Loc, llvm::Type *ArgsType,
5217 ArrayRef<const Expr *> Privates, ArrayRef<const Expr *> LHSExprs,
5218 ArrayRef<const Expr *> RHSExprs, ArrayRef<const Expr *> ReductionOps) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005219 auto &C = CGM.getContext();
5220
5221 // void reduction_func(void *LHSArg, void *RHSArg);
5222 FunctionArgList Args;
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005223 ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
5224 ImplicitParamDecl::Other);
5225 ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
5226 ImplicitParamDecl::Other);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005227 Args.push_back(&LHSArg);
5228 Args.push_back(&RHSArg);
John McCallc56a8b32016-03-11 04:30:31 +00005229 auto &CGFI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005230 auto *Fn = llvm::Function::Create(
5231 CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
5232 ".omp.reduction.reduction_func", &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00005233 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005234 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005235 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005236
5237 // Dst = (void*[n])(LHSArg);
5238 // Src = (void*[n])(RHSArg);
John McCall7f416cc2015-09-08 08:05:57 +00005239 Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5240 CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)),
5241 ArgsType), CGF.getPointerAlign());
5242 Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5243 CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)),
5244 ArgsType), CGF.getPointerAlign());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005245
5246 // ...
5247 // *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
5248 // ...
5249 CodeGenFunction::OMPPrivateScope Scope(CGF);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005250 auto IPriv = Privates.begin();
5251 unsigned Idx = 0;
5252 for (unsigned I = 0, E = ReductionOps.size(); I < E; ++I, ++IPriv, ++Idx) {
John McCall7f416cc2015-09-08 08:05:57 +00005253 auto RHSVar = cast<VarDecl>(cast<DeclRefExpr>(RHSExprs[I])->getDecl());
5254 Scope.addPrivate(RHSVar, [&]() -> Address {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005255 return emitAddrOfVarFromArray(CGF, RHS, Idx, RHSVar);
John McCall7f416cc2015-09-08 08:05:57 +00005256 });
5257 auto LHSVar = cast<VarDecl>(cast<DeclRefExpr>(LHSExprs[I])->getDecl());
5258 Scope.addPrivate(LHSVar, [&]() -> Address {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005259 return emitAddrOfVarFromArray(CGF, LHS, Idx, LHSVar);
John McCall7f416cc2015-09-08 08:05:57 +00005260 });
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005261 QualType PrivTy = (*IPriv)->getType();
Alexey Bataev1189bd02016-01-26 12:20:39 +00005262 if (PrivTy->isVariablyModifiedType()) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005263 // Get array size and emit VLA type.
5264 ++Idx;
5265 Address Elem =
5266 CGF.Builder.CreateConstArrayGEP(LHS, Idx, CGF.getPointerSize());
5267 llvm::Value *Ptr = CGF.Builder.CreateLoad(Elem);
Alexey Bataev1189bd02016-01-26 12:20:39 +00005268 auto *VLA = CGF.getContext().getAsVariableArrayType(PrivTy);
5269 auto *OVE = cast<OpaqueValueExpr>(VLA->getSizeExpr());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005270 CodeGenFunction::OpaqueValueMapping OpaqueMap(
Alexey Bataev1189bd02016-01-26 12:20:39 +00005271 CGF, OVE, RValue::get(CGF.Builder.CreatePtrToInt(Ptr, CGF.SizeTy)));
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005272 CGF.EmitVariablyModifiedType(PrivTy);
5273 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005274 }
5275 Scope.Privatize();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005276 IPriv = Privates.begin();
5277 auto ILHS = LHSExprs.begin();
5278 auto IRHS = RHSExprs.begin();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005279 for (auto *E : ReductionOps) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005280 if ((*IPriv)->getType()->isArrayType()) {
5281 // Emit reduction for array section.
5282 auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
5283 auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
Alexey Bataeva839ddd2016-03-17 10:19:46 +00005284 EmitOMPAggregateReduction(
5285 CGF, (*IPriv)->getType(), LHSVar, RHSVar,
5286 [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) {
5287 emitReductionCombiner(CGF, E);
5288 });
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005289 } else
5290 // Emit reduction for array subscript or single variable.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00005291 emitReductionCombiner(CGF, E);
Richard Trieucc3949d2016-02-18 22:34:54 +00005292 ++IPriv;
5293 ++ILHS;
5294 ++IRHS;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005295 }
5296 Scope.ForceCleanup();
5297 CGF.FinishFunction();
5298 return Fn;
5299}
5300
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00005301void CGOpenMPRuntime::emitSingleReductionCombiner(CodeGenFunction &CGF,
5302 const Expr *ReductionOp,
5303 const Expr *PrivateRef,
5304 const DeclRefExpr *LHS,
5305 const DeclRefExpr *RHS) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005306 if (PrivateRef->getType()->isArrayType()) {
5307 // Emit reduction for array section.
5308 auto *LHSVar = cast<VarDecl>(LHS->getDecl());
5309 auto *RHSVar = cast<VarDecl>(RHS->getDecl());
5310 EmitOMPAggregateReduction(
5311 CGF, PrivateRef->getType(), LHSVar, RHSVar,
5312 [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) {
5313 emitReductionCombiner(CGF, ReductionOp);
5314 });
5315 } else
5316 // Emit reduction for array subscript or single variable.
5317 emitReductionCombiner(CGF, ReductionOp);
5318}
5319
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005320void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005321 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005322 ArrayRef<const Expr *> LHSExprs,
5323 ArrayRef<const Expr *> RHSExprs,
5324 ArrayRef<const Expr *> ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00005325 ReductionOptionsTy Options) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00005326 if (!CGF.HaveInsertPoint())
5327 return;
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00005328
5329 bool WithNowait = Options.WithNowait;
5330 bool SimpleReduction = Options.SimpleReduction;
5331
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005332 // Next code should be emitted for reduction:
5333 //
5334 // static kmp_critical_name lock = { 0 };
5335 //
5336 // void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
5337 // *(Type0*)lhs[0] = ReductionOperation0(*(Type0*)lhs[0], *(Type0*)rhs[0]);
5338 // ...
5339 // *(Type<n>-1*)lhs[<n>-1] = ReductionOperation<n>-1(*(Type<n>-1*)lhs[<n>-1],
5340 // *(Type<n>-1*)rhs[<n>-1]);
5341 // }
5342 //
5343 // ...
5344 // void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
5345 // switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
5346 // RedList, reduce_func, &<lock>)) {
5347 // case 1:
5348 // ...
5349 // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
5350 // ...
5351 // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
5352 // break;
5353 // case 2:
5354 // ...
5355 // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
5356 // ...
Alexey Bataev69a47792015-05-07 03:54:03 +00005357 // [__kmpc_end_reduce(<loc>, <gtid>, &<lock>);]
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005358 // break;
5359 // default:;
5360 // }
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00005361 //
5362 // if SimpleReduction is true, only the next code is generated:
5363 // ...
5364 // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
5365 // ...
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005366
5367 auto &C = CGM.getContext();
5368
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00005369 if (SimpleReduction) {
5370 CodeGenFunction::RunCleanupsScope Scope(CGF);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005371 auto IPriv = Privates.begin();
5372 auto ILHS = LHSExprs.begin();
5373 auto IRHS = RHSExprs.begin();
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00005374 for (auto *E : ReductionOps) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005375 emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS),
5376 cast<DeclRefExpr>(*IRHS));
Richard Trieucc3949d2016-02-18 22:34:54 +00005377 ++IPriv;
5378 ++ILHS;
5379 ++IRHS;
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00005380 }
5381 return;
5382 }
5383
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005384 // 1. Build a list of reduction variables.
5385 // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]};
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005386 auto Size = RHSExprs.size();
5387 for (auto *E : Privates) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00005388 if (E->getType()->isVariablyModifiedType())
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005389 // Reserve place for array size.
5390 ++Size;
5391 }
5392 llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005393 QualType ReductionArrayTy =
5394 C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal,
5395 /*IndexTypeQuals=*/0);
John McCall7f416cc2015-09-08 08:05:57 +00005396 Address ReductionList =
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005397 CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list");
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005398 auto IPriv = Privates.begin();
5399 unsigned Idx = 0;
5400 for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) {
John McCall7f416cc2015-09-08 08:05:57 +00005401 Address Elem =
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005402 CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, CGF.getPointerSize());
John McCall7f416cc2015-09-08 08:05:57 +00005403 CGF.Builder.CreateStore(
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005404 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
John McCall7f416cc2015-09-08 08:05:57 +00005405 CGF.EmitLValue(RHSExprs[I]).getPointer(), CGF.VoidPtrTy),
5406 Elem);
Alexey Bataev1189bd02016-01-26 12:20:39 +00005407 if ((*IPriv)->getType()->isVariablyModifiedType()) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005408 // Store array size.
5409 ++Idx;
5410 Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx,
5411 CGF.getPointerSize());
Alexey Bataev1189bd02016-01-26 12:20:39 +00005412 llvm::Value *Size = CGF.Builder.CreateIntCast(
5413 CGF.getVLASize(
5414 CGF.getContext().getAsVariableArrayType((*IPriv)->getType()))
Sander de Smalen891af03a2018-02-03 13:55:59 +00005415 .NumElts,
Alexey Bataev1189bd02016-01-26 12:20:39 +00005416 CGF.SizeTy, /*isSigned=*/false);
5417 CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy),
5418 Elem);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005419 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005420 }
5421
5422 // 2. Emit reduce_func().
5423 auto *ReductionFn = emitReductionFunction(
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005424 CGM, Loc, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(),
5425 Privates, LHSExprs, RHSExprs, ReductionOps);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005426
5427 // 3. Create static kmp_critical_name lock = { 0 };
5428 auto *Lock = getCriticalRegionLock(".reduction");
5429
5430 // 4. Build res = __kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
5431 // RedList, reduce_func, &<lock>);
Alexey Bataev50b3c952016-02-19 10:38:26 +00005432 auto *IdentTLoc = emitUpdateLocation(CGF, Loc, OMP_ATOMIC_REDUCE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005433 auto *ThreadId = getThreadID(CGF, Loc);
Alexey Bataev1189bd02016-01-26 12:20:39 +00005434 auto *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy);
Samuel Antao4c8035b2016-12-12 18:00:20 +00005435 auto *RL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5436 ReductionList.getPointer(), CGF.VoidPtrTy);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005437 llvm::Value *Args[] = {
5438 IdentTLoc, // ident_t *<loc>
5439 ThreadId, // i32 <gtid>
5440 CGF.Builder.getInt32(RHSExprs.size()), // i32 <n>
5441 ReductionArrayTySize, // size_type sizeof(RedList)
5442 RL, // void *RedList
5443 ReductionFn, // void (*) (void *, void *) <reduce_func>
5444 Lock // kmp_critical_name *&<lock>
5445 };
5446 auto Res = CGF.EmitRuntimeCall(
5447 createRuntimeFunction(WithNowait ? OMPRTL__kmpc_reduce_nowait
5448 : OMPRTL__kmpc_reduce),
5449 Args);
5450
5451 // 5. Build switch(res)
5452 auto *DefaultBB = CGF.createBasicBlock(".omp.reduction.default");
5453 auto *SwInst = CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/2);
5454
5455 // 6. Build case 1:
5456 // ...
5457 // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
5458 // ...
5459 // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
5460 // break;
5461 auto *Case1BB = CGF.createBasicBlock(".omp.reduction.case1");
5462 SwInst->addCase(CGF.Builder.getInt32(1), Case1BB);
5463 CGF.EmitBlock(Case1BB);
5464
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005465 // Add emission of __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
5466 llvm::Value *EndArgs[] = {
5467 IdentTLoc, // ident_t *<loc>
5468 ThreadId, // i32 <gtid>
5469 Lock // kmp_critical_name *&<lock>
5470 };
5471 auto &&CodeGen = [&Privates, &LHSExprs, &RHSExprs, &ReductionOps](
5472 CodeGenFunction &CGF, PrePostActionTy &Action) {
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00005473 auto &RT = CGF.CGM.getOpenMPRuntime();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005474 auto IPriv = Privates.begin();
5475 auto ILHS = LHSExprs.begin();
5476 auto IRHS = RHSExprs.begin();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005477 for (auto *E : ReductionOps) {
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00005478 RT.emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS),
5479 cast<DeclRefExpr>(*IRHS));
Richard Trieucc3949d2016-02-18 22:34:54 +00005480 ++IPriv;
5481 ++ILHS;
5482 ++IRHS;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005483 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005484 };
5485 RegionCodeGenTy RCG(CodeGen);
5486 CommonActionTy Action(
5487 nullptr, llvm::None,
5488 createRuntimeFunction(WithNowait ? OMPRTL__kmpc_end_reduce_nowait
5489 : OMPRTL__kmpc_end_reduce),
5490 EndArgs);
5491 RCG.setAction(Action);
5492 RCG(CGF);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005493
5494 CGF.EmitBranch(DefaultBB);
5495
5496 // 7. Build case 2:
5497 // ...
5498 // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
5499 // ...
5500 // break;
5501 auto *Case2BB = CGF.createBasicBlock(".omp.reduction.case2");
5502 SwInst->addCase(CGF.Builder.getInt32(2), Case2BB);
5503 CGF.EmitBlock(Case2BB);
5504
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005505 auto &&AtomicCodeGen = [Loc, &Privates, &LHSExprs, &RHSExprs, &ReductionOps](
5506 CodeGenFunction &CGF, PrePostActionTy &Action) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005507 auto ILHS = LHSExprs.begin();
5508 auto IRHS = RHSExprs.begin();
5509 auto IPriv = Privates.begin();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005510 for (auto *E : ReductionOps) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005511 const Expr *XExpr = nullptr;
5512 const Expr *EExpr = nullptr;
5513 const Expr *UpExpr = nullptr;
5514 BinaryOperatorKind BO = BO_Comma;
5515 if (auto *BO = dyn_cast<BinaryOperator>(E)) {
5516 if (BO->getOpcode() == BO_Assign) {
5517 XExpr = BO->getLHS();
5518 UpExpr = BO->getRHS();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005519 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005520 }
5521 // Try to emit update expression as a simple atomic.
5522 auto *RHSExpr = UpExpr;
5523 if (RHSExpr) {
5524 // Analyze RHS part of the whole expression.
5525 if (auto *ACO = dyn_cast<AbstractConditionalOperator>(
5526 RHSExpr->IgnoreParenImpCasts())) {
5527 // If this is a conditional operator, analyze its condition for
5528 // min/max reduction operator.
5529 RHSExpr = ACO->getCond();
Alexey Bataev69a47792015-05-07 03:54:03 +00005530 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005531 if (auto *BORHS =
5532 dyn_cast<BinaryOperator>(RHSExpr->IgnoreParenImpCasts())) {
5533 EExpr = BORHS->getRHS();
5534 BO = BORHS->getOpcode();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005535 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005536 }
5537 if (XExpr) {
5538 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00005539 auto &&AtomicRedGen = [BO, VD,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005540 Loc](CodeGenFunction &CGF, const Expr *XExpr,
5541 const Expr *EExpr, const Expr *UpExpr) {
5542 LValue X = CGF.EmitLValue(XExpr);
5543 RValue E;
5544 if (EExpr)
5545 E = CGF.EmitAnyExpr(EExpr);
5546 CGF.EmitOMPAtomicSimpleUpdateExpr(
JF Bastien92f4ef12016-04-06 17:26:42 +00005547 X, E, BO, /*IsXLHSInRHSPart=*/true,
5548 llvm::AtomicOrdering::Monotonic, Loc,
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00005549 [&CGF, UpExpr, VD, Loc](RValue XRValue) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005550 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
5551 PrivateScope.addPrivate(
5552 VD, [&CGF, VD, XRValue, Loc]() -> Address {
5553 Address LHSTemp = CGF.CreateMemTemp(VD->getType());
5554 CGF.emitOMPSimpleStore(
5555 CGF.MakeAddrLValue(LHSTemp, VD->getType()), XRValue,
5556 VD->getType().getNonReferenceType(), Loc);
5557 return LHSTemp;
5558 });
5559 (void)PrivateScope.Privatize();
5560 return CGF.EmitAnyExpr(UpExpr);
5561 });
5562 };
5563 if ((*IPriv)->getType()->isArrayType()) {
5564 // Emit atomic reduction for array section.
5565 auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
5566 EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), VD, RHSVar,
5567 AtomicRedGen, XExpr, EExpr, UpExpr);
5568 } else
5569 // Emit atomic reduction for array subscript or single variable.
5570 AtomicRedGen(CGF, XExpr, EExpr, UpExpr);
5571 } else {
5572 // Emit as a critical region.
5573 auto &&CritRedGen = [E, Loc](CodeGenFunction &CGF, const Expr *,
5574 const Expr *, const Expr *) {
5575 auto &RT = CGF.CGM.getOpenMPRuntime();
5576 RT.emitCriticalRegion(
5577 CGF, ".atomic_reduction",
5578 [=](CodeGenFunction &CGF, PrePostActionTy &Action) {
5579 Action.Enter(CGF);
5580 emitReductionCombiner(CGF, E);
5581 },
5582 Loc);
5583 };
5584 if ((*IPriv)->getType()->isArrayType()) {
5585 auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
5586 auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
5587 EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), LHSVar, RHSVar,
5588 CritRedGen);
5589 } else
5590 CritRedGen(CGF, nullptr, nullptr, nullptr);
5591 }
Richard Trieucc3949d2016-02-18 22:34:54 +00005592 ++ILHS;
5593 ++IRHS;
5594 ++IPriv;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005595 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005596 };
5597 RegionCodeGenTy AtomicRCG(AtomicCodeGen);
5598 if (!WithNowait) {
5599 // Add emission of __kmpc_end_reduce(<loc>, <gtid>, &<lock>);
5600 llvm::Value *EndArgs[] = {
5601 IdentTLoc, // ident_t *<loc>
5602 ThreadId, // i32 <gtid>
5603 Lock // kmp_critical_name *&<lock>
5604 };
5605 CommonActionTy Action(nullptr, llvm::None,
5606 createRuntimeFunction(OMPRTL__kmpc_end_reduce),
5607 EndArgs);
5608 AtomicRCG.setAction(Action);
5609 AtomicRCG(CGF);
5610 } else
5611 AtomicRCG(CGF);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005612
5613 CGF.EmitBranch(DefaultBB);
5614 CGF.EmitBlock(DefaultBB, /*IsFinished=*/true);
5615}
5616
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005617/// Generates unique name for artificial threadprivate variables.
Alexey Bataev1c44e152018-03-06 18:59:43 +00005618/// Format is: <Prefix> "." <Decl_mangled_name> "_" "<Decl_start_loc_raw_enc>"
5619static std::string generateUniqueName(CodeGenModule &CGM, StringRef Prefix,
5620 const Expr *Ref) {
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005621 SmallString<256> Buffer;
5622 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev1c44e152018-03-06 18:59:43 +00005623 const clang::DeclRefExpr *DE;
5624 const VarDecl *D = ::getBaseDecl(Ref, DE);
5625 if (!D)
5626 D = cast<VarDecl>(cast<DeclRefExpr>(Ref)->getDecl());
5627 D = D->getCanonicalDecl();
5628 Out << Prefix << "."
5629 << (D->isLocalVarDeclOrParm() ? D->getName() : CGM.getMangledName(D))
5630 << "_" << D->getCanonicalDecl()->getLocStart().getRawEncoding();
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005631 return Out.str();
5632}
5633
5634/// Emits reduction initializer function:
5635/// \code
5636/// void @.red_init(void* %arg) {
5637/// %0 = bitcast void* %arg to <type>*
5638/// store <type> <init>, <type>* %0
5639/// ret void
5640/// }
5641/// \endcode
5642static llvm::Value *emitReduceInitFunction(CodeGenModule &CGM,
5643 SourceLocation Loc,
5644 ReductionCodeGen &RCG, unsigned N) {
5645 auto &C = CGM.getContext();
5646 FunctionArgList Args;
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005647 ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
5648 ImplicitParamDecl::Other);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005649 Args.emplace_back(&Param);
5650 auto &FnInfo =
5651 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
5652 auto *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
5653 auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage,
5654 ".red_init.", &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00005655 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005656 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005657 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005658 Address PrivateAddr = CGF.EmitLoadOfPointer(
5659 CGF.GetAddrOfLocalVar(&Param),
5660 C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
5661 llvm::Value *Size = nullptr;
5662 // If the size of the reduction item is non-constant, load it from global
5663 // threadprivate variable.
5664 if (RCG.getSizes(N).second) {
5665 Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate(
5666 CGF, CGM.getContext().getSizeType(),
Alexey Bataev1c44e152018-03-06 18:59:43 +00005667 generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N)));
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00005668 Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false,
5669 CGM.getContext().getSizeType(), Loc);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005670 }
5671 RCG.emitAggregateType(CGF, N, Size);
5672 LValue SharedLVal;
5673 // If initializer uses initializer from declare reduction construct, emit a
5674 // pointer to the address of the original reduction item (reuired by reduction
5675 // initializer)
5676 if (RCG.usesReductionInitializer(N)) {
5677 Address SharedAddr =
5678 CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate(
5679 CGF, CGM.getContext().VoidPtrTy,
Alexey Bataev1c44e152018-03-06 18:59:43 +00005680 generateUniqueName(CGM, "reduction", RCG.getRefExpr(N)));
Alexey Bataev21dab122018-03-09 15:20:30 +00005681 SharedAddr = CGF.EmitLoadOfPointer(
5682 SharedAddr,
5683 CGM.getContext().VoidPtrTy.castAs<PointerType>()->getTypePtr());
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005684 SharedLVal = CGF.MakeAddrLValue(SharedAddr, CGM.getContext().VoidPtrTy);
5685 } else {
5686 SharedLVal = CGF.MakeNaturalAlignAddrLValue(
5687 llvm::ConstantPointerNull::get(CGM.VoidPtrTy),
5688 CGM.getContext().VoidPtrTy);
5689 }
5690 // Emit the initializer:
5691 // %0 = bitcast void* %arg to <type>*
5692 // store <type> <init>, <type>* %0
5693 RCG.emitInitialization(CGF, N, PrivateAddr, SharedLVal,
5694 [](CodeGenFunction &) { return false; });
5695 CGF.FinishFunction();
5696 return Fn;
5697}
5698
5699/// Emits reduction combiner function:
5700/// \code
5701/// void @.red_comb(void* %arg0, void* %arg1) {
5702/// %lhs = bitcast void* %arg0 to <type>*
5703/// %rhs = bitcast void* %arg1 to <type>*
5704/// %2 = <ReductionOp>(<type>* %lhs, <type>* %rhs)
5705/// store <type> %2, <type>* %lhs
5706/// ret void
5707/// }
5708/// \endcode
5709static llvm::Value *emitReduceCombFunction(CodeGenModule &CGM,
5710 SourceLocation Loc,
5711 ReductionCodeGen &RCG, unsigned N,
5712 const Expr *ReductionOp,
5713 const Expr *LHS, const Expr *RHS,
5714 const Expr *PrivateRef) {
5715 auto &C = CGM.getContext();
5716 auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(LHS)->getDecl());
5717 auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(RHS)->getDecl());
5718 FunctionArgList Args;
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005719 ImplicitParamDecl ParamInOut(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
5720 C.VoidPtrTy, ImplicitParamDecl::Other);
5721 ImplicitParamDecl ParamIn(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
5722 ImplicitParamDecl::Other);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005723 Args.emplace_back(&ParamInOut);
5724 Args.emplace_back(&ParamIn);
5725 auto &FnInfo =
5726 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
5727 auto *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
5728 auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage,
5729 ".red_comb.", &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00005730 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005731 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005732 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005733 llvm::Value *Size = nullptr;
5734 // If the size of the reduction item is non-constant, load it from global
5735 // threadprivate variable.
5736 if (RCG.getSizes(N).second) {
5737 Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate(
5738 CGF, CGM.getContext().getSizeType(),
Alexey Bataev1c44e152018-03-06 18:59:43 +00005739 generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N)));
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00005740 Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false,
5741 CGM.getContext().getSizeType(), Loc);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005742 }
5743 RCG.emitAggregateType(CGF, N, Size);
5744 // Remap lhs and rhs variables to the addresses of the function arguments.
5745 // %lhs = bitcast void* %arg0 to <type>*
5746 // %rhs = bitcast void* %arg1 to <type>*
5747 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
5748 PrivateScope.addPrivate(LHSVD, [&C, &CGF, &ParamInOut, LHSVD]() -> Address {
5749 // Pull out the pointer to the variable.
5750 Address PtrAddr = CGF.EmitLoadOfPointer(
5751 CGF.GetAddrOfLocalVar(&ParamInOut),
5752 C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
5753 return CGF.Builder.CreateElementBitCast(
5754 PtrAddr, CGF.ConvertTypeForMem(LHSVD->getType()));
5755 });
5756 PrivateScope.addPrivate(RHSVD, [&C, &CGF, &ParamIn, RHSVD]() -> Address {
5757 // Pull out the pointer to the variable.
5758 Address PtrAddr = CGF.EmitLoadOfPointer(
5759 CGF.GetAddrOfLocalVar(&ParamIn),
5760 C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
5761 return CGF.Builder.CreateElementBitCast(
5762 PtrAddr, CGF.ConvertTypeForMem(RHSVD->getType()));
5763 });
5764 PrivateScope.Privatize();
5765 // Emit the combiner body:
5766 // %2 = <ReductionOp>(<type> *%lhs, <type> *%rhs)
5767 // store <type> %2, <type>* %lhs
5768 CGM.getOpenMPRuntime().emitSingleReductionCombiner(
5769 CGF, ReductionOp, PrivateRef, cast<DeclRefExpr>(LHS),
5770 cast<DeclRefExpr>(RHS));
5771 CGF.FinishFunction();
5772 return Fn;
5773}
5774
5775/// Emits reduction finalizer function:
5776/// \code
5777/// void @.red_fini(void* %arg) {
5778/// %0 = bitcast void* %arg to <type>*
5779/// <destroy>(<type>* %0)
5780/// ret void
5781/// }
5782/// \endcode
5783static llvm::Value *emitReduceFiniFunction(CodeGenModule &CGM,
5784 SourceLocation Loc,
5785 ReductionCodeGen &RCG, unsigned N) {
5786 if (!RCG.needCleanups(N))
5787 return nullptr;
5788 auto &C = CGM.getContext();
5789 FunctionArgList Args;
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005790 ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
5791 ImplicitParamDecl::Other);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005792 Args.emplace_back(&Param);
5793 auto &FnInfo =
5794 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
5795 auto *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
5796 auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage,
5797 ".red_fini.", &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00005798 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005799 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005800 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005801 Address PrivateAddr = CGF.EmitLoadOfPointer(
5802 CGF.GetAddrOfLocalVar(&Param),
5803 C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
5804 llvm::Value *Size = nullptr;
5805 // If the size of the reduction item is non-constant, load it from global
5806 // threadprivate variable.
5807 if (RCG.getSizes(N).second) {
5808 Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate(
5809 CGF, CGM.getContext().getSizeType(),
Alexey Bataev1c44e152018-03-06 18:59:43 +00005810 generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N)));
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00005811 Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false,
5812 CGM.getContext().getSizeType(), Loc);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005813 }
5814 RCG.emitAggregateType(CGF, N, Size);
5815 // Emit the finalizer body:
5816 // <destroy>(<type>* %0)
5817 RCG.emitCleanups(CGF, N, PrivateAddr);
5818 CGF.FinishFunction();
5819 return Fn;
5820}
5821
5822llvm::Value *CGOpenMPRuntime::emitTaskReductionInit(
5823 CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs,
5824 ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) {
5825 if (!CGF.HaveInsertPoint() || Data.ReductionVars.empty())
5826 return nullptr;
5827
5828 // Build typedef struct:
5829 // kmp_task_red_input {
5830 // void *reduce_shar; // shared reduction item
5831 // size_t reduce_size; // size of data item
5832 // void *reduce_init; // data initialization routine
5833 // void *reduce_fini; // data finalization routine
5834 // void *reduce_comb; // data combiner routine
5835 // kmp_task_red_flags_t flags; // flags for additional info from compiler
5836 // } kmp_task_red_input_t;
5837 ASTContext &C = CGM.getContext();
5838 auto *RD = C.buildImplicitRecord("kmp_task_red_input_t");
5839 RD->startDefinition();
5840 const FieldDecl *SharedFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy);
5841 const FieldDecl *SizeFD = addFieldToRecordDecl(C, RD, C.getSizeType());
5842 const FieldDecl *InitFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy);
5843 const FieldDecl *FiniFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy);
5844 const FieldDecl *CombFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy);
5845 const FieldDecl *FlagsFD = addFieldToRecordDecl(
5846 C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/false));
5847 RD->completeDefinition();
5848 QualType RDType = C.getRecordType(RD);
5849 unsigned Size = Data.ReductionVars.size();
5850 llvm::APInt ArraySize(/*numBits=*/64, Size);
5851 QualType ArrayRDType = C.getConstantArrayType(
5852 RDType, ArraySize, ArrayType::Normal, /*IndexTypeQuals=*/0);
5853 // kmp_task_red_input_t .rd_input.[Size];
5854 Address TaskRedInput = CGF.CreateMemTemp(ArrayRDType, ".rd_input.");
5855 ReductionCodeGen RCG(Data.ReductionVars, Data.ReductionCopies,
5856 Data.ReductionOps);
5857 for (unsigned Cnt = 0; Cnt < Size; ++Cnt) {
5858 // kmp_task_red_input_t &ElemLVal = .rd_input.[Cnt];
5859 llvm::Value *Idxs[] = {llvm::ConstantInt::get(CGM.SizeTy, /*V=*/0),
5860 llvm::ConstantInt::get(CGM.SizeTy, Cnt)};
5861 llvm::Value *GEP = CGF.EmitCheckedInBoundsGEP(
5862 TaskRedInput.getPointer(), Idxs,
5863 /*SignedIndices=*/false, /*IsSubtraction=*/false, Loc,
5864 ".rd_input.gep.");
5865 LValue ElemLVal = CGF.MakeNaturalAlignAddrLValue(GEP, RDType);
5866 // ElemLVal.reduce_shar = &Shareds[Cnt];
5867 LValue SharedLVal = CGF.EmitLValueForField(ElemLVal, SharedFD);
5868 RCG.emitSharedLValue(CGF, Cnt);
5869 llvm::Value *CastedShared =
5870 CGF.EmitCastToVoidPtr(RCG.getSharedLValue(Cnt).getPointer());
5871 CGF.EmitStoreOfScalar(CastedShared, SharedLVal);
5872 RCG.emitAggregateType(CGF, Cnt);
5873 llvm::Value *SizeValInChars;
5874 llvm::Value *SizeVal;
5875 std::tie(SizeValInChars, SizeVal) = RCG.getSizes(Cnt);
5876 // We use delayed creation/initialization for VLAs, array sections and
5877 // custom reduction initializations. It is required because runtime does not
5878 // provide the way to pass the sizes of VLAs/array sections to
5879 // initializer/combiner/finalizer functions and does not pass the pointer to
5880 // original reduction item to the initializer. Instead threadprivate global
5881 // variables are used to store these values and use them in the functions.
5882 bool DelayedCreation = !!SizeVal;
5883 SizeValInChars = CGF.Builder.CreateIntCast(SizeValInChars, CGM.SizeTy,
5884 /*isSigned=*/false);
5885 LValue SizeLVal = CGF.EmitLValueForField(ElemLVal, SizeFD);
5886 CGF.EmitStoreOfScalar(SizeValInChars, SizeLVal);
5887 // ElemLVal.reduce_init = init;
5888 LValue InitLVal = CGF.EmitLValueForField(ElemLVal, InitFD);
5889 llvm::Value *InitAddr =
5890 CGF.EmitCastToVoidPtr(emitReduceInitFunction(CGM, Loc, RCG, Cnt));
5891 CGF.EmitStoreOfScalar(InitAddr, InitLVal);
5892 DelayedCreation = DelayedCreation || RCG.usesReductionInitializer(Cnt);
5893 // ElemLVal.reduce_fini = fini;
5894 LValue FiniLVal = CGF.EmitLValueForField(ElemLVal, FiniFD);
5895 llvm::Value *Fini = emitReduceFiniFunction(CGM, Loc, RCG, Cnt);
5896 llvm::Value *FiniAddr = Fini
5897 ? CGF.EmitCastToVoidPtr(Fini)
5898 : llvm::ConstantPointerNull::get(CGM.VoidPtrTy);
5899 CGF.EmitStoreOfScalar(FiniAddr, FiniLVal);
5900 // ElemLVal.reduce_comb = comb;
5901 LValue CombLVal = CGF.EmitLValueForField(ElemLVal, CombFD);
5902 llvm::Value *CombAddr = CGF.EmitCastToVoidPtr(emitReduceCombFunction(
5903 CGM, Loc, RCG, Cnt, Data.ReductionOps[Cnt], LHSExprs[Cnt],
5904 RHSExprs[Cnt], Data.ReductionCopies[Cnt]));
5905 CGF.EmitStoreOfScalar(CombAddr, CombLVal);
5906 // ElemLVal.flags = 0;
5907 LValue FlagsLVal = CGF.EmitLValueForField(ElemLVal, FlagsFD);
5908 if (DelayedCreation) {
5909 CGF.EmitStoreOfScalar(
5910 llvm::ConstantInt::get(CGM.Int32Ty, /*V=*/1, /*IsSigned=*/true),
5911 FlagsLVal);
5912 } else
5913 CGF.EmitNullInitialization(FlagsLVal.getAddress(), FlagsLVal.getType());
5914 }
5915 // Build call void *__kmpc_task_reduction_init(int gtid, int num_data, void
5916 // *data);
5917 llvm::Value *Args[] = {
5918 CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy,
5919 /*isSigned=*/true),
5920 llvm::ConstantInt::get(CGM.IntTy, Size, /*isSigned=*/true),
5921 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(TaskRedInput.getPointer(),
5922 CGM.VoidPtrTy)};
5923 return CGF.EmitRuntimeCall(
5924 createRuntimeFunction(OMPRTL__kmpc_task_reduction_init), Args);
5925}
5926
5927void CGOpenMPRuntime::emitTaskReductionFixups(CodeGenFunction &CGF,
5928 SourceLocation Loc,
5929 ReductionCodeGen &RCG,
5930 unsigned N) {
5931 auto Sizes = RCG.getSizes(N);
5932 // Emit threadprivate global variable if the type is non-constant
5933 // (Sizes.second = nullptr).
5934 if (Sizes.second) {
5935 llvm::Value *SizeVal = CGF.Builder.CreateIntCast(Sizes.second, CGM.SizeTy,
5936 /*isSigned=*/false);
5937 Address SizeAddr = getAddrOfArtificialThreadPrivate(
5938 CGF, CGM.getContext().getSizeType(),
Alexey Bataev1c44e152018-03-06 18:59:43 +00005939 generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N)));
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005940 CGF.Builder.CreateStore(SizeVal, SizeAddr, /*IsVolatile=*/false);
5941 }
5942 // Store address of the original reduction item if custom initializer is used.
5943 if (RCG.usesReductionInitializer(N)) {
5944 Address SharedAddr = getAddrOfArtificialThreadPrivate(
5945 CGF, CGM.getContext().VoidPtrTy,
Alexey Bataev1c44e152018-03-06 18:59:43 +00005946 generateUniqueName(CGM, "reduction", RCG.getRefExpr(N)));
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005947 CGF.Builder.CreateStore(
5948 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5949 RCG.getSharedLValue(N).getPointer(), CGM.VoidPtrTy),
5950 SharedAddr, /*IsVolatile=*/false);
5951 }
5952}
5953
5954Address CGOpenMPRuntime::getTaskReductionItem(CodeGenFunction &CGF,
5955 SourceLocation Loc,
5956 llvm::Value *ReductionsPtr,
5957 LValue SharedLVal) {
5958 // Build call void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void
5959 // *d);
5960 llvm::Value *Args[] = {
5961 CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy,
5962 /*isSigned=*/true),
5963 ReductionsPtr,
5964 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(SharedLVal.getPointer(),
5965 CGM.VoidPtrTy)};
5966 return Address(
5967 CGF.EmitRuntimeCall(
5968 createRuntimeFunction(OMPRTL__kmpc_task_reduction_get_th_data), Args),
5969 SharedLVal.getAlignment());
5970}
5971
Alexey Bataev8b8e2022015-04-27 05:22:09 +00005972void CGOpenMPRuntime::emitTaskwaitCall(CodeGenFunction &CGF,
5973 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00005974 if (!CGF.HaveInsertPoint())
5975 return;
Alexey Bataev8b8e2022015-04-27 05:22:09 +00005976 // Build call kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32
5977 // global_tid);
5978 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
5979 // Ignore return result until untied tasks are supported.
5980 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskwait), Args);
Alexey Bataev48591dd2016-04-20 04:01:36 +00005981 if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
5982 Region->emitUntiedSwitch(CGF);
Alexey Bataev8b8e2022015-04-27 05:22:09 +00005983}
5984
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00005985void CGOpenMPRuntime::emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00005986 OpenMPDirectiveKind InnerKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005987 const RegionCodeGenTy &CodeGen,
5988 bool HasCancel) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00005989 if (!CGF.HaveInsertPoint())
5990 return;
Alexey Bataev25e5b442015-09-15 12:52:43 +00005991 InlinedOpenMPRegionRAII Region(CGF, CodeGen, InnerKind, HasCancel);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00005992 CGF.CapturedStmtInfo->EmitBody(CGF, /*S=*/nullptr);
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00005993}
5994
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00005995namespace {
5996enum RTCancelKind {
5997 CancelNoreq = 0,
5998 CancelParallel = 1,
5999 CancelLoop = 2,
6000 CancelSections = 3,
6001 CancelTaskgroup = 4
6002};
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00006003} // anonymous namespace
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006004
6005static RTCancelKind getCancellationKind(OpenMPDirectiveKind CancelRegion) {
6006 RTCancelKind CancelKind = CancelNoreq;
Alexey Bataev0f34da12015-07-02 04:17:07 +00006007 if (CancelRegion == OMPD_parallel)
6008 CancelKind = CancelParallel;
6009 else if (CancelRegion == OMPD_for)
6010 CancelKind = CancelLoop;
6011 else if (CancelRegion == OMPD_sections)
6012 CancelKind = CancelSections;
6013 else {
6014 assert(CancelRegion == OMPD_taskgroup);
6015 CancelKind = CancelTaskgroup;
6016 }
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006017 return CancelKind;
6018}
6019
6020void CGOpenMPRuntime::emitCancellationPointCall(
6021 CodeGenFunction &CGF, SourceLocation Loc,
6022 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00006023 if (!CGF.HaveInsertPoint())
6024 return;
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006025 // Build call kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32
6026 // global_tid, kmp_int32 cncl_kind);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00006027 if (auto *OMPRegionInfo =
6028 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
Jonas Hahnfeldb07931f2017-02-17 18:32:58 +00006029 // For 'cancellation point taskgroup', the task region info may not have a
6030 // cancel. This may instead happen in another adjacent task.
6031 if (CancelRegion == OMPD_taskgroup || OMPRegionInfo->hasCancel()) {
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006032 llvm::Value *Args[] = {
6033 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
6034 CGF.Builder.getInt32(getCancellationKind(CancelRegion))};
Alexey Bataev81c7ea02015-07-03 09:56:58 +00006035 // Ignore return result until untied tasks are supported.
6036 auto *Result = CGF.EmitRuntimeCall(
6037 createRuntimeFunction(OMPRTL__kmpc_cancellationpoint), Args);
6038 // if (__kmpc_cancellationpoint()) {
Alexey Bataev81c7ea02015-07-03 09:56:58 +00006039 // exit from construct;
6040 // }
6041 auto *ExitBB = CGF.createBasicBlock(".cancel.exit");
6042 auto *ContBB = CGF.createBasicBlock(".cancel.continue");
6043 auto *Cmp = CGF.Builder.CreateIsNotNull(Result);
6044 CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB);
6045 CGF.EmitBlock(ExitBB);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00006046 // exit from construct;
Alexey Bataev25e5b442015-09-15 12:52:43 +00006047 auto CancelDest =
6048 CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind());
Alexey Bataev81c7ea02015-07-03 09:56:58 +00006049 CGF.EmitBranchThroughCleanup(CancelDest);
6050 CGF.EmitBlock(ContBB, /*IsFinished=*/true);
6051 }
Alexey Bataev0f34da12015-07-02 04:17:07 +00006052 }
Alexey Bataev0f34da12015-07-02 04:17:07 +00006053}
6054
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006055void CGOpenMPRuntime::emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +00006056 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006057 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00006058 if (!CGF.HaveInsertPoint())
6059 return;
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006060 // Build call kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid,
6061 // kmp_int32 cncl_kind);
6062 if (auto *OMPRegionInfo =
6063 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006064 auto &&ThenGen = [Loc, CancelRegion, OMPRegionInfo](CodeGenFunction &CGF,
6065 PrePostActionTy &) {
6066 auto &RT = CGF.CGM.getOpenMPRuntime();
Alexey Bataev87933c72015-09-18 08:07:34 +00006067 llvm::Value *Args[] = {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006068 RT.emitUpdateLocation(CGF, Loc), RT.getThreadID(CGF, Loc),
Alexey Bataev87933c72015-09-18 08:07:34 +00006069 CGF.Builder.getInt32(getCancellationKind(CancelRegion))};
6070 // Ignore return result until untied tasks are supported.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006071 auto *Result = CGF.EmitRuntimeCall(
6072 RT.createRuntimeFunction(OMPRTL__kmpc_cancel), Args);
Alexey Bataev87933c72015-09-18 08:07:34 +00006073 // if (__kmpc_cancel()) {
Alexey Bataev87933c72015-09-18 08:07:34 +00006074 // exit from construct;
6075 // }
6076 auto *ExitBB = CGF.createBasicBlock(".cancel.exit");
6077 auto *ContBB = CGF.createBasicBlock(".cancel.continue");
6078 auto *Cmp = CGF.Builder.CreateIsNotNull(Result);
6079 CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB);
6080 CGF.EmitBlock(ExitBB);
Alexey Bataev87933c72015-09-18 08:07:34 +00006081 // exit from construct;
6082 auto CancelDest =
6083 CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind());
6084 CGF.EmitBranchThroughCleanup(CancelDest);
6085 CGF.EmitBlock(ContBB, /*IsFinished=*/true);
6086 };
6087 if (IfCond)
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006088 emitOMPIfClause(CGF, IfCond, ThenGen,
6089 [](CodeGenFunction &, PrePostActionTy &) {});
6090 else {
6091 RegionCodeGenTy ThenRCG(ThenGen);
6092 ThenRCG(CGF);
6093 }
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006094 }
6095}
Samuel Antaobed3c462015-10-02 16:14:20 +00006096
Samuel Antaoee8fb302016-01-06 13:42:12 +00006097void CGOpenMPRuntime::emitTargetOutlinedFunction(
6098 const OMPExecutableDirective &D, StringRef ParentName,
6099 llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006100 bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00006101 assert(!ParentName.empty() && "Invalid target region parent name!");
6102
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00006103 emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID,
6104 IsOffloadEntry, CodeGen);
6105}
6106
6107void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper(
6108 const OMPExecutableDirective &D, StringRef ParentName,
6109 llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
6110 bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
Samuel Antao2de62b02016-02-13 23:35:10 +00006111 // Create a unique name for the entry function using the source location
6112 // information of the current target region. The name will be something like:
Samuel Antaoee8fb302016-01-06 13:42:12 +00006113 //
Samuel Antao2de62b02016-02-13 23:35:10 +00006114 // __omp_offloading_DD_FFFF_PP_lBB
Samuel Antaoee8fb302016-01-06 13:42:12 +00006115 //
6116 // where DD_FFFF is an ID unique to the file (device and file IDs), PP is the
Samuel Antao2de62b02016-02-13 23:35:10 +00006117 // mangled name of the function that encloses the target region and BB is the
6118 // line number of the target region.
Samuel Antaoee8fb302016-01-06 13:42:12 +00006119
6120 unsigned DeviceID;
6121 unsigned FileID;
6122 unsigned Line;
Samuel Antaoee8fb302016-01-06 13:42:12 +00006123 getTargetEntryUniqueInfo(CGM.getContext(), D.getLocStart(), DeviceID, FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +00006124 Line);
Samuel Antaoee8fb302016-01-06 13:42:12 +00006125 SmallString<64> EntryFnName;
6126 {
6127 llvm::raw_svector_ostream OS(EntryFnName);
Samuel Antao2de62b02016-02-13 23:35:10 +00006128 OS << "__omp_offloading" << llvm::format("_%x", DeviceID)
6129 << llvm::format("_%x_", FileID) << ParentName << "_l" << Line;
Samuel Antaoee8fb302016-01-06 13:42:12 +00006130 }
6131
Alexey Bataev475a7442018-01-12 19:39:11 +00006132 const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00006133
Samuel Antaobed3c462015-10-02 16:14:20 +00006134 CodeGenFunction CGF(CGM, true);
Samuel Antaoee8fb302016-01-06 13:42:12 +00006135 CGOpenMPTargetRegionInfo CGInfo(CS, CodeGen, EntryFnName);
Samuel Antaobed3c462015-10-02 16:14:20 +00006136 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
Samuel Antaoee8fb302016-01-06 13:42:12 +00006137
Samuel Antao6d004262016-06-16 18:39:34 +00006138 OutlinedFn = CGF.GenerateOpenMPCapturedStmtFunction(CS);
Samuel Antaoee8fb302016-01-06 13:42:12 +00006139
6140 // If this target outline function is not an offload entry, we don't need to
6141 // register it.
6142 if (!IsOffloadEntry)
6143 return;
6144
6145 // The target region ID is used by the runtime library to identify the current
6146 // target region, so it only has to be unique and not necessarily point to
6147 // anything. It could be the pointer to the outlined function that implements
6148 // the target region, but we aren't using that so that the compiler doesn't
6149 // need to keep that, and could therefore inline the host function if proven
6150 // worthwhile during optimization. In the other hand, if emitting code for the
6151 // device, the ID has to be the function address so that it can retrieved from
6152 // the offloading entry and launched by the runtime library. We also mark the
6153 // outlined function to have external linkage in case we are emitting code for
6154 // the device, because these functions will be entry points to the device.
6155
6156 if (CGM.getLangOpts().OpenMPIsDevice) {
6157 OutlinedFnID = llvm::ConstantExpr::getBitCast(OutlinedFn, CGM.Int8PtrTy);
6158 OutlinedFn->setLinkage(llvm::GlobalValue::ExternalLinkage);
Rafael Espindolacbca4872018-01-11 22:15:12 +00006159 OutlinedFn->setDSOLocal(false);
Samuel Antaoee8fb302016-01-06 13:42:12 +00006160 } else
6161 OutlinedFnID = new llvm::GlobalVariable(
6162 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
6163 llvm::GlobalValue::PrivateLinkage,
6164 llvm::Constant::getNullValue(CGM.Int8Ty), ".omp_offload.region_id");
6165
6166 // Register the information for the entry associated with this target region.
6167 OffloadEntriesInfoManager.registerTargetRegionEntryInfo(
Samuel Antaof83efdb2017-01-05 16:02:49 +00006168 DeviceID, FileID, ParentName, Line, OutlinedFn, OutlinedFnID,
Alexey Bataev03f270c2018-03-30 18:31:07 +00006169 OffloadEntriesInfoManagerTy::OMPTargetRegionEntryTargetRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +00006170}
6171
Carlo Bertolli6eee9062016-04-29 01:37:30 +00006172/// discard all CompoundStmts intervening between two constructs
6173static const Stmt *ignoreCompoundStmts(const Stmt *Body) {
6174 while (auto *CS = dyn_cast_or_null<CompoundStmt>(Body))
6175 Body = CS->body_front();
6176
6177 return Body;
6178}
6179
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006180/// Emit the number of teams for a target directive. Inspect the num_teams
6181/// clause associated with a teams construct combined or closely nested
6182/// with the target directive.
6183///
6184/// Emit a team of size one for directives such as 'target parallel' that
6185/// have no associated teams construct.
6186///
6187/// Otherwise, return nullptr.
Samuel Antaob68e2db2016-03-03 16:20:23 +00006188static llvm::Value *
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006189emitNumTeamsForTargetDirective(CGOpenMPRuntime &OMPRuntime,
6190 CodeGenFunction &CGF,
6191 const OMPExecutableDirective &D) {
Samuel Antaob68e2db2016-03-03 16:20:23 +00006192
6193 assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the "
6194 "teams directive expected to be "
6195 "emitted only for the host!");
6196
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00006197 auto &Bld = CGF.Builder;
6198
6199 // If the target directive is combined with a teams directive:
6200 // Return the value in the num_teams clause, if any.
6201 // Otherwise, return 0 to denote the runtime default.
6202 if (isOpenMPTeamsDirective(D.getDirectiveKind())) {
6203 if (const auto *NumTeamsClause = D.getSingleClause<OMPNumTeamsClause>()) {
6204 CodeGenFunction::RunCleanupsScope NumTeamsScope(CGF);
6205 auto NumTeams = CGF.EmitScalarExpr(NumTeamsClause->getNumTeams(),
6206 /*IgnoreResultAssign*/ true);
6207 return Bld.CreateIntCast(NumTeams, CGF.Int32Ty,
6208 /*IsSigned=*/true);
6209 }
6210
6211 // The default value is 0.
6212 return Bld.getInt32(0);
6213 }
6214
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006215 // If the target directive is combined with a parallel directive but not a
6216 // teams directive, start one team.
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00006217 if (isOpenMPParallelDirective(D.getDirectiveKind()))
6218 return Bld.getInt32(1);
Samuel Antaob68e2db2016-03-03 16:20:23 +00006219
6220 // If the current target region has a teams region enclosed, we need to get
6221 // the number of teams to pass to the runtime function call. This is done
6222 // by generating the expression in a inlined region. This is required because
6223 // the expression is captured in the enclosing target environment when the
6224 // teams directive is not combined with target.
6225
Alexey Bataev475a7442018-01-12 19:39:11 +00006226 const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target);
Samuel Antaob68e2db2016-03-03 16:20:23 +00006227
Alexey Bataev50a1c782017-12-01 21:31:08 +00006228 if (auto *TeamsDir = dyn_cast_or_null<OMPExecutableDirective>(
Carlo Bertolli6eee9062016-04-29 01:37:30 +00006229 ignoreCompoundStmts(CS.getCapturedStmt()))) {
Alexey Bataev50a1c782017-12-01 21:31:08 +00006230 if (isOpenMPTeamsDirective(TeamsDir->getDirectiveKind())) {
6231 if (auto *NTE = TeamsDir->getSingleClause<OMPNumTeamsClause>()) {
6232 CGOpenMPInnerExprInfo CGInfo(CGF, CS);
6233 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
6234 llvm::Value *NumTeams = CGF.EmitScalarExpr(NTE->getNumTeams());
6235 return Bld.CreateIntCast(NumTeams, CGF.Int32Ty,
6236 /*IsSigned=*/true);
6237 }
Samuel Antaob68e2db2016-03-03 16:20:23 +00006238
Alexey Bataev50a1c782017-12-01 21:31:08 +00006239 // If we have an enclosed teams directive but no num_teams clause we use
6240 // the default value 0.
6241 return Bld.getInt32(0);
6242 }
Samuel Antaob68e2db2016-03-03 16:20:23 +00006243 }
6244
6245 // No teams associated with the directive.
6246 return nullptr;
6247}
6248
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006249/// Emit the number of threads for a target directive. Inspect the
6250/// thread_limit clause associated with a teams construct combined or closely
6251/// nested with the target directive.
6252///
6253/// Emit the num_threads clause for directives such as 'target parallel' that
6254/// have no associated teams construct.
6255///
6256/// Otherwise, return nullptr.
Samuel Antaob68e2db2016-03-03 16:20:23 +00006257static llvm::Value *
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006258emitNumThreadsForTargetDirective(CGOpenMPRuntime &OMPRuntime,
6259 CodeGenFunction &CGF,
6260 const OMPExecutableDirective &D) {
Samuel Antaob68e2db2016-03-03 16:20:23 +00006261
6262 assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the "
6263 "teams directive expected to be "
6264 "emitted only for the host!");
6265
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006266 auto &Bld = CGF.Builder;
6267
6268 //
6269 // If the target directive is combined with a teams directive:
6270 // Return the value in the thread_limit clause, if any.
6271 //
6272 // If the target directive is combined with a parallel directive:
6273 // Return the value in the num_threads clause, if any.
6274 //
6275 // If both clauses are set, select the minimum of the two.
6276 //
6277 // If neither teams or parallel combined directives set the number of threads
6278 // in a team, return 0 to denote the runtime default.
6279 //
6280 // If this is not a teams directive return nullptr.
6281
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00006282 if (isOpenMPTeamsDirective(D.getDirectiveKind()) ||
6283 isOpenMPParallelDirective(D.getDirectiveKind())) {
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006284 llvm::Value *DefaultThreadLimitVal = Bld.getInt32(0);
6285 llvm::Value *NumThreadsVal = nullptr;
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00006286 llvm::Value *ThreadLimitVal = nullptr;
6287
6288 if (const auto *ThreadLimitClause =
6289 D.getSingleClause<OMPThreadLimitClause>()) {
6290 CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF);
6291 auto ThreadLimit = CGF.EmitScalarExpr(ThreadLimitClause->getThreadLimit(),
6292 /*IgnoreResultAssign*/ true);
6293 ThreadLimitVal = Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty,
6294 /*IsSigned=*/true);
6295 }
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006296
6297 if (const auto *NumThreadsClause =
6298 D.getSingleClause<OMPNumThreadsClause>()) {
6299 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
6300 llvm::Value *NumThreads =
6301 CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
6302 /*IgnoreResultAssign*/ true);
6303 NumThreadsVal =
6304 Bld.CreateIntCast(NumThreads, CGF.Int32Ty, /*IsSigned=*/true);
6305 }
6306
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00006307 // Select the lesser of thread_limit and num_threads.
6308 if (NumThreadsVal)
6309 ThreadLimitVal = ThreadLimitVal
6310 ? Bld.CreateSelect(Bld.CreateICmpSLT(NumThreadsVal,
6311 ThreadLimitVal),
6312 NumThreadsVal, ThreadLimitVal)
6313 : NumThreadsVal;
Samuel Antaob68e2db2016-03-03 16:20:23 +00006314
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00006315 // Set default value passed to the runtime if either teams or a target
6316 // parallel type directive is found but no clause is specified.
6317 if (!ThreadLimitVal)
6318 ThreadLimitVal = DefaultThreadLimitVal;
6319
6320 return ThreadLimitVal;
6321 }
Arpith Chacko Jacob86f9e462017-01-25 01:45:59 +00006322
Samuel Antaob68e2db2016-03-03 16:20:23 +00006323 // If the current target region has a teams region enclosed, we need to get
6324 // the thread limit to pass to the runtime function call. This is done
6325 // by generating the expression in a inlined region. This is required because
6326 // the expression is captured in the enclosing target environment when the
6327 // teams directive is not combined with target.
6328
Alexey Bataev475a7442018-01-12 19:39:11 +00006329 const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target);
Samuel Antaob68e2db2016-03-03 16:20:23 +00006330
Alexey Bataev50a1c782017-12-01 21:31:08 +00006331 if (auto *TeamsDir = dyn_cast_or_null<OMPExecutableDirective>(
Carlo Bertolli6eee9062016-04-29 01:37:30 +00006332 ignoreCompoundStmts(CS.getCapturedStmt()))) {
Alexey Bataev50a1c782017-12-01 21:31:08 +00006333 if (isOpenMPTeamsDirective(TeamsDir->getDirectiveKind())) {
6334 if (auto *TLE = TeamsDir->getSingleClause<OMPThreadLimitClause>()) {
6335 CGOpenMPInnerExprInfo CGInfo(CGF, CS);
6336 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
6337 llvm::Value *ThreadLimit = CGF.EmitScalarExpr(TLE->getThreadLimit());
6338 return CGF.Builder.CreateIntCast(ThreadLimit, CGF.Int32Ty,
6339 /*IsSigned=*/true);
6340 }
Samuel Antaob68e2db2016-03-03 16:20:23 +00006341
Alexey Bataev50a1c782017-12-01 21:31:08 +00006342 // If we have an enclosed teams directive but no thread_limit clause we
6343 // use the default value 0.
6344 return CGF.Builder.getInt32(0);
6345 }
Samuel Antaob68e2db2016-03-03 16:20:23 +00006346 }
6347
6348 // No teams associated with the directive.
6349 return nullptr;
6350}
6351
Samuel Antao86ace552016-04-27 22:40:57 +00006352namespace {
6353// \brief Utility to handle information from clauses associated with a given
6354// construct that use mappable expressions (e.g. 'map' clause, 'to' clause).
6355// It provides a convenient interface to obtain the information and generate
6356// code for that information.
6357class MappableExprsHandler {
6358public:
6359 /// \brief Values for bit flags used to specify the mapping type for
6360 /// offloading.
6361 enum OpenMPOffloadMappingFlags {
Samuel Antao86ace552016-04-27 22:40:57 +00006362 /// \brief Allocate memory on the device and move data from host to device.
6363 OMP_MAP_TO = 0x01,
6364 /// \brief Allocate memory on the device and move data from device to host.
6365 OMP_MAP_FROM = 0x02,
6366 /// \brief Always perform the requested mapping action on the element, even
6367 /// if it was already mapped before.
6368 OMP_MAP_ALWAYS = 0x04,
Samuel Antao86ace552016-04-27 22:40:57 +00006369 /// \brief Delete the element from the device environment, ignoring the
6370 /// current reference count associated with the element.
Samuel Antao6782e942016-05-26 16:48:10 +00006371 OMP_MAP_DELETE = 0x08,
George Rokos065755d2017-11-07 18:27:04 +00006372 /// \brief The element being mapped is a pointer-pointee pair; both the
6373 /// pointer and the pointee should be mapped.
6374 OMP_MAP_PTR_AND_OBJ = 0x10,
6375 /// \brief This flags signals that the base address of an entry should be
6376 /// passed to the target kernel as an argument.
6377 OMP_MAP_TARGET_PARAM = 0x20,
Samuel Antaocc10b852016-07-28 14:23:26 +00006378 /// \brief Signal that the runtime library has to return the device pointer
George Rokos065755d2017-11-07 18:27:04 +00006379 /// in the current position for the data being mapped. Used when we have the
6380 /// use_device_ptr clause.
6381 OMP_MAP_RETURN_PARAM = 0x40,
Samuel Antaod486f842016-05-26 16:53:38 +00006382 /// \brief This flag signals that the reference being passed is a pointer to
6383 /// private data.
George Rokos065755d2017-11-07 18:27:04 +00006384 OMP_MAP_PRIVATE = 0x80,
Samuel Antao86ace552016-04-27 22:40:57 +00006385 /// \brief Pass the element to the device by value.
George Rokos065755d2017-11-07 18:27:04 +00006386 OMP_MAP_LITERAL = 0x100,
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006387 /// Implicit map
6388 OMP_MAP_IMPLICIT = 0x200,
Samuel Antao86ace552016-04-27 22:40:57 +00006389 };
6390
Samuel Antaocc10b852016-07-28 14:23:26 +00006391 /// Class that associates information with a base pointer to be passed to the
6392 /// runtime library.
6393 class BasePointerInfo {
6394 /// The base pointer.
6395 llvm::Value *Ptr = nullptr;
6396 /// The base declaration that refers to this device pointer, or null if
6397 /// there is none.
6398 const ValueDecl *DevPtrDecl = nullptr;
6399
6400 public:
6401 BasePointerInfo(llvm::Value *Ptr, const ValueDecl *DevPtrDecl = nullptr)
6402 : Ptr(Ptr), DevPtrDecl(DevPtrDecl) {}
6403 llvm::Value *operator*() const { return Ptr; }
6404 const ValueDecl *getDevicePtrDecl() const { return DevPtrDecl; }
6405 void setDevicePtrDecl(const ValueDecl *D) { DevPtrDecl = D; }
6406 };
6407
6408 typedef SmallVector<BasePointerInfo, 16> MapBaseValuesArrayTy;
Samuel Antao86ace552016-04-27 22:40:57 +00006409 typedef SmallVector<llvm::Value *, 16> MapValuesArrayTy;
George Rokos63bc9d62017-11-21 18:25:12 +00006410 typedef SmallVector<uint64_t, 16> MapFlagsArrayTy;
Samuel Antao86ace552016-04-27 22:40:57 +00006411
6412private:
6413 /// \brief Directive from where the map clauses were extracted.
Samuel Antao44bcdb32016-07-28 15:31:29 +00006414 const OMPExecutableDirective &CurDir;
Samuel Antao86ace552016-04-27 22:40:57 +00006415
6416 /// \brief Function the directive is being generated for.
6417 CodeGenFunction &CGF;
6418
Samuel Antaod486f842016-05-26 16:53:38 +00006419 /// \brief Set of all first private variables in the current directive.
6420 llvm::SmallPtrSet<const VarDecl *, 8> FirstPrivateDecls;
Alexey Bataev3f96fe62017-12-13 17:31:39 +00006421 /// Set of all reduction variables in the current directive.
6422 llvm::SmallPtrSet<const VarDecl *, 8> ReductionDecls;
Samuel Antaod486f842016-05-26 16:53:38 +00006423
Samuel Antao6890b092016-07-28 14:25:09 +00006424 /// Map between device pointer declarations and their expression components.
6425 /// The key value for declarations in 'this' is null.
6426 llvm::DenseMap<
6427 const ValueDecl *,
6428 SmallVector<OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>>
6429 DevPointersMap;
6430
Samuel Antao86ace552016-04-27 22:40:57 +00006431 llvm::Value *getExprTypeSize(const Expr *E) const {
6432 auto ExprTy = E->getType().getCanonicalType();
6433
6434 // Reference types are ignored for mapping purposes.
6435 if (auto *RefTy = ExprTy->getAs<ReferenceType>())
6436 ExprTy = RefTy->getPointeeType().getCanonicalType();
6437
6438 // Given that an array section is considered a built-in type, we need to
6439 // do the calculation based on the length of the section instead of relying
6440 // on CGF.getTypeSize(E->getType()).
6441 if (const auto *OAE = dyn_cast<OMPArraySectionExpr>(E)) {
6442 QualType BaseTy = OMPArraySectionExpr::getBaseOriginalType(
6443 OAE->getBase()->IgnoreParenImpCasts())
6444 .getCanonicalType();
6445
6446 // If there is no length associated with the expression, that means we
6447 // are using the whole length of the base.
6448 if (!OAE->getLength() && OAE->getColonLoc().isValid())
6449 return CGF.getTypeSize(BaseTy);
6450
6451 llvm::Value *ElemSize;
6452 if (auto *PTy = BaseTy->getAs<PointerType>())
6453 ElemSize = CGF.getTypeSize(PTy->getPointeeType().getCanonicalType());
6454 else {
6455 auto *ATy = cast<ArrayType>(BaseTy.getTypePtr());
6456 assert(ATy && "Expecting array type if not a pointer type.");
6457 ElemSize = CGF.getTypeSize(ATy->getElementType().getCanonicalType());
6458 }
6459
6460 // If we don't have a length at this point, that is because we have an
6461 // array section with a single element.
6462 if (!OAE->getLength())
6463 return ElemSize;
6464
6465 auto *LengthVal = CGF.EmitScalarExpr(OAE->getLength());
6466 LengthVal =
6467 CGF.Builder.CreateIntCast(LengthVal, CGF.SizeTy, /*isSigned=*/false);
6468 return CGF.Builder.CreateNUWMul(LengthVal, ElemSize);
6469 }
6470 return CGF.getTypeSize(ExprTy);
6471 }
6472
6473 /// \brief Return the corresponding bits for a given map clause modifier. Add
6474 /// a flag marking the map as a pointer if requested. Add a flag marking the
Samuel Antao6782e942016-05-26 16:48:10 +00006475 /// map as the first one of a series of maps that relate to the same map
6476 /// expression.
George Rokos63bc9d62017-11-21 18:25:12 +00006477 uint64_t getMapTypeBits(OpenMPMapClauseKind MapType,
Samuel Antao86ace552016-04-27 22:40:57 +00006478 OpenMPMapClauseKind MapTypeModifier, bool AddPtrFlag,
George Rokos065755d2017-11-07 18:27:04 +00006479 bool AddIsTargetParamFlag) const {
George Rokos63bc9d62017-11-21 18:25:12 +00006480 uint64_t Bits = 0u;
Samuel Antao86ace552016-04-27 22:40:57 +00006481 switch (MapType) {
6482 case OMPC_MAP_alloc:
Samuel Antao6782e942016-05-26 16:48:10 +00006483 case OMPC_MAP_release:
6484 // alloc and release is the default behavior in the runtime library, i.e.
6485 // if we don't pass any bits alloc/release that is what the runtime is
6486 // going to do. Therefore, we don't need to signal anything for these two
6487 // type modifiers.
Samuel Antao86ace552016-04-27 22:40:57 +00006488 break;
6489 case OMPC_MAP_to:
6490 Bits = OMP_MAP_TO;
6491 break;
6492 case OMPC_MAP_from:
6493 Bits = OMP_MAP_FROM;
6494 break;
6495 case OMPC_MAP_tofrom:
6496 Bits = OMP_MAP_TO | OMP_MAP_FROM;
6497 break;
6498 case OMPC_MAP_delete:
6499 Bits = OMP_MAP_DELETE;
6500 break;
Samuel Antao86ace552016-04-27 22:40:57 +00006501 default:
6502 llvm_unreachable("Unexpected map type!");
6503 break;
6504 }
6505 if (AddPtrFlag)
George Rokos065755d2017-11-07 18:27:04 +00006506 Bits |= OMP_MAP_PTR_AND_OBJ;
6507 if (AddIsTargetParamFlag)
6508 Bits |= OMP_MAP_TARGET_PARAM;
Samuel Antao86ace552016-04-27 22:40:57 +00006509 if (MapTypeModifier == OMPC_MAP_always)
6510 Bits |= OMP_MAP_ALWAYS;
6511 return Bits;
6512 }
6513
6514 /// \brief Return true if the provided expression is a final array section. A
6515 /// final array section, is one whose length can't be proved to be one.
6516 bool isFinalArraySectionExpression(const Expr *E) const {
6517 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
6518
6519 // It is not an array section and therefore not a unity-size one.
6520 if (!OASE)
6521 return false;
6522
6523 // An array section with no colon always refer to a single element.
6524 if (OASE->getColonLoc().isInvalid())
6525 return false;
6526
6527 auto *Length = OASE->getLength();
6528
6529 // If we don't have a length we have to check if the array has size 1
6530 // for this dimension. Also, we should always expect a length if the
6531 // base type is pointer.
6532 if (!Length) {
6533 auto BaseQTy = OMPArraySectionExpr::getBaseOriginalType(
6534 OASE->getBase()->IgnoreParenImpCasts())
6535 .getCanonicalType();
6536 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
6537 return ATy->getSize().getSExtValue() != 1;
6538 // If we don't have a constant dimension length, we have to consider
6539 // the current section as having any size, so it is not necessarily
6540 // unitary. If it happen to be unity size, that's user fault.
6541 return true;
6542 }
6543
6544 // Check if the length evaluates to 1.
6545 llvm::APSInt ConstLength;
6546 if (!Length->EvaluateAsInt(ConstLength, CGF.getContext()))
6547 return true; // Can have more that size 1.
6548
6549 return ConstLength.getSExtValue() != 1;
6550 }
6551
Alexey Bataev92327c52018-03-26 16:40:55 +00006552 /// \brief Return the adjusted map modifiers if the declaration a capture
6553 /// refers to appears in a first-private clause. This is expected to be used
6554 /// only with directives that start with 'target'.
6555 unsigned adjustMapModifiersForPrivateClauses(const CapturedStmt::Capture &Cap,
6556 unsigned CurrentModifiers) {
6557 assert(Cap.capturesVariable() && "Expected capture by reference only!");
6558
6559 // A first private variable captured by reference will use only the
6560 // 'private ptr' and 'map to' flag. Return the right flags if the captured
6561 // declaration is known as first-private in this handler.
6562 if (FirstPrivateDecls.count(Cap.getCapturedVar()))
6563 return MappableExprsHandler::OMP_MAP_PRIVATE |
6564 MappableExprsHandler::OMP_MAP_TO;
6565 // Reduction variable will use only the 'private ptr' and 'map to_from'
6566 // flag.
6567 if (ReductionDecls.count(Cap.getCapturedVar())) {
6568 return MappableExprsHandler::OMP_MAP_TO |
6569 MappableExprsHandler::OMP_MAP_FROM;
6570 }
6571
6572 // We didn't modify anything.
6573 return CurrentModifiers;
6574 }
6575
6576public:
6577 MappableExprsHandler(const OMPExecutableDirective &Dir, CodeGenFunction &CGF)
6578 : CurDir(Dir), CGF(CGF) {
6579 // Extract firstprivate clause information.
6580 for (const auto *C : Dir.getClausesOfKind<OMPFirstprivateClause>())
6581 for (const auto *D : C->varlists())
6582 FirstPrivateDecls.insert(
6583 cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl());
6584 for (const auto *C : Dir.getClausesOfKind<OMPReductionClause>()) {
6585 for (const auto *D : C->varlists()) {
6586 ReductionDecls.insert(
6587 cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl());
6588 }
6589 }
6590 // Extract device pointer clause information.
6591 for (const auto *C : Dir.getClausesOfKind<OMPIsDevicePtrClause>())
6592 for (auto L : C->component_lists())
6593 DevPointersMap[L.first].push_back(L.second);
6594 }
6595
Samuel Antao86ace552016-04-27 22:40:57 +00006596 /// \brief Generate the base pointers, section pointers, sizes and map type
6597 /// bits for the provided map type, map modifier, and expression components.
6598 /// \a IsFirstComponent should be set to true if the provided set of
6599 /// components is the first associated with a capture.
6600 void generateInfoForComponentList(
6601 OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapTypeModifier,
6602 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
Samuel Antaocc10b852016-07-28 14:23:26 +00006603 MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers,
Samuel Antao86ace552016-04-27 22:40:57 +00006604 MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types,
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006605 bool IsFirstComponentList, bool IsImplicit) const {
Samuel Antao86ace552016-04-27 22:40:57 +00006606
6607 // The following summarizes what has to be generated for each map and the
6608 // types bellow. The generated information is expressed in this order:
6609 // base pointer, section pointer, size, flags
6610 // (to add to the ones that come from the map type and modifier).
6611 //
6612 // double d;
6613 // int i[100];
6614 // float *p;
6615 //
6616 // struct S1 {
6617 // int i;
6618 // float f[50];
6619 // }
6620 // struct S2 {
6621 // int i;
6622 // float f[50];
6623 // S1 s;
6624 // double *p;
6625 // struct S2 *ps;
6626 // }
6627 // S2 s;
6628 // S2 *ps;
6629 //
6630 // map(d)
6631 // &d, &d, sizeof(double), noflags
6632 //
6633 // map(i)
6634 // &i, &i, 100*sizeof(int), noflags
6635 //
6636 // map(i[1:23])
6637 // &i(=&i[0]), &i[1], 23*sizeof(int), noflags
6638 //
6639 // map(p)
6640 // &p, &p, sizeof(float*), noflags
6641 //
6642 // map(p[1:24])
6643 // p, &p[1], 24*sizeof(float), noflags
6644 //
6645 // map(s)
6646 // &s, &s, sizeof(S2), noflags
6647 //
6648 // map(s.i)
6649 // &s, &(s.i), sizeof(int), noflags
6650 //
6651 // map(s.s.f)
6652 // &s, &(s.i.f), 50*sizeof(int), noflags
6653 //
6654 // map(s.p)
6655 // &s, &(s.p), sizeof(double*), noflags
6656 //
6657 // map(s.p[:22], s.a s.b)
6658 // &s, &(s.p), sizeof(double*), noflags
George Rokos065755d2017-11-07 18:27:04 +00006659 // &(s.p), &(s.p[0]), 22*sizeof(double), ptr_flag
Samuel Antao86ace552016-04-27 22:40:57 +00006660 //
6661 // map(s.ps)
6662 // &s, &(s.ps), sizeof(S2*), noflags
6663 //
6664 // map(s.ps->s.i)
6665 // &s, &(s.ps), sizeof(S2*), noflags
George Rokos065755d2017-11-07 18:27:04 +00006666 // &(s.ps), &(s.ps->s.i), sizeof(int), ptr_flag
Samuel Antao86ace552016-04-27 22:40:57 +00006667 //
6668 // map(s.ps->ps)
6669 // &s, &(s.ps), sizeof(S2*), noflags
George Rokos065755d2017-11-07 18:27:04 +00006670 // &(s.ps), &(s.ps->ps), sizeof(S2*), ptr_flag
Samuel Antao86ace552016-04-27 22:40:57 +00006671 //
6672 // map(s.ps->ps->ps)
6673 // &s, &(s.ps), sizeof(S2*), noflags
George Rokos065755d2017-11-07 18:27:04 +00006674 // &(s.ps), &(s.ps->ps), sizeof(S2*), ptr_flag
6675 // &(s.ps->ps), &(s.ps->ps->ps), sizeof(S2*), ptr_flag
Samuel Antao86ace552016-04-27 22:40:57 +00006676 //
6677 // map(s.ps->ps->s.f[:22])
6678 // &s, &(s.ps), sizeof(S2*), noflags
George Rokos065755d2017-11-07 18:27:04 +00006679 // &(s.ps), &(s.ps->ps), sizeof(S2*), ptr_flag
6680 // &(s.ps->ps), &(s.ps->ps->s.f[0]), 22*sizeof(float), ptr_flag
Samuel Antao86ace552016-04-27 22:40:57 +00006681 //
6682 // map(ps)
6683 // &ps, &ps, sizeof(S2*), noflags
6684 //
6685 // map(ps->i)
6686 // ps, &(ps->i), sizeof(int), noflags
6687 //
6688 // map(ps->s.f)
6689 // ps, &(ps->s.f[0]), 50*sizeof(float), noflags
6690 //
6691 // map(ps->p)
6692 // ps, &(ps->p), sizeof(double*), noflags
6693 //
6694 // map(ps->p[:22])
6695 // ps, &(ps->p), sizeof(double*), noflags
George Rokos065755d2017-11-07 18:27:04 +00006696 // &(ps->p), &(ps->p[0]), 22*sizeof(double), ptr_flag
Samuel Antao86ace552016-04-27 22:40:57 +00006697 //
6698 // map(ps->ps)
6699 // ps, &(ps->ps), sizeof(S2*), noflags
6700 //
6701 // map(ps->ps->s.i)
6702 // ps, &(ps->ps), sizeof(S2*), noflags
George Rokos065755d2017-11-07 18:27:04 +00006703 // &(ps->ps), &(ps->ps->s.i), sizeof(int), ptr_flag
Samuel Antao86ace552016-04-27 22:40:57 +00006704 //
6705 // map(ps->ps->ps)
6706 // ps, &(ps->ps), sizeof(S2*), noflags
George Rokos065755d2017-11-07 18:27:04 +00006707 // &(ps->ps), &(ps->ps->ps), sizeof(S2*), ptr_flag
Samuel Antao86ace552016-04-27 22:40:57 +00006708 //
6709 // map(ps->ps->ps->ps)
6710 // ps, &(ps->ps), sizeof(S2*), noflags
George Rokos065755d2017-11-07 18:27:04 +00006711 // &(ps->ps), &(ps->ps->ps), sizeof(S2*), ptr_flag
6712 // &(ps->ps->ps), &(ps->ps->ps->ps), sizeof(S2*), ptr_flag
Samuel Antao86ace552016-04-27 22:40:57 +00006713 //
6714 // map(ps->ps->ps->s.f[:22])
6715 // ps, &(ps->ps), sizeof(S2*), noflags
George Rokos065755d2017-11-07 18:27:04 +00006716 // &(ps->ps), &(ps->ps->ps), sizeof(S2*), ptr_flag
6717 // &(ps->ps->ps), &(ps->ps->ps->s.f[0]), 22*sizeof(float), ptr_flag
Samuel Antao86ace552016-04-27 22:40:57 +00006718
6719 // Track if the map information being generated is the first for a capture.
6720 bool IsCaptureFirstInfo = IsFirstComponentList;
Alexey Bataev92327c52018-03-26 16:40:55 +00006721 bool IsLink = false; // Is this variable a "declare target link"?
Samuel Antao86ace552016-04-27 22:40:57 +00006722
6723 // Scan the components from the base to the complete expression.
6724 auto CI = Components.rbegin();
6725 auto CE = Components.rend();
6726 auto I = CI;
6727
6728 // Track if the map information being generated is the first for a list of
6729 // components.
6730 bool IsExpressionFirstInfo = true;
6731 llvm::Value *BP = nullptr;
6732
6733 if (auto *ME = dyn_cast<MemberExpr>(I->getAssociatedExpression())) {
6734 // The base is the 'this' pointer. The content of the pointer is going
6735 // to be the base of the field being mapped.
6736 BP = CGF.EmitScalarExpr(ME->getBase());
6737 } else {
6738 // The base is the reference to the variable.
6739 // BP = &Var.
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006740 BP = CGF.EmitOMPSharedLValue(I->getAssociatedExpression()).getPointer();
Alexey Bataev92327c52018-03-26 16:40:55 +00006741 if (const auto *VD =
6742 dyn_cast_or_null<VarDecl>(I->getAssociatedDeclaration())) {
6743 if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
6744 isDeclareTargetDeclaration(VD)) {
6745 assert(*Res == OMPDeclareTargetDeclAttr::MT_Link &&
6746 "Declare target link is expected.");
6747 // Avoid warning in release build.
6748 (void)*Res;
6749 IsLink = true;
6750 BP = CGF.CGM.getOpenMPRuntime()
Alexey Bataev03f270c2018-03-30 18:31:07 +00006751 .getAddrOfDeclareTargetLink(VD)
Alexey Bataev92327c52018-03-26 16:40:55 +00006752 .getPointer();
6753 }
6754 }
Samuel Antao86ace552016-04-27 22:40:57 +00006755
6756 // If the variable is a pointer and is being dereferenced (i.e. is not
Nico Webera6916892016-06-10 18:53:04 +00006757 // the last component), the base has to be the pointer itself, not its
Samuel Antao403ffd42016-07-27 22:49:49 +00006758 // reference. References are ignored for mapping purposes.
6759 QualType Ty =
6760 I->getAssociatedDeclaration()->getType().getNonReferenceType();
6761 if (Ty->isAnyPointerType() && std::next(I) != CE) {
6762 auto PtrAddr = CGF.MakeNaturalAlignAddrLValue(BP, Ty);
Samuel Antao86ace552016-04-27 22:40:57 +00006763 BP = CGF.EmitLoadOfPointerLValue(PtrAddr.getAddress(),
Samuel Antao403ffd42016-07-27 22:49:49 +00006764 Ty->castAs<PointerType>())
Samuel Antao86ace552016-04-27 22:40:57 +00006765 .getPointer();
6766
6767 // We do not need to generate individual map information for the
6768 // pointer, it can be associated with the combined storage.
6769 ++I;
6770 }
6771 }
6772
George Rokos63bc9d62017-11-21 18:25:12 +00006773 uint64_t DefaultFlags = IsImplicit ? OMP_MAP_IMPLICIT : 0;
Samuel Antao86ace552016-04-27 22:40:57 +00006774 for (; I != CE; ++I) {
6775 auto Next = std::next(I);
6776
6777 // We need to generate the addresses and sizes if this is the last
6778 // component, if the component is a pointer or if it is an array section
6779 // whose length can't be proved to be one. If this is a pointer, it
6780 // becomes the base address for the following components.
6781
6782 // A final array section, is one whose length can't be proved to be one.
6783 bool IsFinalArraySection =
6784 isFinalArraySectionExpression(I->getAssociatedExpression());
6785
6786 // Get information on whether the element is a pointer. Have to do a
6787 // special treatment for array sections given that they are built-in
6788 // types.
6789 const auto *OASE =
6790 dyn_cast<OMPArraySectionExpr>(I->getAssociatedExpression());
6791 bool IsPointer =
6792 (OASE &&
6793 OMPArraySectionExpr::getBaseOriginalType(OASE)
6794 .getCanonicalType()
6795 ->isAnyPointerType()) ||
6796 I->getAssociatedExpression()->getType()->isAnyPointerType();
6797
6798 if (Next == CE || IsPointer || IsFinalArraySection) {
6799
6800 // If this is not the last component, we expect the pointer to be
6801 // associated with an array expression or member expression.
6802 assert((Next == CE ||
6803 isa<MemberExpr>(Next->getAssociatedExpression()) ||
6804 isa<ArraySubscriptExpr>(Next->getAssociatedExpression()) ||
6805 isa<OMPArraySectionExpr>(Next->getAssociatedExpression())) &&
6806 "Unexpected expression");
6807
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006808 llvm::Value *LB =
6809 CGF.EmitOMPSharedLValue(I->getAssociatedExpression()).getPointer();
Samuel Antao86ace552016-04-27 22:40:57 +00006810 auto *Size = getExprTypeSize(I->getAssociatedExpression());
6811
Samuel Antao03a3cec2016-07-27 22:52:16 +00006812 // If we have a member expression and the current component is a
6813 // reference, we have to map the reference too. Whenever we have a
6814 // reference, the section that reference refers to is going to be a
6815 // load instruction from the storage assigned to the reference.
6816 if (isa<MemberExpr>(I->getAssociatedExpression()) &&
6817 I->getAssociatedDeclaration()->getType()->isReferenceType()) {
6818 auto *LI = cast<llvm::LoadInst>(LB);
6819 auto *RefAddr = LI->getPointerOperand();
6820
6821 BasePointers.push_back(BP);
6822 Pointers.push_back(RefAddr);
6823 Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy));
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006824 Types.push_back(DefaultFlags |
6825 getMapTypeBits(
6826 /*MapType*/ OMPC_MAP_alloc,
6827 /*MapTypeModifier=*/OMPC_MAP_unknown,
6828 !IsExpressionFirstInfo, IsCaptureFirstInfo));
Samuel Antao03a3cec2016-07-27 22:52:16 +00006829 IsExpressionFirstInfo = false;
6830 IsCaptureFirstInfo = false;
6831 // The reference will be the next base address.
6832 BP = RefAddr;
6833 }
6834
6835 BasePointers.push_back(BP);
Samuel Antao86ace552016-04-27 22:40:57 +00006836 Pointers.push_back(LB);
6837 Sizes.push_back(Size);
Samuel Antao03a3cec2016-07-27 22:52:16 +00006838
Samuel Antao6782e942016-05-26 16:48:10 +00006839 // We need to add a pointer flag for each map that comes from the
6840 // same expression except for the first one. We also need to signal
6841 // this map is the first one that relates with the current capture
6842 // (there is a set of entries for each capture).
Alexey Bataev92327c52018-03-26 16:40:55 +00006843 Types.push_back(DefaultFlags |
6844 getMapTypeBits(MapType, MapTypeModifier,
6845 !IsExpressionFirstInfo || IsLink,
6846 IsCaptureFirstInfo && !IsLink));
Samuel Antao86ace552016-04-27 22:40:57 +00006847
6848 // If we have a final array section, we are done with this expression.
6849 if (IsFinalArraySection)
6850 break;
6851
6852 // The pointer becomes the base for the next element.
6853 if (Next != CE)
6854 BP = LB;
6855
6856 IsExpressionFirstInfo = false;
6857 IsCaptureFirstInfo = false;
Samuel Antao86ace552016-04-27 22:40:57 +00006858 }
6859 }
6860 }
6861
Samuel Antao86ace552016-04-27 22:40:57 +00006862 /// \brief Generate all the base pointers, section pointers, sizes and map
Samuel Antaocc10b852016-07-28 14:23:26 +00006863 /// types for the extracted mappable expressions. Also, for each item that
6864 /// relates with a device pointer, a pair of the relevant declaration and
6865 /// index where it occurs is appended to the device pointers info array.
6866 void generateAllInfo(MapBaseValuesArrayTy &BasePointers,
Samuel Antao86ace552016-04-27 22:40:57 +00006867 MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes,
6868 MapFlagsArrayTy &Types) const {
6869 BasePointers.clear();
6870 Pointers.clear();
6871 Sizes.clear();
6872 Types.clear();
6873
6874 struct MapInfo {
Samuel Antaocc10b852016-07-28 14:23:26 +00006875 /// Kind that defines how a device pointer has to be returned.
6876 enum ReturnPointerKind {
6877 // Don't have to return any pointer.
6878 RPK_None,
6879 // Pointer is the base of the declaration.
6880 RPK_Base,
6881 // Pointer is a member of the base declaration - 'this'
6882 RPK_Member,
6883 // Pointer is a reference and a member of the base declaration - 'this'
6884 RPK_MemberReference,
6885 };
Samuel Antao86ace552016-04-27 22:40:57 +00006886 OMPClauseMappableExprCommon::MappableExprComponentListRef Components;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006887 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
6888 OpenMPMapClauseKind MapTypeModifier = OMPC_MAP_unknown;
6889 ReturnPointerKind ReturnDevicePointer = RPK_None;
6890 bool IsImplicit = false;
Hans Wennborgbc1b58d2016-07-30 00:41:37 +00006891
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006892 MapInfo() = default;
Samuel Antaocc10b852016-07-28 14:23:26 +00006893 MapInfo(
6894 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
6895 OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapTypeModifier,
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006896 ReturnPointerKind ReturnDevicePointer, bool IsImplicit)
Samuel Antaocc10b852016-07-28 14:23:26 +00006897 : Components(Components), MapType(MapType),
6898 MapTypeModifier(MapTypeModifier),
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006899 ReturnDevicePointer(ReturnDevicePointer), IsImplicit(IsImplicit) {}
Samuel Antao86ace552016-04-27 22:40:57 +00006900 };
6901
6902 // We have to process the component lists that relate with the same
6903 // declaration in a single chunk so that we can generate the map flags
6904 // correctly. Therefore, we organize all lists in a map.
Alexey Bataev5d1c3f62017-06-27 15:46:42 +00006905 llvm::MapVector<const ValueDecl *, SmallVector<MapInfo, 8>> Info;
Samuel Antao8d2d7302016-05-26 18:30:22 +00006906
6907 // Helper function to fill the information map for the different supported
6908 // clauses.
Samuel Antaocc10b852016-07-28 14:23:26 +00006909 auto &&InfoGen = [&Info](
6910 const ValueDecl *D,
6911 OMPClauseMappableExprCommon::MappableExprComponentListRef L,
6912 OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapModifier,
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006913 MapInfo::ReturnPointerKind ReturnDevicePointer, bool IsImplicit) {
Samuel Antaocc10b852016-07-28 14:23:26 +00006914 const ValueDecl *VD =
6915 D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006916 Info[VD].emplace_back(L, MapType, MapModifier, ReturnDevicePointer,
6917 IsImplicit);
Samuel Antaocc10b852016-07-28 14:23:26 +00006918 };
Samuel Antao8d2d7302016-05-26 18:30:22 +00006919
Paul Robinson78fb1322016-08-01 22:12:46 +00006920 // FIXME: MSVC 2013 seems to require this-> to find member CurDir.
Paul Robinson15c84002016-07-29 20:46:16 +00006921 for (auto *C : this->CurDir.getClausesOfKind<OMPMapClause>())
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006922 for (auto L : C->component_lists()) {
Samuel Antaocf3f83e2016-07-28 14:47:35 +00006923 InfoGen(L.first, L.second, C->getMapType(), C->getMapTypeModifier(),
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006924 MapInfo::RPK_None, C->isImplicit());
6925 }
Paul Robinson15c84002016-07-29 20:46:16 +00006926 for (auto *C : this->CurDir.getClausesOfKind<OMPToClause>())
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006927 for (auto L : C->component_lists()) {
Samuel Antaocf3f83e2016-07-28 14:47:35 +00006928 InfoGen(L.first, L.second, OMPC_MAP_to, OMPC_MAP_unknown,
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006929 MapInfo::RPK_None, C->isImplicit());
6930 }
Paul Robinson15c84002016-07-29 20:46:16 +00006931 for (auto *C : this->CurDir.getClausesOfKind<OMPFromClause>())
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006932 for (auto L : C->component_lists()) {
Samuel Antaocf3f83e2016-07-28 14:47:35 +00006933 InfoGen(L.first, L.second, OMPC_MAP_from, OMPC_MAP_unknown,
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006934 MapInfo::RPK_None, C->isImplicit());
6935 }
Samuel Antao86ace552016-04-27 22:40:57 +00006936
Samuel Antaocc10b852016-07-28 14:23:26 +00006937 // Look at the use_device_ptr clause information and mark the existing map
6938 // entries as such. If there is no map information for an entry in the
6939 // use_device_ptr list, we create one with map type 'alloc' and zero size
6940 // section. It is the user fault if that was not mapped before.
Paul Robinson78fb1322016-08-01 22:12:46 +00006941 // FIXME: MSVC 2013 seems to require this-> to find member CurDir.
Paul Robinson15c84002016-07-29 20:46:16 +00006942 for (auto *C : this->CurDir.getClausesOfKind<OMPUseDevicePtrClause>())
Samuel Antaocc10b852016-07-28 14:23:26 +00006943 for (auto L : C->component_lists()) {
6944 assert(!L.second.empty() && "Not expecting empty list of components!");
6945 const ValueDecl *VD = L.second.back().getAssociatedDeclaration();
6946 VD = cast<ValueDecl>(VD->getCanonicalDecl());
6947 auto *IE = L.second.back().getAssociatedExpression();
6948 // If the first component is a member expression, we have to look into
6949 // 'this', which maps to null in the map of map information. Otherwise
6950 // look directly for the information.
6951 auto It = Info.find(isa<MemberExpr>(IE) ? nullptr : VD);
6952
6953 // We potentially have map information for this declaration already.
6954 // Look for the first set of components that refer to it.
6955 if (It != Info.end()) {
6956 auto CI = std::find_if(
6957 It->second.begin(), It->second.end(), [VD](const MapInfo &MI) {
6958 return MI.Components.back().getAssociatedDeclaration() == VD;
6959 });
6960 // If we found a map entry, signal that the pointer has to be returned
6961 // and move on to the next declaration.
6962 if (CI != It->second.end()) {
6963 CI->ReturnDevicePointer = isa<MemberExpr>(IE)
6964 ? (VD->getType()->isReferenceType()
6965 ? MapInfo::RPK_MemberReference
6966 : MapInfo::RPK_Member)
6967 : MapInfo::RPK_Base;
6968 continue;
6969 }
6970 }
6971
6972 // We didn't find any match in our map information - generate a zero
6973 // size array section.
Paul Robinson78fb1322016-08-01 22:12:46 +00006974 // FIXME: MSVC 2013 seems to require this-> to find member CGF.
Alexey Bataev1e491372018-01-23 18:44:14 +00006975 llvm::Value *Ptr = this->CGF.EmitLoadOfScalar(this->CGF.EmitLValue(IE),
6976 IE->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +00006977 BasePointers.push_back({Ptr, VD});
6978 Pointers.push_back(Ptr);
Paul Robinson15c84002016-07-29 20:46:16 +00006979 Sizes.push_back(llvm::Constant::getNullValue(this->CGF.SizeTy));
George Rokos065755d2017-11-07 18:27:04 +00006980 Types.push_back(OMP_MAP_RETURN_PARAM | OMP_MAP_TARGET_PARAM);
Samuel Antaocc10b852016-07-28 14:23:26 +00006981 }
6982
Samuel Antao86ace552016-04-27 22:40:57 +00006983 for (auto &M : Info) {
6984 // We need to know when we generate information for the first component
6985 // associated with a capture, because the mapping flags depend on it.
6986 bool IsFirstComponentList = true;
6987 for (MapInfo &L : M.second) {
6988 assert(!L.Components.empty() &&
6989 "Not expecting declaration with no component lists.");
Samuel Antaocc10b852016-07-28 14:23:26 +00006990
6991 // Remember the current base pointer index.
6992 unsigned CurrentBasePointersIdx = BasePointers.size();
Paul Robinson78fb1322016-08-01 22:12:46 +00006993 // FIXME: MSVC 2013 seems to require this-> to find the member method.
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006994 this->generateInfoForComponentList(
6995 L.MapType, L.MapTypeModifier, L.Components, BasePointers, Pointers,
6996 Sizes, Types, IsFirstComponentList, L.IsImplicit);
Samuel Antaocc10b852016-07-28 14:23:26 +00006997
6998 // If this entry relates with a device pointer, set the relevant
6999 // declaration and add the 'return pointer' flag.
7000 if (IsFirstComponentList &&
7001 L.ReturnDevicePointer != MapInfo::RPK_None) {
7002 // If the pointer is not the base of the map, we need to skip the
7003 // base. If it is a reference in a member field, we also need to skip
7004 // the map of the reference.
7005 if (L.ReturnDevicePointer != MapInfo::RPK_Base) {
7006 ++CurrentBasePointersIdx;
7007 if (L.ReturnDevicePointer == MapInfo::RPK_MemberReference)
7008 ++CurrentBasePointersIdx;
7009 }
7010 assert(BasePointers.size() > CurrentBasePointersIdx &&
7011 "Unexpected number of mapped base pointers.");
7012
7013 auto *RelevantVD = L.Components.back().getAssociatedDeclaration();
7014 assert(RelevantVD &&
7015 "No relevant declaration related with device pointer??");
7016
7017 BasePointers[CurrentBasePointersIdx].setDevicePtrDecl(RelevantVD);
George Rokos065755d2017-11-07 18:27:04 +00007018 Types[CurrentBasePointersIdx] |= OMP_MAP_RETURN_PARAM;
Samuel Antaocc10b852016-07-28 14:23:26 +00007019 }
Samuel Antao86ace552016-04-27 22:40:57 +00007020 IsFirstComponentList = false;
7021 }
7022 }
7023 }
7024
7025 /// \brief Generate the base pointers, section pointers, sizes and map types
7026 /// associated to a given capture.
7027 void generateInfoForCapture(const CapturedStmt::Capture *Cap,
Samuel Antao6890b092016-07-28 14:25:09 +00007028 llvm::Value *Arg,
Samuel Antaocc10b852016-07-28 14:23:26 +00007029 MapBaseValuesArrayTy &BasePointers,
Samuel Antao86ace552016-04-27 22:40:57 +00007030 MapValuesArrayTy &Pointers,
7031 MapValuesArrayTy &Sizes,
7032 MapFlagsArrayTy &Types) const {
7033 assert(!Cap->capturesVariableArrayType() &&
7034 "Not expecting to generate map info for a variable array type!");
7035
7036 BasePointers.clear();
7037 Pointers.clear();
7038 Sizes.clear();
7039 Types.clear();
7040
Samuel Antao6890b092016-07-28 14:25:09 +00007041 // We need to know when we generating information for the first component
7042 // associated with a capture, because the mapping flags depend on it.
7043 bool IsFirstComponentList = true;
7044
Samuel Antao86ace552016-04-27 22:40:57 +00007045 const ValueDecl *VD =
7046 Cap->capturesThis()
7047 ? nullptr
George Burgess IV00f70bd2018-03-01 05:43:23 +00007048 : Cap->getCapturedVar()->getCanonicalDecl();
Samuel Antao86ace552016-04-27 22:40:57 +00007049
Samuel Antao6890b092016-07-28 14:25:09 +00007050 // If this declaration appears in a is_device_ptr clause we just have to
7051 // pass the pointer by value. If it is a reference to a declaration, we just
7052 // pass its value, otherwise, if it is a member expression, we need to map
7053 // 'to' the field.
7054 if (!VD) {
7055 auto It = DevPointersMap.find(VD);
7056 if (It != DevPointersMap.end()) {
7057 for (auto L : It->second) {
7058 generateInfoForComponentList(
7059 /*MapType=*/OMPC_MAP_to, /*MapTypeModifier=*/OMPC_MAP_unknown, L,
Alexey Bataevf47c4b42017-09-26 13:47:31 +00007060 BasePointers, Pointers, Sizes, Types, IsFirstComponentList,
7061 /*IsImplicit=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +00007062 IsFirstComponentList = false;
7063 }
7064 return;
7065 }
7066 } else if (DevPointersMap.count(VD)) {
7067 BasePointers.push_back({Arg, VD});
7068 Pointers.push_back(Arg);
7069 Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy));
George Rokos065755d2017-11-07 18:27:04 +00007070 Types.push_back(OMP_MAP_LITERAL | OMP_MAP_TARGET_PARAM);
Samuel Antao6890b092016-07-28 14:25:09 +00007071 return;
7072 }
7073
Paul Robinson78fb1322016-08-01 22:12:46 +00007074 // FIXME: MSVC 2013 seems to require this-> to find member CurDir.
Paul Robinson15c84002016-07-29 20:46:16 +00007075 for (auto *C : this->CurDir.getClausesOfKind<OMPMapClause>())
Samuel Antao86ace552016-04-27 22:40:57 +00007076 for (auto L : C->decl_component_lists(VD)) {
7077 assert(L.first == VD &&
7078 "We got information for the wrong declaration??");
7079 assert(!L.second.empty() &&
7080 "Not expecting declaration with no component lists.");
Alexey Bataevf47c4b42017-09-26 13:47:31 +00007081 generateInfoForComponentList(
7082 C->getMapType(), C->getMapTypeModifier(), L.second, BasePointers,
7083 Pointers, Sizes, Types, IsFirstComponentList, C->isImplicit());
Samuel Antao86ace552016-04-27 22:40:57 +00007084 IsFirstComponentList = false;
7085 }
7086
7087 return;
7088 }
Samuel Antaod486f842016-05-26 16:53:38 +00007089
7090 /// \brief Generate the default map information for a given capture \a CI,
7091 /// record field declaration \a RI and captured value \a CV.
Samuel Antaocc10b852016-07-28 14:23:26 +00007092 void generateDefaultMapInfo(const CapturedStmt::Capture &CI,
7093 const FieldDecl &RI, llvm::Value *CV,
7094 MapBaseValuesArrayTy &CurBasePointers,
7095 MapValuesArrayTy &CurPointers,
7096 MapValuesArrayTy &CurSizes,
7097 MapFlagsArrayTy &CurMapTypes) {
Samuel Antaod486f842016-05-26 16:53:38 +00007098
7099 // Do the default mapping.
7100 if (CI.capturesThis()) {
7101 CurBasePointers.push_back(CV);
7102 CurPointers.push_back(CV);
7103 const PointerType *PtrTy = cast<PointerType>(RI.getType().getTypePtr());
7104 CurSizes.push_back(CGF.getTypeSize(PtrTy->getPointeeType()));
7105 // Default map type.
Samuel Antaocc10b852016-07-28 14:23:26 +00007106 CurMapTypes.push_back(OMP_MAP_TO | OMP_MAP_FROM);
Samuel Antaod486f842016-05-26 16:53:38 +00007107 } else if (CI.capturesVariableByCopy()) {
Samuel Antao6d004262016-06-16 18:39:34 +00007108 CurBasePointers.push_back(CV);
7109 CurPointers.push_back(CV);
Samuel Antaod486f842016-05-26 16:53:38 +00007110 if (!RI.getType()->isAnyPointerType()) {
Samuel Antao6d004262016-06-16 18:39:34 +00007111 // We have to signal to the runtime captures passed by value that are
7112 // not pointers.
George Rokos065755d2017-11-07 18:27:04 +00007113 CurMapTypes.push_back(OMP_MAP_LITERAL);
Samuel Antaod486f842016-05-26 16:53:38 +00007114 CurSizes.push_back(CGF.getTypeSize(RI.getType()));
7115 } else {
7116 // Pointers are implicitly mapped with a zero size and no flags
7117 // (other than first map that is added for all implicit maps).
7118 CurMapTypes.push_back(0u);
Samuel Antaod486f842016-05-26 16:53:38 +00007119 CurSizes.push_back(llvm::Constant::getNullValue(CGF.SizeTy));
7120 }
7121 } else {
7122 assert(CI.capturesVariable() && "Expected captured reference.");
7123 CurBasePointers.push_back(CV);
7124 CurPointers.push_back(CV);
7125
7126 const ReferenceType *PtrTy =
7127 cast<ReferenceType>(RI.getType().getTypePtr());
7128 QualType ElementType = PtrTy->getPointeeType();
7129 CurSizes.push_back(CGF.getTypeSize(ElementType));
7130 // The default map type for a scalar/complex type is 'to' because by
7131 // default the value doesn't have to be retrieved. For an aggregate
7132 // type, the default is 'tofrom'.
Alexey Bataev3f96fe62017-12-13 17:31:39 +00007133 CurMapTypes.emplace_back(adjustMapModifiersForPrivateClauses(
7134 CI, ElementType->isAggregateType() ? (OMP_MAP_TO | OMP_MAP_FROM)
7135 : OMP_MAP_TO));
Samuel Antaod486f842016-05-26 16:53:38 +00007136 }
George Rokos065755d2017-11-07 18:27:04 +00007137 // Every default map produces a single argument which is a target parameter.
7138 CurMapTypes.back() |= OMP_MAP_TARGET_PARAM;
Samuel Antaod486f842016-05-26 16:53:38 +00007139 }
Samuel Antao86ace552016-04-27 22:40:57 +00007140};
Samuel Antaodf158d52016-04-27 22:58:19 +00007141
7142enum OpenMPOffloadingReservedDeviceIDs {
7143 /// \brief Device ID if the device was not defined, runtime should get it
7144 /// from environment variables in the spec.
7145 OMP_DEVICEID_UNDEF = -1,
7146};
7147} // anonymous namespace
7148
7149/// \brief Emit the arrays used to pass the captures and map information to the
7150/// offloading runtime library. If there is no map or capture information,
7151/// return nullptr by reference.
7152static void
Samuel Antaocc10b852016-07-28 14:23:26 +00007153emitOffloadingArrays(CodeGenFunction &CGF,
7154 MappableExprsHandler::MapBaseValuesArrayTy &BasePointers,
Samuel Antaodf158d52016-04-27 22:58:19 +00007155 MappableExprsHandler::MapValuesArrayTy &Pointers,
7156 MappableExprsHandler::MapValuesArrayTy &Sizes,
Samuel Antaocc10b852016-07-28 14:23:26 +00007157 MappableExprsHandler::MapFlagsArrayTy &MapTypes,
7158 CGOpenMPRuntime::TargetDataInfo &Info) {
Samuel Antaodf158d52016-04-27 22:58:19 +00007159 auto &CGM = CGF.CGM;
7160 auto &Ctx = CGF.getContext();
7161
Samuel Antaocc10b852016-07-28 14:23:26 +00007162 // Reset the array information.
7163 Info.clearArrayInfo();
7164 Info.NumberOfPtrs = BasePointers.size();
Samuel Antaodf158d52016-04-27 22:58:19 +00007165
Samuel Antaocc10b852016-07-28 14:23:26 +00007166 if (Info.NumberOfPtrs) {
Samuel Antaodf158d52016-04-27 22:58:19 +00007167 // Detect if we have any capture size requiring runtime evaluation of the
7168 // size so that a constant array could be eventually used.
7169 bool hasRuntimeEvaluationCaptureSize = false;
7170 for (auto *S : Sizes)
7171 if (!isa<llvm::Constant>(S)) {
7172 hasRuntimeEvaluationCaptureSize = true;
7173 break;
7174 }
7175
Samuel Antaocc10b852016-07-28 14:23:26 +00007176 llvm::APInt PointerNumAP(32, Info.NumberOfPtrs, /*isSigned=*/true);
Samuel Antaodf158d52016-04-27 22:58:19 +00007177 QualType PointerArrayType =
7178 Ctx.getConstantArrayType(Ctx.VoidPtrTy, PointerNumAP, ArrayType::Normal,
7179 /*IndexTypeQuals=*/0);
7180
Samuel Antaocc10b852016-07-28 14:23:26 +00007181 Info.BasePointersArray =
Samuel Antaodf158d52016-04-27 22:58:19 +00007182 CGF.CreateMemTemp(PointerArrayType, ".offload_baseptrs").getPointer();
Samuel Antaocc10b852016-07-28 14:23:26 +00007183 Info.PointersArray =
Samuel Antaodf158d52016-04-27 22:58:19 +00007184 CGF.CreateMemTemp(PointerArrayType, ".offload_ptrs").getPointer();
7185
7186 // If we don't have any VLA types or other types that require runtime
7187 // evaluation, we can use a constant array for the map sizes, otherwise we
7188 // need to fill up the arrays as we do for the pointers.
7189 if (hasRuntimeEvaluationCaptureSize) {
7190 QualType SizeArrayType = Ctx.getConstantArrayType(
7191 Ctx.getSizeType(), PointerNumAP, ArrayType::Normal,
7192 /*IndexTypeQuals=*/0);
Samuel Antaocc10b852016-07-28 14:23:26 +00007193 Info.SizesArray =
Samuel Antaodf158d52016-04-27 22:58:19 +00007194 CGF.CreateMemTemp(SizeArrayType, ".offload_sizes").getPointer();
7195 } else {
7196 // We expect all the sizes to be constant, so we collect them to create
7197 // a constant array.
7198 SmallVector<llvm::Constant *, 16> ConstSizes;
7199 for (auto S : Sizes)
7200 ConstSizes.push_back(cast<llvm::Constant>(S));
7201
7202 auto *SizesArrayInit = llvm::ConstantArray::get(
7203 llvm::ArrayType::get(CGM.SizeTy, ConstSizes.size()), ConstSizes);
7204 auto *SizesArrayGbl = new llvm::GlobalVariable(
7205 CGM.getModule(), SizesArrayInit->getType(),
7206 /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage,
7207 SizesArrayInit, ".offload_sizes");
Peter Collingbournebcf909d2016-06-14 21:02:05 +00007208 SizesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Samuel Antaocc10b852016-07-28 14:23:26 +00007209 Info.SizesArray = SizesArrayGbl;
Samuel Antaodf158d52016-04-27 22:58:19 +00007210 }
7211
7212 // The map types are always constant so we don't need to generate code to
7213 // fill arrays. Instead, we create an array constant.
7214 llvm::Constant *MapTypesArrayInit =
7215 llvm::ConstantDataArray::get(CGF.Builder.getContext(), MapTypes);
7216 auto *MapTypesArrayGbl = new llvm::GlobalVariable(
7217 CGM.getModule(), MapTypesArrayInit->getType(),
7218 /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage,
7219 MapTypesArrayInit, ".offload_maptypes");
Peter Collingbournebcf909d2016-06-14 21:02:05 +00007220 MapTypesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Samuel Antaocc10b852016-07-28 14:23:26 +00007221 Info.MapTypesArray = MapTypesArrayGbl;
Samuel Antaodf158d52016-04-27 22:58:19 +00007222
Samuel Antaocc10b852016-07-28 14:23:26 +00007223 for (unsigned i = 0; i < Info.NumberOfPtrs; ++i) {
7224 llvm::Value *BPVal = *BasePointers[i];
Samuel Antaodf158d52016-04-27 22:58:19 +00007225 llvm::Value *BP = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00007226 llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
7227 Info.BasePointersArray, 0, i);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +00007228 BP = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
7229 BP, BPVal->getType()->getPointerTo(/*AddrSpace=*/0));
Samuel Antaodf158d52016-04-27 22:58:19 +00007230 Address BPAddr(BP, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy));
7231 CGF.Builder.CreateStore(BPVal, BPAddr);
7232
Samuel Antaocc10b852016-07-28 14:23:26 +00007233 if (Info.requiresDevicePointerInfo())
7234 if (auto *DevVD = BasePointers[i].getDevicePtrDecl())
7235 Info.CaptureDeviceAddrMap.insert(std::make_pair(DevVD, BPAddr));
7236
Samuel Antaodf158d52016-04-27 22:58:19 +00007237 llvm::Value *PVal = Pointers[i];
Samuel Antaodf158d52016-04-27 22:58:19 +00007238 llvm::Value *P = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00007239 llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
7240 Info.PointersArray, 0, i);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +00007241 P = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
7242 P, PVal->getType()->getPointerTo(/*AddrSpace=*/0));
Samuel Antaodf158d52016-04-27 22:58:19 +00007243 Address PAddr(P, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy));
7244 CGF.Builder.CreateStore(PVal, PAddr);
7245
7246 if (hasRuntimeEvaluationCaptureSize) {
7247 llvm::Value *S = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00007248 llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs),
7249 Info.SizesArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00007250 /*Idx0=*/0,
7251 /*Idx1=*/i);
7252 Address SAddr(S, Ctx.getTypeAlignInChars(Ctx.getSizeType()));
7253 CGF.Builder.CreateStore(
7254 CGF.Builder.CreateIntCast(Sizes[i], CGM.SizeTy, /*isSigned=*/true),
7255 SAddr);
7256 }
7257 }
7258 }
7259}
7260/// \brief Emit the arguments to be passed to the runtime library based on the
7261/// arrays of pointers, sizes and map types.
7262static void emitOffloadingArraysArgument(
7263 CodeGenFunction &CGF, llvm::Value *&BasePointersArrayArg,
7264 llvm::Value *&PointersArrayArg, llvm::Value *&SizesArrayArg,
Samuel Antaocc10b852016-07-28 14:23:26 +00007265 llvm::Value *&MapTypesArrayArg, CGOpenMPRuntime::TargetDataInfo &Info) {
Samuel Antaodf158d52016-04-27 22:58:19 +00007266 auto &CGM = CGF.CGM;
Samuel Antaocc10b852016-07-28 14:23:26 +00007267 if (Info.NumberOfPtrs) {
Samuel Antaodf158d52016-04-27 22:58:19 +00007268 BasePointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00007269 llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
7270 Info.BasePointersArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00007271 /*Idx0=*/0, /*Idx1=*/0);
7272 PointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00007273 llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
7274 Info.PointersArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00007275 /*Idx0=*/0,
7276 /*Idx1=*/0);
7277 SizesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00007278 llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), Info.SizesArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00007279 /*Idx0=*/0, /*Idx1=*/0);
7280 MapTypesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
George Rokos63bc9d62017-11-21 18:25:12 +00007281 llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs),
Samuel Antaocc10b852016-07-28 14:23:26 +00007282 Info.MapTypesArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00007283 /*Idx0=*/0,
7284 /*Idx1=*/0);
7285 } else {
7286 BasePointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
7287 PointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
7288 SizesArrayArg = llvm::ConstantPointerNull::get(CGM.SizeTy->getPointerTo());
7289 MapTypesArrayArg =
George Rokos63bc9d62017-11-21 18:25:12 +00007290 llvm::ConstantPointerNull::get(CGM.Int64Ty->getPointerTo());
Samuel Antaodf158d52016-04-27 22:58:19 +00007291 }
Samuel Antao86ace552016-04-27 22:40:57 +00007292}
7293
Samuel Antaobed3c462015-10-02 16:14:20 +00007294void CGOpenMPRuntime::emitTargetCall(CodeGenFunction &CGF,
7295 const OMPExecutableDirective &D,
7296 llvm::Value *OutlinedFn,
Samuel Antaoee8fb302016-01-06 13:42:12 +00007297 llvm::Value *OutlinedFnID,
Alexey Bataev8451efa2018-01-15 19:06:12 +00007298 const Expr *IfCond, const Expr *Device) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00007299 if (!CGF.HaveInsertPoint())
7300 return;
Samuel Antaobed3c462015-10-02 16:14:20 +00007301
Samuel Antaoee8fb302016-01-06 13:42:12 +00007302 assert(OutlinedFn && "Invalid outlined function!");
7303
Alexey Bataev8451efa2018-01-15 19:06:12 +00007304 const bool RequiresOuterTask = D.hasClausesOfKind<OMPDependClause>();
7305 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
Alexey Bataev475a7442018-01-12 19:39:11 +00007306 const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target);
Alexey Bataev8451efa2018-01-15 19:06:12 +00007307 auto &&ArgsCodegen = [&CS, &CapturedVars](CodeGenFunction &CGF,
7308 PrePostActionTy &) {
7309 CGF.GenerateOpenMPCapturedVars(CS, CapturedVars);
7310 };
7311 emitInlinedDirective(CGF, OMPD_unknown, ArgsCodegen);
Samuel Antao86ace552016-04-27 22:40:57 +00007312
Alexey Bataev8451efa2018-01-15 19:06:12 +00007313 CodeGenFunction::OMPTargetDataInfo InputInfo;
7314 llvm::Value *MapTypesArray = nullptr;
Samuel Antaobed3c462015-10-02 16:14:20 +00007315 // Fill up the pointer arrays and transfer execution to the device.
Alexey Bataev8451efa2018-01-15 19:06:12 +00007316 auto &&ThenGen = [this, Device, OutlinedFn, OutlinedFnID, &D, &InputInfo,
7317 &MapTypesArray, &CS, RequiresOuterTask,
7318 &CapturedVars](CodeGenFunction &CGF, PrePostActionTy &) {
Samuel Antaobed3c462015-10-02 16:14:20 +00007319 // On top of the arrays that were filled up, the target offloading call
7320 // takes as arguments the device id as well as the host pointer. The host
7321 // pointer is used by the runtime library to identify the current target
7322 // region, so it only has to be unique and not necessarily point to
7323 // anything. It could be the pointer to the outlined function that
7324 // implements the target region, but we aren't using that so that the
7325 // compiler doesn't need to keep that, and could therefore inline the host
7326 // function if proven worthwhile during optimization.
7327
Samuel Antaoee8fb302016-01-06 13:42:12 +00007328 // From this point on, we need to have an ID of the target region defined.
7329 assert(OutlinedFnID && "Invalid outlined function ID!");
Samuel Antaobed3c462015-10-02 16:14:20 +00007330
7331 // Emit device ID if any.
7332 llvm::Value *DeviceID;
George Rokos63bc9d62017-11-21 18:25:12 +00007333 if (Device) {
Samuel Antaobed3c462015-10-02 16:14:20 +00007334 DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
George Rokos63bc9d62017-11-21 18:25:12 +00007335 CGF.Int64Ty, /*isSigned=*/true);
7336 } else {
7337 DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF);
7338 }
Samuel Antaobed3c462015-10-02 16:14:20 +00007339
Samuel Antaodf158d52016-04-27 22:58:19 +00007340 // Emit the number of elements in the offloading arrays.
Alexey Bataev8451efa2018-01-15 19:06:12 +00007341 llvm::Value *PointerNum =
7342 CGF.Builder.getInt32(InputInfo.NumberOfTargetItems);
Samuel Antaodf158d52016-04-27 22:58:19 +00007343
Samuel Antaob68e2db2016-03-03 16:20:23 +00007344 // Return value of the runtime offloading call.
7345 llvm::Value *Return;
7346
Alexey Bataev8451efa2018-01-15 19:06:12 +00007347 auto *NumTeams = emitNumTeamsForTargetDirective(*this, CGF, D);
7348 auto *NumThreads = emitNumThreadsForTargetDirective(*this, CGF, D);
Samuel Antaob68e2db2016-03-03 16:20:23 +00007349
Alexey Bataeva9f77c62017-12-13 21:04:20 +00007350 bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>();
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007351 // The target region is an outlined function launched by the runtime
7352 // via calls __tgt_target() or __tgt_target_teams().
7353 //
7354 // __tgt_target() launches a target region with one team and one thread,
7355 // executing a serial region. This master thread may in turn launch
7356 // more threads within its team upon encountering a parallel region,
7357 // however, no additional teams can be launched on the device.
7358 //
7359 // __tgt_target_teams() launches a target region with one or more teams,
7360 // each with one or more threads. This call is required for target
7361 // constructs such as:
7362 // 'target teams'
7363 // 'target' / 'teams'
7364 // 'target teams distribute parallel for'
7365 // 'target parallel'
7366 // and so on.
7367 //
7368 // Note that on the host and CPU targets, the runtime implementation of
7369 // these calls simply call the outlined function without forking threads.
7370 // The outlined functions themselves have runtime calls to
7371 // __kmpc_fork_teams() and __kmpc_fork() for this purpose, codegen'd by
7372 // the compiler in emitTeamsCall() and emitParallelCall().
7373 //
7374 // In contrast, on the NVPTX target, the implementation of
7375 // __tgt_target_teams() launches a GPU kernel with the requested number
7376 // of teams and threads so no additional calls to the runtime are required.
Samuel Antaob68e2db2016-03-03 16:20:23 +00007377 if (NumTeams) {
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007378 // If we have NumTeams defined this means that we have an enclosed teams
7379 // region. Therefore we also expect to have NumThreads defined. These two
7380 // values should be defined in the presence of a teams directive,
7381 // regardless of having any clauses associated. If the user is using teams
7382 // but no clauses, these two values will be the default that should be
7383 // passed to the runtime library - a 32-bit integer with the value zero.
7384 assert(NumThreads && "Thread limit expression should be available along "
7385 "with number of teams.");
Alexey Bataev8451efa2018-01-15 19:06:12 +00007386 llvm::Value *OffloadingArgs[] = {DeviceID,
7387 OutlinedFnID,
7388 PointerNum,
7389 InputInfo.BasePointersArray.getPointer(),
7390 InputInfo.PointersArray.getPointer(),
7391 InputInfo.SizesArray.getPointer(),
7392 MapTypesArray,
7393 NumTeams,
7394 NumThreads};
Samuel Antaob68e2db2016-03-03 16:20:23 +00007395 Return = CGF.EmitRuntimeCall(
Alexey Bataev8451efa2018-01-15 19:06:12 +00007396 createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_teams_nowait
7397 : OMPRTL__tgt_target_teams),
Alexey Bataeva9f77c62017-12-13 21:04:20 +00007398 OffloadingArgs);
Samuel Antaob68e2db2016-03-03 16:20:23 +00007399 } else {
Alexey Bataev8451efa2018-01-15 19:06:12 +00007400 llvm::Value *OffloadingArgs[] = {DeviceID,
7401 OutlinedFnID,
7402 PointerNum,
7403 InputInfo.BasePointersArray.getPointer(),
7404 InputInfo.PointersArray.getPointer(),
7405 InputInfo.SizesArray.getPointer(),
7406 MapTypesArray};
Alexey Bataeva9f77c62017-12-13 21:04:20 +00007407 Return = CGF.EmitRuntimeCall(
Alexey Bataev8451efa2018-01-15 19:06:12 +00007408 createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_nowait
7409 : OMPRTL__tgt_target),
Alexey Bataeva9f77c62017-12-13 21:04:20 +00007410 OffloadingArgs);
Samuel Antaob68e2db2016-03-03 16:20:23 +00007411 }
Samuel Antaobed3c462015-10-02 16:14:20 +00007412
Alexey Bataev2a007e02017-10-02 14:20:58 +00007413 // Check the error code and execute the host version if required.
7414 llvm::BasicBlock *OffloadFailedBlock =
7415 CGF.createBasicBlock("omp_offload.failed");
7416 llvm::BasicBlock *OffloadContBlock =
7417 CGF.createBasicBlock("omp_offload.cont");
7418 llvm::Value *Failed = CGF.Builder.CreateIsNotNull(Return);
7419 CGF.Builder.CreateCondBr(Failed, OffloadFailedBlock, OffloadContBlock);
7420
7421 CGF.EmitBlock(OffloadFailedBlock);
Alexey Bataev8451efa2018-01-15 19:06:12 +00007422 if (RequiresOuterTask) {
7423 CapturedVars.clear();
7424 CGF.GenerateOpenMPCapturedVars(CS, CapturedVars);
7425 }
7426 emitOutlinedFunctionCall(CGF, D.getLocStart(), OutlinedFn, CapturedVars);
Alexey Bataev2a007e02017-10-02 14:20:58 +00007427 CGF.EmitBranch(OffloadContBlock);
7428
7429 CGF.EmitBlock(OffloadContBlock, /*IsFinished=*/true);
Samuel Antaobed3c462015-10-02 16:14:20 +00007430 };
7431
Samuel Antaoee8fb302016-01-06 13:42:12 +00007432 // Notify that the host version must be executed.
Alexey Bataev8451efa2018-01-15 19:06:12 +00007433 auto &&ElseGen = [this, &D, OutlinedFn, &CS, &CapturedVars,
7434 RequiresOuterTask](CodeGenFunction &CGF,
7435 PrePostActionTy &) {
7436 if (RequiresOuterTask) {
7437 CapturedVars.clear();
7438 CGF.GenerateOpenMPCapturedVars(CS, CapturedVars);
7439 }
7440 emitOutlinedFunctionCall(CGF, D.getLocStart(), OutlinedFn, CapturedVars);
7441 };
7442
7443 auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray,
7444 &CapturedVars, RequiresOuterTask,
7445 &CS](CodeGenFunction &CGF, PrePostActionTy &) {
7446 // Fill up the arrays with all the captured variables.
7447 MappableExprsHandler::MapBaseValuesArrayTy BasePointers;
7448 MappableExprsHandler::MapValuesArrayTy Pointers;
7449 MappableExprsHandler::MapValuesArrayTy Sizes;
7450 MappableExprsHandler::MapFlagsArrayTy MapTypes;
7451
7452 MappableExprsHandler::MapBaseValuesArrayTy CurBasePointers;
7453 MappableExprsHandler::MapValuesArrayTy CurPointers;
7454 MappableExprsHandler::MapValuesArrayTy CurSizes;
7455 MappableExprsHandler::MapFlagsArrayTy CurMapTypes;
7456
7457 // Get mappable expression information.
7458 MappableExprsHandler MEHandler(D, CGF);
7459
7460 auto RI = CS.getCapturedRecordDecl()->field_begin();
7461 auto CV = CapturedVars.begin();
7462 for (CapturedStmt::const_capture_iterator CI = CS.capture_begin(),
7463 CE = CS.capture_end();
7464 CI != CE; ++CI, ++RI, ++CV) {
7465 CurBasePointers.clear();
7466 CurPointers.clear();
7467 CurSizes.clear();
7468 CurMapTypes.clear();
7469
7470 // VLA sizes are passed to the outlined region by copy and do not have map
7471 // information associated.
7472 if (CI->capturesVariableArrayType()) {
7473 CurBasePointers.push_back(*CV);
7474 CurPointers.push_back(*CV);
7475 CurSizes.push_back(CGF.getTypeSize(RI->getType()));
7476 // Copy to the device as an argument. No need to retrieve it.
7477 CurMapTypes.push_back(MappableExprsHandler::OMP_MAP_LITERAL |
7478 MappableExprsHandler::OMP_MAP_TARGET_PARAM);
7479 } else {
7480 // If we have any information in the map clause, we use it, otherwise we
7481 // just do a default mapping.
7482 MEHandler.generateInfoForCapture(CI, *CV, CurBasePointers, CurPointers,
7483 CurSizes, CurMapTypes);
7484 if (CurBasePointers.empty())
7485 MEHandler.generateDefaultMapInfo(*CI, **RI, *CV, CurBasePointers,
7486 CurPointers, CurSizes, CurMapTypes);
7487 }
7488 // We expect to have at least an element of information for this capture.
7489 assert(!CurBasePointers.empty() &&
7490 "Non-existing map pointer for capture!");
7491 assert(CurBasePointers.size() == CurPointers.size() &&
7492 CurBasePointers.size() == CurSizes.size() &&
7493 CurBasePointers.size() == CurMapTypes.size() &&
7494 "Inconsistent map information sizes!");
7495
7496 // We need to append the results of this capture to what we already have.
7497 BasePointers.append(CurBasePointers.begin(), CurBasePointers.end());
7498 Pointers.append(CurPointers.begin(), CurPointers.end());
7499 Sizes.append(CurSizes.begin(), CurSizes.end());
7500 MapTypes.append(CurMapTypes.begin(), CurMapTypes.end());
7501 }
Alexey Bataev92327c52018-03-26 16:40:55 +00007502 // Map other list items in the map clause which are not captured variables
7503 // but "declare target link" global variables.
7504 for (const auto *C : D.getClausesOfKind<OMPMapClause>()) {
7505 for (auto L : C->component_lists()) {
7506 if (!L.first)
7507 continue;
7508 const auto *VD = dyn_cast<VarDecl>(L.first);
7509 if (!VD)
7510 continue;
7511 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
7512 isDeclareTargetDeclaration(VD);
7513 if (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link)
7514 continue;
7515 MEHandler.generateInfoForComponentList(
7516 C->getMapType(), C->getMapTypeModifier(), L.second, BasePointers,
7517 Pointers, Sizes, MapTypes, /*IsFirstComponentList=*/true,
7518 C->isImplicit());
7519 }
7520 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00007521
7522 TargetDataInfo Info;
7523 // Fill up the arrays and create the arguments.
7524 emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info);
7525 emitOffloadingArraysArgument(CGF, Info.BasePointersArray,
7526 Info.PointersArray, Info.SizesArray,
7527 Info.MapTypesArray, Info);
7528 InputInfo.NumberOfTargetItems = Info.NumberOfPtrs;
7529 InputInfo.BasePointersArray =
7530 Address(Info.BasePointersArray, CGM.getPointerAlign());
7531 InputInfo.PointersArray =
7532 Address(Info.PointersArray, CGM.getPointerAlign());
7533 InputInfo.SizesArray = Address(Info.SizesArray, CGM.getPointerAlign());
7534 MapTypesArray = Info.MapTypesArray;
7535 if (RequiresOuterTask)
7536 CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo);
7537 else
7538 emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen);
7539 };
7540
7541 auto &&TargetElseGen = [this, &ElseGen, &D, RequiresOuterTask](
7542 CodeGenFunction &CGF, PrePostActionTy &) {
7543 if (RequiresOuterTask) {
7544 CodeGenFunction::OMPTargetDataInfo InputInfo;
7545 CGF.EmitOMPTargetTaskBasedDirective(D, ElseGen, InputInfo);
7546 } else {
7547 emitInlinedDirective(CGF, D.getDirectiveKind(), ElseGen);
7548 }
Samuel Antaoee8fb302016-01-06 13:42:12 +00007549 };
7550
7551 // If we have a target function ID it means that we need to support
7552 // offloading, otherwise, just execute on the host. We need to execute on host
7553 // regardless of the conditional in the if clause if, e.g., the user do not
7554 // specify target triples.
7555 if (OutlinedFnID) {
Alexey Bataev8451efa2018-01-15 19:06:12 +00007556 if (IfCond) {
7557 emitOMPIfClause(CGF, IfCond, TargetThenGen, TargetElseGen);
7558 } else {
7559 RegionCodeGenTy ThenRCG(TargetThenGen);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00007560 ThenRCG(CGF);
Alexey Bataevf539faa2016-03-28 12:58:34 +00007561 }
7562 } else {
Alexey Bataev8451efa2018-01-15 19:06:12 +00007563 RegionCodeGenTy ElseRCG(TargetElseGen);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00007564 ElseRCG(CGF);
Alexey Bataevf539faa2016-03-28 12:58:34 +00007565 }
Samuel Antaobed3c462015-10-02 16:14:20 +00007566}
Samuel Antaoee8fb302016-01-06 13:42:12 +00007567
7568void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S,
7569 StringRef ParentName) {
7570 if (!S)
7571 return;
7572
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00007573 // Codegen OMP target directives that offload compute to the device.
7574 bool requiresDeviceCodegen =
7575 isa<OMPExecutableDirective>(S) &&
7576 isOpenMPTargetExecutionDirective(
7577 cast<OMPExecutableDirective>(S)->getDirectiveKind());
Samuel Antaoee8fb302016-01-06 13:42:12 +00007578
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00007579 if (requiresDeviceCodegen) {
7580 auto &E = *cast<OMPExecutableDirective>(S);
Samuel Antaoee8fb302016-01-06 13:42:12 +00007581 unsigned DeviceID;
7582 unsigned FileID;
7583 unsigned Line;
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00007584 getTargetEntryUniqueInfo(CGM.getContext(), E.getLocStart(), DeviceID,
Samuel Antao2de62b02016-02-13 23:35:10 +00007585 FileID, Line);
Samuel Antaoee8fb302016-01-06 13:42:12 +00007586
7587 // Is this a target region that should not be emitted as an entry point? If
7588 // so just signal we are done with this target region.
Samuel Antao2de62b02016-02-13 23:35:10 +00007589 if (!OffloadEntriesInfoManager.hasTargetRegionEntryInfo(DeviceID, FileID,
7590 ParentName, Line))
Samuel Antaoee8fb302016-01-06 13:42:12 +00007591 return;
7592
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00007593 switch (S->getStmtClass()) {
7594 case Stmt::OMPTargetDirectiveClass:
7595 CodeGenFunction::EmitOMPTargetDeviceFunction(
7596 CGM, ParentName, cast<OMPTargetDirective>(*S));
7597 break;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00007598 case Stmt::OMPTargetParallelDirectiveClass:
7599 CodeGenFunction::EmitOMPTargetParallelDeviceFunction(
7600 CGM, ParentName, cast<OMPTargetParallelDirective>(*S));
7601 break;
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00007602 case Stmt::OMPTargetTeamsDirectiveClass:
7603 CodeGenFunction::EmitOMPTargetTeamsDeviceFunction(
7604 CGM, ParentName, cast<OMPTargetTeamsDirective>(*S));
7605 break;
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007606 case Stmt::OMPTargetTeamsDistributeDirectiveClass:
7607 CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction(
7608 CGM, ParentName, cast<OMPTargetTeamsDistributeDirective>(*S));
7609 break;
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00007610 case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
7611 CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction(
7612 CGM, ParentName, cast<OMPTargetTeamsDistributeSimdDirective>(*S));
7613 break;
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007614 case Stmt::OMPTargetParallelForDirectiveClass:
7615 CodeGenFunction::EmitOMPTargetParallelForDeviceFunction(
7616 CGM, ParentName, cast<OMPTargetParallelForDirective>(*S));
7617 break;
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007618 case Stmt::OMPTargetParallelForSimdDirectiveClass:
7619 CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction(
7620 CGM, ParentName, cast<OMPTargetParallelForSimdDirective>(*S));
7621 break;
Alexey Bataevf8365372017-11-17 17:57:25 +00007622 case Stmt::OMPTargetSimdDirectiveClass:
7623 CodeGenFunction::EmitOMPTargetSimdDeviceFunction(
7624 CGM, ParentName, cast<OMPTargetSimdDirective>(*S));
7625 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00007626 case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
7627 CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction(
7628 CGM, ParentName,
7629 cast<OMPTargetTeamsDistributeParallelForDirective>(*S));
7630 break;
Alexey Bataev647dd842018-01-15 20:59:40 +00007631 case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
7632 CodeGenFunction::
7633 EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction(
7634 CGM, ParentName,
7635 cast<OMPTargetTeamsDistributeParallelForSimdDirective>(*S));
7636 break;
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00007637 default:
7638 llvm_unreachable("Unknown target directive for OpenMP device codegen.");
7639 }
Samuel Antaoee8fb302016-01-06 13:42:12 +00007640 return;
7641 }
7642
7643 if (const OMPExecutableDirective *E = dyn_cast<OMPExecutableDirective>(S)) {
Alexey Bataev475a7442018-01-12 19:39:11 +00007644 if (!E->hasAssociatedStmt() || !E->getAssociatedStmt())
Samuel Antaoee8fb302016-01-06 13:42:12 +00007645 return;
7646
7647 scanForTargetRegionsFunctions(
Alexey Bataev475a7442018-01-12 19:39:11 +00007648 E->getInnermostCapturedStmt()->getCapturedStmt(), ParentName);
Samuel Antaoee8fb302016-01-06 13:42:12 +00007649 return;
7650 }
7651
7652 // If this is a lambda function, look into its body.
7653 if (auto *L = dyn_cast<LambdaExpr>(S))
7654 S = L->getBody();
7655
7656 // Keep looking for target regions recursively.
7657 for (auto *II : S->children())
7658 scanForTargetRegionsFunctions(II, ParentName);
Samuel Antaoee8fb302016-01-06 13:42:12 +00007659}
7660
7661bool CGOpenMPRuntime::emitTargetFunctions(GlobalDecl GD) {
7662 auto &FD = *cast<FunctionDecl>(GD.getDecl());
7663
7664 // If emitting code for the host, we do not process FD here. Instead we do
7665 // the normal code generation.
7666 if (!CGM.getLangOpts().OpenMPIsDevice)
7667 return false;
7668
7669 // Try to detect target regions in the function.
7670 scanForTargetRegionsFunctions(FD.getBody(), CGM.getMangledName(GD));
7671
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00007672 // Do not to emit function if it is not marked as declare target.
Alexey Bataev92327c52018-03-26 16:40:55 +00007673 return !isDeclareTargetDeclaration(&FD);
Samuel Antaoee8fb302016-01-06 13:42:12 +00007674}
7675
7676bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) {
7677 if (!CGM.getLangOpts().OpenMPIsDevice)
7678 return false;
7679
7680 // Check if there are Ctors/Dtors in this declaration and look for target
7681 // regions in it. We use the complete variant to produce the kernel name
7682 // mangling.
7683 QualType RDTy = cast<VarDecl>(GD.getDecl())->getType();
7684 if (auto *RD = RDTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) {
7685 for (auto *Ctor : RD->ctors()) {
7686 StringRef ParentName =
7687 CGM.getMangledName(GlobalDecl(Ctor, Ctor_Complete));
7688 scanForTargetRegionsFunctions(Ctor->getBody(), ParentName);
7689 }
7690 auto *Dtor = RD->getDestructor();
7691 if (Dtor) {
7692 StringRef ParentName =
7693 CGM.getMangledName(GlobalDecl(Dtor, Dtor_Complete));
7694 scanForTargetRegionsFunctions(Dtor->getBody(), ParentName);
7695 }
7696 }
7697
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00007698 // Do not to emit variable if it is not marked as declare target.
Alexey Bataev92327c52018-03-26 16:40:55 +00007699 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev03f270c2018-03-30 18:31:07 +00007700 isDeclareTargetDeclaration(cast<VarDecl>(GD.getDecl()));
Alexey Bataev92327c52018-03-26 16:40:55 +00007701 return !Res || *Res == OMPDeclareTargetDeclAttr::MT_Link;
Samuel Antaoee8fb302016-01-06 13:42:12 +00007702}
7703
Alexey Bataev03f270c2018-03-30 18:31:07 +00007704void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD,
7705 llvm::Constant *Addr) {
7706 if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
7707 isDeclareTargetDeclaration(VD)) {
7708 OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags;
7709 StringRef VarName;
7710 CharUnits VarSize;
7711 llvm::GlobalValue::LinkageTypes Linkage;
7712 switch (*Res) {
7713 case OMPDeclareTargetDeclAttr::MT_To:
7714 Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo;
7715 VarName = CGM.getMangledName(VD);
7716 VarSize = CGM.getContext().getTypeSizeInChars(VD->getType());
7717 Linkage = CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false);
7718 break;
7719 case OMPDeclareTargetDeclAttr::MT_Link:
7720 // Map type 'to' because we do not map the original variable but the
7721 // reference.
7722 Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo;
7723 if (!CGM.getLangOpts().OpenMPIsDevice) {
7724 Addr =
7725 cast<llvm::Constant>(getAddrOfDeclareTargetLink(VD).getPointer());
7726 }
7727 VarName = Addr->getName();
7728 VarSize = CGM.getPointerSize();
7729 Linkage = llvm::GlobalValue::WeakAnyLinkage;
7730 break;
7731 }
7732 OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo(
7733 VarName, Addr, VarSize, Flags, Linkage);
7734 }
7735}
7736
Samuel Antaoee8fb302016-01-06 13:42:12 +00007737bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) {
7738 auto *VD = GD.getDecl();
7739 if (isa<FunctionDecl>(VD))
7740 return emitTargetFunctions(GD);
7741
7742 return emitTargetGlobalVariable(GD);
7743}
7744
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00007745CGOpenMPRuntime::DisableAutoDeclareTargetRAII::DisableAutoDeclareTargetRAII(
7746 CodeGenModule &CGM)
7747 : CGM(CGM) {
7748 if (CGM.getLangOpts().OpenMPIsDevice) {
7749 SavedShouldMarkAsGlobal = CGM.getOpenMPRuntime().ShouldMarkAsGlobal;
7750 CGM.getOpenMPRuntime().ShouldMarkAsGlobal = false;
7751 }
7752}
7753
7754CGOpenMPRuntime::DisableAutoDeclareTargetRAII::~DisableAutoDeclareTargetRAII() {
7755 if (CGM.getLangOpts().OpenMPIsDevice)
7756 CGM.getOpenMPRuntime().ShouldMarkAsGlobal = SavedShouldMarkAsGlobal;
7757}
7758
7759bool CGOpenMPRuntime::markAsGlobalTarget(const FunctionDecl *D) {
7760 if (!CGM.getLangOpts().OpenMPIsDevice || !ShouldMarkAsGlobal)
7761 return true;
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00007762
7763 const FunctionDecl *FD = D->getCanonicalDecl();
Alexey Bataev34f8a702018-03-28 14:28:54 +00007764 // Do not to emit function if it is marked as declare target as it was already
7765 // emitted.
7766 if (isDeclareTargetDeclaration(D)) {
7767 if (D->hasBody() && AlreadyEmittedTargetFunctions.count(FD) == 0) {
7768 if (auto *F = dyn_cast_or_null<llvm::Function>(
7769 CGM.GetGlobalValue(CGM.getMangledName(D))))
7770 return !F->isDeclaration();
7771 return false;
7772 }
7773 return true;
7774 }
7775
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00007776 // Do not mark member functions except for static.
7777 if (const auto *Method = dyn_cast<CXXMethodDecl>(FD))
7778 if (!Method->isStatic())
7779 return true;
7780
7781 return !AlreadyEmittedTargetFunctions.insert(FD).second;
7782}
7783
Samuel Antaoee8fb302016-01-06 13:42:12 +00007784llvm::Function *CGOpenMPRuntime::emitRegistrationFunction() {
7785 // If we have offloading in the current module, we need to emit the entries
7786 // now and register the offloading descriptor.
7787 createOffloadEntriesAndInfoMetadata();
7788
7789 // Create and register the offloading binary descriptors. This is the main
7790 // entity that captures all the information about offloading in the current
7791 // compilation unit.
7792 return createOffloadingBinaryDescriptorRegistration();
7793}
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00007794
7795void CGOpenMPRuntime::emitTeamsCall(CodeGenFunction &CGF,
7796 const OMPExecutableDirective &D,
7797 SourceLocation Loc,
7798 llvm::Value *OutlinedFn,
7799 ArrayRef<llvm::Value *> CapturedVars) {
7800 if (!CGF.HaveInsertPoint())
7801 return;
7802
7803 auto *RTLoc = emitUpdateLocation(CGF, Loc);
7804 CodeGenFunction::RunCleanupsScope Scope(CGF);
7805
7806 // Build call __kmpc_fork_teams(loc, n, microtask, var1, .., varn);
7807 llvm::Value *Args[] = {
7808 RTLoc,
7809 CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars
7810 CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy())};
7811 llvm::SmallVector<llvm::Value *, 16> RealArgs;
7812 RealArgs.append(std::begin(Args), std::end(Args));
7813 RealArgs.append(CapturedVars.begin(), CapturedVars.end());
7814
7815 auto RTLFn = createRuntimeFunction(OMPRTL__kmpc_fork_teams);
7816 CGF.EmitRuntimeCall(RTLFn, RealArgs);
7817}
7818
7819void CGOpenMPRuntime::emitNumTeamsClause(CodeGenFunction &CGF,
Carlo Bertollic6872252016-04-04 15:55:02 +00007820 const Expr *NumTeams,
7821 const Expr *ThreadLimit,
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00007822 SourceLocation Loc) {
7823 if (!CGF.HaveInsertPoint())
7824 return;
7825
7826 auto *RTLoc = emitUpdateLocation(CGF, Loc);
7827
Carlo Bertollic6872252016-04-04 15:55:02 +00007828 llvm::Value *NumTeamsVal =
7829 (NumTeams)
7830 ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(NumTeams),
7831 CGF.CGM.Int32Ty, /* isSigned = */ true)
7832 : CGF.Builder.getInt32(0);
7833
7834 llvm::Value *ThreadLimitVal =
7835 (ThreadLimit)
7836 ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(ThreadLimit),
7837 CGF.CGM.Int32Ty, /* isSigned = */ true)
7838 : CGF.Builder.getInt32(0);
7839
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00007840 // Build call __kmpc_push_num_teamss(&loc, global_tid, num_teams, thread_limit)
Carlo Bertollic6872252016-04-04 15:55:02 +00007841 llvm::Value *PushNumTeamsArgs[] = {RTLoc, getThreadID(CGF, Loc), NumTeamsVal,
7842 ThreadLimitVal};
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00007843 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_teams),
7844 PushNumTeamsArgs);
7845}
Samuel Antaodf158d52016-04-27 22:58:19 +00007846
Samuel Antaocc10b852016-07-28 14:23:26 +00007847void CGOpenMPRuntime::emitTargetDataCalls(
7848 CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
7849 const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) {
Samuel Antaodf158d52016-04-27 22:58:19 +00007850 if (!CGF.HaveInsertPoint())
7851 return;
7852
Samuel Antaocc10b852016-07-28 14:23:26 +00007853 // Action used to replace the default codegen action and turn privatization
7854 // off.
7855 PrePostActionTy NoPrivAction;
Samuel Antaodf158d52016-04-27 22:58:19 +00007856
7857 // Generate the code for the opening of the data environment. Capture all the
7858 // arguments of the runtime call by reference because they are used in the
7859 // closing of the region.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00007860 auto &&BeginThenGen = [this, &D, Device, &Info,
7861 &CodeGen](CodeGenFunction &CGF, PrePostActionTy &) {
Samuel Antaodf158d52016-04-27 22:58:19 +00007862 // Fill up the arrays with all the mapped variables.
Samuel Antaocc10b852016-07-28 14:23:26 +00007863 MappableExprsHandler::MapBaseValuesArrayTy BasePointers;
Samuel Antaodf158d52016-04-27 22:58:19 +00007864 MappableExprsHandler::MapValuesArrayTy Pointers;
7865 MappableExprsHandler::MapValuesArrayTy Sizes;
7866 MappableExprsHandler::MapFlagsArrayTy MapTypes;
7867
7868 // Get map clause information.
7869 MappableExprsHandler MCHandler(D, CGF);
7870 MCHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes);
Samuel Antaodf158d52016-04-27 22:58:19 +00007871
7872 // Fill up the arrays and create the arguments.
Samuel Antaocc10b852016-07-28 14:23:26 +00007873 emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info);
Samuel Antaodf158d52016-04-27 22:58:19 +00007874
7875 llvm::Value *BasePointersArrayArg = nullptr;
7876 llvm::Value *PointersArrayArg = nullptr;
7877 llvm::Value *SizesArrayArg = nullptr;
7878 llvm::Value *MapTypesArrayArg = nullptr;
7879 emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg,
Samuel Antaocc10b852016-07-28 14:23:26 +00007880 SizesArrayArg, MapTypesArrayArg, Info);
Samuel Antaodf158d52016-04-27 22:58:19 +00007881
7882 // Emit device ID if any.
7883 llvm::Value *DeviceID = nullptr;
George Rokos63bc9d62017-11-21 18:25:12 +00007884 if (Device) {
Samuel Antaodf158d52016-04-27 22:58:19 +00007885 DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
George Rokos63bc9d62017-11-21 18:25:12 +00007886 CGF.Int64Ty, /*isSigned=*/true);
7887 } else {
7888 DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF);
7889 }
Samuel Antaodf158d52016-04-27 22:58:19 +00007890
7891 // Emit the number of elements in the offloading arrays.
Samuel Antaocc10b852016-07-28 14:23:26 +00007892 auto *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs);
Samuel Antaodf158d52016-04-27 22:58:19 +00007893
7894 llvm::Value *OffloadingArgs[] = {
7895 DeviceID, PointerNum, BasePointersArrayArg,
7896 PointersArrayArg, SizesArrayArg, MapTypesArrayArg};
Alexey Bataevd2202ca2017-12-27 17:58:32 +00007897 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_begin),
Samuel Antaodf158d52016-04-27 22:58:19 +00007898 OffloadingArgs);
Samuel Antaocc10b852016-07-28 14:23:26 +00007899
7900 // If device pointer privatization is required, emit the body of the region
7901 // here. It will have to be duplicated: with and without privatization.
7902 if (!Info.CaptureDeviceAddrMap.empty())
7903 CodeGen(CGF);
Samuel Antaodf158d52016-04-27 22:58:19 +00007904 };
7905
7906 // Generate code for the closing of the data region.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00007907 auto &&EndThenGen = [this, Device, &Info](CodeGenFunction &CGF,
7908 PrePostActionTy &) {
Samuel Antaocc10b852016-07-28 14:23:26 +00007909 assert(Info.isValid() && "Invalid data environment closing arguments.");
Samuel Antaodf158d52016-04-27 22:58:19 +00007910
7911 llvm::Value *BasePointersArrayArg = nullptr;
7912 llvm::Value *PointersArrayArg = nullptr;
7913 llvm::Value *SizesArrayArg = nullptr;
7914 llvm::Value *MapTypesArrayArg = nullptr;
7915 emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg,
Samuel Antaocc10b852016-07-28 14:23:26 +00007916 SizesArrayArg, MapTypesArrayArg, Info);
Samuel Antaodf158d52016-04-27 22:58:19 +00007917
7918 // Emit device ID if any.
7919 llvm::Value *DeviceID = nullptr;
George Rokos63bc9d62017-11-21 18:25:12 +00007920 if (Device) {
Samuel Antaodf158d52016-04-27 22:58:19 +00007921 DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
George Rokos63bc9d62017-11-21 18:25:12 +00007922 CGF.Int64Ty, /*isSigned=*/true);
7923 } else {
7924 DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF);
7925 }
Samuel Antaodf158d52016-04-27 22:58:19 +00007926
7927 // Emit the number of elements in the offloading arrays.
Samuel Antaocc10b852016-07-28 14:23:26 +00007928 auto *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs);
Samuel Antaodf158d52016-04-27 22:58:19 +00007929
7930 llvm::Value *OffloadingArgs[] = {
7931 DeviceID, PointerNum, BasePointersArrayArg,
7932 PointersArrayArg, SizesArrayArg, MapTypesArrayArg};
Alexey Bataevd2202ca2017-12-27 17:58:32 +00007933 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_end),
Samuel Antaodf158d52016-04-27 22:58:19 +00007934 OffloadingArgs);
7935 };
7936
Samuel Antaocc10b852016-07-28 14:23:26 +00007937 // If we need device pointer privatization, we need to emit the body of the
7938 // region with no privatization in the 'else' branch of the conditional.
7939 // Otherwise, we don't have to do anything.
7940 auto &&BeginElseGen = [&Info, &CodeGen, &NoPrivAction](CodeGenFunction &CGF,
7941 PrePostActionTy &) {
7942 if (!Info.CaptureDeviceAddrMap.empty()) {
7943 CodeGen.setAction(NoPrivAction);
7944 CodeGen(CGF);
7945 }
7946 };
7947
7948 // We don't have to do anything to close the region if the if clause evaluates
7949 // to false.
7950 auto &&EndElseGen = [](CodeGenFunction &CGF, PrePostActionTy &) {};
Samuel Antaodf158d52016-04-27 22:58:19 +00007951
7952 if (IfCond) {
Samuel Antaocc10b852016-07-28 14:23:26 +00007953 emitOMPIfClause(CGF, IfCond, BeginThenGen, BeginElseGen);
Samuel Antaodf158d52016-04-27 22:58:19 +00007954 } else {
Samuel Antaocc10b852016-07-28 14:23:26 +00007955 RegionCodeGenTy RCG(BeginThenGen);
7956 RCG(CGF);
Samuel Antaodf158d52016-04-27 22:58:19 +00007957 }
7958
Samuel Antaocc10b852016-07-28 14:23:26 +00007959 // If we don't require privatization of device pointers, we emit the body in
7960 // between the runtime calls. This avoids duplicating the body code.
7961 if (Info.CaptureDeviceAddrMap.empty()) {
7962 CodeGen.setAction(NoPrivAction);
7963 CodeGen(CGF);
7964 }
Samuel Antaodf158d52016-04-27 22:58:19 +00007965
7966 if (IfCond) {
Samuel Antaocc10b852016-07-28 14:23:26 +00007967 emitOMPIfClause(CGF, IfCond, EndThenGen, EndElseGen);
Samuel Antaodf158d52016-04-27 22:58:19 +00007968 } else {
Samuel Antaocc10b852016-07-28 14:23:26 +00007969 RegionCodeGenTy RCG(EndThenGen);
7970 RCG(CGF);
Samuel Antaodf158d52016-04-27 22:58:19 +00007971 }
7972}
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00007973
Samuel Antao8d2d7302016-05-26 18:30:22 +00007974void CGOpenMPRuntime::emitTargetDataStandAloneCall(
Samuel Antao8dd66282016-04-27 23:14:30 +00007975 CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
7976 const Expr *Device) {
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00007977 if (!CGF.HaveInsertPoint())
7978 return;
7979
Samuel Antao8dd66282016-04-27 23:14:30 +00007980 assert((isa<OMPTargetEnterDataDirective>(D) ||
Samuel Antao8d2d7302016-05-26 18:30:22 +00007981 isa<OMPTargetExitDataDirective>(D) ||
7982 isa<OMPTargetUpdateDirective>(D)) &&
7983 "Expecting either target enter, exit data, or update directives.");
Samuel Antao8dd66282016-04-27 23:14:30 +00007984
Alexey Bataevd2202ca2017-12-27 17:58:32 +00007985 CodeGenFunction::OMPTargetDataInfo InputInfo;
7986 llvm::Value *MapTypesArray = nullptr;
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00007987 // Generate the code for the opening of the data environment.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00007988 auto &&ThenGen = [this, &D, Device, &InputInfo,
7989 &MapTypesArray](CodeGenFunction &CGF, PrePostActionTy &) {
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00007990 // Emit device ID if any.
7991 llvm::Value *DeviceID = nullptr;
George Rokos63bc9d62017-11-21 18:25:12 +00007992 if (Device) {
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00007993 DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
George Rokos63bc9d62017-11-21 18:25:12 +00007994 CGF.Int64Ty, /*isSigned=*/true);
7995 } else {
7996 DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF);
7997 }
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00007998
7999 // Emit the number of elements in the offloading arrays.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008000 llvm::Constant *PointerNum =
8001 CGF.Builder.getInt32(InputInfo.NumberOfTargetItems);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00008002
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008003 llvm::Value *OffloadingArgs[] = {DeviceID,
8004 PointerNum,
8005 InputInfo.BasePointersArray.getPointer(),
8006 InputInfo.PointersArray.getPointer(),
8007 InputInfo.SizesArray.getPointer(),
8008 MapTypesArray};
Samuel Antao8d2d7302016-05-26 18:30:22 +00008009
Samuel Antao8d2d7302016-05-26 18:30:22 +00008010 // Select the right runtime function call for each expected standalone
8011 // directive.
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +00008012 const bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>();
Samuel Antao8d2d7302016-05-26 18:30:22 +00008013 OpenMPRTLFunction RTLFn;
8014 switch (D.getDirectiveKind()) {
8015 default:
8016 llvm_unreachable("Unexpected standalone target data directive.");
8017 break;
8018 case OMPD_target_enter_data:
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +00008019 RTLFn = HasNowait ? OMPRTL__tgt_target_data_begin_nowait
8020 : OMPRTL__tgt_target_data_begin;
Samuel Antao8d2d7302016-05-26 18:30:22 +00008021 break;
8022 case OMPD_target_exit_data:
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +00008023 RTLFn = HasNowait ? OMPRTL__tgt_target_data_end_nowait
8024 : OMPRTL__tgt_target_data_end;
Samuel Antao8d2d7302016-05-26 18:30:22 +00008025 break;
8026 case OMPD_target_update:
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +00008027 RTLFn = HasNowait ? OMPRTL__tgt_target_data_update_nowait
8028 : OMPRTL__tgt_target_data_update;
Samuel Antao8d2d7302016-05-26 18:30:22 +00008029 break;
8030 }
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008031 CGF.EmitRuntimeCall(createRuntimeFunction(RTLFn), OffloadingArgs);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00008032 };
8033
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008034 auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray](
8035 CodeGenFunction &CGF, PrePostActionTy &) {
8036 // Fill up the arrays with all the mapped variables.
8037 MappableExprsHandler::MapBaseValuesArrayTy BasePointers;
8038 MappableExprsHandler::MapValuesArrayTy Pointers;
8039 MappableExprsHandler::MapValuesArrayTy Sizes;
8040 MappableExprsHandler::MapFlagsArrayTy MapTypes;
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00008041
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008042 // Get map clause information.
8043 MappableExprsHandler MEHandler(D, CGF);
8044 MEHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes);
8045
8046 TargetDataInfo Info;
8047 // Fill up the arrays and create the arguments.
8048 emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info);
8049 emitOffloadingArraysArgument(CGF, Info.BasePointersArray,
8050 Info.PointersArray, Info.SizesArray,
8051 Info.MapTypesArray, Info);
8052 InputInfo.NumberOfTargetItems = Info.NumberOfPtrs;
8053 InputInfo.BasePointersArray =
8054 Address(Info.BasePointersArray, CGM.getPointerAlign());
8055 InputInfo.PointersArray =
8056 Address(Info.PointersArray, CGM.getPointerAlign());
8057 InputInfo.SizesArray =
8058 Address(Info.SizesArray, CGM.getPointerAlign());
8059 MapTypesArray = Info.MapTypesArray;
8060 if (D.hasClausesOfKind<OMPDependClause>())
8061 CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo);
8062 else
Alexey Bataev768f1f22018-01-09 19:59:25 +00008063 emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen);
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008064 };
8065
8066 if (IfCond)
8067 emitOMPIfClause(CGF, IfCond, TargetThenGen,
8068 [](CodeGenFunction &CGF, PrePostActionTy &) {});
8069 else {
8070 RegionCodeGenTy ThenRCG(TargetThenGen);
8071 ThenRCG(CGF);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00008072 }
8073}
Alexey Bataevc7a82b42016-05-06 09:40:08 +00008074
8075namespace {
8076 /// Kind of parameter in a function with 'declare simd' directive.
8077 enum ParamKindTy { LinearWithVarStride, Linear, Uniform, Vector };
8078 /// Attribute set of the parameter.
8079 struct ParamAttrTy {
8080 ParamKindTy Kind = Vector;
8081 llvm::APSInt StrideOrArg;
8082 llvm::APSInt Alignment;
8083 };
8084} // namespace
8085
8086static unsigned evaluateCDTSize(const FunctionDecl *FD,
8087 ArrayRef<ParamAttrTy> ParamAttrs) {
8088 // Every vector variant of a SIMD-enabled function has a vector length (VLEN).
8089 // If OpenMP clause "simdlen" is used, the VLEN is the value of the argument
8090 // of that clause. The VLEN value must be power of 2.
8091 // In other case the notion of the function`s "characteristic data type" (CDT)
8092 // is used to compute the vector length.
8093 // CDT is defined in the following order:
8094 // a) For non-void function, the CDT is the return type.
8095 // b) If the function has any non-uniform, non-linear parameters, then the
8096 // CDT is the type of the first such parameter.
8097 // c) If the CDT determined by a) or b) above is struct, union, or class
8098 // type which is pass-by-value (except for the type that maps to the
8099 // built-in complex data type), the characteristic data type is int.
8100 // d) If none of the above three cases is applicable, the CDT is int.
8101 // The VLEN is then determined based on the CDT and the size of vector
8102 // register of that ISA for which current vector version is generated. The
8103 // VLEN is computed using the formula below:
8104 // VLEN = sizeof(vector_register) / sizeof(CDT),
8105 // where vector register size specified in section 3.2.1 Registers and the
8106 // Stack Frame of original AMD64 ABI document.
8107 QualType RetType = FD->getReturnType();
8108 if (RetType.isNull())
8109 return 0;
8110 ASTContext &C = FD->getASTContext();
8111 QualType CDT;
8112 if (!RetType.isNull() && !RetType->isVoidType())
8113 CDT = RetType;
8114 else {
8115 unsigned Offset = 0;
8116 if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
8117 if (ParamAttrs[Offset].Kind == Vector)
8118 CDT = C.getPointerType(C.getRecordType(MD->getParent()));
8119 ++Offset;
8120 }
8121 if (CDT.isNull()) {
8122 for (unsigned I = 0, E = FD->getNumParams(); I < E; ++I) {
8123 if (ParamAttrs[I + Offset].Kind == Vector) {
8124 CDT = FD->getParamDecl(I)->getType();
8125 break;
8126 }
8127 }
8128 }
8129 }
8130 if (CDT.isNull())
8131 CDT = C.IntTy;
8132 CDT = CDT->getCanonicalTypeUnqualified();
8133 if (CDT->isRecordType() || CDT->isUnionType())
8134 CDT = C.IntTy;
8135 return C.getTypeSize(CDT);
8136}
8137
8138static void
8139emitX86DeclareSimdFunction(const FunctionDecl *FD, llvm::Function *Fn,
Benjamin Kramer81cb4b72016-11-24 16:01:20 +00008140 const llvm::APSInt &VLENVal,
Alexey Bataevc7a82b42016-05-06 09:40:08 +00008141 ArrayRef<ParamAttrTy> ParamAttrs,
8142 OMPDeclareSimdDeclAttr::BranchStateTy State) {
8143 struct ISADataTy {
8144 char ISA;
8145 unsigned VecRegSize;
8146 };
8147 ISADataTy ISAData[] = {
8148 {
8149 'b', 128
8150 }, // SSE
8151 {
8152 'c', 256
8153 }, // AVX
8154 {
8155 'd', 256
8156 }, // AVX2
8157 {
8158 'e', 512
8159 }, // AVX512
8160 };
8161 llvm::SmallVector<char, 2> Masked;
8162 switch (State) {
8163 case OMPDeclareSimdDeclAttr::BS_Undefined:
8164 Masked.push_back('N');
8165 Masked.push_back('M');
8166 break;
8167 case OMPDeclareSimdDeclAttr::BS_Notinbranch:
8168 Masked.push_back('N');
8169 break;
8170 case OMPDeclareSimdDeclAttr::BS_Inbranch:
8171 Masked.push_back('M');
8172 break;
8173 }
8174 for (auto Mask : Masked) {
8175 for (auto &Data : ISAData) {
8176 SmallString<256> Buffer;
8177 llvm::raw_svector_ostream Out(Buffer);
8178 Out << "_ZGV" << Data.ISA << Mask;
8179 if (!VLENVal) {
8180 Out << llvm::APSInt::getUnsigned(Data.VecRegSize /
8181 evaluateCDTSize(FD, ParamAttrs));
8182 } else
8183 Out << VLENVal;
8184 for (auto &ParamAttr : ParamAttrs) {
8185 switch (ParamAttr.Kind){
8186 case LinearWithVarStride:
8187 Out << 's' << ParamAttr.StrideOrArg;
8188 break;
8189 case Linear:
8190 Out << 'l';
8191 if (!!ParamAttr.StrideOrArg)
8192 Out << ParamAttr.StrideOrArg;
8193 break;
8194 case Uniform:
8195 Out << 'u';
8196 break;
8197 case Vector:
8198 Out << 'v';
8199 break;
8200 }
8201 if (!!ParamAttr.Alignment)
8202 Out << 'a' << ParamAttr.Alignment;
8203 }
8204 Out << '_' << Fn->getName();
8205 Fn->addFnAttr(Out.str());
8206 }
8207 }
8208}
8209
8210void CGOpenMPRuntime::emitDeclareSimdFunction(const FunctionDecl *FD,
8211 llvm::Function *Fn) {
8212 ASTContext &C = CGM.getContext();
Alexey Bataev20cf67c2018-03-02 18:07:00 +00008213 FD = FD->getMostRecentDecl();
Alexey Bataevc7a82b42016-05-06 09:40:08 +00008214 // Map params to their positions in function decl.
8215 llvm::DenseMap<const Decl *, unsigned> ParamPositions;
8216 if (isa<CXXMethodDecl>(FD))
8217 ParamPositions.insert({FD, 0});
8218 unsigned ParamPos = ParamPositions.size();
David Majnemer59f77922016-06-24 04:05:48 +00008219 for (auto *P : FD->parameters()) {
Alexey Bataevc7a82b42016-05-06 09:40:08 +00008220 ParamPositions.insert({P->getCanonicalDecl(), ParamPos});
8221 ++ParamPos;
8222 }
Alexey Bataev20cf67c2018-03-02 18:07:00 +00008223 while (FD) {
8224 for (auto *Attr : FD->specific_attrs<OMPDeclareSimdDeclAttr>()) {
8225 llvm::SmallVector<ParamAttrTy, 8> ParamAttrs(ParamPositions.size());
8226 // Mark uniform parameters.
8227 for (auto *E : Attr->uniforms()) {
8228 E = E->IgnoreParenImpCasts();
8229 unsigned Pos;
8230 if (isa<CXXThisExpr>(E))
8231 Pos = ParamPositions[FD];
8232 else {
8233 auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl())
8234 ->getCanonicalDecl();
8235 Pos = ParamPositions[PVD];
8236 }
8237 ParamAttrs[Pos].Kind = Uniform;
Alexey Bataevc7a82b42016-05-06 09:40:08 +00008238 }
Alexey Bataev20cf67c2018-03-02 18:07:00 +00008239 // Get alignment info.
8240 auto NI = Attr->alignments_begin();
8241 for (auto *E : Attr->aligneds()) {
8242 E = E->IgnoreParenImpCasts();
8243 unsigned Pos;
8244 QualType ParmTy;
8245 if (isa<CXXThisExpr>(E)) {
8246 Pos = ParamPositions[FD];
8247 ParmTy = E->getType();
8248 } else {
8249 auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl())
8250 ->getCanonicalDecl();
8251 Pos = ParamPositions[PVD];
8252 ParmTy = PVD->getType();
8253 }
8254 ParamAttrs[Pos].Alignment =
8255 (*NI)
8256 ? (*NI)->EvaluateKnownConstInt(C)
Alexey Bataevc7a82b42016-05-06 09:40:08 +00008257 : llvm::APSInt::getUnsigned(
8258 C.toCharUnitsFromBits(C.getOpenMPDefaultSimdAlign(ParmTy))
8259 .getQuantity());
Alexey Bataev20cf67c2018-03-02 18:07:00 +00008260 ++NI;
Alexey Bataevc7a82b42016-05-06 09:40:08 +00008261 }
Alexey Bataev20cf67c2018-03-02 18:07:00 +00008262 // Mark linear parameters.
8263 auto SI = Attr->steps_begin();
8264 auto MI = Attr->modifiers_begin();
8265 for (auto *E : Attr->linears()) {
8266 E = E->IgnoreParenImpCasts();
8267 unsigned Pos;
8268 if (isa<CXXThisExpr>(E))
8269 Pos = ParamPositions[FD];
8270 else {
8271 auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl())
8272 ->getCanonicalDecl();
8273 Pos = ParamPositions[PVD];
8274 }
8275 auto &ParamAttr = ParamAttrs[Pos];
8276 ParamAttr.Kind = Linear;
8277 if (*SI) {
8278 if (!(*SI)->EvaluateAsInt(ParamAttr.StrideOrArg, C,
8279 Expr::SE_AllowSideEffects)) {
8280 if (auto *DRE = cast<DeclRefExpr>((*SI)->IgnoreParenImpCasts())) {
8281 if (auto *StridePVD = cast<ParmVarDecl>(DRE->getDecl())) {
8282 ParamAttr.Kind = LinearWithVarStride;
8283 ParamAttr.StrideOrArg = llvm::APSInt::getUnsigned(
8284 ParamPositions[StridePVD->getCanonicalDecl()]);
8285 }
Alexey Bataevc7a82b42016-05-06 09:40:08 +00008286 }
8287 }
8288 }
Alexey Bataev20cf67c2018-03-02 18:07:00 +00008289 ++SI;
8290 ++MI;
Alexey Bataevc7a82b42016-05-06 09:40:08 +00008291 }
Alexey Bataev20cf67c2018-03-02 18:07:00 +00008292 llvm::APSInt VLENVal;
8293 if (const Expr *VLEN = Attr->getSimdlen())
8294 VLENVal = VLEN->EvaluateKnownConstInt(C);
8295 OMPDeclareSimdDeclAttr::BranchStateTy State = Attr->getBranchState();
8296 if (CGM.getTriple().getArch() == llvm::Triple::x86 ||
8297 CGM.getTriple().getArch() == llvm::Triple::x86_64)
8298 emitX86DeclareSimdFunction(FD, Fn, VLENVal, ParamAttrs, State);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00008299 }
Alexey Bataev20cf67c2018-03-02 18:07:00 +00008300 FD = FD->getPreviousDecl();
Alexey Bataevc7a82b42016-05-06 09:40:08 +00008301 }
8302}
Alexey Bataev8b427062016-05-25 12:36:08 +00008303
8304namespace {
8305/// Cleanup action for doacross support.
8306class DoacrossCleanupTy final : public EHScopeStack::Cleanup {
8307public:
8308 static const int DoacrossFinArgs = 2;
8309
8310private:
8311 llvm::Value *RTLFn;
8312 llvm::Value *Args[DoacrossFinArgs];
8313
8314public:
8315 DoacrossCleanupTy(llvm::Value *RTLFn, ArrayRef<llvm::Value *> CallArgs)
8316 : RTLFn(RTLFn) {
8317 assert(CallArgs.size() == DoacrossFinArgs);
8318 std::copy(CallArgs.begin(), CallArgs.end(), std::begin(Args));
8319 }
8320 void Emit(CodeGenFunction &CGF, Flags /*flags*/) override {
8321 if (!CGF.HaveInsertPoint())
8322 return;
8323 CGF.EmitRuntimeCall(RTLFn, Args);
8324 }
8325};
8326} // namespace
8327
8328void CGOpenMPRuntime::emitDoacrossInit(CodeGenFunction &CGF,
8329 const OMPLoopDirective &D) {
8330 if (!CGF.HaveInsertPoint())
8331 return;
8332
8333 ASTContext &C = CGM.getContext();
8334 QualType Int64Ty = C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/true);
8335 RecordDecl *RD;
8336 if (KmpDimTy.isNull()) {
8337 // Build struct kmp_dim { // loop bounds info casted to kmp_int64
8338 // kmp_int64 lo; // lower
8339 // kmp_int64 up; // upper
8340 // kmp_int64 st; // stride
8341 // };
8342 RD = C.buildImplicitRecord("kmp_dim");
8343 RD->startDefinition();
8344 addFieldToRecordDecl(C, RD, Int64Ty);
8345 addFieldToRecordDecl(C, RD, Int64Ty);
8346 addFieldToRecordDecl(C, RD, Int64Ty);
8347 RD->completeDefinition();
8348 KmpDimTy = C.getRecordType(RD);
8349 } else
8350 RD = cast<RecordDecl>(KmpDimTy->getAsTagDecl());
8351
8352 Address DimsAddr = CGF.CreateMemTemp(KmpDimTy, "dims");
8353 CGF.EmitNullInitialization(DimsAddr, KmpDimTy);
8354 enum { LowerFD = 0, UpperFD, StrideFD };
8355 // Fill dims with data.
8356 LValue DimsLVal = CGF.MakeAddrLValue(DimsAddr, KmpDimTy);
8357 // dims.upper = num_iterations;
8358 LValue UpperLVal =
8359 CGF.EmitLValueForField(DimsLVal, *std::next(RD->field_begin(), UpperFD));
8360 llvm::Value *NumIterVal = CGF.EmitScalarConversion(
8361 CGF.EmitScalarExpr(D.getNumIterations()), D.getNumIterations()->getType(),
8362 Int64Ty, D.getNumIterations()->getExprLoc());
8363 CGF.EmitStoreOfScalar(NumIterVal, UpperLVal);
8364 // dims.stride = 1;
8365 LValue StrideLVal =
8366 CGF.EmitLValueForField(DimsLVal, *std::next(RD->field_begin(), StrideFD));
8367 CGF.EmitStoreOfScalar(llvm::ConstantInt::getSigned(CGM.Int64Ty, /*V=*/1),
8368 StrideLVal);
8369
8370 // Build call void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid,
8371 // kmp_int32 num_dims, struct kmp_dim * dims);
8372 llvm::Value *Args[] = {emitUpdateLocation(CGF, D.getLocStart()),
8373 getThreadID(CGF, D.getLocStart()),
8374 llvm::ConstantInt::getSigned(CGM.Int32Ty, 1),
8375 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
8376 DimsAddr.getPointer(), CGM.VoidPtrTy)};
8377
8378 llvm::Value *RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_init);
8379 CGF.EmitRuntimeCall(RTLFn, Args);
8380 llvm::Value *FiniArgs[DoacrossCleanupTy::DoacrossFinArgs] = {
8381 emitUpdateLocation(CGF, D.getLocEnd()), getThreadID(CGF, D.getLocEnd())};
8382 llvm::Value *FiniRTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_fini);
8383 CGF.EHStack.pushCleanup<DoacrossCleanupTy>(NormalAndEHCleanup, FiniRTLFn,
8384 llvm::makeArrayRef(FiniArgs));
8385}
8386
8387void CGOpenMPRuntime::emitDoacrossOrdered(CodeGenFunction &CGF,
8388 const OMPDependClause *C) {
8389 QualType Int64Ty =
8390 CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
8391 const Expr *CounterVal = C->getCounterValue();
8392 assert(CounterVal);
8393 llvm::Value *CntVal = CGF.EmitScalarConversion(CGF.EmitScalarExpr(CounterVal),
8394 CounterVal->getType(), Int64Ty,
8395 CounterVal->getExprLoc());
8396 Address CntAddr = CGF.CreateMemTemp(Int64Ty, ".cnt.addr");
8397 CGF.EmitStoreOfScalar(CntVal, CntAddr, /*Volatile=*/false, Int64Ty);
8398 llvm::Value *Args[] = {emitUpdateLocation(CGF, C->getLocStart()),
8399 getThreadID(CGF, C->getLocStart()),
8400 CntAddr.getPointer()};
8401 llvm::Value *RTLFn;
8402 if (C->getDependencyKind() == OMPC_DEPEND_source)
8403 RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_post);
8404 else {
8405 assert(C->getDependencyKind() == OMPC_DEPEND_sink);
8406 RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_wait);
8407 }
8408 CGF.EmitRuntimeCall(RTLFn, Args);
8409}
8410
Alexey Bataev7ef47a62018-02-22 18:33:31 +00008411void CGOpenMPRuntime::emitCall(CodeGenFunction &CGF, SourceLocation Loc,
8412 llvm::Value *Callee,
8413 ArrayRef<llvm::Value *> Args) const {
8414 assert(Loc.isValid() && "Outlined function call location must be valid.");
Alexey Bataev3c595a62017-08-14 15:01:03 +00008415 auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc);
8416
8417 if (auto *Fn = dyn_cast<llvm::Function>(Callee)) {
Alexey Bataev2c7eee52017-08-04 19:10:54 +00008418 if (Fn->doesNotThrow()) {
Alexey Bataev3c595a62017-08-14 15:01:03 +00008419 CGF.EmitNounwindRuntimeCall(Fn, Args);
Alexey Bataev2c7eee52017-08-04 19:10:54 +00008420 return;
8421 }
8422 }
Alexey Bataev3c595a62017-08-14 15:01:03 +00008423 CGF.EmitRuntimeCall(Callee, Args);
8424}
8425
8426void CGOpenMPRuntime::emitOutlinedFunctionCall(
8427 CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn,
8428 ArrayRef<llvm::Value *> Args) const {
Alexey Bataev7ef47a62018-02-22 18:33:31 +00008429 emitCall(CGF, Loc, OutlinedFn, Args);
Alexey Bataev2c7eee52017-08-04 19:10:54 +00008430}
Alexey Bataev3b8d5582017-08-08 18:04:06 +00008431
8432Address CGOpenMPRuntime::getParameterAddress(CodeGenFunction &CGF,
8433 const VarDecl *NativeParam,
8434 const VarDecl *TargetParam) const {
8435 return CGF.GetAddrOfLocalVar(NativeParam);
8436}
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00008437
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00008438Address CGOpenMPRuntime::getAddressOfLocalVariable(CodeGenFunction &CGF,
8439 const VarDecl *VD) {
8440 return Address::invalid();
8441}
8442
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00008443llvm::Value *CGOpenMPSIMDRuntime::emitParallelOutlinedFunction(
8444 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
8445 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
8446 llvm_unreachable("Not supported in SIMD-only mode");
8447}
8448
8449llvm::Value *CGOpenMPSIMDRuntime::emitTeamsOutlinedFunction(
8450 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
8451 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
8452 llvm_unreachable("Not supported in SIMD-only mode");
8453}
8454
8455llvm::Value *CGOpenMPSIMDRuntime::emitTaskOutlinedFunction(
8456 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
8457 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
8458 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
8459 bool Tied, unsigned &NumberOfParts) {
8460 llvm_unreachable("Not supported in SIMD-only mode");
8461}
8462
8463void CGOpenMPSIMDRuntime::emitParallelCall(CodeGenFunction &CGF,
8464 SourceLocation Loc,
8465 llvm::Value *OutlinedFn,
8466 ArrayRef<llvm::Value *> CapturedVars,
8467 const Expr *IfCond) {
8468 llvm_unreachable("Not supported in SIMD-only mode");
8469}
8470
8471void CGOpenMPSIMDRuntime::emitCriticalRegion(
8472 CodeGenFunction &CGF, StringRef CriticalName,
8473 const RegionCodeGenTy &CriticalOpGen, SourceLocation Loc,
8474 const Expr *Hint) {
8475 llvm_unreachable("Not supported in SIMD-only mode");
8476}
8477
8478void CGOpenMPSIMDRuntime::emitMasterRegion(CodeGenFunction &CGF,
8479 const RegionCodeGenTy &MasterOpGen,
8480 SourceLocation Loc) {
8481 llvm_unreachable("Not supported in SIMD-only mode");
8482}
8483
8484void CGOpenMPSIMDRuntime::emitTaskyieldCall(CodeGenFunction &CGF,
8485 SourceLocation Loc) {
8486 llvm_unreachable("Not supported in SIMD-only mode");
8487}
8488
8489void CGOpenMPSIMDRuntime::emitTaskgroupRegion(
8490 CodeGenFunction &CGF, const RegionCodeGenTy &TaskgroupOpGen,
8491 SourceLocation Loc) {
8492 llvm_unreachable("Not supported in SIMD-only mode");
8493}
8494
8495void CGOpenMPSIMDRuntime::emitSingleRegion(
8496 CodeGenFunction &CGF, const RegionCodeGenTy &SingleOpGen,
8497 SourceLocation Loc, ArrayRef<const Expr *> CopyprivateVars,
8498 ArrayRef<const Expr *> DestExprs, ArrayRef<const Expr *> SrcExprs,
8499 ArrayRef<const Expr *> AssignmentOps) {
8500 llvm_unreachable("Not supported in SIMD-only mode");
8501}
8502
8503void CGOpenMPSIMDRuntime::emitOrderedRegion(CodeGenFunction &CGF,
8504 const RegionCodeGenTy &OrderedOpGen,
8505 SourceLocation Loc,
8506 bool IsThreads) {
8507 llvm_unreachable("Not supported in SIMD-only mode");
8508}
8509
8510void CGOpenMPSIMDRuntime::emitBarrierCall(CodeGenFunction &CGF,
8511 SourceLocation Loc,
8512 OpenMPDirectiveKind Kind,
8513 bool EmitChecks,
8514 bool ForceSimpleCall) {
8515 llvm_unreachable("Not supported in SIMD-only mode");
8516}
8517
8518void CGOpenMPSIMDRuntime::emitForDispatchInit(
8519 CodeGenFunction &CGF, SourceLocation Loc,
8520 const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned,
8521 bool Ordered, const DispatchRTInput &DispatchValues) {
8522 llvm_unreachable("Not supported in SIMD-only mode");
8523}
8524
8525void CGOpenMPSIMDRuntime::emitForStaticInit(
8526 CodeGenFunction &CGF, SourceLocation Loc, OpenMPDirectiveKind DKind,
8527 const OpenMPScheduleTy &ScheduleKind, const StaticRTInput &Values) {
8528 llvm_unreachable("Not supported in SIMD-only mode");
8529}
8530
8531void CGOpenMPSIMDRuntime::emitDistributeStaticInit(
8532 CodeGenFunction &CGF, SourceLocation Loc,
8533 OpenMPDistScheduleClauseKind SchedKind, const StaticRTInput &Values) {
8534 llvm_unreachable("Not supported in SIMD-only mode");
8535}
8536
8537void CGOpenMPSIMDRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF,
8538 SourceLocation Loc,
8539 unsigned IVSize,
8540 bool IVSigned) {
8541 llvm_unreachable("Not supported in SIMD-only mode");
8542}
8543
8544void CGOpenMPSIMDRuntime::emitForStaticFinish(CodeGenFunction &CGF,
8545 SourceLocation Loc,
8546 OpenMPDirectiveKind DKind) {
8547 llvm_unreachable("Not supported in SIMD-only mode");
8548}
8549
8550llvm::Value *CGOpenMPSIMDRuntime::emitForNext(CodeGenFunction &CGF,
8551 SourceLocation Loc,
8552 unsigned IVSize, bool IVSigned,
8553 Address IL, Address LB,
8554 Address UB, Address ST) {
8555 llvm_unreachable("Not supported in SIMD-only mode");
8556}
8557
8558void CGOpenMPSIMDRuntime::emitNumThreadsClause(CodeGenFunction &CGF,
8559 llvm::Value *NumThreads,
8560 SourceLocation Loc) {
8561 llvm_unreachable("Not supported in SIMD-only mode");
8562}
8563
8564void CGOpenMPSIMDRuntime::emitProcBindClause(CodeGenFunction &CGF,
8565 OpenMPProcBindClauseKind ProcBind,
8566 SourceLocation Loc) {
8567 llvm_unreachable("Not supported in SIMD-only mode");
8568}
8569
8570Address CGOpenMPSIMDRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF,
8571 const VarDecl *VD,
8572 Address VDAddr,
8573 SourceLocation Loc) {
8574 llvm_unreachable("Not supported in SIMD-only mode");
8575}
8576
8577llvm::Function *CGOpenMPSIMDRuntime::emitThreadPrivateVarDefinition(
8578 const VarDecl *VD, Address VDAddr, SourceLocation Loc, bool PerformInit,
8579 CodeGenFunction *CGF) {
8580 llvm_unreachable("Not supported in SIMD-only mode");
8581}
8582
8583Address CGOpenMPSIMDRuntime::getAddrOfArtificialThreadPrivate(
8584 CodeGenFunction &CGF, QualType VarType, StringRef Name) {
8585 llvm_unreachable("Not supported in SIMD-only mode");
8586}
8587
8588void CGOpenMPSIMDRuntime::emitFlush(CodeGenFunction &CGF,
8589 ArrayRef<const Expr *> Vars,
8590 SourceLocation Loc) {
8591 llvm_unreachable("Not supported in SIMD-only mode");
8592}
8593
8594void CGOpenMPSIMDRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
8595 const OMPExecutableDirective &D,
8596 llvm::Value *TaskFunction,
8597 QualType SharedsTy, Address Shareds,
8598 const Expr *IfCond,
8599 const OMPTaskDataTy &Data) {
8600 llvm_unreachable("Not supported in SIMD-only mode");
8601}
8602
8603void CGOpenMPSIMDRuntime::emitTaskLoopCall(
8604 CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
8605 llvm::Value *TaskFunction, QualType SharedsTy, Address Shareds,
8606 const Expr *IfCond, const OMPTaskDataTy &Data) {
8607 llvm_unreachable("Not supported in SIMD-only mode");
8608}
8609
8610void CGOpenMPSIMDRuntime::emitReduction(
8611 CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> Privates,
8612 ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs,
8613 ArrayRef<const Expr *> ReductionOps, ReductionOptionsTy Options) {
8614 assert(Options.SimpleReduction && "Only simple reduction is expected.");
8615 CGOpenMPRuntime::emitReduction(CGF, Loc, Privates, LHSExprs, RHSExprs,
8616 ReductionOps, Options);
8617}
8618
8619llvm::Value *CGOpenMPSIMDRuntime::emitTaskReductionInit(
8620 CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs,
8621 ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) {
8622 llvm_unreachable("Not supported in SIMD-only mode");
8623}
8624
8625void CGOpenMPSIMDRuntime::emitTaskReductionFixups(CodeGenFunction &CGF,
8626 SourceLocation Loc,
8627 ReductionCodeGen &RCG,
8628 unsigned N) {
8629 llvm_unreachable("Not supported in SIMD-only mode");
8630}
8631
8632Address CGOpenMPSIMDRuntime::getTaskReductionItem(CodeGenFunction &CGF,
8633 SourceLocation Loc,
8634 llvm::Value *ReductionsPtr,
8635 LValue SharedLVal) {
8636 llvm_unreachable("Not supported in SIMD-only mode");
8637}
8638
8639void CGOpenMPSIMDRuntime::emitTaskwaitCall(CodeGenFunction &CGF,
8640 SourceLocation Loc) {
8641 llvm_unreachable("Not supported in SIMD-only mode");
8642}
8643
8644void CGOpenMPSIMDRuntime::emitCancellationPointCall(
8645 CodeGenFunction &CGF, SourceLocation Loc,
8646 OpenMPDirectiveKind CancelRegion) {
8647 llvm_unreachable("Not supported in SIMD-only mode");
8648}
8649
8650void CGOpenMPSIMDRuntime::emitCancelCall(CodeGenFunction &CGF,
8651 SourceLocation Loc, const Expr *IfCond,
8652 OpenMPDirectiveKind CancelRegion) {
8653 llvm_unreachable("Not supported in SIMD-only mode");
8654}
8655
8656void CGOpenMPSIMDRuntime::emitTargetOutlinedFunction(
8657 const OMPExecutableDirective &D, StringRef ParentName,
8658 llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
8659 bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
8660 llvm_unreachable("Not supported in SIMD-only mode");
8661}
8662
8663void CGOpenMPSIMDRuntime::emitTargetCall(CodeGenFunction &CGF,
8664 const OMPExecutableDirective &D,
8665 llvm::Value *OutlinedFn,
8666 llvm::Value *OutlinedFnID,
Alexey Bataev8451efa2018-01-15 19:06:12 +00008667 const Expr *IfCond, const Expr *Device) {
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00008668 llvm_unreachable("Not supported in SIMD-only mode");
8669}
8670
8671bool CGOpenMPSIMDRuntime::emitTargetFunctions(GlobalDecl GD) {
8672 llvm_unreachable("Not supported in SIMD-only mode");
8673}
8674
8675bool CGOpenMPSIMDRuntime::emitTargetGlobalVariable(GlobalDecl GD) {
8676 llvm_unreachable("Not supported in SIMD-only mode");
8677}
8678
8679bool CGOpenMPSIMDRuntime::emitTargetGlobal(GlobalDecl GD) {
8680 return false;
8681}
8682
8683llvm::Function *CGOpenMPSIMDRuntime::emitRegistrationFunction() {
8684 return nullptr;
8685}
8686
8687void CGOpenMPSIMDRuntime::emitTeamsCall(CodeGenFunction &CGF,
8688 const OMPExecutableDirective &D,
8689 SourceLocation Loc,
8690 llvm::Value *OutlinedFn,
8691 ArrayRef<llvm::Value *> CapturedVars) {
8692 llvm_unreachable("Not supported in SIMD-only mode");
8693}
8694
8695void CGOpenMPSIMDRuntime::emitNumTeamsClause(CodeGenFunction &CGF,
8696 const Expr *NumTeams,
8697 const Expr *ThreadLimit,
8698 SourceLocation Loc) {
8699 llvm_unreachable("Not supported in SIMD-only mode");
8700}
8701
8702void CGOpenMPSIMDRuntime::emitTargetDataCalls(
8703 CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
8704 const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) {
8705 llvm_unreachable("Not supported in SIMD-only mode");
8706}
8707
8708void CGOpenMPSIMDRuntime::emitTargetDataStandAloneCall(
8709 CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
8710 const Expr *Device) {
8711 llvm_unreachable("Not supported in SIMD-only mode");
8712}
8713
8714void CGOpenMPSIMDRuntime::emitDoacrossInit(CodeGenFunction &CGF,
8715 const OMPLoopDirective &D) {
8716 llvm_unreachable("Not supported in SIMD-only mode");
8717}
8718
8719void CGOpenMPSIMDRuntime::emitDoacrossOrdered(CodeGenFunction &CGF,
8720 const OMPDependClause *C) {
8721 llvm_unreachable("Not supported in SIMD-only mode");
8722}
8723
8724const VarDecl *
8725CGOpenMPSIMDRuntime::translateParameter(const FieldDecl *FD,
8726 const VarDecl *NativeParam) const {
8727 llvm_unreachable("Not supported in SIMD-only mode");
8728}
8729
8730Address
8731CGOpenMPSIMDRuntime::getParameterAddress(CodeGenFunction &CGF,
8732 const VarDecl *NativeParam,
8733 const VarDecl *TargetParam) const {
8734 llvm_unreachable("Not supported in SIMD-only mode");
8735}
8736