blob: 6e663bb46bb5d4e6c9a3f01ce1bd166c614800d8 [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"
18#include "clang/AST/Decl.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000019#include "clang/AST/StmtOpenMP.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000020#include "llvm/ADT/ArrayRef.h"
Teresa Johnsonffc4e242016-11-11 05:35:12 +000021#include "llvm/Bitcode/BitcodeReader.h"
Alexey Bataevd74d0602014-10-13 06:02:40 +000022#include "llvm/IR/CallSite.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000023#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/GlobalValue.h"
25#include "llvm/IR/Value.h"
Samuel Antaoee8fb302016-01-06 13:42:12 +000026#include "llvm/Support/Format.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000027#include "llvm/Support/raw_ostream.h"
Alexey Bataev23b69422014-06-18 07:08:49 +000028#include <cassert>
Alexey Bataev9959db52014-05-06 10:08:46 +000029
30using namespace clang;
31using namespace CodeGen;
32
Benjamin Kramerc52193f2014-10-10 13:57:57 +000033namespace {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000034/// \brief Base class for handling code generation inside OpenMP regions.
Alexey Bataev18095712014-10-10 12:19:54 +000035class CGOpenMPRegionInfo : public CodeGenFunction::CGCapturedStmtInfo {
36public:
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000037 /// \brief Kinds of OpenMP regions used in codegen.
38 enum CGOpenMPRegionKind {
39 /// \brief Region with outlined function for standalone 'parallel'
40 /// directive.
41 ParallelOutlinedRegion,
42 /// \brief Region with outlined function for standalone 'task' directive.
43 TaskOutlinedRegion,
44 /// \brief Region for constructs that do not require function outlining,
45 /// like 'for', 'sections', 'atomic' etc. directives.
46 InlinedRegion,
Samuel Antaobed3c462015-10-02 16:14:20 +000047 /// \brief Region with outlined function for standalone 'target' directive.
48 TargetRegion,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000049 };
Alexey Bataev18095712014-10-10 12:19:54 +000050
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000051 CGOpenMPRegionInfo(const CapturedStmt &CS,
52 const CGOpenMPRegionKind RegionKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +000053 const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind,
54 bool HasCancel)
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000055 : CGCapturedStmtInfo(CS, CR_OpenMP), RegionKind(RegionKind),
Alexey Bataev25e5b442015-09-15 12:52:43 +000056 CodeGen(CodeGen), Kind(Kind), HasCancel(HasCancel) {}
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000057
58 CGOpenMPRegionInfo(const CGOpenMPRegionKind RegionKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +000059 const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind,
60 bool HasCancel)
Alexey Bataev81c7ea02015-07-03 09:56:58 +000061 : CGCapturedStmtInfo(CR_OpenMP), RegionKind(RegionKind), CodeGen(CodeGen),
Alexey Bataev25e5b442015-09-15 12:52:43 +000062 Kind(Kind), HasCancel(HasCancel) {}
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000063
64 /// \brief Get a variable or parameter for storing global thread id
Alexey Bataev18095712014-10-10 12:19:54 +000065 /// inside OpenMP construct.
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000066 virtual const VarDecl *getThreadIDVariable() const = 0;
Alexey Bataev18095712014-10-10 12:19:54 +000067
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000068 /// \brief Emit the captured statement body.
Hans Wennborg7eb54642015-09-10 17:07:54 +000069 void EmitBody(CodeGenFunction &CGF, const Stmt *S) override;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000070
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000071 /// \brief Get an LValue for the current ThreadID variable.
Alexey Bataev62b63b12015-03-10 07:28:44 +000072 /// \return LValue for thread id variable. This LValue always has type int32*.
73 virtual LValue getThreadIDVariableLValue(CodeGenFunction &CGF);
Alexey Bataev18095712014-10-10 12:19:54 +000074
Alexey Bataev48591dd2016-04-20 04:01:36 +000075 virtual void emitUntiedSwitch(CodeGenFunction & /*CGF*/) {}
76
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000077 CGOpenMPRegionKind getRegionKind() const { return RegionKind; }
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000078
Alexey Bataev81c7ea02015-07-03 09:56:58 +000079 OpenMPDirectiveKind getDirectiveKind() const { return Kind; }
80
Alexey Bataev25e5b442015-09-15 12:52:43 +000081 bool hasCancel() const { return HasCancel; }
82
Alexey Bataev18095712014-10-10 12:19:54 +000083 static bool classof(const CGCapturedStmtInfo *Info) {
84 return Info->getKind() == CR_OpenMP;
85 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000086
Alexey Bataev48591dd2016-04-20 04:01:36 +000087 ~CGOpenMPRegionInfo() override = default;
88
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000089protected:
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000090 CGOpenMPRegionKind RegionKind;
Hans Wennborg45c74392016-01-12 20:54:36 +000091 RegionCodeGenTy CodeGen;
Alexey Bataev81c7ea02015-07-03 09:56:58 +000092 OpenMPDirectiveKind Kind;
Alexey Bataev25e5b442015-09-15 12:52:43 +000093 bool HasCancel;
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000094};
Alexey Bataev18095712014-10-10 12:19:54 +000095
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000096/// \brief API for captured statement code generation in OpenMP constructs.
Alexey Bataev48591dd2016-04-20 04:01:36 +000097class CGOpenMPOutlinedRegionInfo final : public CGOpenMPRegionInfo {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000098public:
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000099 CGOpenMPOutlinedRegionInfo(const CapturedStmt &CS, const VarDecl *ThreadIDVar,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000100 const RegionCodeGenTy &CodeGen,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000101 OpenMPDirectiveKind Kind, bool HasCancel)
102 : CGOpenMPRegionInfo(CS, ParallelOutlinedRegion, CodeGen, Kind,
103 HasCancel),
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000104 ThreadIDVar(ThreadIDVar) {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000105 assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region.");
106 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000107
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000108 /// \brief Get a variable or parameter for storing global thread id
109 /// inside OpenMP construct.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000110 const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000111
Alexey Bataev18095712014-10-10 12:19:54 +0000112 /// \brief Get the name of the capture helper.
Benjamin Kramerc52193f2014-10-10 13:57:57 +0000113 StringRef getHelperName() const override { return ".omp_outlined."; }
Alexey Bataev18095712014-10-10 12:19:54 +0000114
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000115 static bool classof(const CGCapturedStmtInfo *Info) {
116 return CGOpenMPRegionInfo::classof(Info) &&
117 cast<CGOpenMPRegionInfo>(Info)->getRegionKind() ==
118 ParallelOutlinedRegion;
119 }
120
Alexey Bataev18095712014-10-10 12:19:54 +0000121private:
122 /// \brief A variable or parameter storing global thread id for OpenMP
123 /// constructs.
124 const VarDecl *ThreadIDVar;
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000125};
126
Alexey Bataev62b63b12015-03-10 07:28:44 +0000127/// \brief API for captured statement code generation in OpenMP constructs.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000128class CGOpenMPTaskOutlinedRegionInfo final : public CGOpenMPRegionInfo {
Alexey Bataev62b63b12015-03-10 07:28:44 +0000129public:
Alexey Bataev48591dd2016-04-20 04:01:36 +0000130 class UntiedTaskActionTy final : public PrePostActionTy {
131 bool Untied;
132 const VarDecl *PartIDVar;
133 const RegionCodeGenTy UntiedCodeGen;
134 llvm::SwitchInst *UntiedSwitch = nullptr;
135
136 public:
137 UntiedTaskActionTy(bool Tied, const VarDecl *PartIDVar,
138 const RegionCodeGenTy &UntiedCodeGen)
139 : Untied(!Tied), PartIDVar(PartIDVar), UntiedCodeGen(UntiedCodeGen) {}
140 void Enter(CodeGenFunction &CGF) override {
141 if (Untied) {
142 // Emit task switching point.
143 auto PartIdLVal = CGF.EmitLoadOfPointerLValue(
144 CGF.GetAddrOfLocalVar(PartIDVar),
145 PartIDVar->getType()->castAs<PointerType>());
146 auto *Res = CGF.EmitLoadOfScalar(PartIdLVal, SourceLocation());
147 auto *DoneBB = CGF.createBasicBlock(".untied.done.");
148 UntiedSwitch = CGF.Builder.CreateSwitch(Res, DoneBB);
149 CGF.EmitBlock(DoneBB);
150 CGF.EmitBranchThroughCleanup(CGF.ReturnBlock);
151 CGF.EmitBlock(CGF.createBasicBlock(".untied.jmp."));
152 UntiedSwitch->addCase(CGF.Builder.getInt32(0),
153 CGF.Builder.GetInsertBlock());
154 emitUntiedSwitch(CGF);
155 }
156 }
157 void emitUntiedSwitch(CodeGenFunction &CGF) const {
158 if (Untied) {
159 auto PartIdLVal = CGF.EmitLoadOfPointerLValue(
160 CGF.GetAddrOfLocalVar(PartIDVar),
161 PartIDVar->getType()->castAs<PointerType>());
162 CGF.EmitStoreOfScalar(CGF.Builder.getInt32(UntiedSwitch->getNumCases()),
163 PartIdLVal);
164 UntiedCodeGen(CGF);
165 CodeGenFunction::JumpDest CurPoint =
166 CGF.getJumpDestInCurrentScope(".untied.next.");
167 CGF.EmitBranchThroughCleanup(CGF.ReturnBlock);
168 CGF.EmitBlock(CGF.createBasicBlock(".untied.jmp."));
169 UntiedSwitch->addCase(CGF.Builder.getInt32(UntiedSwitch->getNumCases()),
170 CGF.Builder.GetInsertBlock());
171 CGF.EmitBranchThroughCleanup(CurPoint);
172 CGF.EmitBlock(CurPoint.getBlock());
173 }
174 }
175 unsigned getNumberOfParts() const { return UntiedSwitch->getNumCases(); }
176 };
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000177 CGOpenMPTaskOutlinedRegionInfo(const CapturedStmt &CS,
Alexey Bataev62b63b12015-03-10 07:28:44 +0000178 const VarDecl *ThreadIDVar,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000179 const RegionCodeGenTy &CodeGen,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000180 OpenMPDirectiveKind Kind, bool HasCancel,
181 const UntiedTaskActionTy &Action)
Alexey Bataev25e5b442015-09-15 12:52:43 +0000182 : CGOpenMPRegionInfo(CS, TaskOutlinedRegion, CodeGen, Kind, HasCancel),
Alexey Bataev48591dd2016-04-20 04:01:36 +0000183 ThreadIDVar(ThreadIDVar), Action(Action) {
Alexey Bataev62b63b12015-03-10 07:28:44 +0000184 assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region.");
185 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000186
Alexey Bataev62b63b12015-03-10 07:28:44 +0000187 /// \brief Get a variable or parameter for storing global thread id
188 /// inside OpenMP construct.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000189 const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; }
Alexey Bataev62b63b12015-03-10 07:28:44 +0000190
191 /// \brief Get an LValue for the current ThreadID variable.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000192 LValue getThreadIDVariableLValue(CodeGenFunction &CGF) override;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000193
Alexey Bataev62b63b12015-03-10 07:28:44 +0000194 /// \brief Get the name of the capture helper.
195 StringRef getHelperName() const override { return ".omp_outlined."; }
196
Alexey Bataev48591dd2016-04-20 04:01:36 +0000197 void emitUntiedSwitch(CodeGenFunction &CGF) override {
198 Action.emitUntiedSwitch(CGF);
199 }
200
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000201 static bool classof(const CGCapturedStmtInfo *Info) {
202 return CGOpenMPRegionInfo::classof(Info) &&
203 cast<CGOpenMPRegionInfo>(Info)->getRegionKind() ==
204 TaskOutlinedRegion;
205 }
206
Alexey Bataev62b63b12015-03-10 07:28:44 +0000207private:
208 /// \brief A variable or parameter storing global thread id for OpenMP
209 /// constructs.
210 const VarDecl *ThreadIDVar;
Alexey Bataev48591dd2016-04-20 04:01:36 +0000211 /// Action for emitting code for untied tasks.
212 const UntiedTaskActionTy &Action;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000213};
214
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000215/// \brief API for inlined captured statement code generation in OpenMP
216/// constructs.
217class CGOpenMPInlinedRegionInfo : public CGOpenMPRegionInfo {
218public:
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000219 CGOpenMPInlinedRegionInfo(CodeGenFunction::CGCapturedStmtInfo *OldCSI,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000220 const RegionCodeGenTy &CodeGen,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000221 OpenMPDirectiveKind Kind, bool HasCancel)
222 : CGOpenMPRegionInfo(InlinedRegion, CodeGen, Kind, HasCancel),
223 OldCSI(OldCSI),
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000224 OuterRegionInfo(dyn_cast_or_null<CGOpenMPRegionInfo>(OldCSI)) {}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000225
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000226 // \brief Retrieve the value of the context parameter.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000227 llvm::Value *getContextValue() const override {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000228 if (OuterRegionInfo)
229 return OuterRegionInfo->getContextValue();
230 llvm_unreachable("No context value for inlined OpenMP region");
231 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000232
Hans Wennborg7eb54642015-09-10 17:07:54 +0000233 void setContextValue(llvm::Value *V) override {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000234 if (OuterRegionInfo) {
235 OuterRegionInfo->setContextValue(V);
236 return;
237 }
238 llvm_unreachable("No context value for inlined OpenMP region");
239 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000240
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000241 /// \brief Lookup the captured field decl for a variable.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000242 const FieldDecl *lookup(const VarDecl *VD) const override {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000243 if (OuterRegionInfo)
244 return OuterRegionInfo->lookup(VD);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000245 // If there is no outer outlined region,no need to lookup in a list of
246 // captured variables, we can use the original one.
247 return nullptr;
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000248 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000249
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000250 FieldDecl *getThisFieldDecl() const override {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000251 if (OuterRegionInfo)
252 return OuterRegionInfo->getThisFieldDecl();
253 return nullptr;
254 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000255
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000256 /// \brief Get a variable or parameter for storing global thread id
257 /// inside OpenMP construct.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000258 const VarDecl *getThreadIDVariable() const override {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000259 if (OuterRegionInfo)
260 return OuterRegionInfo->getThreadIDVariable();
261 return nullptr;
262 }
Alexey Bataev62b63b12015-03-10 07:28:44 +0000263
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000264 /// \brief Get the name of the capture helper.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000265 StringRef getHelperName() const override {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000266 if (auto *OuterRegionInfo = getOldCSI())
267 return OuterRegionInfo->getHelperName();
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000268 llvm_unreachable("No helper name for inlined OpenMP construct");
269 }
270
Alexey Bataev48591dd2016-04-20 04:01:36 +0000271 void emitUntiedSwitch(CodeGenFunction &CGF) override {
272 if (OuterRegionInfo)
273 OuterRegionInfo->emitUntiedSwitch(CGF);
274 }
275
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000276 CodeGenFunction::CGCapturedStmtInfo *getOldCSI() const { return OldCSI; }
277
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000278 static bool classof(const CGCapturedStmtInfo *Info) {
279 return CGOpenMPRegionInfo::classof(Info) &&
280 cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == InlinedRegion;
281 }
282
Alexey Bataev48591dd2016-04-20 04:01:36 +0000283 ~CGOpenMPInlinedRegionInfo() override = default;
284
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000285private:
286 /// \brief CodeGen info about outer OpenMP region.
287 CodeGenFunction::CGCapturedStmtInfo *OldCSI;
288 CGOpenMPRegionInfo *OuterRegionInfo;
Alexey Bataev18095712014-10-10 12:19:54 +0000289};
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000290
Samuel Antaobed3c462015-10-02 16:14:20 +0000291/// \brief API for captured statement code generation in OpenMP target
292/// constructs. For this captures, implicit parameters are used instead of the
Samuel Antaoee8fb302016-01-06 13:42:12 +0000293/// captured fields. The name of the target region has to be unique in a given
294/// application so it is provided by the client, because only the client has
295/// the information to generate that.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000296class CGOpenMPTargetRegionInfo final : public CGOpenMPRegionInfo {
Samuel Antaobed3c462015-10-02 16:14:20 +0000297public:
298 CGOpenMPTargetRegionInfo(const CapturedStmt &CS,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000299 const RegionCodeGenTy &CodeGen, StringRef HelperName)
Samuel Antaobed3c462015-10-02 16:14:20 +0000300 : CGOpenMPRegionInfo(CS, TargetRegion, CodeGen, OMPD_target,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000301 /*HasCancel=*/false),
302 HelperName(HelperName) {}
Samuel Antaobed3c462015-10-02 16:14:20 +0000303
304 /// \brief This is unused for target regions because each starts executing
305 /// with a single thread.
306 const VarDecl *getThreadIDVariable() const override { return nullptr; }
307
308 /// \brief Get the name of the capture helper.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000309 StringRef getHelperName() const override { return HelperName; }
Samuel Antaobed3c462015-10-02 16:14:20 +0000310
311 static bool classof(const CGCapturedStmtInfo *Info) {
312 return CGOpenMPRegionInfo::classof(Info) &&
313 cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == TargetRegion;
314 }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000315
316private:
317 StringRef HelperName;
Samuel Antaobed3c462015-10-02 16:14:20 +0000318};
319
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000320static void EmptyCodeGen(CodeGenFunction &, PrePostActionTy &) {
Samuel Antaob68e2db2016-03-03 16:20:23 +0000321 llvm_unreachable("No codegen for expressions");
322}
323/// \brief API for generation of expressions captured in a innermost OpenMP
324/// region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000325class CGOpenMPInnerExprInfo final : public CGOpenMPInlinedRegionInfo {
Samuel Antaob68e2db2016-03-03 16:20:23 +0000326public:
327 CGOpenMPInnerExprInfo(CodeGenFunction &CGF, const CapturedStmt &CS)
328 : CGOpenMPInlinedRegionInfo(CGF.CapturedStmtInfo, EmptyCodeGen,
329 OMPD_unknown,
330 /*HasCancel=*/false),
331 PrivScope(CGF) {
332 // Make sure the globals captured in the provided statement are local by
333 // using the privatization logic. We assume the same variable is not
334 // captured more than once.
335 for (auto &C : CS.captures()) {
336 if (!C.capturesVariable() && !C.capturesVariableByCopy())
337 continue;
338
339 const VarDecl *VD = C.getCapturedVar();
340 if (VD->isLocalVarDeclOrParm())
341 continue;
342
343 DeclRefExpr DRE(const_cast<VarDecl *>(VD),
344 /*RefersToEnclosingVariableOrCapture=*/false,
345 VD->getType().getNonReferenceType(), VK_LValue,
346 SourceLocation());
347 PrivScope.addPrivate(VD, [&CGF, &DRE]() -> Address {
348 return CGF.EmitLValue(&DRE).getAddress();
349 });
350 }
351 (void)PrivScope.Privatize();
352 }
353
354 /// \brief Lookup the captured field decl for a variable.
355 const FieldDecl *lookup(const VarDecl *VD) const override {
356 if (auto *FD = CGOpenMPInlinedRegionInfo::lookup(VD))
357 return FD;
358 return nullptr;
359 }
360
361 /// \brief Emit the captured statement body.
362 void EmitBody(CodeGenFunction &CGF, const Stmt *S) override {
363 llvm_unreachable("No body for expressions");
364 }
365
366 /// \brief Get a variable or parameter for storing global thread id
367 /// inside OpenMP construct.
368 const VarDecl *getThreadIDVariable() const override {
369 llvm_unreachable("No thread id for expressions");
370 }
371
372 /// \brief Get the name of the capture helper.
373 StringRef getHelperName() const override {
374 llvm_unreachable("No helper name for expressions");
375 }
376
377 static bool classof(const CGCapturedStmtInfo *Info) { return false; }
378
379private:
380 /// Private scope to capture global variables.
381 CodeGenFunction::OMPPrivateScope PrivScope;
382};
383
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000384/// \brief RAII for emitting code of OpenMP constructs.
385class InlinedOpenMPRegionRAII {
386 CodeGenFunction &CGF;
Alexey Bataev4ba78a42016-04-27 07:56:03 +0000387 llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
388 FieldDecl *LambdaThisCaptureField = nullptr;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000389
390public:
391 /// \brief Constructs region for combined constructs.
392 /// \param CodeGen Code generation sequence for combined directives. Includes
393 /// a list of functions used for code generation of implicitly inlined
394 /// regions.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000395 InlinedOpenMPRegionRAII(CodeGenFunction &CGF, const RegionCodeGenTy &CodeGen,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000396 OpenMPDirectiveKind Kind, bool HasCancel)
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000397 : CGF(CGF) {
398 // Start emission for the construct.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000399 CGF.CapturedStmtInfo = new CGOpenMPInlinedRegionInfo(
400 CGF.CapturedStmtInfo, CodeGen, Kind, HasCancel);
Alexey Bataev4ba78a42016-04-27 07:56:03 +0000401 std::swap(CGF.LambdaCaptureFields, LambdaCaptureFields);
402 LambdaThisCaptureField = CGF.LambdaThisCaptureField;
403 CGF.LambdaThisCaptureField = nullptr;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000404 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000405
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000406 ~InlinedOpenMPRegionRAII() {
407 // Restore original CapturedStmtInfo only if we're done with code emission.
408 auto *OldCSI =
409 cast<CGOpenMPInlinedRegionInfo>(CGF.CapturedStmtInfo)->getOldCSI();
410 delete CGF.CapturedStmtInfo;
411 CGF.CapturedStmtInfo = OldCSI;
Alexey Bataev4ba78a42016-04-27 07:56:03 +0000412 std::swap(CGF.LambdaCaptureFields, LambdaCaptureFields);
413 CGF.LambdaThisCaptureField = LambdaThisCaptureField;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000414 }
415};
416
Alexey Bataev50b3c952016-02-19 10:38:26 +0000417/// \brief Values for bit flags used in the ident_t to describe the fields.
418/// All enumeric elements are named and described in accordance with the code
419/// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
420enum OpenMPLocationFlags {
421 /// \brief Use trampoline for internal microtask.
422 OMP_IDENT_IMD = 0x01,
423 /// \brief Use c-style ident structure.
424 OMP_IDENT_KMPC = 0x02,
425 /// \brief Atomic reduction option for kmpc_reduce.
426 OMP_ATOMIC_REDUCE = 0x10,
427 /// \brief Explicit 'barrier' directive.
428 OMP_IDENT_BARRIER_EXPL = 0x20,
429 /// \brief Implicit barrier in code.
430 OMP_IDENT_BARRIER_IMPL = 0x40,
431 /// \brief Implicit barrier in 'for' directive.
432 OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
433 /// \brief Implicit barrier in 'sections' directive.
434 OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
435 /// \brief Implicit barrier in 'single' directive.
436 OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
437};
438
439/// \brief Describes ident structure that describes a source location.
440/// All descriptions are taken from
441/// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
442/// Original structure:
443/// typedef struct ident {
444/// kmp_int32 reserved_1; /**< might be used in Fortran;
445/// see above */
446/// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
447/// KMP_IDENT_KMPC identifies this union
448/// member */
449/// kmp_int32 reserved_2; /**< not really used in Fortran any more;
450/// see above */
451///#if USE_ITT_BUILD
452/// /* but currently used for storing
453/// region-specific ITT */
454/// /* contextual information. */
455///#endif /* USE_ITT_BUILD */
456/// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
457/// C++ */
458/// char const *psource; /**< String describing the source location.
459/// The string is composed of semi-colon separated
460// fields which describe the source file,
461/// the function and a pair of line numbers that
462/// delimit the construct.
463/// */
464/// } ident_t;
465enum IdentFieldIndex {
466 /// \brief might be used in Fortran
467 IdentField_Reserved_1,
468 /// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
469 IdentField_Flags,
470 /// \brief Not really used in Fortran any more
471 IdentField_Reserved_2,
472 /// \brief Source[4] in Fortran, do not use for C++
473 IdentField_Reserved_3,
474 /// \brief String describing the source location. The string is composed of
475 /// semi-colon separated fields which describe the source file, the function
476 /// and a pair of line numbers that delimit the construct.
477 IdentField_PSource
478};
479
480/// \brief Schedule types for 'omp for' loops (these enumerators are taken from
481/// the enum sched_type in kmp.h).
482enum OpenMPSchedType {
483 /// \brief Lower bound for default (unordered) versions.
484 OMP_sch_lower = 32,
485 OMP_sch_static_chunked = 33,
486 OMP_sch_static = 34,
487 OMP_sch_dynamic_chunked = 35,
488 OMP_sch_guided_chunked = 36,
489 OMP_sch_runtime = 37,
490 OMP_sch_auto = 38,
Alexey Bataev6cff6242016-05-30 13:05:14 +0000491 /// static with chunk adjustment (e.g., simd)
492 OMP_sch_static_balanced_chunked = 45,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000493 /// \brief Lower bound for 'ordered' versions.
494 OMP_ord_lower = 64,
495 OMP_ord_static_chunked = 65,
496 OMP_ord_static = 66,
497 OMP_ord_dynamic_chunked = 67,
498 OMP_ord_guided_chunked = 68,
499 OMP_ord_runtime = 69,
500 OMP_ord_auto = 70,
501 OMP_sch_default = OMP_sch_static,
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000502 /// \brief dist_schedule types
503 OMP_dist_sch_static_chunked = 91,
504 OMP_dist_sch_static = 92,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000505 /// Support for OpenMP 4.5 monotonic and nonmonotonic schedule modifiers.
506 /// Set if the monotonic schedule modifier was present.
507 OMP_sch_modifier_monotonic = (1 << 29),
508 /// Set if the nonmonotonic schedule modifier was present.
509 OMP_sch_modifier_nonmonotonic = (1 << 30),
Alexey Bataev50b3c952016-02-19 10:38:26 +0000510};
511
512enum OpenMPRTLFunction {
513 /// \brief Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc,
514 /// kmpc_micro microtask, ...);
515 OMPRTL__kmpc_fork_call,
516 /// \brief Call to void *__kmpc_threadprivate_cached(ident_t *loc,
517 /// kmp_int32 global_tid, void *data, size_t size, void ***cache);
518 OMPRTL__kmpc_threadprivate_cached,
519 /// \brief Call to void __kmpc_threadprivate_register( ident_t *,
520 /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
521 OMPRTL__kmpc_threadprivate_register,
522 // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
523 OMPRTL__kmpc_global_thread_num,
524 // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
525 // kmp_critical_name *crit);
526 OMPRTL__kmpc_critical,
527 // Call to void __kmpc_critical_with_hint(ident_t *loc, kmp_int32
528 // global_tid, kmp_critical_name *crit, uintptr_t hint);
529 OMPRTL__kmpc_critical_with_hint,
530 // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
531 // kmp_critical_name *crit);
532 OMPRTL__kmpc_end_critical,
533 // Call to kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32
534 // global_tid);
535 OMPRTL__kmpc_cancel_barrier,
536 // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
537 OMPRTL__kmpc_barrier,
538 // Call to void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
539 OMPRTL__kmpc_for_static_fini,
540 // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
541 // global_tid);
542 OMPRTL__kmpc_serialized_parallel,
543 // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
544 // global_tid);
545 OMPRTL__kmpc_end_serialized_parallel,
546 // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
547 // kmp_int32 num_threads);
548 OMPRTL__kmpc_push_num_threads,
549 // Call to void __kmpc_flush(ident_t *loc);
550 OMPRTL__kmpc_flush,
551 // Call to kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid);
552 OMPRTL__kmpc_master,
553 // Call to void __kmpc_end_master(ident_t *, kmp_int32 global_tid);
554 OMPRTL__kmpc_end_master,
555 // Call to kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid,
556 // int end_part);
557 OMPRTL__kmpc_omp_taskyield,
558 // Call to kmp_int32 __kmpc_single(ident_t *, kmp_int32 global_tid);
559 OMPRTL__kmpc_single,
560 // Call to void __kmpc_end_single(ident_t *, kmp_int32 global_tid);
561 OMPRTL__kmpc_end_single,
562 // Call to kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
563 // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
564 // kmp_routine_entry_t *task_entry);
565 OMPRTL__kmpc_omp_task_alloc,
566 // Call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t *
567 // new_task);
568 OMPRTL__kmpc_omp_task,
569 // Call to void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid,
570 // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *),
571 // kmp_int32 didit);
572 OMPRTL__kmpc_copyprivate,
573 // Call to kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid,
574 // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void
575 // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck);
576 OMPRTL__kmpc_reduce,
577 // Call to kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32
578 // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data,
579 // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name
580 // *lck);
581 OMPRTL__kmpc_reduce_nowait,
582 // Call to void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
583 // kmp_critical_name *lck);
584 OMPRTL__kmpc_end_reduce,
585 // Call to void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
586 // kmp_critical_name *lck);
587 OMPRTL__kmpc_end_reduce_nowait,
588 // Call to void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid,
589 // kmp_task_t * new_task);
590 OMPRTL__kmpc_omp_task_begin_if0,
591 // Call to void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid,
592 // kmp_task_t * new_task);
593 OMPRTL__kmpc_omp_task_complete_if0,
594 // Call to void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid);
595 OMPRTL__kmpc_ordered,
596 // Call to void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid);
597 OMPRTL__kmpc_end_ordered,
598 // Call to kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32
599 // global_tid);
600 OMPRTL__kmpc_omp_taskwait,
601 // Call to void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid);
602 OMPRTL__kmpc_taskgroup,
603 // Call to void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid);
604 OMPRTL__kmpc_end_taskgroup,
605 // Call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid,
606 // int proc_bind);
607 OMPRTL__kmpc_push_proc_bind,
608 // Call to kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32
609 // gtid, kmp_task_t * new_task, kmp_int32 ndeps, kmp_depend_info_t
610 // *dep_list, kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
611 OMPRTL__kmpc_omp_task_with_deps,
612 // Call to void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32
613 // gtid, kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32
614 // ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
615 OMPRTL__kmpc_omp_wait_deps,
616 // Call to kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32
617 // global_tid, kmp_int32 cncl_kind);
618 OMPRTL__kmpc_cancellationpoint,
619 // Call to kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid,
620 // kmp_int32 cncl_kind);
621 OMPRTL__kmpc_cancel,
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000622 // Call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32 global_tid,
623 // kmp_int32 num_teams, kmp_int32 thread_limit);
624 OMPRTL__kmpc_push_num_teams,
Alexey Bataev7292c292016-04-25 12:22:29 +0000625 // Call to void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro
626 // microtask, ...);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000627 OMPRTL__kmpc_fork_teams,
Alexey Bataev7292c292016-04-25 12:22:29 +0000628 // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int
629 // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int
630 // sched, kmp_uint64 grainsize, void *task_dup);
631 OMPRTL__kmpc_taskloop,
Alexey Bataev8b427062016-05-25 12:36:08 +0000632 // Call to void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32
633 // num_dims, struct kmp_dim *dims);
634 OMPRTL__kmpc_doacross_init,
635 // Call to void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid);
636 OMPRTL__kmpc_doacross_fini,
637 // Call to void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64
638 // *vec);
639 OMPRTL__kmpc_doacross_post,
640 // Call to void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64
641 // *vec);
642 OMPRTL__kmpc_doacross_wait,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000643
644 //
645 // Offloading related calls
646 //
647 // Call to int32_t __tgt_target(int32_t device_id, void *host_ptr, int32_t
648 // arg_num, void** args_base, void **args, size_t *arg_sizes, int32_t
649 // *arg_types);
650 OMPRTL__tgt_target,
Samuel Antaob68e2db2016-03-03 16:20:23 +0000651 // Call to int32_t __tgt_target_teams(int32_t device_id, void *host_ptr,
652 // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes,
653 // int32_t *arg_types, int32_t num_teams, int32_t thread_limit);
654 OMPRTL__tgt_target_teams,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000655 // Call to void __tgt_register_lib(__tgt_bin_desc *desc);
656 OMPRTL__tgt_register_lib,
657 // Call to void __tgt_unregister_lib(__tgt_bin_desc *desc);
658 OMPRTL__tgt_unregister_lib,
Samuel Antaodf158d52016-04-27 22:58:19 +0000659 // Call to void __tgt_target_data_begin(int32_t device_id, int32_t arg_num,
660 // void** args_base, void **args, size_t *arg_sizes, int32_t *arg_types);
661 OMPRTL__tgt_target_data_begin,
662 // Call to void __tgt_target_data_end(int32_t device_id, int32_t arg_num,
663 // void** args_base, void **args, size_t *arg_sizes, int32_t *arg_types);
664 OMPRTL__tgt_target_data_end,
Samuel Antao8d2d7302016-05-26 18:30:22 +0000665 // Call to void __tgt_target_data_update(int32_t device_id, int32_t arg_num,
666 // void** args_base, void **args, size_t *arg_sizes, int32_t *arg_types);
667 OMPRTL__tgt_target_data_update,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000668};
669
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000670/// A basic class for pre|post-action for advanced codegen sequence for OpenMP
671/// region.
672class CleanupTy final : public EHScopeStack::Cleanup {
673 PrePostActionTy *Action;
674
675public:
676 explicit CleanupTy(PrePostActionTy *Action) : Action(Action) {}
677 void Emit(CodeGenFunction &CGF, Flags /*flags*/) override {
678 if (!CGF.HaveInsertPoint())
679 return;
680 Action->Exit(CGF);
681 }
682};
683
Hans Wennborg7eb54642015-09-10 17:07:54 +0000684} // anonymous namespace
Alexey Bataev18095712014-10-10 12:19:54 +0000685
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000686void RegionCodeGenTy::operator()(CodeGenFunction &CGF) const {
687 CodeGenFunction::RunCleanupsScope Scope(CGF);
688 if (PrePostAction) {
689 CGF.EHStack.pushCleanup<CleanupTy>(NormalAndEHCleanup, PrePostAction);
690 Callback(CodeGen, CGF, *PrePostAction);
691 } else {
692 PrePostActionTy Action;
693 Callback(CodeGen, CGF, Action);
694 }
695}
696
Alexey Bataev18095712014-10-10 12:19:54 +0000697LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) {
Alexey Bataev31300ed2016-02-04 11:27:03 +0000698 return CGF.EmitLoadOfPointerLValue(
699 CGF.GetAddrOfLocalVar(getThreadIDVariable()),
700 getThreadIDVariable()->getType()->castAs<PointerType>());
Alexey Bataev18095712014-10-10 12:19:54 +0000701}
702
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000703void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, const Stmt * /*S*/) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000704 if (!CGF.HaveInsertPoint())
705 return;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000706 // 1.2.2 OpenMP Language Terminology
707 // Structured block - An executable statement with a single entry at the
708 // top and a single exit at the bottom.
709 // The point of exit cannot be a branch out of the structured block.
710 // longjmp() and throw() must not violate the entry/exit criteria.
711 CGF.EHStack.pushTerminate();
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000712 CodeGen(CGF);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000713 CGF.EHStack.popTerminate();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000714}
715
Alexey Bataev62b63b12015-03-10 07:28:44 +0000716LValue CGOpenMPTaskOutlinedRegionInfo::getThreadIDVariableLValue(
717 CodeGenFunction &CGF) {
Alexey Bataev2377fe92015-09-10 08:12:02 +0000718 return CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(getThreadIDVariable()),
719 getThreadIDVariable()->getType(),
720 AlignmentSource::Decl);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000721}
722
Alexey Bataev9959db52014-05-06 10:08:46 +0000723CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM)
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000724 : CGM(CGM), OffloadEntriesInfoManager(CGM) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000725 IdentTy = llvm::StructType::create(
726 "ident_t", CGM.Int32Ty /* reserved_1 */, CGM.Int32Ty /* flags */,
727 CGM.Int32Ty /* reserved_2 */, CGM.Int32Ty /* reserved_3 */,
Alexander Musmanfdfa8552014-09-11 08:10:57 +0000728 CGM.Int8PtrTy /* psource */, nullptr);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000729 KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8);
Samuel Antaoee8fb302016-01-06 13:42:12 +0000730
731 loadOffloadInfoMetadata();
Alexey Bataev9959db52014-05-06 10:08:46 +0000732}
733
Alexey Bataev91797552015-03-18 04:13:55 +0000734void CGOpenMPRuntime::clear() {
735 InternalVars.clear();
736}
737
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000738static llvm::Function *
739emitCombinerOrInitializer(CodeGenModule &CGM, QualType Ty,
740 const Expr *CombinerInitializer, const VarDecl *In,
741 const VarDecl *Out, bool IsCombiner) {
742 // void .omp_combiner.(Ty *in, Ty *out);
743 auto &C = CGM.getContext();
744 QualType PtrTy = C.getPointerType(Ty).withRestrict();
745 FunctionArgList Args;
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000746 ImplicitParamDecl OmpOutParm(C, /*DC=*/nullptr, Out->getLocation(),
747 /*Id=*/nullptr, PtrTy);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000748 ImplicitParamDecl OmpInParm(C, /*DC=*/nullptr, In->getLocation(),
749 /*Id=*/nullptr, PtrTy);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000750 Args.push_back(&OmpOutParm);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000751 Args.push_back(&OmpInParm);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000752 auto &FnInfo =
John McCallc56a8b32016-03-11 04:30:31 +0000753 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000754 auto *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
755 auto *Fn = llvm::Function::Create(
756 FnTy, llvm::GlobalValue::InternalLinkage,
757 IsCombiner ? ".omp_combiner." : ".omp_initializer.", &CGM.getModule());
758 CGM.SetInternalFunctionAttributes(/*D=*/nullptr, Fn, FnInfo);
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000759 Fn->addFnAttr(llvm::Attribute::AlwaysInline);
Alexey Bataevc5b1d322016-03-04 09:22:22 +0000760 CodeGenFunction CGF(CGM);
761 // Map "T omp_in;" variable to "*omp_in_parm" value in all expressions.
762 // Map "T omp_out;" variable to "*omp_out_parm" value in all expressions.
763 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args);
764 CodeGenFunction::OMPPrivateScope Scope(CGF);
765 Address AddrIn = CGF.GetAddrOfLocalVar(&OmpInParm);
766 Scope.addPrivate(In, [&CGF, AddrIn, PtrTy]() -> Address {
767 return CGF.EmitLoadOfPointerLValue(AddrIn, PtrTy->castAs<PointerType>())
768 .getAddress();
769 });
770 Address AddrOut = CGF.GetAddrOfLocalVar(&OmpOutParm);
771 Scope.addPrivate(Out, [&CGF, AddrOut, PtrTy]() -> Address {
772 return CGF.EmitLoadOfPointerLValue(AddrOut, PtrTy->castAs<PointerType>())
773 .getAddress();
774 });
775 (void)Scope.Privatize();
776 CGF.EmitIgnoredExpr(CombinerInitializer);
777 Scope.ForceCleanup();
778 CGF.FinishFunction();
779 return Fn;
780}
781
782void CGOpenMPRuntime::emitUserDefinedReduction(
783 CodeGenFunction *CGF, const OMPDeclareReductionDecl *D) {
784 if (UDRMap.count(D) > 0)
785 return;
786 auto &C = CGM.getContext();
787 if (!In || !Out) {
788 In = &C.Idents.get("omp_in");
789 Out = &C.Idents.get("omp_out");
790 }
791 llvm::Function *Combiner = emitCombinerOrInitializer(
792 CGM, D->getType(), D->getCombiner(), cast<VarDecl>(D->lookup(In).front()),
793 cast<VarDecl>(D->lookup(Out).front()),
794 /*IsCombiner=*/true);
795 llvm::Function *Initializer = nullptr;
796 if (auto *Init = D->getInitializer()) {
797 if (!Priv || !Orig) {
798 Priv = &C.Idents.get("omp_priv");
799 Orig = &C.Idents.get("omp_orig");
800 }
801 Initializer = emitCombinerOrInitializer(
802 CGM, D->getType(), Init, cast<VarDecl>(D->lookup(Orig).front()),
803 cast<VarDecl>(D->lookup(Priv).front()),
804 /*IsCombiner=*/false);
805 }
806 UDRMap.insert(std::make_pair(D, std::make_pair(Combiner, Initializer)));
807 if (CGF) {
808 auto &Decls = FunctionUDRMap.FindAndConstruct(CGF->CurFn);
809 Decls.second.push_back(D);
810 }
811}
812
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000813std::pair<llvm::Function *, llvm::Function *>
814CGOpenMPRuntime::getUserDefinedReduction(const OMPDeclareReductionDecl *D) {
815 auto I = UDRMap.find(D);
816 if (I != UDRMap.end())
817 return I->second;
818 emitUserDefinedReduction(/*CGF=*/nullptr, D);
819 return UDRMap.lookup(D);
820}
821
John McCall7f416cc2015-09-08 08:05:57 +0000822// Layout information for ident_t.
823static CharUnits getIdentAlign(CodeGenModule &CGM) {
824 return CGM.getPointerAlign();
825}
826static CharUnits getIdentSize(CodeGenModule &CGM) {
827 assert((4 * CGM.getPointerSize()).isMultipleOf(CGM.getPointerAlign()));
828 return CharUnits::fromQuantity(16) + CGM.getPointerSize();
829}
Alexey Bataev50b3c952016-02-19 10:38:26 +0000830static CharUnits getOffsetOfIdentField(IdentFieldIndex Field) {
John McCall7f416cc2015-09-08 08:05:57 +0000831 // All the fields except the last are i32, so this works beautifully.
832 return unsigned(Field) * CharUnits::fromQuantity(4);
833}
834static Address createIdentFieldGEP(CodeGenFunction &CGF, Address Addr,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000835 IdentFieldIndex Field,
John McCall7f416cc2015-09-08 08:05:57 +0000836 const llvm::Twine &Name = "") {
837 auto Offset = getOffsetOfIdentField(Field);
838 return CGF.Builder.CreateStructGEP(Addr, Field, Offset, Name);
839}
840
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000841llvm::Value *CGOpenMPRuntime::emitParallelOrTeamsOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000842 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
843 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
Alexey Bataev62b63b12015-03-10 07:28:44 +0000844 assert(ThreadIDVar->getType()->isPointerType() &&
845 "thread id variable must be of type kmp_int32 *");
Alexey Bataev18095712014-10-10 12:19:54 +0000846 const CapturedStmt *CS = cast<CapturedStmt>(D.getAssociatedStmt());
847 CodeGenFunction CGF(CGM, true);
Alexey Bataev25e5b442015-09-15 12:52:43 +0000848 bool HasCancel = false;
849 if (auto *OPD = dyn_cast<OMPParallelDirective>(&D))
850 HasCancel = OPD->hasCancel();
851 else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&D))
852 HasCancel = OPSD->hasCancel();
853 else if (auto *OPFD = dyn_cast<OMPParallelForDirective>(&D))
854 HasCancel = OPFD->hasCancel();
855 CGOpenMPOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, InnermostKind,
856 HasCancel);
Alexey Bataevd157d472015-06-24 03:35:38 +0000857 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000858 return CGF.GenerateOpenMPCapturedStmtFunction(*CS);
Alexey Bataev18095712014-10-10 12:19:54 +0000859}
860
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000861llvm::Value *CGOpenMPRuntime::emitTaskOutlinedFunction(
862 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000863 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
864 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
865 bool Tied, unsigned &NumberOfParts) {
866 auto &&UntiedCodeGen = [this, &D, TaskTVar](CodeGenFunction &CGF,
867 PrePostActionTy &) {
868 auto *ThreadID = getThreadID(CGF, D.getLocStart());
869 auto *UpLoc = emitUpdateLocation(CGF, D.getLocStart());
870 llvm::Value *TaskArgs[] = {
871 UpLoc, ThreadID,
872 CGF.EmitLoadOfPointerLValue(CGF.GetAddrOfLocalVar(TaskTVar),
873 TaskTVar->getType()->castAs<PointerType>())
874 .getPointer()};
875 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), TaskArgs);
876 };
877 CGOpenMPTaskOutlinedRegionInfo::UntiedTaskActionTy Action(Tied, PartIDVar,
878 UntiedCodeGen);
879 CodeGen.setAction(Action);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000880 assert(!ThreadIDVar->getType()->isPointerType() &&
881 "thread id variable must be of type kmp_int32 for tasks");
882 auto *CS = cast<CapturedStmt>(D.getAssociatedStmt());
Alexey Bataev7292c292016-04-25 12:22:29 +0000883 auto *TD = dyn_cast<OMPTaskDirective>(&D);
Alexey Bataev62b63b12015-03-10 07:28:44 +0000884 CodeGenFunction CGF(CGM, true);
Alexey Bataev7292c292016-04-25 12:22:29 +0000885 CGOpenMPTaskOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen,
886 InnermostKind,
887 TD ? TD->hasCancel() : false, Action);
Alexey Bataevd157d472015-06-24 03:35:38 +0000888 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
Alexey Bataev48591dd2016-04-20 04:01:36 +0000889 auto *Res = CGF.GenerateCapturedStmtFunction(*CS);
890 if (!Tied)
891 NumberOfParts = Action.getNumberOfParts();
892 return Res;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000893}
894
Alexey Bataev50b3c952016-02-19 10:38:26 +0000895Address CGOpenMPRuntime::getOrCreateDefaultLocation(unsigned Flags) {
John McCall7f416cc2015-09-08 08:05:57 +0000896 CharUnits Align = getIdentAlign(CGM);
Alexey Bataev15007ba2014-05-07 06:18:01 +0000897 llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags);
Alexey Bataev9959db52014-05-06 10:08:46 +0000898 if (!Entry) {
899 if (!DefaultOpenMPPSource) {
900 // Initialize default location for psource field of ident_t structure of
901 // all ident_t objects. Format is ";file;function;line;column;;".
902 // Taken from
903 // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp_str.c
904 DefaultOpenMPPSource =
John McCall7f416cc2015-09-08 08:05:57 +0000905 CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;").getPointer();
Alexey Bataev9959db52014-05-06 10:08:46 +0000906 DefaultOpenMPPSource =
907 llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy);
908 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000909 auto DefaultOpenMPLocation = new llvm::GlobalVariable(
910 CGM.getModule(), IdentTy, /*isConstant*/ true,
911 llvm::GlobalValue::PrivateLinkage, /*Initializer*/ nullptr);
Peter Collingbournebcf909d2016-06-14 21:02:05 +0000912 DefaultOpenMPLocation->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
John McCall7f416cc2015-09-08 08:05:57 +0000913 DefaultOpenMPLocation->setAlignment(Align.getQuantity());
Alexey Bataev9959db52014-05-06 10:08:46 +0000914
915 llvm::Constant *Zero = llvm::ConstantInt::get(CGM.Int32Ty, 0, true);
Alexey Bataev23b69422014-06-18 07:08:49 +0000916 llvm::Constant *Values[] = {Zero,
917 llvm::ConstantInt::get(CGM.Int32Ty, Flags),
918 Zero, Zero, DefaultOpenMPPSource};
Alexey Bataev9959db52014-05-06 10:08:46 +0000919 llvm::Constant *Init = llvm::ConstantStruct::get(IdentTy, Values);
920 DefaultOpenMPLocation->setInitializer(Init);
John McCall7f416cc2015-09-08 08:05:57 +0000921 OpenMPDefaultLocMap[Flags] = Entry = DefaultOpenMPLocation;
Alexey Bataev9959db52014-05-06 10:08:46 +0000922 }
John McCall7f416cc2015-09-08 08:05:57 +0000923 return Address(Entry, Align);
Alexey Bataev9959db52014-05-06 10:08:46 +0000924}
925
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000926llvm::Value *CGOpenMPRuntime::emitUpdateLocation(CodeGenFunction &CGF,
927 SourceLocation Loc,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000928 unsigned Flags) {
929 Flags |= OMP_IDENT_KMPC;
Alexey Bataev9959db52014-05-06 10:08:46 +0000930 // If no debug info is generated - return global default location.
Benjamin Kramer8c305922016-02-02 11:06:51 +0000931 if (CGM.getCodeGenOpts().getDebugInfo() == codegenoptions::NoDebugInfo ||
Alexey Bataev9959db52014-05-06 10:08:46 +0000932 Loc.isInvalid())
John McCall7f416cc2015-09-08 08:05:57 +0000933 return getOrCreateDefaultLocation(Flags).getPointer();
Alexey Bataev9959db52014-05-06 10:08:46 +0000934
935 assert(CGF.CurFn && "No function in current CodeGenFunction.");
936
John McCall7f416cc2015-09-08 08:05:57 +0000937 Address LocValue = Address::invalid();
Alexey Bataev1e4b7132014-12-03 12:11:24 +0000938 auto I = OpenMPLocThreadIDMap.find(CGF.CurFn);
939 if (I != OpenMPLocThreadIDMap.end())
John McCall7f416cc2015-09-08 08:05:57 +0000940 LocValue = Address(I->second.DebugLoc, getIdentAlign(CGF.CGM));
941
Alexander Musmanc6388682014-12-15 07:07:06 +0000942 // OpenMPLocThreadIDMap may have null DebugLoc and non-null ThreadID, if
943 // GetOpenMPThreadID was called before this routine.
John McCall7f416cc2015-09-08 08:05:57 +0000944 if (!LocValue.isValid()) {
Alexey Bataev15007ba2014-05-07 06:18:01 +0000945 // Generate "ident_t .kmpc_loc.addr;"
John McCall7f416cc2015-09-08 08:05:57 +0000946 Address AI = CGF.CreateTempAlloca(IdentTy, getIdentAlign(CGF.CGM),
947 ".kmpc_loc.addr");
Alexey Bataev18095712014-10-10 12:19:54 +0000948 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
John McCall7f416cc2015-09-08 08:05:57 +0000949 Elem.second.DebugLoc = AI.getPointer();
Alexey Bataev9959db52014-05-06 10:08:46 +0000950 LocValue = AI;
951
952 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
953 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000954 CGF.Builder.CreateMemCpy(LocValue, getOrCreateDefaultLocation(Flags),
John McCall7f416cc2015-09-08 08:05:57 +0000955 CGM.getSize(getIdentSize(CGF.CGM)));
Alexey Bataev9959db52014-05-06 10:08:46 +0000956 }
957
958 // char **psource = &.kmpc_loc_<flags>.addr.psource;
John McCall7f416cc2015-09-08 08:05:57 +0000959 Address PSource = createIdentFieldGEP(CGF, LocValue, IdentField_PSource);
Alexey Bataev9959db52014-05-06 10:08:46 +0000960
Alexey Bataevf002aca2014-05-30 05:48:40 +0000961 auto OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding());
962 if (OMPDebugLoc == nullptr) {
963 SmallString<128> Buffer2;
964 llvm::raw_svector_ostream OS2(Buffer2);
965 // Build debug location
966 PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc);
967 OS2 << ";" << PLoc.getFilename() << ";";
968 if (const FunctionDecl *FD =
969 dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) {
970 OS2 << FD->getQualifiedNameAsString();
971 }
972 OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;";
973 OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str());
974 OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc;
Alexey Bataev9959db52014-05-06 10:08:46 +0000975 }
Alexey Bataev9959db52014-05-06 10:08:46 +0000976 // *psource = ";<File>;<Function>;<Line>;<Column>;;";
Alexey Bataevf002aca2014-05-30 05:48:40 +0000977 CGF.Builder.CreateStore(OMPDebugLoc, PSource);
978
John McCall7f416cc2015-09-08 08:05:57 +0000979 // Our callers always pass this to a runtime function, so for
980 // convenience, go ahead and return a naked pointer.
981 return LocValue.getPointer();
Alexey Bataev9959db52014-05-06 10:08:46 +0000982}
983
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000984llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF,
985 SourceLocation Loc) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000986 assert(CGF.CurFn && "No function in current CodeGenFunction.");
987
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000988 llvm::Value *ThreadID = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000989 // Check whether we've already cached a load of the thread id in this
990 // function.
Alexey Bataev1e4b7132014-12-03 12:11:24 +0000991 auto I = OpenMPLocThreadIDMap.find(CGF.CurFn);
Alexey Bataev18095712014-10-10 12:19:54 +0000992 if (I != OpenMPLocThreadIDMap.end()) {
993 ThreadID = I->second.ThreadID;
Alexey Bataev03b340a2014-10-21 03:16:40 +0000994 if (ThreadID != nullptr)
995 return ThreadID;
996 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +0000997 if (auto *OMPRegionInfo =
Alexey Bataev1e4b7132014-12-03 12:11:24 +0000998 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
Alexey Bataev62b63b12015-03-10 07:28:44 +0000999 if (OMPRegionInfo->getThreadIDVariable()) {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001000 // Check if this an outlined function with thread id passed as argument.
1001 auto LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF);
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001002 ThreadID = CGF.EmitLoadOfLValue(LVal, Loc).getScalarVal();
1003 // If value loaded in entry block, cache it and use it everywhere in
1004 // function.
1005 if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) {
1006 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
1007 Elem.second.ThreadID = ThreadID;
1008 }
1009 return ThreadID;
Alexey Bataevd6c57552014-07-25 07:55:17 +00001010 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001011 }
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001012
1013 // This is not an outlined function region - need to call __kmpc_int32
1014 // kmpc_global_thread_num(ident_t *loc).
1015 // Generate thread id value and cache this value for use across the
1016 // function.
1017 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
1018 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
1019 ThreadID =
1020 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num),
1021 emitUpdateLocation(CGF, Loc));
1022 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
1023 Elem.second.ThreadID = ThreadID;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001024 return ThreadID;
Alexey Bataev9959db52014-05-06 10:08:46 +00001025}
1026
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001027void CGOpenMPRuntime::functionFinished(CodeGenFunction &CGF) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001028 assert(CGF.CurFn && "No function in current CodeGenFunction.");
Alexey Bataev03b340a2014-10-21 03:16:40 +00001029 if (OpenMPLocThreadIDMap.count(CGF.CurFn))
1030 OpenMPLocThreadIDMap.erase(CGF.CurFn);
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001031 if (FunctionUDRMap.count(CGF.CurFn) > 0) {
1032 for(auto *D : FunctionUDRMap[CGF.CurFn]) {
1033 UDRMap.erase(D);
1034 }
1035 FunctionUDRMap.erase(CGF.CurFn);
1036 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001037}
1038
1039llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001040 if (!IdentTy) {
1041 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001042 return llvm::PointerType::getUnqual(IdentTy);
1043}
1044
1045llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001046 if (!Kmpc_MicroTy) {
1047 // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...)
1048 llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty),
1049 llvm::PointerType::getUnqual(CGM.Int32Ty)};
1050 Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true);
1051 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001052 return llvm::PointerType::getUnqual(Kmpc_MicroTy);
1053}
1054
1055llvm::Constant *
Alexey Bataev50b3c952016-02-19 10:38:26 +00001056CGOpenMPRuntime::createRuntimeFunction(unsigned Function) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001057 llvm::Constant *RTLFn = nullptr;
Alexey Bataev50b3c952016-02-19 10:38:26 +00001058 switch (static_cast<OpenMPRTLFunction>(Function)) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001059 case OMPRTL__kmpc_fork_call: {
1060 // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro
1061 // microtask, ...);
Alexey Bataev23b69422014-06-18 07:08:49 +00001062 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1063 getKmpc_MicroPointerTy()};
Alexey Bataev9959db52014-05-06 10:08:46 +00001064 llvm::FunctionType *FnTy =
Alexey Bataevd74d0602014-10-13 06:02:40 +00001065 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true);
Alexey Bataev9959db52014-05-06 10:08:46 +00001066 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call");
1067 break;
1068 }
1069 case OMPRTL__kmpc_global_thread_num: {
1070 // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc);
Alexey Bataev23b69422014-06-18 07:08:49 +00001071 llvm::Type *TypeParams[] = {getIdentTyPointerTy()};
Alexey Bataev9959db52014-05-06 10:08:46 +00001072 llvm::FunctionType *FnTy =
Alexey Bataevd74d0602014-10-13 06:02:40 +00001073 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
Alexey Bataev9959db52014-05-06 10:08:46 +00001074 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num");
1075 break;
1076 }
Alexey Bataev97720002014-11-11 04:05:39 +00001077 case OMPRTL__kmpc_threadprivate_cached: {
1078 // Build void *__kmpc_threadprivate_cached(ident_t *loc,
1079 // kmp_int32 global_tid, void *data, size_t size, void ***cache);
1080 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1081 CGM.VoidPtrTy, CGM.SizeTy,
1082 CGM.VoidPtrTy->getPointerTo()->getPointerTo()};
1083 llvm::FunctionType *FnTy =
1084 llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg*/ false);
1085 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_cached");
1086 break;
1087 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001088 case OMPRTL__kmpc_critical: {
Alexey Bataevf9472182014-09-22 12:32:31 +00001089 // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
1090 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001091 llvm::Type *TypeParams[] = {
1092 getIdentTyPointerTy(), CGM.Int32Ty,
1093 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
1094 llvm::FunctionType *FnTy =
1095 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1096 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical");
1097 break;
1098 }
Alexey Bataevfc57d162015-12-15 10:55:09 +00001099 case OMPRTL__kmpc_critical_with_hint: {
1100 // Build void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 global_tid,
1101 // kmp_critical_name *crit, uintptr_t hint);
1102 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1103 llvm::PointerType::getUnqual(KmpCriticalNameTy),
1104 CGM.IntPtrTy};
1105 llvm::FunctionType *FnTy =
1106 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1107 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical_with_hint");
1108 break;
1109 }
Alexey Bataev97720002014-11-11 04:05:39 +00001110 case OMPRTL__kmpc_threadprivate_register: {
1111 // Build void __kmpc_threadprivate_register(ident_t *, void *data,
1112 // kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
1113 // typedef void *(*kmpc_ctor)(void *);
1114 auto KmpcCtorTy =
1115 llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy,
1116 /*isVarArg*/ false)->getPointerTo();
1117 // typedef void *(*kmpc_cctor)(void *, void *);
1118 llvm::Type *KmpcCopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
1119 auto KmpcCopyCtorTy =
1120 llvm::FunctionType::get(CGM.VoidPtrTy, KmpcCopyCtorTyArgs,
1121 /*isVarArg*/ false)->getPointerTo();
1122 // typedef void (*kmpc_dtor)(void *);
1123 auto KmpcDtorTy =
1124 llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, /*isVarArg*/ false)
1125 ->getPointerTo();
1126 llvm::Type *FnTyArgs[] = {getIdentTyPointerTy(), CGM.VoidPtrTy, KmpcCtorTy,
1127 KmpcCopyCtorTy, KmpcDtorTy};
1128 auto FnTy = llvm::FunctionType::get(CGM.VoidTy, FnTyArgs,
1129 /*isVarArg*/ false);
1130 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_register");
1131 break;
1132 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001133 case OMPRTL__kmpc_end_critical: {
Alexey Bataevf9472182014-09-22 12:32:31 +00001134 // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
1135 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001136 llvm::Type *TypeParams[] = {
1137 getIdentTyPointerTy(), CGM.Int32Ty,
1138 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
1139 llvm::FunctionType *FnTy =
1140 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1141 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical");
1142 break;
1143 }
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00001144 case OMPRTL__kmpc_cancel_barrier: {
1145 // Build kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32
1146 // global_tid);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001147 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1148 llvm::FunctionType *FnTy =
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00001149 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1150 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_cancel_barrier");
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001151 break;
1152 }
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001153 case OMPRTL__kmpc_barrier: {
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001154 // Build void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001155 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1156 llvm::FunctionType *FnTy =
1157 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1158 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_barrier");
1159 break;
1160 }
Alexander Musmanc6388682014-12-15 07:07:06 +00001161 case OMPRTL__kmpc_for_static_fini: {
1162 // Build void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
1163 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1164 llvm::FunctionType *FnTy =
1165 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1166 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_fini");
1167 break;
1168 }
Alexey Bataevb2059782014-10-13 08:23:51 +00001169 case OMPRTL__kmpc_push_num_threads: {
1170 // Build void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
1171 // kmp_int32 num_threads)
1172 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1173 CGM.Int32Ty};
1174 llvm::FunctionType *FnTy =
1175 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1176 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_threads");
1177 break;
1178 }
Alexey Bataevd74d0602014-10-13 06:02:40 +00001179 case OMPRTL__kmpc_serialized_parallel: {
1180 // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
1181 // global_tid);
1182 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1183 llvm::FunctionType *FnTy =
1184 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1185 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel");
1186 break;
1187 }
1188 case OMPRTL__kmpc_end_serialized_parallel: {
1189 // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
1190 // global_tid);
1191 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1192 llvm::FunctionType *FnTy =
1193 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1194 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel");
1195 break;
1196 }
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001197 case OMPRTL__kmpc_flush: {
Alexey Bataevd76df6d2015-02-24 12:55:09 +00001198 // Build void __kmpc_flush(ident_t *loc);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001199 llvm::Type *TypeParams[] = {getIdentTyPointerTy()};
1200 llvm::FunctionType *FnTy =
Alexey Bataevd76df6d2015-02-24 12:55:09 +00001201 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001202 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_flush");
1203 break;
1204 }
Alexey Bataev8d690652014-12-04 07:23:53 +00001205 case OMPRTL__kmpc_master: {
1206 // Build kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid);
1207 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1208 llvm::FunctionType *FnTy =
1209 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1210 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_master");
1211 break;
1212 }
1213 case OMPRTL__kmpc_end_master: {
1214 // Build void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid);
1215 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1216 llvm::FunctionType *FnTy =
1217 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1218 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_master");
1219 break;
1220 }
Alexey Bataev9f797f32015-02-05 05:57:51 +00001221 case OMPRTL__kmpc_omp_taskyield: {
1222 // Build kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid,
1223 // int end_part);
1224 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy};
1225 llvm::FunctionType *FnTy =
1226 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1227 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_taskyield");
1228 break;
1229 }
Alexey Bataev6956e2e2015-02-05 06:35:41 +00001230 case OMPRTL__kmpc_single: {
1231 // Build kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid);
1232 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1233 llvm::FunctionType *FnTy =
1234 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1235 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_single");
1236 break;
1237 }
1238 case OMPRTL__kmpc_end_single: {
1239 // Build void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid);
1240 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1241 llvm::FunctionType *FnTy =
1242 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1243 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_single");
1244 break;
1245 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00001246 case OMPRTL__kmpc_omp_task_alloc: {
1247 // Build kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
1248 // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1249 // kmp_routine_entry_t *task_entry);
1250 assert(KmpRoutineEntryPtrTy != nullptr &&
1251 "Type kmp_routine_entry_t must be created.");
1252 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty,
1253 CGM.SizeTy, CGM.SizeTy, KmpRoutineEntryPtrTy};
1254 // Return void * and then cast to particular kmp_task_t type.
1255 llvm::FunctionType *FnTy =
1256 llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false);
1257 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_alloc");
1258 break;
1259 }
1260 case OMPRTL__kmpc_omp_task: {
1261 // Build kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t
1262 // *new_task);
1263 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1264 CGM.VoidPtrTy};
1265 llvm::FunctionType *FnTy =
1266 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1267 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task");
1268 break;
1269 }
Alexey Bataeva63048e2015-03-23 06:18:07 +00001270 case OMPRTL__kmpc_copyprivate: {
1271 // Build void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid,
Alexey Bataev66beaa92015-04-30 03:47:32 +00001272 // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *),
Alexey Bataeva63048e2015-03-23 06:18:07 +00001273 // kmp_int32 didit);
1274 llvm::Type *CpyTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
1275 auto *CpyFnTy =
1276 llvm::FunctionType::get(CGM.VoidTy, CpyTypeParams, /*isVarArg=*/false);
Alexey Bataev66beaa92015-04-30 03:47:32 +00001277 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.SizeTy,
Alexey Bataeva63048e2015-03-23 06:18:07 +00001278 CGM.VoidPtrTy, CpyFnTy->getPointerTo(),
1279 CGM.Int32Ty};
1280 llvm::FunctionType *FnTy =
1281 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1282 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_copyprivate");
1283 break;
1284 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001285 case OMPRTL__kmpc_reduce: {
1286 // Build kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid,
1287 // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void
1288 // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck);
1289 llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
1290 auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams,
1291 /*isVarArg=*/false);
1292 llvm::Type *TypeParams[] = {
1293 getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy,
1294 CGM.VoidPtrTy, ReduceFnTy->getPointerTo(),
1295 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
1296 llvm::FunctionType *FnTy =
1297 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1298 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce");
1299 break;
1300 }
1301 case OMPRTL__kmpc_reduce_nowait: {
1302 // Build kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32
1303 // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data,
1304 // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name
1305 // *lck);
1306 llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
1307 auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams,
1308 /*isVarArg=*/false);
1309 llvm::Type *TypeParams[] = {
1310 getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy,
1311 CGM.VoidPtrTy, ReduceFnTy->getPointerTo(),
1312 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
1313 llvm::FunctionType *FnTy =
1314 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1315 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce_nowait");
1316 break;
1317 }
1318 case OMPRTL__kmpc_end_reduce: {
1319 // Build void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
1320 // kmp_critical_name *lck);
1321 llvm::Type *TypeParams[] = {
1322 getIdentTyPointerTy(), CGM.Int32Ty,
1323 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
1324 llvm::FunctionType *FnTy =
1325 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1326 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce");
1327 break;
1328 }
1329 case OMPRTL__kmpc_end_reduce_nowait: {
1330 // Build __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
1331 // kmp_critical_name *lck);
1332 llvm::Type *TypeParams[] = {
1333 getIdentTyPointerTy(), CGM.Int32Ty,
1334 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
1335 llvm::FunctionType *FnTy =
1336 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1337 RTLFn =
1338 CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce_nowait");
1339 break;
1340 }
Alexey Bataev1d677132015-04-22 13:57:31 +00001341 case OMPRTL__kmpc_omp_task_begin_if0: {
1342 // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t
1343 // *new_task);
1344 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1345 CGM.VoidPtrTy};
1346 llvm::FunctionType *FnTy =
1347 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1348 RTLFn =
1349 CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_begin_if0");
1350 break;
1351 }
1352 case OMPRTL__kmpc_omp_task_complete_if0: {
1353 // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t
1354 // *new_task);
1355 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1356 CGM.VoidPtrTy};
1357 llvm::FunctionType *FnTy =
1358 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1359 RTLFn = CGM.CreateRuntimeFunction(FnTy,
1360 /*Name=*/"__kmpc_omp_task_complete_if0");
1361 break;
1362 }
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001363 case OMPRTL__kmpc_ordered: {
1364 // Build void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid);
1365 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1366 llvm::FunctionType *FnTy =
1367 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1368 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_ordered");
1369 break;
1370 }
1371 case OMPRTL__kmpc_end_ordered: {
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001372 // Build void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001373 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1374 llvm::FunctionType *FnTy =
1375 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1376 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_ordered");
1377 break;
1378 }
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001379 case OMPRTL__kmpc_omp_taskwait: {
1380 // Build kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 global_tid);
1381 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1382 llvm::FunctionType *FnTy =
1383 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1384 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_omp_taskwait");
1385 break;
1386 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001387 case OMPRTL__kmpc_taskgroup: {
1388 // Build void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid);
1389 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1390 llvm::FunctionType *FnTy =
1391 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1392 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_taskgroup");
1393 break;
1394 }
1395 case OMPRTL__kmpc_end_taskgroup: {
1396 // Build void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid);
1397 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1398 llvm::FunctionType *FnTy =
1399 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1400 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_taskgroup");
1401 break;
1402 }
Alexey Bataev7f210c62015-06-18 13:40:03 +00001403 case OMPRTL__kmpc_push_proc_bind: {
1404 // Build void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid,
1405 // int proc_bind)
1406 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy};
1407 llvm::FunctionType *FnTy =
1408 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1409 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_proc_bind");
1410 break;
1411 }
Alexey Bataev1d2353d2015-06-24 11:01:36 +00001412 case OMPRTL__kmpc_omp_task_with_deps: {
1413 // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid,
1414 // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list,
1415 // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
1416 llvm::Type *TypeParams[] = {
1417 getIdentTyPointerTy(), CGM.Int32Ty, CGM.VoidPtrTy, CGM.Int32Ty,
1418 CGM.VoidPtrTy, CGM.Int32Ty, CGM.VoidPtrTy};
1419 llvm::FunctionType *FnTy =
1420 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1421 RTLFn =
1422 CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_with_deps");
1423 break;
1424 }
1425 case OMPRTL__kmpc_omp_wait_deps: {
1426 // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid,
1427 // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias,
1428 // kmp_depend_info_t *noalias_dep_list);
1429 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1430 CGM.Int32Ty, CGM.VoidPtrTy,
1431 CGM.Int32Ty, CGM.VoidPtrTy};
1432 llvm::FunctionType *FnTy =
1433 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1434 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_wait_deps");
1435 break;
1436 }
Alexey Bataev0f34da12015-07-02 04:17:07 +00001437 case OMPRTL__kmpc_cancellationpoint: {
1438 // Build kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32
1439 // global_tid, kmp_int32 cncl_kind)
1440 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy};
1441 llvm::FunctionType *FnTy =
1442 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1443 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancellationpoint");
1444 break;
1445 }
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001446 case OMPRTL__kmpc_cancel: {
1447 // Build kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid,
1448 // kmp_int32 cncl_kind)
1449 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy};
1450 llvm::FunctionType *FnTy =
1451 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1452 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancel");
1453 break;
1454 }
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001455 case OMPRTL__kmpc_push_num_teams: {
1456 // Build void kmpc_push_num_teams (ident_t loc, kmp_int32 global_tid,
1457 // kmp_int32 num_teams, kmp_int32 num_threads)
1458 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty,
1459 CGM.Int32Ty};
1460 llvm::FunctionType *FnTy =
1461 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1462 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_teams");
1463 break;
1464 }
1465 case OMPRTL__kmpc_fork_teams: {
1466 // Build void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro
1467 // microtask, ...);
1468 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1469 getKmpc_MicroPointerTy()};
1470 llvm::FunctionType *FnTy =
1471 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true);
1472 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_teams");
1473 break;
1474 }
Alexey Bataev7292c292016-04-25 12:22:29 +00001475 case OMPRTL__kmpc_taskloop: {
1476 // Build void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int
1477 // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int
1478 // sched, kmp_uint64 grainsize, void *task_dup);
1479 llvm::Type *TypeParams[] = {getIdentTyPointerTy(),
1480 CGM.IntTy,
1481 CGM.VoidPtrTy,
1482 CGM.IntTy,
1483 CGM.Int64Ty->getPointerTo(),
1484 CGM.Int64Ty->getPointerTo(),
1485 CGM.Int64Ty,
1486 CGM.IntTy,
1487 CGM.IntTy,
1488 CGM.Int64Ty,
1489 CGM.VoidPtrTy};
1490 llvm::FunctionType *FnTy =
1491 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1492 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_taskloop");
1493 break;
1494 }
Alexey Bataev8b427062016-05-25 12:36:08 +00001495 case OMPRTL__kmpc_doacross_init: {
1496 // Build void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32
1497 // num_dims, struct kmp_dim *dims);
1498 llvm::Type *TypeParams[] = {getIdentTyPointerTy(),
1499 CGM.Int32Ty,
1500 CGM.Int32Ty,
1501 CGM.VoidPtrTy};
1502 llvm::FunctionType *FnTy =
1503 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1504 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_init");
1505 break;
1506 }
1507 case OMPRTL__kmpc_doacross_fini: {
1508 // Build void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid);
1509 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1510 llvm::FunctionType *FnTy =
1511 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1512 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_fini");
1513 break;
1514 }
1515 case OMPRTL__kmpc_doacross_post: {
1516 // Build void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64
1517 // *vec);
1518 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1519 CGM.Int64Ty->getPointerTo()};
1520 llvm::FunctionType *FnTy =
1521 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1522 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_post");
1523 break;
1524 }
1525 case OMPRTL__kmpc_doacross_wait: {
1526 // Build void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64
1527 // *vec);
1528 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1529 CGM.Int64Ty->getPointerTo()};
1530 llvm::FunctionType *FnTy =
1531 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1532 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_wait");
1533 break;
1534 }
Samuel Antaobed3c462015-10-02 16:14:20 +00001535 case OMPRTL__tgt_target: {
1536 // Build int32_t __tgt_target(int32_t device_id, void *host_ptr, int32_t
1537 // arg_num, void** args_base, void **args, size_t *arg_sizes, int32_t
1538 // *arg_types);
1539 llvm::Type *TypeParams[] = {CGM.Int32Ty,
1540 CGM.VoidPtrTy,
1541 CGM.Int32Ty,
1542 CGM.VoidPtrPtrTy,
1543 CGM.VoidPtrPtrTy,
1544 CGM.SizeTy->getPointerTo(),
1545 CGM.Int32Ty->getPointerTo()};
1546 llvm::FunctionType *FnTy =
1547 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1548 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target");
1549 break;
1550 }
Samuel Antaob68e2db2016-03-03 16:20:23 +00001551 case OMPRTL__tgt_target_teams: {
1552 // Build int32_t __tgt_target_teams(int32_t device_id, void *host_ptr,
1553 // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes,
1554 // int32_t *arg_types, int32_t num_teams, int32_t thread_limit);
1555 llvm::Type *TypeParams[] = {CGM.Int32Ty,
1556 CGM.VoidPtrTy,
1557 CGM.Int32Ty,
1558 CGM.VoidPtrPtrTy,
1559 CGM.VoidPtrPtrTy,
1560 CGM.SizeTy->getPointerTo(),
1561 CGM.Int32Ty->getPointerTo(),
1562 CGM.Int32Ty,
1563 CGM.Int32Ty};
1564 llvm::FunctionType *FnTy =
1565 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1566 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams");
1567 break;
1568 }
Samuel Antaoee8fb302016-01-06 13:42:12 +00001569 case OMPRTL__tgt_register_lib: {
1570 // Build void __tgt_register_lib(__tgt_bin_desc *desc);
1571 QualType ParamTy =
1572 CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy());
1573 llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)};
1574 llvm::FunctionType *FnTy =
1575 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1576 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_register_lib");
1577 break;
1578 }
1579 case OMPRTL__tgt_unregister_lib: {
1580 // Build void __tgt_unregister_lib(__tgt_bin_desc *desc);
1581 QualType ParamTy =
1582 CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy());
1583 llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)};
1584 llvm::FunctionType *FnTy =
1585 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1586 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_unregister_lib");
1587 break;
1588 }
Samuel Antaodf158d52016-04-27 22:58:19 +00001589 case OMPRTL__tgt_target_data_begin: {
1590 // Build void __tgt_target_data_begin(int32_t device_id, int32_t arg_num,
1591 // void** args_base, void **args, size_t *arg_sizes, int32_t *arg_types);
1592 llvm::Type *TypeParams[] = {CGM.Int32Ty,
1593 CGM.Int32Ty,
1594 CGM.VoidPtrPtrTy,
1595 CGM.VoidPtrPtrTy,
1596 CGM.SizeTy->getPointerTo(),
1597 CGM.Int32Ty->getPointerTo()};
1598 llvm::FunctionType *FnTy =
1599 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1600 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin");
1601 break;
1602 }
1603 case OMPRTL__tgt_target_data_end: {
1604 // Build void __tgt_target_data_end(int32_t device_id, int32_t arg_num,
1605 // void** args_base, void **args, size_t *arg_sizes, int32_t *arg_types);
1606 llvm::Type *TypeParams[] = {CGM.Int32Ty,
1607 CGM.Int32Ty,
1608 CGM.VoidPtrPtrTy,
1609 CGM.VoidPtrPtrTy,
1610 CGM.SizeTy->getPointerTo(),
1611 CGM.Int32Ty->getPointerTo()};
1612 llvm::FunctionType *FnTy =
1613 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1614 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end");
1615 break;
1616 }
Samuel Antao8d2d7302016-05-26 18:30:22 +00001617 case OMPRTL__tgt_target_data_update: {
1618 // Build void __tgt_target_data_update(int32_t device_id, int32_t arg_num,
1619 // void** args_base, void **args, size_t *arg_sizes, int32_t *arg_types);
1620 llvm::Type *TypeParams[] = {CGM.Int32Ty,
1621 CGM.Int32Ty,
1622 CGM.VoidPtrPtrTy,
1623 CGM.VoidPtrPtrTy,
1624 CGM.SizeTy->getPointerTo(),
1625 CGM.Int32Ty->getPointerTo()};
1626 llvm::FunctionType *FnTy =
1627 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1628 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update");
1629 break;
1630 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001631 }
Alexey Bataev50b3c952016-02-19 10:38:26 +00001632 assert(RTLFn && "Unable to find OpenMP runtime function");
Alexey Bataev9959db52014-05-06 10:08:46 +00001633 return RTLFn;
1634}
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001635
Alexander Musman21212e42015-03-13 10:38:23 +00001636llvm::Constant *CGOpenMPRuntime::createForStaticInitFunction(unsigned IVSize,
1637 bool IVSigned) {
1638 assert((IVSize == 32 || IVSize == 64) &&
1639 "IV size is not compatible with the omp runtime");
1640 auto Name = IVSize == 32 ? (IVSigned ? "__kmpc_for_static_init_4"
1641 : "__kmpc_for_static_init_4u")
1642 : (IVSigned ? "__kmpc_for_static_init_8"
1643 : "__kmpc_for_static_init_8u");
1644 auto ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty;
1645 auto PtrTy = llvm::PointerType::getUnqual(ITy);
1646 llvm::Type *TypeParams[] = {
1647 getIdentTyPointerTy(), // loc
1648 CGM.Int32Ty, // tid
1649 CGM.Int32Ty, // schedtype
1650 llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
1651 PtrTy, // p_lower
1652 PtrTy, // p_upper
1653 PtrTy, // p_stride
1654 ITy, // incr
1655 ITy // chunk
1656 };
1657 llvm::FunctionType *FnTy =
1658 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1659 return CGM.CreateRuntimeFunction(FnTy, Name);
1660}
1661
Alexander Musman92bdaab2015-03-12 13:37:50 +00001662llvm::Constant *CGOpenMPRuntime::createDispatchInitFunction(unsigned IVSize,
1663 bool IVSigned) {
1664 assert((IVSize == 32 || IVSize == 64) &&
1665 "IV size is not compatible with the omp runtime");
1666 auto Name =
1667 IVSize == 32
1668 ? (IVSigned ? "__kmpc_dispatch_init_4" : "__kmpc_dispatch_init_4u")
1669 : (IVSigned ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_8u");
1670 auto ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty;
1671 llvm::Type *TypeParams[] = { getIdentTyPointerTy(), // loc
1672 CGM.Int32Ty, // tid
1673 CGM.Int32Ty, // schedtype
1674 ITy, // lower
1675 ITy, // upper
1676 ITy, // stride
1677 ITy // chunk
1678 };
1679 llvm::FunctionType *FnTy =
1680 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1681 return CGM.CreateRuntimeFunction(FnTy, Name);
1682}
1683
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001684llvm::Constant *CGOpenMPRuntime::createDispatchFiniFunction(unsigned IVSize,
1685 bool IVSigned) {
1686 assert((IVSize == 32 || IVSize == 64) &&
1687 "IV size is not compatible with the omp runtime");
1688 auto Name =
1689 IVSize == 32
1690 ? (IVSigned ? "__kmpc_dispatch_fini_4" : "__kmpc_dispatch_fini_4u")
1691 : (IVSigned ? "__kmpc_dispatch_fini_8" : "__kmpc_dispatch_fini_8u");
1692 llvm::Type *TypeParams[] = {
1693 getIdentTyPointerTy(), // loc
1694 CGM.Int32Ty, // tid
1695 };
1696 llvm::FunctionType *FnTy =
1697 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1698 return CGM.CreateRuntimeFunction(FnTy, Name);
1699}
1700
Alexander Musman92bdaab2015-03-12 13:37:50 +00001701llvm::Constant *CGOpenMPRuntime::createDispatchNextFunction(unsigned IVSize,
1702 bool IVSigned) {
1703 assert((IVSize == 32 || IVSize == 64) &&
1704 "IV size is not compatible with the omp runtime");
1705 auto Name =
1706 IVSize == 32
1707 ? (IVSigned ? "__kmpc_dispatch_next_4" : "__kmpc_dispatch_next_4u")
1708 : (IVSigned ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_8u");
1709 auto ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty;
1710 auto PtrTy = llvm::PointerType::getUnqual(ITy);
1711 llvm::Type *TypeParams[] = {
1712 getIdentTyPointerTy(), // loc
1713 CGM.Int32Ty, // tid
1714 llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
1715 PtrTy, // p_lower
1716 PtrTy, // p_upper
1717 PtrTy // p_stride
1718 };
1719 llvm::FunctionType *FnTy =
1720 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1721 return CGM.CreateRuntimeFunction(FnTy, Name);
1722}
1723
Alexey Bataev97720002014-11-11 04:05:39 +00001724llvm::Constant *
1725CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) {
Samuel Antaof8b50122015-07-13 22:54:53 +00001726 assert(!CGM.getLangOpts().OpenMPUseTLS ||
1727 !CGM.getContext().getTargetInfo().isTLSSupported());
Alexey Bataev97720002014-11-11 04:05:39 +00001728 // Lookup the entry, lazily creating it if necessary.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001729 return getOrCreateInternalVariable(CGM.Int8PtrPtrTy,
Alexey Bataev97720002014-11-11 04:05:39 +00001730 Twine(CGM.getMangledName(VD)) + ".cache.");
1731}
1732
John McCall7f416cc2015-09-08 08:05:57 +00001733Address CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF,
1734 const VarDecl *VD,
1735 Address VDAddr,
1736 SourceLocation Loc) {
Samuel Antaof8b50122015-07-13 22:54:53 +00001737 if (CGM.getLangOpts().OpenMPUseTLS &&
1738 CGM.getContext().getTargetInfo().isTLSSupported())
1739 return VDAddr;
1740
John McCall7f416cc2015-09-08 08:05:57 +00001741 auto VarTy = VDAddr.getElementType();
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001742 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
John McCall7f416cc2015-09-08 08:05:57 +00001743 CGF.Builder.CreatePointerCast(VDAddr.getPointer(),
1744 CGM.Int8PtrTy),
Alexey Bataev97720002014-11-11 04:05:39 +00001745 CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)),
1746 getOrCreateThreadPrivateCache(VD)};
John McCall7f416cc2015-09-08 08:05:57 +00001747 return Address(CGF.EmitRuntimeCall(
1748 createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args),
1749 VDAddr.getAlignment());
Alexey Bataev97720002014-11-11 04:05:39 +00001750}
1751
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001752void CGOpenMPRuntime::emitThreadPrivateVarInit(
John McCall7f416cc2015-09-08 08:05:57 +00001753 CodeGenFunction &CGF, Address VDAddr, llvm::Value *Ctor,
Alexey Bataev97720002014-11-11 04:05:39 +00001754 llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) {
1755 // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime
1756 // library.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001757 auto OMPLoc = emitUpdateLocation(CGF, Loc);
1758 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num),
Alexey Bataev97720002014-11-11 04:05:39 +00001759 OMPLoc);
1760 // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor)
1761 // to register constructor/destructor for variable.
1762 llvm::Value *Args[] = {OMPLoc,
John McCall7f416cc2015-09-08 08:05:57 +00001763 CGF.Builder.CreatePointerCast(VDAddr.getPointer(),
1764 CGM.VoidPtrTy),
Alexey Bataev97720002014-11-11 04:05:39 +00001765 Ctor, CopyCtor, Dtor};
Alexey Bataev1e4b7132014-12-03 12:11:24 +00001766 CGF.EmitRuntimeCall(
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001767 createRuntimeFunction(OMPRTL__kmpc_threadprivate_register), Args);
Alexey Bataev97720002014-11-11 04:05:39 +00001768}
1769
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001770llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition(
John McCall7f416cc2015-09-08 08:05:57 +00001771 const VarDecl *VD, Address VDAddr, SourceLocation Loc,
Alexey Bataev97720002014-11-11 04:05:39 +00001772 bool PerformInit, CodeGenFunction *CGF) {
Samuel Antaof8b50122015-07-13 22:54:53 +00001773 if (CGM.getLangOpts().OpenMPUseTLS &&
1774 CGM.getContext().getTargetInfo().isTLSSupported())
1775 return nullptr;
1776
Alexey Bataev97720002014-11-11 04:05:39 +00001777 VD = VD->getDefinition(CGM.getContext());
1778 if (VD && ThreadPrivateWithDefinition.count(VD) == 0) {
1779 ThreadPrivateWithDefinition.insert(VD);
1780 QualType ASTTy = VD->getType();
1781
1782 llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr;
1783 auto Init = VD->getAnyInitializer();
1784 if (CGM.getLangOpts().CPlusPlus && PerformInit) {
1785 // Generate function that re-emits the declaration's initializer into the
1786 // threadprivate copy of the variable VD
1787 CodeGenFunction CtorCGF(CGM);
1788 FunctionArgList Args;
1789 ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, SourceLocation(),
1790 /*Id=*/nullptr, CGM.getContext().VoidPtrTy);
1791 Args.push_back(&Dst);
1792
John McCallc56a8b32016-03-11 04:30:31 +00001793 auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(
1794 CGM.getContext().VoidPtrTy, Args);
Alexey Bataev97720002014-11-11 04:05:39 +00001795 auto FTy = CGM.getTypes().GetFunctionType(FI);
1796 auto Fn = CGM.CreateGlobalInitOrDestructFunction(
Akira Hatanaka7791f1a42015-10-31 01:28:07 +00001797 FTy, ".__kmpc_global_ctor_.", FI, Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001798 CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI,
1799 Args, SourceLocation());
1800 auto ArgVal = CtorCGF.EmitLoadOfScalar(
John McCall7f416cc2015-09-08 08:05:57 +00001801 CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false,
Alexey Bataev97720002014-11-11 04:05:39 +00001802 CGM.getContext().VoidPtrTy, Dst.getLocation());
John McCall7f416cc2015-09-08 08:05:57 +00001803 Address Arg = Address(ArgVal, VDAddr.getAlignment());
1804 Arg = CtorCGF.Builder.CreateElementBitCast(Arg,
1805 CtorCGF.ConvertTypeForMem(ASTTy));
Alexey Bataev97720002014-11-11 04:05:39 +00001806 CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(),
1807 /*IsInitializer=*/true);
1808 ArgVal = CtorCGF.EmitLoadOfScalar(
John McCall7f416cc2015-09-08 08:05:57 +00001809 CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false,
Alexey Bataev97720002014-11-11 04:05:39 +00001810 CGM.getContext().VoidPtrTy, Dst.getLocation());
1811 CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue);
1812 CtorCGF.FinishFunction();
1813 Ctor = Fn;
1814 }
1815 if (VD->getType().isDestructedType() != QualType::DK_none) {
1816 // Generate function that emits destructor call for the threadprivate copy
1817 // of the variable VD
1818 CodeGenFunction DtorCGF(CGM);
1819 FunctionArgList Args;
1820 ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, SourceLocation(),
1821 /*Id=*/nullptr, CGM.getContext().VoidPtrTy);
1822 Args.push_back(&Dst);
1823
John McCallc56a8b32016-03-11 04:30:31 +00001824 auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(
1825 CGM.getContext().VoidTy, Args);
Alexey Bataev97720002014-11-11 04:05:39 +00001826 auto FTy = CGM.getTypes().GetFunctionType(FI);
1827 auto Fn = CGM.CreateGlobalInitOrDestructFunction(
Akira Hatanaka7791f1a42015-10-31 01:28:07 +00001828 FTy, ".__kmpc_global_dtor_.", FI, Loc);
Adrian Prantl1858c662016-04-24 22:22:29 +00001829 auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF);
Alexey Bataev97720002014-11-11 04:05:39 +00001830 DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args,
1831 SourceLocation());
Adrian Prantl1858c662016-04-24 22:22:29 +00001832 // Create a scope with an artificial location for the body of this function.
1833 auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF);
Alexey Bataev97720002014-11-11 04:05:39 +00001834 auto ArgVal = DtorCGF.EmitLoadOfScalar(
1835 DtorCGF.GetAddrOfLocalVar(&Dst),
John McCall7f416cc2015-09-08 08:05:57 +00001836 /*Volatile=*/false, CGM.getContext().VoidPtrTy, Dst.getLocation());
1837 DtorCGF.emitDestroy(Address(ArgVal, VDAddr.getAlignment()), ASTTy,
Alexey Bataev97720002014-11-11 04:05:39 +00001838 DtorCGF.getDestroyer(ASTTy.isDestructedType()),
1839 DtorCGF.needsEHCleanup(ASTTy.isDestructedType()));
1840 DtorCGF.FinishFunction();
1841 Dtor = Fn;
1842 }
1843 // Do not emit init function if it is not required.
1844 if (!Ctor && !Dtor)
1845 return nullptr;
1846
1847 llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
1848 auto CopyCtorTy =
1849 llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs,
1850 /*isVarArg=*/false)->getPointerTo();
1851 // Copying constructor for the threadprivate variable.
1852 // Must be NULL - reserved by runtime, but currently it requires that this
1853 // parameter is always NULL. Otherwise it fires assertion.
1854 CopyCtor = llvm::Constant::getNullValue(CopyCtorTy);
1855 if (Ctor == nullptr) {
1856 auto CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy,
1857 /*isVarArg=*/false)->getPointerTo();
1858 Ctor = llvm::Constant::getNullValue(CtorTy);
1859 }
1860 if (Dtor == nullptr) {
1861 auto DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy,
1862 /*isVarArg=*/false)->getPointerTo();
1863 Dtor = llvm::Constant::getNullValue(DtorTy);
1864 }
1865 if (!CGF) {
1866 auto InitFunctionTy =
1867 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false);
1868 auto InitFunction = CGM.CreateGlobalInitOrDestructFunction(
Akira Hatanaka7791f1a42015-10-31 01:28:07 +00001869 InitFunctionTy, ".__omp_threadprivate_init_.",
1870 CGM.getTypes().arrangeNullaryFunction());
Alexey Bataev97720002014-11-11 04:05:39 +00001871 CodeGenFunction InitCGF(CGM);
1872 FunctionArgList ArgList;
1873 InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction,
1874 CGM.getTypes().arrangeNullaryFunction(), ArgList,
1875 Loc);
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001876 emitThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001877 InitCGF.FinishFunction();
1878 return InitFunction;
1879 }
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001880 emitThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00001881 }
1882 return nullptr;
1883}
1884
Alexey Bataev1d677132015-04-22 13:57:31 +00001885/// \brief Emits code for OpenMP 'if' clause using specified \a CodeGen
1886/// function. Here is the logic:
1887/// if (Cond) {
1888/// ThenGen();
1889/// } else {
1890/// ElseGen();
1891/// }
1892static void emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond,
1893 const RegionCodeGenTy &ThenGen,
1894 const RegionCodeGenTy &ElseGen) {
1895 CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange());
1896
1897 // If the condition constant folds and can be elided, try to avoid emitting
1898 // the condition and the dead arm of the if/else.
1899 bool CondConstant;
1900 if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001901 if (CondConstant)
Alexey Bataev1d677132015-04-22 13:57:31 +00001902 ThenGen(CGF);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001903 else
Alexey Bataev1d677132015-04-22 13:57:31 +00001904 ElseGen(CGF);
Alexey Bataev1d677132015-04-22 13:57:31 +00001905 return;
1906 }
1907
1908 // Otherwise, the condition did not fold, or we couldn't elide it. Just
1909 // emit the conditional branch.
1910 auto ThenBlock = CGF.createBasicBlock("omp_if.then");
1911 auto ElseBlock = CGF.createBasicBlock("omp_if.else");
1912 auto ContBlock = CGF.createBasicBlock("omp_if.end");
1913 CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount=*/0);
1914
1915 // Emit the 'then' code.
1916 CGF.EmitBlock(ThenBlock);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001917 ThenGen(CGF);
Alexey Bataev1d677132015-04-22 13:57:31 +00001918 CGF.EmitBranch(ContBlock);
1919 // Emit the 'else' code if present.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001920 // There is no need to emit line number for unconditional branch.
1921 (void)ApplyDebugLocation::CreateEmpty(CGF);
1922 CGF.EmitBlock(ElseBlock);
1923 ElseGen(CGF);
1924 // There is no need to emit line number for unconditional branch.
1925 (void)ApplyDebugLocation::CreateEmpty(CGF);
1926 CGF.EmitBranch(ContBlock);
Alexey Bataev1d677132015-04-22 13:57:31 +00001927 // Emit the continuation block for code after the if.
1928 CGF.EmitBlock(ContBlock, /*IsFinished=*/true);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001929}
1930
Alexey Bataev1d677132015-04-22 13:57:31 +00001931void CGOpenMPRuntime::emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
1932 llvm::Value *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +00001933 ArrayRef<llvm::Value *> CapturedVars,
Alexey Bataev1d677132015-04-22 13:57:31 +00001934 const Expr *IfCond) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001935 if (!CGF.HaveInsertPoint())
1936 return;
Alexey Bataev1d677132015-04-22 13:57:31 +00001937 auto *RTLoc = emitUpdateLocation(CGF, Loc);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001938 auto &&ThenGen = [OutlinedFn, CapturedVars, RTLoc](CodeGenFunction &CGF,
1939 PrePostActionTy &) {
Alexey Bataev2377fe92015-09-10 08:12:02 +00001940 // Build call __kmpc_fork_call(loc, n, microtask, var1, .., varn);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001941 auto &RT = CGF.CGM.getOpenMPRuntime();
Alexey Bataev2377fe92015-09-10 08:12:02 +00001942 llvm::Value *Args[] = {
1943 RTLoc,
1944 CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001945 CGF.Builder.CreateBitCast(OutlinedFn, RT.getKmpc_MicroPointerTy())};
Alexey Bataev2377fe92015-09-10 08:12:02 +00001946 llvm::SmallVector<llvm::Value *, 16> RealArgs;
1947 RealArgs.append(std::begin(Args), std::end(Args));
1948 RealArgs.append(CapturedVars.begin(), CapturedVars.end());
1949
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001950 auto RTLFn = RT.createRuntimeFunction(OMPRTL__kmpc_fork_call);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001951 CGF.EmitRuntimeCall(RTLFn, RealArgs);
1952 };
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001953 auto &&ElseGen = [OutlinedFn, CapturedVars, RTLoc, Loc](CodeGenFunction &CGF,
1954 PrePostActionTy &) {
1955 auto &RT = CGF.CGM.getOpenMPRuntime();
1956 auto ThreadID = RT.getThreadID(CGF, Loc);
Alexey Bataev1d677132015-04-22 13:57:31 +00001957 // Build calls:
1958 // __kmpc_serialized_parallel(&Loc, GTid);
1959 llvm::Value *Args[] = {RTLoc, ThreadID};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001960 CGF.EmitRuntimeCall(
1961 RT.createRuntimeFunction(OMPRTL__kmpc_serialized_parallel), Args);
Alexey Bataevd74d0602014-10-13 06:02:40 +00001962
Alexey Bataev1d677132015-04-22 13:57:31 +00001963 // OutlinedFn(&GTid, &zero, CapturedStruct);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001964 auto ThreadIDAddr = RT.emitThreadIDAddress(CGF, Loc);
John McCall7f416cc2015-09-08 08:05:57 +00001965 Address ZeroAddr =
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001966 CGF.CreateTempAlloca(CGF.Int32Ty, CharUnits::fromQuantity(4),
1967 /*Name*/ ".zero.addr");
Alexey Bataev1d677132015-04-22 13:57:31 +00001968 CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
Alexey Bataev2377fe92015-09-10 08:12:02 +00001969 llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs;
1970 OutlinedFnArgs.push_back(ThreadIDAddr.getPointer());
1971 OutlinedFnArgs.push_back(ZeroAddr.getPointer());
1972 OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end());
Alexey Bataev1d677132015-04-22 13:57:31 +00001973 CGF.EmitCallOrInvoke(OutlinedFn, OutlinedFnArgs);
Alexey Bataevd74d0602014-10-13 06:02:40 +00001974
Alexey Bataev1d677132015-04-22 13:57:31 +00001975 // __kmpc_end_serialized_parallel(&Loc, GTid);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001976 llvm::Value *EndArgs[] = {RT.emitUpdateLocation(CGF, Loc), ThreadID};
Alexey Bataev1d677132015-04-22 13:57:31 +00001977 CGF.EmitRuntimeCall(
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001978 RT.createRuntimeFunction(OMPRTL__kmpc_end_serialized_parallel),
1979 EndArgs);
Alexey Bataev1d677132015-04-22 13:57:31 +00001980 };
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001981 if (IfCond)
Alexey Bataev1d677132015-04-22 13:57:31 +00001982 emitOMPIfClause(CGF, IfCond, ThenGen, ElseGen);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001983 else {
1984 RegionCodeGenTy ThenRCG(ThenGen);
1985 ThenRCG(CGF);
Alexey Bataevf539faa2016-03-28 12:58:34 +00001986 }
Alexey Bataevd74d0602014-10-13 06:02:40 +00001987}
1988
NAKAMURA Takumi59c74b222014-10-27 08:08:18 +00001989// If we're inside an (outlined) parallel region, use the region info's
Alexey Bataevd74d0602014-10-13 06:02:40 +00001990// thread-ID variable (it is passed in a first argument of the outlined function
1991// as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in
1992// regular serial code region, get thread ID by calling kmp_int32
1993// kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and
1994// return the address of that temp.
John McCall7f416cc2015-09-08 08:05:57 +00001995Address CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF,
1996 SourceLocation Loc) {
Alexey Bataev3015bcc2016-01-22 08:56:50 +00001997 if (auto *OMPRegionInfo =
Alexey Bataevd74d0602014-10-13 06:02:40 +00001998 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001999 if (OMPRegionInfo->getThreadIDVariable())
Alexey Bataev62b63b12015-03-10 07:28:44 +00002000 return OMPRegionInfo->getThreadIDVariableLValue(CGF).getAddress();
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00002001
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002002 auto ThreadID = getThreadID(CGF, Loc);
Alexey Bataevd74d0602014-10-13 06:02:40 +00002003 auto Int32Ty =
2004 CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true);
2005 auto ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp.");
2006 CGF.EmitStoreOfScalar(ThreadID,
John McCall7f416cc2015-09-08 08:05:57 +00002007 CGF.MakeAddrLValue(ThreadIDTemp, Int32Ty));
Alexey Bataevd74d0602014-10-13 06:02:40 +00002008
2009 return ThreadIDTemp;
2010}
2011
Alexey Bataev97720002014-11-11 04:05:39 +00002012llvm::Constant *
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002013CGOpenMPRuntime::getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +00002014 const llvm::Twine &Name) {
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002015 SmallString<256> Buffer;
2016 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev97720002014-11-11 04:05:39 +00002017 Out << Name;
2018 auto RuntimeName = Out.str();
David Blaikie13156b62014-11-19 03:06:06 +00002019 auto &Elem = *InternalVars.insert(std::make_pair(RuntimeName, nullptr)).first;
2020 if (Elem.second) {
2021 assert(Elem.second->getType()->getPointerElementType() == Ty &&
Alexey Bataev97720002014-11-11 04:05:39 +00002022 "OMP internal variable has different type than requested");
David Blaikie13156b62014-11-19 03:06:06 +00002023 return &*Elem.second;
Alexey Bataev97720002014-11-11 04:05:39 +00002024 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002025
David Blaikie13156b62014-11-19 03:06:06 +00002026 return Elem.second = new llvm::GlobalVariable(
2027 CGM.getModule(), Ty, /*IsConstant*/ false,
2028 llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty),
2029 Elem.first());
Alexey Bataev97720002014-11-11 04:05:39 +00002030}
2031
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002032llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) {
Alexey Bataev97720002014-11-11 04:05:39 +00002033 llvm::Twine Name(".gomp_critical_user_", CriticalName);
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002034 return getOrCreateInternalVariable(KmpCriticalNameTy, Name.concat(".var"));
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002035}
2036
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002037namespace {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002038/// Common pre(post)-action for different OpenMP constructs.
2039class CommonActionTy final : public PrePostActionTy {
2040 llvm::Value *EnterCallee;
2041 ArrayRef<llvm::Value *> EnterArgs;
2042 llvm::Value *ExitCallee;
2043 ArrayRef<llvm::Value *> ExitArgs;
2044 bool Conditional;
2045 llvm::BasicBlock *ContBlock = nullptr;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002046
2047public:
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002048 CommonActionTy(llvm::Value *EnterCallee, ArrayRef<llvm::Value *> EnterArgs,
2049 llvm::Value *ExitCallee, ArrayRef<llvm::Value *> ExitArgs,
2050 bool Conditional = false)
2051 : EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee),
2052 ExitArgs(ExitArgs), Conditional(Conditional) {}
2053 void Enter(CodeGenFunction &CGF) override {
2054 llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs);
2055 if (Conditional) {
2056 llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes);
2057 auto *ThenBlock = CGF.createBasicBlock("omp_if.then");
2058 ContBlock = CGF.createBasicBlock("omp_if.end");
2059 // Generate the branch (If-stmt)
2060 CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock);
2061 CGF.EmitBlock(ThenBlock);
2062 }
Alexey Bataeva744ff52015-05-05 09:24:37 +00002063 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002064 void Done(CodeGenFunction &CGF) {
2065 // Emit the rest of blocks/branches
2066 CGF.EmitBranch(ContBlock);
2067 CGF.EmitBlock(ContBlock, true);
2068 }
2069 void Exit(CodeGenFunction &CGF) override {
2070 CGF.EmitRuntimeCall(ExitCallee, ExitArgs);
Alexey Bataev3e6124b2015-04-10 07:48:12 +00002071 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002072};
Hans Wennborg7eb54642015-09-10 17:07:54 +00002073} // anonymous namespace
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002074
2075void CGOpenMPRuntime::emitCriticalRegion(CodeGenFunction &CGF,
2076 StringRef CriticalName,
2077 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +00002078 SourceLocation Loc, const Expr *Hint) {
2079 // __kmpc_critical[_with_hint](ident_t *, gtid, Lock[, hint]);
Alexey Bataev75ddfab2014-12-01 11:32:38 +00002080 // CriticalOpGen();
2081 // __kmpc_end_critical(ident_t *, gtid, Lock);
2082 // Prepare arguments and build a call to __kmpc_critical
Alexey Bataev8ef31412015-12-18 07:58:25 +00002083 if (!CGF.HaveInsertPoint())
2084 return;
Alexey Bataevfc57d162015-12-15 10:55:09 +00002085 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
2086 getCriticalRegionLock(CriticalName)};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002087 llvm::SmallVector<llvm::Value *, 4> EnterArgs(std::begin(Args),
2088 std::end(Args));
Alexey Bataevfc57d162015-12-15 10:55:09 +00002089 if (Hint) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002090 EnterArgs.push_back(CGF.Builder.CreateIntCast(
2091 CGF.EmitScalarExpr(Hint), CGM.IntPtrTy, /*isSigned=*/false));
2092 }
2093 CommonActionTy Action(
2094 createRuntimeFunction(Hint ? OMPRTL__kmpc_critical_with_hint
2095 : OMPRTL__kmpc_critical),
2096 EnterArgs, createRuntimeFunction(OMPRTL__kmpc_end_critical), Args);
2097 CriticalOpGen.setAction(Action);
Alexey Bataevfc57d162015-12-15 10:55:09 +00002098 emitInlinedDirective(CGF, OMPD_critical, CriticalOpGen);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002099}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00002100
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002101void CGOpenMPRuntime::emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002102 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002103 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002104 if (!CGF.HaveInsertPoint())
2105 return;
Alexey Bataev8d690652014-12-04 07:23:53 +00002106 // if(__kmpc_master(ident_t *, gtid)) {
2107 // MasterOpGen();
2108 // __kmpc_end_master(ident_t *, gtid);
2109 // }
2110 // Prepare arguments and build a call to __kmpc_master
Alexey Bataevd7614fb2015-04-10 06:33:45 +00002111 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002112 CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_master), Args,
2113 createRuntimeFunction(OMPRTL__kmpc_end_master), Args,
2114 /*Conditional=*/true);
2115 MasterOpGen.setAction(Action);
2116 emitInlinedDirective(CGF, OMPD_master, MasterOpGen);
2117 Action.Done(CGF);
Alexey Bataev8d690652014-12-04 07:23:53 +00002118}
2119
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002120void CGOpenMPRuntime::emitTaskyieldCall(CodeGenFunction &CGF,
2121 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002122 if (!CGF.HaveInsertPoint())
2123 return;
Alexey Bataev9f797f32015-02-05 05:57:51 +00002124 // Build call __kmpc_omp_taskyield(loc, thread_id, 0);
2125 llvm::Value *Args[] = {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002126 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
Alexey Bataev9f797f32015-02-05 05:57:51 +00002127 llvm::ConstantInt::get(CGM.IntTy, /*V=*/0, /*isSigned=*/true)};
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002128 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskyield), Args);
Alexey Bataev48591dd2016-04-20 04:01:36 +00002129 if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
2130 Region->emitUntiedSwitch(CGF);
Alexey Bataev9f797f32015-02-05 05:57:51 +00002131}
2132
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002133void CGOpenMPRuntime::emitTaskgroupRegion(CodeGenFunction &CGF,
2134 const RegionCodeGenTy &TaskgroupOpGen,
2135 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002136 if (!CGF.HaveInsertPoint())
2137 return;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002138 // __kmpc_taskgroup(ident_t *, gtid);
2139 // TaskgroupOpGen();
2140 // __kmpc_end_taskgroup(ident_t *, gtid);
2141 // Prepare arguments and build a call to __kmpc_taskgroup
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002142 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
2143 CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_taskgroup), Args,
2144 createRuntimeFunction(OMPRTL__kmpc_end_taskgroup),
2145 Args);
2146 TaskgroupOpGen.setAction(Action);
2147 emitInlinedDirective(CGF, OMPD_taskgroup, TaskgroupOpGen);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002148}
2149
John McCall7f416cc2015-09-08 08:05:57 +00002150/// Given an array of pointers to variables, project the address of a
2151/// given variable.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00002152static Address emitAddrOfVarFromArray(CodeGenFunction &CGF, Address Array,
2153 unsigned Index, const VarDecl *Var) {
John McCall7f416cc2015-09-08 08:05:57 +00002154 // Pull out the pointer to the variable.
2155 Address PtrAddr =
Alexey Bataevf24e7b12015-10-08 09:10:53 +00002156 CGF.Builder.CreateConstArrayGEP(Array, Index, CGF.getPointerSize());
John McCall7f416cc2015-09-08 08:05:57 +00002157 llvm::Value *Ptr = CGF.Builder.CreateLoad(PtrAddr);
2158
2159 Address Addr = Address(Ptr, CGF.getContext().getDeclAlign(Var));
Alexey Bataevf24e7b12015-10-08 09:10:53 +00002160 Addr = CGF.Builder.CreateElementBitCast(
2161 Addr, CGF.ConvertTypeForMem(Var->getType()));
John McCall7f416cc2015-09-08 08:05:57 +00002162 return Addr;
2163}
2164
Alexey Bataeva63048e2015-03-23 06:18:07 +00002165static llvm::Value *emitCopyprivateCopyFunction(
Alexey Bataev420d45b2015-04-14 05:11:24 +00002166 CodeGenModule &CGM, llvm::Type *ArgsType,
2167 ArrayRef<const Expr *> CopyprivateVars, ArrayRef<const Expr *> DestExprs,
2168 ArrayRef<const Expr *> SrcExprs, ArrayRef<const Expr *> AssignmentOps) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00002169 auto &C = CGM.getContext();
2170 // void copy_func(void *LHSArg, void *RHSArg);
2171 FunctionArgList Args;
2172 ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, SourceLocation(), /*Id=*/nullptr,
2173 C.VoidPtrTy);
2174 ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, SourceLocation(), /*Id=*/nullptr,
2175 C.VoidPtrTy);
2176 Args.push_back(&LHSArg);
2177 Args.push_back(&RHSArg);
John McCallc56a8b32016-03-11 04:30:31 +00002178 auto &CGFI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataeva63048e2015-03-23 06:18:07 +00002179 auto *Fn = llvm::Function::Create(
2180 CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
2181 ".omp.copyprivate.copy_func", &CGM.getModule());
Akira Hatanaka44a59f82015-10-28 02:30:47 +00002182 CGM.SetInternalFunctionAttributes(/*D=*/nullptr, Fn, CGFI);
Alexey Bataeva63048e2015-03-23 06:18:07 +00002183 CodeGenFunction CGF(CGM);
2184 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args);
Alexey Bataev420d45b2015-04-14 05:11:24 +00002185 // Dest = (void*[n])(LHSArg);
Alexey Bataeva63048e2015-03-23 06:18:07 +00002186 // Src = (void*[n])(RHSArg);
John McCall7f416cc2015-09-08 08:05:57 +00002187 Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
2188 CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)),
2189 ArgsType), CGF.getPointerAlign());
2190 Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
2191 CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)),
2192 ArgsType), CGF.getPointerAlign());
Alexey Bataeva63048e2015-03-23 06:18:07 +00002193 // *(Type0*)Dst[0] = *(Type0*)Src[0];
2194 // *(Type1*)Dst[1] = *(Type1*)Src[1];
2195 // ...
2196 // *(Typen*)Dst[n] = *(Typen*)Src[n];
Alexey Bataeva63048e2015-03-23 06:18:07 +00002197 for (unsigned I = 0, E = AssignmentOps.size(); I < E; ++I) {
John McCall7f416cc2015-09-08 08:05:57 +00002198 auto DestVar = cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl());
2199 Address DestAddr = emitAddrOfVarFromArray(CGF, LHS, I, DestVar);
2200
2201 auto SrcVar = cast<VarDecl>(cast<DeclRefExpr>(SrcExprs[I])->getDecl());
2202 Address SrcAddr = emitAddrOfVarFromArray(CGF, RHS, I, SrcVar);
2203
Alexey Bataev1d9c15c2015-05-19 12:31:28 +00002204 auto *VD = cast<DeclRefExpr>(CopyprivateVars[I])->getDecl();
2205 QualType Type = VD->getType();
John McCall7f416cc2015-09-08 08:05:57 +00002206 CGF.EmitOMPCopy(Type, DestAddr, SrcAddr, DestVar, SrcVar, AssignmentOps[I]);
Alexey Bataeva63048e2015-03-23 06:18:07 +00002207 }
Alexey Bataeva63048e2015-03-23 06:18:07 +00002208 CGF.FinishFunction();
2209 return Fn;
2210}
2211
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002212void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002213 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +00002214 SourceLocation Loc,
2215 ArrayRef<const Expr *> CopyprivateVars,
2216 ArrayRef<const Expr *> SrcExprs,
2217 ArrayRef<const Expr *> DstExprs,
2218 ArrayRef<const Expr *> AssignmentOps) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002219 if (!CGF.HaveInsertPoint())
2220 return;
Alexey Bataeva63048e2015-03-23 06:18:07 +00002221 assert(CopyprivateVars.size() == SrcExprs.size() &&
2222 CopyprivateVars.size() == DstExprs.size() &&
2223 CopyprivateVars.size() == AssignmentOps.size());
2224 auto &C = CGM.getContext();
2225 // int32 did_it = 0;
Alexey Bataev6956e2e2015-02-05 06:35:41 +00002226 // if(__kmpc_single(ident_t *, gtid)) {
2227 // SingleOpGen();
2228 // __kmpc_end_single(ident_t *, gtid);
Alexey Bataeva63048e2015-03-23 06:18:07 +00002229 // did_it = 1;
Alexey Bataev6956e2e2015-02-05 06:35:41 +00002230 // }
Alexey Bataeva63048e2015-03-23 06:18:07 +00002231 // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>,
2232 // <copy_func>, did_it);
2233
John McCall7f416cc2015-09-08 08:05:57 +00002234 Address DidIt = Address::invalid();
Alexey Bataeva63048e2015-03-23 06:18:07 +00002235 if (!CopyprivateVars.empty()) {
2236 // int32 did_it = 0;
2237 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
2238 DidIt = CGF.CreateMemTemp(KmpInt32Ty, ".omp.copyprivate.did_it");
John McCall7f416cc2015-09-08 08:05:57 +00002239 CGF.Builder.CreateStore(CGF.Builder.getInt32(0), DidIt);
Alexey Bataeva63048e2015-03-23 06:18:07 +00002240 }
Alexey Bataev6956e2e2015-02-05 06:35:41 +00002241 // Prepare arguments and build a call to __kmpc_single
Alexey Bataevd7614fb2015-04-10 06:33:45 +00002242 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002243 CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_single), Args,
2244 createRuntimeFunction(OMPRTL__kmpc_end_single), Args,
2245 /*Conditional=*/true);
2246 SingleOpGen.setAction(Action);
2247 emitInlinedDirective(CGF, OMPD_single, SingleOpGen);
2248 if (DidIt.isValid()) {
2249 // did_it = 1;
2250 CGF.Builder.CreateStore(CGF.Builder.getInt32(1), DidIt);
2251 }
2252 Action.Done(CGF);
Alexey Bataeva63048e2015-03-23 06:18:07 +00002253 // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>,
2254 // <copy_func>, did_it);
John McCall7f416cc2015-09-08 08:05:57 +00002255 if (DidIt.isValid()) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00002256 llvm::APInt ArraySize(/*unsigned int numBits=*/32, CopyprivateVars.size());
2257 auto CopyprivateArrayTy =
2258 C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal,
2259 /*IndexTypeQuals=*/0);
2260 // Create a list of all private variables for copyprivate.
John McCall7f416cc2015-09-08 08:05:57 +00002261 Address CopyprivateList =
Alexey Bataeva63048e2015-03-23 06:18:07 +00002262 CGF.CreateMemTemp(CopyprivateArrayTy, ".omp.copyprivate.cpr_list");
2263 for (unsigned I = 0, E = CopyprivateVars.size(); I < E; ++I) {
John McCall7f416cc2015-09-08 08:05:57 +00002264 Address Elem = CGF.Builder.CreateConstArrayGEP(
2265 CopyprivateList, I, CGF.getPointerSize());
2266 CGF.Builder.CreateStore(
Alexey Bataeva63048e2015-03-23 06:18:07 +00002267 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
John McCall7f416cc2015-09-08 08:05:57 +00002268 CGF.EmitLValue(CopyprivateVars[I]).getPointer(), CGF.VoidPtrTy),
2269 Elem);
Alexey Bataeva63048e2015-03-23 06:18:07 +00002270 }
2271 // Build function that copies private values from single region to all other
2272 // threads in the corresponding parallel region.
2273 auto *CpyFn = emitCopyprivateCopyFunction(
2274 CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy)->getPointerTo(),
Alexey Bataev420d45b2015-04-14 05:11:24 +00002275 CopyprivateVars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataev1189bd02016-01-26 12:20:39 +00002276 auto *BufSize = CGF.getTypeSize(CopyprivateArrayTy);
John McCall7f416cc2015-09-08 08:05:57 +00002277 Address CL =
2278 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(CopyprivateList,
2279 CGF.VoidPtrTy);
2280 auto *DidItVal = CGF.Builder.CreateLoad(DidIt);
Alexey Bataeva63048e2015-03-23 06:18:07 +00002281 llvm::Value *Args[] = {
2282 emitUpdateLocation(CGF, Loc), // ident_t *<loc>
2283 getThreadID(CGF, Loc), // i32 <gtid>
Alexey Bataev66beaa92015-04-30 03:47:32 +00002284 BufSize, // size_t <buf_size>
John McCall7f416cc2015-09-08 08:05:57 +00002285 CL.getPointer(), // void *<copyprivate list>
Alexey Bataeva63048e2015-03-23 06:18:07 +00002286 CpyFn, // void (*) (void *, void *) <copy_func>
2287 DidItVal // i32 did_it
2288 };
2289 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_copyprivate), Args);
2290 }
Alexey Bataev6956e2e2015-02-05 06:35:41 +00002291}
2292
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002293void CGOpenMPRuntime::emitOrderedRegion(CodeGenFunction &CGF,
2294 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +00002295 SourceLocation Loc, bool IsThreads) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002296 if (!CGF.HaveInsertPoint())
2297 return;
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002298 // __kmpc_ordered(ident_t *, gtid);
2299 // OrderedOpGen();
2300 // __kmpc_end_ordered(ident_t *, gtid);
2301 // Prepare arguments and build a call to __kmpc_ordered
Alexey Bataev5f600d62015-09-29 03:48:57 +00002302 if (IsThreads) {
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002303 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002304 CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_ordered), Args,
2305 createRuntimeFunction(OMPRTL__kmpc_end_ordered),
2306 Args);
2307 OrderedOpGen.setAction(Action);
2308 emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen);
2309 return;
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002310 }
Alexey Bataev5f600d62015-09-29 03:48:57 +00002311 emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002312}
2313
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002314void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00002315 OpenMPDirectiveKind Kind, bool EmitChecks,
2316 bool ForceSimpleCall) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002317 if (!CGF.HaveInsertPoint())
2318 return;
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00002319 // Build call __kmpc_cancel_barrier(loc, thread_id);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002320 // Build call __kmpc_barrier(loc, thread_id);
Alexey Bataev50b3c952016-02-19 10:38:26 +00002321 unsigned Flags;
2322 if (Kind == OMPD_for)
2323 Flags = OMP_IDENT_BARRIER_IMPL_FOR;
2324 else if (Kind == OMPD_sections)
2325 Flags = OMP_IDENT_BARRIER_IMPL_SECTIONS;
2326 else if (Kind == OMPD_single)
2327 Flags = OMP_IDENT_BARRIER_IMPL_SINGLE;
2328 else if (Kind == OMPD_barrier)
2329 Flags = OMP_IDENT_BARRIER_EXPL;
2330 else
2331 Flags = OMP_IDENT_BARRIER_IMPL;
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002332 // Build call __kmpc_cancel_barrier(loc, thread_id) or __kmpc_barrier(loc,
2333 // thread_id);
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002334 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, Flags),
2335 getThreadID(CGF, Loc)};
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002336 if (auto *OMPRegionInfo =
2337 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
Alexey Bataev25e5b442015-09-15 12:52:43 +00002338 if (!ForceSimpleCall && OMPRegionInfo->hasCancel()) {
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002339 auto *Result = CGF.EmitRuntimeCall(
2340 createRuntimeFunction(OMPRTL__kmpc_cancel_barrier), Args);
Alexey Bataev25e5b442015-09-15 12:52:43 +00002341 if (EmitChecks) {
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002342 // if (__kmpc_cancel_barrier()) {
2343 // exit from construct;
2344 // }
2345 auto *ExitBB = CGF.createBasicBlock(".cancel.exit");
2346 auto *ContBB = CGF.createBasicBlock(".cancel.continue");
2347 auto *Cmp = CGF.Builder.CreateIsNotNull(Result);
2348 CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB);
2349 CGF.EmitBlock(ExitBB);
2350 // exit from construct;
Alexey Bataev25e5b442015-09-15 12:52:43 +00002351 auto CancelDestination =
2352 CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind());
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002353 CGF.EmitBranchThroughCleanup(CancelDestination);
2354 CGF.EmitBlock(ContBB, /*IsFinished=*/true);
2355 }
2356 return;
2357 }
2358 }
2359 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_barrier), Args);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00002360}
2361
Alexander Musmanc6388682014-12-15 07:07:06 +00002362/// \brief Map the OpenMP loop schedule to the runtime enumeration.
2363static OpenMPSchedType getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002364 bool Chunked, bool Ordered) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002365 switch (ScheduleKind) {
2366 case OMPC_SCHEDULE_static:
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002367 return Chunked ? (Ordered ? OMP_ord_static_chunked : OMP_sch_static_chunked)
2368 : (Ordered ? OMP_ord_static : OMP_sch_static);
Alexander Musmanc6388682014-12-15 07:07:06 +00002369 case OMPC_SCHEDULE_dynamic:
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002370 return Ordered ? OMP_ord_dynamic_chunked : OMP_sch_dynamic_chunked;
Alexander Musmanc6388682014-12-15 07:07:06 +00002371 case OMPC_SCHEDULE_guided:
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002372 return Ordered ? OMP_ord_guided_chunked : OMP_sch_guided_chunked;
Alexander Musmanc6388682014-12-15 07:07:06 +00002373 case OMPC_SCHEDULE_runtime:
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002374 return Ordered ? OMP_ord_runtime : OMP_sch_runtime;
2375 case OMPC_SCHEDULE_auto:
2376 return Ordered ? OMP_ord_auto : OMP_sch_auto;
Alexander Musmanc6388682014-12-15 07:07:06 +00002377 case OMPC_SCHEDULE_unknown:
2378 assert(!Chunked && "chunk was specified but schedule kind not known");
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002379 return Ordered ? OMP_ord_static : OMP_sch_static;
Alexander Musmanc6388682014-12-15 07:07:06 +00002380 }
2381 llvm_unreachable("Unexpected runtime schedule");
2382}
2383
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002384/// \brief Map the OpenMP distribute schedule to the runtime enumeration.
2385static OpenMPSchedType
2386getRuntimeSchedule(OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) {
2387 // only static is allowed for dist_schedule
2388 return Chunked ? OMP_dist_sch_static_chunked : OMP_dist_sch_static;
2389}
2390
Alexander Musmanc6388682014-12-15 07:07:06 +00002391bool CGOpenMPRuntime::isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
2392 bool Chunked) const {
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002393 auto Schedule = getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false);
Alexander Musmanc6388682014-12-15 07:07:06 +00002394 return Schedule == OMP_sch_static;
2395}
2396
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002397bool CGOpenMPRuntime::isStaticNonchunked(
2398 OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const {
2399 auto Schedule = getRuntimeSchedule(ScheduleKind, Chunked);
2400 return Schedule == OMP_dist_sch_static;
2401}
2402
2403
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002404bool CGOpenMPRuntime::isDynamic(OpenMPScheduleClauseKind ScheduleKind) const {
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002405 auto Schedule =
2406 getRuntimeSchedule(ScheduleKind, /*Chunked=*/false, /*Ordered=*/false);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002407 assert(Schedule != OMP_sch_static_chunked && "cannot be chunked here");
2408 return Schedule != OMP_sch_static;
2409}
2410
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002411static int addMonoNonMonoModifier(OpenMPSchedType Schedule,
2412 OpenMPScheduleClauseModifier M1,
2413 OpenMPScheduleClauseModifier M2) {
Alexey Bataev6cff6242016-05-30 13:05:14 +00002414 int Modifier = 0;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002415 switch (M1) {
2416 case OMPC_SCHEDULE_MODIFIER_monotonic:
Alexey Bataev6cff6242016-05-30 13:05:14 +00002417 Modifier = OMP_sch_modifier_monotonic;
2418 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002419 case OMPC_SCHEDULE_MODIFIER_nonmonotonic:
Alexey Bataev6cff6242016-05-30 13:05:14 +00002420 Modifier = OMP_sch_modifier_nonmonotonic;
2421 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002422 case OMPC_SCHEDULE_MODIFIER_simd:
Alexey Bataev6cff6242016-05-30 13:05:14 +00002423 if (Schedule == OMP_sch_static_chunked)
2424 Schedule = OMP_sch_static_balanced_chunked;
2425 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002426 case OMPC_SCHEDULE_MODIFIER_last:
2427 case OMPC_SCHEDULE_MODIFIER_unknown:
2428 break;
2429 }
2430 switch (M2) {
2431 case OMPC_SCHEDULE_MODIFIER_monotonic:
Alexey Bataev6cff6242016-05-30 13:05:14 +00002432 Modifier = OMP_sch_modifier_monotonic;
2433 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002434 case OMPC_SCHEDULE_MODIFIER_nonmonotonic:
Alexey Bataev6cff6242016-05-30 13:05:14 +00002435 Modifier = OMP_sch_modifier_nonmonotonic;
2436 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002437 case OMPC_SCHEDULE_MODIFIER_simd:
Alexey Bataev6cff6242016-05-30 13:05:14 +00002438 if (Schedule == OMP_sch_static_chunked)
2439 Schedule = OMP_sch_static_balanced_chunked;
2440 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002441 case OMPC_SCHEDULE_MODIFIER_last:
2442 case OMPC_SCHEDULE_MODIFIER_unknown:
2443 break;
2444 }
Alexey Bataev6cff6242016-05-30 13:05:14 +00002445 return Schedule | Modifier;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002446}
2447
John McCall7f416cc2015-09-08 08:05:57 +00002448void CGOpenMPRuntime::emitForDispatchInit(CodeGenFunction &CGF,
2449 SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002450 const OpenMPScheduleTy &ScheduleKind,
John McCall7f416cc2015-09-08 08:05:57 +00002451 unsigned IVSize, bool IVSigned,
2452 bool Ordered, llvm::Value *UB,
2453 llvm::Value *Chunk) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002454 if (!CGF.HaveInsertPoint())
2455 return;
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002456 OpenMPSchedType Schedule =
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002457 getRuntimeSchedule(ScheduleKind.Schedule, Chunk != nullptr, Ordered);
John McCall7f416cc2015-09-08 08:05:57 +00002458 assert(Ordered ||
2459 (Schedule != OMP_sch_static && Schedule != OMP_sch_static_chunked &&
Alexey Bataev6cff6242016-05-30 13:05:14 +00002460 Schedule != OMP_ord_static && Schedule != OMP_ord_static_chunked &&
2461 Schedule != OMP_sch_static_balanced_chunked));
John McCall7f416cc2015-09-08 08:05:57 +00002462 // Call __kmpc_dispatch_init(
2463 // ident_t *loc, kmp_int32 tid, kmp_int32 schedule,
2464 // kmp_int[32|64] lower, kmp_int[32|64] upper,
2465 // kmp_int[32|64] stride, kmp_int[32|64] chunk);
Alexander Musmanc6388682014-12-15 07:07:06 +00002466
John McCall7f416cc2015-09-08 08:05:57 +00002467 // If the Chunk was not specified in the clause - use default value 1.
2468 if (Chunk == nullptr)
2469 Chunk = CGF.Builder.getIntN(IVSize, 1);
2470 llvm::Value *Args[] = {
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002471 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
2472 CGF.Builder.getInt32(addMonoNonMonoModifier(
2473 Schedule, ScheduleKind.M1, ScheduleKind.M2)), // Schedule type
2474 CGF.Builder.getIntN(IVSize, 0), // Lower
2475 UB, // Upper
2476 CGF.Builder.getIntN(IVSize, 1), // Stride
2477 Chunk // Chunk
John McCall7f416cc2015-09-08 08:05:57 +00002478 };
2479 CGF.EmitRuntimeCall(createDispatchInitFunction(IVSize, IVSigned), Args);
2480}
2481
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002482static void emitForStaticInitCall(
2483 CodeGenFunction &CGF, llvm::Value *UpdateLocation, llvm::Value *ThreadId,
2484 llvm::Constant *ForStaticInitFunction, OpenMPSchedType Schedule,
2485 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
2486 unsigned IVSize, bool Ordered, Address IL, Address LB, Address UB,
2487 Address ST, llvm::Value *Chunk) {
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002488 if (!CGF.HaveInsertPoint())
2489 return;
2490
2491 assert(!Ordered);
2492 assert(Schedule == OMP_sch_static || Schedule == OMP_sch_static_chunked ||
Alexey Bataev6cff6242016-05-30 13:05:14 +00002493 Schedule == OMP_sch_static_balanced_chunked ||
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002494 Schedule == OMP_ord_static || Schedule == OMP_ord_static_chunked ||
2495 Schedule == OMP_dist_sch_static ||
2496 Schedule == OMP_dist_sch_static_chunked);
2497
2498 // Call __kmpc_for_static_init(
2499 // ident_t *loc, kmp_int32 tid, kmp_int32 schedtype,
2500 // kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower,
2501 // kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride,
2502 // kmp_int[32|64] incr, kmp_int[32|64] chunk);
2503 if (Chunk == nullptr) {
2504 assert((Schedule == OMP_sch_static || Schedule == OMP_ord_static ||
2505 Schedule == OMP_dist_sch_static) &&
2506 "expected static non-chunked schedule");
2507 // If the Chunk was not specified in the clause - use default value 1.
2508 Chunk = CGF.Builder.getIntN(IVSize, 1);
2509 } else {
2510 assert((Schedule == OMP_sch_static_chunked ||
Alexey Bataev6cff6242016-05-30 13:05:14 +00002511 Schedule == OMP_sch_static_balanced_chunked ||
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002512 Schedule == OMP_ord_static_chunked ||
2513 Schedule == OMP_dist_sch_static_chunked) &&
2514 "expected static chunked schedule");
2515 }
2516 llvm::Value *Args[] = {
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002517 UpdateLocation, ThreadId, CGF.Builder.getInt32(addMonoNonMonoModifier(
2518 Schedule, M1, M2)), // Schedule type
2519 IL.getPointer(), // &isLastIter
2520 LB.getPointer(), // &LB
2521 UB.getPointer(), // &UB
2522 ST.getPointer(), // &Stride
2523 CGF.Builder.getIntN(IVSize, 1), // Incr
2524 Chunk // Chunk
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002525 };
2526 CGF.EmitRuntimeCall(ForStaticInitFunction, Args);
2527}
2528
John McCall7f416cc2015-09-08 08:05:57 +00002529void CGOpenMPRuntime::emitForStaticInit(CodeGenFunction &CGF,
2530 SourceLocation Loc,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002531 const OpenMPScheduleTy &ScheduleKind,
John McCall7f416cc2015-09-08 08:05:57 +00002532 unsigned IVSize, bool IVSigned,
2533 bool Ordered, Address IL, Address LB,
2534 Address UB, Address ST,
2535 llvm::Value *Chunk) {
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002536 OpenMPSchedType ScheduleNum =
2537 getRuntimeSchedule(ScheduleKind.Schedule, Chunk != nullptr, Ordered);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002538 auto *UpdatedLocation = emitUpdateLocation(CGF, Loc);
2539 auto *ThreadId = getThreadID(CGF, Loc);
2540 auto *StaticInitFunction = createForStaticInitFunction(IVSize, IVSigned);
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002541 emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction,
2542 ScheduleNum, ScheduleKind.M1, ScheduleKind.M2, IVSize,
2543 Ordered, IL, LB, UB, ST, Chunk);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002544}
John McCall7f416cc2015-09-08 08:05:57 +00002545
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002546void CGOpenMPRuntime::emitDistributeStaticInit(
2547 CodeGenFunction &CGF, SourceLocation Loc,
2548 OpenMPDistScheduleClauseKind SchedKind, unsigned IVSize, bool IVSigned,
2549 bool Ordered, Address IL, Address LB, Address UB, Address ST,
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002550 llvm::Value *Chunk) {
2551 OpenMPSchedType ScheduleNum = getRuntimeSchedule(SchedKind, Chunk != nullptr);
2552 auto *UpdatedLocation = emitUpdateLocation(CGF, Loc);
2553 auto *ThreadId = getThreadID(CGF, Loc);
2554 auto *StaticInitFunction = createForStaticInitFunction(IVSize, IVSigned);
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002555 emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction,
2556 ScheduleNum, OMPC_SCHEDULE_MODIFIER_unknown,
2557 OMPC_SCHEDULE_MODIFIER_unknown, IVSize, Ordered, IL, LB,
2558 UB, ST, Chunk);
Alexander Musmanc6388682014-12-15 07:07:06 +00002559}
2560
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002561void CGOpenMPRuntime::emitForStaticFinish(CodeGenFunction &CGF,
2562 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002563 if (!CGF.HaveInsertPoint())
2564 return;
Alexander Musmanc6388682014-12-15 07:07:06 +00002565 // Call __kmpc_for_static_fini(ident_t *loc, kmp_int32 tid);
Alexey Bataev50b3c952016-02-19 10:38:26 +00002566 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002567 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_for_static_fini),
2568 Args);
Alexander Musmanc6388682014-12-15 07:07:06 +00002569}
2570
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002571void CGOpenMPRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF,
2572 SourceLocation Loc,
2573 unsigned IVSize,
2574 bool IVSigned) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002575 if (!CGF.HaveInsertPoint())
2576 return;
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002577 // Call __kmpc_for_dynamic_fini_(4|8)[u](ident_t *loc, kmp_int32 tid);
Alexey Bataev50b3c952016-02-19 10:38:26 +00002578 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002579 CGF.EmitRuntimeCall(createDispatchFiniFunction(IVSize, IVSigned), Args);
2580}
2581
Alexander Musman92bdaab2015-03-12 13:37:50 +00002582llvm::Value *CGOpenMPRuntime::emitForNext(CodeGenFunction &CGF,
2583 SourceLocation Loc, unsigned IVSize,
John McCall7f416cc2015-09-08 08:05:57 +00002584 bool IVSigned, Address IL,
2585 Address LB, Address UB,
2586 Address ST) {
Alexander Musman92bdaab2015-03-12 13:37:50 +00002587 // Call __kmpc_dispatch_next(
2588 // ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
2589 // kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
2590 // kmp_int[32|64] *p_stride);
2591 llvm::Value *Args[] = {
Alexey Bataev50b3c952016-02-19 10:38:26 +00002592 emitUpdateLocation(CGF, Loc),
2593 getThreadID(CGF, Loc),
John McCall7f416cc2015-09-08 08:05:57 +00002594 IL.getPointer(), // &isLastIter
2595 LB.getPointer(), // &Lower
2596 UB.getPointer(), // &Upper
2597 ST.getPointer() // &Stride
Alexander Musman92bdaab2015-03-12 13:37:50 +00002598 };
2599 llvm::Value *Call =
2600 CGF.EmitRuntimeCall(createDispatchNextFunction(IVSize, IVSigned), Args);
2601 return CGF.EmitScalarConversion(
2602 Call, CGF.getContext().getIntTypeForBitwidth(32, /* Signed */ true),
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002603 CGF.getContext().BoolTy, Loc);
Alexander Musman92bdaab2015-03-12 13:37:50 +00002604}
2605
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002606void CGOpenMPRuntime::emitNumThreadsClause(CodeGenFunction &CGF,
2607 llvm::Value *NumThreads,
2608 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002609 if (!CGF.HaveInsertPoint())
2610 return;
Alexey Bataevb2059782014-10-13 08:23:51 +00002611 // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads)
2612 llvm::Value *Args[] = {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002613 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
Alexey Bataevb2059782014-10-13 08:23:51 +00002614 CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)};
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002615 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_threads),
2616 Args);
Alexey Bataevb2059782014-10-13 08:23:51 +00002617}
2618
Alexey Bataev7f210c62015-06-18 13:40:03 +00002619void CGOpenMPRuntime::emitProcBindClause(CodeGenFunction &CGF,
2620 OpenMPProcBindClauseKind ProcBind,
2621 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002622 if (!CGF.HaveInsertPoint())
2623 return;
Alexey Bataev7f210c62015-06-18 13:40:03 +00002624 // Constants for proc bind value accepted by the runtime.
2625 enum ProcBindTy {
2626 ProcBindFalse = 0,
2627 ProcBindTrue,
2628 ProcBindMaster,
2629 ProcBindClose,
2630 ProcBindSpread,
2631 ProcBindIntel,
2632 ProcBindDefault
2633 } RuntimeProcBind;
2634 switch (ProcBind) {
2635 case OMPC_PROC_BIND_master:
2636 RuntimeProcBind = ProcBindMaster;
2637 break;
2638 case OMPC_PROC_BIND_close:
2639 RuntimeProcBind = ProcBindClose;
2640 break;
2641 case OMPC_PROC_BIND_spread:
2642 RuntimeProcBind = ProcBindSpread;
2643 break;
2644 case OMPC_PROC_BIND_unknown:
2645 llvm_unreachable("Unsupported proc_bind value.");
2646 }
2647 // Build call __kmpc_push_proc_bind(&loc, global_tid, proc_bind)
2648 llvm::Value *Args[] = {
2649 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
2650 llvm::ConstantInt::get(CGM.IntTy, RuntimeProcBind, /*isSigned=*/true)};
2651 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_proc_bind), Args);
2652}
2653
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002654void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>,
2655 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002656 if (!CGF.HaveInsertPoint())
2657 return;
Alexey Bataevd76df6d2015-02-24 12:55:09 +00002658 // Build call void __kmpc_flush(ident_t *loc)
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002659 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_flush),
2660 emitUpdateLocation(CGF, Loc));
Alexey Bataevcc37cc12014-11-20 04:34:54 +00002661}
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002662
Alexey Bataev62b63b12015-03-10 07:28:44 +00002663namespace {
2664/// \brief Indexes of fields for type kmp_task_t.
2665enum KmpTaskTFields {
2666 /// \brief List of shared variables.
2667 KmpTaskTShareds,
2668 /// \brief Task routine.
2669 KmpTaskTRoutine,
2670 /// \brief Partition id for the untied tasks.
2671 KmpTaskTPartId,
Alexey Bataevad537bb2016-05-30 09:06:50 +00002672 /// Function with call of destructors for private variables.
2673 Data1,
2674 /// Task priority.
2675 Data2,
Alexey Bataev7292c292016-04-25 12:22:29 +00002676 /// (Taskloops only) Lower bound.
2677 KmpTaskTLowerBound,
2678 /// (Taskloops only) Upper bound.
2679 KmpTaskTUpperBound,
2680 /// (Taskloops only) Stride.
2681 KmpTaskTStride,
2682 /// (Taskloops only) Is last iteration flag.
2683 KmpTaskTLastIter,
Alexey Bataev62b63b12015-03-10 07:28:44 +00002684};
Hans Wennborg7eb54642015-09-10 17:07:54 +00002685} // anonymous namespace
Alexey Bataev62b63b12015-03-10 07:28:44 +00002686
Samuel Antaoee8fb302016-01-06 13:42:12 +00002687bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::empty() const {
2688 // FIXME: Add other entries type when they become supported.
2689 return OffloadEntriesTargetRegion.empty();
2690}
2691
2692/// \brief Initialize target region entry.
2693void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
2694 initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
2695 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +00002696 unsigned Order) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00002697 assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is "
2698 "only required for the device "
2699 "code generation.");
Samuel Antao2de62b02016-02-13 23:35:10 +00002700 OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] =
Samuel Antaoee8fb302016-01-06 13:42:12 +00002701 OffloadEntryInfoTargetRegion(Order, /*Addr=*/nullptr, /*ID=*/nullptr);
2702 ++OffloadingEntriesNum;
2703}
2704
2705void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
2706 registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
2707 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +00002708 llvm::Constant *Addr, llvm::Constant *ID) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00002709 // If we are emitting code for a target, the entry is already initialized,
2710 // only has to be registered.
2711 if (CGM.getLangOpts().OpenMPIsDevice) {
Samuel Antao2de62b02016-02-13 23:35:10 +00002712 assert(hasTargetRegionEntryInfo(DeviceID, FileID, ParentName, LineNum) &&
Samuel Antaoee8fb302016-01-06 13:42:12 +00002713 "Entry must exist.");
Samuel Antao2de62b02016-02-13 23:35:10 +00002714 auto &Entry =
2715 OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum];
Samuel Antaoee8fb302016-01-06 13:42:12 +00002716 assert(Entry.isValid() && "Entry not initialized!");
2717 Entry.setAddress(Addr);
2718 Entry.setID(ID);
2719 return;
2720 } else {
2721 OffloadEntryInfoTargetRegion Entry(OffloadingEntriesNum++, Addr, ID);
Samuel Antao2de62b02016-02-13 23:35:10 +00002722 OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = Entry;
Samuel Antaoee8fb302016-01-06 13:42:12 +00002723 }
2724}
2725
2726bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::hasTargetRegionEntryInfo(
Samuel Antao2de62b02016-02-13 23:35:10 +00002727 unsigned DeviceID, unsigned FileID, StringRef ParentName,
2728 unsigned LineNum) const {
Samuel Antaoee8fb302016-01-06 13:42:12 +00002729 auto PerDevice = OffloadEntriesTargetRegion.find(DeviceID);
2730 if (PerDevice == OffloadEntriesTargetRegion.end())
2731 return false;
2732 auto PerFile = PerDevice->second.find(FileID);
2733 if (PerFile == PerDevice->second.end())
2734 return false;
2735 auto PerParentName = PerFile->second.find(ParentName);
2736 if (PerParentName == PerFile->second.end())
2737 return false;
2738 auto PerLine = PerParentName->second.find(LineNum);
2739 if (PerLine == PerParentName->second.end())
2740 return false;
Samuel Antaoee8fb302016-01-06 13:42:12 +00002741 // Fail if this entry is already registered.
Samuel Antao2de62b02016-02-13 23:35:10 +00002742 if (PerLine->second.getAddress() || PerLine->second.getID())
Samuel Antaoee8fb302016-01-06 13:42:12 +00002743 return false;
2744 return true;
2745}
2746
2747void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::actOnTargetRegionEntriesInfo(
2748 const OffloadTargetRegionEntryInfoActTy &Action) {
2749 // Scan all target region entries and perform the provided action.
2750 for (auto &D : OffloadEntriesTargetRegion)
2751 for (auto &F : D.second)
2752 for (auto &P : F.second)
2753 for (auto &L : P.second)
Samuel Antao2de62b02016-02-13 23:35:10 +00002754 Action(D.first, F.first, P.first(), L.first, L.second);
Samuel Antaoee8fb302016-01-06 13:42:12 +00002755}
2756
2757/// \brief Create a Ctor/Dtor-like function whose body is emitted through
2758/// \a Codegen. This is used to emit the two functions that register and
2759/// unregister the descriptor of the current compilation unit.
2760static llvm::Function *
2761createOffloadingBinaryDescriptorFunction(CodeGenModule &CGM, StringRef Name,
2762 const RegionCodeGenTy &Codegen) {
2763 auto &C = CGM.getContext();
2764 FunctionArgList Args;
2765 ImplicitParamDecl DummyPtr(C, /*DC=*/nullptr, SourceLocation(),
2766 /*Id=*/nullptr, C.VoidPtrTy);
2767 Args.push_back(&DummyPtr);
2768
2769 CodeGenFunction CGF(CGM);
2770 GlobalDecl();
John McCallc56a8b32016-03-11 04:30:31 +00002771 auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Samuel Antaoee8fb302016-01-06 13:42:12 +00002772 auto FTy = CGM.getTypes().GetFunctionType(FI);
2773 auto *Fn =
2774 CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, SourceLocation());
2775 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FI, Args, SourceLocation());
2776 Codegen(CGF);
2777 CGF.FinishFunction();
2778 return Fn;
2779}
2780
2781llvm::Function *
2782CGOpenMPRuntime::createOffloadingBinaryDescriptorRegistration() {
2783
2784 // If we don't have entries or if we are emitting code for the device, we
2785 // don't need to do anything.
2786 if (CGM.getLangOpts().OpenMPIsDevice || OffloadEntriesInfoManager.empty())
2787 return nullptr;
2788
2789 auto &M = CGM.getModule();
2790 auto &C = CGM.getContext();
2791
2792 // Get list of devices we care about
2793 auto &Devices = CGM.getLangOpts().OMPTargetTriples;
2794
2795 // We should be creating an offloading descriptor only if there are devices
2796 // specified.
2797 assert(!Devices.empty() && "No OpenMP offloading devices??");
2798
2799 // Create the external variables that will point to the begin and end of the
2800 // host entries section. These will be defined by the linker.
2801 auto *OffloadEntryTy =
2802 CGM.getTypes().ConvertTypeForMem(getTgtOffloadEntryQTy());
2803 llvm::GlobalVariable *HostEntriesBegin = new llvm::GlobalVariable(
2804 M, OffloadEntryTy, /*isConstant=*/true,
Eugene Zelenko1660a5d2016-01-26 19:01:06 +00002805 llvm::GlobalValue::ExternalLinkage, /*Initializer=*/nullptr,
Samuel Antaoee8fb302016-01-06 13:42:12 +00002806 ".omp_offloading.entries_begin");
2807 llvm::GlobalVariable *HostEntriesEnd = new llvm::GlobalVariable(
2808 M, OffloadEntryTy, /*isConstant=*/true,
Eugene Zelenko1660a5d2016-01-26 19:01:06 +00002809 llvm::GlobalValue::ExternalLinkage, /*Initializer=*/nullptr,
Samuel Antaoee8fb302016-01-06 13:42:12 +00002810 ".omp_offloading.entries_end");
2811
2812 // Create all device images
2813 llvm::SmallVector<llvm::Constant *, 4> DeviceImagesEntires;
2814 auto *DeviceImageTy = cast<llvm::StructType>(
2815 CGM.getTypes().ConvertTypeForMem(getTgtDeviceImageQTy()));
2816
2817 for (unsigned i = 0; i < Devices.size(); ++i) {
2818 StringRef T = Devices[i].getTriple();
2819 auto *ImgBegin = new llvm::GlobalVariable(
2820 M, CGM.Int8Ty, /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage,
Eugene Zelenko1660a5d2016-01-26 19:01:06 +00002821 /*Initializer=*/nullptr,
2822 Twine(".omp_offloading.img_start.") + Twine(T));
Samuel Antaoee8fb302016-01-06 13:42:12 +00002823 auto *ImgEnd = new llvm::GlobalVariable(
2824 M, CGM.Int8Ty, /*isConstant=*/true, llvm::GlobalValue::ExternalLinkage,
Eugene Zelenko1660a5d2016-01-26 19:01:06 +00002825 /*Initializer=*/nullptr, Twine(".omp_offloading.img_end.") + Twine(T));
Samuel Antaoee8fb302016-01-06 13:42:12 +00002826
2827 llvm::Constant *Dev =
2828 llvm::ConstantStruct::get(DeviceImageTy, ImgBegin, ImgEnd,
2829 HostEntriesBegin, HostEntriesEnd, nullptr);
2830 DeviceImagesEntires.push_back(Dev);
2831 }
2832
2833 // Create device images global array.
2834 llvm::ArrayType *DeviceImagesInitTy =
2835 llvm::ArrayType::get(DeviceImageTy, DeviceImagesEntires.size());
2836 llvm::Constant *DeviceImagesInit =
2837 llvm::ConstantArray::get(DeviceImagesInitTy, DeviceImagesEntires);
2838
2839 llvm::GlobalVariable *DeviceImages = new llvm::GlobalVariable(
2840 M, DeviceImagesInitTy, /*isConstant=*/true,
2841 llvm::GlobalValue::InternalLinkage, DeviceImagesInit,
2842 ".omp_offloading.device_images");
Peter Collingbournebcf909d2016-06-14 21:02:05 +00002843 DeviceImages->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Samuel Antaoee8fb302016-01-06 13:42:12 +00002844
2845 // This is a Zero array to be used in the creation of the constant expressions
2846 llvm::Constant *Index[] = {llvm::Constant::getNullValue(CGM.Int32Ty),
2847 llvm::Constant::getNullValue(CGM.Int32Ty)};
2848
2849 // Create the target region descriptor.
2850 auto *BinaryDescriptorTy = cast<llvm::StructType>(
2851 CGM.getTypes().ConvertTypeForMem(getTgtBinaryDescriptorQTy()));
2852 llvm::Constant *TargetRegionsDescriptorInit = llvm::ConstantStruct::get(
2853 BinaryDescriptorTy, llvm::ConstantInt::get(CGM.Int32Ty, Devices.size()),
2854 llvm::ConstantExpr::getGetElementPtr(DeviceImagesInitTy, DeviceImages,
2855 Index),
2856 HostEntriesBegin, HostEntriesEnd, nullptr);
2857
2858 auto *Desc = new llvm::GlobalVariable(
2859 M, BinaryDescriptorTy, /*isConstant=*/true,
2860 llvm::GlobalValue::InternalLinkage, TargetRegionsDescriptorInit,
2861 ".omp_offloading.descriptor");
2862
2863 // Emit code to register or unregister the descriptor at execution
2864 // startup or closing, respectively.
2865
2866 // Create a variable to drive the registration and unregistration of the
2867 // descriptor, so we can reuse the logic that emits Ctors and Dtors.
2868 auto *IdentInfo = &C.Idents.get(".omp_offloading.reg_unreg_var");
2869 ImplicitParamDecl RegUnregVar(C, C.getTranslationUnitDecl(), SourceLocation(),
2870 IdentInfo, C.CharTy);
2871
2872 auto *UnRegFn = createOffloadingBinaryDescriptorFunction(
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002873 CGM, ".omp_offloading.descriptor_unreg",
2874 [&](CodeGenFunction &CGF, PrePostActionTy &) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00002875 CGF.EmitCallOrInvoke(createRuntimeFunction(OMPRTL__tgt_unregister_lib),
2876 Desc);
2877 });
2878 auto *RegFn = createOffloadingBinaryDescriptorFunction(
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002879 CGM, ".omp_offloading.descriptor_reg",
2880 [&](CodeGenFunction &CGF, PrePostActionTy &) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00002881 CGF.EmitCallOrInvoke(createRuntimeFunction(OMPRTL__tgt_register_lib),
2882 Desc);
2883 CGM.getCXXABI().registerGlobalDtor(CGF, RegUnregVar, UnRegFn, Desc);
2884 });
2885 return RegFn;
2886}
2887
Samuel Antao2de62b02016-02-13 23:35:10 +00002888void CGOpenMPRuntime::createOffloadEntry(llvm::Constant *ID,
2889 llvm::Constant *Addr, uint64_t Size) {
2890 StringRef Name = Addr->getName();
Samuel Antaoee8fb302016-01-06 13:42:12 +00002891 auto *TgtOffloadEntryType = cast<llvm::StructType>(
2892 CGM.getTypes().ConvertTypeForMem(getTgtOffloadEntryQTy()));
2893 llvm::LLVMContext &C = CGM.getModule().getContext();
2894 llvm::Module &M = CGM.getModule();
2895
2896 // Make sure the address has the right type.
Samuel Antao2de62b02016-02-13 23:35:10 +00002897 llvm::Constant *AddrPtr = llvm::ConstantExpr::getBitCast(ID, CGM.VoidPtrTy);
Samuel Antaoee8fb302016-01-06 13:42:12 +00002898
2899 // Create constant string with the name.
2900 llvm::Constant *StrPtrInit = llvm::ConstantDataArray::getString(C, Name);
2901
2902 llvm::GlobalVariable *Str =
2903 new llvm::GlobalVariable(M, StrPtrInit->getType(), /*isConstant=*/true,
2904 llvm::GlobalValue::InternalLinkage, StrPtrInit,
2905 ".omp_offloading.entry_name");
Peter Collingbournebcf909d2016-06-14 21:02:05 +00002906 Str->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Samuel Antaoee8fb302016-01-06 13:42:12 +00002907 llvm::Constant *StrPtr = llvm::ConstantExpr::getBitCast(Str, CGM.Int8PtrTy);
2908
2909 // Create the entry struct.
2910 llvm::Constant *EntryInit = llvm::ConstantStruct::get(
2911 TgtOffloadEntryType, AddrPtr, StrPtr,
2912 llvm::ConstantInt::get(CGM.SizeTy, Size), nullptr);
2913 llvm::GlobalVariable *Entry = new llvm::GlobalVariable(
2914 M, TgtOffloadEntryType, true, llvm::GlobalValue::ExternalLinkage,
2915 EntryInit, ".omp_offloading.entry");
2916
2917 // The entry has to be created in the section the linker expects it to be.
2918 Entry->setSection(".omp_offloading.entries");
2919 // We can't have any padding between symbols, so we need to have 1-byte
2920 // alignment.
2921 Entry->setAlignment(1);
Samuel Antaoee8fb302016-01-06 13:42:12 +00002922}
2923
2924void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() {
2925 // Emit the offloading entries and metadata so that the device codegen side
2926 // can
2927 // easily figure out what to emit. The produced metadata looks like this:
2928 //
2929 // !omp_offload.info = !{!1, ...}
2930 //
2931 // Right now we only generate metadata for function that contain target
2932 // regions.
2933
2934 // If we do not have entries, we dont need to do anything.
2935 if (OffloadEntriesInfoManager.empty())
2936 return;
2937
2938 llvm::Module &M = CGM.getModule();
2939 llvm::LLVMContext &C = M.getContext();
2940 SmallVector<OffloadEntriesInfoManagerTy::OffloadEntryInfo *, 16>
2941 OrderedEntries(OffloadEntriesInfoManager.size());
2942
2943 // Create the offloading info metadata node.
2944 llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("omp_offload.info");
2945
2946 // Auxiliar methods to create metadata values and strings.
2947 auto getMDInt = [&](unsigned v) {
2948 return llvm::ConstantAsMetadata::get(
2949 llvm::ConstantInt::get(llvm::Type::getInt32Ty(C), v));
2950 };
2951
2952 auto getMDString = [&](StringRef v) { return llvm::MDString::get(C, v); };
2953
2954 // Create function that emits metadata for each target region entry;
2955 auto &&TargetRegionMetadataEmitter = [&](
2956 unsigned DeviceID, unsigned FileID, StringRef ParentName, unsigned Line,
Samuel Antaoee8fb302016-01-06 13:42:12 +00002957 OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion &E) {
2958 llvm::SmallVector<llvm::Metadata *, 32> Ops;
2959 // Generate metadata for target regions. Each entry of this metadata
2960 // contains:
2961 // - Entry 0 -> Kind of this type of metadata (0).
2962 // - Entry 1 -> Device ID of the file where the entry was identified.
2963 // - Entry 2 -> File ID of the file where the entry was identified.
2964 // - Entry 3 -> Mangled name of the function where the entry was identified.
2965 // - Entry 4 -> Line in the file where the entry was identified.
Samuel Antao2de62b02016-02-13 23:35:10 +00002966 // - Entry 5 -> Order the entry was created.
Samuel Antaoee8fb302016-01-06 13:42:12 +00002967 // The first element of the metadata node is the kind.
2968 Ops.push_back(getMDInt(E.getKind()));
2969 Ops.push_back(getMDInt(DeviceID));
2970 Ops.push_back(getMDInt(FileID));
2971 Ops.push_back(getMDString(ParentName));
2972 Ops.push_back(getMDInt(Line));
Samuel Antaoee8fb302016-01-06 13:42:12 +00002973 Ops.push_back(getMDInt(E.getOrder()));
2974
2975 // Save this entry in the right position of the ordered entries array.
2976 OrderedEntries[E.getOrder()] = &E;
2977
2978 // Add metadata to the named metadata node.
2979 MD->addOperand(llvm::MDNode::get(C, Ops));
2980 };
2981
2982 OffloadEntriesInfoManager.actOnTargetRegionEntriesInfo(
2983 TargetRegionMetadataEmitter);
2984
2985 for (auto *E : OrderedEntries) {
2986 assert(E && "All ordered entries must exist!");
2987 if (auto *CE =
2988 dyn_cast<OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion>(
2989 E)) {
2990 assert(CE->getID() && CE->getAddress() &&
2991 "Entry ID and Addr are invalid!");
Samuel Antao2de62b02016-02-13 23:35:10 +00002992 createOffloadEntry(CE->getID(), CE->getAddress(), /*Size=*/0);
Samuel Antaoee8fb302016-01-06 13:42:12 +00002993 } else
2994 llvm_unreachable("Unsupported entry kind.");
2995 }
2996}
2997
2998/// \brief Loads all the offload entries information from the host IR
2999/// metadata.
3000void CGOpenMPRuntime::loadOffloadInfoMetadata() {
3001 // If we are in target mode, load the metadata from the host IR. This code has
3002 // to match the metadaata creation in createOffloadEntriesAndInfoMetadata().
3003
3004 if (!CGM.getLangOpts().OpenMPIsDevice)
3005 return;
3006
3007 if (CGM.getLangOpts().OMPHostIRFile.empty())
3008 return;
3009
3010 auto Buf = llvm::MemoryBuffer::getFile(CGM.getLangOpts().OMPHostIRFile);
3011 if (Buf.getError())
3012 return;
3013
3014 llvm::LLVMContext C;
Peter Collingbourned9445c42016-11-13 07:00:17 +00003015 auto ME = expectedToErrorOrAndEmitErrors(
3016 C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C));
Samuel Antaoee8fb302016-01-06 13:42:12 +00003017
3018 if (ME.getError())
3019 return;
3020
3021 llvm::NamedMDNode *MD = ME.get()->getNamedMetadata("omp_offload.info");
3022 if (!MD)
3023 return;
3024
3025 for (auto I : MD->operands()) {
3026 llvm::MDNode *MN = cast<llvm::MDNode>(I);
3027
3028 auto getMDInt = [&](unsigned Idx) {
3029 llvm::ConstantAsMetadata *V =
3030 cast<llvm::ConstantAsMetadata>(MN->getOperand(Idx));
3031 return cast<llvm::ConstantInt>(V->getValue())->getZExtValue();
3032 };
3033
3034 auto getMDString = [&](unsigned Idx) {
3035 llvm::MDString *V = cast<llvm::MDString>(MN->getOperand(Idx));
3036 return V->getString();
3037 };
3038
3039 switch (getMDInt(0)) {
3040 default:
3041 llvm_unreachable("Unexpected metadata!");
3042 break;
3043 case OffloadEntriesInfoManagerTy::OffloadEntryInfo::
3044 OFFLOAD_ENTRY_INFO_TARGET_REGION:
3045 OffloadEntriesInfoManager.initializeTargetRegionEntryInfo(
3046 /*DeviceID=*/getMDInt(1), /*FileID=*/getMDInt(2),
3047 /*ParentName=*/getMDString(3), /*Line=*/getMDInt(4),
Samuel Antao2de62b02016-02-13 23:35:10 +00003048 /*Order=*/getMDInt(5));
Samuel Antaoee8fb302016-01-06 13:42:12 +00003049 break;
3050 }
3051 }
3052}
3053
Alexey Bataev62b63b12015-03-10 07:28:44 +00003054void CGOpenMPRuntime::emitKmpRoutineEntryT(QualType KmpInt32Ty) {
3055 if (!KmpRoutineEntryPtrTy) {
3056 // Build typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); type.
3057 auto &C = CGM.getContext();
3058 QualType KmpRoutineEntryTyArgs[] = {KmpInt32Ty, C.VoidPtrTy};
3059 FunctionProtoType::ExtProtoInfo EPI;
3060 KmpRoutineEntryPtrQTy = C.getPointerType(
3061 C.getFunctionType(KmpInt32Ty, KmpRoutineEntryTyArgs, EPI));
3062 KmpRoutineEntryPtrTy = CGM.getTypes().ConvertType(KmpRoutineEntryPtrQTy);
3063 }
3064}
3065
Alexey Bataevc71a4092015-09-11 10:29:41 +00003066static FieldDecl *addFieldToRecordDecl(ASTContext &C, DeclContext *DC,
3067 QualType FieldTy) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00003068 auto *Field = FieldDecl::Create(
3069 C, DC, SourceLocation(), SourceLocation(), /*Id=*/nullptr, FieldTy,
3070 C.getTrivialTypeSourceInfo(FieldTy, SourceLocation()),
3071 /*BW=*/nullptr, /*Mutable=*/false, /*InitStyle=*/ICIS_NoInit);
3072 Field->setAccess(AS_public);
3073 DC->addDecl(Field);
Alexey Bataevc71a4092015-09-11 10:29:41 +00003074 return Field;
Alexey Bataev62b63b12015-03-10 07:28:44 +00003075}
3076
Samuel Antaoee8fb302016-01-06 13:42:12 +00003077QualType CGOpenMPRuntime::getTgtOffloadEntryQTy() {
3078
3079 // Make sure the type of the entry is already created. This is the type we
3080 // have to create:
3081 // struct __tgt_offload_entry{
3082 // void *addr; // Pointer to the offload entry info.
3083 // // (function or global)
3084 // char *name; // Name of the function or global.
3085 // size_t size; // Size of the entry info (0 if it a function).
3086 // };
3087 if (TgtOffloadEntryQTy.isNull()) {
3088 ASTContext &C = CGM.getContext();
3089 auto *RD = C.buildImplicitRecord("__tgt_offload_entry");
3090 RD->startDefinition();
3091 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
3092 addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy));
3093 addFieldToRecordDecl(C, RD, C.getSizeType());
3094 RD->completeDefinition();
3095 TgtOffloadEntryQTy = C.getRecordType(RD);
3096 }
3097 return TgtOffloadEntryQTy;
3098}
3099
3100QualType CGOpenMPRuntime::getTgtDeviceImageQTy() {
3101 // These are the types we need to build:
3102 // struct __tgt_device_image{
3103 // void *ImageStart; // Pointer to the target code start.
3104 // void *ImageEnd; // Pointer to the target code end.
3105 // // We also add the host entries to the device image, as it may be useful
3106 // // for the target runtime to have access to that information.
3107 // __tgt_offload_entry *EntriesBegin; // Begin of the table with all
3108 // // the entries.
3109 // __tgt_offload_entry *EntriesEnd; // End of the table with all the
3110 // // entries (non inclusive).
3111 // };
3112 if (TgtDeviceImageQTy.isNull()) {
3113 ASTContext &C = CGM.getContext();
3114 auto *RD = C.buildImplicitRecord("__tgt_device_image");
3115 RD->startDefinition();
3116 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
3117 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
3118 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy()));
3119 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy()));
3120 RD->completeDefinition();
3121 TgtDeviceImageQTy = C.getRecordType(RD);
3122 }
3123 return TgtDeviceImageQTy;
3124}
3125
3126QualType CGOpenMPRuntime::getTgtBinaryDescriptorQTy() {
3127 // struct __tgt_bin_desc{
3128 // int32_t NumDevices; // Number of devices supported.
3129 // __tgt_device_image *DeviceImages; // Arrays of device images
3130 // // (one per device).
3131 // __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
3132 // // entries.
3133 // __tgt_offload_entry *EntriesEnd; // End of the table with all the
3134 // // entries (non inclusive).
3135 // };
3136 if (TgtBinaryDescriptorQTy.isNull()) {
3137 ASTContext &C = CGM.getContext();
3138 auto *RD = C.buildImplicitRecord("__tgt_bin_desc");
3139 RD->startDefinition();
3140 addFieldToRecordDecl(
3141 C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true));
3142 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtDeviceImageQTy()));
3143 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy()));
3144 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy()));
3145 RD->completeDefinition();
3146 TgtBinaryDescriptorQTy = C.getRecordType(RD);
3147 }
3148 return TgtBinaryDescriptorQTy;
3149}
3150
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003151namespace {
Alexey Bataev9e034042015-05-05 04:05:12 +00003152struct PrivateHelpersTy {
3153 PrivateHelpersTy(const VarDecl *Original, const VarDecl *PrivateCopy,
3154 const VarDecl *PrivateElemInit)
3155 : Original(Original), PrivateCopy(PrivateCopy),
3156 PrivateElemInit(PrivateElemInit) {}
3157 const VarDecl *Original;
3158 const VarDecl *PrivateCopy;
3159 const VarDecl *PrivateElemInit;
3160};
3161typedef std::pair<CharUnits /*Align*/, PrivateHelpersTy> PrivateDataTy;
Hans Wennborg7eb54642015-09-10 17:07:54 +00003162} // anonymous namespace
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003163
Alexey Bataev9e034042015-05-05 04:05:12 +00003164static RecordDecl *
Craig Topper8674c5c2015-09-29 04:30:07 +00003165createPrivatesRecordDecl(CodeGenModule &CGM, ArrayRef<PrivateDataTy> Privates) {
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003166 if (!Privates.empty()) {
3167 auto &C = CGM.getContext();
3168 // Build struct .kmp_privates_t. {
3169 // /* private vars */
3170 // };
3171 auto *RD = C.buildImplicitRecord(".kmp_privates.t");
3172 RD->startDefinition();
3173 for (auto &&Pair : Privates) {
Alexey Bataevc71a4092015-09-11 10:29:41 +00003174 auto *VD = Pair.second.Original;
3175 auto Type = VD->getType();
Alexey Bataev1d9c15c2015-05-19 12:31:28 +00003176 Type = Type.getNonReferenceType();
Alexey Bataevc71a4092015-09-11 10:29:41 +00003177 auto *FD = addFieldToRecordDecl(C, RD, Type);
3178 if (VD->hasAttrs()) {
3179 for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()),
3180 E(VD->getAttrs().end());
3181 I != E; ++I)
3182 FD->addAttr(*I);
3183 }
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003184 }
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003185 RD->completeDefinition();
3186 return RD;
3187 }
3188 return nullptr;
3189}
3190
Alexey Bataev9e034042015-05-05 04:05:12 +00003191static RecordDecl *
Alexey Bataev7292c292016-04-25 12:22:29 +00003192createKmpTaskTRecordDecl(CodeGenModule &CGM, OpenMPDirectiveKind Kind,
3193 QualType KmpInt32Ty,
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003194 QualType KmpRoutineEntryPointerQTy) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00003195 auto &C = CGM.getContext();
3196 // Build struct kmp_task_t {
3197 // void * shareds;
3198 // kmp_routine_entry_t routine;
3199 // kmp_int32 part_id;
Alexey Bataevad537bb2016-05-30 09:06:50 +00003200 // kmp_cmplrdata_t data1;
3201 // kmp_cmplrdata_t data2;
Alexey Bataev7292c292016-04-25 12:22:29 +00003202 // For taskloops additional fields:
3203 // kmp_uint64 lb;
3204 // kmp_uint64 ub;
3205 // kmp_int64 st;
3206 // kmp_int32 liter;
Alexey Bataev62b63b12015-03-10 07:28:44 +00003207 // };
Alexey Bataevad537bb2016-05-30 09:06:50 +00003208 auto *UD = C.buildImplicitRecord("kmp_cmplrdata_t", TTK_Union);
3209 UD->startDefinition();
3210 addFieldToRecordDecl(C, UD, KmpInt32Ty);
3211 addFieldToRecordDecl(C, UD, KmpRoutineEntryPointerQTy);
3212 UD->completeDefinition();
3213 QualType KmpCmplrdataTy = C.getRecordType(UD);
Alexey Bataev62b63b12015-03-10 07:28:44 +00003214 auto *RD = C.buildImplicitRecord("kmp_task_t");
3215 RD->startDefinition();
3216 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
3217 addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy);
3218 addFieldToRecordDecl(C, RD, KmpInt32Ty);
Alexey Bataevad537bb2016-05-30 09:06:50 +00003219 addFieldToRecordDecl(C, RD, KmpCmplrdataTy);
3220 addFieldToRecordDecl(C, RD, KmpCmplrdataTy);
Alexey Bataev7292c292016-04-25 12:22:29 +00003221 if (isOpenMPTaskLoopDirective(Kind)) {
3222 QualType KmpUInt64Ty =
3223 CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
3224 QualType KmpInt64Ty =
3225 CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
3226 addFieldToRecordDecl(C, RD, KmpUInt64Ty);
3227 addFieldToRecordDecl(C, RD, KmpUInt64Ty);
3228 addFieldToRecordDecl(C, RD, KmpInt64Ty);
3229 addFieldToRecordDecl(C, RD, KmpInt32Ty);
3230 }
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003231 RD->completeDefinition();
3232 return RD;
3233}
3234
3235static RecordDecl *
3236createKmpTaskTWithPrivatesRecordDecl(CodeGenModule &CGM, QualType KmpTaskTQTy,
Craig Topper8674c5c2015-09-29 04:30:07 +00003237 ArrayRef<PrivateDataTy> Privates) {
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003238 auto &C = CGM.getContext();
3239 // Build struct kmp_task_t_with_privates {
3240 // kmp_task_t task_data;
3241 // .kmp_privates_t. privates;
3242 // };
3243 auto *RD = C.buildImplicitRecord("kmp_task_t_with_privates");
3244 RD->startDefinition();
3245 addFieldToRecordDecl(C, RD, KmpTaskTQTy);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003246 if (auto *PrivateRD = createPrivatesRecordDecl(CGM, Privates)) {
3247 addFieldToRecordDecl(C, RD, C.getRecordType(PrivateRD));
3248 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00003249 RD->completeDefinition();
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003250 return RD;
Alexey Bataev62b63b12015-03-10 07:28:44 +00003251}
3252
3253/// \brief Emit a proxy function which accepts kmp_task_t as the second
3254/// argument.
3255/// \code
3256/// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00003257/// TaskFunction(gtid, tt->part_id, &tt->privates, task_privates_map, tt,
Alexey Bataev7292c292016-04-25 12:22:29 +00003258/// For taskloops:
3259/// tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter,
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003260/// tt->shareds);
Alexey Bataev62b63b12015-03-10 07:28:44 +00003261/// return 0;
3262/// }
3263/// \endcode
3264static llvm::Value *
3265emitProxyTaskFunction(CodeGenModule &CGM, SourceLocation Loc,
Alexey Bataev7292c292016-04-25 12:22:29 +00003266 OpenMPDirectiveKind Kind, QualType KmpInt32Ty,
3267 QualType KmpTaskTWithPrivatesPtrQTy,
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003268 QualType KmpTaskTWithPrivatesQTy, QualType KmpTaskTQTy,
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003269 QualType SharedsPtrTy, llvm::Value *TaskFunction,
3270 llvm::Value *TaskPrivatesMap) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00003271 auto &C = CGM.getContext();
3272 FunctionArgList Args;
3273 ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty);
3274 ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc,
Alexey Bataev2377fe92015-09-10 08:12:02 +00003275 /*Id=*/nullptr,
3276 KmpTaskTWithPrivatesPtrQTy.withRestrict());
Alexey Bataev62b63b12015-03-10 07:28:44 +00003277 Args.push_back(&GtidArg);
3278 Args.push_back(&TaskTypeArg);
Alexey Bataev62b63b12015-03-10 07:28:44 +00003279 auto &TaskEntryFnInfo =
John McCallc56a8b32016-03-11 04:30:31 +00003280 CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args);
Alexey Bataev62b63b12015-03-10 07:28:44 +00003281 auto *TaskEntryTy = CGM.getTypes().GetFunctionType(TaskEntryFnInfo);
3282 auto *TaskEntry =
3283 llvm::Function::Create(TaskEntryTy, llvm::GlobalValue::InternalLinkage,
3284 ".omp_task_entry.", &CGM.getModule());
Akira Hatanaka44a59f82015-10-28 02:30:47 +00003285 CGM.SetInternalFunctionAttributes(/*D=*/nullptr, TaskEntry, TaskEntryFnInfo);
Alexey Bataev62b63b12015-03-10 07:28:44 +00003286 CodeGenFunction CGF(CGM);
3287 CGF.disableDebugInfo();
3288 CGF.StartFunction(GlobalDecl(), KmpInt32Ty, TaskEntry, TaskEntryFnInfo, Args);
3289
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003290 // TaskFunction(gtid, tt->task_data.part_id, &tt->privates, task_privates_map,
Alexey Bataev7292c292016-04-25 12:22:29 +00003291 // tt,
3292 // For taskloops:
3293 // tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter,
3294 // tt->task_data.shareds);
Alexey Bataev62b63b12015-03-10 07:28:44 +00003295 auto *GtidParam = CGF.EmitLoadOfScalar(
John McCall7f416cc2015-09-08 08:05:57 +00003296 CGF.GetAddrOfLocalVar(&GtidArg), /*Volatile=*/false, KmpInt32Ty, Loc);
Alexey Bataev31300ed2016-02-04 11:27:03 +00003297 LValue TDBase = CGF.EmitLoadOfPointerLValue(
3298 CGF.GetAddrOfLocalVar(&TaskTypeArg),
3299 KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003300 auto *KmpTaskTWithPrivatesQTyRD =
3301 cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003302 LValue Base =
3303 CGF.EmitLValueForField(TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin());
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003304 auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl());
3305 auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId);
3306 auto PartIdLVal = CGF.EmitLValueForField(Base, *PartIdFI);
Alexey Bataev48591dd2016-04-20 04:01:36 +00003307 auto *PartidParam = PartIdLVal.getPointer();
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003308
3309 auto SharedsFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTShareds);
3310 auto SharedsLVal = CGF.EmitLValueForField(Base, *SharedsFI);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003311 auto *SharedsParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003312 CGF.EmitLoadOfLValue(SharedsLVal, Loc).getScalarVal(),
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003313 CGF.ConvertTypeForMem(SharedsPtrTy));
3314
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003315 auto PrivatesFI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1);
3316 llvm::Value *PrivatesParam;
3317 if (PrivatesFI != KmpTaskTWithPrivatesQTyRD->field_end()) {
3318 auto PrivatesLVal = CGF.EmitLValueForField(TDBase, *PrivatesFI);
3319 PrivatesParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
John McCall7f416cc2015-09-08 08:05:57 +00003320 PrivatesLVal.getPointer(), CGF.VoidPtrTy);
Alexey Bataev7292c292016-04-25 12:22:29 +00003321 } else
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003322 PrivatesParam = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003323
Alexey Bataev7292c292016-04-25 12:22:29 +00003324 llvm::Value *CommonArgs[] = {GtidParam, PartidParam, PrivatesParam,
3325 TaskPrivatesMap,
3326 CGF.Builder
3327 .CreatePointerBitCastOrAddrSpaceCast(
3328 TDBase.getAddress(), CGF.VoidPtrTy)
3329 .getPointer()};
3330 SmallVector<llvm::Value *, 16> CallArgs(std::begin(CommonArgs),
3331 std::end(CommonArgs));
3332 if (isOpenMPTaskLoopDirective(Kind)) {
3333 auto LBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound);
3334 auto LBLVal = CGF.EmitLValueForField(Base, *LBFI);
3335 auto *LBParam = CGF.EmitLoadOfLValue(LBLVal, Loc).getScalarVal();
3336 auto UBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound);
3337 auto UBLVal = CGF.EmitLValueForField(Base, *UBFI);
3338 auto *UBParam = CGF.EmitLoadOfLValue(UBLVal, Loc).getScalarVal();
3339 auto StFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTStride);
3340 auto StLVal = CGF.EmitLValueForField(Base, *StFI);
3341 auto *StParam = CGF.EmitLoadOfLValue(StLVal, Loc).getScalarVal();
3342 auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter);
3343 auto LILVal = CGF.EmitLValueForField(Base, *LIFI);
3344 auto *LIParam = CGF.EmitLoadOfLValue(LILVal, Loc).getScalarVal();
3345 CallArgs.push_back(LBParam);
3346 CallArgs.push_back(UBParam);
3347 CallArgs.push_back(StParam);
3348 CallArgs.push_back(LIParam);
3349 }
3350 CallArgs.push_back(SharedsParam);
3351
Alexey Bataev62b63b12015-03-10 07:28:44 +00003352 CGF.EmitCallOrInvoke(TaskFunction, CallArgs);
3353 CGF.EmitStoreThroughLValue(
3354 RValue::get(CGF.Builder.getInt32(/*C=*/0)),
John McCall7f416cc2015-09-08 08:05:57 +00003355 CGF.MakeAddrLValue(CGF.ReturnValue, KmpInt32Ty));
Alexey Bataev62b63b12015-03-10 07:28:44 +00003356 CGF.FinishFunction();
3357 return TaskEntry;
3358}
3359
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003360static llvm::Value *emitDestructorsFunction(CodeGenModule &CGM,
3361 SourceLocation Loc,
3362 QualType KmpInt32Ty,
3363 QualType KmpTaskTWithPrivatesPtrQTy,
3364 QualType KmpTaskTWithPrivatesQTy) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00003365 auto &C = CGM.getContext();
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003366 FunctionArgList Args;
3367 ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty);
3368 ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc,
Alexey Bataev2377fe92015-09-10 08:12:02 +00003369 /*Id=*/nullptr,
3370 KmpTaskTWithPrivatesPtrQTy.withRestrict());
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003371 Args.push_back(&GtidArg);
3372 Args.push_back(&TaskTypeArg);
3373 FunctionType::ExtInfo Info;
3374 auto &DestructorFnInfo =
John McCallc56a8b32016-03-11 04:30:31 +00003375 CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003376 auto *DestructorFnTy = CGM.getTypes().GetFunctionType(DestructorFnInfo);
3377 auto *DestructorFn =
3378 llvm::Function::Create(DestructorFnTy, llvm::GlobalValue::InternalLinkage,
3379 ".omp_task_destructor.", &CGM.getModule());
Akira Hatanaka44a59f82015-10-28 02:30:47 +00003380 CGM.SetInternalFunctionAttributes(/*D=*/nullptr, DestructorFn,
3381 DestructorFnInfo);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003382 CodeGenFunction CGF(CGM);
3383 CGF.disableDebugInfo();
3384 CGF.StartFunction(GlobalDecl(), KmpInt32Ty, DestructorFn, DestructorFnInfo,
3385 Args);
3386
Alexey Bataev31300ed2016-02-04 11:27:03 +00003387 LValue Base = CGF.EmitLoadOfPointerLValue(
3388 CGF.GetAddrOfLocalVar(&TaskTypeArg),
3389 KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003390 auto *KmpTaskTWithPrivatesQTyRD =
3391 cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl());
3392 auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin());
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003393 Base = CGF.EmitLValueForField(Base, *FI);
3394 for (auto *Field :
3395 cast<RecordDecl>(FI->getType()->getAsTagDecl())->fields()) {
3396 if (auto DtorKind = Field->getType().isDestructedType()) {
3397 auto FieldLValue = CGF.EmitLValueForField(Base, Field);
3398 CGF.pushDestroy(DtorKind, FieldLValue.getAddress(), Field->getType());
3399 }
3400 }
3401 CGF.FinishFunction();
3402 return DestructorFn;
3403}
3404
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003405/// \brief Emit a privates mapping function for correct handling of private and
3406/// firstprivate variables.
3407/// \code
3408/// void .omp_task_privates_map.(const .privates. *noalias privs, <ty1>
3409/// **noalias priv1,..., <tyn> **noalias privn) {
3410/// *priv1 = &.privates.priv1;
3411/// ...;
3412/// *privn = &.privates.privn;
3413/// }
3414/// \endcode
3415static llvm::Value *
3416emitTaskPrivateMappingFunction(CodeGenModule &CGM, SourceLocation Loc,
Craig Topper8674c5c2015-09-29 04:30:07 +00003417 ArrayRef<const Expr *> PrivateVars,
3418 ArrayRef<const Expr *> FirstprivateVars,
Alexey Bataevf93095a2016-05-05 08:46:22 +00003419 ArrayRef<const Expr *> LastprivateVars,
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003420 QualType PrivatesQTy,
Craig Topper8674c5c2015-09-29 04:30:07 +00003421 ArrayRef<PrivateDataTy> Privates) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003422 auto &C = CGM.getContext();
3423 FunctionArgList Args;
3424 ImplicitParamDecl TaskPrivatesArg(
3425 C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
3426 C.getPointerType(PrivatesQTy).withConst().withRestrict());
3427 Args.push_back(&TaskPrivatesArg);
3428 llvm::DenseMap<const VarDecl *, unsigned> PrivateVarsPos;
3429 unsigned Counter = 1;
3430 for (auto *E: PrivateVars) {
3431 Args.push_back(ImplicitParamDecl::Create(
3432 C, /*DC=*/nullptr, Loc,
3433 /*Id=*/nullptr, C.getPointerType(C.getPointerType(E->getType()))
3434 .withConst()
3435 .withRestrict()));
3436 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
3437 PrivateVarsPos[VD] = Counter;
3438 ++Counter;
3439 }
3440 for (auto *E : FirstprivateVars) {
3441 Args.push_back(ImplicitParamDecl::Create(
3442 C, /*DC=*/nullptr, Loc,
3443 /*Id=*/nullptr, C.getPointerType(C.getPointerType(E->getType()))
3444 .withConst()
3445 .withRestrict()));
3446 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
3447 PrivateVarsPos[VD] = Counter;
3448 ++Counter;
3449 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00003450 for (auto *E: LastprivateVars) {
3451 Args.push_back(ImplicitParamDecl::Create(
3452 C, /*DC=*/nullptr, Loc,
3453 /*Id=*/nullptr, C.getPointerType(C.getPointerType(E->getType()))
3454 .withConst()
3455 .withRestrict()));
3456 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
3457 PrivateVarsPos[VD] = Counter;
3458 ++Counter;
3459 }
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003460 auto &TaskPrivatesMapFnInfo =
John McCallc56a8b32016-03-11 04:30:31 +00003461 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003462 auto *TaskPrivatesMapTy =
3463 CGM.getTypes().GetFunctionType(TaskPrivatesMapFnInfo);
3464 auto *TaskPrivatesMap = llvm::Function::Create(
3465 TaskPrivatesMapTy, llvm::GlobalValue::InternalLinkage,
3466 ".omp_task_privates_map.", &CGM.getModule());
Akira Hatanaka44a59f82015-10-28 02:30:47 +00003467 CGM.SetInternalFunctionAttributes(/*D=*/nullptr, TaskPrivatesMap,
3468 TaskPrivatesMapFnInfo);
Evgeniy Stepanov6b2a61d2015-09-14 21:35:16 +00003469 TaskPrivatesMap->addFnAttr(llvm::Attribute::AlwaysInline);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003470 CodeGenFunction CGF(CGM);
3471 CGF.disableDebugInfo();
3472 CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskPrivatesMap,
3473 TaskPrivatesMapFnInfo, Args);
3474
3475 // *privi = &.privates.privi;
Alexey Bataev31300ed2016-02-04 11:27:03 +00003476 LValue Base = CGF.EmitLoadOfPointerLValue(
3477 CGF.GetAddrOfLocalVar(&TaskPrivatesArg),
3478 TaskPrivatesArg.getType()->castAs<PointerType>());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003479 auto *PrivatesQTyRD = cast<RecordDecl>(PrivatesQTy->getAsTagDecl());
3480 Counter = 0;
3481 for (auto *Field : PrivatesQTyRD->fields()) {
3482 auto FieldLVal = CGF.EmitLValueForField(Base, Field);
3483 auto *VD = Args[PrivateVarsPos[Privates[Counter].second.Original]];
John McCall7f416cc2015-09-08 08:05:57 +00003484 auto RefLVal = CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType());
Alexey Bataev31300ed2016-02-04 11:27:03 +00003485 auto RefLoadLVal = CGF.EmitLoadOfPointerLValue(
3486 RefLVal.getAddress(), RefLVal.getType()->castAs<PointerType>());
Alexey Bataev2377fe92015-09-10 08:12:02 +00003487 CGF.EmitStoreOfScalar(FieldLVal.getPointer(), RefLoadLVal);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003488 ++Counter;
3489 }
3490 CGF.FinishFunction();
3491 return TaskPrivatesMap;
3492}
3493
Alexey Bataev9e034042015-05-05 04:05:12 +00003494static int array_pod_sort_comparator(const PrivateDataTy *P1,
3495 const PrivateDataTy *P2) {
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003496 return P1->first < P2->first ? 1 : (P2->first < P1->first ? -1 : 0);
3497}
3498
Alexey Bataevf93095a2016-05-05 08:46:22 +00003499/// Emit initialization for private variables in task-based directives.
Alexey Bataev8a831592016-05-10 10:36:51 +00003500static void emitPrivatesInit(CodeGenFunction &CGF,
Alexey Bataevf93095a2016-05-05 08:46:22 +00003501 const OMPExecutableDirective &D,
3502 Address KmpTaskSharedsPtr, LValue TDBase,
3503 const RecordDecl *KmpTaskTWithPrivatesQTyRD,
3504 QualType SharedsTy, QualType SharedsPtrTy,
3505 const OMPTaskDataTy &Data,
3506 ArrayRef<PrivateDataTy> Privates, bool ForDup) {
3507 auto &C = CGF.getContext();
Alexey Bataevf93095a2016-05-05 08:46:22 +00003508 auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin());
3509 LValue PrivatesBase = CGF.EmitLValueForField(TDBase, *FI);
3510 LValue SrcBase;
3511 if (!Data.FirstprivateVars.empty()) {
3512 SrcBase = CGF.MakeAddrLValue(
3513 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
3514 KmpTaskSharedsPtr, CGF.ConvertTypeForMem(SharedsPtrTy)),
3515 SharedsTy);
3516 }
3517 CodeGenFunction::CGCapturedStmtInfo CapturesInfo(
3518 cast<CapturedStmt>(*D.getAssociatedStmt()));
3519 FI = cast<RecordDecl>(FI->getType()->getAsTagDecl())->field_begin();
3520 for (auto &&Pair : Privates) {
3521 auto *VD = Pair.second.PrivateCopy;
3522 auto *Init = VD->getAnyInitializer();
Alexey Bataevf93095a2016-05-05 08:46:22 +00003523 if (Init && (!ForDup || (isa<CXXConstructExpr>(Init) &&
3524 !CGF.isTrivialInitializer(Init)))) {
Alexey Bataev8a831592016-05-10 10:36:51 +00003525 LValue PrivateLValue = CGF.EmitLValueForField(PrivatesBase, *FI);
Alexey Bataevf93095a2016-05-05 08:46:22 +00003526 if (auto *Elem = Pair.second.PrivateElemInit) {
3527 auto *OriginalVD = Pair.second.Original;
3528 auto *SharedField = CapturesInfo.lookup(OriginalVD);
3529 auto SharedRefLValue = CGF.EmitLValueForField(SrcBase, SharedField);
3530 SharedRefLValue = CGF.MakeAddrLValue(
3531 Address(SharedRefLValue.getPointer(), C.getDeclAlign(OriginalVD)),
3532 SharedRefLValue.getType(), AlignmentSource::Decl);
3533 QualType Type = OriginalVD->getType();
3534 if (Type->isArrayType()) {
3535 // Initialize firstprivate array.
3536 if (!isa<CXXConstructExpr>(Init) || CGF.isTrivialInitializer(Init)) {
3537 // Perform simple memcpy.
3538 CGF.EmitAggregateAssign(PrivateLValue.getAddress(),
3539 SharedRefLValue.getAddress(), Type);
3540 } else {
3541 // Initialize firstprivate array using element-by-element
3542 // intialization.
3543 CGF.EmitOMPAggregateAssign(
3544 PrivateLValue.getAddress(), SharedRefLValue.getAddress(), Type,
3545 [&CGF, Elem, Init, &CapturesInfo](Address DestElement,
3546 Address SrcElement) {
3547 // Clean up any temporaries needed by the initialization.
3548 CodeGenFunction::OMPPrivateScope InitScope(CGF);
3549 InitScope.addPrivate(
3550 Elem, [SrcElement]() -> Address { return SrcElement; });
3551 (void)InitScope.Privatize();
3552 // Emit initialization for single element.
3553 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(
3554 CGF, &CapturesInfo);
3555 CGF.EmitAnyExprToMem(Init, DestElement,
3556 Init->getType().getQualifiers(),
3557 /*IsInitializer=*/false);
3558 });
3559 }
3560 } else {
3561 CodeGenFunction::OMPPrivateScope InitScope(CGF);
3562 InitScope.addPrivate(Elem, [SharedRefLValue]() -> Address {
3563 return SharedRefLValue.getAddress();
3564 });
3565 (void)InitScope.Privatize();
3566 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CapturesInfo);
3567 CGF.EmitExprAsInit(Init, VD, PrivateLValue,
3568 /*capturedByInit=*/false);
3569 }
3570 } else
3571 CGF.EmitExprAsInit(Init, VD, PrivateLValue, /*capturedByInit=*/false);
3572 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00003573 ++FI;
3574 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00003575}
3576
3577/// Check if duplication function is required for taskloops.
3578static bool checkInitIsRequired(CodeGenFunction &CGF,
3579 ArrayRef<PrivateDataTy> Privates) {
3580 bool InitRequired = false;
3581 for (auto &&Pair : Privates) {
3582 auto *VD = Pair.second.PrivateCopy;
3583 auto *Init = VD->getAnyInitializer();
3584 InitRequired = InitRequired || (Init && isa<CXXConstructExpr>(Init) &&
3585 !CGF.isTrivialInitializer(Init));
3586 }
3587 return InitRequired;
3588}
3589
3590
3591/// Emit task_dup function (for initialization of
3592/// private/firstprivate/lastprivate vars and last_iter flag)
3593/// \code
3594/// void __task_dup_entry(kmp_task_t *task_dst, const kmp_task_t *task_src, int
3595/// lastpriv) {
3596/// // setup lastprivate flag
3597/// task_dst->last = lastpriv;
3598/// // could be constructor calls here...
3599/// }
3600/// \endcode
3601static llvm::Value *
3602emitTaskDupFunction(CodeGenModule &CGM, SourceLocation Loc,
3603 const OMPExecutableDirective &D,
3604 QualType KmpTaskTWithPrivatesPtrQTy,
3605 const RecordDecl *KmpTaskTWithPrivatesQTyRD,
3606 const RecordDecl *KmpTaskTQTyRD, QualType SharedsTy,
3607 QualType SharedsPtrTy, const OMPTaskDataTy &Data,
3608 ArrayRef<PrivateDataTy> Privates, bool WithLastIter) {
3609 auto &C = CGM.getContext();
3610 FunctionArgList Args;
3611 ImplicitParamDecl DstArg(C, /*DC=*/nullptr, Loc,
3612 /*Id=*/nullptr, KmpTaskTWithPrivatesPtrQTy);
3613 ImplicitParamDecl SrcArg(C, /*DC=*/nullptr, Loc,
3614 /*Id=*/nullptr, KmpTaskTWithPrivatesPtrQTy);
3615 ImplicitParamDecl LastprivArg(C, /*DC=*/nullptr, Loc,
3616 /*Id=*/nullptr, C.IntTy);
3617 Args.push_back(&DstArg);
3618 Args.push_back(&SrcArg);
3619 Args.push_back(&LastprivArg);
3620 auto &TaskDupFnInfo =
3621 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
3622 auto *TaskDupTy = CGM.getTypes().GetFunctionType(TaskDupFnInfo);
3623 auto *TaskDup =
3624 llvm::Function::Create(TaskDupTy, llvm::GlobalValue::InternalLinkage,
3625 ".omp_task_dup.", &CGM.getModule());
3626 CGM.SetInternalFunctionAttributes(/*D=*/nullptr, TaskDup, TaskDupFnInfo);
3627 CodeGenFunction CGF(CGM);
3628 CGF.disableDebugInfo();
3629 CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskDup, TaskDupFnInfo, Args);
3630
3631 LValue TDBase = CGF.EmitLoadOfPointerLValue(
3632 CGF.GetAddrOfLocalVar(&DstArg),
3633 KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
3634 // task_dst->liter = lastpriv;
3635 if (WithLastIter) {
3636 auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter);
3637 LValue Base = CGF.EmitLValueForField(
3638 TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin());
3639 LValue LILVal = CGF.EmitLValueForField(Base, *LIFI);
3640 llvm::Value *Lastpriv = CGF.EmitLoadOfScalar(
3641 CGF.GetAddrOfLocalVar(&LastprivArg), /*Volatile=*/false, C.IntTy, Loc);
3642 CGF.EmitStoreOfScalar(Lastpriv, LILVal);
3643 }
3644
3645 // Emit initial values for private copies (if any).
3646 assert(!Privates.empty());
3647 Address KmpTaskSharedsPtr = Address::invalid();
3648 if (!Data.FirstprivateVars.empty()) {
3649 LValue TDBase = CGF.EmitLoadOfPointerLValue(
3650 CGF.GetAddrOfLocalVar(&SrcArg),
3651 KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
3652 LValue Base = CGF.EmitLValueForField(
3653 TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin());
3654 KmpTaskSharedsPtr = Address(
3655 CGF.EmitLoadOfScalar(CGF.EmitLValueForField(
3656 Base, *std::next(KmpTaskTQTyRD->field_begin(),
3657 KmpTaskTShareds)),
3658 Loc),
3659 CGF.getNaturalTypeAlignment(SharedsTy));
3660 }
Alexey Bataev8a831592016-05-10 10:36:51 +00003661 emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, TDBase, KmpTaskTWithPrivatesQTyRD,
3662 SharedsTy, SharedsPtrTy, Data, Privates, /*ForDup=*/true);
Alexey Bataevf93095a2016-05-05 08:46:22 +00003663 CGF.FinishFunction();
3664 return TaskDup;
3665}
3666
Alexey Bataev8a831592016-05-10 10:36:51 +00003667/// Checks if destructor function is required to be generated.
3668/// \return true if cleanups are required, false otherwise.
3669static bool
3670checkDestructorsRequired(const RecordDecl *KmpTaskTWithPrivatesQTyRD) {
3671 bool NeedsCleanup = false;
3672 auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin());
3673 auto *PrivateRD = cast<RecordDecl>(FI->getType()->getAsTagDecl());
3674 for (auto *FD : PrivateRD->fields()) {
3675 NeedsCleanup = NeedsCleanup || FD->getType().isDestructedType();
3676 if (NeedsCleanup)
3677 break;
3678 }
3679 return NeedsCleanup;
3680}
3681
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003682CGOpenMPRuntime::TaskResultTy
3683CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
3684 const OMPExecutableDirective &D,
3685 llvm::Value *TaskFunction, QualType SharedsTy,
3686 Address Shareds, const OMPTaskDataTy &Data) {
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003687 auto &C = CGM.getContext();
Alexey Bataev7292c292016-04-25 12:22:29 +00003688 llvm::SmallVector<PrivateDataTy, 4> Privates;
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003689 // Aggregate privates and sort them by the alignment.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003690 auto I = Data.PrivateCopies.begin();
3691 for (auto *E : Data.PrivateVars) {
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003692 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
3693 Privates.push_back(std::make_pair(
Alexey Bataevc71a4092015-09-11 10:29:41 +00003694 C.getDeclAlign(VD),
Alexey Bataev9e034042015-05-05 04:05:12 +00003695 PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()),
3696 /*PrivateElemInit=*/nullptr)));
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003697 ++I;
3698 }
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003699 I = Data.FirstprivateCopies.begin();
3700 auto IElemInitRef = Data.FirstprivateInits.begin();
3701 for (auto *E : Data.FirstprivateVars) {
Alexey Bataev9e034042015-05-05 04:05:12 +00003702 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
3703 Privates.push_back(std::make_pair(
Alexey Bataevc71a4092015-09-11 10:29:41 +00003704 C.getDeclAlign(VD),
Alexey Bataev9e034042015-05-05 04:05:12 +00003705 PrivateHelpersTy(
3706 VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()),
3707 cast<VarDecl>(cast<DeclRefExpr>(*IElemInitRef)->getDecl()))));
Richard Trieucc3949d2016-02-18 22:34:54 +00003708 ++I;
3709 ++IElemInitRef;
Alexey Bataev9e034042015-05-05 04:05:12 +00003710 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00003711 I = Data.LastprivateCopies.begin();
3712 for (auto *E : Data.LastprivateVars) {
3713 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
3714 Privates.push_back(std::make_pair(
3715 C.getDeclAlign(VD),
3716 PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()),
3717 /*PrivateElemInit=*/nullptr)));
3718 ++I;
3719 }
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003720 llvm::array_pod_sort(Privates.begin(), Privates.end(),
3721 array_pod_sort_comparator);
Alexey Bataev62b63b12015-03-10 07:28:44 +00003722 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
3723 // Build type kmp_routine_entry_t (if not built yet).
3724 emitKmpRoutineEntryT(KmpInt32Ty);
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003725 // Build type kmp_task_t (if not built yet).
3726 if (KmpTaskTQTy.isNull()) {
Alexey Bataev7292c292016-04-25 12:22:29 +00003727 KmpTaskTQTy = C.getRecordType(createKmpTaskTRecordDecl(
3728 CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy));
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003729 }
3730 auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl());
Alexey Bataev62b63b12015-03-10 07:28:44 +00003731 // Build particular struct kmp_task_t for the given task.
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003732 auto *KmpTaskTWithPrivatesQTyRD =
3733 createKmpTaskTWithPrivatesRecordDecl(CGM, KmpTaskTQTy, Privates);
3734 auto KmpTaskTWithPrivatesQTy = C.getRecordType(KmpTaskTWithPrivatesQTyRD);
3735 QualType KmpTaskTWithPrivatesPtrQTy =
3736 C.getPointerType(KmpTaskTWithPrivatesQTy);
3737 auto *KmpTaskTWithPrivatesTy = CGF.ConvertType(KmpTaskTWithPrivatesQTy);
3738 auto *KmpTaskTWithPrivatesPtrTy = KmpTaskTWithPrivatesTy->getPointerTo();
Alexey Bataev1189bd02016-01-26 12:20:39 +00003739 auto *KmpTaskTWithPrivatesTySize = CGF.getTypeSize(KmpTaskTWithPrivatesQTy);
Alexey Bataev62b63b12015-03-10 07:28:44 +00003740 QualType SharedsPtrTy = C.getPointerType(SharedsTy);
3741
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003742 // Emit initial values for private copies (if any).
3743 llvm::Value *TaskPrivatesMap = nullptr;
3744 auto *TaskPrivatesMapTy =
3745 std::next(cast<llvm::Function>(TaskFunction)->getArgumentList().begin(),
3746 3)
3747 ->getType();
3748 if (!Privates.empty()) {
3749 auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin());
Alexey Bataevf93095a2016-05-05 08:46:22 +00003750 TaskPrivatesMap = emitTaskPrivateMappingFunction(
3751 CGM, Loc, Data.PrivateVars, Data.FirstprivateVars, Data.LastprivateVars,
3752 FI->getType(), Privates);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00003753 TaskPrivatesMap = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
3754 TaskPrivatesMap, TaskPrivatesMapTy);
3755 } else {
3756 TaskPrivatesMap = llvm::ConstantPointerNull::get(
3757 cast<llvm::PointerType>(TaskPrivatesMapTy));
3758 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00003759 // Build a proxy function kmp_int32 .omp_task_entry.(kmp_int32 gtid,
3760 // kmp_task_t *tt);
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003761 auto *TaskEntry = emitProxyTaskFunction(
Alexey Bataev7292c292016-04-25 12:22:29 +00003762 CGM, Loc, D.getDirectiveKind(), KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy,
3763 KmpTaskTWithPrivatesQTy, KmpTaskTQTy, SharedsPtrTy, TaskFunction,
3764 TaskPrivatesMap);
Alexey Bataev62b63b12015-03-10 07:28:44 +00003765
3766 // Build call kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
3767 // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
3768 // kmp_routine_entry_t *task_entry);
3769 // Task flags. Format is taken from
3770 // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h,
3771 // description of kmp_tasking_flags struct.
Alexey Bataev1e1e2862016-05-10 12:21:02 +00003772 enum {
3773 TiedFlag = 0x1,
3774 FinalFlag = 0x2,
3775 DestructorsFlag = 0x8,
3776 PriorityFlag = 0x20
3777 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003778 unsigned Flags = Data.Tied ? TiedFlag : 0;
Alexey Bataev8a831592016-05-10 10:36:51 +00003779 bool NeedsCleanup = false;
3780 if (!Privates.empty()) {
3781 NeedsCleanup = checkDestructorsRequired(KmpTaskTWithPrivatesQTyRD);
3782 if (NeedsCleanup)
3783 Flags = Flags | DestructorsFlag;
3784 }
Alexey Bataev1e1e2862016-05-10 12:21:02 +00003785 if (Data.Priority.getInt())
3786 Flags = Flags | PriorityFlag;
Alexey Bataev62b63b12015-03-10 07:28:44 +00003787 auto *TaskFlags =
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003788 Data.Final.getPointer()
3789 ? CGF.Builder.CreateSelect(Data.Final.getPointer(),
Alexey Bataev62b63b12015-03-10 07:28:44 +00003790 CGF.Builder.getInt32(FinalFlag),
3791 CGF.Builder.getInt32(/*C=*/0))
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003792 : CGF.Builder.getInt32(Data.Final.getInt() ? FinalFlag : 0);
Alexey Bataev62b63b12015-03-10 07:28:44 +00003793 TaskFlags = CGF.Builder.CreateOr(TaskFlags, CGF.Builder.getInt32(Flags));
Alexey Bataev40e36f12015-11-24 13:01:44 +00003794 auto *SharedsSize = CGM.getSize(C.getTypeSizeInChars(SharedsTy));
Alexey Bataevf24e7b12015-10-08 09:10:53 +00003795 llvm::Value *AllocArgs[] = {emitUpdateLocation(CGF, Loc),
3796 getThreadID(CGF, Loc), TaskFlags,
3797 KmpTaskTWithPrivatesTySize, SharedsSize,
3798 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
3799 TaskEntry, KmpRoutineEntryPtrTy)};
Alexey Bataev62b63b12015-03-10 07:28:44 +00003800 auto *NewTask = CGF.EmitRuntimeCall(
3801 createRuntimeFunction(OMPRTL__kmpc_omp_task_alloc), AllocArgs);
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003802 auto *NewTaskNewTaskTTy = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
3803 NewTask, KmpTaskTWithPrivatesPtrTy);
3804 LValue Base = CGF.MakeNaturalAlignAddrLValue(NewTaskNewTaskTTy,
3805 KmpTaskTWithPrivatesQTy);
3806 LValue TDBase =
3807 CGF.EmitLValueForField(Base, *KmpTaskTWithPrivatesQTyRD->field_begin());
Alexey Bataev62b63b12015-03-10 07:28:44 +00003808 // Fill the data in the resulting kmp_task_t record.
3809 // Copy shareds if there are any.
John McCall7f416cc2015-09-08 08:05:57 +00003810 Address KmpTaskSharedsPtr = Address::invalid();
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003811 if (!SharedsTy->getAsStructureType()->getDecl()->field_empty()) {
Alexey Bataev2377fe92015-09-10 08:12:02 +00003812 KmpTaskSharedsPtr =
3813 Address(CGF.EmitLoadOfScalar(
3814 CGF.EmitLValueForField(
3815 TDBase, *std::next(KmpTaskTQTyRD->field_begin(),
3816 KmpTaskTShareds)),
3817 Loc),
3818 CGF.getNaturalTypeAlignment(SharedsTy));
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003819 CGF.EmitAggregateCopy(KmpTaskSharedsPtr, Shareds, SharedsTy);
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00003820 }
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003821 // Emit initial values for private copies (if any).
Alexey Bataevf93095a2016-05-05 08:46:22 +00003822 TaskResultTy Result;
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003823 if (!Privates.empty()) {
Alexey Bataev8a831592016-05-10 10:36:51 +00003824 emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, Base, KmpTaskTWithPrivatesQTyRD,
3825 SharedsTy, SharedsPtrTy, Data, Privates,
3826 /*ForDup=*/false);
Alexey Bataevf93095a2016-05-05 08:46:22 +00003827 if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) &&
3828 (!Data.LastprivateVars.empty() || checkInitIsRequired(CGF, Privates))) {
3829 Result.TaskDupFn = emitTaskDupFunction(
3830 CGM, Loc, D, KmpTaskTWithPrivatesPtrQTy, KmpTaskTWithPrivatesQTyRD,
3831 KmpTaskTQTyRD, SharedsTy, SharedsPtrTy, Data, Privates,
3832 /*WithLastIter=*/!Data.LastprivateVars.empty());
Alexey Bataev36c1eb92015-04-30 06:51:57 +00003833 }
3834 }
Alexey Bataevad537bb2016-05-30 09:06:50 +00003835 // Fields of union "kmp_cmplrdata_t" for destructors and priority.
3836 enum { Priority = 0, Destructors = 1 };
Alexey Bataev62b63b12015-03-10 07:28:44 +00003837 // Provide pointer to function with destructors for privates.
Alexey Bataevad537bb2016-05-30 09:06:50 +00003838 auto FI = std::next(KmpTaskTQTyRD->field_begin(), Data1);
3839 auto *KmpCmplrdataUD = (*FI)->getType()->getAsUnionType()->getDecl();
3840 if (NeedsCleanup) {
3841 llvm::Value *DestructorFn = emitDestructorsFunction(
3842 CGM, Loc, KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy,
3843 KmpTaskTWithPrivatesQTy);
3844 LValue Data1LV = CGF.EmitLValueForField(TDBase, *FI);
3845 LValue DestructorsLV = CGF.EmitLValueForField(
3846 Data1LV, *std::next(KmpCmplrdataUD->field_begin(), Destructors));
3847 CGF.EmitStoreOfScalar(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
3848 DestructorFn, KmpRoutineEntryPtrTy),
3849 DestructorsLV);
3850 }
3851 // Set priority.
3852 if (Data.Priority.getInt()) {
3853 LValue Data2LV = CGF.EmitLValueForField(
3854 TDBase, *std::next(KmpTaskTQTyRD->field_begin(), Data2));
3855 LValue PriorityLV = CGF.EmitLValueForField(
3856 Data2LV, *std::next(KmpCmplrdataUD->field_begin(), Priority));
3857 CGF.EmitStoreOfScalar(Data.Priority.getPointer(), PriorityLV);
3858 }
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003859 Result.NewTask = NewTask;
3860 Result.TaskEntry = TaskEntry;
3861 Result.NewTaskNewTaskTTy = NewTaskNewTaskTTy;
3862 Result.TDBase = TDBase;
3863 Result.KmpTaskTQTyRD = KmpTaskTQTyRD;
3864 return Result;
Alexey Bataev7292c292016-04-25 12:22:29 +00003865}
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003866
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003867void CGOpenMPRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
3868 const OMPExecutableDirective &D,
3869 llvm::Value *TaskFunction,
3870 QualType SharedsTy, Address Shareds,
3871 const Expr *IfCond,
3872 const OMPTaskDataTy &Data) {
Alexey Bataev7292c292016-04-25 12:22:29 +00003873 if (!CGF.HaveInsertPoint())
3874 return;
3875
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003876 TaskResultTy Result =
3877 emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data);
3878 llvm::Value *NewTask = Result.NewTask;
3879 llvm::Value *TaskEntry = Result.TaskEntry;
3880 llvm::Value *NewTaskNewTaskTTy = Result.NewTaskNewTaskTTy;
3881 LValue TDBase = Result.TDBase;
3882 RecordDecl *KmpTaskTQTyRD = Result.KmpTaskTQTyRD;
Alexey Bataev7292c292016-04-25 12:22:29 +00003883 auto &C = CGM.getContext();
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003884 // Process list of dependences.
John McCall7f416cc2015-09-08 08:05:57 +00003885 Address DependenciesArray = Address::invalid();
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003886 unsigned NumDependencies = Data.Dependences.size();
John McCall7f416cc2015-09-08 08:05:57 +00003887 if (NumDependencies) {
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003888 // Dependence kind for RTL.
Alexey Bataev92e82f92015-11-23 13:33:42 +00003889 enum RTLDependenceKindTy { DepIn = 0x01, DepInOut = 0x3 };
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003890 enum RTLDependInfoFieldsTy { BaseAddr, Len, Flags };
3891 RecordDecl *KmpDependInfoRD;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00003892 QualType FlagsTy =
3893 C.getIntTypeForBitwidth(C.getTypeSize(C.BoolTy), /*Signed=*/false);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003894 llvm::Type *LLVMFlagsTy = CGF.ConvertTypeForMem(FlagsTy);
3895 if (KmpDependInfoTy.isNull()) {
3896 KmpDependInfoRD = C.buildImplicitRecord("kmp_depend_info");
3897 KmpDependInfoRD->startDefinition();
3898 addFieldToRecordDecl(C, KmpDependInfoRD, C.getIntPtrType());
3899 addFieldToRecordDecl(C, KmpDependInfoRD, C.getSizeType());
3900 addFieldToRecordDecl(C, KmpDependInfoRD, FlagsTy);
3901 KmpDependInfoRD->completeDefinition();
3902 KmpDependInfoTy = C.getRecordType(KmpDependInfoRD);
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003903 } else
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003904 KmpDependInfoRD = cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl());
John McCall7f416cc2015-09-08 08:05:57 +00003905 CharUnits DependencySize = C.getTypeSizeInChars(KmpDependInfoTy);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003906 // Define type kmp_depend_info[<Dependences.size()>];
3907 QualType KmpDependInfoArrayTy = C.getConstantArrayType(
John McCall7f416cc2015-09-08 08:05:57 +00003908 KmpDependInfoTy, llvm::APInt(/*numBits=*/64, NumDependencies),
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003909 ArrayType::Normal, /*IndexTypeQuals=*/0);
3910 // kmp_depend_info[<Dependences.size()>] deps;
Alexey Bataev48591dd2016-04-20 04:01:36 +00003911 DependenciesArray =
3912 CGF.CreateMemTemp(KmpDependInfoArrayTy, ".dep.arr.addr");
John McCall7f416cc2015-09-08 08:05:57 +00003913 for (unsigned i = 0; i < NumDependencies; ++i) {
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003914 const Expr *E = Data.Dependences[i].second;
John McCall7f416cc2015-09-08 08:05:57 +00003915 auto Addr = CGF.EmitLValue(E);
Alexey Bataevd6fdc8b2015-08-31 07:32:19 +00003916 llvm::Value *Size;
3917 QualType Ty = E->getType();
Alexey Bataevd6fdc8b2015-08-31 07:32:19 +00003918 if (auto *ASE = dyn_cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts())) {
3919 LValue UpAddrLVal =
3920 CGF.EmitOMPArraySectionExpr(ASE, /*LowerBound=*/false);
3921 llvm::Value *UpAddr =
John McCall7f416cc2015-09-08 08:05:57 +00003922 CGF.Builder.CreateConstGEP1_32(UpAddrLVal.getPointer(), /*Idx0=*/1);
Alexey Bataevd6fdc8b2015-08-31 07:32:19 +00003923 llvm::Value *LowIntPtr =
John McCall7f416cc2015-09-08 08:05:57 +00003924 CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGM.SizeTy);
Alexey Bataevd6fdc8b2015-08-31 07:32:19 +00003925 llvm::Value *UpIntPtr = CGF.Builder.CreatePtrToInt(UpAddr, CGM.SizeTy);
3926 Size = CGF.Builder.CreateNUWSub(UpIntPtr, LowIntPtr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00003927 } else
Alexey Bataev1189bd02016-01-26 12:20:39 +00003928 Size = CGF.getTypeSize(Ty);
John McCall7f416cc2015-09-08 08:05:57 +00003929 auto Base = CGF.MakeAddrLValue(
3930 CGF.Builder.CreateConstArrayGEP(DependenciesArray, i, DependencySize),
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003931 KmpDependInfoTy);
3932 // deps[i].base_addr = &<Dependences[i].second>;
3933 auto BaseAddrLVal = CGF.EmitLValueForField(
3934 Base, *std::next(KmpDependInfoRD->field_begin(), BaseAddr));
John McCall7f416cc2015-09-08 08:05:57 +00003935 CGF.EmitStoreOfScalar(
3936 CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGF.IntPtrTy),
3937 BaseAddrLVal);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003938 // deps[i].len = sizeof(<Dependences[i].second>);
3939 auto LenLVal = CGF.EmitLValueForField(
3940 Base, *std::next(KmpDependInfoRD->field_begin(), Len));
3941 CGF.EmitStoreOfScalar(Size, LenLVal);
3942 // deps[i].flags = <Dependences[i].first>;
3943 RTLDependenceKindTy DepKind;
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003944 switch (Data.Dependences[i].first) {
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003945 case OMPC_DEPEND_in:
3946 DepKind = DepIn;
3947 break;
Alexey Bataev92e82f92015-11-23 13:33:42 +00003948 // Out and InOut dependencies must use the same code.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003949 case OMPC_DEPEND_out:
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003950 case OMPC_DEPEND_inout:
3951 DepKind = DepInOut;
3952 break;
Alexey Bataeveb482352015-12-18 05:05:56 +00003953 case OMPC_DEPEND_source:
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003954 case OMPC_DEPEND_sink:
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003955 case OMPC_DEPEND_unknown:
3956 llvm_unreachable("Unknown task dependence type");
3957 }
3958 auto FlagsLVal = CGF.EmitLValueForField(
3959 Base, *std::next(KmpDependInfoRD->field_begin(), Flags));
3960 CGF.EmitStoreOfScalar(llvm::ConstantInt::get(LLVMFlagsTy, DepKind),
3961 FlagsLVal);
3962 }
John McCall7f416cc2015-09-08 08:05:57 +00003963 DependenciesArray = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
3964 CGF.Builder.CreateStructGEP(DependenciesArray, 0, CharUnits::Zero()),
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003965 CGF.VoidPtrTy);
3966 }
3967
Alexey Bataev62b63b12015-03-10 07:28:44 +00003968 // NOTE: routine and part_id fields are intialized by __kmpc_omp_task_alloc()
3969 // libcall.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003970 // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid,
3971 // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list,
3972 // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list) if dependence
3973 // list is not empty
Alexey Bataev1d677132015-04-22 13:57:31 +00003974 auto *ThreadID = getThreadID(CGF, Loc);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00003975 auto *UpLoc = emitUpdateLocation(CGF, Loc);
John McCall7f416cc2015-09-08 08:05:57 +00003976 llvm::Value *TaskArgs[] = { UpLoc, ThreadID, NewTask };
3977 llvm::Value *DepTaskArgs[7];
3978 if (NumDependencies) {
3979 DepTaskArgs[0] = UpLoc;
3980 DepTaskArgs[1] = ThreadID;
3981 DepTaskArgs[2] = NewTask;
3982 DepTaskArgs[3] = CGF.Builder.getInt32(NumDependencies);
3983 DepTaskArgs[4] = DependenciesArray.getPointer();
3984 DepTaskArgs[5] = CGF.Builder.getInt32(0);
3985 DepTaskArgs[6] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
3986 }
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003987 auto &&ThenCodeGen = [this, Loc, &Data, TDBase, KmpTaskTQTyRD,
Alexey Bataev48591dd2016-04-20 04:01:36 +00003988 NumDependencies, &TaskArgs,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003989 &DepTaskArgs](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003990 if (!Data.Tied) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00003991 auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId);
3992 auto PartIdLVal = CGF.EmitLValueForField(TDBase, *PartIdFI);
3993 CGF.EmitStoreOfScalar(CGF.Builder.getInt32(0), PartIdLVal);
3994 }
John McCall7f416cc2015-09-08 08:05:57 +00003995 if (NumDependencies) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003996 CGF.EmitRuntimeCall(
Alexey Bataev48591dd2016-04-20 04:01:36 +00003997 createRuntimeFunction(OMPRTL__kmpc_omp_task_with_deps), DepTaskArgs);
John McCall7f416cc2015-09-08 08:05:57 +00003998 } else {
Alexey Bataev48591dd2016-04-20 04:01:36 +00003999 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task),
John McCall7f416cc2015-09-08 08:05:57 +00004000 TaskArgs);
4001 }
Alexey Bataev48591dd2016-04-20 04:01:36 +00004002 // Check if parent region is untied and build return for untied task;
4003 if (auto *Region =
4004 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
4005 Region->emitUntiedSwitch(CGF);
Alexey Bataev1d677132015-04-22 13:57:31 +00004006 };
John McCall7f416cc2015-09-08 08:05:57 +00004007
4008 llvm::Value *DepWaitTaskArgs[6];
4009 if (NumDependencies) {
4010 DepWaitTaskArgs[0] = UpLoc;
4011 DepWaitTaskArgs[1] = ThreadID;
4012 DepWaitTaskArgs[2] = CGF.Builder.getInt32(NumDependencies);
4013 DepWaitTaskArgs[3] = DependenciesArray.getPointer();
4014 DepWaitTaskArgs[4] = CGF.Builder.getInt32(0);
4015 DepWaitTaskArgs[5] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
4016 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004017 auto &&ElseCodeGen = [&TaskArgs, ThreadID, NewTaskNewTaskTTy, TaskEntry,
4018 NumDependencies, &DepWaitTaskArgs](CodeGenFunction &CGF,
4019 PrePostActionTy &) {
4020 auto &RT = CGF.CGM.getOpenMPRuntime();
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004021 CodeGenFunction::RunCleanupsScope LocalScope(CGF);
4022 // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid,
4023 // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32
4024 // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); if dependence info
4025 // is specified.
John McCall7f416cc2015-09-08 08:05:57 +00004026 if (NumDependencies)
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004027 CGF.EmitRuntimeCall(RT.createRuntimeFunction(OMPRTL__kmpc_omp_wait_deps),
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004028 DepWaitTaskArgs);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004029 // Call proxy_task_entry(gtid, new_task);
4030 auto &&CodeGen = [TaskEntry, ThreadID, NewTaskNewTaskTTy](
4031 CodeGenFunction &CGF, PrePostActionTy &Action) {
4032 Action.Enter(CGF);
4033 llvm::Value *OutlinedFnArgs[] = {ThreadID, NewTaskNewTaskTTy};
4034 CGF.EmitCallOrInvoke(TaskEntry, OutlinedFnArgs);
4035 };
4036
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004037 // Build void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid,
4038 // kmp_task_t *new_task);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004039 // Build void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid,
4040 // kmp_task_t *new_task);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004041 RegionCodeGenTy RCG(CodeGen);
4042 CommonActionTy Action(
4043 RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_begin_if0), TaskArgs,
4044 RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_complete_if0), TaskArgs);
4045 RCG.setAction(Action);
4046 RCG(CGF);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00004047 };
John McCall7f416cc2015-09-08 08:05:57 +00004048
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004049 if (IfCond)
Alexey Bataev1d677132015-04-22 13:57:31 +00004050 emitOMPIfClause(CGF, IfCond, ThenCodeGen, ElseCodeGen);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004051 else {
4052 RegionCodeGenTy ThenRCG(ThenCodeGen);
4053 ThenRCG(CGF);
Alexey Bataevf539faa2016-03-28 12:58:34 +00004054 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00004055}
4056
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004057void CGOpenMPRuntime::emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
4058 const OMPLoopDirective &D,
4059 llvm::Value *TaskFunction,
4060 QualType SharedsTy, Address Shareds,
4061 const Expr *IfCond,
4062 const OMPTaskDataTy &Data) {
Alexey Bataev7292c292016-04-25 12:22:29 +00004063 if (!CGF.HaveInsertPoint())
4064 return;
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004065 TaskResultTy Result =
4066 emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00004067 // NOTE: routine and part_id fields are intialized by __kmpc_omp_task_alloc()
4068 // libcall.
4069 // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int
4070 // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int
4071 // sched, kmp_uint64 grainsize, void *task_dup);
4072 llvm::Value *ThreadID = getThreadID(CGF, Loc);
4073 llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc);
4074 llvm::Value *IfVal;
4075 if (IfCond) {
4076 IfVal = CGF.Builder.CreateIntCast(CGF.EvaluateExprAsBool(IfCond), CGF.IntTy,
4077 /*isSigned=*/true);
4078 } else
4079 IfVal = llvm::ConstantInt::getSigned(CGF.IntTy, /*V=*/1);
4080
4081 LValue LBLVal = CGF.EmitLValueForField(
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004082 Result.TDBase,
4083 *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound));
Alexey Bataev7292c292016-04-25 12:22:29 +00004084 auto *LBVar =
4085 cast<VarDecl>(cast<DeclRefExpr>(D.getLowerBoundVariable())->getDecl());
4086 CGF.EmitAnyExprToMem(LBVar->getInit(), LBLVal.getAddress(), LBLVal.getQuals(),
4087 /*IsInitializer=*/true);
4088 LValue UBLVal = CGF.EmitLValueForField(
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004089 Result.TDBase,
4090 *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound));
Alexey Bataev7292c292016-04-25 12:22:29 +00004091 auto *UBVar =
4092 cast<VarDecl>(cast<DeclRefExpr>(D.getUpperBoundVariable())->getDecl());
4093 CGF.EmitAnyExprToMem(UBVar->getInit(), UBLVal.getAddress(), UBLVal.getQuals(),
4094 /*IsInitializer=*/true);
4095 LValue StLVal = CGF.EmitLValueForField(
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004096 Result.TDBase,
4097 *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTStride));
Alexey Bataev7292c292016-04-25 12:22:29 +00004098 auto *StVar =
4099 cast<VarDecl>(cast<DeclRefExpr>(D.getStrideVariable())->getDecl());
4100 CGF.EmitAnyExprToMem(StVar->getInit(), StLVal.getAddress(), StLVal.getQuals(),
4101 /*IsInitializer=*/true);
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00004102 enum { NoSchedule = 0, Grainsize = 1, NumTasks = 2 };
Alexey Bataev7292c292016-04-25 12:22:29 +00004103 llvm::Value *TaskArgs[] = {
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004104 UpLoc, ThreadID, Result.NewTask, IfVal, LBLVal.getPointer(),
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00004105 UBLVal.getPointer(), CGF.EmitLoadOfScalar(StLVal, SourceLocation()),
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004106 llvm::ConstantInt::getSigned(CGF.IntTy, Data.Nogroup ? 1 : 0),
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00004107 llvm::ConstantInt::getSigned(
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004108 CGF.IntTy, Data.Schedule.getPointer()
4109 ? Data.Schedule.getInt() ? NumTasks : Grainsize
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00004110 : NoSchedule),
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004111 Data.Schedule.getPointer()
4112 ? CGF.Builder.CreateIntCast(Data.Schedule.getPointer(), CGF.Int64Ty,
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00004113 /*isSigned=*/false)
4114 : llvm::ConstantInt::get(CGF.Int64Ty, /*V=*/0),
Alexey Bataevf93095a2016-05-05 08:46:22 +00004115 Result.TaskDupFn
4116 ? CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Result.TaskDupFn,
4117 CGF.VoidPtrTy)
4118 : llvm::ConstantPointerNull::get(CGF.VoidPtrTy)};
Alexey Bataev7292c292016-04-25 12:22:29 +00004119 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_taskloop), TaskArgs);
4120}
4121
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004122/// \brief Emit reduction operation for each element of array (required for
4123/// array sections) LHS op = RHS.
4124/// \param Type Type of array.
4125/// \param LHSVar Variable on the left side of the reduction operation
4126/// (references element of array in original variable).
4127/// \param RHSVar Variable on the right side of the reduction operation
4128/// (references element of array in original variable).
4129/// \param RedOpGen Generator of reduction operation with use of LHSVar and
4130/// RHSVar.
Benjamin Kramere003ca22015-10-28 13:54:16 +00004131static void EmitOMPAggregateReduction(
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004132 CodeGenFunction &CGF, QualType Type, const VarDecl *LHSVar,
4133 const VarDecl *RHSVar,
4134 const llvm::function_ref<void(CodeGenFunction &CGF, const Expr *,
4135 const Expr *, const Expr *)> &RedOpGen,
4136 const Expr *XExpr = nullptr, const Expr *EExpr = nullptr,
4137 const Expr *UpExpr = nullptr) {
4138 // Perform element-by-element initialization.
4139 QualType ElementTy;
4140 Address LHSAddr = CGF.GetAddrOfLocalVar(LHSVar);
4141 Address RHSAddr = CGF.GetAddrOfLocalVar(RHSVar);
4142
4143 // Drill down to the base element type on both arrays.
4144 auto ArrayTy = Type->getAsArrayTypeUnsafe();
4145 auto NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, LHSAddr);
4146
4147 auto RHSBegin = RHSAddr.getPointer();
4148 auto LHSBegin = LHSAddr.getPointer();
4149 // Cast from pointer to array type to pointer to single element.
4150 auto LHSEnd = CGF.Builder.CreateGEP(LHSBegin, NumElements);
4151 // The basic structure here is a while-do loop.
4152 auto BodyBB = CGF.createBasicBlock("omp.arraycpy.body");
4153 auto DoneBB = CGF.createBasicBlock("omp.arraycpy.done");
4154 auto IsEmpty =
4155 CGF.Builder.CreateICmpEQ(LHSBegin, LHSEnd, "omp.arraycpy.isempty");
4156 CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
4157
4158 // Enter the loop body, making that address the current address.
4159 auto EntryBB = CGF.Builder.GetInsertBlock();
4160 CGF.EmitBlock(BodyBB);
4161
4162 CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy);
4163
4164 llvm::PHINode *RHSElementPHI = CGF.Builder.CreatePHI(
4165 RHSBegin->getType(), 2, "omp.arraycpy.srcElementPast");
4166 RHSElementPHI->addIncoming(RHSBegin, EntryBB);
4167 Address RHSElementCurrent =
4168 Address(RHSElementPHI,
4169 RHSAddr.getAlignment().alignmentOfArrayElement(ElementSize));
4170
4171 llvm::PHINode *LHSElementPHI = CGF.Builder.CreatePHI(
4172 LHSBegin->getType(), 2, "omp.arraycpy.destElementPast");
4173 LHSElementPHI->addIncoming(LHSBegin, EntryBB);
4174 Address LHSElementCurrent =
4175 Address(LHSElementPHI,
4176 LHSAddr.getAlignment().alignmentOfArrayElement(ElementSize));
4177
4178 // Emit copy.
4179 CodeGenFunction::OMPPrivateScope Scope(CGF);
4180 Scope.addPrivate(LHSVar, [=]() -> Address { return LHSElementCurrent; });
4181 Scope.addPrivate(RHSVar, [=]() -> Address { return RHSElementCurrent; });
4182 Scope.Privatize();
4183 RedOpGen(CGF, XExpr, EExpr, UpExpr);
4184 Scope.ForceCleanup();
4185
4186 // Shift the address forward by one element.
4187 auto LHSElementNext = CGF.Builder.CreateConstGEP1_32(
4188 LHSElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
4189 auto RHSElementNext = CGF.Builder.CreateConstGEP1_32(
4190 RHSElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element");
4191 // Check whether we've reached the end.
4192 auto Done =
4193 CGF.Builder.CreateICmpEQ(LHSElementNext, LHSEnd, "omp.arraycpy.done");
4194 CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB);
4195 LHSElementPHI->addIncoming(LHSElementNext, CGF.Builder.GetInsertBlock());
4196 RHSElementPHI->addIncoming(RHSElementNext, CGF.Builder.GetInsertBlock());
4197
4198 // Done.
4199 CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
4200}
4201
Alexey Bataeva839ddd2016-03-17 10:19:46 +00004202/// Emit reduction combiner. If the combiner is a simple expression emit it as
4203/// is, otherwise consider it as combiner of UDR decl and emit it as a call of
4204/// UDR combiner function.
4205static void emitReductionCombiner(CodeGenFunction &CGF,
4206 const Expr *ReductionOp) {
4207 if (auto *CE = dyn_cast<CallExpr>(ReductionOp))
4208 if (auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee()))
4209 if (auto *DRE =
4210 dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts()))
4211 if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) {
4212 std::pair<llvm::Function *, llvm::Function *> Reduction =
4213 CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD);
4214 RValue Func = RValue::get(Reduction.first);
4215 CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func);
4216 CGF.EmitIgnoredExpr(ReductionOp);
4217 return;
4218 }
4219 CGF.EmitIgnoredExpr(ReductionOp);
4220}
4221
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004222static llvm::Value *emitReductionFunction(CodeGenModule &CGM,
4223 llvm::Type *ArgsType,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004224 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004225 ArrayRef<const Expr *> LHSExprs,
4226 ArrayRef<const Expr *> RHSExprs,
4227 ArrayRef<const Expr *> ReductionOps) {
4228 auto &C = CGM.getContext();
4229
4230 // void reduction_func(void *LHSArg, void *RHSArg);
4231 FunctionArgList Args;
4232 ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, SourceLocation(), /*Id=*/nullptr,
4233 C.VoidPtrTy);
4234 ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, SourceLocation(), /*Id=*/nullptr,
4235 C.VoidPtrTy);
4236 Args.push_back(&LHSArg);
4237 Args.push_back(&RHSArg);
John McCallc56a8b32016-03-11 04:30:31 +00004238 auto &CGFI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004239 auto *Fn = llvm::Function::Create(
4240 CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
4241 ".omp.reduction.reduction_func", &CGM.getModule());
Akira Hatanaka44a59f82015-10-28 02:30:47 +00004242 CGM.SetInternalFunctionAttributes(/*D=*/nullptr, Fn, CGFI);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004243 CodeGenFunction CGF(CGM);
4244 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args);
4245
4246 // Dst = (void*[n])(LHSArg);
4247 // Src = (void*[n])(RHSArg);
John McCall7f416cc2015-09-08 08:05:57 +00004248 Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4249 CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)),
4250 ArgsType), CGF.getPointerAlign());
4251 Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4252 CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)),
4253 ArgsType), CGF.getPointerAlign());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004254
4255 // ...
4256 // *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
4257 // ...
4258 CodeGenFunction::OMPPrivateScope Scope(CGF);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004259 auto IPriv = Privates.begin();
4260 unsigned Idx = 0;
4261 for (unsigned I = 0, E = ReductionOps.size(); I < E; ++I, ++IPriv, ++Idx) {
John McCall7f416cc2015-09-08 08:05:57 +00004262 auto RHSVar = cast<VarDecl>(cast<DeclRefExpr>(RHSExprs[I])->getDecl());
4263 Scope.addPrivate(RHSVar, [&]() -> Address {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004264 return emitAddrOfVarFromArray(CGF, RHS, Idx, RHSVar);
John McCall7f416cc2015-09-08 08:05:57 +00004265 });
4266 auto LHSVar = cast<VarDecl>(cast<DeclRefExpr>(LHSExprs[I])->getDecl());
4267 Scope.addPrivate(LHSVar, [&]() -> Address {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004268 return emitAddrOfVarFromArray(CGF, LHS, Idx, LHSVar);
John McCall7f416cc2015-09-08 08:05:57 +00004269 });
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004270 QualType PrivTy = (*IPriv)->getType();
Alexey Bataev1189bd02016-01-26 12:20:39 +00004271 if (PrivTy->isVariablyModifiedType()) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004272 // Get array size and emit VLA type.
4273 ++Idx;
4274 Address Elem =
4275 CGF.Builder.CreateConstArrayGEP(LHS, Idx, CGF.getPointerSize());
4276 llvm::Value *Ptr = CGF.Builder.CreateLoad(Elem);
Alexey Bataev1189bd02016-01-26 12:20:39 +00004277 auto *VLA = CGF.getContext().getAsVariableArrayType(PrivTy);
4278 auto *OVE = cast<OpaqueValueExpr>(VLA->getSizeExpr());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004279 CodeGenFunction::OpaqueValueMapping OpaqueMap(
Alexey Bataev1189bd02016-01-26 12:20:39 +00004280 CGF, OVE, RValue::get(CGF.Builder.CreatePtrToInt(Ptr, CGF.SizeTy)));
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004281 CGF.EmitVariablyModifiedType(PrivTy);
4282 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004283 }
4284 Scope.Privatize();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004285 IPriv = Privates.begin();
4286 auto ILHS = LHSExprs.begin();
4287 auto IRHS = RHSExprs.begin();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004288 for (auto *E : ReductionOps) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004289 if ((*IPriv)->getType()->isArrayType()) {
4290 // Emit reduction for array section.
4291 auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
4292 auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
Alexey Bataeva839ddd2016-03-17 10:19:46 +00004293 EmitOMPAggregateReduction(
4294 CGF, (*IPriv)->getType(), LHSVar, RHSVar,
4295 [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) {
4296 emitReductionCombiner(CGF, E);
4297 });
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004298 } else
4299 // Emit reduction for array subscript or single variable.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00004300 emitReductionCombiner(CGF, E);
Richard Trieucc3949d2016-02-18 22:34:54 +00004301 ++IPriv;
4302 ++ILHS;
4303 ++IRHS;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004304 }
4305 Scope.ForceCleanup();
4306 CGF.FinishFunction();
4307 return Fn;
4308}
4309
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004310static void emitSingleReductionCombiner(CodeGenFunction &CGF,
4311 const Expr *ReductionOp,
4312 const Expr *PrivateRef,
4313 const DeclRefExpr *LHS,
4314 const DeclRefExpr *RHS) {
4315 if (PrivateRef->getType()->isArrayType()) {
4316 // Emit reduction for array section.
4317 auto *LHSVar = cast<VarDecl>(LHS->getDecl());
4318 auto *RHSVar = cast<VarDecl>(RHS->getDecl());
4319 EmitOMPAggregateReduction(
4320 CGF, PrivateRef->getType(), LHSVar, RHSVar,
4321 [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) {
4322 emitReductionCombiner(CGF, ReductionOp);
4323 });
4324 } else
4325 // Emit reduction for array subscript or single variable.
4326 emitReductionCombiner(CGF, ReductionOp);
4327}
4328
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004329void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004330 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004331 ArrayRef<const Expr *> LHSExprs,
4332 ArrayRef<const Expr *> RHSExprs,
4333 ArrayRef<const Expr *> ReductionOps,
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00004334 bool WithNowait, bool SimpleReduction) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00004335 if (!CGF.HaveInsertPoint())
4336 return;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004337 // Next code should be emitted for reduction:
4338 //
4339 // static kmp_critical_name lock = { 0 };
4340 //
4341 // void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
4342 // *(Type0*)lhs[0] = ReductionOperation0(*(Type0*)lhs[0], *(Type0*)rhs[0]);
4343 // ...
4344 // *(Type<n>-1*)lhs[<n>-1] = ReductionOperation<n>-1(*(Type<n>-1*)lhs[<n>-1],
4345 // *(Type<n>-1*)rhs[<n>-1]);
4346 // }
4347 //
4348 // ...
4349 // void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
4350 // switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
4351 // RedList, reduce_func, &<lock>)) {
4352 // case 1:
4353 // ...
4354 // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
4355 // ...
4356 // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
4357 // break;
4358 // case 2:
4359 // ...
4360 // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
4361 // ...
Alexey Bataev69a47792015-05-07 03:54:03 +00004362 // [__kmpc_end_reduce(<loc>, <gtid>, &<lock>);]
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004363 // break;
4364 // default:;
4365 // }
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00004366 //
4367 // if SimpleReduction is true, only the next code is generated:
4368 // ...
4369 // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
4370 // ...
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004371
4372 auto &C = CGM.getContext();
4373
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00004374 if (SimpleReduction) {
4375 CodeGenFunction::RunCleanupsScope Scope(CGF);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004376 auto IPriv = Privates.begin();
4377 auto ILHS = LHSExprs.begin();
4378 auto IRHS = RHSExprs.begin();
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00004379 for (auto *E : ReductionOps) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004380 emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS),
4381 cast<DeclRefExpr>(*IRHS));
Richard Trieucc3949d2016-02-18 22:34:54 +00004382 ++IPriv;
4383 ++ILHS;
4384 ++IRHS;
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00004385 }
4386 return;
4387 }
4388
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004389 // 1. Build a list of reduction variables.
4390 // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]};
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004391 auto Size = RHSExprs.size();
4392 for (auto *E : Privates) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00004393 if (E->getType()->isVariablyModifiedType())
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004394 // Reserve place for array size.
4395 ++Size;
4396 }
4397 llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004398 QualType ReductionArrayTy =
4399 C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal,
4400 /*IndexTypeQuals=*/0);
John McCall7f416cc2015-09-08 08:05:57 +00004401 Address ReductionList =
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004402 CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list");
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004403 auto IPriv = Privates.begin();
4404 unsigned Idx = 0;
4405 for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) {
John McCall7f416cc2015-09-08 08:05:57 +00004406 Address Elem =
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004407 CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, CGF.getPointerSize());
John McCall7f416cc2015-09-08 08:05:57 +00004408 CGF.Builder.CreateStore(
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004409 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
John McCall7f416cc2015-09-08 08:05:57 +00004410 CGF.EmitLValue(RHSExprs[I]).getPointer(), CGF.VoidPtrTy),
4411 Elem);
Alexey Bataev1189bd02016-01-26 12:20:39 +00004412 if ((*IPriv)->getType()->isVariablyModifiedType()) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004413 // Store array size.
4414 ++Idx;
4415 Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx,
4416 CGF.getPointerSize());
Alexey Bataev1189bd02016-01-26 12:20:39 +00004417 llvm::Value *Size = CGF.Builder.CreateIntCast(
4418 CGF.getVLASize(
4419 CGF.getContext().getAsVariableArrayType((*IPriv)->getType()))
4420 .first,
4421 CGF.SizeTy, /*isSigned=*/false);
4422 CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy),
4423 Elem);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004424 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004425 }
4426
4427 // 2. Emit reduce_func().
4428 auto *ReductionFn = emitReductionFunction(
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004429 CGM, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(), Privates,
4430 LHSExprs, RHSExprs, ReductionOps);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004431
4432 // 3. Create static kmp_critical_name lock = { 0 };
4433 auto *Lock = getCriticalRegionLock(".reduction");
4434
4435 // 4. Build res = __kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
4436 // RedList, reduce_func, &<lock>);
Alexey Bataev50b3c952016-02-19 10:38:26 +00004437 auto *IdentTLoc = emitUpdateLocation(CGF, Loc, OMP_ATOMIC_REDUCE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004438 auto *ThreadId = getThreadID(CGF, Loc);
Alexey Bataev1189bd02016-01-26 12:20:39 +00004439 auto *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy);
John McCall7f416cc2015-09-08 08:05:57 +00004440 auto *RL =
4441 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(ReductionList.getPointer(),
4442 CGF.VoidPtrTy);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004443 llvm::Value *Args[] = {
4444 IdentTLoc, // ident_t *<loc>
4445 ThreadId, // i32 <gtid>
4446 CGF.Builder.getInt32(RHSExprs.size()), // i32 <n>
4447 ReductionArrayTySize, // size_type sizeof(RedList)
4448 RL, // void *RedList
4449 ReductionFn, // void (*) (void *, void *) <reduce_func>
4450 Lock // kmp_critical_name *&<lock>
4451 };
4452 auto Res = CGF.EmitRuntimeCall(
4453 createRuntimeFunction(WithNowait ? OMPRTL__kmpc_reduce_nowait
4454 : OMPRTL__kmpc_reduce),
4455 Args);
4456
4457 // 5. Build switch(res)
4458 auto *DefaultBB = CGF.createBasicBlock(".omp.reduction.default");
4459 auto *SwInst = CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/2);
4460
4461 // 6. Build case 1:
4462 // ...
4463 // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
4464 // ...
4465 // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
4466 // break;
4467 auto *Case1BB = CGF.createBasicBlock(".omp.reduction.case1");
4468 SwInst->addCase(CGF.Builder.getInt32(1), Case1BB);
4469 CGF.EmitBlock(Case1BB);
4470
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004471 // Add emission of __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
4472 llvm::Value *EndArgs[] = {
4473 IdentTLoc, // ident_t *<loc>
4474 ThreadId, // i32 <gtid>
4475 Lock // kmp_critical_name *&<lock>
4476 };
4477 auto &&CodeGen = [&Privates, &LHSExprs, &RHSExprs, &ReductionOps](
4478 CodeGenFunction &CGF, PrePostActionTy &Action) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004479 auto IPriv = Privates.begin();
4480 auto ILHS = LHSExprs.begin();
4481 auto IRHS = RHSExprs.begin();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004482 for (auto *E : ReductionOps) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004483 emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS),
4484 cast<DeclRefExpr>(*IRHS));
Richard Trieucc3949d2016-02-18 22:34:54 +00004485 ++IPriv;
4486 ++ILHS;
4487 ++IRHS;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004488 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004489 };
4490 RegionCodeGenTy RCG(CodeGen);
4491 CommonActionTy Action(
4492 nullptr, llvm::None,
4493 createRuntimeFunction(WithNowait ? OMPRTL__kmpc_end_reduce_nowait
4494 : OMPRTL__kmpc_end_reduce),
4495 EndArgs);
4496 RCG.setAction(Action);
4497 RCG(CGF);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004498
4499 CGF.EmitBranch(DefaultBB);
4500
4501 // 7. Build case 2:
4502 // ...
4503 // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
4504 // ...
4505 // break;
4506 auto *Case2BB = CGF.createBasicBlock(".omp.reduction.case2");
4507 SwInst->addCase(CGF.Builder.getInt32(2), Case2BB);
4508 CGF.EmitBlock(Case2BB);
4509
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004510 auto &&AtomicCodeGen = [Loc, &Privates, &LHSExprs, &RHSExprs, &ReductionOps](
4511 CodeGenFunction &CGF, PrePostActionTy &Action) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004512 auto ILHS = LHSExprs.begin();
4513 auto IRHS = RHSExprs.begin();
4514 auto IPriv = Privates.begin();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004515 for (auto *E : ReductionOps) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004516 const Expr *XExpr = nullptr;
4517 const Expr *EExpr = nullptr;
4518 const Expr *UpExpr = nullptr;
4519 BinaryOperatorKind BO = BO_Comma;
4520 if (auto *BO = dyn_cast<BinaryOperator>(E)) {
4521 if (BO->getOpcode() == BO_Assign) {
4522 XExpr = BO->getLHS();
4523 UpExpr = BO->getRHS();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004524 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004525 }
4526 // Try to emit update expression as a simple atomic.
4527 auto *RHSExpr = UpExpr;
4528 if (RHSExpr) {
4529 // Analyze RHS part of the whole expression.
4530 if (auto *ACO = dyn_cast<AbstractConditionalOperator>(
4531 RHSExpr->IgnoreParenImpCasts())) {
4532 // If this is a conditional operator, analyze its condition for
4533 // min/max reduction operator.
4534 RHSExpr = ACO->getCond();
Alexey Bataev69a47792015-05-07 03:54:03 +00004535 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004536 if (auto *BORHS =
4537 dyn_cast<BinaryOperator>(RHSExpr->IgnoreParenImpCasts())) {
4538 EExpr = BORHS->getRHS();
4539 BO = BORHS->getOpcode();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004540 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004541 }
4542 if (XExpr) {
4543 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
4544 auto &&AtomicRedGen = [BO, VD, IPriv,
4545 Loc](CodeGenFunction &CGF, const Expr *XExpr,
4546 const Expr *EExpr, const Expr *UpExpr) {
4547 LValue X = CGF.EmitLValue(XExpr);
4548 RValue E;
4549 if (EExpr)
4550 E = CGF.EmitAnyExpr(EExpr);
4551 CGF.EmitOMPAtomicSimpleUpdateExpr(
JF Bastien92f4ef12016-04-06 17:26:42 +00004552 X, E, BO, /*IsXLHSInRHSPart=*/true,
4553 llvm::AtomicOrdering::Monotonic, Loc,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004554 [&CGF, UpExpr, VD, IPriv, Loc](RValue XRValue) {
4555 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
4556 PrivateScope.addPrivate(
4557 VD, [&CGF, VD, XRValue, Loc]() -> Address {
4558 Address LHSTemp = CGF.CreateMemTemp(VD->getType());
4559 CGF.emitOMPSimpleStore(
4560 CGF.MakeAddrLValue(LHSTemp, VD->getType()), XRValue,
4561 VD->getType().getNonReferenceType(), Loc);
4562 return LHSTemp;
4563 });
4564 (void)PrivateScope.Privatize();
4565 return CGF.EmitAnyExpr(UpExpr);
4566 });
4567 };
4568 if ((*IPriv)->getType()->isArrayType()) {
4569 // Emit atomic reduction for array section.
4570 auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
4571 EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), VD, RHSVar,
4572 AtomicRedGen, XExpr, EExpr, UpExpr);
4573 } else
4574 // Emit atomic reduction for array subscript or single variable.
4575 AtomicRedGen(CGF, XExpr, EExpr, UpExpr);
4576 } else {
4577 // Emit as a critical region.
4578 auto &&CritRedGen = [E, Loc](CodeGenFunction &CGF, const Expr *,
4579 const Expr *, const Expr *) {
4580 auto &RT = CGF.CGM.getOpenMPRuntime();
4581 RT.emitCriticalRegion(
4582 CGF, ".atomic_reduction",
4583 [=](CodeGenFunction &CGF, PrePostActionTy &Action) {
4584 Action.Enter(CGF);
4585 emitReductionCombiner(CGF, E);
4586 },
4587 Loc);
4588 };
4589 if ((*IPriv)->getType()->isArrayType()) {
4590 auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
4591 auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
4592 EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), LHSVar, RHSVar,
4593 CritRedGen);
4594 } else
4595 CritRedGen(CGF, nullptr, nullptr, nullptr);
4596 }
Richard Trieucc3949d2016-02-18 22:34:54 +00004597 ++ILHS;
4598 ++IRHS;
4599 ++IPriv;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004600 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004601 };
4602 RegionCodeGenTy AtomicRCG(AtomicCodeGen);
4603 if (!WithNowait) {
4604 // Add emission of __kmpc_end_reduce(<loc>, <gtid>, &<lock>);
4605 llvm::Value *EndArgs[] = {
4606 IdentTLoc, // ident_t *<loc>
4607 ThreadId, // i32 <gtid>
4608 Lock // kmp_critical_name *&<lock>
4609 };
4610 CommonActionTy Action(nullptr, llvm::None,
4611 createRuntimeFunction(OMPRTL__kmpc_end_reduce),
4612 EndArgs);
4613 AtomicRCG.setAction(Action);
4614 AtomicRCG(CGF);
4615 } else
4616 AtomicRCG(CGF);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00004617
4618 CGF.EmitBranch(DefaultBB);
4619 CGF.EmitBlock(DefaultBB, /*IsFinished=*/true);
4620}
4621
Alexey Bataev8b8e2022015-04-27 05:22:09 +00004622void CGOpenMPRuntime::emitTaskwaitCall(CodeGenFunction &CGF,
4623 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00004624 if (!CGF.HaveInsertPoint())
4625 return;
Alexey Bataev8b8e2022015-04-27 05:22:09 +00004626 // Build call kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32
4627 // global_tid);
4628 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
4629 // Ignore return result until untied tasks are supported.
4630 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskwait), Args);
Alexey Bataev48591dd2016-04-20 04:01:36 +00004631 if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
4632 Region->emitUntiedSwitch(CGF);
Alexey Bataev8b8e2022015-04-27 05:22:09 +00004633}
4634
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00004635void CGOpenMPRuntime::emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00004636 OpenMPDirectiveKind InnerKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004637 const RegionCodeGenTy &CodeGen,
4638 bool HasCancel) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00004639 if (!CGF.HaveInsertPoint())
4640 return;
Alexey Bataev25e5b442015-09-15 12:52:43 +00004641 InlinedOpenMPRegionRAII Region(CGF, CodeGen, InnerKind, HasCancel);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00004642 CGF.CapturedStmtInfo->EmitBody(CGF, /*S=*/nullptr);
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00004643}
4644
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00004645namespace {
4646enum RTCancelKind {
4647 CancelNoreq = 0,
4648 CancelParallel = 1,
4649 CancelLoop = 2,
4650 CancelSections = 3,
4651 CancelTaskgroup = 4
4652};
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00004653} // anonymous namespace
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00004654
4655static RTCancelKind getCancellationKind(OpenMPDirectiveKind CancelRegion) {
4656 RTCancelKind CancelKind = CancelNoreq;
Alexey Bataev0f34da12015-07-02 04:17:07 +00004657 if (CancelRegion == OMPD_parallel)
4658 CancelKind = CancelParallel;
4659 else if (CancelRegion == OMPD_for)
4660 CancelKind = CancelLoop;
4661 else if (CancelRegion == OMPD_sections)
4662 CancelKind = CancelSections;
4663 else {
4664 assert(CancelRegion == OMPD_taskgroup);
4665 CancelKind = CancelTaskgroup;
4666 }
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00004667 return CancelKind;
4668}
4669
4670void CGOpenMPRuntime::emitCancellationPointCall(
4671 CodeGenFunction &CGF, SourceLocation Loc,
4672 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00004673 if (!CGF.HaveInsertPoint())
4674 return;
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00004675 // Build call kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32
4676 // global_tid, kmp_int32 cncl_kind);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00004677 if (auto *OMPRegionInfo =
4678 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
Alexey Bataev25e5b442015-09-15 12:52:43 +00004679 if (OMPRegionInfo->hasCancel()) {
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00004680 llvm::Value *Args[] = {
4681 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
4682 CGF.Builder.getInt32(getCancellationKind(CancelRegion))};
Alexey Bataev81c7ea02015-07-03 09:56:58 +00004683 // Ignore return result until untied tasks are supported.
4684 auto *Result = CGF.EmitRuntimeCall(
4685 createRuntimeFunction(OMPRTL__kmpc_cancellationpoint), Args);
4686 // if (__kmpc_cancellationpoint()) {
4687 // __kmpc_cancel_barrier();
4688 // exit from construct;
4689 // }
4690 auto *ExitBB = CGF.createBasicBlock(".cancel.exit");
4691 auto *ContBB = CGF.createBasicBlock(".cancel.continue");
4692 auto *Cmp = CGF.Builder.CreateIsNotNull(Result);
4693 CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB);
4694 CGF.EmitBlock(ExitBB);
4695 // __kmpc_cancel_barrier();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004696 emitBarrierCall(CGF, Loc, OMPD_unknown, /*EmitChecks=*/false);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00004697 // exit from construct;
Alexey Bataev25e5b442015-09-15 12:52:43 +00004698 auto CancelDest =
4699 CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind());
Alexey Bataev81c7ea02015-07-03 09:56:58 +00004700 CGF.EmitBranchThroughCleanup(CancelDest);
4701 CGF.EmitBlock(ContBB, /*IsFinished=*/true);
4702 }
Alexey Bataev0f34da12015-07-02 04:17:07 +00004703 }
Alexey Bataev0f34da12015-07-02 04:17:07 +00004704}
4705
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00004706void CGOpenMPRuntime::emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +00004707 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00004708 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00004709 if (!CGF.HaveInsertPoint())
4710 return;
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00004711 // Build call kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid,
4712 // kmp_int32 cncl_kind);
4713 if (auto *OMPRegionInfo =
4714 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004715 auto &&ThenGen = [Loc, CancelRegion, OMPRegionInfo](CodeGenFunction &CGF,
4716 PrePostActionTy &) {
4717 auto &RT = CGF.CGM.getOpenMPRuntime();
Alexey Bataev87933c72015-09-18 08:07:34 +00004718 llvm::Value *Args[] = {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004719 RT.emitUpdateLocation(CGF, Loc), RT.getThreadID(CGF, Loc),
Alexey Bataev87933c72015-09-18 08:07:34 +00004720 CGF.Builder.getInt32(getCancellationKind(CancelRegion))};
4721 // Ignore return result until untied tasks are supported.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004722 auto *Result = CGF.EmitRuntimeCall(
4723 RT.createRuntimeFunction(OMPRTL__kmpc_cancel), Args);
Alexey Bataev87933c72015-09-18 08:07:34 +00004724 // if (__kmpc_cancel()) {
4725 // __kmpc_cancel_barrier();
4726 // exit from construct;
4727 // }
4728 auto *ExitBB = CGF.createBasicBlock(".cancel.exit");
4729 auto *ContBB = CGF.createBasicBlock(".cancel.continue");
4730 auto *Cmp = CGF.Builder.CreateIsNotNull(Result);
4731 CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB);
4732 CGF.EmitBlock(ExitBB);
4733 // __kmpc_cancel_barrier();
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004734 RT.emitBarrierCall(CGF, Loc, OMPD_unknown, /*EmitChecks=*/false);
Alexey Bataev87933c72015-09-18 08:07:34 +00004735 // exit from construct;
4736 auto CancelDest =
4737 CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind());
4738 CGF.EmitBranchThroughCleanup(CancelDest);
4739 CGF.EmitBlock(ContBB, /*IsFinished=*/true);
4740 };
4741 if (IfCond)
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004742 emitOMPIfClause(CGF, IfCond, ThenGen,
4743 [](CodeGenFunction &, PrePostActionTy &) {});
4744 else {
4745 RegionCodeGenTy ThenRCG(ThenGen);
4746 ThenRCG(CGF);
4747 }
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00004748 }
4749}
Samuel Antaobed3c462015-10-02 16:14:20 +00004750
Samuel Antaoee8fb302016-01-06 13:42:12 +00004751/// \brief Obtain information that uniquely identifies a target entry. This
Samuel Antao2de62b02016-02-13 23:35:10 +00004752/// consists of the file and device IDs as well as line number associated with
4753/// the relevant entry source location.
Samuel Antaoee8fb302016-01-06 13:42:12 +00004754static void getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc,
4755 unsigned &DeviceID, unsigned &FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +00004756 unsigned &LineNum) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00004757
4758 auto &SM = C.getSourceManager();
4759
4760 // The loc should be always valid and have a file ID (the user cannot use
4761 // #pragma directives in macros)
4762
4763 assert(Loc.isValid() && "Source location is expected to be always valid.");
4764 assert(Loc.isFileID() && "Source location is expected to refer to a file.");
4765
4766 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
4767 assert(PLoc.isValid() && "Source location is expected to be always valid.");
4768
4769 llvm::sys::fs::UniqueID ID;
4770 if (llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID))
4771 llvm_unreachable("Source file with target region no longer exists!");
4772
4773 DeviceID = ID.getDevice();
4774 FileID = ID.getFile();
4775 LineNum = PLoc.getLine();
Samuel Antaoee8fb302016-01-06 13:42:12 +00004776}
4777
4778void CGOpenMPRuntime::emitTargetOutlinedFunction(
4779 const OMPExecutableDirective &D, StringRef ParentName,
4780 llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00004781 bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00004782 assert(!ParentName.empty() && "Invalid target region parent name!");
4783
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00004784 emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID,
4785 IsOffloadEntry, CodeGen);
4786}
4787
4788void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper(
4789 const OMPExecutableDirective &D, StringRef ParentName,
4790 llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
4791 bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
Samuel Antao2de62b02016-02-13 23:35:10 +00004792 // Create a unique name for the entry function using the source location
4793 // information of the current target region. The name will be something like:
Samuel Antaoee8fb302016-01-06 13:42:12 +00004794 //
Samuel Antao2de62b02016-02-13 23:35:10 +00004795 // __omp_offloading_DD_FFFF_PP_lBB
Samuel Antaoee8fb302016-01-06 13:42:12 +00004796 //
4797 // where DD_FFFF is an ID unique to the file (device and file IDs), PP is the
Samuel Antao2de62b02016-02-13 23:35:10 +00004798 // mangled name of the function that encloses the target region and BB is the
4799 // line number of the target region.
Samuel Antaoee8fb302016-01-06 13:42:12 +00004800
4801 unsigned DeviceID;
4802 unsigned FileID;
4803 unsigned Line;
Samuel Antaoee8fb302016-01-06 13:42:12 +00004804 getTargetEntryUniqueInfo(CGM.getContext(), D.getLocStart(), DeviceID, FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +00004805 Line);
Samuel Antaoee8fb302016-01-06 13:42:12 +00004806 SmallString<64> EntryFnName;
4807 {
4808 llvm::raw_svector_ostream OS(EntryFnName);
Samuel Antao2de62b02016-02-13 23:35:10 +00004809 OS << "__omp_offloading" << llvm::format("_%x", DeviceID)
4810 << llvm::format("_%x_", FileID) << ParentName << "_l" << Line;
Samuel Antaoee8fb302016-01-06 13:42:12 +00004811 }
4812
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00004813 const CapturedStmt &CS = *cast<CapturedStmt>(D.getAssociatedStmt());
4814
Samuel Antaobed3c462015-10-02 16:14:20 +00004815 CodeGenFunction CGF(CGM, true);
Samuel Antaoee8fb302016-01-06 13:42:12 +00004816 CGOpenMPTargetRegionInfo CGInfo(CS, CodeGen, EntryFnName);
Samuel Antaobed3c462015-10-02 16:14:20 +00004817 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
Samuel Antaoee8fb302016-01-06 13:42:12 +00004818
Samuel Antao6d004262016-06-16 18:39:34 +00004819 OutlinedFn = CGF.GenerateOpenMPCapturedStmtFunction(CS);
Samuel Antaoee8fb302016-01-06 13:42:12 +00004820
4821 // If this target outline function is not an offload entry, we don't need to
4822 // register it.
4823 if (!IsOffloadEntry)
4824 return;
4825
4826 // The target region ID is used by the runtime library to identify the current
4827 // target region, so it only has to be unique and not necessarily point to
4828 // anything. It could be the pointer to the outlined function that implements
4829 // the target region, but we aren't using that so that the compiler doesn't
4830 // need to keep that, and could therefore inline the host function if proven
4831 // worthwhile during optimization. In the other hand, if emitting code for the
4832 // device, the ID has to be the function address so that it can retrieved from
4833 // the offloading entry and launched by the runtime library. We also mark the
4834 // outlined function to have external linkage in case we are emitting code for
4835 // the device, because these functions will be entry points to the device.
4836
4837 if (CGM.getLangOpts().OpenMPIsDevice) {
4838 OutlinedFnID = llvm::ConstantExpr::getBitCast(OutlinedFn, CGM.Int8PtrTy);
4839 OutlinedFn->setLinkage(llvm::GlobalValue::ExternalLinkage);
4840 } else
4841 OutlinedFnID = new llvm::GlobalVariable(
4842 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
4843 llvm::GlobalValue::PrivateLinkage,
4844 llvm::Constant::getNullValue(CGM.Int8Ty), ".omp_offload.region_id");
4845
4846 // Register the information for the entry associated with this target region.
4847 OffloadEntriesInfoManager.registerTargetRegionEntryInfo(
Samuel Antao2de62b02016-02-13 23:35:10 +00004848 DeviceID, FileID, ParentName, Line, OutlinedFn, OutlinedFnID);
Samuel Antaobed3c462015-10-02 16:14:20 +00004849}
4850
Carlo Bertolli6eee9062016-04-29 01:37:30 +00004851/// discard all CompoundStmts intervening between two constructs
4852static const Stmt *ignoreCompoundStmts(const Stmt *Body) {
4853 while (auto *CS = dyn_cast_or_null<CompoundStmt>(Body))
4854 Body = CS->body_front();
4855
4856 return Body;
4857}
4858
Samuel Antaob68e2db2016-03-03 16:20:23 +00004859/// \brief Emit the num_teams clause of an enclosed teams directive at the
4860/// target region scope. If there is no teams directive associated with the
4861/// target directive, or if there is no num_teams clause associated with the
4862/// enclosed teams directive, return nullptr.
4863static llvm::Value *
4864emitNumTeamsClauseForTargetDirective(CGOpenMPRuntime &OMPRuntime,
4865 CodeGenFunction &CGF,
4866 const OMPExecutableDirective &D) {
4867
4868 assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the "
4869 "teams directive expected to be "
4870 "emitted only for the host!");
4871
4872 // FIXME: For the moment we do not support combined directives with target and
4873 // teams, so we do not expect to get any num_teams clause in the provided
4874 // directive. Once we support that, this assertion can be replaced by the
4875 // actual emission of the clause expression.
4876 assert(D.getSingleClause<OMPNumTeamsClause>() == nullptr &&
4877 "Not expecting clause in directive.");
4878
4879 // If the current target region has a teams region enclosed, we need to get
4880 // the number of teams to pass to the runtime function call. This is done
4881 // by generating the expression in a inlined region. This is required because
4882 // the expression is captured in the enclosing target environment when the
4883 // teams directive is not combined with target.
4884
4885 const CapturedStmt &CS = *cast<CapturedStmt>(D.getAssociatedStmt());
4886
4887 // FIXME: Accommodate other combined directives with teams when they become
4888 // available.
Carlo Bertolli6eee9062016-04-29 01:37:30 +00004889 if (auto *TeamsDir = dyn_cast_or_null<OMPTeamsDirective>(
4890 ignoreCompoundStmts(CS.getCapturedStmt()))) {
Samuel Antaob68e2db2016-03-03 16:20:23 +00004891 if (auto *NTE = TeamsDir->getSingleClause<OMPNumTeamsClause>()) {
4892 CGOpenMPInnerExprInfo CGInfo(CGF, CS);
4893 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
4894 llvm::Value *NumTeams = CGF.EmitScalarExpr(NTE->getNumTeams());
4895 return CGF.Builder.CreateIntCast(NumTeams, CGF.Int32Ty,
4896 /*IsSigned=*/true);
4897 }
4898
4899 // If we have an enclosed teams directive but no num_teams clause we use
4900 // the default value 0.
4901 return CGF.Builder.getInt32(0);
4902 }
4903
4904 // No teams associated with the directive.
4905 return nullptr;
4906}
4907
4908/// \brief Emit the thread_limit clause of an enclosed teams directive at the
4909/// target region scope. If there is no teams directive associated with the
4910/// target directive, or if there is no thread_limit clause associated with the
4911/// enclosed teams directive, return nullptr.
4912static llvm::Value *
4913emitThreadLimitClauseForTargetDirective(CGOpenMPRuntime &OMPRuntime,
4914 CodeGenFunction &CGF,
4915 const OMPExecutableDirective &D) {
4916
4917 assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the "
4918 "teams directive expected to be "
4919 "emitted only for the host!");
4920
4921 // FIXME: For the moment we do not support combined directives with target and
4922 // teams, so we do not expect to get any thread_limit clause in the provided
4923 // directive. Once we support that, this assertion can be replaced by the
4924 // actual emission of the clause expression.
4925 assert(D.getSingleClause<OMPThreadLimitClause>() == nullptr &&
4926 "Not expecting clause in directive.");
4927
4928 // If the current target region has a teams region enclosed, we need to get
4929 // the thread limit to pass to the runtime function call. This is done
4930 // by generating the expression in a inlined region. This is required because
4931 // the expression is captured in the enclosing target environment when the
4932 // teams directive is not combined with target.
4933
4934 const CapturedStmt &CS = *cast<CapturedStmt>(D.getAssociatedStmt());
4935
4936 // FIXME: Accommodate other combined directives with teams when they become
4937 // available.
Carlo Bertolli6eee9062016-04-29 01:37:30 +00004938 if (auto *TeamsDir = dyn_cast_or_null<OMPTeamsDirective>(
4939 ignoreCompoundStmts(CS.getCapturedStmt()))) {
Samuel Antaob68e2db2016-03-03 16:20:23 +00004940 if (auto *TLE = TeamsDir->getSingleClause<OMPThreadLimitClause>()) {
4941 CGOpenMPInnerExprInfo CGInfo(CGF, CS);
4942 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
4943 llvm::Value *ThreadLimit = CGF.EmitScalarExpr(TLE->getThreadLimit());
4944 return CGF.Builder.CreateIntCast(ThreadLimit, CGF.Int32Ty,
4945 /*IsSigned=*/true);
4946 }
4947
4948 // If we have an enclosed teams directive but no thread_limit clause we use
4949 // the default value 0.
4950 return CGF.Builder.getInt32(0);
4951 }
4952
4953 // No teams associated with the directive.
4954 return nullptr;
4955}
4956
Samuel Antao86ace552016-04-27 22:40:57 +00004957namespace {
4958// \brief Utility to handle information from clauses associated with a given
4959// construct that use mappable expressions (e.g. 'map' clause, 'to' clause).
4960// It provides a convenient interface to obtain the information and generate
4961// code for that information.
4962class MappableExprsHandler {
4963public:
4964 /// \brief Values for bit flags used to specify the mapping type for
4965 /// offloading.
4966 enum OpenMPOffloadMappingFlags {
Samuel Antao86ace552016-04-27 22:40:57 +00004967 /// \brief Allocate memory on the device and move data from host to device.
4968 OMP_MAP_TO = 0x01,
4969 /// \brief Allocate memory on the device and move data from device to host.
4970 OMP_MAP_FROM = 0x02,
4971 /// \brief Always perform the requested mapping action on the element, even
4972 /// if it was already mapped before.
4973 OMP_MAP_ALWAYS = 0x04,
Samuel Antao86ace552016-04-27 22:40:57 +00004974 /// \brief Delete the element from the device environment, ignoring the
4975 /// current reference count associated with the element.
Samuel Antao6782e942016-05-26 16:48:10 +00004976 OMP_MAP_DELETE = 0x08,
4977 /// \brief The element being mapped is a pointer, therefore the pointee
4978 /// should be mapped as well.
4979 OMP_MAP_IS_PTR = 0x10,
4980 /// \brief This flags signals that an argument is the first one relating to
4981 /// a map/private clause expression. For some cases a single
4982 /// map/privatization results in multiple arguments passed to the runtime
4983 /// library.
4984 OMP_MAP_FIRST_REF = 0x20,
Samuel Antaocc10b852016-07-28 14:23:26 +00004985 /// \brief Signal that the runtime library has to return the device pointer
4986 /// in the current position for the data being mapped.
4987 OMP_MAP_RETURN_PTR = 0x40,
Samuel Antaod486f842016-05-26 16:53:38 +00004988 /// \brief This flag signals that the reference being passed is a pointer to
4989 /// private data.
4990 OMP_MAP_PRIVATE_PTR = 0x80,
Samuel Antao86ace552016-04-27 22:40:57 +00004991 /// \brief Pass the element to the device by value.
Samuel Antao6782e942016-05-26 16:48:10 +00004992 OMP_MAP_PRIVATE_VAL = 0x100,
Samuel Antao86ace552016-04-27 22:40:57 +00004993 };
4994
Samuel Antaocc10b852016-07-28 14:23:26 +00004995 /// Class that associates information with a base pointer to be passed to the
4996 /// runtime library.
4997 class BasePointerInfo {
4998 /// The base pointer.
4999 llvm::Value *Ptr = nullptr;
5000 /// The base declaration that refers to this device pointer, or null if
5001 /// there is none.
5002 const ValueDecl *DevPtrDecl = nullptr;
5003
5004 public:
5005 BasePointerInfo(llvm::Value *Ptr, const ValueDecl *DevPtrDecl = nullptr)
5006 : Ptr(Ptr), DevPtrDecl(DevPtrDecl) {}
5007 llvm::Value *operator*() const { return Ptr; }
5008 const ValueDecl *getDevicePtrDecl() const { return DevPtrDecl; }
5009 void setDevicePtrDecl(const ValueDecl *D) { DevPtrDecl = D; }
5010 };
5011
5012 typedef SmallVector<BasePointerInfo, 16> MapBaseValuesArrayTy;
Samuel Antao86ace552016-04-27 22:40:57 +00005013 typedef SmallVector<llvm::Value *, 16> MapValuesArrayTy;
5014 typedef SmallVector<unsigned, 16> MapFlagsArrayTy;
5015
5016private:
5017 /// \brief Directive from where the map clauses were extracted.
Samuel Antao44bcdb32016-07-28 15:31:29 +00005018 const OMPExecutableDirective &CurDir;
Samuel Antao86ace552016-04-27 22:40:57 +00005019
5020 /// \brief Function the directive is being generated for.
5021 CodeGenFunction &CGF;
5022
Samuel Antaod486f842016-05-26 16:53:38 +00005023 /// \brief Set of all first private variables in the current directive.
5024 llvm::SmallPtrSet<const VarDecl *, 8> FirstPrivateDecls;
5025
Samuel Antao6890b092016-07-28 14:25:09 +00005026 /// Map between device pointer declarations and their expression components.
5027 /// The key value for declarations in 'this' is null.
5028 llvm::DenseMap<
5029 const ValueDecl *,
5030 SmallVector<OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>>
5031 DevPointersMap;
5032
Samuel Antao86ace552016-04-27 22:40:57 +00005033 llvm::Value *getExprTypeSize(const Expr *E) const {
5034 auto ExprTy = E->getType().getCanonicalType();
5035
5036 // Reference types are ignored for mapping purposes.
5037 if (auto *RefTy = ExprTy->getAs<ReferenceType>())
5038 ExprTy = RefTy->getPointeeType().getCanonicalType();
5039
5040 // Given that an array section is considered a built-in type, we need to
5041 // do the calculation based on the length of the section instead of relying
5042 // on CGF.getTypeSize(E->getType()).
5043 if (const auto *OAE = dyn_cast<OMPArraySectionExpr>(E)) {
5044 QualType BaseTy = OMPArraySectionExpr::getBaseOriginalType(
5045 OAE->getBase()->IgnoreParenImpCasts())
5046 .getCanonicalType();
5047
5048 // If there is no length associated with the expression, that means we
5049 // are using the whole length of the base.
5050 if (!OAE->getLength() && OAE->getColonLoc().isValid())
5051 return CGF.getTypeSize(BaseTy);
5052
5053 llvm::Value *ElemSize;
5054 if (auto *PTy = BaseTy->getAs<PointerType>())
5055 ElemSize = CGF.getTypeSize(PTy->getPointeeType().getCanonicalType());
5056 else {
5057 auto *ATy = cast<ArrayType>(BaseTy.getTypePtr());
5058 assert(ATy && "Expecting array type if not a pointer type.");
5059 ElemSize = CGF.getTypeSize(ATy->getElementType().getCanonicalType());
5060 }
5061
5062 // If we don't have a length at this point, that is because we have an
5063 // array section with a single element.
5064 if (!OAE->getLength())
5065 return ElemSize;
5066
5067 auto *LengthVal = CGF.EmitScalarExpr(OAE->getLength());
5068 LengthVal =
5069 CGF.Builder.CreateIntCast(LengthVal, CGF.SizeTy, /*isSigned=*/false);
5070 return CGF.Builder.CreateNUWMul(LengthVal, ElemSize);
5071 }
5072 return CGF.getTypeSize(ExprTy);
5073 }
5074
5075 /// \brief Return the corresponding bits for a given map clause modifier. Add
5076 /// a flag marking the map as a pointer if requested. Add a flag marking the
Samuel Antao6782e942016-05-26 16:48:10 +00005077 /// map as the first one of a series of maps that relate to the same map
5078 /// expression.
Samuel Antao86ace552016-04-27 22:40:57 +00005079 unsigned getMapTypeBits(OpenMPMapClauseKind MapType,
5080 OpenMPMapClauseKind MapTypeModifier, bool AddPtrFlag,
Samuel Antao6782e942016-05-26 16:48:10 +00005081 bool AddIsFirstFlag) const {
Samuel Antao86ace552016-04-27 22:40:57 +00005082 unsigned Bits = 0u;
5083 switch (MapType) {
5084 case OMPC_MAP_alloc:
Samuel Antao6782e942016-05-26 16:48:10 +00005085 case OMPC_MAP_release:
5086 // alloc and release is the default behavior in the runtime library, i.e.
5087 // if we don't pass any bits alloc/release that is what the runtime is
5088 // going to do. Therefore, we don't need to signal anything for these two
5089 // type modifiers.
Samuel Antao86ace552016-04-27 22:40:57 +00005090 break;
5091 case OMPC_MAP_to:
5092 Bits = OMP_MAP_TO;
5093 break;
5094 case OMPC_MAP_from:
5095 Bits = OMP_MAP_FROM;
5096 break;
5097 case OMPC_MAP_tofrom:
5098 Bits = OMP_MAP_TO | OMP_MAP_FROM;
5099 break;
5100 case OMPC_MAP_delete:
5101 Bits = OMP_MAP_DELETE;
5102 break;
Samuel Antao86ace552016-04-27 22:40:57 +00005103 default:
5104 llvm_unreachable("Unexpected map type!");
5105 break;
5106 }
5107 if (AddPtrFlag)
Samuel Antao6782e942016-05-26 16:48:10 +00005108 Bits |= OMP_MAP_IS_PTR;
5109 if (AddIsFirstFlag)
5110 Bits |= OMP_MAP_FIRST_REF;
Samuel Antao86ace552016-04-27 22:40:57 +00005111 if (MapTypeModifier == OMPC_MAP_always)
5112 Bits |= OMP_MAP_ALWAYS;
5113 return Bits;
5114 }
5115
5116 /// \brief Return true if the provided expression is a final array section. A
5117 /// final array section, is one whose length can't be proved to be one.
5118 bool isFinalArraySectionExpression(const Expr *E) const {
5119 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
5120
5121 // It is not an array section and therefore not a unity-size one.
5122 if (!OASE)
5123 return false;
5124
5125 // An array section with no colon always refer to a single element.
5126 if (OASE->getColonLoc().isInvalid())
5127 return false;
5128
5129 auto *Length = OASE->getLength();
5130
5131 // If we don't have a length we have to check if the array has size 1
5132 // for this dimension. Also, we should always expect a length if the
5133 // base type is pointer.
5134 if (!Length) {
5135 auto BaseQTy = OMPArraySectionExpr::getBaseOriginalType(
5136 OASE->getBase()->IgnoreParenImpCasts())
5137 .getCanonicalType();
5138 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
5139 return ATy->getSize().getSExtValue() != 1;
5140 // If we don't have a constant dimension length, we have to consider
5141 // the current section as having any size, so it is not necessarily
5142 // unitary. If it happen to be unity size, that's user fault.
5143 return true;
5144 }
5145
5146 // Check if the length evaluates to 1.
5147 llvm::APSInt ConstLength;
5148 if (!Length->EvaluateAsInt(ConstLength, CGF.getContext()))
5149 return true; // Can have more that size 1.
5150
5151 return ConstLength.getSExtValue() != 1;
5152 }
5153
5154 /// \brief Generate the base pointers, section pointers, sizes and map type
5155 /// bits for the provided map type, map modifier, and expression components.
5156 /// \a IsFirstComponent should be set to true if the provided set of
5157 /// components is the first associated with a capture.
5158 void generateInfoForComponentList(
5159 OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapTypeModifier,
5160 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
Samuel Antaocc10b852016-07-28 14:23:26 +00005161 MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers,
Samuel Antao86ace552016-04-27 22:40:57 +00005162 MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types,
5163 bool IsFirstComponentList) const {
5164
5165 // The following summarizes what has to be generated for each map and the
5166 // types bellow. The generated information is expressed in this order:
5167 // base pointer, section pointer, size, flags
5168 // (to add to the ones that come from the map type and modifier).
5169 //
5170 // double d;
5171 // int i[100];
5172 // float *p;
5173 //
5174 // struct S1 {
5175 // int i;
5176 // float f[50];
5177 // }
5178 // struct S2 {
5179 // int i;
5180 // float f[50];
5181 // S1 s;
5182 // double *p;
5183 // struct S2 *ps;
5184 // }
5185 // S2 s;
5186 // S2 *ps;
5187 //
5188 // map(d)
5189 // &d, &d, sizeof(double), noflags
5190 //
5191 // map(i)
5192 // &i, &i, 100*sizeof(int), noflags
5193 //
5194 // map(i[1:23])
5195 // &i(=&i[0]), &i[1], 23*sizeof(int), noflags
5196 //
5197 // map(p)
5198 // &p, &p, sizeof(float*), noflags
5199 //
5200 // map(p[1:24])
5201 // p, &p[1], 24*sizeof(float), noflags
5202 //
5203 // map(s)
5204 // &s, &s, sizeof(S2), noflags
5205 //
5206 // map(s.i)
5207 // &s, &(s.i), sizeof(int), noflags
5208 //
5209 // map(s.s.f)
5210 // &s, &(s.i.f), 50*sizeof(int), noflags
5211 //
5212 // map(s.p)
5213 // &s, &(s.p), sizeof(double*), noflags
5214 //
5215 // map(s.p[:22], s.a s.b)
5216 // &s, &(s.p), sizeof(double*), noflags
5217 // &(s.p), &(s.p[0]), 22*sizeof(double), ptr_flag + extra_flag
5218 //
5219 // map(s.ps)
5220 // &s, &(s.ps), sizeof(S2*), noflags
5221 //
5222 // map(s.ps->s.i)
5223 // &s, &(s.ps), sizeof(S2*), noflags
5224 // &(s.ps), &(s.ps->s.i), sizeof(int), ptr_flag + extra_flag
5225 //
5226 // map(s.ps->ps)
5227 // &s, &(s.ps), sizeof(S2*), noflags
5228 // &(s.ps), &(s.ps->ps), sizeof(S2*), ptr_flag + extra_flag
5229 //
5230 // map(s.ps->ps->ps)
5231 // &s, &(s.ps), sizeof(S2*), noflags
5232 // &(s.ps), &(s.ps->ps), sizeof(S2*), ptr_flag + extra_flag
5233 // &(s.ps->ps), &(s.ps->ps->ps), sizeof(S2*), ptr_flag + extra_flag
5234 //
5235 // map(s.ps->ps->s.f[:22])
5236 // &s, &(s.ps), sizeof(S2*), noflags
5237 // &(s.ps), &(s.ps->ps), sizeof(S2*), ptr_flag + extra_flag
5238 // &(s.ps->ps), &(s.ps->ps->s.f[0]), 22*sizeof(float), ptr_flag + extra_flag
5239 //
5240 // map(ps)
5241 // &ps, &ps, sizeof(S2*), noflags
5242 //
5243 // map(ps->i)
5244 // ps, &(ps->i), sizeof(int), noflags
5245 //
5246 // map(ps->s.f)
5247 // ps, &(ps->s.f[0]), 50*sizeof(float), noflags
5248 //
5249 // map(ps->p)
5250 // ps, &(ps->p), sizeof(double*), noflags
5251 //
5252 // map(ps->p[:22])
5253 // ps, &(ps->p), sizeof(double*), noflags
5254 // &(ps->p), &(ps->p[0]), 22*sizeof(double), ptr_flag + extra_flag
5255 //
5256 // map(ps->ps)
5257 // ps, &(ps->ps), sizeof(S2*), noflags
5258 //
5259 // map(ps->ps->s.i)
5260 // ps, &(ps->ps), sizeof(S2*), noflags
5261 // &(ps->ps), &(ps->ps->s.i), sizeof(int), ptr_flag + extra_flag
5262 //
5263 // map(ps->ps->ps)
5264 // ps, &(ps->ps), sizeof(S2*), noflags
5265 // &(ps->ps), &(ps->ps->ps), sizeof(S2*), ptr_flag + extra_flag
5266 //
5267 // map(ps->ps->ps->ps)
5268 // ps, &(ps->ps), sizeof(S2*), noflags
5269 // &(ps->ps), &(ps->ps->ps), sizeof(S2*), ptr_flag + extra_flag
5270 // &(ps->ps->ps), &(ps->ps->ps->ps), sizeof(S2*), ptr_flag + extra_flag
5271 //
5272 // map(ps->ps->ps->s.f[:22])
5273 // ps, &(ps->ps), sizeof(S2*), noflags
5274 // &(ps->ps), &(ps->ps->ps), sizeof(S2*), ptr_flag + extra_flag
5275 // &(ps->ps->ps), &(ps->ps->ps->s.f[0]), 22*sizeof(float), ptr_flag +
5276 // extra_flag
5277
5278 // Track if the map information being generated is the first for a capture.
5279 bool IsCaptureFirstInfo = IsFirstComponentList;
5280
5281 // Scan the components from the base to the complete expression.
5282 auto CI = Components.rbegin();
5283 auto CE = Components.rend();
5284 auto I = CI;
5285
5286 // Track if the map information being generated is the first for a list of
5287 // components.
5288 bool IsExpressionFirstInfo = true;
5289 llvm::Value *BP = nullptr;
5290
5291 if (auto *ME = dyn_cast<MemberExpr>(I->getAssociatedExpression())) {
5292 // The base is the 'this' pointer. The content of the pointer is going
5293 // to be the base of the field being mapped.
5294 BP = CGF.EmitScalarExpr(ME->getBase());
5295 } else {
5296 // The base is the reference to the variable.
5297 // BP = &Var.
5298 BP = CGF.EmitLValue(cast<DeclRefExpr>(I->getAssociatedExpression()))
5299 .getPointer();
5300
5301 // If the variable is a pointer and is being dereferenced (i.e. is not
Nico Webera6916892016-06-10 18:53:04 +00005302 // the last component), the base has to be the pointer itself, not its
Samuel Antao403ffd42016-07-27 22:49:49 +00005303 // reference. References are ignored for mapping purposes.
5304 QualType Ty =
5305 I->getAssociatedDeclaration()->getType().getNonReferenceType();
5306 if (Ty->isAnyPointerType() && std::next(I) != CE) {
5307 auto PtrAddr = CGF.MakeNaturalAlignAddrLValue(BP, Ty);
Samuel Antao86ace552016-04-27 22:40:57 +00005308 BP = CGF.EmitLoadOfPointerLValue(PtrAddr.getAddress(),
Samuel Antao403ffd42016-07-27 22:49:49 +00005309 Ty->castAs<PointerType>())
Samuel Antao86ace552016-04-27 22:40:57 +00005310 .getPointer();
5311
5312 // We do not need to generate individual map information for the
5313 // pointer, it can be associated with the combined storage.
5314 ++I;
5315 }
5316 }
5317
5318 for (; I != CE; ++I) {
5319 auto Next = std::next(I);
5320
5321 // We need to generate the addresses and sizes if this is the last
5322 // component, if the component is a pointer or if it is an array section
5323 // whose length can't be proved to be one. If this is a pointer, it
5324 // becomes the base address for the following components.
5325
5326 // A final array section, is one whose length can't be proved to be one.
5327 bool IsFinalArraySection =
5328 isFinalArraySectionExpression(I->getAssociatedExpression());
5329
5330 // Get information on whether the element is a pointer. Have to do a
5331 // special treatment for array sections given that they are built-in
5332 // types.
5333 const auto *OASE =
5334 dyn_cast<OMPArraySectionExpr>(I->getAssociatedExpression());
5335 bool IsPointer =
5336 (OASE &&
5337 OMPArraySectionExpr::getBaseOriginalType(OASE)
5338 .getCanonicalType()
5339 ->isAnyPointerType()) ||
5340 I->getAssociatedExpression()->getType()->isAnyPointerType();
5341
5342 if (Next == CE || IsPointer || IsFinalArraySection) {
5343
5344 // If this is not the last component, we expect the pointer to be
5345 // associated with an array expression or member expression.
5346 assert((Next == CE ||
5347 isa<MemberExpr>(Next->getAssociatedExpression()) ||
5348 isa<ArraySubscriptExpr>(Next->getAssociatedExpression()) ||
5349 isa<OMPArraySectionExpr>(Next->getAssociatedExpression())) &&
5350 "Unexpected expression");
5351
Samuel Antao86ace552016-04-27 22:40:57 +00005352 auto *LB = CGF.EmitLValue(I->getAssociatedExpression()).getPointer();
5353 auto *Size = getExprTypeSize(I->getAssociatedExpression());
5354
Samuel Antao03a3cec2016-07-27 22:52:16 +00005355 // If we have a member expression and the current component is a
5356 // reference, we have to map the reference too. Whenever we have a
5357 // reference, the section that reference refers to is going to be a
5358 // load instruction from the storage assigned to the reference.
5359 if (isa<MemberExpr>(I->getAssociatedExpression()) &&
5360 I->getAssociatedDeclaration()->getType()->isReferenceType()) {
5361 auto *LI = cast<llvm::LoadInst>(LB);
5362 auto *RefAddr = LI->getPointerOperand();
5363
5364 BasePointers.push_back(BP);
5365 Pointers.push_back(RefAddr);
5366 Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy));
5367 Types.push_back(getMapTypeBits(
5368 /*MapType*/ OMPC_MAP_alloc, /*MapTypeModifier=*/OMPC_MAP_unknown,
5369 !IsExpressionFirstInfo, IsCaptureFirstInfo));
5370 IsExpressionFirstInfo = false;
5371 IsCaptureFirstInfo = false;
5372 // The reference will be the next base address.
5373 BP = RefAddr;
5374 }
5375
5376 BasePointers.push_back(BP);
Samuel Antao86ace552016-04-27 22:40:57 +00005377 Pointers.push_back(LB);
5378 Sizes.push_back(Size);
Samuel Antao03a3cec2016-07-27 22:52:16 +00005379
Samuel Antao6782e942016-05-26 16:48:10 +00005380 // We need to add a pointer flag for each map that comes from the
5381 // same expression except for the first one. We also need to signal
5382 // this map is the first one that relates with the current capture
5383 // (there is a set of entries for each capture).
Samuel Antao86ace552016-04-27 22:40:57 +00005384 Types.push_back(getMapTypeBits(MapType, MapTypeModifier,
5385 !IsExpressionFirstInfo,
Samuel Antao6782e942016-05-26 16:48:10 +00005386 IsCaptureFirstInfo));
Samuel Antao86ace552016-04-27 22:40:57 +00005387
5388 // If we have a final array section, we are done with this expression.
5389 if (IsFinalArraySection)
5390 break;
5391
5392 // The pointer becomes the base for the next element.
5393 if (Next != CE)
5394 BP = LB;
5395
5396 IsExpressionFirstInfo = false;
5397 IsCaptureFirstInfo = false;
5398 continue;
5399 }
5400 }
5401 }
5402
Samuel Antaod486f842016-05-26 16:53:38 +00005403 /// \brief Return the adjusted map modifiers if the declaration a capture
5404 /// refers to appears in a first-private clause. This is expected to be used
5405 /// only with directives that start with 'target'.
5406 unsigned adjustMapModifiersForPrivateClauses(const CapturedStmt::Capture &Cap,
5407 unsigned CurrentModifiers) {
5408 assert(Cap.capturesVariable() && "Expected capture by reference only!");
5409
5410 // A first private variable captured by reference will use only the
5411 // 'private ptr' and 'map to' flag. Return the right flags if the captured
5412 // declaration is known as first-private in this handler.
5413 if (FirstPrivateDecls.count(Cap.getCapturedVar()))
5414 return MappableExprsHandler::OMP_MAP_PRIVATE_PTR |
5415 MappableExprsHandler::OMP_MAP_TO;
5416
5417 // We didn't modify anything.
5418 return CurrentModifiers;
5419 }
5420
Samuel Antao86ace552016-04-27 22:40:57 +00005421public:
5422 MappableExprsHandler(const OMPExecutableDirective &Dir, CodeGenFunction &CGF)
Samuel Antao44bcdb32016-07-28 15:31:29 +00005423 : CurDir(Dir), CGF(CGF) {
Samuel Antaod486f842016-05-26 16:53:38 +00005424 // Extract firstprivate clause information.
5425 for (const auto *C : Dir.getClausesOfKind<OMPFirstprivateClause>())
5426 for (const auto *D : C->varlists())
5427 FirstPrivateDecls.insert(
5428 cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl());
Samuel Antao6890b092016-07-28 14:25:09 +00005429 // Extract device pointer clause information.
5430 for (const auto *C : Dir.getClausesOfKind<OMPIsDevicePtrClause>())
5431 for (auto L : C->component_lists())
5432 DevPointersMap[L.first].push_back(L.second);
Samuel Antaod486f842016-05-26 16:53:38 +00005433 }
Samuel Antao86ace552016-04-27 22:40:57 +00005434
5435 /// \brief Generate all the base pointers, section pointers, sizes and map
Samuel Antaocc10b852016-07-28 14:23:26 +00005436 /// types for the extracted mappable expressions. Also, for each item that
5437 /// relates with a device pointer, a pair of the relevant declaration and
5438 /// index where it occurs is appended to the device pointers info array.
5439 void generateAllInfo(MapBaseValuesArrayTy &BasePointers,
Samuel Antao86ace552016-04-27 22:40:57 +00005440 MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes,
5441 MapFlagsArrayTy &Types) const {
5442 BasePointers.clear();
5443 Pointers.clear();
5444 Sizes.clear();
5445 Types.clear();
5446
5447 struct MapInfo {
Samuel Antaocc10b852016-07-28 14:23:26 +00005448 /// Kind that defines how a device pointer has to be returned.
5449 enum ReturnPointerKind {
5450 // Don't have to return any pointer.
5451 RPK_None,
5452 // Pointer is the base of the declaration.
5453 RPK_Base,
5454 // Pointer is a member of the base declaration - 'this'
5455 RPK_Member,
5456 // Pointer is a reference and a member of the base declaration - 'this'
5457 RPK_MemberReference,
5458 };
Samuel Antao86ace552016-04-27 22:40:57 +00005459 OMPClauseMappableExprCommon::MappableExprComponentListRef Components;
Hans Wennborgbc1b58d2016-07-30 00:41:37 +00005460 OpenMPMapClauseKind MapType;
5461 OpenMPMapClauseKind MapTypeModifier;
5462 ReturnPointerKind ReturnDevicePointer;
5463
5464 MapInfo()
5465 : MapType(OMPC_MAP_unknown), MapTypeModifier(OMPC_MAP_unknown),
5466 ReturnDevicePointer(RPK_None) {}
Samuel Antaocc10b852016-07-28 14:23:26 +00005467 MapInfo(
5468 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
5469 OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapTypeModifier,
5470 ReturnPointerKind ReturnDevicePointer)
5471 : Components(Components), MapType(MapType),
5472 MapTypeModifier(MapTypeModifier),
5473 ReturnDevicePointer(ReturnDevicePointer) {}
Samuel Antao86ace552016-04-27 22:40:57 +00005474 };
5475
5476 // We have to process the component lists that relate with the same
5477 // declaration in a single chunk so that we can generate the map flags
5478 // correctly. Therefore, we organize all lists in a map.
5479 llvm::DenseMap<const ValueDecl *, SmallVector<MapInfo, 8>> Info;
Samuel Antao8d2d7302016-05-26 18:30:22 +00005480
5481 // Helper function to fill the information map for the different supported
5482 // clauses.
Samuel Antaocc10b852016-07-28 14:23:26 +00005483 auto &&InfoGen = [&Info](
5484 const ValueDecl *D,
5485 OMPClauseMappableExprCommon::MappableExprComponentListRef L,
5486 OpenMPMapClauseKind MapType, OpenMPMapClauseKind MapModifier,
Samuel Antaocf3f83e2016-07-28 14:47:35 +00005487 MapInfo::ReturnPointerKind ReturnDevicePointer) {
Samuel Antaocc10b852016-07-28 14:23:26 +00005488 const ValueDecl *VD =
5489 D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
5490 Info[VD].push_back({L, MapType, MapModifier, ReturnDevicePointer});
5491 };
Samuel Antao8d2d7302016-05-26 18:30:22 +00005492
Paul Robinson78fb1322016-08-01 22:12:46 +00005493 // FIXME: MSVC 2013 seems to require this-> to find member CurDir.
Paul Robinson15c84002016-07-29 20:46:16 +00005494 for (auto *C : this->CurDir.getClausesOfKind<OMPMapClause>())
Samuel Antao8d2d7302016-05-26 18:30:22 +00005495 for (auto L : C->component_lists())
Samuel Antaocf3f83e2016-07-28 14:47:35 +00005496 InfoGen(L.first, L.second, C->getMapType(), C->getMapTypeModifier(),
5497 MapInfo::RPK_None);
Paul Robinson15c84002016-07-29 20:46:16 +00005498 for (auto *C : this->CurDir.getClausesOfKind<OMPToClause>())
Samuel Antao8d2d7302016-05-26 18:30:22 +00005499 for (auto L : C->component_lists())
Samuel Antaocf3f83e2016-07-28 14:47:35 +00005500 InfoGen(L.first, L.second, OMPC_MAP_to, OMPC_MAP_unknown,
5501 MapInfo::RPK_None);
Paul Robinson15c84002016-07-29 20:46:16 +00005502 for (auto *C : this->CurDir.getClausesOfKind<OMPFromClause>())
Samuel Antao8d2d7302016-05-26 18:30:22 +00005503 for (auto L : C->component_lists())
Samuel Antaocf3f83e2016-07-28 14:47:35 +00005504 InfoGen(L.first, L.second, OMPC_MAP_from, OMPC_MAP_unknown,
5505 MapInfo::RPK_None);
Samuel Antao86ace552016-04-27 22:40:57 +00005506
Samuel Antaocc10b852016-07-28 14:23:26 +00005507 // Look at the use_device_ptr clause information and mark the existing map
5508 // entries as such. If there is no map information for an entry in the
5509 // use_device_ptr list, we create one with map type 'alloc' and zero size
5510 // section. It is the user fault if that was not mapped before.
Paul Robinson78fb1322016-08-01 22:12:46 +00005511 // FIXME: MSVC 2013 seems to require this-> to find member CurDir.
Paul Robinson15c84002016-07-29 20:46:16 +00005512 for (auto *C : this->CurDir.getClausesOfKind<OMPUseDevicePtrClause>())
Samuel Antaocc10b852016-07-28 14:23:26 +00005513 for (auto L : C->component_lists()) {
5514 assert(!L.second.empty() && "Not expecting empty list of components!");
5515 const ValueDecl *VD = L.second.back().getAssociatedDeclaration();
5516 VD = cast<ValueDecl>(VD->getCanonicalDecl());
5517 auto *IE = L.second.back().getAssociatedExpression();
5518 // If the first component is a member expression, we have to look into
5519 // 'this', which maps to null in the map of map information. Otherwise
5520 // look directly for the information.
5521 auto It = Info.find(isa<MemberExpr>(IE) ? nullptr : VD);
5522
5523 // We potentially have map information for this declaration already.
5524 // Look for the first set of components that refer to it.
5525 if (It != Info.end()) {
5526 auto CI = std::find_if(
5527 It->second.begin(), It->second.end(), [VD](const MapInfo &MI) {
5528 return MI.Components.back().getAssociatedDeclaration() == VD;
5529 });
5530 // If we found a map entry, signal that the pointer has to be returned
5531 // and move on to the next declaration.
5532 if (CI != It->second.end()) {
5533 CI->ReturnDevicePointer = isa<MemberExpr>(IE)
5534 ? (VD->getType()->isReferenceType()
5535 ? MapInfo::RPK_MemberReference
5536 : MapInfo::RPK_Member)
5537 : MapInfo::RPK_Base;
5538 continue;
5539 }
5540 }
5541
5542 // We didn't find any match in our map information - generate a zero
5543 // size array section.
Paul Robinson78fb1322016-08-01 22:12:46 +00005544 // FIXME: MSVC 2013 seems to require this-> to find member CGF.
Samuel Antaocc10b852016-07-28 14:23:26 +00005545 llvm::Value *Ptr =
Paul Robinson15c84002016-07-29 20:46:16 +00005546 this->CGF
5547 .EmitLoadOfLValue(this->CGF.EmitLValue(IE), SourceLocation())
Samuel Antaocc10b852016-07-28 14:23:26 +00005548 .getScalarVal();
5549 BasePointers.push_back({Ptr, VD});
5550 Pointers.push_back(Ptr);
Paul Robinson15c84002016-07-29 20:46:16 +00005551 Sizes.push_back(llvm::Constant::getNullValue(this->CGF.SizeTy));
Samuel Antaocc10b852016-07-28 14:23:26 +00005552 Types.push_back(OMP_MAP_RETURN_PTR | OMP_MAP_FIRST_REF);
5553 }
5554
Samuel Antao86ace552016-04-27 22:40:57 +00005555 for (auto &M : Info) {
5556 // We need to know when we generate information for the first component
5557 // associated with a capture, because the mapping flags depend on it.
5558 bool IsFirstComponentList = true;
5559 for (MapInfo &L : M.second) {
5560 assert(!L.Components.empty() &&
5561 "Not expecting declaration with no component lists.");
Samuel Antaocc10b852016-07-28 14:23:26 +00005562
5563 // Remember the current base pointer index.
5564 unsigned CurrentBasePointersIdx = BasePointers.size();
Paul Robinson78fb1322016-08-01 22:12:46 +00005565 // FIXME: MSVC 2013 seems to require this-> to find the member method.
Paul Robinson15c84002016-07-29 20:46:16 +00005566 this->generateInfoForComponentList(L.MapType, L.MapTypeModifier,
5567 L.Components, BasePointers, Pointers,
5568 Sizes, Types, IsFirstComponentList);
Samuel Antaocc10b852016-07-28 14:23:26 +00005569
5570 // If this entry relates with a device pointer, set the relevant
5571 // declaration and add the 'return pointer' flag.
5572 if (IsFirstComponentList &&
5573 L.ReturnDevicePointer != MapInfo::RPK_None) {
5574 // If the pointer is not the base of the map, we need to skip the
5575 // base. If it is a reference in a member field, we also need to skip
5576 // the map of the reference.
5577 if (L.ReturnDevicePointer != MapInfo::RPK_Base) {
5578 ++CurrentBasePointersIdx;
5579 if (L.ReturnDevicePointer == MapInfo::RPK_MemberReference)
5580 ++CurrentBasePointersIdx;
5581 }
5582 assert(BasePointers.size() > CurrentBasePointersIdx &&
5583 "Unexpected number of mapped base pointers.");
5584
5585 auto *RelevantVD = L.Components.back().getAssociatedDeclaration();
5586 assert(RelevantVD &&
5587 "No relevant declaration related with device pointer??");
5588
5589 BasePointers[CurrentBasePointersIdx].setDevicePtrDecl(RelevantVD);
5590 Types[CurrentBasePointersIdx] |= OMP_MAP_RETURN_PTR;
5591 }
Samuel Antao86ace552016-04-27 22:40:57 +00005592 IsFirstComponentList = false;
5593 }
5594 }
5595 }
5596
5597 /// \brief Generate the base pointers, section pointers, sizes and map types
5598 /// associated to a given capture.
5599 void generateInfoForCapture(const CapturedStmt::Capture *Cap,
Samuel Antao6890b092016-07-28 14:25:09 +00005600 llvm::Value *Arg,
Samuel Antaocc10b852016-07-28 14:23:26 +00005601 MapBaseValuesArrayTy &BasePointers,
Samuel Antao86ace552016-04-27 22:40:57 +00005602 MapValuesArrayTy &Pointers,
5603 MapValuesArrayTy &Sizes,
5604 MapFlagsArrayTy &Types) const {
5605 assert(!Cap->capturesVariableArrayType() &&
5606 "Not expecting to generate map info for a variable array type!");
5607
5608 BasePointers.clear();
5609 Pointers.clear();
5610 Sizes.clear();
5611 Types.clear();
5612
Samuel Antao6890b092016-07-28 14:25:09 +00005613 // We need to know when we generating information for the first component
5614 // associated with a capture, because the mapping flags depend on it.
5615 bool IsFirstComponentList = true;
5616
Samuel Antao86ace552016-04-27 22:40:57 +00005617 const ValueDecl *VD =
5618 Cap->capturesThis()
5619 ? nullptr
5620 : cast<ValueDecl>(Cap->getCapturedVar()->getCanonicalDecl());
5621
Samuel Antao6890b092016-07-28 14:25:09 +00005622 // If this declaration appears in a is_device_ptr clause we just have to
5623 // pass the pointer by value. If it is a reference to a declaration, we just
5624 // pass its value, otherwise, if it is a member expression, we need to map
5625 // 'to' the field.
5626 if (!VD) {
5627 auto It = DevPointersMap.find(VD);
5628 if (It != DevPointersMap.end()) {
5629 for (auto L : It->second) {
5630 generateInfoForComponentList(
5631 /*MapType=*/OMPC_MAP_to, /*MapTypeModifier=*/OMPC_MAP_unknown, L,
5632 BasePointers, Pointers, Sizes, Types, IsFirstComponentList);
5633 IsFirstComponentList = false;
5634 }
5635 return;
5636 }
5637 } else if (DevPointersMap.count(VD)) {
5638 BasePointers.push_back({Arg, VD});
5639 Pointers.push_back(Arg);
5640 Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy));
5641 Types.push_back(OMP_MAP_PRIVATE_VAL | OMP_MAP_FIRST_REF);
5642 return;
5643 }
5644
Paul Robinson78fb1322016-08-01 22:12:46 +00005645 // FIXME: MSVC 2013 seems to require this-> to find member CurDir.
Paul Robinson15c84002016-07-29 20:46:16 +00005646 for (auto *C : this->CurDir.getClausesOfKind<OMPMapClause>())
Samuel Antao86ace552016-04-27 22:40:57 +00005647 for (auto L : C->decl_component_lists(VD)) {
5648 assert(L.first == VD &&
5649 "We got information for the wrong declaration??");
5650 assert(!L.second.empty() &&
5651 "Not expecting declaration with no component lists.");
5652 generateInfoForComponentList(C->getMapType(), C->getMapTypeModifier(),
5653 L.second, BasePointers, Pointers, Sizes,
5654 Types, IsFirstComponentList);
5655 IsFirstComponentList = false;
5656 }
5657
5658 return;
5659 }
Samuel Antaod486f842016-05-26 16:53:38 +00005660
5661 /// \brief Generate the default map information for a given capture \a CI,
5662 /// record field declaration \a RI and captured value \a CV.
Samuel Antaocc10b852016-07-28 14:23:26 +00005663 void generateDefaultMapInfo(const CapturedStmt::Capture &CI,
5664 const FieldDecl &RI, llvm::Value *CV,
5665 MapBaseValuesArrayTy &CurBasePointers,
5666 MapValuesArrayTy &CurPointers,
5667 MapValuesArrayTy &CurSizes,
5668 MapFlagsArrayTy &CurMapTypes) {
Samuel Antaod486f842016-05-26 16:53:38 +00005669
5670 // Do the default mapping.
5671 if (CI.capturesThis()) {
5672 CurBasePointers.push_back(CV);
5673 CurPointers.push_back(CV);
5674 const PointerType *PtrTy = cast<PointerType>(RI.getType().getTypePtr());
5675 CurSizes.push_back(CGF.getTypeSize(PtrTy->getPointeeType()));
5676 // Default map type.
Samuel Antaocc10b852016-07-28 14:23:26 +00005677 CurMapTypes.push_back(OMP_MAP_TO | OMP_MAP_FROM);
Samuel Antaod486f842016-05-26 16:53:38 +00005678 } else if (CI.capturesVariableByCopy()) {
Samuel Antao6d004262016-06-16 18:39:34 +00005679 CurBasePointers.push_back(CV);
5680 CurPointers.push_back(CV);
Samuel Antaod486f842016-05-26 16:53:38 +00005681 if (!RI.getType()->isAnyPointerType()) {
Samuel Antao6d004262016-06-16 18:39:34 +00005682 // We have to signal to the runtime captures passed by value that are
5683 // not pointers.
Samuel Antaocc10b852016-07-28 14:23:26 +00005684 CurMapTypes.push_back(OMP_MAP_PRIVATE_VAL);
Samuel Antaod486f842016-05-26 16:53:38 +00005685 CurSizes.push_back(CGF.getTypeSize(RI.getType()));
5686 } else {
5687 // Pointers are implicitly mapped with a zero size and no flags
5688 // (other than first map that is added for all implicit maps).
5689 CurMapTypes.push_back(0u);
Samuel Antaod486f842016-05-26 16:53:38 +00005690 CurSizes.push_back(llvm::Constant::getNullValue(CGF.SizeTy));
5691 }
5692 } else {
5693 assert(CI.capturesVariable() && "Expected captured reference.");
5694 CurBasePointers.push_back(CV);
5695 CurPointers.push_back(CV);
5696
5697 const ReferenceType *PtrTy =
5698 cast<ReferenceType>(RI.getType().getTypePtr());
5699 QualType ElementType = PtrTy->getPointeeType();
5700 CurSizes.push_back(CGF.getTypeSize(ElementType));
5701 // The default map type for a scalar/complex type is 'to' because by
5702 // default the value doesn't have to be retrieved. For an aggregate
5703 // type, the default is 'tofrom'.
5704 CurMapTypes.push_back(ElementType->isAggregateType()
Samuel Antaocc10b852016-07-28 14:23:26 +00005705 ? (OMP_MAP_TO | OMP_MAP_FROM)
5706 : OMP_MAP_TO);
Samuel Antaod486f842016-05-26 16:53:38 +00005707
5708 // If we have a capture by reference we may need to add the private
5709 // pointer flag if the base declaration shows in some first-private
5710 // clause.
5711 CurMapTypes.back() =
5712 adjustMapModifiersForPrivateClauses(CI, CurMapTypes.back());
5713 }
5714 // Every default map produces a single argument, so, it is always the
5715 // first one.
Samuel Antaocc10b852016-07-28 14:23:26 +00005716 CurMapTypes.back() |= OMP_MAP_FIRST_REF;
Samuel Antaod486f842016-05-26 16:53:38 +00005717 }
Samuel Antao86ace552016-04-27 22:40:57 +00005718};
Samuel Antaodf158d52016-04-27 22:58:19 +00005719
5720enum OpenMPOffloadingReservedDeviceIDs {
5721 /// \brief Device ID if the device was not defined, runtime should get it
5722 /// from environment variables in the spec.
5723 OMP_DEVICEID_UNDEF = -1,
5724};
5725} // anonymous namespace
5726
5727/// \brief Emit the arrays used to pass the captures and map information to the
5728/// offloading runtime library. If there is no map or capture information,
5729/// return nullptr by reference.
5730static void
Samuel Antaocc10b852016-07-28 14:23:26 +00005731emitOffloadingArrays(CodeGenFunction &CGF,
5732 MappableExprsHandler::MapBaseValuesArrayTy &BasePointers,
Samuel Antaodf158d52016-04-27 22:58:19 +00005733 MappableExprsHandler::MapValuesArrayTy &Pointers,
5734 MappableExprsHandler::MapValuesArrayTy &Sizes,
Samuel Antaocc10b852016-07-28 14:23:26 +00005735 MappableExprsHandler::MapFlagsArrayTy &MapTypes,
5736 CGOpenMPRuntime::TargetDataInfo &Info) {
Samuel Antaodf158d52016-04-27 22:58:19 +00005737 auto &CGM = CGF.CGM;
5738 auto &Ctx = CGF.getContext();
5739
Samuel Antaocc10b852016-07-28 14:23:26 +00005740 // Reset the array information.
5741 Info.clearArrayInfo();
5742 Info.NumberOfPtrs = BasePointers.size();
Samuel Antaodf158d52016-04-27 22:58:19 +00005743
Samuel Antaocc10b852016-07-28 14:23:26 +00005744 if (Info.NumberOfPtrs) {
Samuel Antaodf158d52016-04-27 22:58:19 +00005745 // Detect if we have any capture size requiring runtime evaluation of the
5746 // size so that a constant array could be eventually used.
5747 bool hasRuntimeEvaluationCaptureSize = false;
5748 for (auto *S : Sizes)
5749 if (!isa<llvm::Constant>(S)) {
5750 hasRuntimeEvaluationCaptureSize = true;
5751 break;
5752 }
5753
Samuel Antaocc10b852016-07-28 14:23:26 +00005754 llvm::APInt PointerNumAP(32, Info.NumberOfPtrs, /*isSigned=*/true);
Samuel Antaodf158d52016-04-27 22:58:19 +00005755 QualType PointerArrayType =
5756 Ctx.getConstantArrayType(Ctx.VoidPtrTy, PointerNumAP, ArrayType::Normal,
5757 /*IndexTypeQuals=*/0);
5758
Samuel Antaocc10b852016-07-28 14:23:26 +00005759 Info.BasePointersArray =
Samuel Antaodf158d52016-04-27 22:58:19 +00005760 CGF.CreateMemTemp(PointerArrayType, ".offload_baseptrs").getPointer();
Samuel Antaocc10b852016-07-28 14:23:26 +00005761 Info.PointersArray =
Samuel Antaodf158d52016-04-27 22:58:19 +00005762 CGF.CreateMemTemp(PointerArrayType, ".offload_ptrs").getPointer();
5763
5764 // If we don't have any VLA types or other types that require runtime
5765 // evaluation, we can use a constant array for the map sizes, otherwise we
5766 // need to fill up the arrays as we do for the pointers.
5767 if (hasRuntimeEvaluationCaptureSize) {
5768 QualType SizeArrayType = Ctx.getConstantArrayType(
5769 Ctx.getSizeType(), PointerNumAP, ArrayType::Normal,
5770 /*IndexTypeQuals=*/0);
Samuel Antaocc10b852016-07-28 14:23:26 +00005771 Info.SizesArray =
Samuel Antaodf158d52016-04-27 22:58:19 +00005772 CGF.CreateMemTemp(SizeArrayType, ".offload_sizes").getPointer();
5773 } else {
5774 // We expect all the sizes to be constant, so we collect them to create
5775 // a constant array.
5776 SmallVector<llvm::Constant *, 16> ConstSizes;
5777 for (auto S : Sizes)
5778 ConstSizes.push_back(cast<llvm::Constant>(S));
5779
5780 auto *SizesArrayInit = llvm::ConstantArray::get(
5781 llvm::ArrayType::get(CGM.SizeTy, ConstSizes.size()), ConstSizes);
5782 auto *SizesArrayGbl = new llvm::GlobalVariable(
5783 CGM.getModule(), SizesArrayInit->getType(),
5784 /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage,
5785 SizesArrayInit, ".offload_sizes");
Peter Collingbournebcf909d2016-06-14 21:02:05 +00005786 SizesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Samuel Antaocc10b852016-07-28 14:23:26 +00005787 Info.SizesArray = SizesArrayGbl;
Samuel Antaodf158d52016-04-27 22:58:19 +00005788 }
5789
5790 // The map types are always constant so we don't need to generate code to
5791 // fill arrays. Instead, we create an array constant.
5792 llvm::Constant *MapTypesArrayInit =
5793 llvm::ConstantDataArray::get(CGF.Builder.getContext(), MapTypes);
5794 auto *MapTypesArrayGbl = new llvm::GlobalVariable(
5795 CGM.getModule(), MapTypesArrayInit->getType(),
5796 /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage,
5797 MapTypesArrayInit, ".offload_maptypes");
Peter Collingbournebcf909d2016-06-14 21:02:05 +00005798 MapTypesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Samuel Antaocc10b852016-07-28 14:23:26 +00005799 Info.MapTypesArray = MapTypesArrayGbl;
Samuel Antaodf158d52016-04-27 22:58:19 +00005800
Samuel Antaocc10b852016-07-28 14:23:26 +00005801 for (unsigned i = 0; i < Info.NumberOfPtrs; ++i) {
5802 llvm::Value *BPVal = *BasePointers[i];
Samuel Antaodf158d52016-04-27 22:58:19 +00005803 if (BPVal->getType()->isPointerTy())
5804 BPVal = CGF.Builder.CreateBitCast(BPVal, CGM.VoidPtrTy);
5805 else {
5806 assert(BPVal->getType()->isIntegerTy() &&
5807 "If not a pointer, the value type must be an integer.");
5808 BPVal = CGF.Builder.CreateIntToPtr(BPVal, CGM.VoidPtrTy);
5809 }
5810 llvm::Value *BP = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00005811 llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
5812 Info.BasePointersArray, 0, i);
Samuel Antaodf158d52016-04-27 22:58:19 +00005813 Address BPAddr(BP, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy));
5814 CGF.Builder.CreateStore(BPVal, BPAddr);
5815
Samuel Antaocc10b852016-07-28 14:23:26 +00005816 if (Info.requiresDevicePointerInfo())
5817 if (auto *DevVD = BasePointers[i].getDevicePtrDecl())
5818 Info.CaptureDeviceAddrMap.insert(std::make_pair(DevVD, BPAddr));
5819
Samuel Antaodf158d52016-04-27 22:58:19 +00005820 llvm::Value *PVal = Pointers[i];
5821 if (PVal->getType()->isPointerTy())
5822 PVal = CGF.Builder.CreateBitCast(PVal, CGM.VoidPtrTy);
5823 else {
5824 assert(PVal->getType()->isIntegerTy() &&
5825 "If not a pointer, the value type must be an integer.");
5826 PVal = CGF.Builder.CreateIntToPtr(PVal, CGM.VoidPtrTy);
5827 }
5828 llvm::Value *P = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00005829 llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
5830 Info.PointersArray, 0, i);
Samuel Antaodf158d52016-04-27 22:58:19 +00005831 Address PAddr(P, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy));
5832 CGF.Builder.CreateStore(PVal, PAddr);
5833
5834 if (hasRuntimeEvaluationCaptureSize) {
5835 llvm::Value *S = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00005836 llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs),
5837 Info.SizesArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00005838 /*Idx0=*/0,
5839 /*Idx1=*/i);
5840 Address SAddr(S, Ctx.getTypeAlignInChars(Ctx.getSizeType()));
5841 CGF.Builder.CreateStore(
5842 CGF.Builder.CreateIntCast(Sizes[i], CGM.SizeTy, /*isSigned=*/true),
5843 SAddr);
5844 }
5845 }
5846 }
5847}
5848/// \brief Emit the arguments to be passed to the runtime library based on the
5849/// arrays of pointers, sizes and map types.
5850static void emitOffloadingArraysArgument(
5851 CodeGenFunction &CGF, llvm::Value *&BasePointersArrayArg,
5852 llvm::Value *&PointersArrayArg, llvm::Value *&SizesArrayArg,
Samuel Antaocc10b852016-07-28 14:23:26 +00005853 llvm::Value *&MapTypesArrayArg, CGOpenMPRuntime::TargetDataInfo &Info) {
Samuel Antaodf158d52016-04-27 22:58:19 +00005854 auto &CGM = CGF.CGM;
Samuel Antaocc10b852016-07-28 14:23:26 +00005855 if (Info.NumberOfPtrs) {
Samuel Antaodf158d52016-04-27 22:58:19 +00005856 BasePointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00005857 llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
5858 Info.BasePointersArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00005859 /*Idx0=*/0, /*Idx1=*/0);
5860 PointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00005861 llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
5862 Info.PointersArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00005863 /*Idx0=*/0,
5864 /*Idx1=*/0);
5865 SizesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00005866 llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), Info.SizesArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00005867 /*Idx0=*/0, /*Idx1=*/0);
5868 MapTypesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00005869 llvm::ArrayType::get(CGM.Int32Ty, Info.NumberOfPtrs),
5870 Info.MapTypesArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00005871 /*Idx0=*/0,
5872 /*Idx1=*/0);
5873 } else {
5874 BasePointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
5875 PointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
5876 SizesArrayArg = llvm::ConstantPointerNull::get(CGM.SizeTy->getPointerTo());
5877 MapTypesArrayArg =
5878 llvm::ConstantPointerNull::get(CGM.Int32Ty->getPointerTo());
5879 }
Samuel Antao86ace552016-04-27 22:40:57 +00005880}
5881
Samuel Antaobed3c462015-10-02 16:14:20 +00005882void CGOpenMPRuntime::emitTargetCall(CodeGenFunction &CGF,
5883 const OMPExecutableDirective &D,
5884 llvm::Value *OutlinedFn,
Samuel Antaoee8fb302016-01-06 13:42:12 +00005885 llvm::Value *OutlinedFnID,
Samuel Antaobed3c462015-10-02 16:14:20 +00005886 const Expr *IfCond, const Expr *Device,
5887 ArrayRef<llvm::Value *> CapturedVars) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00005888 if (!CGF.HaveInsertPoint())
5889 return;
Samuel Antaobed3c462015-10-02 16:14:20 +00005890
Samuel Antaoee8fb302016-01-06 13:42:12 +00005891 assert(OutlinedFn && "Invalid outlined function!");
5892
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005893 auto &Ctx = CGF.getContext();
5894
Samuel Antao86ace552016-04-27 22:40:57 +00005895 // Fill up the arrays with all the captured variables.
5896 MappableExprsHandler::MapValuesArrayTy KernelArgs;
Samuel Antaocc10b852016-07-28 14:23:26 +00005897 MappableExprsHandler::MapBaseValuesArrayTy BasePointers;
Samuel Antao86ace552016-04-27 22:40:57 +00005898 MappableExprsHandler::MapValuesArrayTy Pointers;
5899 MappableExprsHandler::MapValuesArrayTy Sizes;
5900 MappableExprsHandler::MapFlagsArrayTy MapTypes;
Samuel Antaobed3c462015-10-02 16:14:20 +00005901
Samuel Antaocc10b852016-07-28 14:23:26 +00005902 MappableExprsHandler::MapBaseValuesArrayTy CurBasePointers;
Samuel Antao86ace552016-04-27 22:40:57 +00005903 MappableExprsHandler::MapValuesArrayTy CurPointers;
5904 MappableExprsHandler::MapValuesArrayTy CurSizes;
5905 MappableExprsHandler::MapFlagsArrayTy CurMapTypes;
5906
Samuel Antaod486f842016-05-26 16:53:38 +00005907 // Get mappable expression information.
5908 MappableExprsHandler MEHandler(D, CGF);
Samuel Antaobed3c462015-10-02 16:14:20 +00005909
5910 const CapturedStmt &CS = *cast<CapturedStmt>(D.getAssociatedStmt());
5911 auto RI = CS.getCapturedRecordDecl()->field_begin();
Samuel Antaobed3c462015-10-02 16:14:20 +00005912 auto CV = CapturedVars.begin();
5913 for (CapturedStmt::const_capture_iterator CI = CS.capture_begin(),
5914 CE = CS.capture_end();
5915 CI != CE; ++CI, ++RI, ++CV) {
5916 StringRef Name;
5917 QualType Ty;
Samuel Antaobed3c462015-10-02 16:14:20 +00005918
Samuel Antao86ace552016-04-27 22:40:57 +00005919 CurBasePointers.clear();
5920 CurPointers.clear();
5921 CurSizes.clear();
5922 CurMapTypes.clear();
5923
5924 // VLA sizes are passed to the outlined region by copy and do not have map
5925 // information associated.
Samuel Antaobed3c462015-10-02 16:14:20 +00005926 if (CI->capturesVariableArrayType()) {
Samuel Antao86ace552016-04-27 22:40:57 +00005927 CurBasePointers.push_back(*CV);
5928 CurPointers.push_back(*CV);
5929 CurSizes.push_back(CGF.getTypeSize(RI->getType()));
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005930 // Copy to the device as an argument. No need to retrieve it.
Samuel Antao6782e942016-05-26 16:48:10 +00005931 CurMapTypes.push_back(MappableExprsHandler::OMP_MAP_PRIVATE_VAL |
5932 MappableExprsHandler::OMP_MAP_FIRST_REF);
Samuel Antaobed3c462015-10-02 16:14:20 +00005933 } else {
Samuel Antao86ace552016-04-27 22:40:57 +00005934 // If we have any information in the map clause, we use it, otherwise we
5935 // just do a default mapping.
Samuel Antao6890b092016-07-28 14:25:09 +00005936 MEHandler.generateInfoForCapture(CI, *CV, CurBasePointers, CurPointers,
Samuel Antao86ace552016-04-27 22:40:57 +00005937 CurSizes, CurMapTypes);
Samuel Antaod486f842016-05-26 16:53:38 +00005938 if (CurBasePointers.empty())
5939 MEHandler.generateDefaultMapInfo(*CI, **RI, *CV, CurBasePointers,
5940 CurPointers, CurSizes, CurMapTypes);
Samuel Antaobed3c462015-10-02 16:14:20 +00005941 }
Samuel Antao86ace552016-04-27 22:40:57 +00005942 // We expect to have at least an element of information for this capture.
5943 assert(!CurBasePointers.empty() && "Non-existing map pointer for capture!");
5944 assert(CurBasePointers.size() == CurPointers.size() &&
5945 CurBasePointers.size() == CurSizes.size() &&
5946 CurBasePointers.size() == CurMapTypes.size() &&
5947 "Inconsistent map information sizes!");
Samuel Antaobed3c462015-10-02 16:14:20 +00005948
Samuel Antao86ace552016-04-27 22:40:57 +00005949 // The kernel args are always the first elements of the base pointers
5950 // associated with a capture.
Samuel Antaocc10b852016-07-28 14:23:26 +00005951 KernelArgs.push_back(*CurBasePointers.front());
Samuel Antao86ace552016-04-27 22:40:57 +00005952 // We need to append the results of this capture to what we already have.
5953 BasePointers.append(CurBasePointers.begin(), CurBasePointers.end());
5954 Pointers.append(CurPointers.begin(), CurPointers.end());
5955 Sizes.append(CurSizes.begin(), CurSizes.end());
5956 MapTypes.append(CurMapTypes.begin(), CurMapTypes.end());
Samuel Antaobed3c462015-10-02 16:14:20 +00005957 }
5958
5959 // Keep track on whether the host function has to be executed.
5960 auto OffloadErrorQType =
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005961 Ctx.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true);
Samuel Antaobed3c462015-10-02 16:14:20 +00005962 auto OffloadError = CGF.MakeAddrLValue(
5963 CGF.CreateMemTemp(OffloadErrorQType, ".run_host_version"),
5964 OffloadErrorQType);
5965 CGF.EmitStoreOfScalar(llvm::Constant::getNullValue(CGM.Int32Ty),
5966 OffloadError);
5967
5968 // Fill up the pointer arrays and transfer execution to the device.
Samuel Antaodf158d52016-04-27 22:58:19 +00005969 auto &&ThenGen = [&Ctx, &BasePointers, &Pointers, &Sizes, &MapTypes, Device,
5970 OutlinedFnID, OffloadError, OffloadErrorQType,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005971 &D](CodeGenFunction &CGF, PrePostActionTy &) {
5972 auto &RT = CGF.CGM.getOpenMPRuntime();
Samuel Antaodf158d52016-04-27 22:58:19 +00005973 // Emit the offloading arrays.
Samuel Antaocc10b852016-07-28 14:23:26 +00005974 TargetDataInfo Info;
5975 emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info);
5976 emitOffloadingArraysArgument(CGF, Info.BasePointersArray,
5977 Info.PointersArray, Info.SizesArray,
5978 Info.MapTypesArray, Info);
Samuel Antaobed3c462015-10-02 16:14:20 +00005979
5980 // On top of the arrays that were filled up, the target offloading call
5981 // takes as arguments the device id as well as the host pointer. The host
5982 // pointer is used by the runtime library to identify the current target
5983 // region, so it only has to be unique and not necessarily point to
5984 // anything. It could be the pointer to the outlined function that
5985 // implements the target region, but we aren't using that so that the
5986 // compiler doesn't need to keep that, and could therefore inline the host
5987 // function if proven worthwhile during optimization.
5988
Samuel Antaoee8fb302016-01-06 13:42:12 +00005989 // From this point on, we need to have an ID of the target region defined.
5990 assert(OutlinedFnID && "Invalid outlined function ID!");
Samuel Antaobed3c462015-10-02 16:14:20 +00005991
5992 // Emit device ID if any.
5993 llvm::Value *DeviceID;
5994 if (Device)
5995 DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005996 CGF.Int32Ty, /*isSigned=*/true);
Samuel Antaobed3c462015-10-02 16:14:20 +00005997 else
5998 DeviceID = CGF.Builder.getInt32(OMP_DEVICEID_UNDEF);
5999
Samuel Antaodf158d52016-04-27 22:58:19 +00006000 // Emit the number of elements in the offloading arrays.
6001 llvm::Value *PointerNum = CGF.Builder.getInt32(BasePointers.size());
6002
Samuel Antaob68e2db2016-03-03 16:20:23 +00006003 // Return value of the runtime offloading call.
6004 llvm::Value *Return;
6005
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006006 auto *NumTeams = emitNumTeamsClauseForTargetDirective(RT, CGF, D);
6007 auto *ThreadLimit = emitThreadLimitClauseForTargetDirective(RT, CGF, D);
Samuel Antaob68e2db2016-03-03 16:20:23 +00006008
6009 // If we have NumTeams defined this means that we have an enclosed teams
6010 // region. Therefore we also expect to have ThreadLimit defined. These two
6011 // values should be defined in the presence of a teams directive, regardless
6012 // of having any clauses associated. If the user is using teams but no
6013 // clauses, these two values will be the default that should be passed to
6014 // the runtime library - a 32-bit integer with the value zero.
6015 if (NumTeams) {
6016 assert(ThreadLimit && "Thread limit expression should be available along "
6017 "with number of teams.");
6018 llvm::Value *OffloadingArgs[] = {
Samuel Antaocc10b852016-07-28 14:23:26 +00006019 DeviceID, OutlinedFnID,
6020 PointerNum, Info.BasePointersArray,
6021 Info.PointersArray, Info.SizesArray,
6022 Info.MapTypesArray, NumTeams,
6023 ThreadLimit};
Samuel Antaob68e2db2016-03-03 16:20:23 +00006024 Return = CGF.EmitRuntimeCall(
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006025 RT.createRuntimeFunction(OMPRTL__tgt_target_teams), OffloadingArgs);
Samuel Antaob68e2db2016-03-03 16:20:23 +00006026 } else {
6027 llvm::Value *OffloadingArgs[] = {
Samuel Antaocc10b852016-07-28 14:23:26 +00006028 DeviceID, OutlinedFnID,
6029 PointerNum, Info.BasePointersArray,
6030 Info.PointersArray, Info.SizesArray,
6031 Info.MapTypesArray};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006032 Return = CGF.EmitRuntimeCall(RT.createRuntimeFunction(OMPRTL__tgt_target),
Samuel Antaob68e2db2016-03-03 16:20:23 +00006033 OffloadingArgs);
6034 }
Samuel Antaobed3c462015-10-02 16:14:20 +00006035
6036 CGF.EmitStoreOfScalar(Return, OffloadError);
6037 };
6038
Samuel Antaoee8fb302016-01-06 13:42:12 +00006039 // Notify that the host version must be executed.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006040 auto &&ElseGen = [OffloadError](CodeGenFunction &CGF, PrePostActionTy &) {
6041 CGF.EmitStoreOfScalar(llvm::ConstantInt::get(CGF.Int32Ty, /*V=*/-1u),
Samuel Antaoee8fb302016-01-06 13:42:12 +00006042 OffloadError);
6043 };
6044
6045 // If we have a target function ID it means that we need to support
6046 // offloading, otherwise, just execute on the host. We need to execute on host
6047 // regardless of the conditional in the if clause if, e.g., the user do not
6048 // specify target triples.
6049 if (OutlinedFnID) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006050 if (IfCond)
Samuel Antaoee8fb302016-01-06 13:42:12 +00006051 emitOMPIfClause(CGF, IfCond, ThenGen, ElseGen);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006052 else {
6053 RegionCodeGenTy ThenRCG(ThenGen);
6054 ThenRCG(CGF);
Alexey Bataevf539faa2016-03-28 12:58:34 +00006055 }
6056 } else {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006057 RegionCodeGenTy ElseRCG(ElseGen);
6058 ElseRCG(CGF);
Alexey Bataevf539faa2016-03-28 12:58:34 +00006059 }
Samuel Antaobed3c462015-10-02 16:14:20 +00006060
6061 // Check the error code and execute the host version if required.
6062 auto OffloadFailedBlock = CGF.createBasicBlock("omp_offload.failed");
6063 auto OffloadContBlock = CGF.createBasicBlock("omp_offload.cont");
6064 auto OffloadErrorVal = CGF.EmitLoadOfScalar(OffloadError, SourceLocation());
6065 auto Failed = CGF.Builder.CreateIsNotNull(OffloadErrorVal);
6066 CGF.Builder.CreateCondBr(Failed, OffloadFailedBlock, OffloadContBlock);
6067
6068 CGF.EmitBlock(OffloadFailedBlock);
Samuel Antao86ace552016-04-27 22:40:57 +00006069 CGF.Builder.CreateCall(OutlinedFn, KernelArgs);
Samuel Antaobed3c462015-10-02 16:14:20 +00006070 CGF.EmitBranch(OffloadContBlock);
6071
6072 CGF.EmitBlock(OffloadContBlock, /*IsFinished=*/true);
Samuel Antaobed3c462015-10-02 16:14:20 +00006073}
Samuel Antaoee8fb302016-01-06 13:42:12 +00006074
6075void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S,
6076 StringRef ParentName) {
6077 if (!S)
6078 return;
6079
6080 // If we find a OMP target directive, codegen the outline function and
6081 // register the result.
6082 // FIXME: Add other directives with target when they become supported.
6083 bool isTargetDirective = isa<OMPTargetDirective>(S);
6084
6085 if (isTargetDirective) {
6086 auto *E = cast<OMPExecutableDirective>(S);
6087 unsigned DeviceID;
6088 unsigned FileID;
6089 unsigned Line;
Samuel Antaoee8fb302016-01-06 13:42:12 +00006090 getTargetEntryUniqueInfo(CGM.getContext(), E->getLocStart(), DeviceID,
Samuel Antao2de62b02016-02-13 23:35:10 +00006091 FileID, Line);
Samuel Antaoee8fb302016-01-06 13:42:12 +00006092
6093 // Is this a target region that should not be emitted as an entry point? If
6094 // so just signal we are done with this target region.
Samuel Antao2de62b02016-02-13 23:35:10 +00006095 if (!OffloadEntriesInfoManager.hasTargetRegionEntryInfo(DeviceID, FileID,
6096 ParentName, Line))
Samuel Antaoee8fb302016-01-06 13:42:12 +00006097 return;
6098
6099 llvm::Function *Fn;
6100 llvm::Constant *Addr;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006101 std::tie(Fn, Addr) =
6102 CodeGenFunction::EmitOMPTargetDirectiveOutlinedFunction(
6103 CGM, cast<OMPTargetDirective>(*E), ParentName,
6104 /*isOffloadEntry=*/true);
Samuel Antaoee8fb302016-01-06 13:42:12 +00006105 assert(Fn && Addr && "Target region emission failed.");
6106 return;
6107 }
6108
6109 if (const OMPExecutableDirective *E = dyn_cast<OMPExecutableDirective>(S)) {
Samuel Antaoe49645c2016-05-08 06:43:56 +00006110 if (!E->hasAssociatedStmt())
Samuel Antaoee8fb302016-01-06 13:42:12 +00006111 return;
6112
6113 scanForTargetRegionsFunctions(
6114 cast<CapturedStmt>(E->getAssociatedStmt())->getCapturedStmt(),
6115 ParentName);
6116 return;
6117 }
6118
6119 // If this is a lambda function, look into its body.
6120 if (auto *L = dyn_cast<LambdaExpr>(S))
6121 S = L->getBody();
6122
6123 // Keep looking for target regions recursively.
6124 for (auto *II : S->children())
6125 scanForTargetRegionsFunctions(II, ParentName);
Samuel Antaoee8fb302016-01-06 13:42:12 +00006126}
6127
6128bool CGOpenMPRuntime::emitTargetFunctions(GlobalDecl GD) {
6129 auto &FD = *cast<FunctionDecl>(GD.getDecl());
6130
6131 // If emitting code for the host, we do not process FD here. Instead we do
6132 // the normal code generation.
6133 if (!CGM.getLangOpts().OpenMPIsDevice)
6134 return false;
6135
6136 // Try to detect target regions in the function.
6137 scanForTargetRegionsFunctions(FD.getBody(), CGM.getMangledName(GD));
6138
6139 // We should not emit any function othen that the ones created during the
6140 // scanning. Therefore, we signal that this function is completely dealt
6141 // with.
6142 return true;
6143}
6144
6145bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) {
6146 if (!CGM.getLangOpts().OpenMPIsDevice)
6147 return false;
6148
6149 // Check if there are Ctors/Dtors in this declaration and look for target
6150 // regions in it. We use the complete variant to produce the kernel name
6151 // mangling.
6152 QualType RDTy = cast<VarDecl>(GD.getDecl())->getType();
6153 if (auto *RD = RDTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) {
6154 for (auto *Ctor : RD->ctors()) {
6155 StringRef ParentName =
6156 CGM.getMangledName(GlobalDecl(Ctor, Ctor_Complete));
6157 scanForTargetRegionsFunctions(Ctor->getBody(), ParentName);
6158 }
6159 auto *Dtor = RD->getDestructor();
6160 if (Dtor) {
6161 StringRef ParentName =
6162 CGM.getMangledName(GlobalDecl(Dtor, Dtor_Complete));
6163 scanForTargetRegionsFunctions(Dtor->getBody(), ParentName);
6164 }
6165 }
6166
6167 // If we are in target mode we do not emit any global (declare target is not
6168 // implemented yet). Therefore we signal that GD was processed in this case.
6169 return true;
6170}
6171
6172bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) {
6173 auto *VD = GD.getDecl();
6174 if (isa<FunctionDecl>(VD))
6175 return emitTargetFunctions(GD);
6176
6177 return emitTargetGlobalVariable(GD);
6178}
6179
6180llvm::Function *CGOpenMPRuntime::emitRegistrationFunction() {
6181 // If we have offloading in the current module, we need to emit the entries
6182 // now and register the offloading descriptor.
6183 createOffloadEntriesAndInfoMetadata();
6184
6185 // Create and register the offloading binary descriptors. This is the main
6186 // entity that captures all the information about offloading in the current
6187 // compilation unit.
6188 return createOffloadingBinaryDescriptorRegistration();
6189}
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00006190
6191void CGOpenMPRuntime::emitTeamsCall(CodeGenFunction &CGF,
6192 const OMPExecutableDirective &D,
6193 SourceLocation Loc,
6194 llvm::Value *OutlinedFn,
6195 ArrayRef<llvm::Value *> CapturedVars) {
6196 if (!CGF.HaveInsertPoint())
6197 return;
6198
6199 auto *RTLoc = emitUpdateLocation(CGF, Loc);
6200 CodeGenFunction::RunCleanupsScope Scope(CGF);
6201
6202 // Build call __kmpc_fork_teams(loc, n, microtask, var1, .., varn);
6203 llvm::Value *Args[] = {
6204 RTLoc,
6205 CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars
6206 CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy())};
6207 llvm::SmallVector<llvm::Value *, 16> RealArgs;
6208 RealArgs.append(std::begin(Args), std::end(Args));
6209 RealArgs.append(CapturedVars.begin(), CapturedVars.end());
6210
6211 auto RTLFn = createRuntimeFunction(OMPRTL__kmpc_fork_teams);
6212 CGF.EmitRuntimeCall(RTLFn, RealArgs);
6213}
6214
6215void CGOpenMPRuntime::emitNumTeamsClause(CodeGenFunction &CGF,
Carlo Bertollic6872252016-04-04 15:55:02 +00006216 const Expr *NumTeams,
6217 const Expr *ThreadLimit,
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00006218 SourceLocation Loc) {
6219 if (!CGF.HaveInsertPoint())
6220 return;
6221
6222 auto *RTLoc = emitUpdateLocation(CGF, Loc);
6223
Carlo Bertollic6872252016-04-04 15:55:02 +00006224 llvm::Value *NumTeamsVal =
6225 (NumTeams)
6226 ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(NumTeams),
6227 CGF.CGM.Int32Ty, /* isSigned = */ true)
6228 : CGF.Builder.getInt32(0);
6229
6230 llvm::Value *ThreadLimitVal =
6231 (ThreadLimit)
6232 ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(ThreadLimit),
6233 CGF.CGM.Int32Ty, /* isSigned = */ true)
6234 : CGF.Builder.getInt32(0);
6235
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00006236 // Build call __kmpc_push_num_teamss(&loc, global_tid, num_teams, thread_limit)
Carlo Bertollic6872252016-04-04 15:55:02 +00006237 llvm::Value *PushNumTeamsArgs[] = {RTLoc, getThreadID(CGF, Loc), NumTeamsVal,
6238 ThreadLimitVal};
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00006239 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_teams),
6240 PushNumTeamsArgs);
6241}
Samuel Antaodf158d52016-04-27 22:58:19 +00006242
Samuel Antaocc10b852016-07-28 14:23:26 +00006243void CGOpenMPRuntime::emitTargetDataCalls(
6244 CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
6245 const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) {
Samuel Antaodf158d52016-04-27 22:58:19 +00006246 if (!CGF.HaveInsertPoint())
6247 return;
6248
Samuel Antaocc10b852016-07-28 14:23:26 +00006249 // Action used to replace the default codegen action and turn privatization
6250 // off.
6251 PrePostActionTy NoPrivAction;
Samuel Antaodf158d52016-04-27 22:58:19 +00006252
6253 // Generate the code for the opening of the data environment. Capture all the
6254 // arguments of the runtime call by reference because they are used in the
6255 // closing of the region.
Samuel Antaocc10b852016-07-28 14:23:26 +00006256 auto &&BeginThenGen = [&D, &CGF, Device, &Info, &CodeGen, &NoPrivAction](
6257 CodeGenFunction &CGF, PrePostActionTy &) {
Samuel Antaodf158d52016-04-27 22:58:19 +00006258 // Fill up the arrays with all the mapped variables.
Samuel Antaocc10b852016-07-28 14:23:26 +00006259 MappableExprsHandler::MapBaseValuesArrayTy BasePointers;
Samuel Antaodf158d52016-04-27 22:58:19 +00006260 MappableExprsHandler::MapValuesArrayTy Pointers;
6261 MappableExprsHandler::MapValuesArrayTy Sizes;
6262 MappableExprsHandler::MapFlagsArrayTy MapTypes;
6263
6264 // Get map clause information.
6265 MappableExprsHandler MCHandler(D, CGF);
6266 MCHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes);
Samuel Antaodf158d52016-04-27 22:58:19 +00006267
6268 // Fill up the arrays and create the arguments.
Samuel Antaocc10b852016-07-28 14:23:26 +00006269 emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info);
Samuel Antaodf158d52016-04-27 22:58:19 +00006270
6271 llvm::Value *BasePointersArrayArg = nullptr;
6272 llvm::Value *PointersArrayArg = nullptr;
6273 llvm::Value *SizesArrayArg = nullptr;
6274 llvm::Value *MapTypesArrayArg = nullptr;
6275 emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg,
Samuel Antaocc10b852016-07-28 14:23:26 +00006276 SizesArrayArg, MapTypesArrayArg, Info);
Samuel Antaodf158d52016-04-27 22:58:19 +00006277
6278 // Emit device ID if any.
6279 llvm::Value *DeviceID = nullptr;
6280 if (Device)
6281 DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
6282 CGF.Int32Ty, /*isSigned=*/true);
6283 else
6284 DeviceID = CGF.Builder.getInt32(OMP_DEVICEID_UNDEF);
6285
6286 // Emit the number of elements in the offloading arrays.
Samuel Antaocc10b852016-07-28 14:23:26 +00006287 auto *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs);
Samuel Antaodf158d52016-04-27 22:58:19 +00006288
6289 llvm::Value *OffloadingArgs[] = {
6290 DeviceID, PointerNum, BasePointersArrayArg,
6291 PointersArrayArg, SizesArrayArg, MapTypesArrayArg};
6292 auto &RT = CGF.CGM.getOpenMPRuntime();
6293 CGF.EmitRuntimeCall(RT.createRuntimeFunction(OMPRTL__tgt_target_data_begin),
6294 OffloadingArgs);
Samuel Antaocc10b852016-07-28 14:23:26 +00006295
6296 // If device pointer privatization is required, emit the body of the region
6297 // here. It will have to be duplicated: with and without privatization.
6298 if (!Info.CaptureDeviceAddrMap.empty())
6299 CodeGen(CGF);
Samuel Antaodf158d52016-04-27 22:58:19 +00006300 };
6301
6302 // Generate code for the closing of the data region.
Samuel Antaocc10b852016-07-28 14:23:26 +00006303 auto &&EndThenGen = [&CGF, Device, &Info](CodeGenFunction &CGF,
6304 PrePostActionTy &) {
6305 assert(Info.isValid() && "Invalid data environment closing arguments.");
Samuel Antaodf158d52016-04-27 22:58:19 +00006306
6307 llvm::Value *BasePointersArrayArg = nullptr;
6308 llvm::Value *PointersArrayArg = nullptr;
6309 llvm::Value *SizesArrayArg = nullptr;
6310 llvm::Value *MapTypesArrayArg = nullptr;
6311 emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg,
Samuel Antaocc10b852016-07-28 14:23:26 +00006312 SizesArrayArg, MapTypesArrayArg, Info);
Samuel Antaodf158d52016-04-27 22:58:19 +00006313
6314 // Emit device ID if any.
6315 llvm::Value *DeviceID = nullptr;
6316 if (Device)
6317 DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
6318 CGF.Int32Ty, /*isSigned=*/true);
6319 else
6320 DeviceID = CGF.Builder.getInt32(OMP_DEVICEID_UNDEF);
6321
6322 // Emit the number of elements in the offloading arrays.
Samuel Antaocc10b852016-07-28 14:23:26 +00006323 auto *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs);
Samuel Antaodf158d52016-04-27 22:58:19 +00006324
6325 llvm::Value *OffloadingArgs[] = {
6326 DeviceID, PointerNum, BasePointersArrayArg,
6327 PointersArrayArg, SizesArrayArg, MapTypesArrayArg};
6328 auto &RT = CGF.CGM.getOpenMPRuntime();
6329 CGF.EmitRuntimeCall(RT.createRuntimeFunction(OMPRTL__tgt_target_data_end),
6330 OffloadingArgs);
6331 };
6332
Samuel Antaocc10b852016-07-28 14:23:26 +00006333 // If we need device pointer privatization, we need to emit the body of the
6334 // region with no privatization in the 'else' branch of the conditional.
6335 // Otherwise, we don't have to do anything.
6336 auto &&BeginElseGen = [&Info, &CodeGen, &NoPrivAction](CodeGenFunction &CGF,
6337 PrePostActionTy &) {
6338 if (!Info.CaptureDeviceAddrMap.empty()) {
6339 CodeGen.setAction(NoPrivAction);
6340 CodeGen(CGF);
6341 }
6342 };
6343
6344 // We don't have to do anything to close the region if the if clause evaluates
6345 // to false.
6346 auto &&EndElseGen = [](CodeGenFunction &CGF, PrePostActionTy &) {};
Samuel Antaodf158d52016-04-27 22:58:19 +00006347
6348 if (IfCond) {
Samuel Antaocc10b852016-07-28 14:23:26 +00006349 emitOMPIfClause(CGF, IfCond, BeginThenGen, BeginElseGen);
Samuel Antaodf158d52016-04-27 22:58:19 +00006350 } else {
Samuel Antaocc10b852016-07-28 14:23:26 +00006351 RegionCodeGenTy RCG(BeginThenGen);
6352 RCG(CGF);
Samuel Antaodf158d52016-04-27 22:58:19 +00006353 }
6354
Samuel Antaocc10b852016-07-28 14:23:26 +00006355 // If we don't require privatization of device pointers, we emit the body in
6356 // between the runtime calls. This avoids duplicating the body code.
6357 if (Info.CaptureDeviceAddrMap.empty()) {
6358 CodeGen.setAction(NoPrivAction);
6359 CodeGen(CGF);
6360 }
Samuel Antaodf158d52016-04-27 22:58:19 +00006361
6362 if (IfCond) {
Samuel Antaocc10b852016-07-28 14:23:26 +00006363 emitOMPIfClause(CGF, IfCond, EndThenGen, EndElseGen);
Samuel Antaodf158d52016-04-27 22:58:19 +00006364 } else {
Samuel Antaocc10b852016-07-28 14:23:26 +00006365 RegionCodeGenTy RCG(EndThenGen);
6366 RCG(CGF);
Samuel Antaodf158d52016-04-27 22:58:19 +00006367 }
6368}
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00006369
Samuel Antao8d2d7302016-05-26 18:30:22 +00006370void CGOpenMPRuntime::emitTargetDataStandAloneCall(
Samuel Antao8dd66282016-04-27 23:14:30 +00006371 CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
6372 const Expr *Device) {
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00006373 if (!CGF.HaveInsertPoint())
6374 return;
6375
Samuel Antao8dd66282016-04-27 23:14:30 +00006376 assert((isa<OMPTargetEnterDataDirective>(D) ||
Samuel Antao8d2d7302016-05-26 18:30:22 +00006377 isa<OMPTargetExitDataDirective>(D) ||
6378 isa<OMPTargetUpdateDirective>(D)) &&
6379 "Expecting either target enter, exit data, or update directives.");
Samuel Antao8dd66282016-04-27 23:14:30 +00006380
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00006381 // Generate the code for the opening of the data environment.
6382 auto &&ThenGen = [&D, &CGF, Device](CodeGenFunction &CGF, PrePostActionTy &) {
6383 // Fill up the arrays with all the mapped variables.
Samuel Antaocc10b852016-07-28 14:23:26 +00006384 MappableExprsHandler::MapBaseValuesArrayTy BasePointers;
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00006385 MappableExprsHandler::MapValuesArrayTy Pointers;
6386 MappableExprsHandler::MapValuesArrayTy Sizes;
6387 MappableExprsHandler::MapFlagsArrayTy MapTypes;
6388
6389 // Get map clause information.
Samuel Antao8d2d7302016-05-26 18:30:22 +00006390 MappableExprsHandler MEHandler(D, CGF);
6391 MEHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00006392
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00006393 // Fill up the arrays and create the arguments.
Samuel Antaocc10b852016-07-28 14:23:26 +00006394 TargetDataInfo Info;
6395 emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info);
6396 emitOffloadingArraysArgument(CGF, Info.BasePointersArray,
6397 Info.PointersArray, Info.SizesArray,
6398 Info.MapTypesArray, Info);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00006399
6400 // Emit device ID if any.
6401 llvm::Value *DeviceID = nullptr;
6402 if (Device)
6403 DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
6404 CGF.Int32Ty, /*isSigned=*/true);
6405 else
6406 DeviceID = CGF.Builder.getInt32(OMP_DEVICEID_UNDEF);
6407
6408 // Emit the number of elements in the offloading arrays.
6409 auto *PointerNum = CGF.Builder.getInt32(BasePointers.size());
6410
6411 llvm::Value *OffloadingArgs[] = {
Samuel Antaocc10b852016-07-28 14:23:26 +00006412 DeviceID, PointerNum, Info.BasePointersArray,
6413 Info.PointersArray, Info.SizesArray, Info.MapTypesArray};
Samuel Antao8d2d7302016-05-26 18:30:22 +00006414
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00006415 auto &RT = CGF.CGM.getOpenMPRuntime();
Samuel Antao8d2d7302016-05-26 18:30:22 +00006416 // Select the right runtime function call for each expected standalone
6417 // directive.
6418 OpenMPRTLFunction RTLFn;
6419 switch (D.getDirectiveKind()) {
6420 default:
6421 llvm_unreachable("Unexpected standalone target data directive.");
6422 break;
6423 case OMPD_target_enter_data:
6424 RTLFn = OMPRTL__tgt_target_data_begin;
6425 break;
6426 case OMPD_target_exit_data:
6427 RTLFn = OMPRTL__tgt_target_data_end;
6428 break;
6429 case OMPD_target_update:
6430 RTLFn = OMPRTL__tgt_target_data_update;
6431 break;
6432 }
6433 CGF.EmitRuntimeCall(RT.createRuntimeFunction(RTLFn), OffloadingArgs);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00006434 };
6435
6436 // In the event we get an if clause, we don't have to take any action on the
6437 // else side.
6438 auto &&ElseGen = [](CodeGenFunction &CGF, PrePostActionTy &) {};
6439
6440 if (IfCond) {
6441 emitOMPIfClause(CGF, IfCond, ThenGen, ElseGen);
6442 } else {
6443 RegionCodeGenTy ThenGenRCG(ThenGen);
6444 ThenGenRCG(CGF);
6445 }
6446}
Alexey Bataevc7a82b42016-05-06 09:40:08 +00006447
6448namespace {
6449 /// Kind of parameter in a function with 'declare simd' directive.
6450 enum ParamKindTy { LinearWithVarStride, Linear, Uniform, Vector };
6451 /// Attribute set of the parameter.
6452 struct ParamAttrTy {
6453 ParamKindTy Kind = Vector;
6454 llvm::APSInt StrideOrArg;
6455 llvm::APSInt Alignment;
6456 };
6457} // namespace
6458
6459static unsigned evaluateCDTSize(const FunctionDecl *FD,
6460 ArrayRef<ParamAttrTy> ParamAttrs) {
6461 // Every vector variant of a SIMD-enabled function has a vector length (VLEN).
6462 // If OpenMP clause "simdlen" is used, the VLEN is the value of the argument
6463 // of that clause. The VLEN value must be power of 2.
6464 // In other case the notion of the function`s "characteristic data type" (CDT)
6465 // is used to compute the vector length.
6466 // CDT is defined in the following order:
6467 // a) For non-void function, the CDT is the return type.
6468 // b) If the function has any non-uniform, non-linear parameters, then the
6469 // CDT is the type of the first such parameter.
6470 // c) If the CDT determined by a) or b) above is struct, union, or class
6471 // type which is pass-by-value (except for the type that maps to the
6472 // built-in complex data type), the characteristic data type is int.
6473 // d) If none of the above three cases is applicable, the CDT is int.
6474 // The VLEN is then determined based on the CDT and the size of vector
6475 // register of that ISA for which current vector version is generated. The
6476 // VLEN is computed using the formula below:
6477 // VLEN = sizeof(vector_register) / sizeof(CDT),
6478 // where vector register size specified in section 3.2.1 Registers and the
6479 // Stack Frame of original AMD64 ABI document.
6480 QualType RetType = FD->getReturnType();
6481 if (RetType.isNull())
6482 return 0;
6483 ASTContext &C = FD->getASTContext();
6484 QualType CDT;
6485 if (!RetType.isNull() && !RetType->isVoidType())
6486 CDT = RetType;
6487 else {
6488 unsigned Offset = 0;
6489 if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
6490 if (ParamAttrs[Offset].Kind == Vector)
6491 CDT = C.getPointerType(C.getRecordType(MD->getParent()));
6492 ++Offset;
6493 }
6494 if (CDT.isNull()) {
6495 for (unsigned I = 0, E = FD->getNumParams(); I < E; ++I) {
6496 if (ParamAttrs[I + Offset].Kind == Vector) {
6497 CDT = FD->getParamDecl(I)->getType();
6498 break;
6499 }
6500 }
6501 }
6502 }
6503 if (CDT.isNull())
6504 CDT = C.IntTy;
6505 CDT = CDT->getCanonicalTypeUnqualified();
6506 if (CDT->isRecordType() || CDT->isUnionType())
6507 CDT = C.IntTy;
6508 return C.getTypeSize(CDT);
6509}
6510
6511static void
6512emitX86DeclareSimdFunction(const FunctionDecl *FD, llvm::Function *Fn,
6513 llvm::APSInt VLENVal,
6514 ArrayRef<ParamAttrTy> ParamAttrs,
6515 OMPDeclareSimdDeclAttr::BranchStateTy State) {
6516 struct ISADataTy {
6517 char ISA;
6518 unsigned VecRegSize;
6519 };
6520 ISADataTy ISAData[] = {
6521 {
6522 'b', 128
6523 }, // SSE
6524 {
6525 'c', 256
6526 }, // AVX
6527 {
6528 'd', 256
6529 }, // AVX2
6530 {
6531 'e', 512
6532 }, // AVX512
6533 };
6534 llvm::SmallVector<char, 2> Masked;
6535 switch (State) {
6536 case OMPDeclareSimdDeclAttr::BS_Undefined:
6537 Masked.push_back('N');
6538 Masked.push_back('M');
6539 break;
6540 case OMPDeclareSimdDeclAttr::BS_Notinbranch:
6541 Masked.push_back('N');
6542 break;
6543 case OMPDeclareSimdDeclAttr::BS_Inbranch:
6544 Masked.push_back('M');
6545 break;
6546 }
6547 for (auto Mask : Masked) {
6548 for (auto &Data : ISAData) {
6549 SmallString<256> Buffer;
6550 llvm::raw_svector_ostream Out(Buffer);
6551 Out << "_ZGV" << Data.ISA << Mask;
6552 if (!VLENVal) {
6553 Out << llvm::APSInt::getUnsigned(Data.VecRegSize /
6554 evaluateCDTSize(FD, ParamAttrs));
6555 } else
6556 Out << VLENVal;
6557 for (auto &ParamAttr : ParamAttrs) {
6558 switch (ParamAttr.Kind){
6559 case LinearWithVarStride:
6560 Out << 's' << ParamAttr.StrideOrArg;
6561 break;
6562 case Linear:
6563 Out << 'l';
6564 if (!!ParamAttr.StrideOrArg)
6565 Out << ParamAttr.StrideOrArg;
6566 break;
6567 case Uniform:
6568 Out << 'u';
6569 break;
6570 case Vector:
6571 Out << 'v';
6572 break;
6573 }
6574 if (!!ParamAttr.Alignment)
6575 Out << 'a' << ParamAttr.Alignment;
6576 }
6577 Out << '_' << Fn->getName();
6578 Fn->addFnAttr(Out.str());
6579 }
6580 }
6581}
6582
6583void CGOpenMPRuntime::emitDeclareSimdFunction(const FunctionDecl *FD,
6584 llvm::Function *Fn) {
6585 ASTContext &C = CGM.getContext();
6586 FD = FD->getCanonicalDecl();
6587 // Map params to their positions in function decl.
6588 llvm::DenseMap<const Decl *, unsigned> ParamPositions;
6589 if (isa<CXXMethodDecl>(FD))
6590 ParamPositions.insert({FD, 0});
6591 unsigned ParamPos = ParamPositions.size();
David Majnemer59f77922016-06-24 04:05:48 +00006592 for (auto *P : FD->parameters()) {
Alexey Bataevc7a82b42016-05-06 09:40:08 +00006593 ParamPositions.insert({P->getCanonicalDecl(), ParamPos});
6594 ++ParamPos;
6595 }
6596 for (auto *Attr : FD->specific_attrs<OMPDeclareSimdDeclAttr>()) {
6597 llvm::SmallVector<ParamAttrTy, 8> ParamAttrs(ParamPositions.size());
6598 // Mark uniform parameters.
6599 for (auto *E : Attr->uniforms()) {
6600 E = E->IgnoreParenImpCasts();
6601 unsigned Pos;
6602 if (isa<CXXThisExpr>(E))
6603 Pos = ParamPositions[FD];
6604 else {
6605 auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl())
6606 ->getCanonicalDecl();
6607 Pos = ParamPositions[PVD];
6608 }
6609 ParamAttrs[Pos].Kind = Uniform;
6610 }
6611 // Get alignment info.
6612 auto NI = Attr->alignments_begin();
6613 for (auto *E : Attr->aligneds()) {
6614 E = E->IgnoreParenImpCasts();
6615 unsigned Pos;
6616 QualType ParmTy;
6617 if (isa<CXXThisExpr>(E)) {
6618 Pos = ParamPositions[FD];
6619 ParmTy = E->getType();
6620 } else {
6621 auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl())
6622 ->getCanonicalDecl();
6623 Pos = ParamPositions[PVD];
6624 ParmTy = PVD->getType();
6625 }
6626 ParamAttrs[Pos].Alignment =
6627 (*NI) ? (*NI)->EvaluateKnownConstInt(C)
6628 : llvm::APSInt::getUnsigned(
6629 C.toCharUnitsFromBits(C.getOpenMPDefaultSimdAlign(ParmTy))
6630 .getQuantity());
6631 ++NI;
6632 }
6633 // Mark linear parameters.
6634 auto SI = Attr->steps_begin();
6635 auto MI = Attr->modifiers_begin();
6636 for (auto *E : Attr->linears()) {
6637 E = E->IgnoreParenImpCasts();
6638 unsigned Pos;
6639 if (isa<CXXThisExpr>(E))
6640 Pos = ParamPositions[FD];
6641 else {
6642 auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl())
6643 ->getCanonicalDecl();
6644 Pos = ParamPositions[PVD];
6645 }
6646 auto &ParamAttr = ParamAttrs[Pos];
6647 ParamAttr.Kind = Linear;
6648 if (*SI) {
6649 if (!(*SI)->EvaluateAsInt(ParamAttr.StrideOrArg, C,
6650 Expr::SE_AllowSideEffects)) {
6651 if (auto *DRE = cast<DeclRefExpr>((*SI)->IgnoreParenImpCasts())) {
6652 if (auto *StridePVD = cast<ParmVarDecl>(DRE->getDecl())) {
6653 ParamAttr.Kind = LinearWithVarStride;
6654 ParamAttr.StrideOrArg = llvm::APSInt::getUnsigned(
6655 ParamPositions[StridePVD->getCanonicalDecl()]);
6656 }
6657 }
6658 }
6659 }
6660 ++SI;
6661 ++MI;
6662 }
6663 llvm::APSInt VLENVal;
6664 if (const Expr *VLEN = Attr->getSimdlen())
6665 VLENVal = VLEN->EvaluateKnownConstInt(C);
6666 OMPDeclareSimdDeclAttr::BranchStateTy State = Attr->getBranchState();
6667 if (CGM.getTriple().getArch() == llvm::Triple::x86 ||
6668 CGM.getTriple().getArch() == llvm::Triple::x86_64)
6669 emitX86DeclareSimdFunction(FD, Fn, VLENVal, ParamAttrs, State);
6670 }
6671}
Alexey Bataev8b427062016-05-25 12:36:08 +00006672
6673namespace {
6674/// Cleanup action for doacross support.
6675class DoacrossCleanupTy final : public EHScopeStack::Cleanup {
6676public:
6677 static const int DoacrossFinArgs = 2;
6678
6679private:
6680 llvm::Value *RTLFn;
6681 llvm::Value *Args[DoacrossFinArgs];
6682
6683public:
6684 DoacrossCleanupTy(llvm::Value *RTLFn, ArrayRef<llvm::Value *> CallArgs)
6685 : RTLFn(RTLFn) {
6686 assert(CallArgs.size() == DoacrossFinArgs);
6687 std::copy(CallArgs.begin(), CallArgs.end(), std::begin(Args));
6688 }
6689 void Emit(CodeGenFunction &CGF, Flags /*flags*/) override {
6690 if (!CGF.HaveInsertPoint())
6691 return;
6692 CGF.EmitRuntimeCall(RTLFn, Args);
6693 }
6694};
6695} // namespace
6696
6697void CGOpenMPRuntime::emitDoacrossInit(CodeGenFunction &CGF,
6698 const OMPLoopDirective &D) {
6699 if (!CGF.HaveInsertPoint())
6700 return;
6701
6702 ASTContext &C = CGM.getContext();
6703 QualType Int64Ty = C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/true);
6704 RecordDecl *RD;
6705 if (KmpDimTy.isNull()) {
6706 // Build struct kmp_dim { // loop bounds info casted to kmp_int64
6707 // kmp_int64 lo; // lower
6708 // kmp_int64 up; // upper
6709 // kmp_int64 st; // stride
6710 // };
6711 RD = C.buildImplicitRecord("kmp_dim");
6712 RD->startDefinition();
6713 addFieldToRecordDecl(C, RD, Int64Ty);
6714 addFieldToRecordDecl(C, RD, Int64Ty);
6715 addFieldToRecordDecl(C, RD, Int64Ty);
6716 RD->completeDefinition();
6717 KmpDimTy = C.getRecordType(RD);
6718 } else
6719 RD = cast<RecordDecl>(KmpDimTy->getAsTagDecl());
6720
6721 Address DimsAddr = CGF.CreateMemTemp(KmpDimTy, "dims");
6722 CGF.EmitNullInitialization(DimsAddr, KmpDimTy);
6723 enum { LowerFD = 0, UpperFD, StrideFD };
6724 // Fill dims with data.
6725 LValue DimsLVal = CGF.MakeAddrLValue(DimsAddr, KmpDimTy);
6726 // dims.upper = num_iterations;
6727 LValue UpperLVal =
6728 CGF.EmitLValueForField(DimsLVal, *std::next(RD->field_begin(), UpperFD));
6729 llvm::Value *NumIterVal = CGF.EmitScalarConversion(
6730 CGF.EmitScalarExpr(D.getNumIterations()), D.getNumIterations()->getType(),
6731 Int64Ty, D.getNumIterations()->getExprLoc());
6732 CGF.EmitStoreOfScalar(NumIterVal, UpperLVal);
6733 // dims.stride = 1;
6734 LValue StrideLVal =
6735 CGF.EmitLValueForField(DimsLVal, *std::next(RD->field_begin(), StrideFD));
6736 CGF.EmitStoreOfScalar(llvm::ConstantInt::getSigned(CGM.Int64Ty, /*V=*/1),
6737 StrideLVal);
6738
6739 // Build call void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid,
6740 // kmp_int32 num_dims, struct kmp_dim * dims);
6741 llvm::Value *Args[] = {emitUpdateLocation(CGF, D.getLocStart()),
6742 getThreadID(CGF, D.getLocStart()),
6743 llvm::ConstantInt::getSigned(CGM.Int32Ty, 1),
6744 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
6745 DimsAddr.getPointer(), CGM.VoidPtrTy)};
6746
6747 llvm::Value *RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_init);
6748 CGF.EmitRuntimeCall(RTLFn, Args);
6749 llvm::Value *FiniArgs[DoacrossCleanupTy::DoacrossFinArgs] = {
6750 emitUpdateLocation(CGF, D.getLocEnd()), getThreadID(CGF, D.getLocEnd())};
6751 llvm::Value *FiniRTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_fini);
6752 CGF.EHStack.pushCleanup<DoacrossCleanupTy>(NormalAndEHCleanup, FiniRTLFn,
6753 llvm::makeArrayRef(FiniArgs));
6754}
6755
6756void CGOpenMPRuntime::emitDoacrossOrdered(CodeGenFunction &CGF,
6757 const OMPDependClause *C) {
6758 QualType Int64Ty =
6759 CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
6760 const Expr *CounterVal = C->getCounterValue();
6761 assert(CounterVal);
6762 llvm::Value *CntVal = CGF.EmitScalarConversion(CGF.EmitScalarExpr(CounterVal),
6763 CounterVal->getType(), Int64Ty,
6764 CounterVal->getExprLoc());
6765 Address CntAddr = CGF.CreateMemTemp(Int64Ty, ".cnt.addr");
6766 CGF.EmitStoreOfScalar(CntVal, CntAddr, /*Volatile=*/false, Int64Ty);
6767 llvm::Value *Args[] = {emitUpdateLocation(CGF, C->getLocStart()),
6768 getThreadID(CGF, C->getLocStart()),
6769 CntAddr.getPointer()};
6770 llvm::Value *RTLFn;
6771 if (C->getDependencyKind() == OMPC_DEPEND_source)
6772 RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_post);
6773 else {
6774 assert(C->getDependencyKind() == OMPC_DEPEND_sink);
6775 RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_wait);
6776 }
6777 CGF.EmitRuntimeCall(RTLFn, Args);
6778}
6779