blob: 5fb5b6488413dd60c3550495590a164ec6087650 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===----- CGOpenMPRuntime.cpp - Interface to OpenMP Runtimes -------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataev9959db52014-05-06 10:08:46 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This provides a class for OpenMP runtime code generation.
10//
11//===----------------------------------------------------------------------===//
12
Samuel Antaoee8fb302016-01-06 13:42:12 +000013#include "CGCXXABI.h"
14#include "CGCleanup.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "CGOpenMPRuntime.h"
Alexey Bataeva4fa0b82018-04-16 17:59:34 +000016#include "CGRecordLayout.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000017#include "CodeGenFunction.h"
John McCall5ad74072017-03-02 20:04:19 +000018#include "clang/CodeGen/ConstantInitBuilder.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000019#include "clang/AST/Decl.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000020#include "clang/AST/StmtOpenMP.h"
Richard Smithbf5bcf22018-06-26 23:20:26 +000021#include "clang/Basic/BitmaskEnum.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000022#include "llvm/ADT/ArrayRef.h"
Teresa Johnsonffc4e242016-11-11 05:35:12 +000023#include "llvm/Bitcode/BitcodeReader.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000024#include "llvm/IR/DerivedTypes.h"
25#include "llvm/IR/GlobalValue.h"
26#include "llvm/IR/Value.h"
Samuel Antaoee8fb302016-01-06 13:42:12 +000027#include "llvm/Support/Format.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000028#include "llvm/Support/raw_ostream.h"
Alexey Bataev23b69422014-06-18 07:08:49 +000029#include <cassert>
Alexey Bataev9959db52014-05-06 10:08:46 +000030
31using namespace clang;
32using namespace CodeGen;
33
Benjamin Kramerc52193f2014-10-10 13:57:57 +000034namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000035/// Base class for handling code generation inside OpenMP regions.
Alexey Bataev18095712014-10-10 12:19:54 +000036class CGOpenMPRegionInfo : public CodeGenFunction::CGCapturedStmtInfo {
37public:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000038 /// Kinds of OpenMP regions used in codegen.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000039 enum CGOpenMPRegionKind {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000040 /// Region with outlined function for standalone 'parallel'
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000041 /// directive.
42 ParallelOutlinedRegion,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000043 /// Region with outlined function for standalone 'task' directive.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000044 TaskOutlinedRegion,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000045 /// Region for constructs that do not require function outlining,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000046 /// like 'for', 'sections', 'atomic' etc. directives.
47 InlinedRegion,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000048 /// Region with outlined function for standalone 'target' directive.
Samuel Antaobed3c462015-10-02 16:14:20 +000049 TargetRegion,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000050 };
Alexey Bataev18095712014-10-10 12:19:54 +000051
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000052 CGOpenMPRegionInfo(const CapturedStmt &CS,
53 const CGOpenMPRegionKind RegionKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +000054 const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind,
55 bool HasCancel)
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000056 : CGCapturedStmtInfo(CS, CR_OpenMP), RegionKind(RegionKind),
Alexey Bataev25e5b442015-09-15 12:52:43 +000057 CodeGen(CodeGen), Kind(Kind), HasCancel(HasCancel) {}
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000058
59 CGOpenMPRegionInfo(const CGOpenMPRegionKind RegionKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +000060 const RegionCodeGenTy &CodeGen, OpenMPDirectiveKind Kind,
61 bool HasCancel)
Alexey Bataev81c7ea02015-07-03 09:56:58 +000062 : CGCapturedStmtInfo(CR_OpenMP), RegionKind(RegionKind), CodeGen(CodeGen),
Alexey Bataev25e5b442015-09-15 12:52:43 +000063 Kind(Kind), HasCancel(HasCancel) {}
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000064
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000065 /// Get a variable or parameter for storing global thread id
Alexey Bataev18095712014-10-10 12:19:54 +000066 /// inside OpenMP construct.
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000067 virtual const VarDecl *getThreadIDVariable() const = 0;
Alexey Bataev18095712014-10-10 12:19:54 +000068
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000069 /// Emit the captured statement body.
Hans Wennborg7eb54642015-09-10 17:07:54 +000070 void EmitBody(CodeGenFunction &CGF, const Stmt *S) override;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000071
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000072 /// Get an LValue for the current ThreadID variable.
Alexey Bataev62b63b12015-03-10 07:28:44 +000073 /// \return LValue for thread id variable. This LValue always has type int32*.
74 virtual LValue getThreadIDVariableLValue(CodeGenFunction &CGF);
Alexey Bataev18095712014-10-10 12:19:54 +000075
Alexey Bataev48591dd2016-04-20 04:01:36 +000076 virtual void emitUntiedSwitch(CodeGenFunction & /*CGF*/) {}
77
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000078 CGOpenMPRegionKind getRegionKind() const { return RegionKind; }
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000079
Alexey Bataev81c7ea02015-07-03 09:56:58 +000080 OpenMPDirectiveKind getDirectiveKind() const { return Kind; }
81
Alexey Bataev25e5b442015-09-15 12:52:43 +000082 bool hasCancel() const { return HasCancel; }
83
Alexey Bataev18095712014-10-10 12:19:54 +000084 static bool classof(const CGCapturedStmtInfo *Info) {
85 return Info->getKind() == CR_OpenMP;
86 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000087
Alexey Bataev48591dd2016-04-20 04:01:36 +000088 ~CGOpenMPRegionInfo() override = default;
89
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000090protected:
Alexey Bataev6f1ffc02015-04-10 04:50:10 +000091 CGOpenMPRegionKind RegionKind;
Hans Wennborg45c74392016-01-12 20:54:36 +000092 RegionCodeGenTy CodeGen;
Alexey Bataev81c7ea02015-07-03 09:56:58 +000093 OpenMPDirectiveKind Kind;
Alexey Bataev25e5b442015-09-15 12:52:43 +000094 bool HasCancel;
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000095};
Alexey Bataev18095712014-10-10 12:19:54 +000096
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000097/// API for captured statement code generation in OpenMP constructs.
Alexey Bataev48591dd2016-04-20 04:01:36 +000098class CGOpenMPOutlinedRegionInfo final : public CGOpenMPRegionInfo {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000099public:
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000100 CGOpenMPOutlinedRegionInfo(const CapturedStmt &CS, const VarDecl *ThreadIDVar,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000101 const RegionCodeGenTy &CodeGen,
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000102 OpenMPDirectiveKind Kind, bool HasCancel,
103 StringRef HelperName)
Alexey Bataev25e5b442015-09-15 12:52:43 +0000104 : CGOpenMPRegionInfo(CS, ParallelOutlinedRegion, CodeGen, Kind,
105 HasCancel),
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000106 ThreadIDVar(ThreadIDVar), HelperName(HelperName) {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000107 assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region.");
108 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000109
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000110 /// Get a variable or parameter for storing global thread id
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000111 /// inside OpenMP construct.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000112 const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000113
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000114 /// Get the name of the capture helper.
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000115 StringRef getHelperName() const override { return HelperName; }
Alexey Bataev18095712014-10-10 12:19:54 +0000116
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000117 static bool classof(const CGCapturedStmtInfo *Info) {
118 return CGOpenMPRegionInfo::classof(Info) &&
119 cast<CGOpenMPRegionInfo>(Info)->getRegionKind() ==
120 ParallelOutlinedRegion;
121 }
122
Alexey Bataev18095712014-10-10 12:19:54 +0000123private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000124 /// A variable or parameter storing global thread id for OpenMP
Alexey Bataev18095712014-10-10 12:19:54 +0000125 /// constructs.
126 const VarDecl *ThreadIDVar;
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000127 StringRef HelperName;
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000128};
129
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000130/// API for captured statement code generation in OpenMP constructs.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000131class CGOpenMPTaskOutlinedRegionInfo final : public CGOpenMPRegionInfo {
Alexey Bataev62b63b12015-03-10 07:28:44 +0000132public:
Alexey Bataev48591dd2016-04-20 04:01:36 +0000133 class UntiedTaskActionTy final : public PrePostActionTy {
134 bool Untied;
135 const VarDecl *PartIDVar;
136 const RegionCodeGenTy UntiedCodeGen;
137 llvm::SwitchInst *UntiedSwitch = nullptr;
138
139 public:
140 UntiedTaskActionTy(bool Tied, const VarDecl *PartIDVar,
141 const RegionCodeGenTy &UntiedCodeGen)
142 : Untied(!Tied), PartIDVar(PartIDVar), UntiedCodeGen(UntiedCodeGen) {}
143 void Enter(CodeGenFunction &CGF) override {
144 if (Untied) {
145 // Emit task switching point.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000146 LValue PartIdLVal = CGF.EmitLoadOfPointerLValue(
Alexey Bataev48591dd2016-04-20 04:01:36 +0000147 CGF.GetAddrOfLocalVar(PartIDVar),
148 PartIDVar->getType()->castAs<PointerType>());
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000149 llvm::Value *Res =
150 CGF.EmitLoadOfScalar(PartIdLVal, PartIDVar->getLocation());
151 llvm::BasicBlock *DoneBB = CGF.createBasicBlock(".untied.done.");
Alexey Bataev48591dd2016-04-20 04:01:36 +0000152 UntiedSwitch = CGF.Builder.CreateSwitch(Res, DoneBB);
153 CGF.EmitBlock(DoneBB);
154 CGF.EmitBranchThroughCleanup(CGF.ReturnBlock);
155 CGF.EmitBlock(CGF.createBasicBlock(".untied.jmp."));
156 UntiedSwitch->addCase(CGF.Builder.getInt32(0),
157 CGF.Builder.GetInsertBlock());
158 emitUntiedSwitch(CGF);
159 }
160 }
161 void emitUntiedSwitch(CodeGenFunction &CGF) const {
162 if (Untied) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000163 LValue PartIdLVal = CGF.EmitLoadOfPointerLValue(
Alexey Bataev48591dd2016-04-20 04:01:36 +0000164 CGF.GetAddrOfLocalVar(PartIDVar),
165 PartIDVar->getType()->castAs<PointerType>());
166 CGF.EmitStoreOfScalar(CGF.Builder.getInt32(UntiedSwitch->getNumCases()),
167 PartIdLVal);
168 UntiedCodeGen(CGF);
169 CodeGenFunction::JumpDest CurPoint =
170 CGF.getJumpDestInCurrentScope(".untied.next.");
171 CGF.EmitBranchThroughCleanup(CGF.ReturnBlock);
172 CGF.EmitBlock(CGF.createBasicBlock(".untied.jmp."));
173 UntiedSwitch->addCase(CGF.Builder.getInt32(UntiedSwitch->getNumCases()),
174 CGF.Builder.GetInsertBlock());
175 CGF.EmitBranchThroughCleanup(CurPoint);
176 CGF.EmitBlock(CurPoint.getBlock());
177 }
178 }
179 unsigned getNumberOfParts() const { return UntiedSwitch->getNumCases(); }
180 };
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000181 CGOpenMPTaskOutlinedRegionInfo(const CapturedStmt &CS,
Alexey Bataev62b63b12015-03-10 07:28:44 +0000182 const VarDecl *ThreadIDVar,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000183 const RegionCodeGenTy &CodeGen,
Alexey Bataev48591dd2016-04-20 04:01:36 +0000184 OpenMPDirectiveKind Kind, bool HasCancel,
185 const UntiedTaskActionTy &Action)
Alexey Bataev25e5b442015-09-15 12:52:43 +0000186 : CGOpenMPRegionInfo(CS, TaskOutlinedRegion, CodeGen, Kind, HasCancel),
Alexey Bataev48591dd2016-04-20 04:01:36 +0000187 ThreadIDVar(ThreadIDVar), Action(Action) {
Alexey Bataev62b63b12015-03-10 07:28:44 +0000188 assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region.");
189 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000190
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000191 /// Get a variable or parameter for storing global thread id
Alexey Bataev62b63b12015-03-10 07:28:44 +0000192 /// inside OpenMP construct.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000193 const VarDecl *getThreadIDVariable() const override { return ThreadIDVar; }
Alexey Bataev62b63b12015-03-10 07:28:44 +0000194
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000195 /// Get an LValue for the current ThreadID variable.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000196 LValue getThreadIDVariableLValue(CodeGenFunction &CGF) override;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000197
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000198 /// Get the name of the capture helper.
Alexey Bataev62b63b12015-03-10 07:28:44 +0000199 StringRef getHelperName() const override { return ".omp_outlined."; }
200
Alexey Bataev48591dd2016-04-20 04:01:36 +0000201 void emitUntiedSwitch(CodeGenFunction &CGF) override {
202 Action.emitUntiedSwitch(CGF);
203 }
204
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000205 static bool classof(const CGCapturedStmtInfo *Info) {
206 return CGOpenMPRegionInfo::classof(Info) &&
207 cast<CGOpenMPRegionInfo>(Info)->getRegionKind() ==
208 TaskOutlinedRegion;
209 }
210
Alexey Bataev62b63b12015-03-10 07:28:44 +0000211private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000212 /// A variable or parameter storing global thread id for OpenMP
Alexey Bataev62b63b12015-03-10 07:28:44 +0000213 /// constructs.
214 const VarDecl *ThreadIDVar;
Alexey Bataev48591dd2016-04-20 04:01:36 +0000215 /// Action for emitting code for untied tasks.
216 const UntiedTaskActionTy &Action;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000217};
218
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000219/// API for inlined captured statement code generation in OpenMP
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000220/// constructs.
221class CGOpenMPInlinedRegionInfo : public CGOpenMPRegionInfo {
222public:
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000223 CGOpenMPInlinedRegionInfo(CodeGenFunction::CGCapturedStmtInfo *OldCSI,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000224 const RegionCodeGenTy &CodeGen,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000225 OpenMPDirectiveKind Kind, bool HasCancel)
226 : CGOpenMPRegionInfo(InlinedRegion, CodeGen, Kind, HasCancel),
227 OldCSI(OldCSI),
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000228 OuterRegionInfo(dyn_cast_or_null<CGOpenMPRegionInfo>(OldCSI)) {}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000229
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000230 // Retrieve the value of the context parameter.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000231 llvm::Value *getContextValue() const override {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000232 if (OuterRegionInfo)
233 return OuterRegionInfo->getContextValue();
234 llvm_unreachable("No context value for inlined OpenMP region");
235 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000236
Hans Wennborg7eb54642015-09-10 17:07:54 +0000237 void setContextValue(llvm::Value *V) override {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000238 if (OuterRegionInfo) {
239 OuterRegionInfo->setContextValue(V);
240 return;
241 }
242 llvm_unreachable("No context value for inlined OpenMP region");
243 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000244
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000245 /// Lookup the captured field decl for a variable.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000246 const FieldDecl *lookup(const VarDecl *VD) const override {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000247 if (OuterRegionInfo)
248 return OuterRegionInfo->lookup(VD);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000249 // If there is no outer outlined region,no need to lookup in a list of
250 // captured variables, we can use the original one.
251 return nullptr;
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000252 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000253
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000254 FieldDecl *getThisFieldDecl() const override {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000255 if (OuterRegionInfo)
256 return OuterRegionInfo->getThisFieldDecl();
257 return nullptr;
258 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000259
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000260 /// Get a variable or parameter for storing global thread id
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000261 /// inside OpenMP construct.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000262 const VarDecl *getThreadIDVariable() const override {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000263 if (OuterRegionInfo)
264 return OuterRegionInfo->getThreadIDVariable();
265 return nullptr;
266 }
Alexey Bataev62b63b12015-03-10 07:28:44 +0000267
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000268 /// Get an LValue for the current ThreadID variable.
Alexey Bataev311a9282017-10-12 13:51:32 +0000269 LValue getThreadIDVariableLValue(CodeGenFunction &CGF) override {
270 if (OuterRegionInfo)
271 return OuterRegionInfo->getThreadIDVariableLValue(CGF);
272 llvm_unreachable("No LValue for inlined OpenMP construct");
273 }
274
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000275 /// Get the name of the capture helper.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000276 StringRef getHelperName() const override {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000277 if (auto *OuterRegionInfo = getOldCSI())
278 return OuterRegionInfo->getHelperName();
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000279 llvm_unreachable("No helper name for inlined OpenMP construct");
280 }
281
Alexey Bataev48591dd2016-04-20 04:01:36 +0000282 void emitUntiedSwitch(CodeGenFunction &CGF) override {
283 if (OuterRegionInfo)
284 OuterRegionInfo->emitUntiedSwitch(CGF);
285 }
286
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000287 CodeGenFunction::CGCapturedStmtInfo *getOldCSI() const { return OldCSI; }
288
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000289 static bool classof(const CGCapturedStmtInfo *Info) {
290 return CGOpenMPRegionInfo::classof(Info) &&
291 cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == InlinedRegion;
292 }
293
Alexey Bataev48591dd2016-04-20 04:01:36 +0000294 ~CGOpenMPInlinedRegionInfo() override = default;
295
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000296private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000297 /// CodeGen info about outer OpenMP region.
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000298 CodeGenFunction::CGCapturedStmtInfo *OldCSI;
299 CGOpenMPRegionInfo *OuterRegionInfo;
Alexey Bataev18095712014-10-10 12:19:54 +0000300};
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000301
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000302/// API for captured statement code generation in OpenMP target
Samuel Antaobed3c462015-10-02 16:14:20 +0000303/// constructs. For this captures, implicit parameters are used instead of the
Samuel Antaoee8fb302016-01-06 13:42:12 +0000304/// captured fields. The name of the target region has to be unique in a given
305/// application so it is provided by the client, because only the client has
306/// the information to generate that.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000307class CGOpenMPTargetRegionInfo final : public CGOpenMPRegionInfo {
Samuel Antaobed3c462015-10-02 16:14:20 +0000308public:
309 CGOpenMPTargetRegionInfo(const CapturedStmt &CS,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000310 const RegionCodeGenTy &CodeGen, StringRef HelperName)
Samuel Antaobed3c462015-10-02 16:14:20 +0000311 : CGOpenMPRegionInfo(CS, TargetRegion, CodeGen, OMPD_target,
Samuel Antaoee8fb302016-01-06 13:42:12 +0000312 /*HasCancel=*/false),
313 HelperName(HelperName) {}
Samuel Antaobed3c462015-10-02 16:14:20 +0000314
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000315 /// This is unused for target regions because each starts executing
Samuel Antaobed3c462015-10-02 16:14:20 +0000316 /// with a single thread.
317 const VarDecl *getThreadIDVariable() const override { return nullptr; }
318
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000319 /// Get the name of the capture helper.
Samuel Antaoee8fb302016-01-06 13:42:12 +0000320 StringRef getHelperName() const override { return HelperName; }
Samuel Antaobed3c462015-10-02 16:14:20 +0000321
322 static bool classof(const CGCapturedStmtInfo *Info) {
323 return CGOpenMPRegionInfo::classof(Info) &&
324 cast<CGOpenMPRegionInfo>(Info)->getRegionKind() == TargetRegion;
325 }
Samuel Antaoee8fb302016-01-06 13:42:12 +0000326
327private:
328 StringRef HelperName;
Samuel Antaobed3c462015-10-02 16:14:20 +0000329};
330
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000331static void EmptyCodeGen(CodeGenFunction &, PrePostActionTy &) {
Samuel Antaob68e2db2016-03-03 16:20:23 +0000332 llvm_unreachable("No codegen for expressions");
333}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000334/// API for generation of expressions captured in a innermost OpenMP
Samuel Antaob68e2db2016-03-03 16:20:23 +0000335/// region.
Alexey Bataev48591dd2016-04-20 04:01:36 +0000336class CGOpenMPInnerExprInfo final : public CGOpenMPInlinedRegionInfo {
Samuel Antaob68e2db2016-03-03 16:20:23 +0000337public:
338 CGOpenMPInnerExprInfo(CodeGenFunction &CGF, const CapturedStmt &CS)
339 : CGOpenMPInlinedRegionInfo(CGF.CapturedStmtInfo, EmptyCodeGen,
340 OMPD_unknown,
341 /*HasCancel=*/false),
342 PrivScope(CGF) {
343 // Make sure the globals captured in the provided statement are local by
344 // using the privatization logic. We assume the same variable is not
345 // captured more than once.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000346 for (const auto &C : CS.captures()) {
Samuel Antaob68e2db2016-03-03 16:20:23 +0000347 if (!C.capturesVariable() && !C.capturesVariableByCopy())
348 continue;
349
350 const VarDecl *VD = C.getCapturedVar();
351 if (VD->isLocalVarDeclOrParm())
352 continue;
353
Bruno Ricci5fc4db72018-12-21 14:10:18 +0000354 DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(VD),
Samuel Antaob68e2db2016-03-03 16:20:23 +0000355 /*RefersToEnclosingVariableOrCapture=*/false,
356 VD->getType().getNonReferenceType(), VK_LValue,
Alexey Bataeva9b9cc02018-01-23 18:12:38 +0000357 C.getLocation());
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000358 PrivScope.addPrivate(
359 VD, [&CGF, &DRE]() { return CGF.EmitLValue(&DRE).getAddress(); });
Samuel Antaob68e2db2016-03-03 16:20:23 +0000360 }
361 (void)PrivScope.Privatize();
362 }
363
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000364 /// Lookup the captured field decl for a variable.
Samuel Antaob68e2db2016-03-03 16:20:23 +0000365 const FieldDecl *lookup(const VarDecl *VD) const override {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000366 if (const FieldDecl *FD = CGOpenMPInlinedRegionInfo::lookup(VD))
Samuel Antaob68e2db2016-03-03 16:20:23 +0000367 return FD;
368 return nullptr;
369 }
370
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000371 /// Emit the captured statement body.
Samuel Antaob68e2db2016-03-03 16:20:23 +0000372 void EmitBody(CodeGenFunction &CGF, const Stmt *S) override {
373 llvm_unreachable("No body for expressions");
374 }
375
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000376 /// Get a variable or parameter for storing global thread id
Samuel Antaob68e2db2016-03-03 16:20:23 +0000377 /// inside OpenMP construct.
378 const VarDecl *getThreadIDVariable() const override {
379 llvm_unreachable("No thread id for expressions");
380 }
381
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000382 /// Get the name of the capture helper.
Samuel Antaob68e2db2016-03-03 16:20:23 +0000383 StringRef getHelperName() const override {
384 llvm_unreachable("No helper name for expressions");
385 }
386
387 static bool classof(const CGCapturedStmtInfo *Info) { return false; }
388
389private:
390 /// Private scope to capture global variables.
391 CodeGenFunction::OMPPrivateScope PrivScope;
392};
393
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000394/// RAII for emitting code of OpenMP constructs.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000395class InlinedOpenMPRegionRAII {
396 CodeGenFunction &CGF;
Alexey Bataev4ba78a42016-04-27 07:56:03 +0000397 llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
398 FieldDecl *LambdaThisCaptureField = nullptr;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +0000399 const CodeGen::CGBlockInfo *BlockInfo = nullptr;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000400
401public:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000402 /// Constructs region for combined constructs.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000403 /// \param CodeGen Code generation sequence for combined directives. Includes
404 /// a list of functions used for code generation of implicitly inlined
405 /// regions.
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000406 InlinedOpenMPRegionRAII(CodeGenFunction &CGF, const RegionCodeGenTy &CodeGen,
Alexey Bataev25e5b442015-09-15 12:52:43 +0000407 OpenMPDirectiveKind Kind, bool HasCancel)
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000408 : CGF(CGF) {
409 // Start emission for the construct.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000410 CGF.CapturedStmtInfo = new CGOpenMPInlinedRegionInfo(
411 CGF.CapturedStmtInfo, CodeGen, Kind, HasCancel);
Alexey Bataev4ba78a42016-04-27 07:56:03 +0000412 std::swap(CGF.LambdaCaptureFields, LambdaCaptureFields);
413 LambdaThisCaptureField = CGF.LambdaThisCaptureField;
414 CGF.LambdaThisCaptureField = nullptr;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +0000415 BlockInfo = CGF.BlockInfo;
416 CGF.BlockInfo = nullptr;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000417 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000418
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000419 ~InlinedOpenMPRegionRAII() {
420 // Restore original CapturedStmtInfo only if we're done with code emission.
421 auto *OldCSI =
422 cast<CGOpenMPInlinedRegionInfo>(CGF.CapturedStmtInfo)->getOldCSI();
423 delete CGF.CapturedStmtInfo;
424 CGF.CapturedStmtInfo = OldCSI;
Alexey Bataev4ba78a42016-04-27 07:56:03 +0000425 std::swap(CGF.LambdaCaptureFields, LambdaCaptureFields);
426 CGF.LambdaThisCaptureField = LambdaThisCaptureField;
Alexey Bataeva8a9153a2017-12-29 18:07:07 +0000427 CGF.BlockInfo = BlockInfo;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000428 }
429};
430
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000431/// Values for bit flags used in the ident_t to describe the fields.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000432/// All enumeric elements are named and described in accordance with the code
James Y Knight5d71fc52019-01-29 16:37:27 +0000433/// from https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp.h
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000434enum OpenMPLocationFlags : unsigned {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000435 /// Use trampoline for internal microtask.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000436 OMP_IDENT_IMD = 0x01,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000437 /// Use c-style ident structure.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000438 OMP_IDENT_KMPC = 0x02,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000439 /// Atomic reduction option for kmpc_reduce.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000440 OMP_ATOMIC_REDUCE = 0x10,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000441 /// Explicit 'barrier' directive.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000442 OMP_IDENT_BARRIER_EXPL = 0x20,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000443 /// Implicit barrier in code.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000444 OMP_IDENT_BARRIER_IMPL = 0x40,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000445 /// Implicit barrier in 'for' directive.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000446 OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000447 /// Implicit barrier in 'sections' directive.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000448 OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000449 /// Implicit barrier in 'single' directive.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +0000450 OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140,
451 /// Call of __kmp_for_static_init for static loop.
452 OMP_IDENT_WORK_LOOP = 0x200,
453 /// Call of __kmp_for_static_init for sections.
454 OMP_IDENT_WORK_SECTIONS = 0x400,
455 /// Call of __kmp_for_static_init for distribute.
456 OMP_IDENT_WORK_DISTRIBUTE = 0x800,
457 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/OMP_IDENT_WORK_DISTRIBUTE)
Alexey Bataev50b3c952016-02-19 10:38:26 +0000458};
459
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000460/// Describes ident structure that describes a source location.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000461/// All descriptions are taken from
James Y Knight5d71fc52019-01-29 16:37:27 +0000462/// https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp.h
Alexey Bataev50b3c952016-02-19 10:38:26 +0000463/// Original structure:
464/// typedef struct ident {
465/// kmp_int32 reserved_1; /**< might be used in Fortran;
466/// see above */
467/// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
468/// KMP_IDENT_KMPC identifies this union
469/// member */
470/// kmp_int32 reserved_2; /**< not really used in Fortran any more;
471/// see above */
472///#if USE_ITT_BUILD
473/// /* but currently used for storing
474/// region-specific ITT */
475/// /* contextual information. */
476///#endif /* USE_ITT_BUILD */
477/// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
478/// C++ */
479/// char const *psource; /**< String describing the source location.
480/// The string is composed of semi-colon separated
481// fields which describe the source file,
482/// the function and a pair of line numbers that
483/// delimit the construct.
484/// */
485/// } ident_t;
486enum IdentFieldIndex {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000487 /// might be used in Fortran
Alexey Bataev50b3c952016-02-19 10:38:26 +0000488 IdentField_Reserved_1,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000489 /// OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000490 IdentField_Flags,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000491 /// Not really used in Fortran any more
Alexey Bataev50b3c952016-02-19 10:38:26 +0000492 IdentField_Reserved_2,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000493 /// Source[4] in Fortran, do not use for C++
Alexey Bataev50b3c952016-02-19 10:38:26 +0000494 IdentField_Reserved_3,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000495 /// String describing the source location. The string is composed of
Alexey Bataev50b3c952016-02-19 10:38:26 +0000496 /// semi-colon separated fields which describe the source file, the function
497 /// and a pair of line numbers that delimit the construct.
498 IdentField_PSource
499};
500
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000501/// Schedule types for 'omp for' loops (these enumerators are taken from
Alexey Bataev50b3c952016-02-19 10:38:26 +0000502/// the enum sched_type in kmp.h).
503enum OpenMPSchedType {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000504 /// Lower bound for default (unordered) versions.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000505 OMP_sch_lower = 32,
506 OMP_sch_static_chunked = 33,
507 OMP_sch_static = 34,
508 OMP_sch_dynamic_chunked = 35,
509 OMP_sch_guided_chunked = 36,
510 OMP_sch_runtime = 37,
511 OMP_sch_auto = 38,
Alexey Bataev6cff6242016-05-30 13:05:14 +0000512 /// static with chunk adjustment (e.g., simd)
Samuel Antao4c8035b2016-12-12 18:00:20 +0000513 OMP_sch_static_balanced_chunked = 45,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000514 /// Lower bound for 'ordered' versions.
Alexey Bataev50b3c952016-02-19 10:38:26 +0000515 OMP_ord_lower = 64,
516 OMP_ord_static_chunked = 65,
517 OMP_ord_static = 66,
518 OMP_ord_dynamic_chunked = 67,
519 OMP_ord_guided_chunked = 68,
520 OMP_ord_runtime = 69,
521 OMP_ord_auto = 70,
522 OMP_sch_default = OMP_sch_static,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000523 /// dist_schedule types
Carlo Bertollifc35ad22016-03-07 16:04:49 +0000524 OMP_dist_sch_static_chunked = 91,
525 OMP_dist_sch_static = 92,
Alexey Bataev9ebd7422016-05-10 09:57:36 +0000526 /// Support for OpenMP 4.5 monotonic and nonmonotonic schedule modifiers.
527 /// Set if the monotonic schedule modifier was present.
528 OMP_sch_modifier_monotonic = (1 << 29),
529 /// Set if the nonmonotonic schedule modifier was present.
530 OMP_sch_modifier_nonmonotonic = (1 << 30),
Alexey Bataev50b3c952016-02-19 10:38:26 +0000531};
532
533enum OpenMPRTLFunction {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000534 /// Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000535 /// kmpc_micro microtask, ...);
536 OMPRTL__kmpc_fork_call,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000537 /// Call to void *__kmpc_threadprivate_cached(ident_t *loc,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000538 /// kmp_int32 global_tid, void *data, size_t size, void ***cache);
539 OMPRTL__kmpc_threadprivate_cached,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000540 /// Call to void __kmpc_threadprivate_register( ident_t *,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000541 /// void *data, kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
542 OMPRTL__kmpc_threadprivate_register,
543 // Call to __kmpc_int32 kmpc_global_thread_num(ident_t *loc);
544 OMPRTL__kmpc_global_thread_num,
545 // Call to void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
546 // kmp_critical_name *crit);
547 OMPRTL__kmpc_critical,
548 // Call to void __kmpc_critical_with_hint(ident_t *loc, kmp_int32
549 // global_tid, kmp_critical_name *crit, uintptr_t hint);
550 OMPRTL__kmpc_critical_with_hint,
551 // Call to void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
552 // kmp_critical_name *crit);
553 OMPRTL__kmpc_end_critical,
554 // Call to kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32
555 // global_tid);
556 OMPRTL__kmpc_cancel_barrier,
557 // Call to void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
558 OMPRTL__kmpc_barrier,
559 // Call to void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
560 OMPRTL__kmpc_for_static_fini,
561 // Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
562 // global_tid);
563 OMPRTL__kmpc_serialized_parallel,
564 // Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
565 // global_tid);
566 OMPRTL__kmpc_end_serialized_parallel,
567 // Call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
568 // kmp_int32 num_threads);
569 OMPRTL__kmpc_push_num_threads,
570 // Call to void __kmpc_flush(ident_t *loc);
571 OMPRTL__kmpc_flush,
572 // Call to kmp_int32 __kmpc_master(ident_t *, kmp_int32 global_tid);
573 OMPRTL__kmpc_master,
574 // Call to void __kmpc_end_master(ident_t *, kmp_int32 global_tid);
575 OMPRTL__kmpc_end_master,
576 // Call to kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid,
577 // int end_part);
578 OMPRTL__kmpc_omp_taskyield,
579 // Call to kmp_int32 __kmpc_single(ident_t *, kmp_int32 global_tid);
580 OMPRTL__kmpc_single,
581 // Call to void __kmpc_end_single(ident_t *, kmp_int32 global_tid);
582 OMPRTL__kmpc_end_single,
583 // Call to kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
584 // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
585 // kmp_routine_entry_t *task_entry);
586 OMPRTL__kmpc_omp_task_alloc,
587 // Call to kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t *
588 // new_task);
589 OMPRTL__kmpc_omp_task,
590 // Call to void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid,
591 // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *),
592 // kmp_int32 didit);
593 OMPRTL__kmpc_copyprivate,
594 // Call to kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid,
595 // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void
596 // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck);
597 OMPRTL__kmpc_reduce,
598 // Call to kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32
599 // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data,
600 // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name
601 // *lck);
602 OMPRTL__kmpc_reduce_nowait,
603 // Call to void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
604 // kmp_critical_name *lck);
605 OMPRTL__kmpc_end_reduce,
606 // Call to void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
607 // kmp_critical_name *lck);
608 OMPRTL__kmpc_end_reduce_nowait,
609 // Call to void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid,
610 // kmp_task_t * new_task);
611 OMPRTL__kmpc_omp_task_begin_if0,
612 // Call to void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid,
613 // kmp_task_t * new_task);
614 OMPRTL__kmpc_omp_task_complete_if0,
615 // Call to void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid);
616 OMPRTL__kmpc_ordered,
617 // Call to void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid);
618 OMPRTL__kmpc_end_ordered,
619 // Call to kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32
620 // global_tid);
621 OMPRTL__kmpc_omp_taskwait,
622 // Call to void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid);
623 OMPRTL__kmpc_taskgroup,
624 // Call to void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid);
625 OMPRTL__kmpc_end_taskgroup,
626 // Call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid,
627 // int proc_bind);
628 OMPRTL__kmpc_push_proc_bind,
629 // Call to kmp_int32 __kmpc_omp_task_with_deps(ident_t *loc_ref, kmp_int32
630 // gtid, kmp_task_t * new_task, kmp_int32 ndeps, kmp_depend_info_t
631 // *dep_list, kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
632 OMPRTL__kmpc_omp_task_with_deps,
633 // Call to void __kmpc_omp_wait_deps(ident_t *loc_ref, kmp_int32
634 // gtid, kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32
635 // ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
636 OMPRTL__kmpc_omp_wait_deps,
637 // Call to kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32
638 // global_tid, kmp_int32 cncl_kind);
639 OMPRTL__kmpc_cancellationpoint,
640 // Call to kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid,
641 // kmp_int32 cncl_kind);
642 OMPRTL__kmpc_cancel,
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000643 // Call to void __kmpc_push_num_teams(ident_t *loc, kmp_int32 global_tid,
644 // kmp_int32 num_teams, kmp_int32 thread_limit);
645 OMPRTL__kmpc_push_num_teams,
Alexey Bataev7292c292016-04-25 12:22:29 +0000646 // Call to void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro
647 // microtask, ...);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +0000648 OMPRTL__kmpc_fork_teams,
Alexey Bataev7292c292016-04-25 12:22:29 +0000649 // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int
650 // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int
651 // sched, kmp_uint64 grainsize, void *task_dup);
652 OMPRTL__kmpc_taskloop,
Alexey Bataev8b427062016-05-25 12:36:08 +0000653 // Call to void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32
654 // num_dims, struct kmp_dim *dims);
655 OMPRTL__kmpc_doacross_init,
656 // Call to void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid);
657 OMPRTL__kmpc_doacross_fini,
658 // Call to void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64
659 // *vec);
660 OMPRTL__kmpc_doacross_post,
661 // Call to void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64
662 // *vec);
663 OMPRTL__kmpc_doacross_wait,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000664 // Call to void *__kmpc_task_reduction_init(int gtid, int num_data, void
665 // *data);
666 OMPRTL__kmpc_task_reduction_init,
667 // Call to void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void
668 // *d);
669 OMPRTL__kmpc_task_reduction_get_th_data,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000670
671 //
672 // Offloading related calls
673 //
Alexey Bataev7bb33532019-01-07 21:30:43 +0000674 // Call to void __kmpc_push_target_tripcount(int64_t device_id, kmp_uint64
675 // size);
676 OMPRTL__kmpc_push_target_tripcount,
George Rokos63bc9d62017-11-21 18:25:12 +0000677 // Call to int32_t __tgt_target(int64_t device_id, void *host_ptr, int32_t
678 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
Alexey Bataev50b3c952016-02-19 10:38:26 +0000679 // *arg_types);
680 OMPRTL__tgt_target,
Alexey Bataeva9f77c62017-12-13 21:04:20 +0000681 // Call to int32_t __tgt_target_nowait(int64_t device_id, void *host_ptr,
682 // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
683 // *arg_types);
684 OMPRTL__tgt_target_nowait,
George Rokos63bc9d62017-11-21 18:25:12 +0000685 // Call to int32_t __tgt_target_teams(int64_t device_id, void *host_ptr,
686 // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
687 // *arg_types, int32_t num_teams, int32_t thread_limit);
Samuel Antaob68e2db2016-03-03 16:20:23 +0000688 OMPRTL__tgt_target_teams,
Alexey Bataeva9f77c62017-12-13 21:04:20 +0000689 // Call to int32_t __tgt_target_teams_nowait(int64_t device_id, void
690 // *host_ptr, int32_t arg_num, void** args_base, void **args, size_t
691 // *arg_sizes, int64_t *arg_types, int32_t num_teams, int32_t thread_limit);
692 OMPRTL__tgt_target_teams_nowait,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000693 // Call to void __tgt_register_lib(__tgt_bin_desc *desc);
694 OMPRTL__tgt_register_lib,
695 // Call to void __tgt_unregister_lib(__tgt_bin_desc *desc);
696 OMPRTL__tgt_unregister_lib,
George Rokos63bc9d62017-11-21 18:25:12 +0000697 // Call to void __tgt_target_data_begin(int64_t device_id, int32_t arg_num,
698 // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types);
Samuel Antaodf158d52016-04-27 22:58:19 +0000699 OMPRTL__tgt_target_data_begin,
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +0000700 // Call to void __tgt_target_data_begin_nowait(int64_t device_id, int32_t
701 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
702 // *arg_types);
703 OMPRTL__tgt_target_data_begin_nowait,
George Rokos63bc9d62017-11-21 18:25:12 +0000704 // Call to void __tgt_target_data_end(int64_t device_id, int32_t arg_num,
705 // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types);
Samuel Antaodf158d52016-04-27 22:58:19 +0000706 OMPRTL__tgt_target_data_end,
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +0000707 // Call to void __tgt_target_data_end_nowait(int64_t device_id, int32_t
708 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
709 // *arg_types);
710 OMPRTL__tgt_target_data_end_nowait,
George Rokos63bc9d62017-11-21 18:25:12 +0000711 // Call to void __tgt_target_data_update(int64_t device_id, int32_t arg_num,
712 // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types);
Samuel Antao8d2d7302016-05-26 18:30:22 +0000713 OMPRTL__tgt_target_data_update,
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +0000714 // Call to void __tgt_target_data_update_nowait(int64_t device_id, int32_t
715 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
716 // *arg_types);
717 OMPRTL__tgt_target_data_update_nowait,
Alexey Bataev50b3c952016-02-19 10:38:26 +0000718};
719
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000720/// A basic class for pre|post-action for advanced codegen sequence for OpenMP
721/// region.
722class CleanupTy final : public EHScopeStack::Cleanup {
723 PrePostActionTy *Action;
724
725public:
726 explicit CleanupTy(PrePostActionTy *Action) : Action(Action) {}
727 void Emit(CodeGenFunction &CGF, Flags /*flags*/) override {
728 if (!CGF.HaveInsertPoint())
729 return;
730 Action->Exit(CGF);
731 }
732};
733
Hans Wennborg7eb54642015-09-10 17:07:54 +0000734} // anonymous namespace
Alexey Bataev18095712014-10-10 12:19:54 +0000735
Alexey Bataev14fa1c62016-03-29 05:34:15 +0000736void RegionCodeGenTy::operator()(CodeGenFunction &CGF) const {
737 CodeGenFunction::RunCleanupsScope Scope(CGF);
738 if (PrePostAction) {
739 CGF.EHStack.pushCleanup<CleanupTy>(NormalAndEHCleanup, PrePostAction);
740 Callback(CodeGen, CGF, *PrePostAction);
741 } else {
742 PrePostActionTy Action;
743 Callback(CodeGen, CGF, Action);
744 }
745}
746
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000747/// Check if the combiner is a call to UDR combiner and if it is so return the
748/// UDR decl used for reduction.
749static const OMPDeclareReductionDecl *
750getReductionInit(const Expr *ReductionOp) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000751 if (const auto *CE = dyn_cast<CallExpr>(ReductionOp))
752 if (const auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee()))
753 if (const auto *DRE =
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000754 dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts()))
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000755 if (const auto *DRD = dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl()))
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000756 return DRD;
757 return nullptr;
758}
759
760static void emitInitWithReductionInitializer(CodeGenFunction &CGF,
761 const OMPDeclareReductionDecl *DRD,
762 const Expr *InitOp,
763 Address Private, Address Original,
764 QualType Ty) {
765 if (DRD->getInitializer()) {
766 std::pair<llvm::Function *, llvm::Function *> Reduction =
767 CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000768 const auto *CE = cast<CallExpr>(InitOp);
769 const auto *OVE = cast<OpaqueValueExpr>(CE->getCallee());
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000770 const Expr *LHS = CE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
771 const Expr *RHS = CE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000772 const auto *LHSDRE =
773 cast<DeclRefExpr>(cast<UnaryOperator>(LHS)->getSubExpr());
774 const auto *RHSDRE =
775 cast<DeclRefExpr>(cast<UnaryOperator>(RHS)->getSubExpr());
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000776 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
777 PrivateScope.addPrivate(cast<VarDecl>(LHSDRE->getDecl()),
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000778 [=]() { return Private; });
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000779 PrivateScope.addPrivate(cast<VarDecl>(RHSDRE->getDecl()),
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000780 [=]() { return Original; });
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000781 (void)PrivateScope.Privatize();
782 RValue Func = RValue::get(Reduction.second);
783 CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func);
784 CGF.EmitIgnoredExpr(InitOp);
785 } else {
786 llvm::Constant *Init = CGF.CGM.EmitNullConstant(Ty);
Alexey Bataev18fa2322018-05-02 14:20:50 +0000787 std::string Name = CGF.CGM.getOpenMPRuntime().getName({"init"});
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000788 auto *GV = new llvm::GlobalVariable(
789 CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true,
Alexey Bataev18fa2322018-05-02 14:20:50 +0000790 llvm::GlobalValue::PrivateLinkage, Init, Name);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000791 LValue LV = CGF.MakeNaturalAlignAddrLValue(GV, Ty);
792 RValue InitRVal;
793 switch (CGF.getEvaluationKind(Ty)) {
794 case TEK_Scalar:
Alexey Bataeva9b9cc02018-01-23 18:12:38 +0000795 InitRVal = CGF.EmitLoadOfLValue(LV, DRD->getLocation());
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000796 break;
797 case TEK_Complex:
798 InitRVal =
Alexey Bataeva9b9cc02018-01-23 18:12:38 +0000799 RValue::getComplex(CGF.EmitLoadOfComplex(LV, DRD->getLocation()));
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000800 break;
801 case TEK_Aggregate:
802 InitRVal = RValue::getAggregate(LV.getAddress());
803 break;
804 }
Alexey Bataeva9b9cc02018-01-23 18:12:38 +0000805 OpaqueValueExpr OVE(DRD->getLocation(), Ty, VK_RValue);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000806 CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE, InitRVal);
807 CGF.EmitAnyExprToMem(&OVE, Private, Ty.getQualifiers(),
808 /*IsInitializer=*/false);
809 }
810}
811
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000812/// Emit initialization of arrays of complex types.
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000813/// \param DestAddr Address of the array.
814/// \param Type Type of array.
815/// \param Init Initial expression of array.
816/// \param SrcAddr Address of the original array.
817static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr,
Alexey Bataeva7b19152017-10-12 20:03:39 +0000818 QualType Type, bool EmitDeclareReductionInit,
819 const Expr *Init,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000820 const OMPDeclareReductionDecl *DRD,
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000821 Address SrcAddr = Address::invalid()) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000822 // Perform element-by-element initialization.
823 QualType ElementTy;
824
825 // Drill down to the base element type on both arrays.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000826 const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe();
827 llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000828 DestAddr =
829 CGF.Builder.CreateElementBitCast(DestAddr, DestAddr.getElementType());
830 if (DRD)
831 SrcAddr =
832 CGF.Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
833
834 llvm::Value *SrcBegin = nullptr;
835 if (DRD)
836 SrcBegin = SrcAddr.getPointer();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000837 llvm::Value *DestBegin = DestAddr.getPointer();
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000838 // Cast from pointer to array type to pointer to single element.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000839 llvm::Value *DestEnd = CGF.Builder.CreateGEP(DestBegin, NumElements);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000840 // The basic structure here is a while-do loop.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000841 llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arrayinit.body");
842 llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arrayinit.done");
843 llvm::Value *IsEmpty =
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000844 CGF.Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arrayinit.isempty");
845 CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
846
847 // Enter the loop body, making that address the current address.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000848 llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock();
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000849 CGF.EmitBlock(BodyBB);
850
851 CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy);
852
853 llvm::PHINode *SrcElementPHI = nullptr;
854 Address SrcElementCurrent = Address::invalid();
855 if (DRD) {
856 SrcElementPHI = CGF.Builder.CreatePHI(SrcBegin->getType(), 2,
857 "omp.arraycpy.srcElementPast");
858 SrcElementPHI->addIncoming(SrcBegin, EntryBB);
859 SrcElementCurrent =
860 Address(SrcElementPHI,
861 SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize));
862 }
863 llvm::PHINode *DestElementPHI = CGF.Builder.CreatePHI(
864 DestBegin->getType(), 2, "omp.arraycpy.destElementPast");
865 DestElementPHI->addIncoming(DestBegin, EntryBB);
866 Address DestElementCurrent =
867 Address(DestElementPHI,
868 DestAddr.getAlignment().alignmentOfArrayElement(ElementSize));
869
870 // Emit copy.
871 {
872 CodeGenFunction::RunCleanupsScope InitScope(CGF);
Alexey Bataeva7b19152017-10-12 20:03:39 +0000873 if (EmitDeclareReductionInit) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000874 emitInitWithReductionInitializer(CGF, DRD, Init, DestElementCurrent,
875 SrcElementCurrent, ElementTy);
876 } else
877 CGF.EmitAnyExprToMem(Init, DestElementCurrent, ElementTy.getQualifiers(),
878 /*IsInitializer=*/false);
879 }
880
881 if (DRD) {
882 // Shift the address forward by one element.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000883 llvm::Value *SrcElementNext = CGF.Builder.CreateConstGEP1_32(
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000884 SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
885 SrcElementPHI->addIncoming(SrcElementNext, CGF.Builder.GetInsertBlock());
886 }
887
888 // Shift the address forward by one element.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000889 llvm::Value *DestElementNext = CGF.Builder.CreateConstGEP1_32(
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000890 DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
891 // Check whether we've reached the end.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000892 llvm::Value *Done =
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000893 CGF.Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
894 CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB);
895 DestElementPHI->addIncoming(DestElementNext, CGF.Builder.GetInsertBlock());
896
897 // Done.
898 CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
899}
900
901LValue ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, const Expr *E) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +0000902 return CGF.EmitOMPSharedLValue(E);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000903}
904
905LValue ReductionCodeGen::emitSharedLValueUB(CodeGenFunction &CGF,
906 const Expr *E) {
907 if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(E))
908 return CGF.EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false);
909 return LValue();
910}
911
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000912void ReductionCodeGen::emitAggregateInitialization(
913 CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal,
914 const OMPDeclareReductionDecl *DRD) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000915 // Emit VarDecl with copy init for arrays.
916 // Get the address of the original variable captured in current
917 // captured region.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000918 const auto *PrivateVD =
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000919 cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
Alexey Bataeva7b19152017-10-12 20:03:39 +0000920 bool EmitDeclareReductionInit =
921 DRD && (DRD->getInitializer() || !PrivateVD->hasInit());
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000922 EmitOMPAggregateInit(CGF, PrivateAddr, PrivateVD->getType(),
Alexey Bataeva7b19152017-10-12 20:03:39 +0000923 EmitDeclareReductionInit,
924 EmitDeclareReductionInit ? ClausesData[N].ReductionOp
925 : PrivateVD->getInit(),
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000926 DRD, SharedLVal.getAddress());
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000927}
928
929ReductionCodeGen::ReductionCodeGen(ArrayRef<const Expr *> Shareds,
930 ArrayRef<const Expr *> Privates,
931 ArrayRef<const Expr *> ReductionOps) {
932 ClausesData.reserve(Shareds.size());
933 SharedAddresses.reserve(Shareds.size());
934 Sizes.reserve(Shareds.size());
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000935 BaseDecls.reserve(Shareds.size());
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000936 auto IPriv = Privates.begin();
937 auto IRed = ReductionOps.begin();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000938 for (const Expr *Ref : Shareds) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000939 ClausesData.emplace_back(Ref, *IPriv, *IRed);
940 std::advance(IPriv, 1);
941 std::advance(IRed, 1);
942 }
943}
944
945void ReductionCodeGen::emitSharedLValue(CodeGenFunction &CGF, unsigned N) {
946 assert(SharedAddresses.size() == N &&
947 "Number of generated lvalues must be exactly N.");
Jonas Hahnfeld4525c822017-10-23 19:01:35 +0000948 LValue First = emitSharedLValue(CGF, ClausesData[N].Ref);
949 LValue Second = emitSharedLValueUB(CGF, ClausesData[N].Ref);
950 SharedAddresses.emplace_back(First, Second);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000951}
952
953void ReductionCodeGen::emitAggregateType(CodeGenFunction &CGF, unsigned N) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000954 const auto *PrivateVD =
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000955 cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
956 QualType PrivateType = PrivateVD->getType();
957 bool AsArraySection = isa<OMPArraySectionExpr>(ClausesData[N].Ref);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +0000958 if (!PrivateType->isVariablyModifiedType()) {
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000959 Sizes.emplace_back(
960 CGF.getTypeSize(
961 SharedAddresses[N].first.getType().getNonReferenceType()),
962 nullptr);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000963 return;
964 }
965 llvm::Value *Size;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000966 llvm::Value *SizeInChars;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000967 auto *ElemType =
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000968 cast<llvm::PointerType>(SharedAddresses[N].first.getPointer()->getType())
969 ->getElementType();
970 auto *ElemSizeOf = llvm::ConstantExpr::getSizeOf(ElemType);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000971 if (AsArraySection) {
972 Size = CGF.Builder.CreatePtrDiff(SharedAddresses[N].second.getPointer(),
973 SharedAddresses[N].first.getPointer());
974 Size = CGF.Builder.CreateNUWAdd(
975 Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1));
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000976 SizeInChars = CGF.Builder.CreateNUWMul(Size, ElemSizeOf);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000977 } else {
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000978 SizeInChars = CGF.getTypeSize(
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000979 SharedAddresses[N].first.getType().getNonReferenceType());
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000980 Size = CGF.Builder.CreateExactUDiv(SizeInChars, ElemSizeOf);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000981 }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000982 Sizes.emplace_back(SizeInChars, Size);
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000983 CodeGenFunction::OpaqueValueMapping OpaqueMap(
984 CGF,
985 cast<OpaqueValueExpr>(
986 CGF.getContext().getAsVariableArrayType(PrivateType)->getSizeExpr()),
987 RValue::get(Size));
988 CGF.EmitVariablyModifiedType(PrivateType);
989}
990
991void ReductionCodeGen::emitAggregateType(CodeGenFunction &CGF, unsigned N,
992 llvm::Value *Size) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +0000993 const auto *PrivateVD =
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000994 cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
995 QualType PrivateType = PrivateVD->getType();
Jonas Hahnfeld4525c822017-10-23 19:01:35 +0000996 if (!PrivateType->isVariablyModifiedType()) {
Alexey Bataevbe5a8b42017-07-17 13:30:36 +0000997 assert(!Size && !Sizes[N].second &&
Jonas Hahnfeld4525c822017-10-23 19:01:35 +0000998 "Size should be nullptr for non-variably modified reduction "
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000999 "items.");
1000 return;
1001 }
1002 CodeGenFunction::OpaqueValueMapping OpaqueMap(
1003 CGF,
1004 cast<OpaqueValueExpr>(
1005 CGF.getContext().getAsVariableArrayType(PrivateType)->getSizeExpr()),
1006 RValue::get(Size));
1007 CGF.EmitVariablyModifiedType(PrivateType);
1008}
1009
1010void ReductionCodeGen::emitInitialization(
1011 CodeGenFunction &CGF, unsigned N, Address PrivateAddr, LValue SharedLVal,
1012 llvm::function_ref<bool(CodeGenFunction &)> DefaultInit) {
1013 assert(SharedAddresses.size() > N && "No variable was generated");
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001014 const auto *PrivateVD =
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001015 cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001016 const OMPDeclareReductionDecl *DRD =
1017 getReductionInit(ClausesData[N].ReductionOp);
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001018 QualType PrivateType = PrivateVD->getType();
1019 PrivateAddr = CGF.Builder.CreateElementBitCast(
1020 PrivateAddr, CGF.ConvertTypeForMem(PrivateType));
1021 QualType SharedType = SharedAddresses[N].first.getType();
1022 SharedLVal = CGF.MakeAddrLValue(
1023 CGF.Builder.CreateElementBitCast(SharedLVal.getAddress(),
1024 CGF.ConvertTypeForMem(SharedType)),
Ivan A. Kosarevf5f20462017-10-12 11:29:46 +00001025 SharedType, SharedAddresses[N].first.getBaseInfo(),
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +00001026 CGF.CGM.getTBAAInfoForSubobject(SharedAddresses[N].first, SharedType));
Jonas Hahnfeld4525c822017-10-23 19:01:35 +00001027 if (CGF.getContext().getAsArrayType(PrivateVD->getType())) {
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001028 emitAggregateInitialization(CGF, N, PrivateAddr, SharedLVal, DRD);
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001029 } else if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) {
1030 emitInitWithReductionInitializer(CGF, DRD, ClausesData[N].ReductionOp,
1031 PrivateAddr, SharedLVal.getAddress(),
1032 SharedLVal.getType());
1033 } else if (!DefaultInit(CGF) && PrivateVD->hasInit() &&
1034 !CGF.isTrivialInitializer(PrivateVD->getInit())) {
1035 CGF.EmitAnyExprToMem(PrivateVD->getInit(), PrivateAddr,
1036 PrivateVD->getType().getQualifiers(),
1037 /*IsInitializer=*/false);
1038 }
1039}
1040
1041bool ReductionCodeGen::needCleanups(unsigned N) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001042 const auto *PrivateVD =
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001043 cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
1044 QualType PrivateType = PrivateVD->getType();
1045 QualType::DestructionKind DTorKind = PrivateType.isDestructedType();
1046 return DTorKind != QualType::DK_none;
1047}
1048
1049void ReductionCodeGen::emitCleanups(CodeGenFunction &CGF, unsigned N,
1050 Address PrivateAddr) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001051 const auto *PrivateVD =
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001052 cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Private)->getDecl());
1053 QualType PrivateType = PrivateVD->getType();
1054 QualType::DestructionKind DTorKind = PrivateType.isDestructedType();
1055 if (needCleanups(N)) {
1056 PrivateAddr = CGF.Builder.CreateElementBitCast(
1057 PrivateAddr, CGF.ConvertTypeForMem(PrivateType));
1058 CGF.pushDestroy(DTorKind, PrivateAddr, PrivateType);
1059 }
1060}
1061
1062static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
1063 LValue BaseLV) {
1064 BaseTy = BaseTy.getNonReferenceType();
1065 while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) &&
1066 !CGF.getContext().hasSameType(BaseTy, ElTy)) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001067 if (const auto *PtrTy = BaseTy->getAs<PointerType>()) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001068 BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001069 } else {
Ivan A. Kosarev9f9d1572017-10-30 11:49:31 +00001070 LValue RefLVal = CGF.MakeAddrLValue(BaseLV.getAddress(), BaseTy);
1071 BaseLV = CGF.EmitLoadOfReferenceLValue(RefLVal);
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001072 }
1073 BaseTy = BaseTy->getPointeeType();
1074 }
1075 return CGF.MakeAddrLValue(
1076 CGF.Builder.CreateElementBitCast(BaseLV.getAddress(),
1077 CGF.ConvertTypeForMem(ElTy)),
Ivan A. Kosarevf5f20462017-10-12 11:29:46 +00001078 BaseLV.getType(), BaseLV.getBaseInfo(),
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +00001079 CGF.CGM.getTBAAInfoForSubobject(BaseLV, BaseLV.getType()));
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001080}
1081
1082static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
1083 llvm::Type *BaseLVType, CharUnits BaseLVAlignment,
1084 llvm::Value *Addr) {
1085 Address Tmp = Address::invalid();
1086 Address TopTmp = Address::invalid();
1087 Address MostTopTmp = Address::invalid();
1088 BaseTy = BaseTy.getNonReferenceType();
1089 while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) &&
1090 !CGF.getContext().hasSameType(BaseTy, ElTy)) {
1091 Tmp = CGF.CreateMemTemp(BaseTy);
1092 if (TopTmp.isValid())
1093 CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp);
1094 else
1095 MostTopTmp = Tmp;
1096 TopTmp = Tmp;
1097 BaseTy = BaseTy->getPointeeType();
1098 }
1099 llvm::Type *Ty = BaseLVType;
1100 if (Tmp.isValid())
1101 Ty = Tmp.getElementType();
1102 Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty);
1103 if (Tmp.isValid()) {
1104 CGF.Builder.CreateStore(Addr, Tmp);
1105 return MostTopTmp;
1106 }
1107 return Address(Addr, BaseLVAlignment);
1108}
1109
Alexey Bataev1c44e152018-03-06 18:59:43 +00001110static const VarDecl *getBaseDecl(const Expr *Ref, const DeclRefExpr *&DE) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001111 const VarDecl *OrigVD = nullptr;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001112 if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(Ref)) {
1113 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
1114 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001115 Base = TempOASE->getBase()->IgnoreParenImpCasts();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001116 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001117 Base = TempASE->getBase()->IgnoreParenImpCasts();
1118 DE = cast<DeclRefExpr>(Base);
1119 OrigVD = cast<VarDecl>(DE->getDecl());
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001120 } else if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Ref)) {
1121 const Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
1122 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001123 Base = TempASE->getBase()->IgnoreParenImpCasts();
1124 DE = cast<DeclRefExpr>(Base);
1125 OrigVD = cast<VarDecl>(DE->getDecl());
1126 }
Alexey Bataev1c44e152018-03-06 18:59:43 +00001127 return OrigVD;
1128}
1129
1130Address ReductionCodeGen::adjustPrivateAddress(CodeGenFunction &CGF, unsigned N,
1131 Address PrivateAddr) {
1132 const DeclRefExpr *DE;
1133 if (const VarDecl *OrigVD = ::getBaseDecl(ClausesData[N].Ref, DE)) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001134 BaseDecls.emplace_back(OrigVD);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001135 LValue OriginalBaseLValue = CGF.EmitLValue(DE);
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001136 LValue BaseLValue =
1137 loadToBegin(CGF, OrigVD->getType(), SharedAddresses[N].first.getType(),
1138 OriginalBaseLValue);
1139 llvm::Value *Adjustment = CGF.Builder.CreatePtrDiff(
1140 BaseLValue.getPointer(), SharedAddresses[N].first.getPointer());
Jonas Hahnfeld273d2612017-12-06 19:15:28 +00001141 llvm::Value *PrivatePointer =
1142 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
1143 PrivateAddr.getPointer(),
1144 SharedAddresses[N].first.getAddress().getType());
1145 llvm::Value *Ptr = CGF.Builder.CreateGEP(PrivatePointer, Adjustment);
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001146 return castToBase(CGF, OrigVD->getType(),
1147 SharedAddresses[N].first.getType(),
Jonas Hahnfeld273d2612017-12-06 19:15:28 +00001148 OriginalBaseLValue.getAddress().getType(),
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001149 OriginalBaseLValue.getAlignment(), Ptr);
1150 }
1151 BaseDecls.emplace_back(
1152 cast<VarDecl>(cast<DeclRefExpr>(ClausesData[N].Ref)->getDecl()));
1153 return PrivateAddr;
1154}
1155
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001156bool ReductionCodeGen::usesReductionInitializer(unsigned N) const {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001157 const OMPDeclareReductionDecl *DRD =
1158 getReductionInit(ClausesData[N].ReductionOp);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00001159 return DRD && DRD->getInitializer();
1160}
1161
Alexey Bataev18095712014-10-10 12:19:54 +00001162LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) {
Alexey Bataev31300ed2016-02-04 11:27:03 +00001163 return CGF.EmitLoadOfPointerLValue(
1164 CGF.GetAddrOfLocalVar(getThreadIDVariable()),
1165 getThreadIDVariable()->getType()->castAs<PointerType>());
Alexey Bataev18095712014-10-10 12:19:54 +00001166}
1167
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001168void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, const Stmt * /*S*/) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001169 if (!CGF.HaveInsertPoint())
1170 return;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001171 // 1.2.2 OpenMP Language Terminology
1172 // Structured block - An executable statement with a single entry at the
1173 // top and a single exit at the bottom.
1174 // The point of exit cannot be a branch out of the structured block.
1175 // longjmp() and throw() must not violate the entry/exit criteria.
1176 CGF.EHStack.pushTerminate();
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001177 CodeGen(CGF);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001178 CGF.EHStack.popTerminate();
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001179}
1180
Alexey Bataev62b63b12015-03-10 07:28:44 +00001181LValue CGOpenMPTaskOutlinedRegionInfo::getThreadIDVariableLValue(
1182 CodeGenFunction &CGF) {
Alexey Bataev2377fe92015-09-10 08:12:02 +00001183 return CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(getThreadIDVariable()),
1184 getThreadIDVariable()->getType(),
Ivan A. Kosarev5f8c0ca2017-10-10 09:39:32 +00001185 AlignmentSource::Decl);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001186}
1187
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001188static FieldDecl *addFieldToRecordDecl(ASTContext &C, DeclContext *DC,
1189 QualType FieldTy) {
1190 auto *Field = FieldDecl::Create(
1191 C, DC, SourceLocation(), SourceLocation(), /*Id=*/nullptr, FieldTy,
1192 C.getTrivialTypeSourceInfo(FieldTy, SourceLocation()),
1193 /*BW=*/nullptr, /*Mutable=*/false, /*InitStyle=*/ICIS_NoInit);
1194 Field->setAccess(AS_public);
1195 DC->addDecl(Field);
1196 return Field;
1197}
1198
Alexey Bataev18fa2322018-05-02 14:20:50 +00001199CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM, StringRef FirstSeparator,
1200 StringRef Separator)
1201 : CGM(CGM), FirstSeparator(FirstSeparator), Separator(Separator),
1202 OffloadEntriesInfoManager(CGM) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001203 ASTContext &C = CGM.getContext();
1204 RecordDecl *RD = C.buildImplicitRecord("ident_t");
1205 QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1206 RD->startDefinition();
1207 // reserved_1
1208 addFieldToRecordDecl(C, RD, KmpInt32Ty);
1209 // flags
1210 addFieldToRecordDecl(C, RD, KmpInt32Ty);
1211 // reserved_2
1212 addFieldToRecordDecl(C, RD, KmpInt32Ty);
1213 // reserved_3
1214 addFieldToRecordDecl(C, RD, KmpInt32Ty);
1215 // psource
1216 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
1217 RD->completeDefinition();
1218 IdentQTy = C.getRecordType(RD);
1219 IdentTy = CGM.getTypes().ConvertRecordDeclType(RD);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001220 KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8);
Samuel Antaoee8fb302016-01-06 13:42:12 +00001221
1222 loadOffloadInfoMetadata();
Alexey Bataev9959db52014-05-06 10:08:46 +00001223}
1224
Alexey Bataev91797552015-03-18 04:13:55 +00001225void CGOpenMPRuntime::clear() {
1226 InternalVars.clear();
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00001227 // Clean non-target variable declarations possibly used only in debug info.
1228 for (const auto &Data : EmittedNonTargetVariables) {
1229 if (!Data.getValue().pointsToAliveValue())
1230 continue;
1231 auto *GV = dyn_cast<llvm::GlobalVariable>(Data.getValue());
1232 if (!GV)
1233 continue;
1234 if (!GV->isDeclaration() || GV->getNumUses() > 0)
1235 continue;
1236 GV->eraseFromParent();
1237 }
Alexey Bataev91797552015-03-18 04:13:55 +00001238}
1239
Alexey Bataev18fa2322018-05-02 14:20:50 +00001240std::string CGOpenMPRuntime::getName(ArrayRef<StringRef> Parts) const {
1241 SmallString<128> Buffer;
1242 llvm::raw_svector_ostream OS(Buffer);
1243 StringRef Sep = FirstSeparator;
1244 for (StringRef Part : Parts) {
1245 OS << Sep << Part;
1246 Sep = Separator;
1247 }
1248 return OS.str();
1249}
1250
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001251static llvm::Function *
1252emitCombinerOrInitializer(CodeGenModule &CGM, QualType Ty,
1253 const Expr *CombinerInitializer, const VarDecl *In,
1254 const VarDecl *Out, bool IsCombiner) {
1255 // void .omp_combiner.(Ty *in, Ty *out);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001256 ASTContext &C = CGM.getContext();
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001257 QualType PtrTy = C.getPointerType(Ty).withRestrict();
1258 FunctionArgList Args;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001259 ImplicitParamDecl OmpOutParm(C, /*DC=*/nullptr, Out->getLocation(),
Alexey Bataev56223232017-06-09 13:40:18 +00001260 /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001261 ImplicitParamDecl OmpInParm(C, /*DC=*/nullptr, In->getLocation(),
Alexey Bataev56223232017-06-09 13:40:18 +00001262 /*Id=*/nullptr, PtrTy, ImplicitParamDecl::Other);
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001263 Args.push_back(&OmpOutParm);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001264 Args.push_back(&OmpInParm);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001265 const CGFunctionInfo &FnInfo =
John McCallc56a8b32016-03-11 04:30:31 +00001266 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001267 llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
Alexey Bataev18fa2322018-05-02 14:20:50 +00001268 std::string Name = CGM.getOpenMPRuntime().getName(
1269 {IsCombiner ? "omp_combiner" : "omp_initializer", ""});
1270 auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage,
1271 Name, &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00001272 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo);
Chandler Carruthfcd33142016-12-23 01:24:49 +00001273 Fn->removeFnAttr(llvm::Attribute::NoInline);
Mehdi Amini6aa9e9b2017-05-29 05:38:20 +00001274 Fn->removeFnAttr(llvm::Attribute::OptimizeNone);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001275 Fn->addFnAttr(llvm::Attribute::AlwaysInline);
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001276 CodeGenFunction CGF(CGM);
1277 // Map "T omp_in;" variable to "*omp_in_parm" value in all expressions.
1278 // Map "T omp_out;" variable to "*omp_out_parm" value in all expressions.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00001279 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, In->getLocation(),
1280 Out->getLocation());
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001281 CodeGenFunction::OMPPrivateScope Scope(CGF);
1282 Address AddrIn = CGF.GetAddrOfLocalVar(&OmpInParm);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001283 Scope.addPrivate(In, [&CGF, AddrIn, PtrTy]() {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001284 return CGF.EmitLoadOfPointerLValue(AddrIn, PtrTy->castAs<PointerType>())
1285 .getAddress();
1286 });
1287 Address AddrOut = CGF.GetAddrOfLocalVar(&OmpOutParm);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001288 Scope.addPrivate(Out, [&CGF, AddrOut, PtrTy]() {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001289 return CGF.EmitLoadOfPointerLValue(AddrOut, PtrTy->castAs<PointerType>())
1290 .getAddress();
1291 });
1292 (void)Scope.Privatize();
Alexey Bataev070f43a2017-09-06 14:49:58 +00001293 if (!IsCombiner && Out->hasInit() &&
1294 !CGF.isTrivialInitializer(Out->getInit())) {
1295 CGF.EmitAnyExprToMem(Out->getInit(), CGF.GetAddrOfLocalVar(Out),
1296 Out->getType().getQualifiers(),
1297 /*IsInitializer=*/true);
1298 }
1299 if (CombinerInitializer)
1300 CGF.EmitIgnoredExpr(CombinerInitializer);
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001301 Scope.ForceCleanup();
1302 CGF.FinishFunction();
1303 return Fn;
1304}
1305
1306void CGOpenMPRuntime::emitUserDefinedReduction(
1307 CodeGenFunction *CGF, const OMPDeclareReductionDecl *D) {
1308 if (UDRMap.count(D) > 0)
1309 return;
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001310 llvm::Function *Combiner = emitCombinerOrInitializer(
Alexey Bataeve6aa4692018-09-13 16:54:05 +00001311 CGM, D->getType(), D->getCombiner(),
1312 cast<VarDecl>(cast<DeclRefExpr>(D->getCombinerIn())->getDecl()),
1313 cast<VarDecl>(cast<DeclRefExpr>(D->getCombinerOut())->getDecl()),
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001314 /*IsCombiner=*/true);
1315 llvm::Function *Initializer = nullptr;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001316 if (const Expr *Init = D->getInitializer()) {
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001317 Initializer = emitCombinerOrInitializer(
Alexey Bataev070f43a2017-09-06 14:49:58 +00001318 CGM, D->getType(),
1319 D->getInitializerKind() == OMPDeclareReductionDecl::CallInit ? Init
1320 : nullptr,
Alexey Bataeve6aa4692018-09-13 16:54:05 +00001321 cast<VarDecl>(cast<DeclRefExpr>(D->getInitOrig())->getDecl()),
1322 cast<VarDecl>(cast<DeclRefExpr>(D->getInitPriv())->getDecl()),
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001323 /*IsCombiner=*/false);
1324 }
Alexey Bataev43a919f2018-04-13 17:48:43 +00001325 UDRMap.try_emplace(D, Combiner, Initializer);
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001326 if (CGF) {
1327 auto &Decls = FunctionUDRMap.FindAndConstruct(CGF->CurFn);
1328 Decls.second.push_back(D);
1329 }
1330}
1331
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001332std::pair<llvm::Function *, llvm::Function *>
1333CGOpenMPRuntime::getUserDefinedReduction(const OMPDeclareReductionDecl *D) {
1334 auto I = UDRMap.find(D);
1335 if (I != UDRMap.end())
1336 return I->second;
1337 emitUserDefinedReduction(/*CGF=*/nullptr, D);
1338 return UDRMap.lookup(D);
1339}
1340
James Y Knight9871db02019-02-05 16:42:33 +00001341static llvm::Function *emitParallelOrTeamsOutlinedFunction(
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00001342 CodeGenModule &CGM, const OMPExecutableDirective &D, const CapturedStmt *CS,
1343 const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind,
1344 const StringRef OutlinedHelperName, const RegionCodeGenTy &CodeGen) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001345 assert(ThreadIDVar->getType()->isPointerType() &&
1346 "thread id variable must be of type kmp_int32 *");
Alexey Bataev18095712014-10-10 12:19:54 +00001347 CodeGenFunction CGF(CGM, true);
Alexey Bataev25e5b442015-09-15 12:52:43 +00001348 bool HasCancel = false;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001349 if (const auto *OPD = dyn_cast<OMPParallelDirective>(&D))
Alexey Bataev25e5b442015-09-15 12:52:43 +00001350 HasCancel = OPD->hasCancel();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001351 else if (const auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&D))
Alexey Bataev25e5b442015-09-15 12:52:43 +00001352 HasCancel = OPSD->hasCancel();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001353 else if (const auto *OPFD = dyn_cast<OMPParallelForDirective>(&D))
Alexey Bataev25e5b442015-09-15 12:52:43 +00001354 HasCancel = OPFD->hasCancel();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001355 else if (const auto *OPFD = dyn_cast<OMPTargetParallelForDirective>(&D))
Alexey Bataev2139ed62017-11-16 18:20:21 +00001356 HasCancel = OPFD->hasCancel();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001357 else if (const auto *OPFD = dyn_cast<OMPDistributeParallelForDirective>(&D))
Alexey Bataev10a54312017-11-27 16:54:08 +00001358 HasCancel = OPFD->hasCancel();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001359 else if (const auto *OPFD =
1360 dyn_cast<OMPTeamsDistributeParallelForDirective>(&D))
Alexey Bataev10a54312017-11-27 16:54:08 +00001361 HasCancel = OPFD->hasCancel();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001362 else if (const auto *OPFD =
Alexey Bataev10a54312017-11-27 16:54:08 +00001363 dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&D))
1364 HasCancel = OPFD->hasCancel();
Alexey Bataev25e5b442015-09-15 12:52:43 +00001365 CGOpenMPOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, InnermostKind,
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00001366 HasCancel, OutlinedHelperName);
Alexey Bataevd157d472015-06-24 03:35:38 +00001367 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001368 return CGF.GenerateOpenMPCapturedStmtFunction(*CS);
Alexey Bataev18095712014-10-10 12:19:54 +00001369}
1370
James Y Knight9871db02019-02-05 16:42:33 +00001371llvm::Function *CGOpenMPRuntime::emitParallelOutlinedFunction(
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00001372 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1373 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
1374 const CapturedStmt *CS = D.getCapturedStmt(OMPD_parallel);
1375 return emitParallelOrTeamsOutlinedFunction(
1376 CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen);
1377}
1378
James Y Knight9871db02019-02-05 16:42:33 +00001379llvm::Function *CGOpenMPRuntime::emitTeamsOutlinedFunction(
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00001380 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1381 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
1382 const CapturedStmt *CS = D.getCapturedStmt(OMPD_teams);
1383 return emitParallelOrTeamsOutlinedFunction(
1384 CGM, D, CS, ThreadIDVar, InnermostKind, getOutlinedHelperName(), CodeGen);
1385}
1386
James Y Knight9871db02019-02-05 16:42:33 +00001387llvm::Function *CGOpenMPRuntime::emitTaskOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001388 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
Alexey Bataev48591dd2016-04-20 04:01:36 +00001389 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
1390 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1391 bool Tied, unsigned &NumberOfParts) {
1392 auto &&UntiedCodeGen = [this, &D, TaskTVar](CodeGenFunction &CGF,
1393 PrePostActionTy &) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001394 llvm::Value *ThreadID = getThreadID(CGF, D.getBeginLoc());
1395 llvm::Value *UpLoc = emitUpdateLocation(CGF, D.getBeginLoc());
Alexey Bataev48591dd2016-04-20 04:01:36 +00001396 llvm::Value *TaskArgs[] = {
1397 UpLoc, ThreadID,
1398 CGF.EmitLoadOfPointerLValue(CGF.GetAddrOfLocalVar(TaskTVar),
1399 TaskTVar->getType()->castAs<PointerType>())
1400 .getPointer()};
1401 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), TaskArgs);
1402 };
1403 CGOpenMPTaskOutlinedRegionInfo::UntiedTaskActionTy Action(Tied, PartIDVar,
1404 UntiedCodeGen);
1405 CodeGen.setAction(Action);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001406 assert(!ThreadIDVar->getType()->isPointerType() &&
1407 "thread id variable must be of type kmp_int32 for tasks");
Alexey Bataev475a7442018-01-12 19:39:11 +00001408 const OpenMPDirectiveKind Region =
1409 isOpenMPTaskLoopDirective(D.getDirectiveKind()) ? OMPD_taskloop
1410 : OMPD_task;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001411 const CapturedStmt *CS = D.getCapturedStmt(Region);
1412 const auto *TD = dyn_cast<OMPTaskDirective>(&D);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001413 CodeGenFunction CGF(CGM, true);
Alexey Bataev7292c292016-04-25 12:22:29 +00001414 CGOpenMPTaskOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen,
1415 InnermostKind,
1416 TD ? TD->hasCancel() : false, Action);
Alexey Bataevd157d472015-06-24 03:35:38 +00001417 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
James Y Knight9871db02019-02-05 16:42:33 +00001418 llvm::Function *Res = CGF.GenerateCapturedStmtFunction(*CS);
Alexey Bataev48591dd2016-04-20 04:01:36 +00001419 if (!Tied)
1420 NumberOfParts = Action.getNumberOfParts();
1421 return Res;
Alexey Bataev62b63b12015-03-10 07:28:44 +00001422}
1423
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001424static void buildStructValue(ConstantStructBuilder &Fields, CodeGenModule &CGM,
1425 const RecordDecl *RD, const CGRecordLayout &RL,
1426 ArrayRef<llvm::Constant *> Data) {
1427 llvm::StructType *StructTy = RL.getLLVMType();
1428 unsigned PrevIdx = 0;
1429 ConstantInitBuilder CIBuilder(CGM);
1430 auto DI = Data.begin();
1431 for (const FieldDecl *FD : RD->fields()) {
1432 unsigned Idx = RL.getLLVMFieldNo(FD);
1433 // Fill the alignment.
1434 for (unsigned I = PrevIdx; I < Idx; ++I)
1435 Fields.add(llvm::Constant::getNullValue(StructTy->getElementType(I)));
1436 PrevIdx = Idx + 1;
1437 Fields.add(*DI);
1438 ++DI;
1439 }
1440}
1441
1442template <class... As>
1443static llvm::GlobalVariable *
Mike Ricee1ca7b62018-08-29 15:45:11 +00001444createGlobalStruct(CodeGenModule &CGM, QualType Ty, bool IsConstant,
1445 ArrayRef<llvm::Constant *> Data, const Twine &Name,
1446 As &&... Args) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001447 const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl());
1448 const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD);
1449 ConstantInitBuilder CIBuilder(CGM);
1450 ConstantStructBuilder Fields = CIBuilder.beginStruct(RL.getLLVMType());
1451 buildStructValue(Fields, CGM, RD, RL, Data);
1452 return Fields.finishAndCreateGlobal(
Mike Ricee1ca7b62018-08-29 15:45:11 +00001453 Name, CGM.getContext().getAlignOfGlobalVarInChars(Ty), IsConstant,
1454 std::forward<As>(Args)...);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001455}
1456
1457template <typename T>
Benjamin Kramer651d0bf2018-05-15 21:26:47 +00001458static void
1459createConstantGlobalStructAndAddToParent(CodeGenModule &CGM, QualType Ty,
1460 ArrayRef<llvm::Constant *> Data,
1461 T &Parent) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001462 const auto *RD = cast<RecordDecl>(Ty->getAsTagDecl());
1463 const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(RD);
1464 ConstantStructBuilder Fields = Parent.beginStruct(RL.getLLVMType());
1465 buildStructValue(Fields, CGM, RD, RL, Data);
1466 Fields.finishAndAddTo(Parent);
1467}
1468
Alexey Bataev50b3c952016-02-19 10:38:26 +00001469Address CGOpenMPRuntime::getOrCreateDefaultLocation(unsigned Flags) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001470 CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy);
Alexey Bataevceeaa482018-11-21 21:04:34 +00001471 unsigned Reserved2Flags = getDefaultLocationReserved2Flags();
1472 FlagsTy FlagsKey(Flags, Reserved2Flags);
1473 llvm::Value *Entry = OpenMPDefaultLocMap.lookup(FlagsKey);
Alexey Bataev9959db52014-05-06 10:08:46 +00001474 if (!Entry) {
1475 if (!DefaultOpenMPPSource) {
1476 // Initialize default location for psource field of ident_t structure of
1477 // all ident_t objects. Format is ";file;function;line;column;;".
1478 // Taken from
James Y Knight5d71fc52019-01-29 16:37:27 +00001479 // https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp_str.cpp
Alexey Bataev9959db52014-05-06 10:08:46 +00001480 DefaultOpenMPPSource =
John McCall7f416cc2015-09-08 08:05:57 +00001481 CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;").getPointer();
Alexey Bataev9959db52014-05-06 10:08:46 +00001482 DefaultOpenMPPSource =
1483 llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy);
1484 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001485
Alexey Bataevceeaa482018-11-21 21:04:34 +00001486 llvm::Constant *Data[] = {
1487 llvm::ConstantInt::getNullValue(CGM.Int32Ty),
1488 llvm::ConstantInt::get(CGM.Int32Ty, Flags),
1489 llvm::ConstantInt::get(CGM.Int32Ty, Reserved2Flags),
1490 llvm::ConstantInt::getNullValue(CGM.Int32Ty), DefaultOpenMPPSource};
Mike Ricee1ca7b62018-08-29 15:45:11 +00001491 llvm::GlobalValue *DefaultOpenMPLocation =
Alexey Bataevceeaa482018-11-21 21:04:34 +00001492 createGlobalStruct(CGM, IdentQTy, isDefaultLocationConstant(), Data, "",
Mike Ricee1ca7b62018-08-29 15:45:11 +00001493 llvm::GlobalValue::PrivateLinkage);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001494 DefaultOpenMPLocation->setUnnamedAddr(
1495 llvm::GlobalValue::UnnamedAddr::Global);
John McCall6c9f1fdb2016-11-19 08:17:24 +00001496
Alexey Bataevceeaa482018-11-21 21:04:34 +00001497 OpenMPDefaultLocMap[FlagsKey] = Entry = DefaultOpenMPLocation;
Alexey Bataev9959db52014-05-06 10:08:46 +00001498 }
John McCall7f416cc2015-09-08 08:05:57 +00001499 return Address(Entry, Align);
Alexey Bataev9959db52014-05-06 10:08:46 +00001500}
1501
Alexey Bataevfd006c42018-10-05 15:08:53 +00001502void CGOpenMPRuntime::setLocThreadIdInsertPt(CodeGenFunction &CGF,
1503 bool AtCurrentPoint) {
1504 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
1505 assert(!Elem.second.ServiceInsertPt && "Insert point is set already.");
1506
1507 llvm::Value *Undef = llvm::UndefValue::get(CGF.Int32Ty);
1508 if (AtCurrentPoint) {
1509 Elem.second.ServiceInsertPt = new llvm::BitCastInst(
1510 Undef, CGF.Int32Ty, "svcpt", CGF.Builder.GetInsertBlock());
1511 } else {
1512 Elem.second.ServiceInsertPt =
1513 new llvm::BitCastInst(Undef, CGF.Int32Ty, "svcpt");
1514 Elem.second.ServiceInsertPt->insertAfter(CGF.AllocaInsertPt);
1515 }
1516}
1517
1518void CGOpenMPRuntime::clearLocThreadIdInsertPt(CodeGenFunction &CGF) {
1519 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
1520 if (Elem.second.ServiceInsertPt) {
1521 llvm::Instruction *Ptr = Elem.second.ServiceInsertPt;
1522 Elem.second.ServiceInsertPt = nullptr;
1523 Ptr->eraseFromParent();
1524 }
1525}
1526
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001527llvm::Value *CGOpenMPRuntime::emitUpdateLocation(CodeGenFunction &CGF,
1528 SourceLocation Loc,
Alexey Bataev50b3c952016-02-19 10:38:26 +00001529 unsigned Flags) {
1530 Flags |= OMP_IDENT_KMPC;
Alexey Bataev9959db52014-05-06 10:08:46 +00001531 // If no debug info is generated - return global default location.
Benjamin Kramer8c305922016-02-02 11:06:51 +00001532 if (CGM.getCodeGenOpts().getDebugInfo() == codegenoptions::NoDebugInfo ||
Alexey Bataev9959db52014-05-06 10:08:46 +00001533 Loc.isInvalid())
John McCall7f416cc2015-09-08 08:05:57 +00001534 return getOrCreateDefaultLocation(Flags).getPointer();
Alexey Bataev9959db52014-05-06 10:08:46 +00001535
1536 assert(CGF.CurFn && "No function in current CodeGenFunction.");
1537
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001538 CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy);
John McCall7f416cc2015-09-08 08:05:57 +00001539 Address LocValue = Address::invalid();
Alexey Bataev1e4b7132014-12-03 12:11:24 +00001540 auto I = OpenMPLocThreadIDMap.find(CGF.CurFn);
1541 if (I != OpenMPLocThreadIDMap.end())
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001542 LocValue = Address(I->second.DebugLoc, Align);
John McCall7f416cc2015-09-08 08:05:57 +00001543
Alexander Musmanc6388682014-12-15 07:07:06 +00001544 // OpenMPLocThreadIDMap may have null DebugLoc and non-null ThreadID, if
1545 // GetOpenMPThreadID was called before this routine.
John McCall7f416cc2015-09-08 08:05:57 +00001546 if (!LocValue.isValid()) {
Alexey Bataev15007ba2014-05-07 06:18:01 +00001547 // Generate "ident_t .kmpc_loc.addr;"
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001548 Address AI = CGF.CreateMemTemp(IdentQTy, ".kmpc_loc.addr");
Alexey Bataev18095712014-10-10 12:19:54 +00001549 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
John McCall7f416cc2015-09-08 08:05:57 +00001550 Elem.second.DebugLoc = AI.getPointer();
Alexey Bataev9959db52014-05-06 10:08:46 +00001551 LocValue = AI;
1552
Alexey Bataevfd006c42018-10-05 15:08:53 +00001553 if (!Elem.second.ServiceInsertPt)
1554 setLocThreadIdInsertPt(CGF);
Alexey Bataev9959db52014-05-06 10:08:46 +00001555 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
Alexey Bataevfd006c42018-10-05 15:08:53 +00001556 CGF.Builder.SetInsertPoint(Elem.second.ServiceInsertPt);
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001557 CGF.Builder.CreateMemCpy(LocValue, getOrCreateDefaultLocation(Flags),
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001558 CGF.getTypeSize(IdentQTy));
Alexey Bataev9959db52014-05-06 10:08:46 +00001559 }
1560
1561 // char **psource = &.kmpc_loc_<flags>.addr.psource;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001562 LValue Base = CGF.MakeAddrLValue(LocValue, IdentQTy);
1563 auto Fields = cast<RecordDecl>(IdentQTy->getAsTagDecl())->field_begin();
1564 LValue PSource =
1565 CGF.EmitLValueForField(Base, *std::next(Fields, IdentField_PSource));
Alexey Bataev9959db52014-05-06 10:08:46 +00001566
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001567 llvm::Value *OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding());
Alexey Bataevf002aca2014-05-30 05:48:40 +00001568 if (OMPDebugLoc == nullptr) {
1569 SmallString<128> Buffer2;
1570 llvm::raw_svector_ostream OS2(Buffer2);
1571 // Build debug location
1572 PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc);
1573 OS2 << ";" << PLoc.getFilename() << ";";
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001574 if (const auto *FD = dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl))
Alexey Bataevf002aca2014-05-30 05:48:40 +00001575 OS2 << FD->getQualifiedNameAsString();
Alexey Bataevf002aca2014-05-30 05:48:40 +00001576 OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;";
1577 OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str());
1578 OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc;
Alexey Bataev9959db52014-05-06 10:08:46 +00001579 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001580 // *psource = ";<File>;<Function>;<Line>;<Column>;;";
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001581 CGF.EmitStoreOfScalar(OMPDebugLoc, PSource);
Alexey Bataevf002aca2014-05-30 05:48:40 +00001582
John McCall7f416cc2015-09-08 08:05:57 +00001583 // Our callers always pass this to a runtime function, so for
1584 // convenience, go ahead and return a naked pointer.
1585 return LocValue.getPointer();
Alexey Bataev9959db52014-05-06 10:08:46 +00001586}
1587
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001588llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF,
1589 SourceLocation Loc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001590 assert(CGF.CurFn && "No function in current CodeGenFunction.");
1591
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001592 llvm::Value *ThreadID = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +00001593 // Check whether we've already cached a load of the thread id in this
1594 // function.
Alexey Bataev1e4b7132014-12-03 12:11:24 +00001595 auto I = OpenMPLocThreadIDMap.find(CGF.CurFn);
Alexey Bataev18095712014-10-10 12:19:54 +00001596 if (I != OpenMPLocThreadIDMap.end()) {
1597 ThreadID = I->second.ThreadID;
Alexey Bataev03b340a2014-10-21 03:16:40 +00001598 if (ThreadID != nullptr)
1599 return ThreadID;
1600 }
Alexey Bataevaee18552017-08-16 14:01:00 +00001601 // If exceptions are enabled, do not use parameter to avoid possible crash.
Alexey Bataev5d2c9a42017-11-02 18:55:05 +00001602 if (!CGF.EHStack.requiresLandingPad() || !CGF.getLangOpts().Exceptions ||
1603 !CGF.getLangOpts().CXXExceptions ||
Alexey Bataev0e1b4582017-11-02 14:25:34 +00001604 CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) {
Alexey Bataevaee18552017-08-16 14:01:00 +00001605 if (auto *OMPRegionInfo =
1606 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
1607 if (OMPRegionInfo->getThreadIDVariable()) {
1608 // Check if this an outlined function with thread id passed as argument.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001609 LValue LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF);
Alexey Bataev1e491372018-01-23 18:44:14 +00001610 ThreadID = CGF.EmitLoadOfScalar(LVal, Loc);
Alexey Bataevaee18552017-08-16 14:01:00 +00001611 // If value loaded in entry block, cache it and use it everywhere in
1612 // function.
1613 if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) {
1614 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
1615 Elem.second.ThreadID = ThreadID;
1616 }
1617 return ThreadID;
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001618 }
Alexey Bataevd6c57552014-07-25 07:55:17 +00001619 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001620 }
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001621
1622 // This is not an outlined function region - need to call __kmpc_int32
1623 // kmpc_global_thread_num(ident_t *loc).
1624 // Generate thread id value and cache this value for use across the
1625 // function.
Alexey Bataevfd006c42018-10-05 15:08:53 +00001626 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
1627 if (!Elem.second.ServiceInsertPt)
1628 setLocThreadIdInsertPt(CGF);
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001629 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
Alexey Bataevfd006c42018-10-05 15:08:53 +00001630 CGF.Builder.SetInsertPoint(Elem.second.ServiceInsertPt);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001631 llvm::CallInst *Call = CGF.Builder.CreateCall(
Alexey Bataev0e1b4582017-11-02 14:25:34 +00001632 createRuntimeFunction(OMPRTL__kmpc_global_thread_num),
1633 emitUpdateLocation(CGF, Loc));
1634 Call->setCallingConv(CGF.getRuntimeCC());
Alexey Bataev0e1b4582017-11-02 14:25:34 +00001635 Elem.second.ThreadID = Call;
1636 return Call;
Alexey Bataev9959db52014-05-06 10:08:46 +00001637}
1638
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001639void CGOpenMPRuntime::functionFinished(CodeGenFunction &CGF) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001640 assert(CGF.CurFn && "No function in current CodeGenFunction.");
Alexey Bataevfd006c42018-10-05 15:08:53 +00001641 if (OpenMPLocThreadIDMap.count(CGF.CurFn)) {
1642 clearLocThreadIdInsertPt(CGF);
Alexey Bataev03b340a2014-10-21 03:16:40 +00001643 OpenMPLocThreadIDMap.erase(CGF.CurFn);
Alexey Bataevfd006c42018-10-05 15:08:53 +00001644 }
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001645 if (FunctionUDRMap.count(CGF.CurFn) > 0) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001646 for(auto *D : FunctionUDRMap[CGF.CurFn])
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001647 UDRMap.erase(D);
Alexey Bataevc5b1d322016-03-04 09:22:22 +00001648 FunctionUDRMap.erase(CGF.CurFn);
1649 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001650}
1651
1652llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001653 return IdentTy->getPointerTo();
Alexey Bataev9959db52014-05-06 10:08:46 +00001654}
1655
1656llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001657 if (!Kmpc_MicroTy) {
1658 // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...)
1659 llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty),
1660 llvm::PointerType::getUnqual(CGM.Int32Ty)};
1661 Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true);
1662 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001663 return llvm::PointerType::getUnqual(Kmpc_MicroTy);
1664}
1665
James Y Knight9871db02019-02-05 16:42:33 +00001666llvm::FunctionCallee CGOpenMPRuntime::createRuntimeFunction(unsigned Function) {
1667 llvm::FunctionCallee RTLFn = nullptr;
Alexey Bataev50b3c952016-02-19 10:38:26 +00001668 switch (static_cast<OpenMPRTLFunction>(Function)) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001669 case OMPRTL__kmpc_fork_call: {
1670 // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro
1671 // microtask, ...);
Alexey Bataev23b69422014-06-18 07:08:49 +00001672 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1673 getKmpc_MicroPointerTy()};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001674 auto *FnTy =
Alexey Bataevd74d0602014-10-13 06:02:40 +00001675 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true);
Alexey Bataev9959db52014-05-06 10:08:46 +00001676 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call");
James Y Knight9871db02019-02-05 16:42:33 +00001677 if (auto *F = dyn_cast<llvm::Function>(RTLFn.getCallee())) {
Johannes Doerfertac991bb2019-01-19 05:36:54 +00001678 if (!F->hasMetadata(llvm::LLVMContext::MD_callback)) {
1679 llvm::LLVMContext &Ctx = F->getContext();
1680 llvm::MDBuilder MDB(Ctx);
1681 // Annotate the callback behavior of the __kmpc_fork_call:
1682 // - The callback callee is argument number 2 (microtask).
1683 // - The first two arguments of the callback callee are unknown (-1).
1684 // - All variadic arguments to the __kmpc_fork_call are passed to the
1685 // callback callee.
1686 F->addMetadata(
1687 llvm::LLVMContext::MD_callback,
1688 *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding(
1689 2, {-1, -1},
1690 /* VarArgsArePassed */ true)}));
1691 }
1692 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001693 break;
1694 }
1695 case OMPRTL__kmpc_global_thread_num: {
1696 // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc);
Alexey Bataev23b69422014-06-18 07:08:49 +00001697 llvm::Type *TypeParams[] = {getIdentTyPointerTy()};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001698 auto *FnTy =
Alexey Bataevd74d0602014-10-13 06:02:40 +00001699 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
Alexey Bataev9959db52014-05-06 10:08:46 +00001700 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num");
1701 break;
1702 }
Alexey Bataev97720002014-11-11 04:05:39 +00001703 case OMPRTL__kmpc_threadprivate_cached: {
1704 // Build void *__kmpc_threadprivate_cached(ident_t *loc,
1705 // kmp_int32 global_tid, void *data, size_t size, void ***cache);
1706 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1707 CGM.VoidPtrTy, CGM.SizeTy,
1708 CGM.VoidPtrTy->getPointerTo()->getPointerTo()};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001709 auto *FnTy =
Alexey Bataev97720002014-11-11 04:05:39 +00001710 llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg*/ false);
1711 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_cached");
1712 break;
1713 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001714 case OMPRTL__kmpc_critical: {
Alexey Bataevf9472182014-09-22 12:32:31 +00001715 // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
1716 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001717 llvm::Type *TypeParams[] = {
1718 getIdentTyPointerTy(), CGM.Int32Ty,
1719 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001720 auto *FnTy =
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001721 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1722 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical");
1723 break;
1724 }
Alexey Bataevfc57d162015-12-15 10:55:09 +00001725 case OMPRTL__kmpc_critical_with_hint: {
1726 // Build void __kmpc_critical_with_hint(ident_t *loc, kmp_int32 global_tid,
1727 // kmp_critical_name *crit, uintptr_t hint);
1728 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1729 llvm::PointerType::getUnqual(KmpCriticalNameTy),
1730 CGM.IntPtrTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001731 auto *FnTy =
Alexey Bataevfc57d162015-12-15 10:55:09 +00001732 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1733 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical_with_hint");
1734 break;
1735 }
Alexey Bataev97720002014-11-11 04:05:39 +00001736 case OMPRTL__kmpc_threadprivate_register: {
1737 // Build void __kmpc_threadprivate_register(ident_t *, void *data,
1738 // kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
1739 // typedef void *(*kmpc_ctor)(void *);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001740 auto *KmpcCtorTy =
Alexey Bataev97720002014-11-11 04:05:39 +00001741 llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy,
1742 /*isVarArg*/ false)->getPointerTo();
1743 // typedef void *(*kmpc_cctor)(void *, void *);
1744 llvm::Type *KmpcCopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001745 auto *KmpcCopyCtorTy =
Alexey Bataev97720002014-11-11 04:05:39 +00001746 llvm::FunctionType::get(CGM.VoidPtrTy, KmpcCopyCtorTyArgs,
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001747 /*isVarArg*/ false)
1748 ->getPointerTo();
Alexey Bataev97720002014-11-11 04:05:39 +00001749 // typedef void (*kmpc_dtor)(void *);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001750 auto *KmpcDtorTy =
Alexey Bataev97720002014-11-11 04:05:39 +00001751 llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, /*isVarArg*/ false)
1752 ->getPointerTo();
1753 llvm::Type *FnTyArgs[] = {getIdentTyPointerTy(), CGM.VoidPtrTy, KmpcCtorTy,
1754 KmpcCopyCtorTy, KmpcDtorTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001755 auto *FnTy = llvm::FunctionType::get(CGM.VoidTy, FnTyArgs,
Alexey Bataev97720002014-11-11 04:05:39 +00001756 /*isVarArg*/ false);
1757 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_register");
1758 break;
1759 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001760 case OMPRTL__kmpc_end_critical: {
Alexey Bataevf9472182014-09-22 12:32:31 +00001761 // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
1762 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001763 llvm::Type *TypeParams[] = {
1764 getIdentTyPointerTy(), CGM.Int32Ty,
1765 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001766 auto *FnTy =
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001767 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1768 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical");
1769 break;
1770 }
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00001771 case OMPRTL__kmpc_cancel_barrier: {
1772 // Build kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32
1773 // global_tid);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001774 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001775 auto *FnTy =
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00001776 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1777 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_cancel_barrier");
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001778 break;
1779 }
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001780 case OMPRTL__kmpc_barrier: {
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00001781 // Build void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001782 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001783 auto *FnTy =
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001784 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1785 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_barrier");
1786 break;
1787 }
Alexander Musmanc6388682014-12-15 07:07:06 +00001788 case OMPRTL__kmpc_for_static_fini: {
1789 // Build void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
1790 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001791 auto *FnTy =
Alexander Musmanc6388682014-12-15 07:07:06 +00001792 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1793 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_fini");
1794 break;
1795 }
Alexey Bataevb2059782014-10-13 08:23:51 +00001796 case OMPRTL__kmpc_push_num_threads: {
1797 // Build void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
1798 // kmp_int32 num_threads)
1799 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1800 CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001801 auto *FnTy =
Alexey Bataevb2059782014-10-13 08:23:51 +00001802 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1803 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_threads");
1804 break;
1805 }
Alexey Bataevd74d0602014-10-13 06:02:40 +00001806 case OMPRTL__kmpc_serialized_parallel: {
1807 // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
1808 // global_tid);
1809 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001810 auto *FnTy =
Alexey Bataevd74d0602014-10-13 06:02:40 +00001811 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1812 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel");
1813 break;
1814 }
1815 case OMPRTL__kmpc_end_serialized_parallel: {
1816 // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
1817 // global_tid);
1818 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001819 auto *FnTy =
Alexey Bataevd74d0602014-10-13 06:02:40 +00001820 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1821 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel");
1822 break;
1823 }
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001824 case OMPRTL__kmpc_flush: {
Alexey Bataevd76df6d2015-02-24 12:55:09 +00001825 // Build void __kmpc_flush(ident_t *loc);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001826 llvm::Type *TypeParams[] = {getIdentTyPointerTy()};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001827 auto *FnTy =
Alexey Bataevd76df6d2015-02-24 12:55:09 +00001828 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001829 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_flush");
1830 break;
1831 }
Alexey Bataev8d690652014-12-04 07:23:53 +00001832 case OMPRTL__kmpc_master: {
1833 // Build kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid);
1834 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001835 auto *FnTy =
Alexey Bataev8d690652014-12-04 07:23:53 +00001836 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1837 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_master");
1838 break;
1839 }
1840 case OMPRTL__kmpc_end_master: {
1841 // Build void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid);
1842 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001843 auto *FnTy =
Alexey Bataev8d690652014-12-04 07:23:53 +00001844 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1845 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_master");
1846 break;
1847 }
Alexey Bataev9f797f32015-02-05 05:57:51 +00001848 case OMPRTL__kmpc_omp_taskyield: {
1849 // Build kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid,
1850 // int end_part);
1851 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001852 auto *FnTy =
Alexey Bataev9f797f32015-02-05 05:57:51 +00001853 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1854 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_taskyield");
1855 break;
1856 }
Alexey Bataev6956e2e2015-02-05 06:35:41 +00001857 case OMPRTL__kmpc_single: {
1858 // Build kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid);
1859 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001860 auto *FnTy =
Alexey Bataev6956e2e2015-02-05 06:35:41 +00001861 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1862 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_single");
1863 break;
1864 }
1865 case OMPRTL__kmpc_end_single: {
1866 // Build void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid);
1867 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001868 auto *FnTy =
Alexey Bataev6956e2e2015-02-05 06:35:41 +00001869 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1870 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_single");
1871 break;
1872 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00001873 case OMPRTL__kmpc_omp_task_alloc: {
1874 // Build kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
1875 // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1876 // kmp_routine_entry_t *task_entry);
1877 assert(KmpRoutineEntryPtrTy != nullptr &&
1878 "Type kmp_routine_entry_t must be created.");
1879 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty,
1880 CGM.SizeTy, CGM.SizeTy, KmpRoutineEntryPtrTy};
1881 // Return void * and then cast to particular kmp_task_t type.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001882 auto *FnTy =
Alexey Bataev62b63b12015-03-10 07:28:44 +00001883 llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false);
1884 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_alloc");
1885 break;
1886 }
1887 case OMPRTL__kmpc_omp_task: {
1888 // Build kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t
1889 // *new_task);
1890 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1891 CGM.VoidPtrTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001892 auto *FnTy =
Alexey Bataev62b63b12015-03-10 07:28:44 +00001893 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1894 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task");
1895 break;
1896 }
Alexey Bataeva63048e2015-03-23 06:18:07 +00001897 case OMPRTL__kmpc_copyprivate: {
1898 // Build void __kmpc_copyprivate(ident_t *loc, kmp_int32 global_tid,
Alexey Bataev66beaa92015-04-30 03:47:32 +00001899 // size_t cpy_size, void *cpy_data, void(*cpy_func)(void *, void *),
Alexey Bataeva63048e2015-03-23 06:18:07 +00001900 // kmp_int32 didit);
1901 llvm::Type *CpyTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
1902 auto *CpyFnTy =
1903 llvm::FunctionType::get(CGM.VoidTy, CpyTypeParams, /*isVarArg=*/false);
Alexey Bataev66beaa92015-04-30 03:47:32 +00001904 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.SizeTy,
Alexey Bataeva63048e2015-03-23 06:18:07 +00001905 CGM.VoidPtrTy, CpyFnTy->getPointerTo(),
1906 CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001907 auto *FnTy =
Alexey Bataeva63048e2015-03-23 06:18:07 +00001908 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1909 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_copyprivate");
1910 break;
1911 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001912 case OMPRTL__kmpc_reduce: {
1913 // Build kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid,
1914 // kmp_int32 num_vars, size_t reduce_size, void *reduce_data, void
1915 // (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name *lck);
1916 llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
1917 auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams,
1918 /*isVarArg=*/false);
1919 llvm::Type *TypeParams[] = {
1920 getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy,
1921 CGM.VoidPtrTy, ReduceFnTy->getPointerTo(),
1922 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001923 auto *FnTy =
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001924 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1925 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce");
1926 break;
1927 }
1928 case OMPRTL__kmpc_reduce_nowait: {
1929 // Build kmp_int32 __kmpc_reduce_nowait(ident_t *loc, kmp_int32
1930 // global_tid, kmp_int32 num_vars, size_t reduce_size, void *reduce_data,
1931 // void (*reduce_func)(void *lhs_data, void *rhs_data), kmp_critical_name
1932 // *lck);
1933 llvm::Type *ReduceTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
1934 auto *ReduceFnTy = llvm::FunctionType::get(CGM.VoidTy, ReduceTypeParams,
1935 /*isVarArg=*/false);
1936 llvm::Type *TypeParams[] = {
1937 getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty, CGM.SizeTy,
1938 CGM.VoidPtrTy, ReduceFnTy->getPointerTo(),
1939 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001940 auto *FnTy =
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001941 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1942 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_reduce_nowait");
1943 break;
1944 }
1945 case OMPRTL__kmpc_end_reduce: {
1946 // Build void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
1947 // kmp_critical_name *lck);
1948 llvm::Type *TypeParams[] = {
1949 getIdentTyPointerTy(), CGM.Int32Ty,
1950 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001951 auto *FnTy =
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001952 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1953 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce");
1954 break;
1955 }
1956 case OMPRTL__kmpc_end_reduce_nowait: {
1957 // Build __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
1958 // kmp_critical_name *lck);
1959 llvm::Type *TypeParams[] = {
1960 getIdentTyPointerTy(), CGM.Int32Ty,
1961 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001962 auto *FnTy =
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001963 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1964 RTLFn =
1965 CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_reduce_nowait");
1966 break;
1967 }
Alexey Bataev1d677132015-04-22 13:57:31 +00001968 case OMPRTL__kmpc_omp_task_begin_if0: {
1969 // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t
1970 // *new_task);
1971 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1972 CGM.VoidPtrTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001973 auto *FnTy =
Alexey Bataev1d677132015-04-22 13:57:31 +00001974 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1975 RTLFn =
1976 CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_begin_if0");
1977 break;
1978 }
1979 case OMPRTL__kmpc_omp_task_complete_if0: {
1980 // Build void __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t
1981 // *new_task);
1982 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
1983 CGM.VoidPtrTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001984 auto *FnTy =
Alexey Bataev1d677132015-04-22 13:57:31 +00001985 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1986 RTLFn = CGM.CreateRuntimeFunction(FnTy,
1987 /*Name=*/"__kmpc_omp_task_complete_if0");
1988 break;
1989 }
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001990 case OMPRTL__kmpc_ordered: {
1991 // Build void __kmpc_ordered(ident_t *loc, kmp_int32 global_tid);
1992 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00001993 auto *FnTy =
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001994 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1995 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_ordered");
1996 break;
1997 }
1998 case OMPRTL__kmpc_end_ordered: {
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001999 // Build void __kmpc_end_ordered(ident_t *loc, kmp_int32 global_tid);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002000 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002001 auto *FnTy =
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002002 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2003 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_ordered");
2004 break;
2005 }
Alexey Bataev8b8e2022015-04-27 05:22:09 +00002006 case OMPRTL__kmpc_omp_taskwait: {
2007 // Build kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32 global_tid);
2008 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002009 auto *FnTy =
Alexey Bataev8b8e2022015-04-27 05:22:09 +00002010 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
2011 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_omp_taskwait");
2012 break;
2013 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002014 case OMPRTL__kmpc_taskgroup: {
2015 // Build void __kmpc_taskgroup(ident_t *loc, kmp_int32 global_tid);
2016 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002017 auto *FnTy =
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002018 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2019 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_taskgroup");
2020 break;
2021 }
2022 case OMPRTL__kmpc_end_taskgroup: {
2023 // Build void __kmpc_end_taskgroup(ident_t *loc, kmp_int32 global_tid);
2024 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002025 auto *FnTy =
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002026 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2027 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_taskgroup");
2028 break;
2029 }
Alexey Bataev7f210c62015-06-18 13:40:03 +00002030 case OMPRTL__kmpc_push_proc_bind: {
2031 // Build void __kmpc_push_proc_bind(ident_t *loc, kmp_int32 global_tid,
2032 // int proc_bind)
2033 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002034 auto *FnTy =
Alexey Bataev7f210c62015-06-18 13:40:03 +00002035 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
2036 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_proc_bind");
2037 break;
2038 }
Alexey Bataev1d2353d2015-06-24 11:01:36 +00002039 case OMPRTL__kmpc_omp_task_with_deps: {
2040 // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid,
2041 // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list,
2042 // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list);
2043 llvm::Type *TypeParams[] = {
2044 getIdentTyPointerTy(), CGM.Int32Ty, CGM.VoidPtrTy, CGM.Int32Ty,
2045 CGM.VoidPtrTy, CGM.Int32Ty, CGM.VoidPtrTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002046 auto *FnTy =
Alexey Bataev1d2353d2015-06-24 11:01:36 +00002047 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
2048 RTLFn =
2049 CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_with_deps");
2050 break;
2051 }
2052 case OMPRTL__kmpc_omp_wait_deps: {
2053 // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid,
2054 // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32 ndeps_noalias,
2055 // kmp_depend_info_t *noalias_dep_list);
2056 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
2057 CGM.Int32Ty, CGM.VoidPtrTy,
2058 CGM.Int32Ty, CGM.VoidPtrTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002059 auto *FnTy =
Alexey Bataev1d2353d2015-06-24 11:01:36 +00002060 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2061 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_wait_deps");
2062 break;
2063 }
Alexey Bataev0f34da12015-07-02 04:17:07 +00002064 case OMPRTL__kmpc_cancellationpoint: {
2065 // Build kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32
2066 // global_tid, kmp_int32 cncl_kind)
2067 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002068 auto *FnTy =
Alexey Bataev0f34da12015-07-02 04:17:07 +00002069 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2070 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancellationpoint");
2071 break;
2072 }
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00002073 case OMPRTL__kmpc_cancel: {
2074 // Build kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid,
2075 // kmp_int32 cncl_kind)
2076 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002077 auto *FnTy =
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00002078 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2079 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_cancel");
2080 break;
2081 }
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00002082 case OMPRTL__kmpc_push_num_teams: {
2083 // Build void kmpc_push_num_teams (ident_t loc, kmp_int32 global_tid,
2084 // kmp_int32 num_teams, kmp_int32 num_threads)
2085 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty,
2086 CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002087 auto *FnTy =
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00002088 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2089 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_teams");
2090 break;
2091 }
2092 case OMPRTL__kmpc_fork_teams: {
2093 // Build void __kmpc_fork_teams(ident_t *loc, kmp_int32 argc, kmpc_micro
2094 // microtask, ...);
2095 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
2096 getKmpc_MicroPointerTy()};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002097 auto *FnTy =
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00002098 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true);
2099 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_teams");
James Y Knight9871db02019-02-05 16:42:33 +00002100 if (auto *F = dyn_cast<llvm::Function>(RTLFn.getCallee())) {
Johannes Doerfertac991bb2019-01-19 05:36:54 +00002101 if (!F->hasMetadata(llvm::LLVMContext::MD_callback)) {
2102 llvm::LLVMContext &Ctx = F->getContext();
2103 llvm::MDBuilder MDB(Ctx);
2104 // Annotate the callback behavior of the __kmpc_fork_teams:
2105 // - The callback callee is argument number 2 (microtask).
2106 // - The first two arguments of the callback callee are unknown (-1).
2107 // - All variadic arguments to the __kmpc_fork_teams are passed to the
2108 // callback callee.
2109 F->addMetadata(
2110 llvm::LLVMContext::MD_callback,
2111 *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding(
2112 2, {-1, -1},
2113 /* VarArgsArePassed */ true)}));
2114 }
2115 }
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00002116 break;
2117 }
Alexey Bataev7292c292016-04-25 12:22:29 +00002118 case OMPRTL__kmpc_taskloop: {
2119 // Build void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int
2120 // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int
2121 // sched, kmp_uint64 grainsize, void *task_dup);
2122 llvm::Type *TypeParams[] = {getIdentTyPointerTy(),
2123 CGM.IntTy,
2124 CGM.VoidPtrTy,
2125 CGM.IntTy,
2126 CGM.Int64Ty->getPointerTo(),
2127 CGM.Int64Ty->getPointerTo(),
2128 CGM.Int64Ty,
2129 CGM.IntTy,
2130 CGM.IntTy,
2131 CGM.Int64Ty,
2132 CGM.VoidPtrTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002133 auto *FnTy =
Alexey Bataev7292c292016-04-25 12:22:29 +00002134 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2135 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_taskloop");
2136 break;
2137 }
Alexey Bataev8b427062016-05-25 12:36:08 +00002138 case OMPRTL__kmpc_doacross_init: {
2139 // Build void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid, kmp_int32
2140 // num_dims, struct kmp_dim *dims);
2141 llvm::Type *TypeParams[] = {getIdentTyPointerTy(),
2142 CGM.Int32Ty,
2143 CGM.Int32Ty,
2144 CGM.VoidPtrTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002145 auto *FnTy =
Alexey Bataev8b427062016-05-25 12:36:08 +00002146 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2147 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_init");
2148 break;
2149 }
2150 case OMPRTL__kmpc_doacross_fini: {
2151 // Build void __kmpc_doacross_fini(ident_t *loc, kmp_int32 gtid);
2152 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002153 auto *FnTy =
Alexey Bataev8b427062016-05-25 12:36:08 +00002154 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2155 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_fini");
2156 break;
2157 }
2158 case OMPRTL__kmpc_doacross_post: {
2159 // Build void __kmpc_doacross_post(ident_t *loc, kmp_int32 gtid, kmp_int64
2160 // *vec);
2161 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
2162 CGM.Int64Ty->getPointerTo()};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002163 auto *FnTy =
Alexey Bataev8b427062016-05-25 12:36:08 +00002164 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2165 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_post");
2166 break;
2167 }
2168 case OMPRTL__kmpc_doacross_wait: {
2169 // Build void __kmpc_doacross_wait(ident_t *loc, kmp_int32 gtid, kmp_int64
2170 // *vec);
2171 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
2172 CGM.Int64Ty->getPointerTo()};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002173 auto *FnTy =
Alexey Bataev8b427062016-05-25 12:36:08 +00002174 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2175 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_doacross_wait");
2176 break;
2177 }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002178 case OMPRTL__kmpc_task_reduction_init: {
2179 // Build void *__kmpc_task_reduction_init(int gtid, int num_data, void
2180 // *data);
2181 llvm::Type *TypeParams[] = {CGM.IntTy, CGM.IntTy, CGM.VoidPtrTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002182 auto *FnTy =
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002183 llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false);
2184 RTLFn =
2185 CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_task_reduction_init");
2186 break;
2187 }
2188 case OMPRTL__kmpc_task_reduction_get_th_data: {
2189 // Build void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void
2190 // *d);
2191 llvm::Type *TypeParams[] = {CGM.IntTy, CGM.VoidPtrTy, CGM.VoidPtrTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002192 auto *FnTy =
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002193 llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false);
2194 RTLFn = CGM.CreateRuntimeFunction(
2195 FnTy, /*Name=*/"__kmpc_task_reduction_get_th_data");
2196 break;
2197 }
Alexey Bataev7bb33532019-01-07 21:30:43 +00002198 case OMPRTL__kmpc_push_target_tripcount: {
2199 // Build void __kmpc_push_target_tripcount(int64_t device_id, kmp_uint64
2200 // size);
2201 llvm::Type *TypeParams[] = {CGM.Int64Ty, CGM.Int64Ty};
2202 llvm::FunctionType *FnTy =
2203 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2204 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_target_tripcount");
2205 break;
2206 }
Samuel Antaobed3c462015-10-02 16:14:20 +00002207 case OMPRTL__tgt_target: {
George Rokos63bc9d62017-11-21 18:25:12 +00002208 // Build int32_t __tgt_target(int64_t device_id, void *host_ptr, int32_t
2209 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
Samuel Antaobed3c462015-10-02 16:14:20 +00002210 // *arg_types);
George Rokos63bc9d62017-11-21 18:25:12 +00002211 llvm::Type *TypeParams[] = {CGM.Int64Ty,
Samuel Antaobed3c462015-10-02 16:14:20 +00002212 CGM.VoidPtrTy,
2213 CGM.Int32Ty,
2214 CGM.VoidPtrPtrTy,
2215 CGM.VoidPtrPtrTy,
2216 CGM.SizeTy->getPointerTo(),
George Rokos63bc9d62017-11-21 18:25:12 +00002217 CGM.Int64Ty->getPointerTo()};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002218 auto *FnTy =
Samuel Antaobed3c462015-10-02 16:14:20 +00002219 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2220 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target");
2221 break;
2222 }
Alexey Bataeva9f77c62017-12-13 21:04:20 +00002223 case OMPRTL__tgt_target_nowait: {
2224 // Build int32_t __tgt_target_nowait(int64_t device_id, void *host_ptr,
2225 // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes,
2226 // int64_t *arg_types);
2227 llvm::Type *TypeParams[] = {CGM.Int64Ty,
2228 CGM.VoidPtrTy,
2229 CGM.Int32Ty,
2230 CGM.VoidPtrPtrTy,
2231 CGM.VoidPtrPtrTy,
2232 CGM.SizeTy->getPointerTo(),
2233 CGM.Int64Ty->getPointerTo()};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002234 auto *FnTy =
Alexey Bataeva9f77c62017-12-13 21:04:20 +00002235 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2236 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_nowait");
2237 break;
2238 }
Samuel Antaob68e2db2016-03-03 16:20:23 +00002239 case OMPRTL__tgt_target_teams: {
George Rokos63bc9d62017-11-21 18:25:12 +00002240 // Build int32_t __tgt_target_teams(int64_t device_id, void *host_ptr,
Samuel Antaob68e2db2016-03-03 16:20:23 +00002241 // int32_t arg_num, void** args_base, void **args, size_t *arg_sizes,
George Rokos63bc9d62017-11-21 18:25:12 +00002242 // int64_t *arg_types, int32_t num_teams, int32_t thread_limit);
2243 llvm::Type *TypeParams[] = {CGM.Int64Ty,
Samuel Antaob68e2db2016-03-03 16:20:23 +00002244 CGM.VoidPtrTy,
2245 CGM.Int32Ty,
2246 CGM.VoidPtrPtrTy,
2247 CGM.VoidPtrPtrTy,
2248 CGM.SizeTy->getPointerTo(),
George Rokos63bc9d62017-11-21 18:25:12 +00002249 CGM.Int64Ty->getPointerTo(),
Samuel Antaob68e2db2016-03-03 16:20:23 +00002250 CGM.Int32Ty,
2251 CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002252 auto *FnTy =
Samuel Antaob68e2db2016-03-03 16:20:23 +00002253 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2254 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams");
2255 break;
2256 }
Alexey Bataeva9f77c62017-12-13 21:04:20 +00002257 case OMPRTL__tgt_target_teams_nowait: {
2258 // Build int32_t __tgt_target_teams_nowait(int64_t device_id, void
2259 // *host_ptr, int32_t arg_num, void** args_base, void **args, size_t
2260 // *arg_sizes, int64_t *arg_types, int32_t num_teams, int32_t thread_limit);
2261 llvm::Type *TypeParams[] = {CGM.Int64Ty,
2262 CGM.VoidPtrTy,
2263 CGM.Int32Ty,
2264 CGM.VoidPtrPtrTy,
2265 CGM.VoidPtrPtrTy,
2266 CGM.SizeTy->getPointerTo(),
2267 CGM.Int64Ty->getPointerTo(),
2268 CGM.Int32Ty,
2269 CGM.Int32Ty};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002270 auto *FnTy =
Alexey Bataeva9f77c62017-12-13 21:04:20 +00002271 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2272 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_teams_nowait");
2273 break;
2274 }
Samuel Antaoee8fb302016-01-06 13:42:12 +00002275 case OMPRTL__tgt_register_lib: {
2276 // Build void __tgt_register_lib(__tgt_bin_desc *desc);
2277 QualType ParamTy =
2278 CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy());
2279 llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002280 auto *FnTy =
Samuel Antaoee8fb302016-01-06 13:42:12 +00002281 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2282 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_register_lib");
2283 break;
2284 }
2285 case OMPRTL__tgt_unregister_lib: {
2286 // Build void __tgt_unregister_lib(__tgt_bin_desc *desc);
2287 QualType ParamTy =
2288 CGM.getContext().getPointerType(getTgtBinaryDescriptorQTy());
2289 llvm::Type *TypeParams[] = {CGM.getTypes().ConvertTypeForMem(ParamTy)};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002290 auto *FnTy =
Samuel Antaoee8fb302016-01-06 13:42:12 +00002291 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2292 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_unregister_lib");
2293 break;
2294 }
Samuel Antaodf158d52016-04-27 22:58:19 +00002295 case OMPRTL__tgt_target_data_begin: {
George Rokos63bc9d62017-11-21 18:25:12 +00002296 // Build void __tgt_target_data_begin(int64_t device_id, int32_t arg_num,
2297 // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types);
2298 llvm::Type *TypeParams[] = {CGM.Int64Ty,
Samuel Antaodf158d52016-04-27 22:58:19 +00002299 CGM.Int32Ty,
2300 CGM.VoidPtrPtrTy,
2301 CGM.VoidPtrPtrTy,
2302 CGM.SizeTy->getPointerTo(),
George Rokos63bc9d62017-11-21 18:25:12 +00002303 CGM.Int64Ty->getPointerTo()};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002304 auto *FnTy =
Samuel Antaodf158d52016-04-27 22:58:19 +00002305 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
2306 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin");
2307 break;
2308 }
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +00002309 case OMPRTL__tgt_target_data_begin_nowait: {
2310 // Build void __tgt_target_data_begin_nowait(int64_t device_id, int32_t
2311 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
2312 // *arg_types);
2313 llvm::Type *TypeParams[] = {CGM.Int64Ty,
2314 CGM.Int32Ty,
2315 CGM.VoidPtrPtrTy,
2316 CGM.VoidPtrPtrTy,
2317 CGM.SizeTy->getPointerTo(),
2318 CGM.Int64Ty->getPointerTo()};
2319 auto *FnTy =
2320 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2321 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_begin_nowait");
2322 break;
2323 }
Samuel Antaodf158d52016-04-27 22:58:19 +00002324 case OMPRTL__tgt_target_data_end: {
George Rokos63bc9d62017-11-21 18:25:12 +00002325 // Build void __tgt_target_data_end(int64_t device_id, int32_t arg_num,
2326 // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types);
2327 llvm::Type *TypeParams[] = {CGM.Int64Ty,
Samuel Antaodf158d52016-04-27 22:58:19 +00002328 CGM.Int32Ty,
2329 CGM.VoidPtrPtrTy,
2330 CGM.VoidPtrPtrTy,
2331 CGM.SizeTy->getPointerTo(),
George Rokos63bc9d62017-11-21 18:25:12 +00002332 CGM.Int64Ty->getPointerTo()};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002333 auto *FnTy =
Samuel Antaodf158d52016-04-27 22:58:19 +00002334 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
2335 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end");
2336 break;
2337 }
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +00002338 case OMPRTL__tgt_target_data_end_nowait: {
2339 // Build void __tgt_target_data_end_nowait(int64_t device_id, int32_t
2340 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
2341 // *arg_types);
2342 llvm::Type *TypeParams[] = {CGM.Int64Ty,
2343 CGM.Int32Ty,
2344 CGM.VoidPtrPtrTy,
2345 CGM.VoidPtrPtrTy,
2346 CGM.SizeTy->getPointerTo(),
2347 CGM.Int64Ty->getPointerTo()};
2348 auto *FnTy =
2349 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2350 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_end_nowait");
2351 break;
2352 }
Samuel Antao8d2d7302016-05-26 18:30:22 +00002353 case OMPRTL__tgt_target_data_update: {
George Rokos63bc9d62017-11-21 18:25:12 +00002354 // Build void __tgt_target_data_update(int64_t device_id, int32_t arg_num,
2355 // void** args_base, void **args, size_t *arg_sizes, int64_t *arg_types);
2356 llvm::Type *TypeParams[] = {CGM.Int64Ty,
Samuel Antao8d2d7302016-05-26 18:30:22 +00002357 CGM.Int32Ty,
2358 CGM.VoidPtrPtrTy,
2359 CGM.VoidPtrPtrTy,
2360 CGM.SizeTy->getPointerTo(),
George Rokos63bc9d62017-11-21 18:25:12 +00002361 CGM.Int64Ty->getPointerTo()};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002362 auto *FnTy =
Samuel Antao8d2d7302016-05-26 18:30:22 +00002363 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
2364 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update");
2365 break;
2366 }
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +00002367 case OMPRTL__tgt_target_data_update_nowait: {
2368 // Build void __tgt_target_data_update_nowait(int64_t device_id, int32_t
2369 // arg_num, void** args_base, void **args, size_t *arg_sizes, int64_t
2370 // *arg_types);
2371 llvm::Type *TypeParams[] = {CGM.Int64Ty,
2372 CGM.Int32Ty,
2373 CGM.VoidPtrPtrTy,
2374 CGM.VoidPtrPtrTy,
2375 CGM.SizeTy->getPointerTo(),
2376 CGM.Int64Ty->getPointerTo()};
2377 auto *FnTy =
2378 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2379 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__tgt_target_data_update_nowait");
2380 break;
2381 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002382 }
Alexey Bataev50b3c952016-02-19 10:38:26 +00002383 assert(RTLFn && "Unable to find OpenMP runtime function");
Alexey Bataev9959db52014-05-06 10:08:46 +00002384 return RTLFn;
2385}
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002386
James Y Knight9871db02019-02-05 16:42:33 +00002387llvm::FunctionCallee
2388CGOpenMPRuntime::createForStaticInitFunction(unsigned IVSize, bool IVSigned) {
Alexander Musman21212e42015-03-13 10:38:23 +00002389 assert((IVSize == 32 || IVSize == 64) &&
2390 "IV size is not compatible with the omp runtime");
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002391 StringRef Name = IVSize == 32 ? (IVSigned ? "__kmpc_for_static_init_4"
2392 : "__kmpc_for_static_init_4u")
2393 : (IVSigned ? "__kmpc_for_static_init_8"
2394 : "__kmpc_for_static_init_8u");
2395 llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty;
2396 auto *PtrTy = llvm::PointerType::getUnqual(ITy);
Alexander Musman21212e42015-03-13 10:38:23 +00002397 llvm::Type *TypeParams[] = {
2398 getIdentTyPointerTy(), // loc
2399 CGM.Int32Ty, // tid
2400 CGM.Int32Ty, // schedtype
2401 llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
2402 PtrTy, // p_lower
2403 PtrTy, // p_upper
2404 PtrTy, // p_stride
2405 ITy, // incr
2406 ITy // chunk
2407 };
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002408 auto *FnTy =
Alexander Musman21212e42015-03-13 10:38:23 +00002409 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
2410 return CGM.CreateRuntimeFunction(FnTy, Name);
2411}
2412
James Y Knight9871db02019-02-05 16:42:33 +00002413llvm::FunctionCallee
2414CGOpenMPRuntime::createDispatchInitFunction(unsigned IVSize, bool IVSigned) {
Alexander Musman92bdaab2015-03-12 13:37:50 +00002415 assert((IVSize == 32 || IVSize == 64) &&
2416 "IV size is not compatible with the omp runtime");
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002417 StringRef Name =
Alexander Musman92bdaab2015-03-12 13:37:50 +00002418 IVSize == 32
2419 ? (IVSigned ? "__kmpc_dispatch_init_4" : "__kmpc_dispatch_init_4u")
2420 : (IVSigned ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_8u");
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002421 llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty;
Alexander Musman92bdaab2015-03-12 13:37:50 +00002422 llvm::Type *TypeParams[] = { getIdentTyPointerTy(), // loc
2423 CGM.Int32Ty, // tid
2424 CGM.Int32Ty, // schedtype
2425 ITy, // lower
2426 ITy, // upper
2427 ITy, // stride
2428 ITy // chunk
2429 };
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002430 auto *FnTy =
Alexander Musman92bdaab2015-03-12 13:37:50 +00002431 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
2432 return CGM.CreateRuntimeFunction(FnTy, Name);
2433}
2434
James Y Knight9871db02019-02-05 16:42:33 +00002435llvm::FunctionCallee
2436CGOpenMPRuntime::createDispatchFiniFunction(unsigned IVSize, bool IVSigned) {
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002437 assert((IVSize == 32 || IVSize == 64) &&
2438 "IV size is not compatible with the omp runtime");
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002439 StringRef Name =
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002440 IVSize == 32
2441 ? (IVSigned ? "__kmpc_dispatch_fini_4" : "__kmpc_dispatch_fini_4u")
2442 : (IVSigned ? "__kmpc_dispatch_fini_8" : "__kmpc_dispatch_fini_8u");
2443 llvm::Type *TypeParams[] = {
2444 getIdentTyPointerTy(), // loc
2445 CGM.Int32Ty, // tid
2446 };
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002447 auto *FnTy =
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002448 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
2449 return CGM.CreateRuntimeFunction(FnTy, Name);
2450}
2451
James Y Knight9871db02019-02-05 16:42:33 +00002452llvm::FunctionCallee
2453CGOpenMPRuntime::createDispatchNextFunction(unsigned IVSize, bool IVSigned) {
Alexander Musman92bdaab2015-03-12 13:37:50 +00002454 assert((IVSize == 32 || IVSize == 64) &&
2455 "IV size is not compatible with the omp runtime");
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002456 StringRef Name =
Alexander Musman92bdaab2015-03-12 13:37:50 +00002457 IVSize == 32
2458 ? (IVSigned ? "__kmpc_dispatch_next_4" : "__kmpc_dispatch_next_4u")
2459 : (IVSigned ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_8u");
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002460 llvm::Type *ITy = IVSize == 32 ? CGM.Int32Ty : CGM.Int64Ty;
2461 auto *PtrTy = llvm::PointerType::getUnqual(ITy);
Alexander Musman92bdaab2015-03-12 13:37:50 +00002462 llvm::Type *TypeParams[] = {
2463 getIdentTyPointerTy(), // loc
2464 CGM.Int32Ty, // tid
2465 llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
2466 PtrTy, // p_lower
2467 PtrTy, // p_upper
2468 PtrTy // p_stride
2469 };
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002470 auto *FnTy =
Alexander Musman92bdaab2015-03-12 13:37:50 +00002471 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
2472 return CGM.CreateRuntimeFunction(FnTy, Name);
2473}
2474
Alexey Bataev03f270c2018-03-30 18:31:07 +00002475Address CGOpenMPRuntime::getAddrOfDeclareTargetLink(const VarDecl *VD) {
2476 if (CGM.getLangOpts().OpenMPSimd)
2477 return Address::invalid();
Alexey Bataev92327c52018-03-26 16:40:55 +00002478 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00002479 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00002480 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
2481 SmallString<64> PtrName;
2482 {
2483 llvm::raw_svector_ostream OS(PtrName);
2484 OS << CGM.getMangledName(GlobalDecl(VD)) << "_decl_tgt_link_ptr";
2485 }
2486 llvm::Value *Ptr = CGM.getModule().getNamedValue(PtrName);
2487 if (!Ptr) {
2488 QualType PtrTy = CGM.getContext().getPointerType(VD->getType());
2489 Ptr = getOrCreateInternalVariable(CGM.getTypes().ConvertTypeForMem(PtrTy),
2490 PtrName);
Alexey Bataev03f270c2018-03-30 18:31:07 +00002491 if (!CGM.getLangOpts().OpenMPIsDevice) {
2492 auto *GV = cast<llvm::GlobalVariable>(Ptr);
2493 GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
2494 GV->setInitializer(CGM.GetAddrOfGlobal(VD));
2495 }
2496 CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ptr));
2497 registerTargetGlobalVariable(VD, cast<llvm::Constant>(Ptr));
Alexey Bataev92327c52018-03-26 16:40:55 +00002498 }
2499 return Address(Ptr, CGM.getContext().getDeclAlign(VD));
2500 }
2501 return Address::invalid();
2502}
2503
Alexey Bataev97720002014-11-11 04:05:39 +00002504llvm::Constant *
2505CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) {
Samuel Antaof8b50122015-07-13 22:54:53 +00002506 assert(!CGM.getLangOpts().OpenMPUseTLS ||
2507 !CGM.getContext().getTargetInfo().isTLSSupported());
Alexey Bataev97720002014-11-11 04:05:39 +00002508 // Lookup the entry, lazily creating it if necessary.
Alexey Bataev18fa2322018-05-02 14:20:50 +00002509 std::string Suffix = getName({"cache", ""});
2510 return getOrCreateInternalVariable(
2511 CGM.Int8PtrPtrTy, Twine(CGM.getMangledName(VD)).concat(Suffix));
Alexey Bataev97720002014-11-11 04:05:39 +00002512}
2513
John McCall7f416cc2015-09-08 08:05:57 +00002514Address CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF,
2515 const VarDecl *VD,
2516 Address VDAddr,
2517 SourceLocation Loc) {
Samuel Antaof8b50122015-07-13 22:54:53 +00002518 if (CGM.getLangOpts().OpenMPUseTLS &&
2519 CGM.getContext().getTargetInfo().isTLSSupported())
2520 return VDAddr;
2521
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002522 llvm::Type *VarTy = VDAddr.getElementType();
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002523 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
John McCall7f416cc2015-09-08 08:05:57 +00002524 CGF.Builder.CreatePointerCast(VDAddr.getPointer(),
2525 CGM.Int8PtrTy),
Alexey Bataev97720002014-11-11 04:05:39 +00002526 CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)),
2527 getOrCreateThreadPrivateCache(VD)};
John McCall7f416cc2015-09-08 08:05:57 +00002528 return Address(CGF.EmitRuntimeCall(
2529 createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args),
2530 VDAddr.getAlignment());
Alexey Bataev97720002014-11-11 04:05:39 +00002531}
2532
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002533void CGOpenMPRuntime::emitThreadPrivateVarInit(
John McCall7f416cc2015-09-08 08:05:57 +00002534 CodeGenFunction &CGF, Address VDAddr, llvm::Value *Ctor,
Alexey Bataev97720002014-11-11 04:05:39 +00002535 llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) {
2536 // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime
2537 // library.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002538 llvm::Value *OMPLoc = emitUpdateLocation(CGF, Loc);
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002539 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num),
Alexey Bataev97720002014-11-11 04:05:39 +00002540 OMPLoc);
2541 // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor)
2542 // to register constructor/destructor for variable.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002543 llvm::Value *Args[] = {
2544 OMPLoc, CGF.Builder.CreatePointerCast(VDAddr.getPointer(), CGM.VoidPtrTy),
2545 Ctor, CopyCtor, Dtor};
Alexey Bataev1e4b7132014-12-03 12:11:24 +00002546 CGF.EmitRuntimeCall(
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002547 createRuntimeFunction(OMPRTL__kmpc_threadprivate_register), Args);
Alexey Bataev97720002014-11-11 04:05:39 +00002548}
2549
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002550llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition(
John McCall7f416cc2015-09-08 08:05:57 +00002551 const VarDecl *VD, Address VDAddr, SourceLocation Loc,
Alexey Bataev97720002014-11-11 04:05:39 +00002552 bool PerformInit, CodeGenFunction *CGF) {
Samuel Antaof8b50122015-07-13 22:54:53 +00002553 if (CGM.getLangOpts().OpenMPUseTLS &&
2554 CGM.getContext().getTargetInfo().isTLSSupported())
2555 return nullptr;
2556
Alexey Bataev97720002014-11-11 04:05:39 +00002557 VD = VD->getDefinition(CGM.getContext());
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00002558 if (VD && ThreadPrivateWithDefinition.insert(CGM.getMangledName(VD)).second) {
Alexey Bataev97720002014-11-11 04:05:39 +00002559 QualType ASTTy = VD->getType();
2560
2561 llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002562 const Expr *Init = VD->getAnyInitializer();
Alexey Bataev97720002014-11-11 04:05:39 +00002563 if (CGM.getLangOpts().CPlusPlus && PerformInit) {
2564 // Generate function that re-emits the declaration's initializer into the
2565 // threadprivate copy of the variable VD
2566 CodeGenFunction CtorCGF(CGM);
2567 FunctionArgList Args;
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002568 ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc,
2569 /*Id=*/nullptr, CGM.getContext().VoidPtrTy,
Alexey Bataev56223232017-06-09 13:40:18 +00002570 ImplicitParamDecl::Other);
Alexey Bataev97720002014-11-11 04:05:39 +00002571 Args.push_back(&Dst);
2572
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002573 const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(
John McCallc56a8b32016-03-11 04:30:31 +00002574 CGM.getContext().VoidPtrTy, Args);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002575 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
Alexey Bataev18fa2322018-05-02 14:20:50 +00002576 std::string Name = getName({"__kmpc_global_ctor_", ""});
2577 llvm::Function *Fn =
2578 CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00002579 CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI,
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002580 Args, Loc, Loc);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002581 llvm::Value *ArgVal = CtorCGF.EmitLoadOfScalar(
John McCall7f416cc2015-09-08 08:05:57 +00002582 CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false,
Alexey Bataev97720002014-11-11 04:05:39 +00002583 CGM.getContext().VoidPtrTy, Dst.getLocation());
John McCall7f416cc2015-09-08 08:05:57 +00002584 Address Arg = Address(ArgVal, VDAddr.getAlignment());
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002585 Arg = CtorCGF.Builder.CreateElementBitCast(
2586 Arg, CtorCGF.ConvertTypeForMem(ASTTy));
Alexey Bataev97720002014-11-11 04:05:39 +00002587 CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(),
2588 /*IsInitializer=*/true);
2589 ArgVal = CtorCGF.EmitLoadOfScalar(
John McCall7f416cc2015-09-08 08:05:57 +00002590 CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false,
Alexey Bataev97720002014-11-11 04:05:39 +00002591 CGM.getContext().VoidPtrTy, Dst.getLocation());
2592 CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue);
2593 CtorCGF.FinishFunction();
2594 Ctor = Fn;
2595 }
2596 if (VD->getType().isDestructedType() != QualType::DK_none) {
2597 // Generate function that emits destructor call for the threadprivate copy
2598 // of the variable VD
2599 CodeGenFunction DtorCGF(CGM);
2600 FunctionArgList Args;
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002601 ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, Loc,
2602 /*Id=*/nullptr, CGM.getContext().VoidPtrTy,
Alexey Bataev56223232017-06-09 13:40:18 +00002603 ImplicitParamDecl::Other);
Alexey Bataev97720002014-11-11 04:05:39 +00002604 Args.push_back(&Dst);
2605
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002606 const auto &FI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(
John McCallc56a8b32016-03-11 04:30:31 +00002607 CGM.getContext().VoidTy, Args);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002608 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
Alexey Bataev18fa2322018-05-02 14:20:50 +00002609 std::string Name = getName({"__kmpc_global_dtor_", ""});
2610 llvm::Function *Fn =
2611 CGM.CreateGlobalInitOrDestructFunction(FTy, Name, FI, Loc);
Adrian Prantl1858c662016-04-24 22:22:29 +00002612 auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF);
Alexey Bataev97720002014-11-11 04:05:39 +00002613 DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args,
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002614 Loc, Loc);
Adrian Prantl1858c662016-04-24 22:22:29 +00002615 // Create a scope with an artificial location for the body of this function.
2616 auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002617 llvm::Value *ArgVal = DtorCGF.EmitLoadOfScalar(
Alexey Bataev97720002014-11-11 04:05:39 +00002618 DtorCGF.GetAddrOfLocalVar(&Dst),
John McCall7f416cc2015-09-08 08:05:57 +00002619 /*Volatile=*/false, CGM.getContext().VoidPtrTy, Dst.getLocation());
2620 DtorCGF.emitDestroy(Address(ArgVal, VDAddr.getAlignment()), ASTTy,
Alexey Bataev97720002014-11-11 04:05:39 +00002621 DtorCGF.getDestroyer(ASTTy.isDestructedType()),
2622 DtorCGF.needsEHCleanup(ASTTy.isDestructedType()));
2623 DtorCGF.FinishFunction();
2624 Dtor = Fn;
2625 }
2626 // Do not emit init function if it is not required.
2627 if (!Ctor && !Dtor)
2628 return nullptr;
2629
2630 llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002631 auto *CopyCtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs,
2632 /*isVarArg=*/false)
2633 ->getPointerTo();
Alexey Bataev97720002014-11-11 04:05:39 +00002634 // Copying constructor for the threadprivate variable.
2635 // Must be NULL - reserved by runtime, but currently it requires that this
2636 // parameter is always NULL. Otherwise it fires assertion.
2637 CopyCtor = llvm::Constant::getNullValue(CopyCtorTy);
2638 if (Ctor == nullptr) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002639 auto *CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy,
2640 /*isVarArg=*/false)
2641 ->getPointerTo();
Alexey Bataev97720002014-11-11 04:05:39 +00002642 Ctor = llvm::Constant::getNullValue(CtorTy);
2643 }
2644 if (Dtor == nullptr) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002645 auto *DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy,
2646 /*isVarArg=*/false)
2647 ->getPointerTo();
Alexey Bataev97720002014-11-11 04:05:39 +00002648 Dtor = llvm::Constant::getNullValue(DtorTy);
2649 }
2650 if (!CGF) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002651 auto *InitFunctionTy =
Alexey Bataev97720002014-11-11 04:05:39 +00002652 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false);
Alexey Bataev18fa2322018-05-02 14:20:50 +00002653 std::string Name = getName({"__omp_threadprivate_init_", ""});
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002654 llvm::Function *InitFunction = CGM.CreateGlobalInitOrDestructFunction(
Alexey Bataev18fa2322018-05-02 14:20:50 +00002655 InitFunctionTy, Name, CGM.getTypes().arrangeNullaryFunction());
Alexey Bataev97720002014-11-11 04:05:39 +00002656 CodeGenFunction InitCGF(CGM);
2657 FunctionArgList ArgList;
2658 InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction,
2659 CGM.getTypes().arrangeNullaryFunction(), ArgList,
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002660 Loc, Loc);
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002661 emitThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00002662 InitCGF.FinishFunction();
2663 return InitFunction;
2664 }
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002665 emitThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc);
Alexey Bataev97720002014-11-11 04:05:39 +00002666 }
2667 return nullptr;
2668}
2669
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002670/// Obtain information that uniquely identifies a target entry. This
Alexey Bataev34f8a702018-03-28 14:28:54 +00002671/// consists of the file and device IDs as well as line number associated with
2672/// the relevant entry source location.
2673static void getTargetEntryUniqueInfo(ASTContext &C, SourceLocation Loc,
2674 unsigned &DeviceID, unsigned &FileID,
2675 unsigned &LineNum) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002676 SourceManager &SM = C.getSourceManager();
Alexey Bataev34f8a702018-03-28 14:28:54 +00002677
2678 // The loc should be always valid and have a file ID (the user cannot use
2679 // #pragma directives in macros)
2680
2681 assert(Loc.isValid() && "Source location is expected to be always valid.");
Alexey Bataev34f8a702018-03-28 14:28:54 +00002682
2683 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
2684 assert(PLoc.isValid() && "Source location is expected to be always valid.");
2685
2686 llvm::sys::fs::UniqueID ID;
Alexey Bataev64e62dc2018-04-30 16:26:57 +00002687 if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID))
2688 SM.getDiagnostics().Report(diag::err_cannot_open_file)
2689 << PLoc.getFilename() << EC.message();
Alexey Bataev34f8a702018-03-28 14:28:54 +00002690
2691 DeviceID = ID.getDevice();
2692 FileID = ID.getFile();
2693 LineNum = PLoc.getLine();
2694}
2695
2696bool CGOpenMPRuntime::emitDeclareTargetVarDefinition(const VarDecl *VD,
2697 llvm::GlobalVariable *Addr,
2698 bool PerformInit) {
2699 Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00002700 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataev34f8a702018-03-28 14:28:54 +00002701 if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link)
Alexey Bataevd01b7492018-08-15 19:45:12 +00002702 return CGM.getLangOpts().OpenMPIsDevice;
Alexey Bataev34f8a702018-03-28 14:28:54 +00002703 VD = VD->getDefinition(CGM.getContext());
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00002704 if (VD && !DeclareTargetWithDefinition.insert(CGM.getMangledName(VD)).second)
Alexey Bataev34f8a702018-03-28 14:28:54 +00002705 return CGM.getLangOpts().OpenMPIsDevice;
2706
2707 QualType ASTTy = VD->getType();
2708
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002709 SourceLocation Loc = VD->getCanonicalDecl()->getBeginLoc();
Alexey Bataev34f8a702018-03-28 14:28:54 +00002710 // Produce the unique prefix to identify the new target regions. We use
2711 // the source location of the variable declaration which we know to not
2712 // conflict with any target region.
2713 unsigned DeviceID;
2714 unsigned FileID;
2715 unsigned Line;
2716 getTargetEntryUniqueInfo(CGM.getContext(), Loc, DeviceID, FileID, Line);
2717 SmallString<128> Buffer, Out;
2718 {
2719 llvm::raw_svector_ostream OS(Buffer);
2720 OS << "__omp_offloading_" << llvm::format("_%x", DeviceID)
2721 << llvm::format("_%x_", FileID) << VD->getName() << "_l" << Line;
2722 }
2723
2724 const Expr *Init = VD->getAnyInitializer();
2725 if (CGM.getLangOpts().CPlusPlus && PerformInit) {
2726 llvm::Constant *Ctor;
2727 llvm::Constant *ID;
2728 if (CGM.getLangOpts().OpenMPIsDevice) {
2729 // Generate function that re-emits the declaration's initializer into
2730 // the threadprivate copy of the variable VD
2731 CodeGenFunction CtorCGF(CGM);
2732
2733 const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
2734 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
2735 llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction(
2736 FTy, Twine(Buffer, "_ctor"), FI, Loc);
2737 auto NL = ApplyDebugLocation::CreateEmpty(CtorCGF);
2738 CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI,
2739 FunctionArgList(), Loc, Loc);
2740 auto AL = ApplyDebugLocation::CreateArtificial(CtorCGF);
2741 CtorCGF.EmitAnyExprToMem(Init,
2742 Address(Addr, CGM.getContext().getDeclAlign(VD)),
2743 Init->getType().getQualifiers(),
2744 /*IsInitializer=*/true);
2745 CtorCGF.FinishFunction();
2746 Ctor = Fn;
2747 ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy);
Alexey Bataeve253f2f2018-05-09 14:15:18 +00002748 CGM.addUsedGlobal(cast<llvm::GlobalValue>(Ctor));
Alexey Bataev34f8a702018-03-28 14:28:54 +00002749 } else {
2750 Ctor = new llvm::GlobalVariable(
2751 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
2752 llvm::GlobalValue::PrivateLinkage,
2753 llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_ctor"));
2754 ID = Ctor;
2755 }
2756
2757 // Register the information for the entry associated with the constructor.
2758 Out.clear();
2759 OffloadEntriesInfoManager.registerTargetRegionEntryInfo(
2760 DeviceID, FileID, Twine(Buffer, "_ctor").toStringRef(Out), Line, Ctor,
Alexey Bataev03f270c2018-03-30 18:31:07 +00002761 ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryCtor);
Alexey Bataev34f8a702018-03-28 14:28:54 +00002762 }
2763 if (VD->getType().isDestructedType() != QualType::DK_none) {
2764 llvm::Constant *Dtor;
2765 llvm::Constant *ID;
2766 if (CGM.getLangOpts().OpenMPIsDevice) {
2767 // Generate function that emits destructor call for the threadprivate
2768 // copy of the variable VD
2769 CodeGenFunction DtorCGF(CGM);
2770
2771 const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction();
2772 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
2773 llvm::Function *Fn = CGM.CreateGlobalInitOrDestructFunction(
2774 FTy, Twine(Buffer, "_dtor"), FI, Loc);
2775 auto NL = ApplyDebugLocation::CreateEmpty(DtorCGF);
2776 DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI,
2777 FunctionArgList(), Loc, Loc);
2778 // Create a scope with an artificial location for the body of this
2779 // function.
2780 auto AL = ApplyDebugLocation::CreateArtificial(DtorCGF);
2781 DtorCGF.emitDestroy(Address(Addr, CGM.getContext().getDeclAlign(VD)),
2782 ASTTy, DtorCGF.getDestroyer(ASTTy.isDestructedType()),
2783 DtorCGF.needsEHCleanup(ASTTy.isDestructedType()));
2784 DtorCGF.FinishFunction();
2785 Dtor = Fn;
2786 ID = llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy);
Alexey Bataeve253f2f2018-05-09 14:15:18 +00002787 CGM.addUsedGlobal(cast<llvm::GlobalValue>(Dtor));
Alexey Bataev34f8a702018-03-28 14:28:54 +00002788 } else {
2789 Dtor = new llvm::GlobalVariable(
2790 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
2791 llvm::GlobalValue::PrivateLinkage,
2792 llvm::Constant::getNullValue(CGM.Int8Ty), Twine(Buffer, "_dtor"));
2793 ID = Dtor;
2794 }
2795 // Register the information for the entry associated with the destructor.
2796 Out.clear();
2797 OffloadEntriesInfoManager.registerTargetRegionEntryInfo(
2798 DeviceID, FileID, Twine(Buffer, "_dtor").toStringRef(Out), Line, Dtor,
Alexey Bataev03f270c2018-03-30 18:31:07 +00002799 ID, OffloadEntriesInfoManagerTy::OMPTargetRegionEntryDtor);
Alexey Bataev34f8a702018-03-28 14:28:54 +00002800 }
2801 return CGM.getLangOpts().OpenMPIsDevice;
2802}
2803
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002804Address CGOpenMPRuntime::getAddrOfArtificialThreadPrivate(CodeGenFunction &CGF,
2805 QualType VarType,
2806 StringRef Name) {
Alexey Bataev18fa2322018-05-02 14:20:50 +00002807 std::string Suffix = getName({"artificial", ""});
2808 std::string CacheSuffix = getName({"cache", ""});
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002809 llvm::Type *VarLVType = CGF.ConvertTypeForMem(VarType);
Alexey Bataev18fa2322018-05-02 14:20:50 +00002810 llvm::Value *GAddr =
2811 getOrCreateInternalVariable(VarLVType, Twine(Name).concat(Suffix));
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002812 llvm::Value *Args[] = {
2813 emitUpdateLocation(CGF, SourceLocation()),
2814 getThreadID(CGF, SourceLocation()),
2815 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(GAddr, CGM.VoidPtrTy),
2816 CGF.Builder.CreateIntCast(CGF.getTypeSize(VarType), CGM.SizeTy,
2817 /*IsSigned=*/false),
Alexey Bataev18fa2322018-05-02 14:20:50 +00002818 getOrCreateInternalVariable(
2819 CGM.VoidPtrPtrTy, Twine(Name).concat(Suffix).concat(CacheSuffix))};
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002820 return Address(
2821 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
2822 CGF.EmitRuntimeCall(
2823 createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args),
2824 VarLVType->getPointerTo(/*AddrSpace=*/0)),
2825 CGM.getPointerAlign());
2826}
2827
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00002828void CGOpenMPRuntime::emitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond,
2829 const RegionCodeGenTy &ThenGen,
2830 const RegionCodeGenTy &ElseGen) {
Alexey Bataev1d677132015-04-22 13:57:31 +00002831 CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange());
2832
2833 // If the condition constant folds and can be elided, try to avoid emitting
2834 // the condition and the dead arm of the if/else.
2835 bool CondConstant;
2836 if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002837 if (CondConstant)
Alexey Bataev1d677132015-04-22 13:57:31 +00002838 ThenGen(CGF);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002839 else
Alexey Bataev1d677132015-04-22 13:57:31 +00002840 ElseGen(CGF);
Alexey Bataev1d677132015-04-22 13:57:31 +00002841 return;
2842 }
2843
2844 // Otherwise, the condition did not fold, or we couldn't elide it. Just
2845 // emit the conditional branch.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002846 llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("omp_if.then");
2847 llvm::BasicBlock *ElseBlock = CGF.createBasicBlock("omp_if.else");
2848 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("omp_if.end");
Alexey Bataev1d677132015-04-22 13:57:31 +00002849 CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount=*/0);
2850
2851 // Emit the 'then' code.
2852 CGF.EmitBlock(ThenBlock);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002853 ThenGen(CGF);
Alexey Bataev1d677132015-04-22 13:57:31 +00002854 CGF.EmitBranch(ContBlock);
2855 // Emit the 'else' code if present.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002856 // There is no need to emit line number for unconditional branch.
2857 (void)ApplyDebugLocation::CreateEmpty(CGF);
2858 CGF.EmitBlock(ElseBlock);
2859 ElseGen(CGF);
2860 // There is no need to emit line number for unconditional branch.
2861 (void)ApplyDebugLocation::CreateEmpty(CGF);
2862 CGF.EmitBranch(ContBlock);
Alexey Bataev1d677132015-04-22 13:57:31 +00002863 // Emit the continuation block for code after the if.
2864 CGF.EmitBlock(ContBlock, /*IsFinished=*/true);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00002865}
2866
Alexey Bataev1d677132015-04-22 13:57:31 +00002867void CGOpenMPRuntime::emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00002868 llvm::Function *OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +00002869 ArrayRef<llvm::Value *> CapturedVars,
Alexey Bataev1d677132015-04-22 13:57:31 +00002870 const Expr *IfCond) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002871 if (!CGF.HaveInsertPoint())
2872 return;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002873 llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002874 auto &&ThenGen = [OutlinedFn, CapturedVars, RTLoc](CodeGenFunction &CGF,
2875 PrePostActionTy &) {
Alexey Bataev2377fe92015-09-10 08:12:02 +00002876 // Build call __kmpc_fork_call(loc, n, microtask, var1, .., varn);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002877 CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime();
Alexey Bataev2377fe92015-09-10 08:12:02 +00002878 llvm::Value *Args[] = {
2879 RTLoc,
2880 CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002881 CGF.Builder.CreateBitCast(OutlinedFn, RT.getKmpc_MicroPointerTy())};
Alexey Bataev2377fe92015-09-10 08:12:02 +00002882 llvm::SmallVector<llvm::Value *, 16> RealArgs;
2883 RealArgs.append(std::begin(Args), std::end(Args));
2884 RealArgs.append(CapturedVars.begin(), CapturedVars.end());
2885
James Y Knight9871db02019-02-05 16:42:33 +00002886 llvm::FunctionCallee RTLFn =
2887 RT.createRuntimeFunction(OMPRTL__kmpc_fork_call);
Alexey Bataev2377fe92015-09-10 08:12:02 +00002888 CGF.EmitRuntimeCall(RTLFn, RealArgs);
2889 };
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002890 auto &&ElseGen = [OutlinedFn, CapturedVars, RTLoc, Loc](CodeGenFunction &CGF,
2891 PrePostActionTy &) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002892 CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime();
2893 llvm::Value *ThreadID = RT.getThreadID(CGF, Loc);
Alexey Bataev1d677132015-04-22 13:57:31 +00002894 // Build calls:
2895 // __kmpc_serialized_parallel(&Loc, GTid);
2896 llvm::Value *Args[] = {RTLoc, ThreadID};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002897 CGF.EmitRuntimeCall(
2898 RT.createRuntimeFunction(OMPRTL__kmpc_serialized_parallel), Args);
Alexey Bataevd74d0602014-10-13 06:02:40 +00002899
Alexey Bataev1d677132015-04-22 13:57:31 +00002900 // OutlinedFn(&GTid, &zero, CapturedStruct);
Alexey Bataev18fa2322018-05-02 14:20:50 +00002901 Address ZeroAddr = CGF.CreateDefaultAlignTempAlloca(CGF.Int32Ty,
2902 /*Name*/ ".zero.addr");
Alexey Bataev1d677132015-04-22 13:57:31 +00002903 CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
Alexey Bataev2377fe92015-09-10 08:12:02 +00002904 llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs;
Alexey Bataev8521ff62018-07-25 20:03:01 +00002905 // ThreadId for serialized parallels is 0.
2906 OutlinedFnArgs.push_back(ZeroAddr.getPointer());
Alexey Bataev2377fe92015-09-10 08:12:02 +00002907 OutlinedFnArgs.push_back(ZeroAddr.getPointer());
2908 OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end());
Alexey Bataev3c595a62017-08-14 15:01:03 +00002909 RT.emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs);
Alexey Bataevd74d0602014-10-13 06:02:40 +00002910
Alexey Bataev1d677132015-04-22 13:57:31 +00002911 // __kmpc_end_serialized_parallel(&Loc, GTid);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002912 llvm::Value *EndArgs[] = {RT.emitUpdateLocation(CGF, Loc), ThreadID};
Alexey Bataev1d677132015-04-22 13:57:31 +00002913 CGF.EmitRuntimeCall(
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002914 RT.createRuntimeFunction(OMPRTL__kmpc_end_serialized_parallel),
2915 EndArgs);
Alexey Bataev1d677132015-04-22 13:57:31 +00002916 };
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002917 if (IfCond) {
Alexey Bataev1d677132015-04-22 13:57:31 +00002918 emitOMPIfClause(CGF, IfCond, ThenGen, ElseGen);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002919 } else {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002920 RegionCodeGenTy ThenRCG(ThenGen);
2921 ThenRCG(CGF);
Alexey Bataevf539faa2016-03-28 12:58:34 +00002922 }
Alexey Bataevd74d0602014-10-13 06:02:40 +00002923}
2924
NAKAMURA Takumi59c74b222014-10-27 08:08:18 +00002925// If we're inside an (outlined) parallel region, use the region info's
Alexey Bataevd74d0602014-10-13 06:02:40 +00002926// thread-ID variable (it is passed in a first argument of the outlined function
2927// as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in
2928// regular serial code region, get thread ID by calling kmp_int32
2929// kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and
2930// return the address of that temp.
John McCall7f416cc2015-09-08 08:05:57 +00002931Address CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF,
2932 SourceLocation Loc) {
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002933 if (auto *OMPRegionInfo =
Alexey Bataevd74d0602014-10-13 06:02:40 +00002934 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00002935 if (OMPRegionInfo->getThreadIDVariable())
Alexey Bataev62b63b12015-03-10 07:28:44 +00002936 return OMPRegionInfo->getThreadIDVariableLValue(CGF).getAddress();
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00002937
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002938 llvm::Value *ThreadID = getThreadID(CGF, Loc);
2939 QualType Int32Ty =
Alexey Bataevd74d0602014-10-13 06:02:40 +00002940 CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002941 Address ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp.");
Alexey Bataevd74d0602014-10-13 06:02:40 +00002942 CGF.EmitStoreOfScalar(ThreadID,
John McCall7f416cc2015-09-08 08:05:57 +00002943 CGF.MakeAddrLValue(ThreadIDTemp, Int32Ty));
Alexey Bataevd74d0602014-10-13 06:02:40 +00002944
2945 return ThreadIDTemp;
2946}
2947
Alexey Bataev97720002014-11-11 04:05:39 +00002948llvm::Constant *
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002949CGOpenMPRuntime::getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +00002950 const llvm::Twine &Name) {
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002951 SmallString<256> Buffer;
2952 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev97720002014-11-11 04:05:39 +00002953 Out << Name;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00002954 StringRef RuntimeName = Out.str();
Alexey Bataev43a919f2018-04-13 17:48:43 +00002955 auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first;
David Blaikie13156b62014-11-19 03:06:06 +00002956 if (Elem.second) {
2957 assert(Elem.second->getType()->getPointerElementType() == Ty &&
Alexey Bataev97720002014-11-11 04:05:39 +00002958 "OMP internal variable has different type than requested");
David Blaikie13156b62014-11-19 03:06:06 +00002959 return &*Elem.second;
Alexey Bataev97720002014-11-11 04:05:39 +00002960 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002961
David Blaikie13156b62014-11-19 03:06:06 +00002962 return Elem.second = new llvm::GlobalVariable(
2963 CGM.getModule(), Ty, /*IsConstant*/ false,
2964 llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty),
2965 Elem.first());
Alexey Bataev97720002014-11-11 04:05:39 +00002966}
2967
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002968llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) {
Alexey Bataev18fa2322018-05-02 14:20:50 +00002969 std::string Prefix = Twine("gomp_critical_user_", CriticalName).str();
2970 std::string Name = getName({Prefix, "var"});
2971 return getOrCreateInternalVariable(KmpCriticalNameTy, Name);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002972}
2973
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002974namespace {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002975/// Common pre(post)-action for different OpenMP constructs.
2976class CommonActionTy final : public PrePostActionTy {
James Y Knight9871db02019-02-05 16:42:33 +00002977 llvm::FunctionCallee EnterCallee;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002978 ArrayRef<llvm::Value *> EnterArgs;
James Y Knight9871db02019-02-05 16:42:33 +00002979 llvm::FunctionCallee ExitCallee;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002980 ArrayRef<llvm::Value *> ExitArgs;
2981 bool Conditional;
2982 llvm::BasicBlock *ContBlock = nullptr;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002983
2984public:
James Y Knight9871db02019-02-05 16:42:33 +00002985 CommonActionTy(llvm::FunctionCallee EnterCallee,
2986 ArrayRef<llvm::Value *> EnterArgs,
2987 llvm::FunctionCallee ExitCallee,
2988 ArrayRef<llvm::Value *> ExitArgs, bool Conditional = false)
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002989 : EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee),
2990 ExitArgs(ExitArgs), Conditional(Conditional) {}
2991 void Enter(CodeGenFunction &CGF) override {
2992 llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs);
2993 if (Conditional) {
2994 llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes);
2995 auto *ThenBlock = CGF.createBasicBlock("omp_if.then");
2996 ContBlock = CGF.createBasicBlock("omp_if.end");
2997 // Generate the branch (If-stmt)
2998 CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock);
2999 CGF.EmitBlock(ThenBlock);
3000 }
Alexey Bataeva744ff52015-05-05 09:24:37 +00003001 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003002 void Done(CodeGenFunction &CGF) {
3003 // Emit the rest of blocks/branches
3004 CGF.EmitBranch(ContBlock);
3005 CGF.EmitBlock(ContBlock, true);
3006 }
3007 void Exit(CodeGenFunction &CGF) override {
3008 CGF.EmitRuntimeCall(ExitCallee, ExitArgs);
Alexey Bataev3e6124b2015-04-10 07:48:12 +00003009 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00003010};
Hans Wennborg7eb54642015-09-10 17:07:54 +00003011} // anonymous namespace
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00003012
3013void CGOpenMPRuntime::emitCriticalRegion(CodeGenFunction &CGF,
3014 StringRef CriticalName,
3015 const RegionCodeGenTy &CriticalOpGen,
Alexey Bataevfc57d162015-12-15 10:55:09 +00003016 SourceLocation Loc, const Expr *Hint) {
3017 // __kmpc_critical[_with_hint](ident_t *, gtid, Lock[, hint]);
Alexey Bataev75ddfab2014-12-01 11:32:38 +00003018 // CriticalOpGen();
3019 // __kmpc_end_critical(ident_t *, gtid, Lock);
3020 // Prepare arguments and build a call to __kmpc_critical
Alexey Bataev8ef31412015-12-18 07:58:25 +00003021 if (!CGF.HaveInsertPoint())
3022 return;
Alexey Bataevfc57d162015-12-15 10:55:09 +00003023 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
3024 getCriticalRegionLock(CriticalName)};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003025 llvm::SmallVector<llvm::Value *, 4> EnterArgs(std::begin(Args),
3026 std::end(Args));
Alexey Bataevfc57d162015-12-15 10:55:09 +00003027 if (Hint) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003028 EnterArgs.push_back(CGF.Builder.CreateIntCast(
3029 CGF.EmitScalarExpr(Hint), CGM.IntPtrTy, /*isSigned=*/false));
3030 }
3031 CommonActionTy Action(
3032 createRuntimeFunction(Hint ? OMPRTL__kmpc_critical_with_hint
3033 : OMPRTL__kmpc_critical),
3034 EnterArgs, createRuntimeFunction(OMPRTL__kmpc_end_critical), Args);
3035 CriticalOpGen.setAction(Action);
Alexey Bataevfc57d162015-12-15 10:55:09 +00003036 emitInlinedDirective(CGF, OMPD_critical, CriticalOpGen);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00003037}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00003038
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003039void CGOpenMPRuntime::emitMasterRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00003040 const RegionCodeGenTy &MasterOpGen,
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003041 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003042 if (!CGF.HaveInsertPoint())
3043 return;
Alexey Bataev8d690652014-12-04 07:23:53 +00003044 // if(__kmpc_master(ident_t *, gtid)) {
3045 // MasterOpGen();
3046 // __kmpc_end_master(ident_t *, gtid);
3047 // }
3048 // Prepare arguments and build a call to __kmpc_master
Alexey Bataevd7614fb2015-04-10 06:33:45 +00003049 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003050 CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_master), Args,
3051 createRuntimeFunction(OMPRTL__kmpc_end_master), Args,
3052 /*Conditional=*/true);
3053 MasterOpGen.setAction(Action);
3054 emitInlinedDirective(CGF, OMPD_master, MasterOpGen);
3055 Action.Done(CGF);
Alexey Bataev8d690652014-12-04 07:23:53 +00003056}
3057
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003058void CGOpenMPRuntime::emitTaskyieldCall(CodeGenFunction &CGF,
3059 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003060 if (!CGF.HaveInsertPoint())
3061 return;
Alexey Bataev9f797f32015-02-05 05:57:51 +00003062 // Build call __kmpc_omp_taskyield(loc, thread_id, 0);
3063 llvm::Value *Args[] = {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003064 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
Alexey Bataev9f797f32015-02-05 05:57:51 +00003065 llvm::ConstantInt::get(CGM.IntTy, /*V=*/0, /*isSigned=*/true)};
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003066 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskyield), Args);
Alexey Bataev48591dd2016-04-20 04:01:36 +00003067 if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
3068 Region->emitUntiedSwitch(CGF);
Alexey Bataev9f797f32015-02-05 05:57:51 +00003069}
3070
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003071void CGOpenMPRuntime::emitTaskgroupRegion(CodeGenFunction &CGF,
3072 const RegionCodeGenTy &TaskgroupOpGen,
3073 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003074 if (!CGF.HaveInsertPoint())
3075 return;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003076 // __kmpc_taskgroup(ident_t *, gtid);
3077 // TaskgroupOpGen();
3078 // __kmpc_end_taskgroup(ident_t *, gtid);
3079 // Prepare arguments and build a call to __kmpc_taskgroup
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003080 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
3081 CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_taskgroup), Args,
3082 createRuntimeFunction(OMPRTL__kmpc_end_taskgroup),
3083 Args);
3084 TaskgroupOpGen.setAction(Action);
3085 emitInlinedDirective(CGF, OMPD_taskgroup, TaskgroupOpGen);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003086}
3087
John McCall7f416cc2015-09-08 08:05:57 +00003088/// Given an array of pointers to variables, project the address of a
3089/// given variable.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00003090static Address emitAddrOfVarFromArray(CodeGenFunction &CGF, Address Array,
3091 unsigned Index, const VarDecl *Var) {
John McCall7f416cc2015-09-08 08:05:57 +00003092 // Pull out the pointer to the variable.
3093 Address PtrAddr =
Alexey Bataevf24e7b12015-10-08 09:10:53 +00003094 CGF.Builder.CreateConstArrayGEP(Array, Index, CGF.getPointerSize());
John McCall7f416cc2015-09-08 08:05:57 +00003095 llvm::Value *Ptr = CGF.Builder.CreateLoad(PtrAddr);
3096
3097 Address Addr = Address(Ptr, CGF.getContext().getDeclAlign(Var));
Alexey Bataevf24e7b12015-10-08 09:10:53 +00003098 Addr = CGF.Builder.CreateElementBitCast(
3099 Addr, CGF.ConvertTypeForMem(Var->getType()));
John McCall7f416cc2015-09-08 08:05:57 +00003100 return Addr;
3101}
3102
Alexey Bataeva63048e2015-03-23 06:18:07 +00003103static llvm::Value *emitCopyprivateCopyFunction(
Alexey Bataev420d45b2015-04-14 05:11:24 +00003104 CodeGenModule &CGM, llvm::Type *ArgsType,
3105 ArrayRef<const Expr *> CopyprivateVars, ArrayRef<const Expr *> DestExprs,
Alexey Bataev7cae94e2018-01-04 19:45:16 +00003106 ArrayRef<const Expr *> SrcExprs, ArrayRef<const Expr *> AssignmentOps,
3107 SourceLocation Loc) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003108 ASTContext &C = CGM.getContext();
Alexey Bataeva63048e2015-03-23 06:18:07 +00003109 // void copy_func(void *LHSArg, void *RHSArg);
3110 FunctionArgList Args;
Alexey Bataev7cae94e2018-01-04 19:45:16 +00003111 ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
3112 ImplicitParamDecl::Other);
3113 ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
3114 ImplicitParamDecl::Other);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003115 Args.push_back(&LHSArg);
3116 Args.push_back(&RHSArg);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003117 const auto &CGFI =
3118 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataev18fa2322018-05-02 14:20:50 +00003119 std::string Name =
3120 CGM.getOpenMPRuntime().getName({"omp", "copyprivate", "copy_func"});
3121 auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI),
3122 llvm::GlobalValue::InternalLinkage, Name,
3123 &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00003124 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003125 Fn->setDoesNotRecurse();
Alexey Bataeva63048e2015-03-23 06:18:07 +00003126 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00003127 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00003128 // Dest = (void*[n])(LHSArg);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003129 // Src = (void*[n])(RHSArg);
John McCall7f416cc2015-09-08 08:05:57 +00003130 Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
3131 CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)),
3132 ArgsType), CGF.getPointerAlign());
3133 Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
3134 CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)),
3135 ArgsType), CGF.getPointerAlign());
Alexey Bataeva63048e2015-03-23 06:18:07 +00003136 // *(Type0*)Dst[0] = *(Type0*)Src[0];
3137 // *(Type1*)Dst[1] = *(Type1*)Src[1];
3138 // ...
3139 // *(Typen*)Dst[n] = *(Typen*)Src[n];
Alexey Bataeva63048e2015-03-23 06:18:07 +00003140 for (unsigned I = 0, E = AssignmentOps.size(); I < E; ++I) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003141 const auto *DestVar =
3142 cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +00003143 Address DestAddr = emitAddrOfVarFromArray(CGF, LHS, I, DestVar);
3144
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003145 const auto *SrcVar =
3146 cast<VarDecl>(cast<DeclRefExpr>(SrcExprs[I])->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +00003147 Address SrcAddr = emitAddrOfVarFromArray(CGF, RHS, I, SrcVar);
3148
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003149 const auto *VD = cast<DeclRefExpr>(CopyprivateVars[I])->getDecl();
Alexey Bataev1d9c15c2015-05-19 12:31:28 +00003150 QualType Type = VD->getType();
John McCall7f416cc2015-09-08 08:05:57 +00003151 CGF.EmitOMPCopy(Type, DestAddr, SrcAddr, DestVar, SrcVar, AssignmentOps[I]);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003152 }
Alexey Bataeva63048e2015-03-23 06:18:07 +00003153 CGF.FinishFunction();
3154 return Fn;
3155}
3156
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003157void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00003158 const RegionCodeGenTy &SingleOpGen,
Alexey Bataeva63048e2015-03-23 06:18:07 +00003159 SourceLocation Loc,
3160 ArrayRef<const Expr *> CopyprivateVars,
3161 ArrayRef<const Expr *> SrcExprs,
3162 ArrayRef<const Expr *> DstExprs,
3163 ArrayRef<const Expr *> AssignmentOps) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003164 if (!CGF.HaveInsertPoint())
3165 return;
Alexey Bataeva63048e2015-03-23 06:18:07 +00003166 assert(CopyprivateVars.size() == SrcExprs.size() &&
3167 CopyprivateVars.size() == DstExprs.size() &&
3168 CopyprivateVars.size() == AssignmentOps.size());
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003169 ASTContext &C = CGM.getContext();
Alexey Bataeva63048e2015-03-23 06:18:07 +00003170 // int32 did_it = 0;
Alexey Bataev6956e2e2015-02-05 06:35:41 +00003171 // if(__kmpc_single(ident_t *, gtid)) {
3172 // SingleOpGen();
3173 // __kmpc_end_single(ident_t *, gtid);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003174 // did_it = 1;
Alexey Bataev6956e2e2015-02-05 06:35:41 +00003175 // }
Alexey Bataeva63048e2015-03-23 06:18:07 +00003176 // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>,
3177 // <copy_func>, did_it);
3178
John McCall7f416cc2015-09-08 08:05:57 +00003179 Address DidIt = Address::invalid();
Alexey Bataeva63048e2015-03-23 06:18:07 +00003180 if (!CopyprivateVars.empty()) {
3181 // int32 did_it = 0;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003182 QualType KmpInt32Ty =
3183 C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003184 DidIt = CGF.CreateMemTemp(KmpInt32Ty, ".omp.copyprivate.did_it");
John McCall7f416cc2015-09-08 08:05:57 +00003185 CGF.Builder.CreateStore(CGF.Builder.getInt32(0), DidIt);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003186 }
Alexey Bataev6956e2e2015-02-05 06:35:41 +00003187 // Prepare arguments and build a call to __kmpc_single
Alexey Bataevd7614fb2015-04-10 06:33:45 +00003188 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003189 CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_single), Args,
3190 createRuntimeFunction(OMPRTL__kmpc_end_single), Args,
3191 /*Conditional=*/true);
3192 SingleOpGen.setAction(Action);
3193 emitInlinedDirective(CGF, OMPD_single, SingleOpGen);
3194 if (DidIt.isValid()) {
3195 // did_it = 1;
3196 CGF.Builder.CreateStore(CGF.Builder.getInt32(1), DidIt);
3197 }
3198 Action.Done(CGF);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003199 // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>,
3200 // <copy_func>, did_it);
John McCall7f416cc2015-09-08 08:05:57 +00003201 if (DidIt.isValid()) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00003202 llvm::APInt ArraySize(/*unsigned int numBits=*/32, CopyprivateVars.size());
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003203 QualType CopyprivateArrayTy =
Alexey Bataeva63048e2015-03-23 06:18:07 +00003204 C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal,
3205 /*IndexTypeQuals=*/0);
3206 // Create a list of all private variables for copyprivate.
John McCall7f416cc2015-09-08 08:05:57 +00003207 Address CopyprivateList =
Alexey Bataeva63048e2015-03-23 06:18:07 +00003208 CGF.CreateMemTemp(CopyprivateArrayTy, ".omp.copyprivate.cpr_list");
3209 for (unsigned I = 0, E = CopyprivateVars.size(); I < E; ++I) {
John McCall7f416cc2015-09-08 08:05:57 +00003210 Address Elem = CGF.Builder.CreateConstArrayGEP(
3211 CopyprivateList, I, CGF.getPointerSize());
3212 CGF.Builder.CreateStore(
Alexey Bataeva63048e2015-03-23 06:18:07 +00003213 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
John McCall7f416cc2015-09-08 08:05:57 +00003214 CGF.EmitLValue(CopyprivateVars[I]).getPointer(), CGF.VoidPtrTy),
3215 Elem);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003216 }
3217 // Build function that copies private values from single region to all other
3218 // threads in the corresponding parallel region.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003219 llvm::Value *CpyFn = emitCopyprivateCopyFunction(
Alexey Bataeva63048e2015-03-23 06:18:07 +00003220 CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy)->getPointerTo(),
Alexey Bataev7cae94e2018-01-04 19:45:16 +00003221 CopyprivateVars, SrcExprs, DstExprs, AssignmentOps, Loc);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003222 llvm::Value *BufSize = CGF.getTypeSize(CopyprivateArrayTy);
John McCall7f416cc2015-09-08 08:05:57 +00003223 Address CL =
3224 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(CopyprivateList,
3225 CGF.VoidPtrTy);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003226 llvm::Value *DidItVal = CGF.Builder.CreateLoad(DidIt);
Alexey Bataeva63048e2015-03-23 06:18:07 +00003227 llvm::Value *Args[] = {
3228 emitUpdateLocation(CGF, Loc), // ident_t *<loc>
3229 getThreadID(CGF, Loc), // i32 <gtid>
Alexey Bataev66beaa92015-04-30 03:47:32 +00003230 BufSize, // size_t <buf_size>
John McCall7f416cc2015-09-08 08:05:57 +00003231 CL.getPointer(), // void *<copyprivate list>
Alexey Bataeva63048e2015-03-23 06:18:07 +00003232 CpyFn, // void (*) (void *, void *) <copy_func>
3233 DidItVal // i32 did_it
3234 };
3235 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_copyprivate), Args);
3236 }
Alexey Bataev6956e2e2015-02-05 06:35:41 +00003237}
3238
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003239void CGOpenMPRuntime::emitOrderedRegion(CodeGenFunction &CGF,
3240 const RegionCodeGenTy &OrderedOpGen,
Alexey Bataev5f600d62015-09-29 03:48:57 +00003241 SourceLocation Loc, bool IsThreads) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003242 if (!CGF.HaveInsertPoint())
3243 return;
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003244 // __kmpc_ordered(ident_t *, gtid);
3245 // OrderedOpGen();
3246 // __kmpc_end_ordered(ident_t *, gtid);
3247 // Prepare arguments and build a call to __kmpc_ordered
Alexey Bataev5f600d62015-09-29 03:48:57 +00003248 if (IsThreads) {
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003249 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003250 CommonActionTy Action(createRuntimeFunction(OMPRTL__kmpc_ordered), Args,
3251 createRuntimeFunction(OMPRTL__kmpc_end_ordered),
3252 Args);
3253 OrderedOpGen.setAction(Action);
3254 emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen);
3255 return;
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003256 }
Alexey Bataev5f600d62015-09-29 03:48:57 +00003257 emitInlinedDirective(CGF, OMPD_ordered, OrderedOpGen);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003258}
3259
Alexey Bataevc3028ca2018-12-04 15:03:25 +00003260unsigned CGOpenMPRuntime::getDefaultFlagsForBarriers(OpenMPDirectiveKind Kind) {
Alexey Bataev50b3c952016-02-19 10:38:26 +00003261 unsigned Flags;
3262 if (Kind == OMPD_for)
3263 Flags = OMP_IDENT_BARRIER_IMPL_FOR;
3264 else if (Kind == OMPD_sections)
3265 Flags = OMP_IDENT_BARRIER_IMPL_SECTIONS;
3266 else if (Kind == OMPD_single)
3267 Flags = OMP_IDENT_BARRIER_IMPL_SINGLE;
3268 else if (Kind == OMPD_barrier)
3269 Flags = OMP_IDENT_BARRIER_EXPL;
3270 else
3271 Flags = OMP_IDENT_BARRIER_IMPL;
Alexey Bataevc3028ca2018-12-04 15:03:25 +00003272 return Flags;
3273}
3274
3275void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
3276 OpenMPDirectiveKind Kind, bool EmitChecks,
3277 bool ForceSimpleCall) {
3278 if (!CGF.HaveInsertPoint())
3279 return;
3280 // Build call __kmpc_cancel_barrier(loc, thread_id);
3281 // Build call __kmpc_barrier(loc, thread_id);
3282 unsigned Flags = getDefaultFlagsForBarriers(Kind);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003283 // Build call __kmpc_cancel_barrier(loc, thread_id) or __kmpc_barrier(loc,
3284 // thread_id);
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003285 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, Flags),
3286 getThreadID(CGF, Loc)};
Alexey Bataev3015bcc2016-01-22 08:56:50 +00003287 if (auto *OMPRegionInfo =
3288 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
Alexey Bataev25e5b442015-09-15 12:52:43 +00003289 if (!ForceSimpleCall && OMPRegionInfo->hasCancel()) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003290 llvm::Value *Result = CGF.EmitRuntimeCall(
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003291 createRuntimeFunction(OMPRTL__kmpc_cancel_barrier), Args);
Alexey Bataev25e5b442015-09-15 12:52:43 +00003292 if (EmitChecks) {
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003293 // if (__kmpc_cancel_barrier()) {
3294 // exit from construct;
3295 // }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003296 llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit");
3297 llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue");
3298 llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003299 CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB);
3300 CGF.EmitBlock(ExitBB);
3301 // exit from construct;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003302 CodeGenFunction::JumpDest CancelDestination =
Alexey Bataev25e5b442015-09-15 12:52:43 +00003303 CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind());
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003304 CGF.EmitBranchThroughCleanup(CancelDestination);
3305 CGF.EmitBlock(ContBB, /*IsFinished=*/true);
3306 }
3307 return;
3308 }
3309 }
3310 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_barrier), Args);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00003311}
3312
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003313/// Map the OpenMP loop schedule to the runtime enumeration.
Alexander Musmanc6388682014-12-15 07:07:06 +00003314static OpenMPSchedType getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003315 bool Chunked, bool Ordered) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003316 switch (ScheduleKind) {
3317 case OMPC_SCHEDULE_static:
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003318 return Chunked ? (Ordered ? OMP_ord_static_chunked : OMP_sch_static_chunked)
3319 : (Ordered ? OMP_ord_static : OMP_sch_static);
Alexander Musmanc6388682014-12-15 07:07:06 +00003320 case OMPC_SCHEDULE_dynamic:
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003321 return Ordered ? OMP_ord_dynamic_chunked : OMP_sch_dynamic_chunked;
Alexander Musmanc6388682014-12-15 07:07:06 +00003322 case OMPC_SCHEDULE_guided:
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003323 return Ordered ? OMP_ord_guided_chunked : OMP_sch_guided_chunked;
Alexander Musmanc6388682014-12-15 07:07:06 +00003324 case OMPC_SCHEDULE_runtime:
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003325 return Ordered ? OMP_ord_runtime : OMP_sch_runtime;
3326 case OMPC_SCHEDULE_auto:
3327 return Ordered ? OMP_ord_auto : OMP_sch_auto;
Alexander Musmanc6388682014-12-15 07:07:06 +00003328 case OMPC_SCHEDULE_unknown:
3329 assert(!Chunked && "chunk was specified but schedule kind not known");
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003330 return Ordered ? OMP_ord_static : OMP_sch_static;
Alexander Musmanc6388682014-12-15 07:07:06 +00003331 }
3332 llvm_unreachable("Unexpected runtime schedule");
3333}
3334
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003335/// Map the OpenMP distribute schedule to the runtime enumeration.
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003336static OpenMPSchedType
3337getRuntimeSchedule(OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) {
3338 // only static is allowed for dist_schedule
3339 return Chunked ? OMP_dist_sch_static_chunked : OMP_dist_sch_static;
3340}
3341
Alexander Musmanc6388682014-12-15 07:07:06 +00003342bool CGOpenMPRuntime::isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
3343 bool Chunked) const {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003344 OpenMPSchedType Schedule =
3345 getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false);
Alexander Musmanc6388682014-12-15 07:07:06 +00003346 return Schedule == OMP_sch_static;
3347}
3348
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003349bool CGOpenMPRuntime::isStaticNonchunked(
3350 OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003351 OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunked);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003352 return Schedule == OMP_dist_sch_static;
3353}
3354
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00003355bool CGOpenMPRuntime::isStaticChunked(OpenMPScheduleClauseKind ScheduleKind,
3356 bool Chunked) const {
3357 OpenMPSchedType Schedule =
3358 getRuntimeSchedule(ScheduleKind, Chunked, /*Ordered=*/false);
3359 return Schedule == OMP_sch_static_chunked;
3360}
3361
3362bool CGOpenMPRuntime::isStaticChunked(
3363 OpenMPDistScheduleClauseKind ScheduleKind, bool Chunked) const {
3364 OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunked);
3365 return Schedule == OMP_dist_sch_static_chunked;
3366}
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003367
Alexander Musmandf7a8e22015-01-22 08:49:35 +00003368bool CGOpenMPRuntime::isDynamic(OpenMPScheduleClauseKind ScheduleKind) const {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003369 OpenMPSchedType Schedule =
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003370 getRuntimeSchedule(ScheduleKind, /*Chunked=*/false, /*Ordered=*/false);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00003371 assert(Schedule != OMP_sch_static_chunked && "cannot be chunked here");
3372 return Schedule != OMP_sch_static;
3373}
3374
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003375static int addMonoNonMonoModifier(OpenMPSchedType Schedule,
3376 OpenMPScheduleClauseModifier M1,
3377 OpenMPScheduleClauseModifier M2) {
Alexey Bataev6cff6242016-05-30 13:05:14 +00003378 int Modifier = 0;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003379 switch (M1) {
3380 case OMPC_SCHEDULE_MODIFIER_monotonic:
Alexey Bataev6cff6242016-05-30 13:05:14 +00003381 Modifier = OMP_sch_modifier_monotonic;
3382 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003383 case OMPC_SCHEDULE_MODIFIER_nonmonotonic:
Alexey Bataev6cff6242016-05-30 13:05:14 +00003384 Modifier = OMP_sch_modifier_nonmonotonic;
3385 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003386 case OMPC_SCHEDULE_MODIFIER_simd:
Alexey Bataev6cff6242016-05-30 13:05:14 +00003387 if (Schedule == OMP_sch_static_chunked)
3388 Schedule = OMP_sch_static_balanced_chunked;
3389 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003390 case OMPC_SCHEDULE_MODIFIER_last:
3391 case OMPC_SCHEDULE_MODIFIER_unknown:
3392 break;
3393 }
3394 switch (M2) {
3395 case OMPC_SCHEDULE_MODIFIER_monotonic:
Alexey Bataev6cff6242016-05-30 13:05:14 +00003396 Modifier = OMP_sch_modifier_monotonic;
3397 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003398 case OMPC_SCHEDULE_MODIFIER_nonmonotonic:
Alexey Bataev6cff6242016-05-30 13:05:14 +00003399 Modifier = OMP_sch_modifier_nonmonotonic;
3400 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003401 case OMPC_SCHEDULE_MODIFIER_simd:
Alexey Bataev6cff6242016-05-30 13:05:14 +00003402 if (Schedule == OMP_sch_static_chunked)
3403 Schedule = OMP_sch_static_balanced_chunked;
3404 break;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003405 case OMPC_SCHEDULE_MODIFIER_last:
3406 case OMPC_SCHEDULE_MODIFIER_unknown:
3407 break;
3408 }
Alexey Bataev6cff6242016-05-30 13:05:14 +00003409 return Schedule | Modifier;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003410}
3411
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003412void CGOpenMPRuntime::emitForDispatchInit(
3413 CodeGenFunction &CGF, SourceLocation Loc,
3414 const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned,
3415 bool Ordered, const DispatchRTInput &DispatchValues) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003416 if (!CGF.HaveInsertPoint())
3417 return;
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003418 OpenMPSchedType Schedule = getRuntimeSchedule(
3419 ScheduleKind.Schedule, DispatchValues.Chunk != nullptr, Ordered);
John McCall7f416cc2015-09-08 08:05:57 +00003420 assert(Ordered ||
3421 (Schedule != OMP_sch_static && Schedule != OMP_sch_static_chunked &&
Alexey Bataev6cff6242016-05-30 13:05:14 +00003422 Schedule != OMP_ord_static && Schedule != OMP_ord_static_chunked &&
3423 Schedule != OMP_sch_static_balanced_chunked));
John McCall7f416cc2015-09-08 08:05:57 +00003424 // Call __kmpc_dispatch_init(
3425 // ident_t *loc, kmp_int32 tid, kmp_int32 schedule,
3426 // kmp_int[32|64] lower, kmp_int[32|64] upper,
3427 // kmp_int[32|64] stride, kmp_int[32|64] chunk);
Alexander Musmanc6388682014-12-15 07:07:06 +00003428
John McCall7f416cc2015-09-08 08:05:57 +00003429 // If the Chunk was not specified in the clause - use default value 1.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003430 llvm::Value *Chunk = DispatchValues.Chunk ? DispatchValues.Chunk
3431 : CGF.Builder.getIntN(IVSize, 1);
John McCall7f416cc2015-09-08 08:05:57 +00003432 llvm::Value *Args[] = {
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003433 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
3434 CGF.Builder.getInt32(addMonoNonMonoModifier(
3435 Schedule, ScheduleKind.M1, ScheduleKind.M2)), // Schedule type
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003436 DispatchValues.LB, // Lower
3437 DispatchValues.UB, // Upper
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003438 CGF.Builder.getIntN(IVSize, 1), // Stride
3439 Chunk // Chunk
John McCall7f416cc2015-09-08 08:05:57 +00003440 };
3441 CGF.EmitRuntimeCall(createDispatchInitFunction(IVSize, IVSigned), Args);
3442}
3443
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003444static void emitForStaticInitCall(
3445 CodeGenFunction &CGF, llvm::Value *UpdateLocation, llvm::Value *ThreadId,
James Y Knight9871db02019-02-05 16:42:33 +00003446 llvm::FunctionCallee ForStaticInitFunction, OpenMPSchedType Schedule,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003447 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003448 const CGOpenMPRuntime::StaticRTInput &Values) {
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003449 if (!CGF.HaveInsertPoint())
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003450 return;
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003451
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003452 assert(!Values.Ordered);
3453 assert(Schedule == OMP_sch_static || Schedule == OMP_sch_static_chunked ||
3454 Schedule == OMP_sch_static_balanced_chunked ||
3455 Schedule == OMP_ord_static || Schedule == OMP_ord_static_chunked ||
3456 Schedule == OMP_dist_sch_static ||
3457 Schedule == OMP_dist_sch_static_chunked);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003458
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003459 // Call __kmpc_for_static_init(
3460 // ident_t *loc, kmp_int32 tid, kmp_int32 schedtype,
3461 // kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower,
3462 // kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride,
3463 // kmp_int[32|64] incr, kmp_int[32|64] chunk);
3464 llvm::Value *Chunk = Values.Chunk;
3465 if (Chunk == nullptr) {
3466 assert((Schedule == OMP_sch_static || Schedule == OMP_ord_static ||
3467 Schedule == OMP_dist_sch_static) &&
3468 "expected static non-chunked schedule");
3469 // If the Chunk was not specified in the clause - use default value 1.
3470 Chunk = CGF.Builder.getIntN(Values.IVSize, 1);
3471 } else {
3472 assert((Schedule == OMP_sch_static_chunked ||
3473 Schedule == OMP_sch_static_balanced_chunked ||
3474 Schedule == OMP_ord_static_chunked ||
3475 Schedule == OMP_dist_sch_static_chunked) &&
3476 "expected static chunked schedule");
3477 }
3478 llvm::Value *Args[] = {
3479 UpdateLocation,
3480 ThreadId,
3481 CGF.Builder.getInt32(addMonoNonMonoModifier(Schedule, M1,
3482 M2)), // Schedule type
3483 Values.IL.getPointer(), // &isLastIter
3484 Values.LB.getPointer(), // &LB
3485 Values.UB.getPointer(), // &UB
3486 Values.ST.getPointer(), // &Stride
3487 CGF.Builder.getIntN(Values.IVSize, 1), // Incr
3488 Chunk // Chunk
3489 };
3490 CGF.EmitRuntimeCall(ForStaticInitFunction, Args);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003491}
3492
John McCall7f416cc2015-09-08 08:05:57 +00003493void CGOpenMPRuntime::emitForStaticInit(CodeGenFunction &CGF,
3494 SourceLocation Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003495 OpenMPDirectiveKind DKind,
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003496 const OpenMPScheduleTy &ScheduleKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003497 const StaticRTInput &Values) {
3498 OpenMPSchedType ScheduleNum = getRuntimeSchedule(
3499 ScheduleKind.Schedule, Values.Chunk != nullptr, Values.Ordered);
3500 assert(isOpenMPWorksharingDirective(DKind) &&
3501 "Expected loop-based or sections-based directive.");
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003502 llvm::Value *UpdatedLocation = emitUpdateLocation(CGF, Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003503 isOpenMPLoopDirective(DKind)
3504 ? OMP_IDENT_WORK_LOOP
3505 : OMP_IDENT_WORK_SECTIONS);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003506 llvm::Value *ThreadId = getThreadID(CGF, Loc);
James Y Knight9871db02019-02-05 16:42:33 +00003507 llvm::FunctionCallee StaticInitFunction =
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003508 createForStaticInitFunction(Values.IVSize, Values.IVSigned);
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003509 emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003510 ScheduleNum, ScheduleKind.M1, ScheduleKind.M2, Values);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003511}
John McCall7f416cc2015-09-08 08:05:57 +00003512
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003513void CGOpenMPRuntime::emitDistributeStaticInit(
3514 CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003515 OpenMPDistScheduleClauseKind SchedKind,
3516 const CGOpenMPRuntime::StaticRTInput &Values) {
3517 OpenMPSchedType ScheduleNum =
3518 getRuntimeSchedule(SchedKind, Values.Chunk != nullptr);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003519 llvm::Value *UpdatedLocation =
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003520 emitUpdateLocation(CGF, Loc, OMP_IDENT_WORK_DISTRIBUTE);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003521 llvm::Value *ThreadId = getThreadID(CGF, Loc);
James Y Knight9871db02019-02-05 16:42:33 +00003522 llvm::FunctionCallee StaticInitFunction =
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003523 createForStaticInitFunction(Values.IVSize, Values.IVSigned);
Alexey Bataev9ebd7422016-05-10 09:57:36 +00003524 emitForStaticInitCall(CGF, UpdatedLocation, ThreadId, StaticInitFunction,
3525 ScheduleNum, OMPC_SCHEDULE_MODIFIER_unknown,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003526 OMPC_SCHEDULE_MODIFIER_unknown, Values);
Alexander Musmanc6388682014-12-15 07:07:06 +00003527}
3528
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003529void CGOpenMPRuntime::emitForStaticFinish(CodeGenFunction &CGF,
Alexey Bataevf43f7142017-09-06 16:17:35 +00003530 SourceLocation Loc,
3531 OpenMPDirectiveKind DKind) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003532 if (!CGF.HaveInsertPoint())
3533 return;
Alexander Musmanc6388682014-12-15 07:07:06 +00003534 // Call __kmpc_for_static_fini(ident_t *loc, kmp_int32 tid);
Alexey Bataevf43f7142017-09-06 16:17:35 +00003535 llvm::Value *Args[] = {
3536 emitUpdateLocation(CGF, Loc,
3537 isOpenMPDistributeDirective(DKind)
3538 ? OMP_IDENT_WORK_DISTRIBUTE
3539 : isOpenMPLoopDirective(DKind)
3540 ? OMP_IDENT_WORK_LOOP
3541 : OMP_IDENT_WORK_SECTIONS),
3542 getThreadID(CGF, Loc)};
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003543 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_for_static_fini),
3544 Args);
Alexander Musmanc6388682014-12-15 07:07:06 +00003545}
3546
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00003547void CGOpenMPRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF,
3548 SourceLocation Loc,
3549 unsigned IVSize,
3550 bool IVSigned) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003551 if (!CGF.HaveInsertPoint())
3552 return;
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003553 // Call __kmpc_for_dynamic_fini_(4|8)[u](ident_t *loc, kmp_int32 tid);
Alexey Bataev50b3c952016-02-19 10:38:26 +00003554 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003555 CGF.EmitRuntimeCall(createDispatchFiniFunction(IVSize, IVSigned), Args);
3556}
3557
Alexander Musman92bdaab2015-03-12 13:37:50 +00003558llvm::Value *CGOpenMPRuntime::emitForNext(CodeGenFunction &CGF,
3559 SourceLocation Loc, unsigned IVSize,
John McCall7f416cc2015-09-08 08:05:57 +00003560 bool IVSigned, Address IL,
3561 Address LB, Address UB,
3562 Address ST) {
Alexander Musman92bdaab2015-03-12 13:37:50 +00003563 // Call __kmpc_dispatch_next(
3564 // ident_t *loc, kmp_int32 tid, kmp_int32 *p_lastiter,
3565 // kmp_int[32|64] *p_lower, kmp_int[32|64] *p_upper,
3566 // kmp_int[32|64] *p_stride);
3567 llvm::Value *Args[] = {
Alexey Bataev50b3c952016-02-19 10:38:26 +00003568 emitUpdateLocation(CGF, Loc),
3569 getThreadID(CGF, Loc),
John McCall7f416cc2015-09-08 08:05:57 +00003570 IL.getPointer(), // &isLastIter
3571 LB.getPointer(), // &Lower
3572 UB.getPointer(), // &Upper
3573 ST.getPointer() // &Stride
Alexander Musman92bdaab2015-03-12 13:37:50 +00003574 };
3575 llvm::Value *Call =
3576 CGF.EmitRuntimeCall(createDispatchNextFunction(IVSize, IVSigned), Args);
3577 return CGF.EmitScalarConversion(
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003578 Call, CGF.getContext().getIntTypeForBitwidth(32, /*Signed=*/1),
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003579 CGF.getContext().BoolTy, Loc);
Alexander Musman92bdaab2015-03-12 13:37:50 +00003580}
3581
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003582void CGOpenMPRuntime::emitNumThreadsClause(CodeGenFunction &CGF,
3583 llvm::Value *NumThreads,
3584 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003585 if (!CGF.HaveInsertPoint())
3586 return;
Alexey Bataevb2059782014-10-13 08:23:51 +00003587 // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads)
3588 llvm::Value *Args[] = {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003589 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
Alexey Bataevb2059782014-10-13 08:23:51 +00003590 CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)};
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003591 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_threads),
3592 Args);
Alexey Bataevb2059782014-10-13 08:23:51 +00003593}
3594
Alexey Bataev7f210c62015-06-18 13:40:03 +00003595void CGOpenMPRuntime::emitProcBindClause(CodeGenFunction &CGF,
3596 OpenMPProcBindClauseKind ProcBind,
3597 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003598 if (!CGF.HaveInsertPoint())
3599 return;
Alexey Bataev7f210c62015-06-18 13:40:03 +00003600 // Constants for proc bind value accepted by the runtime.
3601 enum ProcBindTy {
3602 ProcBindFalse = 0,
3603 ProcBindTrue,
3604 ProcBindMaster,
3605 ProcBindClose,
3606 ProcBindSpread,
3607 ProcBindIntel,
3608 ProcBindDefault
3609 } RuntimeProcBind;
3610 switch (ProcBind) {
3611 case OMPC_PROC_BIND_master:
3612 RuntimeProcBind = ProcBindMaster;
3613 break;
3614 case OMPC_PROC_BIND_close:
3615 RuntimeProcBind = ProcBindClose;
3616 break;
3617 case OMPC_PROC_BIND_spread:
3618 RuntimeProcBind = ProcBindSpread;
3619 break;
3620 case OMPC_PROC_BIND_unknown:
3621 llvm_unreachable("Unsupported proc_bind value.");
3622 }
3623 // Build call __kmpc_push_proc_bind(&loc, global_tid, proc_bind)
3624 llvm::Value *Args[] = {
3625 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
3626 llvm::ConstantInt::get(CGM.IntTy, RuntimeProcBind, /*isSigned=*/true)};
3627 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_proc_bind), Args);
3628}
3629
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003630void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>,
3631 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00003632 if (!CGF.HaveInsertPoint())
3633 return;
Alexey Bataevd76df6d2015-02-24 12:55:09 +00003634 // Build call void __kmpc_flush(ident_t *loc)
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003635 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_flush),
3636 emitUpdateLocation(CGF, Loc));
Alexey Bataevcc37cc12014-11-20 04:34:54 +00003637}
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003638
Alexey Bataev62b63b12015-03-10 07:28:44 +00003639namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003640/// Indexes of fields for type kmp_task_t.
Alexey Bataev62b63b12015-03-10 07:28:44 +00003641enum KmpTaskTFields {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003642 /// List of shared variables.
Alexey Bataev62b63b12015-03-10 07:28:44 +00003643 KmpTaskTShareds,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003644 /// Task routine.
Alexey Bataev62b63b12015-03-10 07:28:44 +00003645 KmpTaskTRoutine,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003646 /// Partition id for the untied tasks.
Alexey Bataev62b63b12015-03-10 07:28:44 +00003647 KmpTaskTPartId,
Alexey Bataevad537bb2016-05-30 09:06:50 +00003648 /// Function with call of destructors for private variables.
3649 Data1,
3650 /// Task priority.
3651 Data2,
Alexey Bataev7292c292016-04-25 12:22:29 +00003652 /// (Taskloops only) Lower bound.
3653 KmpTaskTLowerBound,
3654 /// (Taskloops only) Upper bound.
3655 KmpTaskTUpperBound,
3656 /// (Taskloops only) Stride.
3657 KmpTaskTStride,
3658 /// (Taskloops only) Is last iteration flag.
3659 KmpTaskTLastIter,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00003660 /// (Taskloops only) Reduction data.
3661 KmpTaskTReductions,
Alexey Bataev62b63b12015-03-10 07:28:44 +00003662};
Hans Wennborg7eb54642015-09-10 17:07:54 +00003663} // anonymous namespace
Alexey Bataev62b63b12015-03-10 07:28:44 +00003664
Samuel Antaoee8fb302016-01-06 13:42:12 +00003665bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::empty() const {
Alexey Bataev03f270c2018-03-30 18:31:07 +00003666 return OffloadEntriesTargetRegion.empty() &&
3667 OffloadEntriesDeviceGlobalVar.empty();
Samuel Antaoee8fb302016-01-06 13:42:12 +00003668}
3669
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003670/// Initialize target region entry.
Samuel Antaoee8fb302016-01-06 13:42:12 +00003671void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
3672 initializeTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
3673 StringRef ParentName, unsigned LineNum,
Samuel Antao2de62b02016-02-13 23:35:10 +00003674 unsigned Order) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003675 assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is "
3676 "only required for the device "
3677 "code generation.");
Samuel Antao2de62b02016-02-13 23:35:10 +00003678 OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] =
Samuel Antaof83efdb2017-01-05 16:02:49 +00003679 OffloadEntryInfoTargetRegion(Order, /*Addr=*/nullptr, /*ID=*/nullptr,
Alexey Bataev34f8a702018-03-28 14:28:54 +00003680 OMPTargetRegionEntryTargetRegion);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003681 ++OffloadingEntriesNum;
3682}
3683
3684void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
3685 registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
3686 StringRef ParentName, unsigned LineNum,
Samuel Antaof83efdb2017-01-05 16:02:49 +00003687 llvm::Constant *Addr, llvm::Constant *ID,
Alexey Bataev34f8a702018-03-28 14:28:54 +00003688 OMPTargetRegionEntryKind Flags) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003689 // If we are emitting code for a target, the entry is already initialized,
3690 // only has to be registered.
3691 if (CGM.getLangOpts().OpenMPIsDevice) {
Alexey Bataev64e62dc2018-04-30 16:26:57 +00003692 if (!hasTargetRegionEntryInfo(DeviceID, FileID, ParentName, LineNum)) {
3693 unsigned DiagID = CGM.getDiags().getCustomDiagID(
3694 DiagnosticsEngine::Error,
3695 "Unable to find target region on line '%0' in the device code.");
3696 CGM.getDiags().Report(DiagID) << LineNum;
3697 return;
3698 }
Samuel Antao2de62b02016-02-13 23:35:10 +00003699 auto &Entry =
3700 OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum];
Samuel Antaoee8fb302016-01-06 13:42:12 +00003701 assert(Entry.isValid() && "Entry not initialized!");
3702 Entry.setAddress(Addr);
3703 Entry.setID(ID);
Samuel Antaof83efdb2017-01-05 16:02:49 +00003704 Entry.setFlags(Flags);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003705 } else {
Alexey Bataev03f270c2018-03-30 18:31:07 +00003706 OffloadEntryInfoTargetRegion Entry(OffloadingEntriesNum, Addr, ID, Flags);
Samuel Antao2de62b02016-02-13 23:35:10 +00003707 OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = Entry;
Alexey Bataev03f270c2018-03-30 18:31:07 +00003708 ++OffloadingEntriesNum;
Samuel Antaoee8fb302016-01-06 13:42:12 +00003709 }
3710}
3711
3712bool CGOpenMPRuntime::OffloadEntriesInfoManagerTy::hasTargetRegionEntryInfo(
Samuel Antao2de62b02016-02-13 23:35:10 +00003713 unsigned DeviceID, unsigned FileID, StringRef ParentName,
3714 unsigned LineNum) const {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003715 auto PerDevice = OffloadEntriesTargetRegion.find(DeviceID);
3716 if (PerDevice == OffloadEntriesTargetRegion.end())
3717 return false;
3718 auto PerFile = PerDevice->second.find(FileID);
3719 if (PerFile == PerDevice->second.end())
3720 return false;
3721 auto PerParentName = PerFile->second.find(ParentName);
3722 if (PerParentName == PerFile->second.end())
3723 return false;
3724 auto PerLine = PerParentName->second.find(LineNum);
3725 if (PerLine == PerParentName->second.end())
3726 return false;
Samuel Antaoee8fb302016-01-06 13:42:12 +00003727 // Fail if this entry is already registered.
Samuel Antao2de62b02016-02-13 23:35:10 +00003728 if (PerLine->second.getAddress() || PerLine->second.getID())
Samuel Antaoee8fb302016-01-06 13:42:12 +00003729 return false;
3730 return true;
3731}
3732
3733void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::actOnTargetRegionEntriesInfo(
3734 const OffloadTargetRegionEntryInfoActTy &Action) {
3735 // Scan all target region entries and perform the provided action.
Alexey Bataev03f270c2018-03-30 18:31:07 +00003736 for (const auto &D : OffloadEntriesTargetRegion)
3737 for (const auto &F : D.second)
3738 for (const auto &P : F.second)
3739 for (const auto &L : P.second)
Samuel Antao2de62b02016-02-13 23:35:10 +00003740 Action(D.first, F.first, P.first(), L.first, L.second);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003741}
3742
Alexey Bataev03f270c2018-03-30 18:31:07 +00003743void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
3744 initializeDeviceGlobalVarEntryInfo(StringRef Name,
3745 OMPTargetGlobalVarEntryKind Flags,
3746 unsigned Order) {
3747 assert(CGM.getLangOpts().OpenMPIsDevice && "Initialization of entries is "
3748 "only required for the device "
3749 "code generation.");
3750 OffloadEntriesDeviceGlobalVar.try_emplace(Name, Order, Flags);
3751 ++OffloadingEntriesNum;
3752}
Samuel Antaoee8fb302016-01-06 13:42:12 +00003753
Alexey Bataev03f270c2018-03-30 18:31:07 +00003754void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
3755 registerDeviceGlobalVarEntryInfo(StringRef VarName, llvm::Constant *Addr,
3756 CharUnits VarSize,
3757 OMPTargetGlobalVarEntryKind Flags,
3758 llvm::GlobalValue::LinkageTypes Linkage) {
3759 if (CGM.getLangOpts().OpenMPIsDevice) {
3760 auto &Entry = OffloadEntriesDeviceGlobalVar[VarName];
3761 assert(Entry.isValid() && Entry.getFlags() == Flags &&
3762 "Entry not initialized!");
3763 assert((!Entry.getAddress() || Entry.getAddress() == Addr) &&
3764 "Resetting with the new address.");
3765 if (Entry.getAddress() && hasDeviceGlobalVarEntryInfo(VarName))
3766 return;
3767 Entry.setAddress(Addr);
3768 Entry.setVarSize(VarSize);
3769 Entry.setLinkage(Linkage);
3770 } else {
3771 if (hasDeviceGlobalVarEntryInfo(VarName))
3772 return;
3773 OffloadEntriesDeviceGlobalVar.try_emplace(
3774 VarName, OffloadingEntriesNum, Addr, VarSize, Flags, Linkage);
3775 ++OffloadingEntriesNum;
3776 }
3777}
3778
3779void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
3780 actOnDeviceGlobalVarEntriesInfo(
3781 const OffloadDeviceGlobalVarEntryInfoActTy &Action) {
3782 // Scan all target region entries and perform the provided action.
3783 for (const auto &E : OffloadEntriesDeviceGlobalVar)
3784 Action(E.getKey(), E.getValue());
Samuel Antaoee8fb302016-01-06 13:42:12 +00003785}
3786
3787llvm::Function *
3788CGOpenMPRuntime::createOffloadingBinaryDescriptorRegistration() {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003789 // If we don't have entries or if we are emitting code for the device, we
3790 // don't need to do anything.
3791 if (CGM.getLangOpts().OpenMPIsDevice || OffloadEntriesInfoManager.empty())
3792 return nullptr;
3793
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003794 llvm::Module &M = CGM.getModule();
3795 ASTContext &C = CGM.getContext();
Samuel Antaoee8fb302016-01-06 13:42:12 +00003796
3797 // Get list of devices we care about
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003798 const std::vector<llvm::Triple> &Devices = CGM.getLangOpts().OMPTargetTriples;
Samuel Antaoee8fb302016-01-06 13:42:12 +00003799
3800 // We should be creating an offloading descriptor only if there are devices
3801 // specified.
3802 assert(!Devices.empty() && "No OpenMP offloading devices??");
3803
3804 // Create the external variables that will point to the begin and end of the
3805 // host entries section. These will be defined by the linker.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003806 llvm::Type *OffloadEntryTy =
Samuel Antaoee8fb302016-01-06 13:42:12 +00003807 CGM.getTypes().ConvertTypeForMem(getTgtOffloadEntryQTy());
Alexey Bataev18fa2322018-05-02 14:20:50 +00003808 std::string EntriesBeginName = getName({"omp_offloading", "entries_begin"});
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003809 auto *HostEntriesBegin = new llvm::GlobalVariable(
Samuel Antaoee8fb302016-01-06 13:42:12 +00003810 M, OffloadEntryTy, /*isConstant=*/true,
Eugene Zelenko1660a5d2016-01-26 19:01:06 +00003811 llvm::GlobalValue::ExternalLinkage, /*Initializer=*/nullptr,
Alexey Bataev18fa2322018-05-02 14:20:50 +00003812 EntriesBeginName);
3813 std::string EntriesEndName = getName({"omp_offloading", "entries_end"});
3814 auto *HostEntriesEnd =
3815 new llvm::GlobalVariable(M, OffloadEntryTy, /*isConstant=*/true,
3816 llvm::GlobalValue::ExternalLinkage,
3817 /*Initializer=*/nullptr, EntriesEndName);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003818
3819 // Create all device images
Samuel Antaoee8fb302016-01-06 13:42:12 +00003820 auto *DeviceImageTy = cast<llvm::StructType>(
3821 CGM.getTypes().ConvertTypeForMem(getTgtDeviceImageQTy()));
John McCall23c9dc62016-11-28 22:18:27 +00003822 ConstantInitBuilder DeviceImagesBuilder(CGM);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003823 ConstantArrayBuilder DeviceImagesEntries =
3824 DeviceImagesBuilder.beginArray(DeviceImageTy);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003825
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003826 for (const llvm::Triple &Device : Devices) {
Alexey Bataev03f270c2018-03-30 18:31:07 +00003827 StringRef T = Device.getTriple();
Alexey Bataev18fa2322018-05-02 14:20:50 +00003828 std::string BeginName = getName({"omp_offloading", "img_start", ""});
Samuel Antaoee8fb302016-01-06 13:42:12 +00003829 auto *ImgBegin = new llvm::GlobalVariable(
Alexey Bataev62a4cb02018-07-31 18:27:42 +00003830 M, CGM.Int8Ty, /*isConstant=*/true,
3831 llvm::GlobalValue::ExternalWeakLinkage,
Alexey Bataev18fa2322018-05-02 14:20:50 +00003832 /*Initializer=*/nullptr, Twine(BeginName).concat(T));
3833 std::string EndName = getName({"omp_offloading", "img_end", ""});
Samuel Antaoee8fb302016-01-06 13:42:12 +00003834 auto *ImgEnd = new llvm::GlobalVariable(
Alexey Bataev62a4cb02018-07-31 18:27:42 +00003835 M, CGM.Int8Ty, /*isConstant=*/true,
3836 llvm::GlobalValue::ExternalWeakLinkage,
Alexey Bataev18fa2322018-05-02 14:20:50 +00003837 /*Initializer=*/nullptr, Twine(EndName).concat(T));
Samuel Antaoee8fb302016-01-06 13:42:12 +00003838
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003839 llvm::Constant *Data[] = {ImgBegin, ImgEnd, HostEntriesBegin,
3840 HostEntriesEnd};
3841 createConstantGlobalStructAndAddToParent(CGM, getTgtDeviceImageQTy(), Data,
3842 DeviceImagesEntries);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003843 }
3844
3845 // Create device images global array.
Alexey Bataev18fa2322018-05-02 14:20:50 +00003846 std::string ImagesName = getName({"omp_offloading", "device_images"});
John McCall6c9f1fdb2016-11-19 08:17:24 +00003847 llvm::GlobalVariable *DeviceImages =
Alexey Bataev18fa2322018-05-02 14:20:50 +00003848 DeviceImagesEntries.finishAndCreateGlobal(ImagesName,
3849 CGM.getPointerAlign(),
3850 /*isConstant=*/true);
Peter Collingbournebcf909d2016-06-14 21:02:05 +00003851 DeviceImages->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003852
3853 // This is a Zero array to be used in the creation of the constant expressions
3854 llvm::Constant *Index[] = {llvm::Constant::getNullValue(CGM.Int32Ty),
3855 llvm::Constant::getNullValue(CGM.Int32Ty)};
3856
3857 // Create the target region descriptor.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003858 llvm::Constant *Data[] = {
3859 llvm::ConstantInt::get(CGM.Int32Ty, Devices.size()),
3860 llvm::ConstantExpr::getGetElementPtr(DeviceImages->getValueType(),
3861 DeviceImages, Index),
3862 HostEntriesBegin, HostEntriesEnd};
Alexey Bataev18fa2322018-05-02 14:20:50 +00003863 std::string Descriptor = getName({"omp_offloading", "descriptor"});
Mike Ricee1ca7b62018-08-29 15:45:11 +00003864 llvm::GlobalVariable *Desc = createGlobalStruct(
3865 CGM, getTgtBinaryDescriptorQTy(), /*IsConstant=*/true, Data, Descriptor);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003866
3867 // Emit code to register or unregister the descriptor at execution
3868 // startup or closing, respectively.
3869
Alexey Bataev03f270c2018-03-30 18:31:07 +00003870 llvm::Function *UnRegFn;
3871 {
3872 FunctionArgList Args;
3873 ImplicitParamDecl DummyPtr(C, C.VoidPtrTy, ImplicitParamDecl::Other);
3874 Args.push_back(&DummyPtr);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003875
Alexey Bataev03f270c2018-03-30 18:31:07 +00003876 CodeGenFunction CGF(CGM);
3877 // Disable debug info for global (de-)initializer because they are not part
3878 // of some particular construct.
3879 CGF.disableDebugInfo();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003880 const auto &FI =
3881 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
3882 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
Alexey Bataev18fa2322018-05-02 14:20:50 +00003883 std::string UnregName = getName({"omp_offloading", "descriptor_unreg"});
3884 UnRegFn = CGM.CreateGlobalInitOrDestructFunction(FTy, UnregName, FI);
Alexey Bataev03f270c2018-03-30 18:31:07 +00003885 CGF.StartFunction(GlobalDecl(), C.VoidTy, UnRegFn, FI, Args);
3886 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_unregister_lib),
3887 Desc);
3888 CGF.FinishFunction();
3889 }
3890 llvm::Function *RegFn;
3891 {
3892 CodeGenFunction CGF(CGM);
3893 // Disable debug info for global (de-)initializer because they are not part
3894 // of some particular construct.
3895 CGF.disableDebugInfo();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003896 const auto &FI = CGM.getTypes().arrangeNullaryFunction();
Alexey Bataev03f270c2018-03-30 18:31:07 +00003897 llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
Sergey Dmitrievbde9cf92018-08-03 20:19:28 +00003898
3899 // Encode offload target triples into the registration function name. It
3900 // will serve as a comdat key for the registration/unregistration code for
3901 // this particular combination of offloading targets.
3902 SmallVector<StringRef, 4U> RegFnNameParts(Devices.size() + 2U);
3903 RegFnNameParts[0] = "omp_offloading";
3904 RegFnNameParts[1] = "descriptor_reg";
3905 llvm::transform(Devices, std::next(RegFnNameParts.begin(), 2),
3906 [](const llvm::Triple &T) -> const std::string& {
3907 return T.getTriple();
3908 });
3909 llvm::sort(std::next(RegFnNameParts.begin(), 2), RegFnNameParts.end());
3910 std::string Descriptor = getName(RegFnNameParts);
Alexey Bataev18fa2322018-05-02 14:20:50 +00003911 RegFn = CGM.CreateGlobalInitOrDestructFunction(FTy, Descriptor, FI);
Alexey Bataev03f270c2018-03-30 18:31:07 +00003912 CGF.StartFunction(GlobalDecl(), C.VoidTy, RegFn, FI, FunctionArgList());
3913 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_register_lib), Desc);
3914 // Create a variable to drive the registration and unregistration of the
3915 // descriptor, so we can reuse the logic that emits Ctors and Dtors.
3916 ImplicitParamDecl RegUnregVar(C, C.getTranslationUnitDecl(),
3917 SourceLocation(), nullptr, C.CharTy,
3918 ImplicitParamDecl::Other);
3919 CGM.getCXXABI().registerGlobalDtor(CGF, RegUnregVar, UnRegFn, Desc);
3920 CGF.FinishFunction();
3921 }
George Rokos29d0f002017-05-27 03:03:13 +00003922 if (CGM.supportsCOMDAT()) {
3923 // It is sufficient to call registration function only once, so create a
3924 // COMDAT group for registration/unregistration functions and associated
3925 // data. That would reduce startup time and code size. Registration
3926 // function serves as a COMDAT group key.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003927 llvm::Comdat *ComdatKey = M.getOrInsertComdat(RegFn->getName());
George Rokos29d0f002017-05-27 03:03:13 +00003928 RegFn->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage);
3929 RegFn->setVisibility(llvm::GlobalValue::HiddenVisibility);
3930 RegFn->setComdat(ComdatKey);
3931 UnRegFn->setComdat(ComdatKey);
3932 DeviceImages->setComdat(ComdatKey);
3933 Desc->setComdat(ComdatKey);
3934 }
Samuel Antaoee8fb302016-01-06 13:42:12 +00003935 return RegFn;
3936}
3937
Alexey Bataev03f270c2018-03-30 18:31:07 +00003938void CGOpenMPRuntime::createOffloadEntry(
3939 llvm::Constant *ID, llvm::Constant *Addr, uint64_t Size, int32_t Flags,
3940 llvm::GlobalValue::LinkageTypes Linkage) {
Samuel Antao2de62b02016-02-13 23:35:10 +00003941 StringRef Name = Addr->getName();
Samuel Antaoee8fb302016-01-06 13:42:12 +00003942 llvm::Module &M = CGM.getModule();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003943 llvm::LLVMContext &C = M.getContext();
Samuel Antaoee8fb302016-01-06 13:42:12 +00003944
3945 // Create constant string with the name.
3946 llvm::Constant *StrPtrInit = llvm::ConstantDataArray::getString(C, Name);
3947
Alexey Bataev18fa2322018-05-02 14:20:50 +00003948 std::string StringName = getName({"omp_offloading", "entry_name"});
3949 auto *Str = new llvm::GlobalVariable(
3950 M, StrPtrInit->getType(), /*isConstant=*/true,
3951 llvm::GlobalValue::InternalLinkage, StrPtrInit, StringName);
Peter Collingbournebcf909d2016-06-14 21:02:05 +00003952 Str->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003953
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003954 llvm::Constant *Data[] = {llvm::ConstantExpr::getBitCast(ID, CGM.VoidPtrTy),
3955 llvm::ConstantExpr::getBitCast(Str, CGM.Int8PtrTy),
3956 llvm::ConstantInt::get(CGM.SizeTy, Size),
3957 llvm::ConstantInt::get(CGM.Int32Ty, Flags),
3958 llvm::ConstantInt::get(CGM.Int32Ty, 0)};
Alexey Bataev18fa2322018-05-02 14:20:50 +00003959 std::string EntryName = getName({"omp_offloading", "entry", ""});
Mike Ricee1ca7b62018-08-29 15:45:11 +00003960 llvm::GlobalVariable *Entry = createGlobalStruct(
3961 CGM, getTgtOffloadEntryQTy(), /*IsConstant=*/true, Data,
3962 Twine(EntryName).concat(Name), llvm::GlobalValue::WeakAnyLinkage);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003963
3964 // The entry has to be created in the section the linker expects it to be.
Alexey Bataev18fa2322018-05-02 14:20:50 +00003965 std::string Section = getName({"omp_offloading", "entries"});
3966 Entry->setSection(Section);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003967}
3968
3969void CGOpenMPRuntime::createOffloadEntriesAndInfoMetadata() {
3970 // Emit the offloading entries and metadata so that the device codegen side
Samuel Antao4c8035b2016-12-12 18:00:20 +00003971 // can easily figure out what to emit. The produced metadata looks like
3972 // this:
Samuel Antaoee8fb302016-01-06 13:42:12 +00003973 //
3974 // !omp_offload.info = !{!1, ...}
3975 //
3976 // Right now we only generate metadata for function that contain target
3977 // regions.
3978
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00003979 // If we do not have entries, we don't need to do anything.
Samuel Antaoee8fb302016-01-06 13:42:12 +00003980 if (OffloadEntriesInfoManager.empty())
3981 return;
3982
3983 llvm::Module &M = CGM.getModule();
3984 llvm::LLVMContext &C = M.getContext();
Alexey Bataev03f270c2018-03-30 18:31:07 +00003985 SmallVector<const OffloadEntriesInfoManagerTy::OffloadEntryInfo *, 16>
Samuel Antaoee8fb302016-01-06 13:42:12 +00003986 OrderedEntries(OffloadEntriesInfoManager.size());
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00003987 llvm::SmallVector<StringRef, 16> ParentFunctions(
3988 OffloadEntriesInfoManager.size());
Samuel Antaoee8fb302016-01-06 13:42:12 +00003989
Simon Pilgrim2c518802017-03-30 14:13:19 +00003990 // Auxiliary methods to create metadata values and strings.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003991 auto &&GetMDInt = [this](unsigned V) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00003992 return llvm::ConstantAsMetadata::get(
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00003993 llvm::ConstantInt::get(CGM.Int32Ty, V));
Samuel Antaoee8fb302016-01-06 13:42:12 +00003994 };
3995
Alexey Bataev03f270c2018-03-30 18:31:07 +00003996 auto &&GetMDString = [&C](StringRef V) { return llvm::MDString::get(C, V); };
3997
3998 // Create the offloading info metadata node.
3999 llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("omp_offload.info");
Samuel Antaoee8fb302016-01-06 13:42:12 +00004000
4001 // Create function that emits metadata for each target region entry;
Alexey Bataev03f270c2018-03-30 18:31:07 +00004002 auto &&TargetRegionMetadataEmitter =
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00004003 [&C, MD, &OrderedEntries, &ParentFunctions, &GetMDInt, &GetMDString](
Alexey Bataev03f270c2018-03-30 18:31:07 +00004004 unsigned DeviceID, unsigned FileID, StringRef ParentName,
4005 unsigned Line,
4006 const OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion &E) {
4007 // Generate metadata for target regions. Each entry of this metadata
4008 // contains:
4009 // - Entry 0 -> Kind of this type of metadata (0).
4010 // - Entry 1 -> Device ID of the file where the entry was identified.
4011 // - Entry 2 -> File ID of the file where the entry was identified.
4012 // - Entry 3 -> Mangled name of the function where the entry was
4013 // identified.
4014 // - Entry 4 -> Line in the file where the entry was identified.
4015 // - Entry 5 -> Order the entry was created.
4016 // The first element of the metadata node is the kind.
4017 llvm::Metadata *Ops[] = {GetMDInt(E.getKind()), GetMDInt(DeviceID),
4018 GetMDInt(FileID), GetMDString(ParentName),
4019 GetMDInt(Line), GetMDInt(E.getOrder())};
Samuel Antaoee8fb302016-01-06 13:42:12 +00004020
Alexey Bataev03f270c2018-03-30 18:31:07 +00004021 // Save this entry in the right position of the ordered entries array.
4022 OrderedEntries[E.getOrder()] = &E;
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00004023 ParentFunctions[E.getOrder()] = ParentName;
Samuel Antaoee8fb302016-01-06 13:42:12 +00004024
Alexey Bataev03f270c2018-03-30 18:31:07 +00004025 // Add metadata to the named metadata node.
4026 MD->addOperand(llvm::MDNode::get(C, Ops));
4027 };
Samuel Antaoee8fb302016-01-06 13:42:12 +00004028
4029 OffloadEntriesInfoManager.actOnTargetRegionEntriesInfo(
4030 TargetRegionMetadataEmitter);
4031
Alexey Bataev03f270c2018-03-30 18:31:07 +00004032 // Create function that emits metadata for each device global variable entry;
4033 auto &&DeviceGlobalVarMetadataEmitter =
4034 [&C, &OrderedEntries, &GetMDInt, &GetMDString,
4035 MD](StringRef MangledName,
4036 const OffloadEntriesInfoManagerTy::OffloadEntryInfoDeviceGlobalVar
4037 &E) {
4038 // Generate metadata for global variables. Each entry of this metadata
4039 // contains:
4040 // - Entry 0 -> Kind of this type of metadata (1).
4041 // - Entry 1 -> Mangled name of the variable.
4042 // - Entry 2 -> Declare target kind.
4043 // - Entry 3 -> Order the entry was created.
4044 // The first element of the metadata node is the kind.
4045 llvm::Metadata *Ops[] = {
4046 GetMDInt(E.getKind()), GetMDString(MangledName),
4047 GetMDInt(E.getFlags()), GetMDInt(E.getOrder())};
4048
4049 // Save this entry in the right position of the ordered entries array.
4050 OrderedEntries[E.getOrder()] = &E;
4051
4052 // Add metadata to the named metadata node.
4053 MD->addOperand(llvm::MDNode::get(C, Ops));
4054 };
4055
4056 OffloadEntriesInfoManager.actOnDeviceGlobalVarEntriesInfo(
4057 DeviceGlobalVarMetadataEmitter);
4058
4059 for (const auto *E : OrderedEntries) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00004060 assert(E && "All ordered entries must exist!");
Alexey Bataev03f270c2018-03-30 18:31:07 +00004061 if (const auto *CE =
Samuel Antaoee8fb302016-01-06 13:42:12 +00004062 dyn_cast<OffloadEntriesInfoManagerTy::OffloadEntryInfoTargetRegion>(
4063 E)) {
Alexey Bataev64e62dc2018-04-30 16:26:57 +00004064 if (!CE->getID() || !CE->getAddress()) {
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00004065 // Do not blame the entry if the parent funtion is not emitted.
4066 StringRef FnName = ParentFunctions[CE->getOrder()];
4067 if (!CGM.GetGlobalValue(FnName))
4068 continue;
Alexey Bataev64e62dc2018-04-30 16:26:57 +00004069 unsigned DiagID = CGM.getDiags().getCustomDiagID(
4070 DiagnosticsEngine::Error,
Alexey Bataev7f01d202018-07-16 18:12:18 +00004071 "Offloading entry for target region is incorrect: either the "
Alexey Bataev64e62dc2018-04-30 16:26:57 +00004072 "address or the ID is invalid.");
4073 CGM.getDiags().Report(DiagID);
4074 continue;
4075 }
Alexey Bataev34f8a702018-03-28 14:28:54 +00004076 createOffloadEntry(CE->getID(), CE->getAddress(), /*Size=*/0,
Alexey Bataev03f270c2018-03-30 18:31:07 +00004077 CE->getFlags(), llvm::GlobalValue::WeakAnyLinkage);
4078 } else if (const auto *CE =
4079 dyn_cast<OffloadEntriesInfoManagerTy::
4080 OffloadEntryInfoDeviceGlobalVar>(E)) {
Alexey Bataevc52f01d2018-07-16 20:05:25 +00004081 OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags =
4082 static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>(
4083 CE->getFlags());
4084 switch (Flags) {
4085 case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo: {
4086 if (!CE->getAddress()) {
4087 unsigned DiagID = CGM.getDiags().getCustomDiagID(
4088 DiagnosticsEngine::Error,
4089 "Offloading entry for declare target variable is incorrect: the "
4090 "address is invalid.");
4091 CGM.getDiags().Report(DiagID);
4092 continue;
4093 }
Alexey Bataevb4dd6d22018-08-29 20:41:37 +00004094 // The vaiable has no definition - no need to add the entry.
4095 if (CE->getVarSize().isZero())
4096 continue;
Alexey Bataevc52f01d2018-07-16 20:05:25 +00004097 break;
4098 }
4099 case OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink:
4100 assert(((CGM.getLangOpts().OpenMPIsDevice && !CE->getAddress()) ||
4101 (!CGM.getLangOpts().OpenMPIsDevice && CE->getAddress())) &&
4102 "Declaret target link address is set.");
4103 if (CGM.getLangOpts().OpenMPIsDevice)
4104 continue;
4105 if (!CE->getAddress()) {
4106 unsigned DiagID = CGM.getDiags().getCustomDiagID(
4107 DiagnosticsEngine::Error,
4108 "Offloading entry for declare target variable is incorrect: the "
4109 "address is invalid.");
4110 CGM.getDiags().Report(DiagID);
4111 continue;
4112 }
4113 break;
Alexey Bataev64e62dc2018-04-30 16:26:57 +00004114 }
Alexey Bataev03f270c2018-03-30 18:31:07 +00004115 createOffloadEntry(CE->getAddress(), CE->getAddress(),
Alexey Bataevc52f01d2018-07-16 20:05:25 +00004116 CE->getVarSize().getQuantity(), Flags,
Alexey Bataev03f270c2018-03-30 18:31:07 +00004117 CE->getLinkage());
4118 } else {
Samuel Antaoee8fb302016-01-06 13:42:12 +00004119 llvm_unreachable("Unsupported entry kind.");
Alexey Bataev03f270c2018-03-30 18:31:07 +00004120 }
Samuel Antaoee8fb302016-01-06 13:42:12 +00004121 }
4122}
4123
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004124/// Loads all the offload entries information from the host IR
Samuel Antaoee8fb302016-01-06 13:42:12 +00004125/// metadata.
4126void CGOpenMPRuntime::loadOffloadInfoMetadata() {
4127 // If we are in target mode, load the metadata from the host IR. This code has
4128 // to match the metadaata creation in createOffloadEntriesAndInfoMetadata().
4129
4130 if (!CGM.getLangOpts().OpenMPIsDevice)
4131 return;
4132
4133 if (CGM.getLangOpts().OMPHostIRFile.empty())
4134 return;
4135
4136 auto Buf = llvm::MemoryBuffer::getFile(CGM.getLangOpts().OMPHostIRFile);
Alexey Bataev64e62dc2018-04-30 16:26:57 +00004137 if (auto EC = Buf.getError()) {
4138 CGM.getDiags().Report(diag::err_cannot_open_file)
4139 << CGM.getLangOpts().OMPHostIRFile << EC.message();
Samuel Antaoee8fb302016-01-06 13:42:12 +00004140 return;
Alexey Bataev64e62dc2018-04-30 16:26:57 +00004141 }
Samuel Antaoee8fb302016-01-06 13:42:12 +00004142
4143 llvm::LLVMContext C;
Peter Collingbourned9445c42016-11-13 07:00:17 +00004144 auto ME = expectedToErrorOrAndEmitErrors(
4145 C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C));
Samuel Antaoee8fb302016-01-06 13:42:12 +00004146
Alexey Bataev64e62dc2018-04-30 16:26:57 +00004147 if (auto EC = ME.getError()) {
4148 unsigned DiagID = CGM.getDiags().getCustomDiagID(
4149 DiagnosticsEngine::Error, "Unable to parse host IR file '%0':'%1'");
4150 CGM.getDiags().Report(DiagID)
4151 << CGM.getLangOpts().OMPHostIRFile << EC.message();
Samuel Antaoee8fb302016-01-06 13:42:12 +00004152 return;
Alexey Bataev64e62dc2018-04-30 16:26:57 +00004153 }
Samuel Antaoee8fb302016-01-06 13:42:12 +00004154
4155 llvm::NamedMDNode *MD = ME.get()->getNamedMetadata("omp_offload.info");
4156 if (!MD)
4157 return;
4158
George Burgess IV00f70bd2018-03-01 05:43:23 +00004159 for (llvm::MDNode *MN : MD->operands()) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004160 auto &&GetMDInt = [MN](unsigned Idx) {
4161 auto *V = cast<llvm::ConstantAsMetadata>(MN->getOperand(Idx));
Samuel Antaoee8fb302016-01-06 13:42:12 +00004162 return cast<llvm::ConstantInt>(V->getValue())->getZExtValue();
4163 };
4164
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004165 auto &&GetMDString = [MN](unsigned Idx) {
4166 auto *V = cast<llvm::MDString>(MN->getOperand(Idx));
Samuel Antaoee8fb302016-01-06 13:42:12 +00004167 return V->getString();
4168 };
4169
Alexey Bataev03f270c2018-03-30 18:31:07 +00004170 switch (GetMDInt(0)) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00004171 default:
4172 llvm_unreachable("Unexpected metadata!");
4173 break;
4174 case OffloadEntriesInfoManagerTy::OffloadEntryInfo::
Alexey Bataev34f8a702018-03-28 14:28:54 +00004175 OffloadingEntryInfoTargetRegion:
Samuel Antaoee8fb302016-01-06 13:42:12 +00004176 OffloadEntriesInfoManager.initializeTargetRegionEntryInfo(
Alexey Bataev03f270c2018-03-30 18:31:07 +00004177 /*DeviceID=*/GetMDInt(1), /*FileID=*/GetMDInt(2),
4178 /*ParentName=*/GetMDString(3), /*Line=*/GetMDInt(4),
4179 /*Order=*/GetMDInt(5));
4180 break;
4181 case OffloadEntriesInfoManagerTy::OffloadEntryInfo::
4182 OffloadingEntryInfoDeviceGlobalVar:
4183 OffloadEntriesInfoManager.initializeDeviceGlobalVarEntryInfo(
4184 /*MangledName=*/GetMDString(1),
4185 static_cast<OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind>(
4186 /*Flags=*/GetMDInt(2)),
4187 /*Order=*/GetMDInt(3));
Samuel Antaoee8fb302016-01-06 13:42:12 +00004188 break;
4189 }
4190 }
4191}
4192
Alexey Bataev62b63b12015-03-10 07:28:44 +00004193void CGOpenMPRuntime::emitKmpRoutineEntryT(QualType KmpInt32Ty) {
4194 if (!KmpRoutineEntryPtrTy) {
4195 // Build typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); type.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004196 ASTContext &C = CGM.getContext();
Alexey Bataev62b63b12015-03-10 07:28:44 +00004197 QualType KmpRoutineEntryTyArgs[] = {KmpInt32Ty, C.VoidPtrTy};
4198 FunctionProtoType::ExtProtoInfo EPI;
4199 KmpRoutineEntryPtrQTy = C.getPointerType(
4200 C.getFunctionType(KmpInt32Ty, KmpRoutineEntryTyArgs, EPI));
4201 KmpRoutineEntryPtrTy = CGM.getTypes().ConvertType(KmpRoutineEntryPtrQTy);
4202 }
4203}
4204
Samuel Antaoee8fb302016-01-06 13:42:12 +00004205QualType CGOpenMPRuntime::getTgtOffloadEntryQTy() {
Samuel Antaoee8fb302016-01-06 13:42:12 +00004206 // Make sure the type of the entry is already created. This is the type we
4207 // have to create:
4208 // struct __tgt_offload_entry{
4209 // void *addr; // Pointer to the offload entry info.
4210 // // (function or global)
4211 // char *name; // Name of the function or global.
4212 // size_t size; // Size of the entry info (0 if it a function).
Samuel Antaof83efdb2017-01-05 16:02:49 +00004213 // int32_t flags; // Flags associated with the entry, e.g. 'link'.
4214 // int32_t reserved; // Reserved, to use by the runtime library.
Samuel Antaoee8fb302016-01-06 13:42:12 +00004215 // };
4216 if (TgtOffloadEntryQTy.isNull()) {
4217 ASTContext &C = CGM.getContext();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004218 RecordDecl *RD = C.buildImplicitRecord("__tgt_offload_entry");
Samuel Antaoee8fb302016-01-06 13:42:12 +00004219 RD->startDefinition();
4220 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
4221 addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy));
4222 addFieldToRecordDecl(C, RD, C.getSizeType());
Samuel Antaof83efdb2017-01-05 16:02:49 +00004223 addFieldToRecordDecl(
4224 C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true));
4225 addFieldToRecordDecl(
4226 C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true));
Samuel Antaoee8fb302016-01-06 13:42:12 +00004227 RD->completeDefinition();
Jonas Hahnfeld5e4df282018-01-18 15:38:03 +00004228 RD->addAttr(PackedAttr::CreateImplicit(C));
Samuel Antaoee8fb302016-01-06 13:42:12 +00004229 TgtOffloadEntryQTy = C.getRecordType(RD);
4230 }
4231 return TgtOffloadEntryQTy;
4232}
4233
4234QualType CGOpenMPRuntime::getTgtDeviceImageQTy() {
4235 // These are the types we need to build:
4236 // struct __tgt_device_image{
4237 // void *ImageStart; // Pointer to the target code start.
4238 // void *ImageEnd; // Pointer to the target code end.
4239 // // We also add the host entries to the device image, as it may be useful
4240 // // for the target runtime to have access to that information.
4241 // __tgt_offload_entry *EntriesBegin; // Begin of the table with all
4242 // // the entries.
4243 // __tgt_offload_entry *EntriesEnd; // End of the table with all the
4244 // // entries (non inclusive).
4245 // };
4246 if (TgtDeviceImageQTy.isNull()) {
4247 ASTContext &C = CGM.getContext();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004248 RecordDecl *RD = C.buildImplicitRecord("__tgt_device_image");
Samuel Antaoee8fb302016-01-06 13:42:12 +00004249 RD->startDefinition();
4250 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
4251 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
4252 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy()));
4253 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy()));
4254 RD->completeDefinition();
4255 TgtDeviceImageQTy = C.getRecordType(RD);
4256 }
4257 return TgtDeviceImageQTy;
4258}
4259
4260QualType CGOpenMPRuntime::getTgtBinaryDescriptorQTy() {
4261 // struct __tgt_bin_desc{
4262 // int32_t NumDevices; // Number of devices supported.
4263 // __tgt_device_image *DeviceImages; // Arrays of device images
4264 // // (one per device).
4265 // __tgt_offload_entry *EntriesBegin; // Begin of the table with all the
4266 // // entries.
4267 // __tgt_offload_entry *EntriesEnd; // End of the table with all the
4268 // // entries (non inclusive).
4269 // };
4270 if (TgtBinaryDescriptorQTy.isNull()) {
4271 ASTContext &C = CGM.getContext();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004272 RecordDecl *RD = C.buildImplicitRecord("__tgt_bin_desc");
Samuel Antaoee8fb302016-01-06 13:42:12 +00004273 RD->startDefinition();
4274 addFieldToRecordDecl(
4275 C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true));
4276 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtDeviceImageQTy()));
4277 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy()));
4278 addFieldToRecordDecl(C, RD, C.getPointerType(getTgtOffloadEntryQTy()));
4279 RD->completeDefinition();
4280 TgtBinaryDescriptorQTy = C.getRecordType(RD);
4281 }
4282 return TgtBinaryDescriptorQTy;
4283}
4284
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004285namespace {
Alexey Bataev9e034042015-05-05 04:05:12 +00004286struct PrivateHelpersTy {
4287 PrivateHelpersTy(const VarDecl *Original, const VarDecl *PrivateCopy,
4288 const VarDecl *PrivateElemInit)
4289 : Original(Original), PrivateCopy(PrivateCopy),
4290 PrivateElemInit(PrivateElemInit) {}
4291 const VarDecl *Original;
4292 const VarDecl *PrivateCopy;
4293 const VarDecl *PrivateElemInit;
4294};
4295typedef std::pair<CharUnits /*Align*/, PrivateHelpersTy> PrivateDataTy;
Hans Wennborg7eb54642015-09-10 17:07:54 +00004296} // anonymous namespace
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004297
Alexey Bataev9e034042015-05-05 04:05:12 +00004298static RecordDecl *
Craig Topper8674c5c2015-09-29 04:30:07 +00004299createPrivatesRecordDecl(CodeGenModule &CGM, ArrayRef<PrivateDataTy> Privates) {
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004300 if (!Privates.empty()) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004301 ASTContext &C = CGM.getContext();
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004302 // Build struct .kmp_privates_t. {
4303 // /* private vars */
4304 // };
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004305 RecordDecl *RD = C.buildImplicitRecord(".kmp_privates.t");
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004306 RD->startDefinition();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004307 for (const auto &Pair : Privates) {
4308 const VarDecl *VD = Pair.second.Original;
4309 QualType Type = VD->getType().getNonReferenceType();
4310 FieldDecl *FD = addFieldToRecordDecl(C, RD, Type);
Alexey Bataevc71a4092015-09-11 10:29:41 +00004311 if (VD->hasAttrs()) {
4312 for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()),
4313 E(VD->getAttrs().end());
4314 I != E; ++I)
4315 FD->addAttr(*I);
4316 }
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004317 }
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004318 RD->completeDefinition();
4319 return RD;
4320 }
4321 return nullptr;
4322}
4323
Alexey Bataev9e034042015-05-05 04:05:12 +00004324static RecordDecl *
Alexey Bataev7292c292016-04-25 12:22:29 +00004325createKmpTaskTRecordDecl(CodeGenModule &CGM, OpenMPDirectiveKind Kind,
4326 QualType KmpInt32Ty,
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004327 QualType KmpRoutineEntryPointerQTy) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004328 ASTContext &C = CGM.getContext();
Alexey Bataev62b63b12015-03-10 07:28:44 +00004329 // Build struct kmp_task_t {
4330 // void * shareds;
4331 // kmp_routine_entry_t routine;
4332 // kmp_int32 part_id;
Alexey Bataevad537bb2016-05-30 09:06:50 +00004333 // kmp_cmplrdata_t data1;
4334 // kmp_cmplrdata_t data2;
Alexey Bataev7292c292016-04-25 12:22:29 +00004335 // For taskloops additional fields:
4336 // kmp_uint64 lb;
4337 // kmp_uint64 ub;
4338 // kmp_int64 st;
4339 // kmp_int32 liter;
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00004340 // void * reductions;
Alexey Bataev62b63b12015-03-10 07:28:44 +00004341 // };
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004342 RecordDecl *UD = C.buildImplicitRecord("kmp_cmplrdata_t", TTK_Union);
Alexey Bataevad537bb2016-05-30 09:06:50 +00004343 UD->startDefinition();
4344 addFieldToRecordDecl(C, UD, KmpInt32Ty);
4345 addFieldToRecordDecl(C, UD, KmpRoutineEntryPointerQTy);
4346 UD->completeDefinition();
4347 QualType KmpCmplrdataTy = C.getRecordType(UD);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004348 RecordDecl *RD = C.buildImplicitRecord("kmp_task_t");
Alexey Bataev62b63b12015-03-10 07:28:44 +00004349 RD->startDefinition();
4350 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
4351 addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy);
4352 addFieldToRecordDecl(C, RD, KmpInt32Ty);
Alexey Bataevad537bb2016-05-30 09:06:50 +00004353 addFieldToRecordDecl(C, RD, KmpCmplrdataTy);
4354 addFieldToRecordDecl(C, RD, KmpCmplrdataTy);
Alexey Bataev7292c292016-04-25 12:22:29 +00004355 if (isOpenMPTaskLoopDirective(Kind)) {
4356 QualType KmpUInt64Ty =
4357 CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4358 QualType KmpInt64Ty =
4359 CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4360 addFieldToRecordDecl(C, RD, KmpUInt64Ty);
4361 addFieldToRecordDecl(C, RD, KmpUInt64Ty);
4362 addFieldToRecordDecl(C, RD, KmpInt64Ty);
4363 addFieldToRecordDecl(C, RD, KmpInt32Ty);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00004364 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
Alexey Bataev7292c292016-04-25 12:22:29 +00004365 }
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004366 RD->completeDefinition();
4367 return RD;
4368}
4369
4370static RecordDecl *
4371createKmpTaskTWithPrivatesRecordDecl(CodeGenModule &CGM, QualType KmpTaskTQTy,
Craig Topper8674c5c2015-09-29 04:30:07 +00004372 ArrayRef<PrivateDataTy> Privates) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004373 ASTContext &C = CGM.getContext();
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004374 // Build struct kmp_task_t_with_privates {
4375 // kmp_task_t task_data;
4376 // .kmp_privates_t. privates;
4377 // };
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004378 RecordDecl *RD = C.buildImplicitRecord("kmp_task_t_with_privates");
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004379 RD->startDefinition();
4380 addFieldToRecordDecl(C, RD, KmpTaskTQTy);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004381 if (const RecordDecl *PrivateRD = createPrivatesRecordDecl(CGM, Privates))
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004382 addFieldToRecordDecl(C, RD, C.getRecordType(PrivateRD));
Alexey Bataev62b63b12015-03-10 07:28:44 +00004383 RD->completeDefinition();
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004384 return RD;
Alexey Bataev62b63b12015-03-10 07:28:44 +00004385}
4386
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004387/// Emit a proxy function which accepts kmp_task_t as the second
Alexey Bataev62b63b12015-03-10 07:28:44 +00004388/// argument.
4389/// \code
4390/// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00004391/// TaskFunction(gtid, tt->part_id, &tt->privates, task_privates_map, tt,
Alexey Bataev7292c292016-04-25 12:22:29 +00004392/// For taskloops:
4393/// tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter,
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00004394/// tt->reductions, tt->shareds);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004395/// return 0;
4396/// }
4397/// \endcode
James Y Knight9871db02019-02-05 16:42:33 +00004398static llvm::Function *
Alexey Bataev62b63b12015-03-10 07:28:44 +00004399emitProxyTaskFunction(CodeGenModule &CGM, SourceLocation Loc,
Alexey Bataev7292c292016-04-25 12:22:29 +00004400 OpenMPDirectiveKind Kind, QualType KmpInt32Ty,
4401 QualType KmpTaskTWithPrivatesPtrQTy,
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004402 QualType KmpTaskTWithPrivatesQTy, QualType KmpTaskTQTy,
James Y Knight9871db02019-02-05 16:42:33 +00004403 QualType SharedsPtrTy, llvm::Function *TaskFunction,
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004404 llvm::Value *TaskPrivatesMap) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004405 ASTContext &C = CGM.getContext();
Alexey Bataev62b63b12015-03-10 07:28:44 +00004406 FunctionArgList Args;
Alexey Bataev56223232017-06-09 13:40:18 +00004407 ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty,
4408 ImplicitParamDecl::Other);
4409 ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
4410 KmpTaskTWithPrivatesPtrQTy.withRestrict(),
4411 ImplicitParamDecl::Other);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004412 Args.push_back(&GtidArg);
4413 Args.push_back(&TaskTypeArg);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004414 const auto &TaskEntryFnInfo =
John McCallc56a8b32016-03-11 04:30:31 +00004415 CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004416 llvm::FunctionType *TaskEntryTy =
4417 CGM.getTypes().GetFunctionType(TaskEntryFnInfo);
Alexey Bataev18fa2322018-05-02 14:20:50 +00004418 std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_entry", ""});
4419 auto *TaskEntry = llvm::Function::Create(
4420 TaskEntryTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00004421 CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskEntry, TaskEntryFnInfo);
Alexey Bataevc0f879b2018-04-10 20:10:53 +00004422 TaskEntry->setDoesNotRecurse();
Alexey Bataev62b63b12015-03-10 07:28:44 +00004423 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00004424 CGF.StartFunction(GlobalDecl(), KmpInt32Ty, TaskEntry, TaskEntryFnInfo, Args,
4425 Loc, Loc);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004426
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004427 // TaskFunction(gtid, tt->task_data.part_id, &tt->privates, task_privates_map,
Alexey Bataev7292c292016-04-25 12:22:29 +00004428 // tt,
4429 // For taskloops:
4430 // tt->task_data.lb, tt->task_data.ub, tt->task_data.st, tt->task_data.liter,
4431 // tt->task_data.shareds);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004432 llvm::Value *GtidParam = CGF.EmitLoadOfScalar(
John McCall7f416cc2015-09-08 08:05:57 +00004433 CGF.GetAddrOfLocalVar(&GtidArg), /*Volatile=*/false, KmpInt32Ty, Loc);
Alexey Bataev31300ed2016-02-04 11:27:03 +00004434 LValue TDBase = CGF.EmitLoadOfPointerLValue(
4435 CGF.GetAddrOfLocalVar(&TaskTypeArg),
4436 KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004437 const auto *KmpTaskTWithPrivatesQTyRD =
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004438 cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004439 LValue Base =
4440 CGF.EmitLValueForField(TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin());
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004441 const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl());
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004442 auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004443 LValue PartIdLVal = CGF.EmitLValueForField(Base, *PartIdFI);
4444 llvm::Value *PartidParam = PartIdLVal.getPointer();
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004445
4446 auto SharedsFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTShareds);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004447 LValue SharedsLVal = CGF.EmitLValueForField(Base, *SharedsFI);
4448 llvm::Value *SharedsParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
Alexey Bataev1e491372018-01-23 18:44:14 +00004449 CGF.EmitLoadOfScalar(SharedsLVal, Loc),
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004450 CGF.ConvertTypeForMem(SharedsPtrTy));
4451
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004452 auto PrivatesFI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1);
4453 llvm::Value *PrivatesParam;
4454 if (PrivatesFI != KmpTaskTWithPrivatesQTyRD->field_end()) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004455 LValue PrivatesLVal = CGF.EmitLValueForField(TDBase, *PrivatesFI);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004456 PrivatesParam = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
John McCall7f416cc2015-09-08 08:05:57 +00004457 PrivatesLVal.getPointer(), CGF.VoidPtrTy);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004458 } else {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004459 PrivatesParam = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004460 }
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004461
Alexey Bataev7292c292016-04-25 12:22:29 +00004462 llvm::Value *CommonArgs[] = {GtidParam, PartidParam, PrivatesParam,
4463 TaskPrivatesMap,
4464 CGF.Builder
4465 .CreatePointerBitCastOrAddrSpaceCast(
4466 TDBase.getAddress(), CGF.VoidPtrTy)
4467 .getPointer()};
4468 SmallVector<llvm::Value *, 16> CallArgs(std::begin(CommonArgs),
4469 std::end(CommonArgs));
4470 if (isOpenMPTaskLoopDirective(Kind)) {
4471 auto LBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004472 LValue LBLVal = CGF.EmitLValueForField(Base, *LBFI);
4473 llvm::Value *LBParam = CGF.EmitLoadOfScalar(LBLVal, Loc);
Alexey Bataev7292c292016-04-25 12:22:29 +00004474 auto UBFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004475 LValue UBLVal = CGF.EmitLValueForField(Base, *UBFI);
4476 llvm::Value *UBParam = CGF.EmitLoadOfScalar(UBLVal, Loc);
Alexey Bataev7292c292016-04-25 12:22:29 +00004477 auto StFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTStride);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004478 LValue StLVal = CGF.EmitLValueForField(Base, *StFI);
4479 llvm::Value *StParam = CGF.EmitLoadOfScalar(StLVal, Loc);
Alexey Bataev7292c292016-04-25 12:22:29 +00004480 auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004481 LValue LILVal = CGF.EmitLValueForField(Base, *LIFI);
4482 llvm::Value *LIParam = CGF.EmitLoadOfScalar(LILVal, Loc);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00004483 auto RFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTReductions);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004484 LValue RLVal = CGF.EmitLValueForField(Base, *RFI);
4485 llvm::Value *RParam = CGF.EmitLoadOfScalar(RLVal, Loc);
Alexey Bataev7292c292016-04-25 12:22:29 +00004486 CallArgs.push_back(LBParam);
4487 CallArgs.push_back(UBParam);
4488 CallArgs.push_back(StParam);
4489 CallArgs.push_back(LIParam);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00004490 CallArgs.push_back(RParam);
Alexey Bataev7292c292016-04-25 12:22:29 +00004491 }
4492 CallArgs.push_back(SharedsParam);
4493
Alexey Bataev3c595a62017-08-14 15:01:03 +00004494 CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskFunction,
4495 CallArgs);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004496 CGF.EmitStoreThroughLValue(RValue::get(CGF.Builder.getInt32(/*C=*/0)),
4497 CGF.MakeAddrLValue(CGF.ReturnValue, KmpInt32Ty));
Alexey Bataev62b63b12015-03-10 07:28:44 +00004498 CGF.FinishFunction();
4499 return TaskEntry;
4500}
4501
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004502static llvm::Value *emitDestructorsFunction(CodeGenModule &CGM,
4503 SourceLocation Loc,
4504 QualType KmpInt32Ty,
4505 QualType KmpTaskTWithPrivatesPtrQTy,
4506 QualType KmpTaskTWithPrivatesQTy) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004507 ASTContext &C = CGM.getContext();
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004508 FunctionArgList Args;
Alexey Bataev56223232017-06-09 13:40:18 +00004509 ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty,
4510 ImplicitParamDecl::Other);
4511 ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
4512 KmpTaskTWithPrivatesPtrQTy.withRestrict(),
4513 ImplicitParamDecl::Other);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004514 Args.push_back(&GtidArg);
4515 Args.push_back(&TaskTypeArg);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004516 const auto &DestructorFnInfo =
John McCallc56a8b32016-03-11 04:30:31 +00004517 CGM.getTypes().arrangeBuiltinFunctionDeclaration(KmpInt32Ty, Args);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004518 llvm::FunctionType *DestructorFnTy =
4519 CGM.getTypes().GetFunctionType(DestructorFnInfo);
Alexey Bataev18fa2322018-05-02 14:20:50 +00004520 std::string Name =
4521 CGM.getOpenMPRuntime().getName({"omp_task_destructor", ""});
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004522 auto *DestructorFn =
4523 llvm::Function::Create(DestructorFnTy, llvm::GlobalValue::InternalLinkage,
Alexey Bataev18fa2322018-05-02 14:20:50 +00004524 Name, &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00004525 CGM.SetInternalFunctionAttributes(GlobalDecl(), DestructorFn,
Akira Hatanaka44a59f82015-10-28 02:30:47 +00004526 DestructorFnInfo);
Alexey Bataevc0f879b2018-04-10 20:10:53 +00004527 DestructorFn->setDoesNotRecurse();
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004528 CodeGenFunction CGF(CGM);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004529 CGF.StartFunction(GlobalDecl(), KmpInt32Ty, DestructorFn, DestructorFnInfo,
Alexey Bataev7cae94e2018-01-04 19:45:16 +00004530 Args, Loc, Loc);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004531
Alexey Bataev31300ed2016-02-04 11:27:03 +00004532 LValue Base = CGF.EmitLoadOfPointerLValue(
4533 CGF.GetAddrOfLocalVar(&TaskTypeArg),
4534 KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004535 const auto *KmpTaskTWithPrivatesQTyRD =
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004536 cast<RecordDecl>(KmpTaskTWithPrivatesQTy->getAsTagDecl());
4537 auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin());
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004538 Base = CGF.EmitLValueForField(Base, *FI);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004539 for (const auto *Field :
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004540 cast<RecordDecl>(FI->getType()->getAsTagDecl())->fields()) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004541 if (QualType::DestructionKind DtorKind =
4542 Field->getType().isDestructedType()) {
4543 LValue FieldLValue = CGF.EmitLValueForField(Base, Field);
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004544 CGF.pushDestroy(DtorKind, FieldLValue.getAddress(), Field->getType());
4545 }
4546 }
4547 CGF.FinishFunction();
4548 return DestructorFn;
4549}
4550
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004551/// Emit a privates mapping function for correct handling of private and
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004552/// firstprivate variables.
4553/// \code
4554/// void .omp_task_privates_map.(const .privates. *noalias privs, <ty1>
4555/// **noalias priv1,..., <tyn> **noalias privn) {
4556/// *priv1 = &.privates.priv1;
4557/// ...;
4558/// *privn = &.privates.privn;
4559/// }
4560/// \endcode
4561static llvm::Value *
4562emitTaskPrivateMappingFunction(CodeGenModule &CGM, SourceLocation Loc,
Craig Topper8674c5c2015-09-29 04:30:07 +00004563 ArrayRef<const Expr *> PrivateVars,
4564 ArrayRef<const Expr *> FirstprivateVars,
Alexey Bataevf93095a2016-05-05 08:46:22 +00004565 ArrayRef<const Expr *> LastprivateVars,
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004566 QualType PrivatesQTy,
Craig Topper8674c5c2015-09-29 04:30:07 +00004567 ArrayRef<PrivateDataTy> Privates) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004568 ASTContext &C = CGM.getContext();
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004569 FunctionArgList Args;
4570 ImplicitParamDecl TaskPrivatesArg(
4571 C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
Alexey Bataev56223232017-06-09 13:40:18 +00004572 C.getPointerType(PrivatesQTy).withConst().withRestrict(),
4573 ImplicitParamDecl::Other);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004574 Args.push_back(&TaskPrivatesArg);
4575 llvm::DenseMap<const VarDecl *, unsigned> PrivateVarsPos;
4576 unsigned Counter = 1;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004577 for (const Expr *E : PrivateVars) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004578 Args.push_back(ImplicitParamDecl::Create(
Alexey Bataev56223232017-06-09 13:40:18 +00004579 C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
4580 C.getPointerType(C.getPointerType(E->getType()))
4581 .withConst()
4582 .withRestrict(),
4583 ImplicitParamDecl::Other));
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004584 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004585 PrivateVarsPos[VD] = Counter;
4586 ++Counter;
4587 }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004588 for (const Expr *E : FirstprivateVars) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004589 Args.push_back(ImplicitParamDecl::Create(
Alexey Bataev56223232017-06-09 13:40:18 +00004590 C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
4591 C.getPointerType(C.getPointerType(E->getType()))
4592 .withConst()
4593 .withRestrict(),
4594 ImplicitParamDecl::Other));
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004595 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004596 PrivateVarsPos[VD] = Counter;
4597 ++Counter;
4598 }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004599 for (const Expr *E : LastprivateVars) {
Alexey Bataevf93095a2016-05-05 08:46:22 +00004600 Args.push_back(ImplicitParamDecl::Create(
Alexey Bataev56223232017-06-09 13:40:18 +00004601 C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
4602 C.getPointerType(C.getPointerType(E->getType()))
4603 .withConst()
4604 .withRestrict(),
4605 ImplicitParamDecl::Other));
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004606 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
Alexey Bataevf93095a2016-05-05 08:46:22 +00004607 PrivateVarsPos[VD] = Counter;
4608 ++Counter;
4609 }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004610 const auto &TaskPrivatesMapFnInfo =
John McCallc56a8b32016-03-11 04:30:31 +00004611 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004612 llvm::FunctionType *TaskPrivatesMapTy =
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004613 CGM.getTypes().GetFunctionType(TaskPrivatesMapFnInfo);
Alexey Bataev18fa2322018-05-02 14:20:50 +00004614 std::string Name =
4615 CGM.getOpenMPRuntime().getName({"omp_task_privates_map", ""});
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004616 auto *TaskPrivatesMap = llvm::Function::Create(
Alexey Bataev18fa2322018-05-02 14:20:50 +00004617 TaskPrivatesMapTy, llvm::GlobalValue::InternalLinkage, Name,
4618 &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00004619 CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskPrivatesMap,
Akira Hatanaka44a59f82015-10-28 02:30:47 +00004620 TaskPrivatesMapFnInfo);
Chandler Carruthfcd33142016-12-23 01:24:49 +00004621 TaskPrivatesMap->removeFnAttr(llvm::Attribute::NoInline);
Mehdi Amini6aa9e9b2017-05-29 05:38:20 +00004622 TaskPrivatesMap->removeFnAttr(llvm::Attribute::OptimizeNone);
Evgeniy Stepanov6b2a61d2015-09-14 21:35:16 +00004623 TaskPrivatesMap->addFnAttr(llvm::Attribute::AlwaysInline);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004624 CodeGenFunction CGF(CGM);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004625 CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskPrivatesMap,
Alexey Bataev7cae94e2018-01-04 19:45:16 +00004626 TaskPrivatesMapFnInfo, Args, Loc, Loc);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004627
4628 // *privi = &.privates.privi;
Alexey Bataev31300ed2016-02-04 11:27:03 +00004629 LValue Base = CGF.EmitLoadOfPointerLValue(
4630 CGF.GetAddrOfLocalVar(&TaskPrivatesArg),
4631 TaskPrivatesArg.getType()->castAs<PointerType>());
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004632 const auto *PrivatesQTyRD = cast<RecordDecl>(PrivatesQTy->getAsTagDecl());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004633 Counter = 0;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004634 for (const FieldDecl *Field : PrivatesQTyRD->fields()) {
4635 LValue FieldLVal = CGF.EmitLValueForField(Base, Field);
4636 const VarDecl *VD = Args[PrivateVarsPos[Privates[Counter].second.Original]];
4637 LValue RefLVal =
4638 CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType());
4639 LValue RefLoadLVal = CGF.EmitLoadOfPointerLValue(
Alexey Bataev31300ed2016-02-04 11:27:03 +00004640 RefLVal.getAddress(), RefLVal.getType()->castAs<PointerType>());
Alexey Bataev2377fe92015-09-10 08:12:02 +00004641 CGF.EmitStoreOfScalar(FieldLVal.getPointer(), RefLoadLVal);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004642 ++Counter;
4643 }
4644 CGF.FinishFunction();
4645 return TaskPrivatesMap;
4646}
4647
Mandeep Singh Grangb14fb6a22017-11-28 20:41:13 +00004648static bool stable_sort_comparator(const PrivateDataTy P1,
4649 const PrivateDataTy P2) {
4650 return P1.first > P2.first;
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004651}
4652
Alexey Bataevf93095a2016-05-05 08:46:22 +00004653/// Emit initialization for private variables in task-based directives.
Alexey Bataev8a831592016-05-10 10:36:51 +00004654static void emitPrivatesInit(CodeGenFunction &CGF,
Alexey Bataevf93095a2016-05-05 08:46:22 +00004655 const OMPExecutableDirective &D,
4656 Address KmpTaskSharedsPtr, LValue TDBase,
4657 const RecordDecl *KmpTaskTWithPrivatesQTyRD,
4658 QualType SharedsTy, QualType SharedsPtrTy,
4659 const OMPTaskDataTy &Data,
4660 ArrayRef<PrivateDataTy> Privates, bool ForDup) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004661 ASTContext &C = CGF.getContext();
Alexey Bataevf93095a2016-05-05 08:46:22 +00004662 auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin());
4663 LValue PrivatesBase = CGF.EmitLValueForField(TDBase, *FI);
Alexey Bataev8451efa2018-01-15 19:06:12 +00004664 OpenMPDirectiveKind Kind = isOpenMPTaskLoopDirective(D.getDirectiveKind())
4665 ? OMPD_taskloop
4666 : OMPD_task;
4667 const CapturedStmt &CS = *D.getCapturedStmt(Kind);
4668 CodeGenFunction::CGCapturedStmtInfo CapturesInfo(CS);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004669 LValue SrcBase;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00004670 bool IsTargetTask =
4671 isOpenMPTargetDataManagementDirective(D.getDirectiveKind()) ||
4672 isOpenMPTargetExecutionDirective(D.getDirectiveKind());
4673 // For target-based directives skip 3 firstprivate arrays BasePointersArray,
4674 // PointersArray and SizesArray. The original variables for these arrays are
4675 // not captured and we get their addresses explicitly.
4676 if ((!IsTargetTask && !Data.FirstprivateVars.empty()) ||
Alexey Bataev8451efa2018-01-15 19:06:12 +00004677 (IsTargetTask && KmpTaskSharedsPtr.isValid())) {
Alexey Bataevf93095a2016-05-05 08:46:22 +00004678 SrcBase = CGF.MakeAddrLValue(
4679 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4680 KmpTaskSharedsPtr, CGF.ConvertTypeForMem(SharedsPtrTy)),
4681 SharedsTy);
4682 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004683 FI = cast<RecordDecl>(FI->getType()->getAsTagDecl())->field_begin();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004684 for (const PrivateDataTy &Pair : Privates) {
4685 const VarDecl *VD = Pair.second.PrivateCopy;
4686 const Expr *Init = VD->getAnyInitializer();
Alexey Bataevf93095a2016-05-05 08:46:22 +00004687 if (Init && (!ForDup || (isa<CXXConstructExpr>(Init) &&
4688 !CGF.isTrivialInitializer(Init)))) {
Alexey Bataev8a831592016-05-10 10:36:51 +00004689 LValue PrivateLValue = CGF.EmitLValueForField(PrivatesBase, *FI);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004690 if (const VarDecl *Elem = Pair.second.PrivateElemInit) {
4691 const VarDecl *OriginalVD = Pair.second.Original;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00004692 // Check if the variable is the target-based BasePointersArray,
4693 // PointersArray or SizesArray.
4694 LValue SharedRefLValue;
Alexey Bataevf93095a2016-05-05 08:46:22 +00004695 QualType Type = OriginalVD->getType();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004696 const FieldDecl *SharedField = CapturesInfo.lookup(OriginalVD);
Alexey Bataev8451efa2018-01-15 19:06:12 +00004697 if (IsTargetTask && !SharedField) {
4698 assert(isa<ImplicitParamDecl>(OriginalVD) &&
4699 isa<CapturedDecl>(OriginalVD->getDeclContext()) &&
4700 cast<CapturedDecl>(OriginalVD->getDeclContext())
4701 ->getNumParams() == 0 &&
4702 isa<TranslationUnitDecl>(
4703 cast<CapturedDecl>(OriginalVD->getDeclContext())
4704 ->getDeclContext()) &&
4705 "Expected artificial target data variable.");
Alexey Bataevd2202ca2017-12-27 17:58:32 +00004706 SharedRefLValue =
4707 CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(OriginalVD), Type);
4708 } else {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00004709 SharedRefLValue = CGF.EmitLValueForField(SrcBase, SharedField);
4710 SharedRefLValue = CGF.MakeAddrLValue(
4711 Address(SharedRefLValue.getPointer(), C.getDeclAlign(OriginalVD)),
4712 SharedRefLValue.getType(), LValueBaseInfo(AlignmentSource::Decl),
4713 SharedRefLValue.getTBAAInfo());
4714 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004715 if (Type->isArrayType()) {
4716 // Initialize firstprivate array.
4717 if (!isa<CXXConstructExpr>(Init) || CGF.isTrivialInitializer(Init)) {
4718 // Perform simple memcpy.
Ivan A. Kosarev1860b522018-01-25 14:21:55 +00004719 CGF.EmitAggregateAssign(PrivateLValue, SharedRefLValue, Type);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004720 } else {
4721 // Initialize firstprivate array using element-by-element
Simon Pilgrim2c518802017-03-30 14:13:19 +00004722 // initialization.
Alexey Bataevf93095a2016-05-05 08:46:22 +00004723 CGF.EmitOMPAggregateAssign(
4724 PrivateLValue.getAddress(), SharedRefLValue.getAddress(), Type,
4725 [&CGF, Elem, Init, &CapturesInfo](Address DestElement,
4726 Address SrcElement) {
4727 // Clean up any temporaries needed by the initialization.
4728 CodeGenFunction::OMPPrivateScope InitScope(CGF);
4729 InitScope.addPrivate(
4730 Elem, [SrcElement]() -> Address { return SrcElement; });
4731 (void)InitScope.Privatize();
4732 // Emit initialization for single element.
4733 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(
4734 CGF, &CapturesInfo);
4735 CGF.EmitAnyExprToMem(Init, DestElement,
4736 Init->getType().getQualifiers(),
4737 /*IsInitializer=*/false);
4738 });
4739 }
4740 } else {
4741 CodeGenFunction::OMPPrivateScope InitScope(CGF);
4742 InitScope.addPrivate(Elem, [SharedRefLValue]() -> Address {
4743 return SharedRefLValue.getAddress();
4744 });
4745 (void)InitScope.Privatize();
4746 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CapturesInfo);
4747 CGF.EmitExprAsInit(Init, VD, PrivateLValue,
4748 /*capturedByInit=*/false);
4749 }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004750 } else {
Alexey Bataevf93095a2016-05-05 08:46:22 +00004751 CGF.EmitExprAsInit(Init, VD, PrivateLValue, /*capturedByInit=*/false);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004752 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004753 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004754 ++FI;
4755 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004756}
4757
4758/// Check if duplication function is required for taskloops.
4759static bool checkInitIsRequired(CodeGenFunction &CGF,
4760 ArrayRef<PrivateDataTy> Privates) {
4761 bool InitRequired = false;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004762 for (const PrivateDataTy &Pair : Privates) {
4763 const VarDecl *VD = Pair.second.PrivateCopy;
4764 const Expr *Init = VD->getAnyInitializer();
Alexey Bataevf93095a2016-05-05 08:46:22 +00004765 InitRequired = InitRequired || (Init && isa<CXXConstructExpr>(Init) &&
4766 !CGF.isTrivialInitializer(Init));
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004767 if (InitRequired)
4768 break;
Alexey Bataevf93095a2016-05-05 08:46:22 +00004769 }
4770 return InitRequired;
4771}
4772
4773
4774/// Emit task_dup function (for initialization of
4775/// private/firstprivate/lastprivate vars and last_iter flag)
4776/// \code
4777/// void __task_dup_entry(kmp_task_t *task_dst, const kmp_task_t *task_src, int
4778/// lastpriv) {
4779/// // setup lastprivate flag
4780/// task_dst->last = lastpriv;
4781/// // could be constructor calls here...
4782/// }
4783/// \endcode
4784static llvm::Value *
4785emitTaskDupFunction(CodeGenModule &CGM, SourceLocation Loc,
4786 const OMPExecutableDirective &D,
4787 QualType KmpTaskTWithPrivatesPtrQTy,
4788 const RecordDecl *KmpTaskTWithPrivatesQTyRD,
4789 const RecordDecl *KmpTaskTQTyRD, QualType SharedsTy,
4790 QualType SharedsPtrTy, const OMPTaskDataTy &Data,
4791 ArrayRef<PrivateDataTy> Privates, bool WithLastIter) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004792 ASTContext &C = CGM.getContext();
Alexey Bataevf93095a2016-05-05 08:46:22 +00004793 FunctionArgList Args;
Alexey Bataev56223232017-06-09 13:40:18 +00004794 ImplicitParamDecl DstArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
4795 KmpTaskTWithPrivatesPtrQTy,
4796 ImplicitParamDecl::Other);
4797 ImplicitParamDecl SrcArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
4798 KmpTaskTWithPrivatesPtrQTy,
4799 ImplicitParamDecl::Other);
4800 ImplicitParamDecl LastprivArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.IntTy,
4801 ImplicitParamDecl::Other);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004802 Args.push_back(&DstArg);
4803 Args.push_back(&SrcArg);
4804 Args.push_back(&LastprivArg);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004805 const auto &TaskDupFnInfo =
Alexey Bataevf93095a2016-05-05 08:46:22 +00004806 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004807 llvm::FunctionType *TaskDupTy = CGM.getTypes().GetFunctionType(TaskDupFnInfo);
Alexey Bataev18fa2322018-05-02 14:20:50 +00004808 std::string Name = CGM.getOpenMPRuntime().getName({"omp_task_dup", ""});
4809 auto *TaskDup = llvm::Function::Create(
4810 TaskDupTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00004811 CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskDup, TaskDupFnInfo);
Alexey Bataevc0f879b2018-04-10 20:10:53 +00004812 TaskDup->setDoesNotRecurse();
Alexey Bataevf93095a2016-05-05 08:46:22 +00004813 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00004814 CGF.StartFunction(GlobalDecl(), C.VoidTy, TaskDup, TaskDupFnInfo, Args, Loc,
4815 Loc);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004816
4817 LValue TDBase = CGF.EmitLoadOfPointerLValue(
4818 CGF.GetAddrOfLocalVar(&DstArg),
4819 KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
4820 // task_dst->liter = lastpriv;
4821 if (WithLastIter) {
4822 auto LIFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTLastIter);
4823 LValue Base = CGF.EmitLValueForField(
4824 TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin());
4825 LValue LILVal = CGF.EmitLValueForField(Base, *LIFI);
4826 llvm::Value *Lastpriv = CGF.EmitLoadOfScalar(
4827 CGF.GetAddrOfLocalVar(&LastprivArg), /*Volatile=*/false, C.IntTy, Loc);
4828 CGF.EmitStoreOfScalar(Lastpriv, LILVal);
4829 }
4830
4831 // Emit initial values for private copies (if any).
4832 assert(!Privates.empty());
4833 Address KmpTaskSharedsPtr = Address::invalid();
4834 if (!Data.FirstprivateVars.empty()) {
4835 LValue TDBase = CGF.EmitLoadOfPointerLValue(
4836 CGF.GetAddrOfLocalVar(&SrcArg),
4837 KmpTaskTWithPrivatesPtrQTy->castAs<PointerType>());
4838 LValue Base = CGF.EmitLValueForField(
4839 TDBase, *KmpTaskTWithPrivatesQTyRD->field_begin());
4840 KmpTaskSharedsPtr = Address(
4841 CGF.EmitLoadOfScalar(CGF.EmitLValueForField(
4842 Base, *std::next(KmpTaskTQTyRD->field_begin(),
4843 KmpTaskTShareds)),
4844 Loc),
4845 CGF.getNaturalTypeAlignment(SharedsTy));
4846 }
Alexey Bataev8a831592016-05-10 10:36:51 +00004847 emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, TDBase, KmpTaskTWithPrivatesQTyRD,
4848 SharedsTy, SharedsPtrTy, Data, Privates, /*ForDup=*/true);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004849 CGF.FinishFunction();
4850 return TaskDup;
4851}
4852
Alexey Bataev8a831592016-05-10 10:36:51 +00004853/// Checks if destructor function is required to be generated.
4854/// \return true if cleanups are required, false otherwise.
4855static bool
4856checkDestructorsRequired(const RecordDecl *KmpTaskTWithPrivatesQTyRD) {
4857 bool NeedsCleanup = false;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004858 auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin(), 1);
4859 const auto *PrivateRD = cast<RecordDecl>(FI->getType()->getAsTagDecl());
4860 for (const FieldDecl *FD : PrivateRD->fields()) {
Alexey Bataev8a831592016-05-10 10:36:51 +00004861 NeedsCleanup = NeedsCleanup || FD->getType().isDestructedType();
4862 if (NeedsCleanup)
4863 break;
4864 }
4865 return NeedsCleanup;
4866}
4867
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004868CGOpenMPRuntime::TaskResultTy
4869CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
4870 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00004871 llvm::Function *TaskFunction, QualType SharedsTy,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004872 Address Shareds, const OMPTaskDataTy &Data) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004873 ASTContext &C = CGM.getContext();
Alexey Bataev7292c292016-04-25 12:22:29 +00004874 llvm::SmallVector<PrivateDataTy, 4> Privates;
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004875 // Aggregate privates and sort them by the alignment.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004876 auto I = Data.PrivateCopies.begin();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004877 for (const Expr *E : Data.PrivateVars) {
4878 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
Alexey Bataev43a919f2018-04-13 17:48:43 +00004879 Privates.emplace_back(
Alexey Bataevc71a4092015-09-11 10:29:41 +00004880 C.getDeclAlign(VD),
Alexey Bataev9e034042015-05-05 04:05:12 +00004881 PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()),
Alexey Bataev43a919f2018-04-13 17:48:43 +00004882 /*PrivateElemInit=*/nullptr));
Alexey Bataev36c1eb92015-04-30 06:51:57 +00004883 ++I;
4884 }
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004885 I = Data.FirstprivateCopies.begin();
4886 auto IElemInitRef = Data.FirstprivateInits.begin();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004887 for (const Expr *E : Data.FirstprivateVars) {
4888 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
Alexey Bataev43a919f2018-04-13 17:48:43 +00004889 Privates.emplace_back(
Alexey Bataevc71a4092015-09-11 10:29:41 +00004890 C.getDeclAlign(VD),
Alexey Bataev9e034042015-05-05 04:05:12 +00004891 PrivateHelpersTy(
4892 VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()),
Alexey Bataev43a919f2018-04-13 17:48:43 +00004893 cast<VarDecl>(cast<DeclRefExpr>(*IElemInitRef)->getDecl())));
Richard Trieucc3949d2016-02-18 22:34:54 +00004894 ++I;
4895 ++IElemInitRef;
Alexey Bataev9e034042015-05-05 04:05:12 +00004896 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004897 I = Data.LastprivateCopies.begin();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004898 for (const Expr *E : Data.LastprivateVars) {
4899 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
Alexey Bataev43a919f2018-04-13 17:48:43 +00004900 Privates.emplace_back(
Alexey Bataevf93095a2016-05-05 08:46:22 +00004901 C.getDeclAlign(VD),
4902 PrivateHelpersTy(VD, cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()),
Alexey Bataev43a919f2018-04-13 17:48:43 +00004903 /*PrivateElemInit=*/nullptr));
Alexey Bataevf93095a2016-05-05 08:46:22 +00004904 ++I;
4905 }
Mandeep Singh Grangb14fb6a22017-11-28 20:41:13 +00004906 std::stable_sort(Privates.begin(), Privates.end(), stable_sort_comparator);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004907 QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004908 // Build type kmp_routine_entry_t (if not built yet).
4909 emitKmpRoutineEntryT(KmpInt32Ty);
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004910 // Build type kmp_task_t (if not built yet).
Alexey Bataeve213f3e2017-10-11 15:29:40 +00004911 if (isOpenMPTaskLoopDirective(D.getDirectiveKind())) {
4912 if (SavedKmpTaskloopTQTy.isNull()) {
4913 SavedKmpTaskloopTQTy = C.getRecordType(createKmpTaskTRecordDecl(
4914 CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy));
4915 }
4916 KmpTaskTQTy = SavedKmpTaskloopTQTy;
Alexey Bataev3a03a7f2017-10-11 15:56:38 +00004917 } else {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00004918 assert((D.getDirectiveKind() == OMPD_task ||
4919 isOpenMPTargetExecutionDirective(D.getDirectiveKind()) ||
4920 isOpenMPTargetDataManagementDirective(D.getDirectiveKind())) &&
4921 "Expected taskloop, task or target directive");
Alexey Bataeve213f3e2017-10-11 15:29:40 +00004922 if (SavedKmpTaskTQTy.isNull()) {
4923 SavedKmpTaskTQTy = C.getRecordType(createKmpTaskTRecordDecl(
4924 CGM, D.getDirectiveKind(), KmpInt32Ty, KmpRoutineEntryPtrQTy));
4925 }
4926 KmpTaskTQTy = SavedKmpTaskTQTy;
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004927 }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004928 const auto *KmpTaskTQTyRD = cast<RecordDecl>(KmpTaskTQTy->getAsTagDecl());
Alexey Bataev62b63b12015-03-10 07:28:44 +00004929 // Build particular struct kmp_task_t for the given task.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004930 const RecordDecl *KmpTaskTWithPrivatesQTyRD =
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004931 createKmpTaskTWithPrivatesRecordDecl(CGM, KmpTaskTQTy, Privates);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004932 QualType KmpTaskTWithPrivatesQTy = C.getRecordType(KmpTaskTWithPrivatesQTyRD);
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00004933 QualType KmpTaskTWithPrivatesPtrQTy =
4934 C.getPointerType(KmpTaskTWithPrivatesQTy);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004935 llvm::Type *KmpTaskTWithPrivatesTy = CGF.ConvertType(KmpTaskTWithPrivatesQTy);
4936 llvm::Type *KmpTaskTWithPrivatesPtrTy =
4937 KmpTaskTWithPrivatesTy->getPointerTo();
4938 llvm::Value *KmpTaskTWithPrivatesTySize =
4939 CGF.getTypeSize(KmpTaskTWithPrivatesQTy);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004940 QualType SharedsPtrTy = C.getPointerType(SharedsTy);
4941
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004942 // Emit initial values for private copies (if any).
4943 llvm::Value *TaskPrivatesMap = nullptr;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004944 llvm::Type *TaskPrivatesMapTy =
James Y Knight9871db02019-02-05 16:42:33 +00004945 std::next(TaskFunction->arg_begin(), 3)->getType();
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004946 if (!Privates.empty()) {
4947 auto FI = std::next(KmpTaskTWithPrivatesQTyRD->field_begin());
Alexey Bataevf93095a2016-05-05 08:46:22 +00004948 TaskPrivatesMap = emitTaskPrivateMappingFunction(
4949 CGM, Loc, Data.PrivateVars, Data.FirstprivateVars, Data.LastprivateVars,
4950 FI->getType(), Privates);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00004951 TaskPrivatesMap = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4952 TaskPrivatesMap, TaskPrivatesMapTy);
4953 } else {
4954 TaskPrivatesMap = llvm::ConstantPointerNull::get(
4955 cast<llvm::PointerType>(TaskPrivatesMapTy));
4956 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00004957 // Build a proxy function kmp_int32 .omp_task_entry.(kmp_int32 gtid,
4958 // kmp_task_t *tt);
James Y Knight9871db02019-02-05 16:42:33 +00004959 llvm::Function *TaskEntry = emitProxyTaskFunction(
Alexey Bataev7292c292016-04-25 12:22:29 +00004960 CGM, Loc, D.getDirectiveKind(), KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy,
4961 KmpTaskTWithPrivatesQTy, KmpTaskTQTy, SharedsPtrTy, TaskFunction,
4962 TaskPrivatesMap);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004963
4964 // Build call kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
4965 // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
4966 // kmp_routine_entry_t *task_entry);
4967 // Task flags. Format is taken from
James Y Knight5d71fc52019-01-29 16:37:27 +00004968 // https://github.com/llvm/llvm-project/blob/master/openmp/runtime/src/kmp.h,
Alexey Bataev62b63b12015-03-10 07:28:44 +00004969 // description of kmp_tasking_flags struct.
Alexey Bataev1e1e2862016-05-10 12:21:02 +00004970 enum {
4971 TiedFlag = 0x1,
4972 FinalFlag = 0x2,
4973 DestructorsFlag = 0x8,
4974 PriorityFlag = 0x20
4975 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004976 unsigned Flags = Data.Tied ? TiedFlag : 0;
Alexey Bataev8a831592016-05-10 10:36:51 +00004977 bool NeedsCleanup = false;
4978 if (!Privates.empty()) {
4979 NeedsCleanup = checkDestructorsRequired(KmpTaskTWithPrivatesQTyRD);
4980 if (NeedsCleanup)
4981 Flags = Flags | DestructorsFlag;
4982 }
Alexey Bataev1e1e2862016-05-10 12:21:02 +00004983 if (Data.Priority.getInt())
4984 Flags = Flags | PriorityFlag;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004985 llvm::Value *TaskFlags =
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004986 Data.Final.getPointer()
4987 ? CGF.Builder.CreateSelect(Data.Final.getPointer(),
Alexey Bataev62b63b12015-03-10 07:28:44 +00004988 CGF.Builder.getInt32(FinalFlag),
4989 CGF.Builder.getInt32(/*C=*/0))
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004990 : CGF.Builder.getInt32(Data.Final.getInt() ? FinalFlag : 0);
Alexey Bataev62b63b12015-03-10 07:28:44 +00004991 TaskFlags = CGF.Builder.CreateOr(TaskFlags, CGF.Builder.getInt32(Flags));
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004992 llvm::Value *SharedsSize = CGM.getSize(C.getTypeSizeInChars(SharedsTy));
Alexey Bataevf24e7b12015-10-08 09:10:53 +00004993 llvm::Value *AllocArgs[] = {emitUpdateLocation(CGF, Loc),
4994 getThreadID(CGF, Loc), TaskFlags,
4995 KmpTaskTWithPrivatesTySize, SharedsSize,
4996 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
4997 TaskEntry, KmpRoutineEntryPtrTy)};
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00004998 llvm::Value *NewTask = CGF.EmitRuntimeCall(
Alexey Bataev62b63b12015-03-10 07:28:44 +00004999 createRuntimeFunction(OMPRTL__kmpc_omp_task_alloc), AllocArgs);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005000 llvm::Value *NewTaskNewTaskTTy =
5001 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5002 NewTask, KmpTaskTWithPrivatesPtrTy);
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00005003 LValue Base = CGF.MakeNaturalAlignAddrLValue(NewTaskNewTaskTTy,
5004 KmpTaskTWithPrivatesQTy);
5005 LValue TDBase =
5006 CGF.EmitLValueForField(Base, *KmpTaskTWithPrivatesQTyRD->field_begin());
Alexey Bataev62b63b12015-03-10 07:28:44 +00005007 // Fill the data in the resulting kmp_task_t record.
5008 // Copy shareds if there are any.
John McCall7f416cc2015-09-08 08:05:57 +00005009 Address KmpTaskSharedsPtr = Address::invalid();
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00005010 if (!SharedsTy->getAsStructureType()->getDecl()->field_empty()) {
Alexey Bataev2377fe92015-09-10 08:12:02 +00005011 KmpTaskSharedsPtr =
5012 Address(CGF.EmitLoadOfScalar(
5013 CGF.EmitLValueForField(
5014 TDBase, *std::next(KmpTaskTQTyRD->field_begin(),
5015 KmpTaskTShareds)),
5016 Loc),
5017 CGF.getNaturalTypeAlignment(SharedsTy));
Ivan A. Kosarev1860b522018-01-25 14:21:55 +00005018 LValue Dest = CGF.MakeAddrLValue(KmpTaskSharedsPtr, SharedsTy);
5019 LValue Src = CGF.MakeAddrLValue(Shareds, SharedsTy);
Richard Smithe78fac52018-04-05 20:52:58 +00005020 CGF.EmitAggregateCopy(Dest, Src, SharedsTy, AggValueSlot::DoesNotOverlap);
Alexey Bataev8fc69dc2015-05-18 07:54:53 +00005021 }
Alexey Bataev36c1eb92015-04-30 06:51:57 +00005022 // Emit initial values for private copies (if any).
Alexey Bataevf93095a2016-05-05 08:46:22 +00005023 TaskResultTy Result;
Alexey Bataev36c1eb92015-04-30 06:51:57 +00005024 if (!Privates.empty()) {
Alexey Bataev8a831592016-05-10 10:36:51 +00005025 emitPrivatesInit(CGF, D, KmpTaskSharedsPtr, Base, KmpTaskTWithPrivatesQTyRD,
5026 SharedsTy, SharedsPtrTy, Data, Privates,
5027 /*ForDup=*/false);
Alexey Bataevf93095a2016-05-05 08:46:22 +00005028 if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) &&
5029 (!Data.LastprivateVars.empty() || checkInitIsRequired(CGF, Privates))) {
5030 Result.TaskDupFn = emitTaskDupFunction(
5031 CGM, Loc, D, KmpTaskTWithPrivatesPtrQTy, KmpTaskTWithPrivatesQTyRD,
5032 KmpTaskTQTyRD, SharedsTy, SharedsPtrTy, Data, Privates,
5033 /*WithLastIter=*/!Data.LastprivateVars.empty());
Alexey Bataev36c1eb92015-04-30 06:51:57 +00005034 }
5035 }
Alexey Bataevad537bb2016-05-30 09:06:50 +00005036 // Fields of union "kmp_cmplrdata_t" for destructors and priority.
5037 enum { Priority = 0, Destructors = 1 };
Alexey Bataev62b63b12015-03-10 07:28:44 +00005038 // Provide pointer to function with destructors for privates.
Alexey Bataevad537bb2016-05-30 09:06:50 +00005039 auto FI = std::next(KmpTaskTQTyRD->field_begin(), Data1);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005040 const RecordDecl *KmpCmplrdataUD =
5041 (*FI)->getType()->getAsUnionType()->getDecl();
Alexey Bataevad537bb2016-05-30 09:06:50 +00005042 if (NeedsCleanup) {
5043 llvm::Value *DestructorFn = emitDestructorsFunction(
5044 CGM, Loc, KmpInt32Ty, KmpTaskTWithPrivatesPtrQTy,
5045 KmpTaskTWithPrivatesQTy);
5046 LValue Data1LV = CGF.EmitLValueForField(TDBase, *FI);
5047 LValue DestructorsLV = CGF.EmitLValueForField(
5048 Data1LV, *std::next(KmpCmplrdataUD->field_begin(), Destructors));
5049 CGF.EmitStoreOfScalar(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5050 DestructorFn, KmpRoutineEntryPtrTy),
5051 DestructorsLV);
5052 }
5053 // Set priority.
5054 if (Data.Priority.getInt()) {
5055 LValue Data2LV = CGF.EmitLValueForField(
5056 TDBase, *std::next(KmpTaskTQTyRD->field_begin(), Data2));
5057 LValue PriorityLV = CGF.EmitLValueForField(
5058 Data2LV, *std::next(KmpCmplrdataUD->field_begin(), Priority));
5059 CGF.EmitStoreOfScalar(Data.Priority.getPointer(), PriorityLV);
5060 }
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005061 Result.NewTask = NewTask;
5062 Result.TaskEntry = TaskEntry;
5063 Result.NewTaskNewTaskTTy = NewTaskNewTaskTTy;
5064 Result.TDBase = TDBase;
5065 Result.KmpTaskTQTyRD = KmpTaskTQTyRD;
5066 return Result;
Alexey Bataev7292c292016-04-25 12:22:29 +00005067}
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005068
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005069void CGOpenMPRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
5070 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00005071 llvm::Function *TaskFunction,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005072 QualType SharedsTy, Address Shareds,
5073 const Expr *IfCond,
5074 const OMPTaskDataTy &Data) {
Alexey Bataev7292c292016-04-25 12:22:29 +00005075 if (!CGF.HaveInsertPoint())
5076 return;
5077
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005078 TaskResultTy Result =
5079 emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data);
5080 llvm::Value *NewTask = Result.NewTask;
James Y Knight9871db02019-02-05 16:42:33 +00005081 llvm::Function *TaskEntry = Result.TaskEntry;
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005082 llvm::Value *NewTaskNewTaskTTy = Result.NewTaskNewTaskTTy;
5083 LValue TDBase = Result.TDBase;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005084 const RecordDecl *KmpTaskTQTyRD = Result.KmpTaskTQTyRD;
5085 ASTContext &C = CGM.getContext();
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005086 // Process list of dependences.
John McCall7f416cc2015-09-08 08:05:57 +00005087 Address DependenciesArray = Address::invalid();
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005088 unsigned NumDependencies = Data.Dependences.size();
John McCall7f416cc2015-09-08 08:05:57 +00005089 if (NumDependencies) {
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005090 // Dependence kind for RTL.
Sergi Mateo Bellido31df1ad2019-02-04 07:33:19 +00005091 enum RTLDependenceKindTy { DepIn = 0x01, DepInOut = 0x3, DepMutexInOutSet = 0x4 };
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005092 enum RTLDependInfoFieldsTy { BaseAddr, Len, Flags };
5093 RecordDecl *KmpDependInfoRD;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005094 QualType FlagsTy =
5095 C.getIntTypeForBitwidth(C.getTypeSize(C.BoolTy), /*Signed=*/false);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005096 llvm::Type *LLVMFlagsTy = CGF.ConvertTypeForMem(FlagsTy);
5097 if (KmpDependInfoTy.isNull()) {
5098 KmpDependInfoRD = C.buildImplicitRecord("kmp_depend_info");
5099 KmpDependInfoRD->startDefinition();
5100 addFieldToRecordDecl(C, KmpDependInfoRD, C.getIntPtrType());
5101 addFieldToRecordDecl(C, KmpDependInfoRD, C.getSizeType());
5102 addFieldToRecordDecl(C, KmpDependInfoRD, FlagsTy);
5103 KmpDependInfoRD->completeDefinition();
5104 KmpDependInfoTy = C.getRecordType(KmpDependInfoRD);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005105 } else {
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005106 KmpDependInfoRD = cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl());
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005107 }
John McCall7f416cc2015-09-08 08:05:57 +00005108 CharUnits DependencySize = C.getTypeSizeInChars(KmpDependInfoTy);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005109 // Define type kmp_depend_info[<Dependences.size()>];
5110 QualType KmpDependInfoArrayTy = C.getConstantArrayType(
John McCall7f416cc2015-09-08 08:05:57 +00005111 KmpDependInfoTy, llvm::APInt(/*numBits=*/64, NumDependencies),
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005112 ArrayType::Normal, /*IndexTypeQuals=*/0);
5113 // kmp_depend_info[<Dependences.size()>] deps;
Alexey Bataev48591dd2016-04-20 04:01:36 +00005114 DependenciesArray =
5115 CGF.CreateMemTemp(KmpDependInfoArrayTy, ".dep.arr.addr");
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005116 for (unsigned I = 0; I < NumDependencies; ++I) {
5117 const Expr *E = Data.Dependences[I].second;
5118 LValue Addr = CGF.EmitLValue(E);
Alexey Bataevd6fdc8b2015-08-31 07:32:19 +00005119 llvm::Value *Size;
5120 QualType Ty = E->getType();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005121 if (const auto *ASE =
5122 dyn_cast<OMPArraySectionExpr>(E->IgnoreParenImpCasts())) {
Alexey Bataevd6fdc8b2015-08-31 07:32:19 +00005123 LValue UpAddrLVal =
5124 CGF.EmitOMPArraySectionExpr(ASE, /*LowerBound=*/false);
5125 llvm::Value *UpAddr =
John McCall7f416cc2015-09-08 08:05:57 +00005126 CGF.Builder.CreateConstGEP1_32(UpAddrLVal.getPointer(), /*Idx0=*/1);
Alexey Bataevd6fdc8b2015-08-31 07:32:19 +00005127 llvm::Value *LowIntPtr =
John McCall7f416cc2015-09-08 08:05:57 +00005128 CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGM.SizeTy);
Alexey Bataevd6fdc8b2015-08-31 07:32:19 +00005129 llvm::Value *UpIntPtr = CGF.Builder.CreatePtrToInt(UpAddr, CGM.SizeTy);
5130 Size = CGF.Builder.CreateNUWSub(UpIntPtr, LowIntPtr);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005131 } else {
Alexey Bataev1189bd02016-01-26 12:20:39 +00005132 Size = CGF.getTypeSize(Ty);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005133 }
5134 LValue Base = CGF.MakeAddrLValue(
5135 CGF.Builder.CreateConstArrayGEP(DependenciesArray, I, DependencySize),
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005136 KmpDependInfoTy);
5137 // deps[i].base_addr = &<Dependences[i].second>;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005138 LValue BaseAddrLVal = CGF.EmitLValueForField(
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005139 Base, *std::next(KmpDependInfoRD->field_begin(), BaseAddr));
John McCall7f416cc2015-09-08 08:05:57 +00005140 CGF.EmitStoreOfScalar(
5141 CGF.Builder.CreatePtrToInt(Addr.getPointer(), CGF.IntPtrTy),
5142 BaseAddrLVal);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005143 // deps[i].len = sizeof(<Dependences[i].second>);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005144 LValue LenLVal = CGF.EmitLValueForField(
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005145 Base, *std::next(KmpDependInfoRD->field_begin(), Len));
5146 CGF.EmitStoreOfScalar(Size, LenLVal);
5147 // deps[i].flags = <Dependences[i].first>;
5148 RTLDependenceKindTy DepKind;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005149 switch (Data.Dependences[I].first) {
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005150 case OMPC_DEPEND_in:
5151 DepKind = DepIn;
5152 break;
Alexey Bataev92e82f92015-11-23 13:33:42 +00005153 // Out and InOut dependencies must use the same code.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005154 case OMPC_DEPEND_out:
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005155 case OMPC_DEPEND_inout:
5156 DepKind = DepInOut;
5157 break;
Sergi Mateo Bellido31df1ad2019-02-04 07:33:19 +00005158 case OMPC_DEPEND_mutexinoutset:
5159 DepKind = DepMutexInOutSet;
5160 break;
Alexey Bataeveb482352015-12-18 05:05:56 +00005161 case OMPC_DEPEND_source:
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005162 case OMPC_DEPEND_sink:
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005163 case OMPC_DEPEND_unknown:
5164 llvm_unreachable("Unknown task dependence type");
5165 }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005166 LValue FlagsLVal = CGF.EmitLValueForField(
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005167 Base, *std::next(KmpDependInfoRD->field_begin(), Flags));
5168 CGF.EmitStoreOfScalar(llvm::ConstantInt::get(LLVMFlagsTy, DepKind),
5169 FlagsLVal);
5170 }
John McCall7f416cc2015-09-08 08:05:57 +00005171 DependenciesArray = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5172 CGF.Builder.CreateStructGEP(DependenciesArray, 0, CharUnits::Zero()),
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005173 CGF.VoidPtrTy);
5174 }
5175
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00005176 // NOTE: routine and part_id fields are initialized by __kmpc_omp_task_alloc()
Alexey Bataev62b63b12015-03-10 07:28:44 +00005177 // libcall.
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005178 // Build kmp_int32 __kmpc_omp_task_with_deps(ident_t *, kmp_int32 gtid,
5179 // kmp_task_t *new_task, kmp_int32 ndeps, kmp_depend_info_t *dep_list,
5180 // kmp_int32 ndeps_noalias, kmp_depend_info_t *noalias_dep_list) if dependence
5181 // list is not empty
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005182 llvm::Value *ThreadID = getThreadID(CGF, Loc);
5183 llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc);
John McCall7f416cc2015-09-08 08:05:57 +00005184 llvm::Value *TaskArgs[] = { UpLoc, ThreadID, NewTask };
5185 llvm::Value *DepTaskArgs[7];
5186 if (NumDependencies) {
5187 DepTaskArgs[0] = UpLoc;
5188 DepTaskArgs[1] = ThreadID;
5189 DepTaskArgs[2] = NewTask;
5190 DepTaskArgs[3] = CGF.Builder.getInt32(NumDependencies);
5191 DepTaskArgs[4] = DependenciesArray.getPointer();
5192 DepTaskArgs[5] = CGF.Builder.getInt32(0);
5193 DepTaskArgs[6] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
5194 }
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00005195 auto &&ThenCodeGen = [this, &Data, TDBase, KmpTaskTQTyRD, NumDependencies,
5196 &TaskArgs,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005197 &DepTaskArgs](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005198 if (!Data.Tied) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00005199 auto PartIdFI = std::next(KmpTaskTQTyRD->field_begin(), KmpTaskTPartId);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005200 LValue PartIdLVal = CGF.EmitLValueForField(TDBase, *PartIdFI);
Alexey Bataev48591dd2016-04-20 04:01:36 +00005201 CGF.EmitStoreOfScalar(CGF.Builder.getInt32(0), PartIdLVal);
5202 }
John McCall7f416cc2015-09-08 08:05:57 +00005203 if (NumDependencies) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005204 CGF.EmitRuntimeCall(
Alexey Bataev48591dd2016-04-20 04:01:36 +00005205 createRuntimeFunction(OMPRTL__kmpc_omp_task_with_deps), DepTaskArgs);
John McCall7f416cc2015-09-08 08:05:57 +00005206 } else {
Alexey Bataev48591dd2016-04-20 04:01:36 +00005207 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task),
John McCall7f416cc2015-09-08 08:05:57 +00005208 TaskArgs);
5209 }
Alexey Bataev48591dd2016-04-20 04:01:36 +00005210 // Check if parent region is untied and build return for untied task;
5211 if (auto *Region =
5212 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
5213 Region->emitUntiedSwitch(CGF);
Alexey Bataev1d677132015-04-22 13:57:31 +00005214 };
John McCall7f416cc2015-09-08 08:05:57 +00005215
5216 llvm::Value *DepWaitTaskArgs[6];
5217 if (NumDependencies) {
5218 DepWaitTaskArgs[0] = UpLoc;
5219 DepWaitTaskArgs[1] = ThreadID;
5220 DepWaitTaskArgs[2] = CGF.Builder.getInt32(NumDependencies);
5221 DepWaitTaskArgs[3] = DependenciesArray.getPointer();
5222 DepWaitTaskArgs[4] = CGF.Builder.getInt32(0);
5223 DepWaitTaskArgs[5] = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
5224 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005225 auto &&ElseCodeGen = [&TaskArgs, ThreadID, NewTaskNewTaskTTy, TaskEntry,
Alexey Bataev3c595a62017-08-14 15:01:03 +00005226 NumDependencies, &DepWaitTaskArgs,
5227 Loc](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005228 CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime();
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005229 CodeGenFunction::RunCleanupsScope LocalScope(CGF);
5230 // Build void __kmpc_omp_wait_deps(ident_t *, kmp_int32 gtid,
5231 // kmp_int32 ndeps, kmp_depend_info_t *dep_list, kmp_int32
5232 // ndeps_noalias, kmp_depend_info_t *noalias_dep_list); if dependence info
5233 // is specified.
John McCall7f416cc2015-09-08 08:05:57 +00005234 if (NumDependencies)
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005235 CGF.EmitRuntimeCall(RT.createRuntimeFunction(OMPRTL__kmpc_omp_wait_deps),
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005236 DepWaitTaskArgs);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005237 // Call proxy_task_entry(gtid, new_task);
Alexey Bataev3c595a62017-08-14 15:01:03 +00005238 auto &&CodeGen = [TaskEntry, ThreadID, NewTaskNewTaskTTy,
5239 Loc](CodeGenFunction &CGF, PrePostActionTy &Action) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005240 Action.Enter(CGF);
5241 llvm::Value *OutlinedFnArgs[] = {ThreadID, NewTaskNewTaskTTy};
Alexey Bataev3c595a62017-08-14 15:01:03 +00005242 CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, Loc, TaskEntry,
Alexey Bataev2c7eee52017-08-04 19:10:54 +00005243 OutlinedFnArgs);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005244 };
5245
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005246 // Build void __kmpc_omp_task_begin_if0(ident_t *, kmp_int32 gtid,
5247 // kmp_task_t *new_task);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005248 // Build void __kmpc_omp_task_complete_if0(ident_t *, kmp_int32 gtid,
5249 // kmp_task_t *new_task);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005250 RegionCodeGenTy RCG(CodeGen);
5251 CommonActionTy Action(
5252 RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_begin_if0), TaskArgs,
5253 RT.createRuntimeFunction(OMPRTL__kmpc_omp_task_complete_if0), TaskArgs);
5254 RCG.setAction(Action);
5255 RCG(CGF);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00005256 };
John McCall7f416cc2015-09-08 08:05:57 +00005257
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005258 if (IfCond) {
Alexey Bataev1d677132015-04-22 13:57:31 +00005259 emitOMPIfClause(CGF, IfCond, ThenCodeGen, ElseCodeGen);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005260 } else {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005261 RegionCodeGenTy ThenRCG(ThenCodeGen);
5262 ThenRCG(CGF);
Alexey Bataevf539faa2016-03-28 12:58:34 +00005263 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00005264}
5265
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005266void CGOpenMPRuntime::emitTaskLoopCall(CodeGenFunction &CGF, SourceLocation Loc,
5267 const OMPLoopDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00005268 llvm::Function *TaskFunction,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005269 QualType SharedsTy, Address Shareds,
5270 const Expr *IfCond,
5271 const OMPTaskDataTy &Data) {
Alexey Bataev7292c292016-04-25 12:22:29 +00005272 if (!CGF.HaveInsertPoint())
5273 return;
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005274 TaskResultTy Result =
5275 emitTaskInit(CGF, Loc, D, TaskFunction, SharedsTy, Shareds, Data);
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00005276 // NOTE: routine and part_id fields are initialized by __kmpc_omp_task_alloc()
Alexey Bataev7292c292016-04-25 12:22:29 +00005277 // libcall.
5278 // Call to void __kmpc_taskloop(ident_t *loc, int gtid, kmp_task_t *task, int
5279 // if_val, kmp_uint64 *lb, kmp_uint64 *ub, kmp_int64 st, int nogroup, int
5280 // sched, kmp_uint64 grainsize, void *task_dup);
5281 llvm::Value *ThreadID = getThreadID(CGF, Loc);
5282 llvm::Value *UpLoc = emitUpdateLocation(CGF, Loc);
5283 llvm::Value *IfVal;
5284 if (IfCond) {
5285 IfVal = CGF.Builder.CreateIntCast(CGF.EvaluateExprAsBool(IfCond), CGF.IntTy,
5286 /*isSigned=*/true);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005287 } else {
Alexey Bataev7292c292016-04-25 12:22:29 +00005288 IfVal = llvm::ConstantInt::getSigned(CGF.IntTy, /*V=*/1);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005289 }
Alexey Bataev7292c292016-04-25 12:22:29 +00005290
5291 LValue LBLVal = CGF.EmitLValueForField(
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005292 Result.TDBase,
5293 *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTLowerBound));
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005294 const auto *LBVar =
Alexey Bataev7292c292016-04-25 12:22:29 +00005295 cast<VarDecl>(cast<DeclRefExpr>(D.getLowerBoundVariable())->getDecl());
5296 CGF.EmitAnyExprToMem(LBVar->getInit(), LBLVal.getAddress(), LBLVal.getQuals(),
5297 /*IsInitializer=*/true);
5298 LValue UBLVal = CGF.EmitLValueForField(
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005299 Result.TDBase,
5300 *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTUpperBound));
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005301 const auto *UBVar =
Alexey Bataev7292c292016-04-25 12:22:29 +00005302 cast<VarDecl>(cast<DeclRefExpr>(D.getUpperBoundVariable())->getDecl());
5303 CGF.EmitAnyExprToMem(UBVar->getInit(), UBLVal.getAddress(), UBLVal.getQuals(),
5304 /*IsInitializer=*/true);
5305 LValue StLVal = CGF.EmitLValueForField(
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005306 Result.TDBase,
5307 *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTStride));
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005308 const auto *StVar =
Alexey Bataev7292c292016-04-25 12:22:29 +00005309 cast<VarDecl>(cast<DeclRefExpr>(D.getStrideVariable())->getDecl());
5310 CGF.EmitAnyExprToMem(StVar->getInit(), StLVal.getAddress(), StLVal.getQuals(),
5311 /*IsInitializer=*/true);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005312 // Store reductions address.
5313 LValue RedLVal = CGF.EmitLValueForField(
5314 Result.TDBase,
5315 *std::next(Result.KmpTaskTQTyRD->field_begin(), KmpTaskTReductions));
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005316 if (Data.Reductions) {
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005317 CGF.EmitStoreOfScalar(Data.Reductions, RedLVal);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005318 } else {
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005319 CGF.EmitNullInitialization(RedLVal.getAddress(),
5320 CGF.getContext().VoidPtrTy);
5321 }
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00005322 enum { NoSchedule = 0, Grainsize = 1, NumTasks = 2 };
Alexey Bataev7292c292016-04-25 12:22:29 +00005323 llvm::Value *TaskArgs[] = {
Alexey Bataev33446032017-07-12 18:09:32 +00005324 UpLoc,
5325 ThreadID,
5326 Result.NewTask,
5327 IfVal,
5328 LBLVal.getPointer(),
5329 UBLVal.getPointer(),
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00005330 CGF.EmitLoadOfScalar(StLVal, Loc),
Alexey Bataevac6e4de2018-10-24 19:06:37 +00005331 llvm::ConstantInt::getSigned(
5332 CGF.IntTy, 1), // Always 1 because taskgroup emitted by the compiler
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00005333 llvm::ConstantInt::getSigned(
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005334 CGF.IntTy, Data.Schedule.getPointer()
5335 ? Data.Schedule.getInt() ? NumTasks : Grainsize
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00005336 : NoSchedule),
Alexey Bataev24b5bae2016-04-28 09:23:51 +00005337 Data.Schedule.getPointer()
5338 ? CGF.Builder.CreateIntCast(Data.Schedule.getPointer(), CGF.Int64Ty,
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00005339 /*isSigned=*/false)
5340 : llvm::ConstantInt::get(CGF.Int64Ty, /*V=*/0),
Alexey Bataev33446032017-07-12 18:09:32 +00005341 Result.TaskDupFn ? CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5342 Result.TaskDupFn, CGF.VoidPtrTy)
5343 : llvm::ConstantPointerNull::get(CGF.VoidPtrTy)};
Alexey Bataev7292c292016-04-25 12:22:29 +00005344 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_taskloop), TaskArgs);
5345}
5346
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005347/// Emit reduction operation for each element of array (required for
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005348/// array sections) LHS op = RHS.
5349/// \param Type Type of array.
5350/// \param LHSVar Variable on the left side of the reduction operation
5351/// (references element of array in original variable).
5352/// \param RHSVar Variable on the right side of the reduction operation
5353/// (references element of array in original variable).
5354/// \param RedOpGen Generator of reduction operation with use of LHSVar and
5355/// RHSVar.
Benjamin Kramere003ca22015-10-28 13:54:16 +00005356static void EmitOMPAggregateReduction(
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005357 CodeGenFunction &CGF, QualType Type, const VarDecl *LHSVar,
5358 const VarDecl *RHSVar,
5359 const llvm::function_ref<void(CodeGenFunction &CGF, const Expr *,
5360 const Expr *, const Expr *)> &RedOpGen,
5361 const Expr *XExpr = nullptr, const Expr *EExpr = nullptr,
5362 const Expr *UpExpr = nullptr) {
5363 // Perform element-by-element initialization.
5364 QualType ElementTy;
5365 Address LHSAddr = CGF.GetAddrOfLocalVar(LHSVar);
5366 Address RHSAddr = CGF.GetAddrOfLocalVar(RHSVar);
5367
5368 // Drill down to the base element type on both arrays.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005369 const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe();
5370 llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, LHSAddr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005371
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005372 llvm::Value *RHSBegin = RHSAddr.getPointer();
5373 llvm::Value *LHSBegin = LHSAddr.getPointer();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005374 // Cast from pointer to array type to pointer to single element.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005375 llvm::Value *LHSEnd = CGF.Builder.CreateGEP(LHSBegin, NumElements);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005376 // The basic structure here is a while-do loop.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005377 llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.arraycpy.body");
5378 llvm::BasicBlock *DoneBB = CGF.createBasicBlock("omp.arraycpy.done");
5379 llvm::Value *IsEmpty =
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005380 CGF.Builder.CreateICmpEQ(LHSBegin, LHSEnd, "omp.arraycpy.isempty");
5381 CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
5382
5383 // Enter the loop body, making that address the current address.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005384 llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005385 CGF.EmitBlock(BodyBB);
5386
5387 CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy);
5388
5389 llvm::PHINode *RHSElementPHI = CGF.Builder.CreatePHI(
5390 RHSBegin->getType(), 2, "omp.arraycpy.srcElementPast");
5391 RHSElementPHI->addIncoming(RHSBegin, EntryBB);
5392 Address RHSElementCurrent =
5393 Address(RHSElementPHI,
5394 RHSAddr.getAlignment().alignmentOfArrayElement(ElementSize));
5395
5396 llvm::PHINode *LHSElementPHI = CGF.Builder.CreatePHI(
5397 LHSBegin->getType(), 2, "omp.arraycpy.destElementPast");
5398 LHSElementPHI->addIncoming(LHSBegin, EntryBB);
5399 Address LHSElementCurrent =
5400 Address(LHSElementPHI,
5401 LHSAddr.getAlignment().alignmentOfArrayElement(ElementSize));
5402
5403 // Emit copy.
5404 CodeGenFunction::OMPPrivateScope Scope(CGF);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005405 Scope.addPrivate(LHSVar, [=]() { return LHSElementCurrent; });
5406 Scope.addPrivate(RHSVar, [=]() { return RHSElementCurrent; });
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005407 Scope.Privatize();
5408 RedOpGen(CGF, XExpr, EExpr, UpExpr);
5409 Scope.ForceCleanup();
5410
5411 // Shift the address forward by one element.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005412 llvm::Value *LHSElementNext = CGF.Builder.CreateConstGEP1_32(
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005413 LHSElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005414 llvm::Value *RHSElementNext = CGF.Builder.CreateConstGEP1_32(
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005415 RHSElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element");
5416 // Check whether we've reached the end.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005417 llvm::Value *Done =
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005418 CGF.Builder.CreateICmpEQ(LHSElementNext, LHSEnd, "omp.arraycpy.done");
5419 CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB);
5420 LHSElementPHI->addIncoming(LHSElementNext, CGF.Builder.GetInsertBlock());
5421 RHSElementPHI->addIncoming(RHSElementNext, CGF.Builder.GetInsertBlock());
5422
5423 // Done.
5424 CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
5425}
5426
Alexey Bataeva839ddd2016-03-17 10:19:46 +00005427/// Emit reduction combiner. If the combiner is a simple expression emit it as
5428/// is, otherwise consider it as combiner of UDR decl and emit it as a call of
5429/// UDR combiner function.
5430static void emitReductionCombiner(CodeGenFunction &CGF,
5431 const Expr *ReductionOp) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005432 if (const auto *CE = dyn_cast<CallExpr>(ReductionOp))
5433 if (const auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee()))
5434 if (const auto *DRE =
Alexey Bataeva839ddd2016-03-17 10:19:46 +00005435 dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts()))
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005436 if (const auto *DRD =
5437 dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +00005438 std::pair<llvm::Function *, llvm::Function *> Reduction =
5439 CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD);
5440 RValue Func = RValue::get(Reduction.first);
5441 CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func);
5442 CGF.EmitIgnoredExpr(ReductionOp);
5443 return;
5444 }
5445 CGF.EmitIgnoredExpr(ReductionOp);
5446}
5447
James Y Knight9871db02019-02-05 16:42:33 +00005448llvm::Function *CGOpenMPRuntime::emitReductionFunction(
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005449 CodeGenModule &CGM, SourceLocation Loc, llvm::Type *ArgsType,
5450 ArrayRef<const Expr *> Privates, ArrayRef<const Expr *> LHSExprs,
5451 ArrayRef<const Expr *> RHSExprs, ArrayRef<const Expr *> ReductionOps) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005452 ASTContext &C = CGM.getContext();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005453
5454 // void reduction_func(void *LHSArg, void *RHSArg);
5455 FunctionArgList Args;
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005456 ImplicitParamDecl LHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
5457 ImplicitParamDecl::Other);
5458 ImplicitParamDecl RHSArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
5459 ImplicitParamDecl::Other);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005460 Args.push_back(&LHSArg);
5461 Args.push_back(&RHSArg);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005462 const auto &CGFI =
5463 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataev18fa2322018-05-02 14:20:50 +00005464 std::string Name = getName({"omp", "reduction", "reduction_func"});
5465 auto *Fn = llvm::Function::Create(CGM.getTypes().GetFunctionType(CGFI),
5466 llvm::GlobalValue::InternalLinkage, Name,
5467 &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00005468 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
Alexey Bataevc0f879b2018-04-10 20:10:53 +00005469 Fn->setDoesNotRecurse();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005470 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005471 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005472
5473 // Dst = (void*[n])(LHSArg);
5474 // Src = (void*[n])(RHSArg);
John McCall7f416cc2015-09-08 08:05:57 +00005475 Address LHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5476 CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&LHSArg)),
5477 ArgsType), CGF.getPointerAlign());
5478 Address RHS(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
5479 CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(&RHSArg)),
5480 ArgsType), CGF.getPointerAlign());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005481
5482 // ...
5483 // *(Type<i>*)lhs[i] = RedOp<i>(*(Type<i>*)lhs[i], *(Type<i>*)rhs[i]);
5484 // ...
5485 CodeGenFunction::OMPPrivateScope Scope(CGF);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005486 auto IPriv = Privates.begin();
5487 unsigned Idx = 0;
5488 for (unsigned I = 0, E = ReductionOps.size(); I < E; ++I, ++IPriv, ++Idx) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005489 const auto *RHSVar =
5490 cast<VarDecl>(cast<DeclRefExpr>(RHSExprs[I])->getDecl());
5491 Scope.addPrivate(RHSVar, [&CGF, RHS, Idx, RHSVar]() {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005492 return emitAddrOfVarFromArray(CGF, RHS, Idx, RHSVar);
John McCall7f416cc2015-09-08 08:05:57 +00005493 });
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005494 const auto *LHSVar =
5495 cast<VarDecl>(cast<DeclRefExpr>(LHSExprs[I])->getDecl());
5496 Scope.addPrivate(LHSVar, [&CGF, LHS, Idx, LHSVar]() {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005497 return emitAddrOfVarFromArray(CGF, LHS, Idx, LHSVar);
John McCall7f416cc2015-09-08 08:05:57 +00005498 });
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005499 QualType PrivTy = (*IPriv)->getType();
Alexey Bataev1189bd02016-01-26 12:20:39 +00005500 if (PrivTy->isVariablyModifiedType()) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005501 // Get array size and emit VLA type.
5502 ++Idx;
5503 Address Elem =
5504 CGF.Builder.CreateConstArrayGEP(LHS, Idx, CGF.getPointerSize());
5505 llvm::Value *Ptr = CGF.Builder.CreateLoad(Elem);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005506 const VariableArrayType *VLA =
5507 CGF.getContext().getAsVariableArrayType(PrivTy);
5508 const auto *OVE = cast<OpaqueValueExpr>(VLA->getSizeExpr());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005509 CodeGenFunction::OpaqueValueMapping OpaqueMap(
Alexey Bataev1189bd02016-01-26 12:20:39 +00005510 CGF, OVE, RValue::get(CGF.Builder.CreatePtrToInt(Ptr, CGF.SizeTy)));
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005511 CGF.EmitVariablyModifiedType(PrivTy);
5512 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005513 }
5514 Scope.Privatize();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005515 IPriv = Privates.begin();
5516 auto ILHS = LHSExprs.begin();
5517 auto IRHS = RHSExprs.begin();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005518 for (const Expr *E : ReductionOps) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005519 if ((*IPriv)->getType()->isArrayType()) {
5520 // Emit reduction for array section.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005521 const auto *LHSVar = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
5522 const auto *RHSVar = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
Alexey Bataeva839ddd2016-03-17 10:19:46 +00005523 EmitOMPAggregateReduction(
5524 CGF, (*IPriv)->getType(), LHSVar, RHSVar,
5525 [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) {
5526 emitReductionCombiner(CGF, E);
5527 });
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005528 } else {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005529 // Emit reduction for array subscript or single variable.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00005530 emitReductionCombiner(CGF, E);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005531 }
Richard Trieucc3949d2016-02-18 22:34:54 +00005532 ++IPriv;
5533 ++ILHS;
5534 ++IRHS;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005535 }
5536 Scope.ForceCleanup();
5537 CGF.FinishFunction();
5538 return Fn;
5539}
5540
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00005541void CGOpenMPRuntime::emitSingleReductionCombiner(CodeGenFunction &CGF,
5542 const Expr *ReductionOp,
5543 const Expr *PrivateRef,
5544 const DeclRefExpr *LHS,
5545 const DeclRefExpr *RHS) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005546 if (PrivateRef->getType()->isArrayType()) {
5547 // Emit reduction for array section.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005548 const auto *LHSVar = cast<VarDecl>(LHS->getDecl());
5549 const auto *RHSVar = cast<VarDecl>(RHS->getDecl());
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005550 EmitOMPAggregateReduction(
5551 CGF, PrivateRef->getType(), LHSVar, RHSVar,
5552 [=](CodeGenFunction &CGF, const Expr *, const Expr *, const Expr *) {
5553 emitReductionCombiner(CGF, ReductionOp);
5554 });
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005555 } else {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005556 // Emit reduction for array subscript or single variable.
5557 emitReductionCombiner(CGF, ReductionOp);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005558 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005559}
5560
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005561void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005562 ArrayRef<const Expr *> Privates,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005563 ArrayRef<const Expr *> LHSExprs,
5564 ArrayRef<const Expr *> RHSExprs,
5565 ArrayRef<const Expr *> ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00005566 ReductionOptionsTy Options) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00005567 if (!CGF.HaveInsertPoint())
5568 return;
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00005569
5570 bool WithNowait = Options.WithNowait;
5571 bool SimpleReduction = Options.SimpleReduction;
5572
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005573 // Next code should be emitted for reduction:
5574 //
5575 // static kmp_critical_name lock = { 0 };
5576 //
5577 // void reduce_func(void *lhs[<n>], void *rhs[<n>]) {
5578 // *(Type0*)lhs[0] = ReductionOperation0(*(Type0*)lhs[0], *(Type0*)rhs[0]);
5579 // ...
5580 // *(Type<n>-1*)lhs[<n>-1] = ReductionOperation<n>-1(*(Type<n>-1*)lhs[<n>-1],
5581 // *(Type<n>-1*)rhs[<n>-1]);
5582 // }
5583 //
5584 // ...
5585 // void *RedList[<n>] = {&<RHSExprs>[0], ..., &<RHSExprs>[<n>-1]};
5586 // switch (__kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
5587 // RedList, reduce_func, &<lock>)) {
5588 // case 1:
5589 // ...
5590 // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
5591 // ...
5592 // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
5593 // break;
5594 // case 2:
5595 // ...
5596 // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
5597 // ...
Alexey Bataev69a47792015-05-07 03:54:03 +00005598 // [__kmpc_end_reduce(<loc>, <gtid>, &<lock>);]
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005599 // break;
5600 // default:;
5601 // }
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00005602 //
5603 // if SimpleReduction is true, only the next code is generated:
5604 // ...
5605 // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
5606 // ...
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005607
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005608 ASTContext &C = CGM.getContext();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005609
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00005610 if (SimpleReduction) {
5611 CodeGenFunction::RunCleanupsScope Scope(CGF);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005612 auto IPriv = Privates.begin();
5613 auto ILHS = LHSExprs.begin();
5614 auto IRHS = RHSExprs.begin();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005615 for (const Expr *E : ReductionOps) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005616 emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS),
5617 cast<DeclRefExpr>(*IRHS));
Richard Trieucc3949d2016-02-18 22:34:54 +00005618 ++IPriv;
5619 ++ILHS;
5620 ++IRHS;
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00005621 }
5622 return;
5623 }
5624
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005625 // 1. Build a list of reduction variables.
5626 // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]};
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005627 auto Size = RHSExprs.size();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005628 for (const Expr *E : Privates) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00005629 if (E->getType()->isVariablyModifiedType())
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005630 // Reserve place for array size.
5631 ++Size;
5632 }
5633 llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005634 QualType ReductionArrayTy =
5635 C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal,
5636 /*IndexTypeQuals=*/0);
John McCall7f416cc2015-09-08 08:05:57 +00005637 Address ReductionList =
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005638 CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list");
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005639 auto IPriv = Privates.begin();
5640 unsigned Idx = 0;
5641 for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) {
John McCall7f416cc2015-09-08 08:05:57 +00005642 Address Elem =
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005643 CGF.Builder.CreateConstArrayGEP(ReductionList, Idx, CGF.getPointerSize());
John McCall7f416cc2015-09-08 08:05:57 +00005644 CGF.Builder.CreateStore(
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005645 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
John McCall7f416cc2015-09-08 08:05:57 +00005646 CGF.EmitLValue(RHSExprs[I]).getPointer(), CGF.VoidPtrTy),
5647 Elem);
Alexey Bataev1189bd02016-01-26 12:20:39 +00005648 if ((*IPriv)->getType()->isVariablyModifiedType()) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005649 // Store array size.
5650 ++Idx;
5651 Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx,
5652 CGF.getPointerSize());
Alexey Bataev1189bd02016-01-26 12:20:39 +00005653 llvm::Value *Size = CGF.Builder.CreateIntCast(
5654 CGF.getVLASize(
5655 CGF.getContext().getAsVariableArrayType((*IPriv)->getType()))
Sander de Smalen891af03a2018-02-03 13:55:59 +00005656 .NumElts,
Alexey Bataev1189bd02016-01-26 12:20:39 +00005657 CGF.SizeTy, /*isSigned=*/false);
5658 CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy),
5659 Elem);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005660 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005661 }
5662
5663 // 2. Emit reduce_func().
James Y Knight9871db02019-02-05 16:42:33 +00005664 llvm::Function *ReductionFn = emitReductionFunction(
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005665 CGM, Loc, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(),
5666 Privates, LHSExprs, RHSExprs, ReductionOps);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005667
5668 // 3. Create static kmp_critical_name lock = { 0 };
Alexey Bataev18fa2322018-05-02 14:20:50 +00005669 std::string Name = getName({"reduction"});
5670 llvm::Value *Lock = getCriticalRegionLock(Name);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005671
5672 // 4. Build res = __kmpc_reduce{_nowait}(<loc>, <gtid>, <n>, sizeof(RedList),
5673 // RedList, reduce_func, &<lock>);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005674 llvm::Value *IdentTLoc = emitUpdateLocation(CGF, Loc, OMP_ATOMIC_REDUCE);
5675 llvm::Value *ThreadId = getThreadID(CGF, Loc);
5676 llvm::Value *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy);
5677 llvm::Value *RL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
Samuel Antao4c8035b2016-12-12 18:00:20 +00005678 ReductionList.getPointer(), CGF.VoidPtrTy);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005679 llvm::Value *Args[] = {
5680 IdentTLoc, // ident_t *<loc>
5681 ThreadId, // i32 <gtid>
5682 CGF.Builder.getInt32(RHSExprs.size()), // i32 <n>
5683 ReductionArrayTySize, // size_type sizeof(RedList)
5684 RL, // void *RedList
5685 ReductionFn, // void (*) (void *, void *) <reduce_func>
5686 Lock // kmp_critical_name *&<lock>
5687 };
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005688 llvm::Value *Res = CGF.EmitRuntimeCall(
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005689 createRuntimeFunction(WithNowait ? OMPRTL__kmpc_reduce_nowait
5690 : OMPRTL__kmpc_reduce),
5691 Args);
5692
5693 // 5. Build switch(res)
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005694 llvm::BasicBlock *DefaultBB = CGF.createBasicBlock(".omp.reduction.default");
5695 llvm::SwitchInst *SwInst =
5696 CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/2);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005697
5698 // 6. Build case 1:
5699 // ...
5700 // <LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]);
5701 // ...
5702 // __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
5703 // break;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005704 llvm::BasicBlock *Case1BB = CGF.createBasicBlock(".omp.reduction.case1");
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005705 SwInst->addCase(CGF.Builder.getInt32(1), Case1BB);
5706 CGF.EmitBlock(Case1BB);
5707
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005708 // Add emission of __kmpc_end_reduce{_nowait}(<loc>, <gtid>, &<lock>);
5709 llvm::Value *EndArgs[] = {
5710 IdentTLoc, // ident_t *<loc>
5711 ThreadId, // i32 <gtid>
5712 Lock // kmp_critical_name *&<lock>
5713 };
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005714 auto &&CodeGen = [Privates, LHSExprs, RHSExprs, ReductionOps](
5715 CodeGenFunction &CGF, PrePostActionTy &Action) {
5716 CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005717 auto IPriv = Privates.begin();
5718 auto ILHS = LHSExprs.begin();
5719 auto IRHS = RHSExprs.begin();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005720 for (const Expr *E : ReductionOps) {
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00005721 RT.emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS),
5722 cast<DeclRefExpr>(*IRHS));
Richard Trieucc3949d2016-02-18 22:34:54 +00005723 ++IPriv;
5724 ++ILHS;
5725 ++IRHS;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005726 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005727 };
5728 RegionCodeGenTy RCG(CodeGen);
5729 CommonActionTy Action(
5730 nullptr, llvm::None,
5731 createRuntimeFunction(WithNowait ? OMPRTL__kmpc_end_reduce_nowait
5732 : OMPRTL__kmpc_end_reduce),
5733 EndArgs);
5734 RCG.setAction(Action);
5735 RCG(CGF);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005736
5737 CGF.EmitBranch(DefaultBB);
5738
5739 // 7. Build case 2:
5740 // ...
5741 // Atomic(<LHSExprs>[i] = RedOp<i>(*<LHSExprs>[i], *<RHSExprs>[i]));
5742 // ...
5743 // break;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005744 llvm::BasicBlock *Case2BB = CGF.createBasicBlock(".omp.reduction.case2");
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005745 SwInst->addCase(CGF.Builder.getInt32(2), Case2BB);
5746 CGF.EmitBlock(Case2BB);
5747
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005748 auto &&AtomicCodeGen = [Loc, Privates, LHSExprs, RHSExprs, ReductionOps](
5749 CodeGenFunction &CGF, PrePostActionTy &Action) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005750 auto ILHS = LHSExprs.begin();
5751 auto IRHS = RHSExprs.begin();
5752 auto IPriv = Privates.begin();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005753 for (const Expr *E : ReductionOps) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005754 const Expr *XExpr = nullptr;
5755 const Expr *EExpr = nullptr;
5756 const Expr *UpExpr = nullptr;
5757 BinaryOperatorKind BO = BO_Comma;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005758 if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005759 if (BO->getOpcode() == BO_Assign) {
5760 XExpr = BO->getLHS();
5761 UpExpr = BO->getRHS();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005762 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005763 }
5764 // Try to emit update expression as a simple atomic.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005765 const Expr *RHSExpr = UpExpr;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005766 if (RHSExpr) {
5767 // Analyze RHS part of the whole expression.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005768 if (const auto *ACO = dyn_cast<AbstractConditionalOperator>(
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005769 RHSExpr->IgnoreParenImpCasts())) {
5770 // If this is a conditional operator, analyze its condition for
5771 // min/max reduction operator.
5772 RHSExpr = ACO->getCond();
Alexey Bataev69a47792015-05-07 03:54:03 +00005773 }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005774 if (const auto *BORHS =
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005775 dyn_cast<BinaryOperator>(RHSExpr->IgnoreParenImpCasts())) {
5776 EExpr = BORHS->getRHS();
5777 BO = BORHS->getOpcode();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00005778 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005779 }
5780 if (XExpr) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005781 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00005782 auto &&AtomicRedGen = [BO, VD,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005783 Loc](CodeGenFunction &CGF, const Expr *XExpr,
5784 const Expr *EExpr, const Expr *UpExpr) {
5785 LValue X = CGF.EmitLValue(XExpr);
5786 RValue E;
5787 if (EExpr)
5788 E = CGF.EmitAnyExpr(EExpr);
5789 CGF.EmitOMPAtomicSimpleUpdateExpr(
JF Bastien92f4ef12016-04-06 17:26:42 +00005790 X, E, BO, /*IsXLHSInRHSPart=*/true,
5791 llvm::AtomicOrdering::Monotonic, Loc,
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00005792 [&CGF, UpExpr, VD, Loc](RValue XRValue) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005793 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
5794 PrivateScope.addPrivate(
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005795 VD, [&CGF, VD, XRValue, Loc]() {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005796 Address LHSTemp = CGF.CreateMemTemp(VD->getType());
5797 CGF.emitOMPSimpleStore(
5798 CGF.MakeAddrLValue(LHSTemp, VD->getType()), XRValue,
5799 VD->getType().getNonReferenceType(), Loc);
5800 return LHSTemp;
5801 });
5802 (void)PrivateScope.Privatize();
5803 return CGF.EmitAnyExpr(UpExpr);
5804 });
5805 };
5806 if ((*IPriv)->getType()->isArrayType()) {
5807 // Emit atomic reduction for array section.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005808 const auto *RHSVar =
5809 cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005810 EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), VD, RHSVar,
5811 AtomicRedGen, XExpr, EExpr, UpExpr);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005812 } else {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005813 // Emit atomic reduction for array subscript or single variable.
5814 AtomicRedGen(CGF, XExpr, EExpr, UpExpr);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005815 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005816 } else {
5817 // Emit as a critical region.
5818 auto &&CritRedGen = [E, Loc](CodeGenFunction &CGF, const Expr *,
Alexey Bataev18fa2322018-05-02 14:20:50 +00005819 const Expr *, const Expr *) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005820 CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime();
Alexey Bataev18fa2322018-05-02 14:20:50 +00005821 std::string Name = RT.getName({"atomic_reduction"});
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005822 RT.emitCriticalRegion(
Alexey Bataev18fa2322018-05-02 14:20:50 +00005823 CGF, Name,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005824 [=](CodeGenFunction &CGF, PrePostActionTy &Action) {
5825 Action.Enter(CGF);
5826 emitReductionCombiner(CGF, E);
5827 },
5828 Loc);
5829 };
5830 if ((*IPriv)->getType()->isArrayType()) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005831 const auto *LHSVar =
5832 cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
5833 const auto *RHSVar =
5834 cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005835 EmitOMPAggregateReduction(CGF, (*IPriv)->getType(), LHSVar, RHSVar,
5836 CritRedGen);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005837 } else {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005838 CritRedGen(CGF, nullptr, nullptr, nullptr);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005839 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005840 }
Richard Trieucc3949d2016-02-18 22:34:54 +00005841 ++ILHS;
5842 ++IRHS;
5843 ++IPriv;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005844 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005845 };
5846 RegionCodeGenTy AtomicRCG(AtomicCodeGen);
5847 if (!WithNowait) {
5848 // Add emission of __kmpc_end_reduce(<loc>, <gtid>, &<lock>);
5849 llvm::Value *EndArgs[] = {
5850 IdentTLoc, // ident_t *<loc>
5851 ThreadId, // i32 <gtid>
5852 Lock // kmp_critical_name *&<lock>
5853 };
5854 CommonActionTy Action(nullptr, llvm::None,
5855 createRuntimeFunction(OMPRTL__kmpc_end_reduce),
5856 EndArgs);
5857 AtomicRCG.setAction(Action);
5858 AtomicRCG(CGF);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005859 } else {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00005860 AtomicRCG(CGF);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005861 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005862
5863 CGF.EmitBranch(DefaultBB);
5864 CGF.EmitBlock(DefaultBB, /*IsFinished=*/true);
5865}
5866
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005867/// Generates unique name for artificial threadprivate variables.
Alexey Bataev1c44e152018-03-06 18:59:43 +00005868/// Format is: <Prefix> "." <Decl_mangled_name> "_" "<Decl_start_loc_raw_enc>"
5869static std::string generateUniqueName(CodeGenModule &CGM, StringRef Prefix,
5870 const Expr *Ref) {
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005871 SmallString<256> Buffer;
5872 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev1c44e152018-03-06 18:59:43 +00005873 const clang::DeclRefExpr *DE;
5874 const VarDecl *D = ::getBaseDecl(Ref, DE);
5875 if (!D)
5876 D = cast<VarDecl>(cast<DeclRefExpr>(Ref)->getDecl());
5877 D = D->getCanonicalDecl();
Alexey Bataev18fa2322018-05-02 14:20:50 +00005878 std::string Name = CGM.getOpenMPRuntime().getName(
5879 {D->isLocalVarDeclOrParm() ? D->getName() : CGM.getMangledName(D)});
5880 Out << Prefix << Name << "_"
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005881 << D->getCanonicalDecl()->getBeginLoc().getRawEncoding();
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005882 return Out.str();
5883}
5884
5885/// Emits reduction initializer function:
5886/// \code
5887/// void @.red_init(void* %arg) {
5888/// %0 = bitcast void* %arg to <type>*
5889/// store <type> <init>, <type>* %0
5890/// ret void
5891/// }
5892/// \endcode
5893static llvm::Value *emitReduceInitFunction(CodeGenModule &CGM,
5894 SourceLocation Loc,
5895 ReductionCodeGen &RCG, unsigned N) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005896 ASTContext &C = CGM.getContext();
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005897 FunctionArgList Args;
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005898 ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
5899 ImplicitParamDecl::Other);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005900 Args.emplace_back(&Param);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005901 const auto &FnInfo =
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005902 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005903 llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
Alexey Bataev18fa2322018-05-02 14:20:50 +00005904 std::string Name = CGM.getOpenMPRuntime().getName({"red_init", ""});
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005905 auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage,
Alexey Bataev18fa2322018-05-02 14:20:50 +00005906 Name, &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00005907 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo);
Alexey Bataevc0f879b2018-04-10 20:10:53 +00005908 Fn->setDoesNotRecurse();
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005909 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005910 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005911 Address PrivateAddr = CGF.EmitLoadOfPointer(
5912 CGF.GetAddrOfLocalVar(&Param),
5913 C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
5914 llvm::Value *Size = nullptr;
5915 // If the size of the reduction item is non-constant, load it from global
5916 // threadprivate variable.
5917 if (RCG.getSizes(N).second) {
5918 Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate(
5919 CGF, CGM.getContext().getSizeType(),
Alexey Bataev1c44e152018-03-06 18:59:43 +00005920 generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N)));
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00005921 Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false,
5922 CGM.getContext().getSizeType(), Loc);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005923 }
5924 RCG.emitAggregateType(CGF, N, Size);
5925 LValue SharedLVal;
5926 // If initializer uses initializer from declare reduction construct, emit a
5927 // pointer to the address of the original reduction item (reuired by reduction
5928 // initializer)
5929 if (RCG.usesReductionInitializer(N)) {
5930 Address SharedAddr =
5931 CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate(
5932 CGF, CGM.getContext().VoidPtrTy,
Alexey Bataev1c44e152018-03-06 18:59:43 +00005933 generateUniqueName(CGM, "reduction", RCG.getRefExpr(N)));
Alexey Bataev21dab122018-03-09 15:20:30 +00005934 SharedAddr = CGF.EmitLoadOfPointer(
5935 SharedAddr,
5936 CGM.getContext().VoidPtrTy.castAs<PointerType>()->getTypePtr());
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005937 SharedLVal = CGF.MakeAddrLValue(SharedAddr, CGM.getContext().VoidPtrTy);
5938 } else {
5939 SharedLVal = CGF.MakeNaturalAlignAddrLValue(
5940 llvm::ConstantPointerNull::get(CGM.VoidPtrTy),
5941 CGM.getContext().VoidPtrTy);
5942 }
5943 // Emit the initializer:
5944 // %0 = bitcast void* %arg to <type>*
5945 // store <type> <init>, <type>* %0
5946 RCG.emitInitialization(CGF, N, PrivateAddr, SharedLVal,
5947 [](CodeGenFunction &) { return false; });
5948 CGF.FinishFunction();
5949 return Fn;
5950}
5951
5952/// Emits reduction combiner function:
5953/// \code
5954/// void @.red_comb(void* %arg0, void* %arg1) {
5955/// %lhs = bitcast void* %arg0 to <type>*
5956/// %rhs = bitcast void* %arg1 to <type>*
5957/// %2 = <ReductionOp>(<type>* %lhs, <type>* %rhs)
5958/// store <type> %2, <type>* %lhs
5959/// ret void
5960/// }
5961/// \endcode
5962static llvm::Value *emitReduceCombFunction(CodeGenModule &CGM,
5963 SourceLocation Loc,
5964 ReductionCodeGen &RCG, unsigned N,
5965 const Expr *ReductionOp,
5966 const Expr *LHS, const Expr *RHS,
5967 const Expr *PrivateRef) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005968 ASTContext &C = CGM.getContext();
5969 const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(LHS)->getDecl());
5970 const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(RHS)->getDecl());
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005971 FunctionArgList Args;
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005972 ImplicitParamDecl ParamInOut(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
5973 C.VoidPtrTy, ImplicitParamDecl::Other);
5974 ImplicitParamDecl ParamIn(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
5975 ImplicitParamDecl::Other);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005976 Args.emplace_back(&ParamInOut);
5977 Args.emplace_back(&ParamIn);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005978 const auto &FnInfo =
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005979 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00005980 llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
Alexey Bataev18fa2322018-05-02 14:20:50 +00005981 std::string Name = CGM.getOpenMPRuntime().getName({"red_comb", ""});
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005982 auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage,
Alexey Bataev18fa2322018-05-02 14:20:50 +00005983 Name, &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00005984 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo);
Alexey Bataevc0f879b2018-04-10 20:10:53 +00005985 Fn->setDoesNotRecurse();
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005986 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00005987 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005988 llvm::Value *Size = nullptr;
5989 // If the size of the reduction item is non-constant, load it from global
5990 // threadprivate variable.
5991 if (RCG.getSizes(N).second) {
5992 Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate(
5993 CGF, CGM.getContext().getSizeType(),
Alexey Bataev1c44e152018-03-06 18:59:43 +00005994 generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N)));
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00005995 Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false,
5996 CGM.getContext().getSizeType(), Loc);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00005997 }
5998 RCG.emitAggregateType(CGF, N, Size);
5999 // Remap lhs and rhs variables to the addresses of the function arguments.
6000 // %lhs = bitcast void* %arg0 to <type>*
6001 // %rhs = bitcast void* %arg1 to <type>*
6002 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006003 PrivateScope.addPrivate(LHSVD, [&C, &CGF, &ParamInOut, LHSVD]() {
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00006004 // Pull out the pointer to the variable.
6005 Address PtrAddr = CGF.EmitLoadOfPointer(
6006 CGF.GetAddrOfLocalVar(&ParamInOut),
6007 C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
6008 return CGF.Builder.CreateElementBitCast(
6009 PtrAddr, CGF.ConvertTypeForMem(LHSVD->getType()));
6010 });
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006011 PrivateScope.addPrivate(RHSVD, [&C, &CGF, &ParamIn, RHSVD]() {
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00006012 // Pull out the pointer to the variable.
6013 Address PtrAddr = CGF.EmitLoadOfPointer(
6014 CGF.GetAddrOfLocalVar(&ParamIn),
6015 C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
6016 return CGF.Builder.CreateElementBitCast(
6017 PtrAddr, CGF.ConvertTypeForMem(RHSVD->getType()));
6018 });
6019 PrivateScope.Privatize();
6020 // Emit the combiner body:
6021 // %2 = <ReductionOp>(<type> *%lhs, <type> *%rhs)
6022 // store <type> %2, <type>* %lhs
6023 CGM.getOpenMPRuntime().emitSingleReductionCombiner(
6024 CGF, ReductionOp, PrivateRef, cast<DeclRefExpr>(LHS),
6025 cast<DeclRefExpr>(RHS));
6026 CGF.FinishFunction();
6027 return Fn;
6028}
6029
6030/// Emits reduction finalizer function:
6031/// \code
6032/// void @.red_fini(void* %arg) {
6033/// %0 = bitcast void* %arg to <type>*
6034/// <destroy>(<type>* %0)
6035/// ret void
6036/// }
6037/// \endcode
6038static llvm::Value *emitReduceFiniFunction(CodeGenModule &CGM,
6039 SourceLocation Loc,
6040 ReductionCodeGen &RCG, unsigned N) {
6041 if (!RCG.needCleanups(N))
6042 return nullptr;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006043 ASTContext &C = CGM.getContext();
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00006044 FunctionArgList Args;
Alexey Bataev7cae94e2018-01-04 19:45:16 +00006045 ImplicitParamDecl Param(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.VoidPtrTy,
6046 ImplicitParamDecl::Other);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00006047 Args.emplace_back(&Param);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006048 const auto &FnInfo =
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00006049 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006050 llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
Alexey Bataev18fa2322018-05-02 14:20:50 +00006051 std::string Name = CGM.getOpenMPRuntime().getName({"red_fini", ""});
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00006052 auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage,
Alexey Bataev18fa2322018-05-02 14:20:50 +00006053 Name, &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00006054 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo);
Alexey Bataevc0f879b2018-04-10 20:10:53 +00006055 Fn->setDoesNotRecurse();
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00006056 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00006057 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00006058 Address PrivateAddr = CGF.EmitLoadOfPointer(
6059 CGF.GetAddrOfLocalVar(&Param),
6060 C.getPointerType(C.VoidPtrTy).castAs<PointerType>());
6061 llvm::Value *Size = nullptr;
6062 // If the size of the reduction item is non-constant, load it from global
6063 // threadprivate variable.
6064 if (RCG.getSizes(N).second) {
6065 Address SizeAddr = CGM.getOpenMPRuntime().getAddrOfArtificialThreadPrivate(
6066 CGF, CGM.getContext().getSizeType(),
Alexey Bataev1c44e152018-03-06 18:59:43 +00006067 generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N)));
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00006068 Size = CGF.EmitLoadOfScalar(SizeAddr, /*Volatile=*/false,
6069 CGM.getContext().getSizeType(), Loc);
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00006070 }
6071 RCG.emitAggregateType(CGF, N, Size);
6072 // Emit the finalizer body:
6073 // <destroy>(<type>* %0)
6074 RCG.emitCleanups(CGF, N, PrivateAddr);
6075 CGF.FinishFunction();
6076 return Fn;
6077}
6078
6079llvm::Value *CGOpenMPRuntime::emitTaskReductionInit(
6080 CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs,
6081 ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) {
6082 if (!CGF.HaveInsertPoint() || Data.ReductionVars.empty())
6083 return nullptr;
6084
6085 // Build typedef struct:
6086 // kmp_task_red_input {
6087 // void *reduce_shar; // shared reduction item
6088 // size_t reduce_size; // size of data item
6089 // void *reduce_init; // data initialization routine
6090 // void *reduce_fini; // data finalization routine
6091 // void *reduce_comb; // data combiner routine
6092 // kmp_task_red_flags_t flags; // flags for additional info from compiler
6093 // } kmp_task_red_input_t;
6094 ASTContext &C = CGM.getContext();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006095 RecordDecl *RD = C.buildImplicitRecord("kmp_task_red_input_t");
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00006096 RD->startDefinition();
6097 const FieldDecl *SharedFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy);
6098 const FieldDecl *SizeFD = addFieldToRecordDecl(C, RD, C.getSizeType());
6099 const FieldDecl *InitFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy);
6100 const FieldDecl *FiniFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy);
6101 const FieldDecl *CombFD = addFieldToRecordDecl(C, RD, C.VoidPtrTy);
6102 const FieldDecl *FlagsFD = addFieldToRecordDecl(
6103 C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/false));
6104 RD->completeDefinition();
6105 QualType RDType = C.getRecordType(RD);
6106 unsigned Size = Data.ReductionVars.size();
6107 llvm::APInt ArraySize(/*numBits=*/64, Size);
6108 QualType ArrayRDType = C.getConstantArrayType(
6109 RDType, ArraySize, ArrayType::Normal, /*IndexTypeQuals=*/0);
6110 // kmp_task_red_input_t .rd_input.[Size];
6111 Address TaskRedInput = CGF.CreateMemTemp(ArrayRDType, ".rd_input.");
6112 ReductionCodeGen RCG(Data.ReductionVars, Data.ReductionCopies,
6113 Data.ReductionOps);
6114 for (unsigned Cnt = 0; Cnt < Size; ++Cnt) {
6115 // kmp_task_red_input_t &ElemLVal = .rd_input.[Cnt];
6116 llvm::Value *Idxs[] = {llvm::ConstantInt::get(CGM.SizeTy, /*V=*/0),
6117 llvm::ConstantInt::get(CGM.SizeTy, Cnt)};
6118 llvm::Value *GEP = CGF.EmitCheckedInBoundsGEP(
6119 TaskRedInput.getPointer(), Idxs,
6120 /*SignedIndices=*/false, /*IsSubtraction=*/false, Loc,
6121 ".rd_input.gep.");
6122 LValue ElemLVal = CGF.MakeNaturalAlignAddrLValue(GEP, RDType);
6123 // ElemLVal.reduce_shar = &Shareds[Cnt];
6124 LValue SharedLVal = CGF.EmitLValueForField(ElemLVal, SharedFD);
6125 RCG.emitSharedLValue(CGF, Cnt);
6126 llvm::Value *CastedShared =
6127 CGF.EmitCastToVoidPtr(RCG.getSharedLValue(Cnt).getPointer());
6128 CGF.EmitStoreOfScalar(CastedShared, SharedLVal);
6129 RCG.emitAggregateType(CGF, Cnt);
6130 llvm::Value *SizeValInChars;
6131 llvm::Value *SizeVal;
6132 std::tie(SizeValInChars, SizeVal) = RCG.getSizes(Cnt);
6133 // We use delayed creation/initialization for VLAs, array sections and
6134 // custom reduction initializations. It is required because runtime does not
6135 // provide the way to pass the sizes of VLAs/array sections to
6136 // initializer/combiner/finalizer functions and does not pass the pointer to
6137 // original reduction item to the initializer. Instead threadprivate global
6138 // variables are used to store these values and use them in the functions.
6139 bool DelayedCreation = !!SizeVal;
6140 SizeValInChars = CGF.Builder.CreateIntCast(SizeValInChars, CGM.SizeTy,
6141 /*isSigned=*/false);
6142 LValue SizeLVal = CGF.EmitLValueForField(ElemLVal, SizeFD);
6143 CGF.EmitStoreOfScalar(SizeValInChars, SizeLVal);
6144 // ElemLVal.reduce_init = init;
6145 LValue InitLVal = CGF.EmitLValueForField(ElemLVal, InitFD);
6146 llvm::Value *InitAddr =
6147 CGF.EmitCastToVoidPtr(emitReduceInitFunction(CGM, Loc, RCG, Cnt));
6148 CGF.EmitStoreOfScalar(InitAddr, InitLVal);
6149 DelayedCreation = DelayedCreation || RCG.usesReductionInitializer(Cnt);
6150 // ElemLVal.reduce_fini = fini;
6151 LValue FiniLVal = CGF.EmitLValueForField(ElemLVal, FiniFD);
6152 llvm::Value *Fini = emitReduceFiniFunction(CGM, Loc, RCG, Cnt);
6153 llvm::Value *FiniAddr = Fini
6154 ? CGF.EmitCastToVoidPtr(Fini)
6155 : llvm::ConstantPointerNull::get(CGM.VoidPtrTy);
6156 CGF.EmitStoreOfScalar(FiniAddr, FiniLVal);
6157 // ElemLVal.reduce_comb = comb;
6158 LValue CombLVal = CGF.EmitLValueForField(ElemLVal, CombFD);
6159 llvm::Value *CombAddr = CGF.EmitCastToVoidPtr(emitReduceCombFunction(
6160 CGM, Loc, RCG, Cnt, Data.ReductionOps[Cnt], LHSExprs[Cnt],
6161 RHSExprs[Cnt], Data.ReductionCopies[Cnt]));
6162 CGF.EmitStoreOfScalar(CombAddr, CombLVal);
6163 // ElemLVal.flags = 0;
6164 LValue FlagsLVal = CGF.EmitLValueForField(ElemLVal, FlagsFD);
6165 if (DelayedCreation) {
6166 CGF.EmitStoreOfScalar(
6167 llvm::ConstantInt::get(CGM.Int32Ty, /*V=*/1, /*IsSigned=*/true),
6168 FlagsLVal);
6169 } else
6170 CGF.EmitNullInitialization(FlagsLVal.getAddress(), FlagsLVal.getType());
6171 }
6172 // Build call void *__kmpc_task_reduction_init(int gtid, int num_data, void
6173 // *data);
6174 llvm::Value *Args[] = {
6175 CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy,
6176 /*isSigned=*/true),
6177 llvm::ConstantInt::get(CGM.IntTy, Size, /*isSigned=*/true),
6178 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(TaskRedInput.getPointer(),
6179 CGM.VoidPtrTy)};
6180 return CGF.EmitRuntimeCall(
6181 createRuntimeFunction(OMPRTL__kmpc_task_reduction_init), Args);
6182}
6183
6184void CGOpenMPRuntime::emitTaskReductionFixups(CodeGenFunction &CGF,
6185 SourceLocation Loc,
6186 ReductionCodeGen &RCG,
6187 unsigned N) {
6188 auto Sizes = RCG.getSizes(N);
6189 // Emit threadprivate global variable if the type is non-constant
6190 // (Sizes.second = nullptr).
6191 if (Sizes.second) {
6192 llvm::Value *SizeVal = CGF.Builder.CreateIntCast(Sizes.second, CGM.SizeTy,
6193 /*isSigned=*/false);
6194 Address SizeAddr = getAddrOfArtificialThreadPrivate(
6195 CGF, CGM.getContext().getSizeType(),
Alexey Bataev1c44e152018-03-06 18:59:43 +00006196 generateUniqueName(CGM, "reduction_size", RCG.getRefExpr(N)));
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00006197 CGF.Builder.CreateStore(SizeVal, SizeAddr, /*IsVolatile=*/false);
6198 }
6199 // Store address of the original reduction item if custom initializer is used.
6200 if (RCG.usesReductionInitializer(N)) {
6201 Address SharedAddr = getAddrOfArtificialThreadPrivate(
6202 CGF, CGM.getContext().VoidPtrTy,
Alexey Bataev1c44e152018-03-06 18:59:43 +00006203 generateUniqueName(CGM, "reduction", RCG.getRefExpr(N)));
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00006204 CGF.Builder.CreateStore(
6205 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
6206 RCG.getSharedLValue(N).getPointer(), CGM.VoidPtrTy),
6207 SharedAddr, /*IsVolatile=*/false);
6208 }
6209}
6210
6211Address CGOpenMPRuntime::getTaskReductionItem(CodeGenFunction &CGF,
6212 SourceLocation Loc,
6213 llvm::Value *ReductionsPtr,
6214 LValue SharedLVal) {
6215 // Build call void *__kmpc_task_reduction_get_th_data(int gtid, void *tg, void
6216 // *d);
6217 llvm::Value *Args[] = {
6218 CGF.Builder.CreateIntCast(getThreadID(CGF, Loc), CGM.IntTy,
6219 /*isSigned=*/true),
6220 ReductionsPtr,
6221 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(SharedLVal.getPointer(),
6222 CGM.VoidPtrTy)};
6223 return Address(
6224 CGF.EmitRuntimeCall(
6225 createRuntimeFunction(OMPRTL__kmpc_task_reduction_get_th_data), Args),
6226 SharedLVal.getAlignment());
6227}
6228
Alexey Bataev8b8e2022015-04-27 05:22:09 +00006229void CGOpenMPRuntime::emitTaskwaitCall(CodeGenFunction &CGF,
6230 SourceLocation Loc) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00006231 if (!CGF.HaveInsertPoint())
6232 return;
Alexey Bataev8b8e2022015-04-27 05:22:09 +00006233 // Build call kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32
6234 // global_tid);
6235 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
6236 // Ignore return result until untied tasks are supported.
6237 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskwait), Args);
Alexey Bataev48591dd2016-04-20 04:01:36 +00006238 if (auto *Region = dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
6239 Region->emitUntiedSwitch(CGF);
Alexey Bataev8b8e2022015-04-27 05:22:09 +00006240}
6241
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00006242void CGOpenMPRuntime::emitInlinedDirective(CodeGenFunction &CGF,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00006243 OpenMPDirectiveKind InnerKind,
Alexey Bataev25e5b442015-09-15 12:52:43 +00006244 const RegionCodeGenTy &CodeGen,
6245 bool HasCancel) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00006246 if (!CGF.HaveInsertPoint())
6247 return;
Alexey Bataev25e5b442015-09-15 12:52:43 +00006248 InlinedOpenMPRegionRAII Region(CGF, CodeGen, InnerKind, HasCancel);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00006249 CGF.CapturedStmtInfo->EmitBody(CGF, /*S=*/nullptr);
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00006250}
6251
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006252namespace {
6253enum RTCancelKind {
6254 CancelNoreq = 0,
6255 CancelParallel = 1,
6256 CancelLoop = 2,
6257 CancelSections = 3,
6258 CancelTaskgroup = 4
6259};
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00006260} // anonymous namespace
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006261
6262static RTCancelKind getCancellationKind(OpenMPDirectiveKind CancelRegion) {
6263 RTCancelKind CancelKind = CancelNoreq;
Alexey Bataev0f34da12015-07-02 04:17:07 +00006264 if (CancelRegion == OMPD_parallel)
6265 CancelKind = CancelParallel;
6266 else if (CancelRegion == OMPD_for)
6267 CancelKind = CancelLoop;
6268 else if (CancelRegion == OMPD_sections)
6269 CancelKind = CancelSections;
6270 else {
6271 assert(CancelRegion == OMPD_taskgroup);
6272 CancelKind = CancelTaskgroup;
6273 }
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006274 return CancelKind;
6275}
6276
6277void CGOpenMPRuntime::emitCancellationPointCall(
6278 CodeGenFunction &CGF, SourceLocation Loc,
6279 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00006280 if (!CGF.HaveInsertPoint())
6281 return;
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006282 // Build call kmp_int32 __kmpc_cancellationpoint(ident_t *loc, kmp_int32
6283 // global_tid, kmp_int32 cncl_kind);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00006284 if (auto *OMPRegionInfo =
6285 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
Jonas Hahnfeldb07931f2017-02-17 18:32:58 +00006286 // For 'cancellation point taskgroup', the task region info may not have a
6287 // cancel. This may instead happen in another adjacent task.
6288 if (CancelRegion == OMPD_taskgroup || OMPRegionInfo->hasCancel()) {
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006289 llvm::Value *Args[] = {
6290 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
6291 CGF.Builder.getInt32(getCancellationKind(CancelRegion))};
Alexey Bataev81c7ea02015-07-03 09:56:58 +00006292 // Ignore return result until untied tasks are supported.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006293 llvm::Value *Result = CGF.EmitRuntimeCall(
Alexey Bataev81c7ea02015-07-03 09:56:58 +00006294 createRuntimeFunction(OMPRTL__kmpc_cancellationpoint), Args);
6295 // if (__kmpc_cancellationpoint()) {
Alexey Bataev81c7ea02015-07-03 09:56:58 +00006296 // exit from construct;
6297 // }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006298 llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit");
6299 llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue");
6300 llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00006301 CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB);
6302 CGF.EmitBlock(ExitBB);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00006303 // exit from construct;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006304 CodeGenFunction::JumpDest CancelDest =
Alexey Bataev25e5b442015-09-15 12:52:43 +00006305 CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind());
Alexey Bataev81c7ea02015-07-03 09:56:58 +00006306 CGF.EmitBranchThroughCleanup(CancelDest);
6307 CGF.EmitBlock(ContBB, /*IsFinished=*/true);
6308 }
Alexey Bataev0f34da12015-07-02 04:17:07 +00006309 }
Alexey Bataev0f34da12015-07-02 04:17:07 +00006310}
6311
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006312void CGOpenMPRuntime::emitCancelCall(CodeGenFunction &CGF, SourceLocation Loc,
Alexey Bataev87933c72015-09-18 08:07:34 +00006313 const Expr *IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006314 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00006315 if (!CGF.HaveInsertPoint())
6316 return;
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006317 // Build call kmp_int32 __kmpc_cancel(ident_t *loc, kmp_int32 global_tid,
6318 // kmp_int32 cncl_kind);
6319 if (auto *OMPRegionInfo =
6320 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006321 auto &&ThenGen = [Loc, CancelRegion, OMPRegionInfo](CodeGenFunction &CGF,
6322 PrePostActionTy &) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006323 CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime();
Alexey Bataev87933c72015-09-18 08:07:34 +00006324 llvm::Value *Args[] = {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006325 RT.emitUpdateLocation(CGF, Loc), RT.getThreadID(CGF, Loc),
Alexey Bataev87933c72015-09-18 08:07:34 +00006326 CGF.Builder.getInt32(getCancellationKind(CancelRegion))};
6327 // Ignore return result until untied tasks are supported.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006328 llvm::Value *Result = CGF.EmitRuntimeCall(
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006329 RT.createRuntimeFunction(OMPRTL__kmpc_cancel), Args);
Alexey Bataev87933c72015-09-18 08:07:34 +00006330 // if (__kmpc_cancel()) {
Alexey Bataev87933c72015-09-18 08:07:34 +00006331 // exit from construct;
6332 // }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006333 llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".cancel.exit");
6334 llvm::BasicBlock *ContBB = CGF.createBasicBlock(".cancel.continue");
6335 llvm::Value *Cmp = CGF.Builder.CreateIsNotNull(Result);
Alexey Bataev87933c72015-09-18 08:07:34 +00006336 CGF.Builder.CreateCondBr(Cmp, ExitBB, ContBB);
6337 CGF.EmitBlock(ExitBB);
Alexey Bataev87933c72015-09-18 08:07:34 +00006338 // exit from construct;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006339 CodeGenFunction::JumpDest CancelDest =
Alexey Bataev87933c72015-09-18 08:07:34 +00006340 CGF.getOMPCancelDestination(OMPRegionInfo->getDirectiveKind());
6341 CGF.EmitBranchThroughCleanup(CancelDest);
6342 CGF.EmitBlock(ContBB, /*IsFinished=*/true);
6343 };
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006344 if (IfCond) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006345 emitOMPIfClause(CGF, IfCond, ThenGen,
6346 [](CodeGenFunction &, PrePostActionTy &) {});
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006347 } else {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006348 RegionCodeGenTy ThenRCG(ThenGen);
6349 ThenRCG(CGF);
6350 }
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00006351 }
6352}
Samuel Antaobed3c462015-10-02 16:14:20 +00006353
Samuel Antaoee8fb302016-01-06 13:42:12 +00006354void CGOpenMPRuntime::emitTargetOutlinedFunction(
6355 const OMPExecutableDirective &D, StringRef ParentName,
6356 llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00006357 bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00006358 assert(!ParentName.empty() && "Invalid target region parent name!");
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00006359 emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID,
6360 IsOffloadEntry, CodeGen);
6361}
6362
6363void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper(
6364 const OMPExecutableDirective &D, StringRef ParentName,
6365 llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
6366 bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
Samuel Antao2de62b02016-02-13 23:35:10 +00006367 // Create a unique name for the entry function using the source location
6368 // information of the current target region. The name will be something like:
Samuel Antaoee8fb302016-01-06 13:42:12 +00006369 //
Samuel Antao2de62b02016-02-13 23:35:10 +00006370 // __omp_offloading_DD_FFFF_PP_lBB
Samuel Antaoee8fb302016-01-06 13:42:12 +00006371 //
6372 // where DD_FFFF is an ID unique to the file (device and file IDs), PP is the
Samuel Antao2de62b02016-02-13 23:35:10 +00006373 // mangled name of the function that encloses the target region and BB is the
6374 // line number of the target region.
Samuel Antaoee8fb302016-01-06 13:42:12 +00006375
6376 unsigned DeviceID;
6377 unsigned FileID;
6378 unsigned Line;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006379 getTargetEntryUniqueInfo(CGM.getContext(), D.getBeginLoc(), DeviceID, FileID,
Samuel Antao2de62b02016-02-13 23:35:10 +00006380 Line);
Samuel Antaoee8fb302016-01-06 13:42:12 +00006381 SmallString<64> EntryFnName;
6382 {
6383 llvm::raw_svector_ostream OS(EntryFnName);
Samuel Antao2de62b02016-02-13 23:35:10 +00006384 OS << "__omp_offloading" << llvm::format("_%x", DeviceID)
6385 << llvm::format("_%x_", FileID) << ParentName << "_l" << Line;
Samuel Antaoee8fb302016-01-06 13:42:12 +00006386 }
6387
Alexey Bataev475a7442018-01-12 19:39:11 +00006388 const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00006389
Samuel Antaobed3c462015-10-02 16:14:20 +00006390 CodeGenFunction CGF(CGM, true);
Samuel Antaoee8fb302016-01-06 13:42:12 +00006391 CGOpenMPTargetRegionInfo CGInfo(CS, CodeGen, EntryFnName);
Samuel Antaobed3c462015-10-02 16:14:20 +00006392 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
Samuel Antaoee8fb302016-01-06 13:42:12 +00006393
Samuel Antao6d004262016-06-16 18:39:34 +00006394 OutlinedFn = CGF.GenerateOpenMPCapturedStmtFunction(CS);
Samuel Antaoee8fb302016-01-06 13:42:12 +00006395
6396 // If this target outline function is not an offload entry, we don't need to
6397 // register it.
6398 if (!IsOffloadEntry)
6399 return;
6400
6401 // The target region ID is used by the runtime library to identify the current
6402 // target region, so it only has to be unique and not necessarily point to
6403 // anything. It could be the pointer to the outlined function that implements
6404 // the target region, but we aren't using that so that the compiler doesn't
6405 // need to keep that, and could therefore inline the host function if proven
6406 // worthwhile during optimization. In the other hand, if emitting code for the
6407 // device, the ID has to be the function address so that it can retrieved from
6408 // the offloading entry and launched by the runtime library. We also mark the
6409 // outlined function to have external linkage in case we are emitting code for
6410 // the device, because these functions will be entry points to the device.
6411
6412 if (CGM.getLangOpts().OpenMPIsDevice) {
6413 OutlinedFnID = llvm::ConstantExpr::getBitCast(OutlinedFn, CGM.Int8PtrTy);
Alexey Bataev9a700172018-05-08 14:16:57 +00006414 OutlinedFn->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Rafael Espindolacbca4872018-01-11 22:15:12 +00006415 OutlinedFn->setDSOLocal(false);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006416 } else {
Alexey Bataevc15ea702018-05-09 18:02:37 +00006417 std::string Name = getName({EntryFnName, "region_id"});
Samuel Antaoee8fb302016-01-06 13:42:12 +00006418 OutlinedFnID = new llvm::GlobalVariable(
6419 CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
Alexey Bataev9a700172018-05-08 14:16:57 +00006420 llvm::GlobalValue::WeakAnyLinkage,
Alexey Bataev18fa2322018-05-02 14:20:50 +00006421 llvm::Constant::getNullValue(CGM.Int8Ty), Name);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006422 }
Samuel Antaoee8fb302016-01-06 13:42:12 +00006423
6424 // Register the information for the entry associated with this target region.
6425 OffloadEntriesInfoManager.registerTargetRegionEntryInfo(
Samuel Antaof83efdb2017-01-05 16:02:49 +00006426 DeviceID, FileID, ParentName, Line, OutlinedFn, OutlinedFnID,
Alexey Bataev03f270c2018-03-30 18:31:07 +00006427 OffloadEntriesInfoManagerTy::OMPTargetRegionEntryTargetRegion);
Samuel Antaobed3c462015-10-02 16:14:20 +00006428}
6429
Carlo Bertolli6eee9062016-04-29 01:37:30 +00006430/// discard all CompoundStmts intervening between two constructs
6431static const Stmt *ignoreCompoundStmts(const Stmt *Body) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006432 while (const auto *CS = dyn_cast_or_null<CompoundStmt>(Body))
Carlo Bertolli6eee9062016-04-29 01:37:30 +00006433 Body = CS->body_front();
6434
6435 return Body;
6436}
6437
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006438/// Emit the number of teams for a target directive. Inspect the num_teams
6439/// clause associated with a teams construct combined or closely nested
6440/// with the target directive.
6441///
6442/// Emit a team of size one for directives such as 'target parallel' that
6443/// have no associated teams construct.
6444///
6445/// Otherwise, return nullptr.
Samuel Antaob68e2db2016-03-03 16:20:23 +00006446static llvm::Value *
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006447emitNumTeamsForTargetDirective(CGOpenMPRuntime &OMPRuntime,
6448 CodeGenFunction &CGF,
6449 const OMPExecutableDirective &D) {
Samuel Antaob68e2db2016-03-03 16:20:23 +00006450 assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the "
6451 "teams directive expected to be "
6452 "emitted only for the host!");
6453
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006454 CGBuilderTy &Bld = CGF.Builder;
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00006455
6456 // If the target directive is combined with a teams directive:
6457 // Return the value in the num_teams clause, if any.
6458 // Otherwise, return 0 to denote the runtime default.
6459 if (isOpenMPTeamsDirective(D.getDirectiveKind())) {
6460 if (const auto *NumTeamsClause = D.getSingleClause<OMPNumTeamsClause>()) {
6461 CodeGenFunction::RunCleanupsScope NumTeamsScope(CGF);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006462 llvm::Value *NumTeams = CGF.EmitScalarExpr(NumTeamsClause->getNumTeams(),
6463 /*IgnoreResultAssign*/ true);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00006464 return Bld.CreateIntCast(NumTeams, CGF.Int32Ty,
6465 /*IsSigned=*/true);
6466 }
6467
6468 // The default value is 0.
6469 return Bld.getInt32(0);
6470 }
6471
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006472 // If the target directive is combined with a parallel directive but not a
6473 // teams directive, start one team.
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00006474 if (isOpenMPParallelDirective(D.getDirectiveKind()))
6475 return Bld.getInt32(1);
Samuel Antaob68e2db2016-03-03 16:20:23 +00006476
6477 // If the current target region has a teams region enclosed, we need to get
6478 // the number of teams to pass to the runtime function call. This is done
6479 // by generating the expression in a inlined region. This is required because
6480 // the expression is captured in the enclosing target environment when the
6481 // teams directive is not combined with target.
6482
Alexey Bataev475a7442018-01-12 19:39:11 +00006483 const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target);
Samuel Antaob68e2db2016-03-03 16:20:23 +00006484
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006485 if (const auto *TeamsDir = dyn_cast_or_null<OMPExecutableDirective>(
Carlo Bertolli6eee9062016-04-29 01:37:30 +00006486 ignoreCompoundStmts(CS.getCapturedStmt()))) {
Alexey Bataev50a1c782017-12-01 21:31:08 +00006487 if (isOpenMPTeamsDirective(TeamsDir->getDirectiveKind())) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006488 if (const auto *NTE = TeamsDir->getSingleClause<OMPNumTeamsClause>()) {
Alexey Bataev50a1c782017-12-01 21:31:08 +00006489 CGOpenMPInnerExprInfo CGInfo(CGF, CS);
6490 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
6491 llvm::Value *NumTeams = CGF.EmitScalarExpr(NTE->getNumTeams());
6492 return Bld.CreateIntCast(NumTeams, CGF.Int32Ty,
6493 /*IsSigned=*/true);
6494 }
Samuel Antaob68e2db2016-03-03 16:20:23 +00006495
Alexey Bataev50a1c782017-12-01 21:31:08 +00006496 // If we have an enclosed teams directive but no num_teams clause we use
6497 // the default value 0.
6498 return Bld.getInt32(0);
6499 }
Samuel Antaob68e2db2016-03-03 16:20:23 +00006500 }
6501
6502 // No teams associated with the directive.
6503 return nullptr;
6504}
6505
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006506/// Emit the number of threads for a target directive. Inspect the
6507/// thread_limit clause associated with a teams construct combined or closely
6508/// nested with the target directive.
6509///
6510/// Emit the num_threads clause for directives such as 'target parallel' that
6511/// have no associated teams construct.
6512///
6513/// Otherwise, return nullptr.
Samuel Antaob68e2db2016-03-03 16:20:23 +00006514static llvm::Value *
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006515emitNumThreadsForTargetDirective(CGOpenMPRuntime &OMPRuntime,
6516 CodeGenFunction &CGF,
6517 const OMPExecutableDirective &D) {
Samuel Antaob68e2db2016-03-03 16:20:23 +00006518 assert(!CGF.getLangOpts().OpenMPIsDevice && "Clauses associated with the "
6519 "teams directive expected to be "
6520 "emitted only for the host!");
6521
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006522 CGBuilderTy &Bld = CGF.Builder;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006523
6524 //
6525 // If the target directive is combined with a teams directive:
6526 // Return the value in the thread_limit clause, if any.
6527 //
6528 // If the target directive is combined with a parallel directive:
6529 // Return the value in the num_threads clause, if any.
6530 //
6531 // If both clauses are set, select the minimum of the two.
6532 //
6533 // If neither teams or parallel combined directives set the number of threads
6534 // in a team, return 0 to denote the runtime default.
6535 //
6536 // If this is not a teams directive return nullptr.
6537
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00006538 if (isOpenMPTeamsDirective(D.getDirectiveKind()) ||
6539 isOpenMPParallelDirective(D.getDirectiveKind())) {
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006540 llvm::Value *DefaultThreadLimitVal = Bld.getInt32(0);
6541 llvm::Value *NumThreadsVal = nullptr;
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00006542 llvm::Value *ThreadLimitVal = nullptr;
6543
6544 if (const auto *ThreadLimitClause =
6545 D.getSingleClause<OMPThreadLimitClause>()) {
6546 CodeGenFunction::RunCleanupsScope ThreadLimitScope(CGF);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006547 llvm::Value *ThreadLimit =
6548 CGF.EmitScalarExpr(ThreadLimitClause->getThreadLimit(),
6549 /*IgnoreResultAssign*/ true);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00006550 ThreadLimitVal = Bld.CreateIntCast(ThreadLimit, CGF.Int32Ty,
6551 /*IsSigned=*/true);
6552 }
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00006553
6554 if (const auto *NumThreadsClause =
6555 D.getSingleClause<OMPNumThreadsClause>()) {
6556 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
6557 llvm::Value *NumThreads =
6558 CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
6559 /*IgnoreResultAssign*/ true);
6560 NumThreadsVal =
6561 Bld.CreateIntCast(NumThreads, CGF.Int32Ty, /*IsSigned=*/true);
6562 }
6563
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00006564 // Select the lesser of thread_limit and num_threads.
6565 if (NumThreadsVal)
6566 ThreadLimitVal = ThreadLimitVal
6567 ? Bld.CreateSelect(Bld.CreateICmpSLT(NumThreadsVal,
6568 ThreadLimitVal),
6569 NumThreadsVal, ThreadLimitVal)
6570 : NumThreadsVal;
Samuel Antaob68e2db2016-03-03 16:20:23 +00006571
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00006572 // Set default value passed to the runtime if either teams or a target
6573 // parallel type directive is found but no clause is specified.
6574 if (!ThreadLimitVal)
6575 ThreadLimitVal = DefaultThreadLimitVal;
6576
6577 return ThreadLimitVal;
6578 }
Arpith Chacko Jacob86f9e462017-01-25 01:45:59 +00006579
Samuel Antaob68e2db2016-03-03 16:20:23 +00006580 // If the current target region has a teams region enclosed, we need to get
6581 // the thread limit to pass to the runtime function call. This is done
6582 // by generating the expression in a inlined region. This is required because
6583 // the expression is captured in the enclosing target environment when the
6584 // teams directive is not combined with target.
6585
Alexey Bataev475a7442018-01-12 19:39:11 +00006586 const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target);
Samuel Antaob68e2db2016-03-03 16:20:23 +00006587
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006588 if (const auto *TeamsDir = dyn_cast_or_null<OMPExecutableDirective>(
Carlo Bertolli6eee9062016-04-29 01:37:30 +00006589 ignoreCompoundStmts(CS.getCapturedStmt()))) {
Alexey Bataev50a1c782017-12-01 21:31:08 +00006590 if (isOpenMPTeamsDirective(TeamsDir->getDirectiveKind())) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006591 if (const auto *TLE = TeamsDir->getSingleClause<OMPThreadLimitClause>()) {
Alexey Bataev50a1c782017-12-01 21:31:08 +00006592 CGOpenMPInnerExprInfo CGInfo(CGF, CS);
6593 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
6594 llvm::Value *ThreadLimit = CGF.EmitScalarExpr(TLE->getThreadLimit());
6595 return CGF.Builder.CreateIntCast(ThreadLimit, CGF.Int32Ty,
6596 /*IsSigned=*/true);
6597 }
Samuel Antaob68e2db2016-03-03 16:20:23 +00006598
Alexey Bataev50a1c782017-12-01 21:31:08 +00006599 // If we have an enclosed teams directive but no thread_limit clause we
6600 // use the default value 0.
6601 return CGF.Builder.getInt32(0);
6602 }
Samuel Antaob68e2db2016-03-03 16:20:23 +00006603 }
6604
6605 // No teams associated with the directive.
6606 return nullptr;
6607}
6608
Samuel Antao86ace552016-04-27 22:40:57 +00006609namespace {
Alexey Bataevb3638132018-07-19 16:34:13 +00006610LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
6611
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006612// Utility to handle information from clauses associated with a given
Samuel Antao86ace552016-04-27 22:40:57 +00006613// construct that use mappable expressions (e.g. 'map' clause, 'to' clause).
6614// It provides a convenient interface to obtain the information and generate
6615// code for that information.
6616class MappableExprsHandler {
6617public:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006618 /// Values for bit flags used to specify the mapping type for
Samuel Antao86ace552016-04-27 22:40:57 +00006619 /// offloading.
Alexey Bataevb3638132018-07-19 16:34:13 +00006620 enum OpenMPOffloadMappingFlags : uint64_t {
6621 /// No flags
6622 OMP_MAP_NONE = 0x0,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006623 /// Allocate memory on the device and move data from host to device.
Samuel Antao86ace552016-04-27 22:40:57 +00006624 OMP_MAP_TO = 0x01,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006625 /// Allocate memory on the device and move data from device to host.
Samuel Antao86ace552016-04-27 22:40:57 +00006626 OMP_MAP_FROM = 0x02,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006627 /// Always perform the requested mapping action on the element, even
Samuel Antao86ace552016-04-27 22:40:57 +00006628 /// if it was already mapped before.
6629 OMP_MAP_ALWAYS = 0x04,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006630 /// Delete the element from the device environment, ignoring the
Samuel Antao86ace552016-04-27 22:40:57 +00006631 /// current reference count associated with the element.
Samuel Antao6782e942016-05-26 16:48:10 +00006632 OMP_MAP_DELETE = 0x08,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006633 /// The element being mapped is a pointer-pointee pair; both the
George Rokos065755d2017-11-07 18:27:04 +00006634 /// pointer and the pointee should be mapped.
6635 OMP_MAP_PTR_AND_OBJ = 0x10,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006636 /// This flags signals that the base address of an entry should be
George Rokos065755d2017-11-07 18:27:04 +00006637 /// passed to the target kernel as an argument.
6638 OMP_MAP_TARGET_PARAM = 0x20,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006639 /// Signal that the runtime library has to return the device pointer
George Rokos065755d2017-11-07 18:27:04 +00006640 /// in the current position for the data being mapped. Used when we have the
6641 /// use_device_ptr clause.
6642 OMP_MAP_RETURN_PARAM = 0x40,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006643 /// This flag signals that the reference being passed is a pointer to
Samuel Antaod486f842016-05-26 16:53:38 +00006644 /// private data.
George Rokos065755d2017-11-07 18:27:04 +00006645 OMP_MAP_PRIVATE = 0x80,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006646 /// Pass the element to the device by value.
George Rokos065755d2017-11-07 18:27:04 +00006647 OMP_MAP_LITERAL = 0x100,
Alexey Bataevf47c4b42017-09-26 13:47:31 +00006648 /// Implicit map
6649 OMP_MAP_IMPLICIT = 0x200,
Alexey Bataevb3638132018-07-19 16:34:13 +00006650 /// The 16 MSBs of the flags indicate whether the entry is member of some
6651 /// struct/class.
6652 OMP_MAP_MEMBER_OF = 0xffff000000000000,
6653 LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ OMP_MAP_MEMBER_OF),
Samuel Antao86ace552016-04-27 22:40:57 +00006654 };
6655
Samuel Antaocc10b852016-07-28 14:23:26 +00006656 /// Class that associates information with a base pointer to be passed to the
6657 /// runtime library.
6658 class BasePointerInfo {
6659 /// The base pointer.
6660 llvm::Value *Ptr = nullptr;
6661 /// The base declaration that refers to this device pointer, or null if
6662 /// there is none.
6663 const ValueDecl *DevPtrDecl = nullptr;
6664
6665 public:
6666 BasePointerInfo(llvm::Value *Ptr, const ValueDecl *DevPtrDecl = nullptr)
6667 : Ptr(Ptr), DevPtrDecl(DevPtrDecl) {}
6668 llvm::Value *operator*() const { return Ptr; }
6669 const ValueDecl *getDevicePtrDecl() const { return DevPtrDecl; }
6670 void setDevicePtrDecl(const ValueDecl *D) { DevPtrDecl = D; }
6671 };
6672
Alexey Bataevb3638132018-07-19 16:34:13 +00006673 using MapBaseValuesArrayTy = SmallVector<BasePointerInfo, 4>;
6674 using MapValuesArrayTy = SmallVector<llvm::Value *, 4>;
6675 using MapFlagsArrayTy = SmallVector<OpenMPOffloadMappingFlags, 4>;
6676
6677 /// Map between a struct and the its lowest & highest elements which have been
6678 /// mapped.
6679 /// [ValueDecl *] --> {LE(FieldIndex, Pointer),
6680 /// HE(FieldIndex, Pointer)}
6681 struct StructRangeInfoTy {
6682 std::pair<unsigned /*FieldIndex*/, Address /*Pointer*/> LowestElem = {
6683 0, Address::invalid()};
6684 std::pair<unsigned /*FieldIndex*/, Address /*Pointer*/> HighestElem = {
6685 0, Address::invalid()};
6686 Address Base = Address::invalid();
6687 };
Samuel Antao86ace552016-04-27 22:40:57 +00006688
6689private:
Alexey Bataevb3638132018-07-19 16:34:13 +00006690 /// Kind that defines how a device pointer has to be returned.
6691 struct MapInfo {
6692 OMPClauseMappableExprCommon::MappableExprComponentListRef Components;
6693 OpenMPMapClauseKind MapType = OMPC_MAP_unknown;
Kelvin Lief579432018-12-18 22:18:41 +00006694 ArrayRef<OpenMPMapModifierKind> MapModifiers;
Alexey Bataevb3638132018-07-19 16:34:13 +00006695 bool ReturnDevicePointer = false;
6696 bool IsImplicit = false;
6697
6698 MapInfo() = default;
6699 MapInfo(
6700 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
Kelvin Lief579432018-12-18 22:18:41 +00006701 OpenMPMapClauseKind MapType,
6702 ArrayRef<OpenMPMapModifierKind> MapModifiers,
Alexey Bataevb3638132018-07-19 16:34:13 +00006703 bool ReturnDevicePointer, bool IsImplicit)
Kelvin Lief579432018-12-18 22:18:41 +00006704 : Components(Components), MapType(MapType), MapModifiers(MapModifiers),
Alexey Bataevb3638132018-07-19 16:34:13 +00006705 ReturnDevicePointer(ReturnDevicePointer), IsImplicit(IsImplicit) {}
6706 };
6707
6708 /// If use_device_ptr is used on a pointer which is a struct member and there
6709 /// is no map information about it, then emission of that entry is deferred
6710 /// until the whole struct has been processed.
6711 struct DeferredDevicePtrEntryTy {
6712 const Expr *IE = nullptr;
6713 const ValueDecl *VD = nullptr;
6714
6715 DeferredDevicePtrEntryTy(const Expr *IE, const ValueDecl *VD)
6716 : IE(IE), VD(VD) {}
6717 };
6718
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006719 /// Directive from where the map clauses were extracted.
Samuel Antao44bcdb32016-07-28 15:31:29 +00006720 const OMPExecutableDirective &CurDir;
Samuel Antao86ace552016-04-27 22:40:57 +00006721
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006722 /// Function the directive is being generated for.
Samuel Antao86ace552016-04-27 22:40:57 +00006723 CodeGenFunction &CGF;
6724
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006725 /// Set of all first private variables in the current directive.
Samuel Antaod486f842016-05-26 16:53:38 +00006726 llvm::SmallPtrSet<const VarDecl *, 8> FirstPrivateDecls;
6727
Samuel Antao6890b092016-07-28 14:25:09 +00006728 /// Map between device pointer declarations and their expression components.
6729 /// The key value for declarations in 'this' is null.
6730 llvm::DenseMap<
6731 const ValueDecl *,
6732 SmallVector<OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>>
6733 DevPointersMap;
6734
Samuel Antao86ace552016-04-27 22:40:57 +00006735 llvm::Value *getExprTypeSize(const Expr *E) const {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006736 QualType ExprTy = E->getType().getCanonicalType();
Samuel Antao86ace552016-04-27 22:40:57 +00006737
6738 // Reference types are ignored for mapping purposes.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006739 if (const auto *RefTy = ExprTy->getAs<ReferenceType>())
Samuel Antao86ace552016-04-27 22:40:57 +00006740 ExprTy = RefTy->getPointeeType().getCanonicalType();
6741
6742 // Given that an array section is considered a built-in type, we need to
6743 // do the calculation based on the length of the section instead of relying
6744 // on CGF.getTypeSize(E->getType()).
6745 if (const auto *OAE = dyn_cast<OMPArraySectionExpr>(E)) {
6746 QualType BaseTy = OMPArraySectionExpr::getBaseOriginalType(
6747 OAE->getBase()->IgnoreParenImpCasts())
6748 .getCanonicalType();
6749
6750 // If there is no length associated with the expression, that means we
6751 // are using the whole length of the base.
6752 if (!OAE->getLength() && OAE->getColonLoc().isValid())
6753 return CGF.getTypeSize(BaseTy);
6754
6755 llvm::Value *ElemSize;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006756 if (const auto *PTy = BaseTy->getAs<PointerType>()) {
Samuel Antao86ace552016-04-27 22:40:57 +00006757 ElemSize = CGF.getTypeSize(PTy->getPointeeType().getCanonicalType());
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006758 } else {
6759 const auto *ATy = cast<ArrayType>(BaseTy.getTypePtr());
Samuel Antao86ace552016-04-27 22:40:57 +00006760 assert(ATy && "Expecting array type if not a pointer type.");
6761 ElemSize = CGF.getTypeSize(ATy->getElementType().getCanonicalType());
6762 }
6763
6764 // If we don't have a length at this point, that is because we have an
6765 // array section with a single element.
6766 if (!OAE->getLength())
6767 return ElemSize;
6768
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006769 llvm::Value *LengthVal = CGF.EmitScalarExpr(OAE->getLength());
Samuel Antao86ace552016-04-27 22:40:57 +00006770 LengthVal =
6771 CGF.Builder.CreateIntCast(LengthVal, CGF.SizeTy, /*isSigned=*/false);
6772 return CGF.Builder.CreateNUWMul(LengthVal, ElemSize);
6773 }
6774 return CGF.getTypeSize(ExprTy);
6775 }
6776
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006777 /// Return the corresponding bits for a given map clause modifier. Add
Samuel Antao86ace552016-04-27 22:40:57 +00006778 /// a flag marking the map as a pointer if requested. Add a flag marking the
Samuel Antao6782e942016-05-26 16:48:10 +00006779 /// map as the first one of a series of maps that relate to the same map
6780 /// expression.
Kelvin Lief579432018-12-18 22:18:41 +00006781 OpenMPOffloadMappingFlags getMapTypeBits(
6782 OpenMPMapClauseKind MapType, ArrayRef<OpenMPMapModifierKind> MapModifiers,
6783 bool IsImplicit, bool AddPtrFlag, bool AddIsTargetParamFlag) const {
Alexey Bataevb3638132018-07-19 16:34:13 +00006784 OpenMPOffloadMappingFlags Bits =
6785 IsImplicit ? OMP_MAP_IMPLICIT : OMP_MAP_NONE;
Samuel Antao86ace552016-04-27 22:40:57 +00006786 switch (MapType) {
6787 case OMPC_MAP_alloc:
Samuel Antao6782e942016-05-26 16:48:10 +00006788 case OMPC_MAP_release:
6789 // alloc and release is the default behavior in the runtime library, i.e.
6790 // if we don't pass any bits alloc/release that is what the runtime is
6791 // going to do. Therefore, we don't need to signal anything for these two
6792 // type modifiers.
Samuel Antao86ace552016-04-27 22:40:57 +00006793 break;
6794 case OMPC_MAP_to:
Alexey Bataevb3638132018-07-19 16:34:13 +00006795 Bits |= OMP_MAP_TO;
Samuel Antao86ace552016-04-27 22:40:57 +00006796 break;
6797 case OMPC_MAP_from:
Alexey Bataevb3638132018-07-19 16:34:13 +00006798 Bits |= OMP_MAP_FROM;
Samuel Antao86ace552016-04-27 22:40:57 +00006799 break;
6800 case OMPC_MAP_tofrom:
Alexey Bataevb3638132018-07-19 16:34:13 +00006801 Bits |= OMP_MAP_TO | OMP_MAP_FROM;
Samuel Antao86ace552016-04-27 22:40:57 +00006802 break;
6803 case OMPC_MAP_delete:
Alexey Bataevb3638132018-07-19 16:34:13 +00006804 Bits |= OMP_MAP_DELETE;
Samuel Antao86ace552016-04-27 22:40:57 +00006805 break;
Alexey Bataevb3638132018-07-19 16:34:13 +00006806 case OMPC_MAP_unknown:
Samuel Antao86ace552016-04-27 22:40:57 +00006807 llvm_unreachable("Unexpected map type!");
Samuel Antao86ace552016-04-27 22:40:57 +00006808 }
6809 if (AddPtrFlag)
George Rokos065755d2017-11-07 18:27:04 +00006810 Bits |= OMP_MAP_PTR_AND_OBJ;
6811 if (AddIsTargetParamFlag)
6812 Bits |= OMP_MAP_TARGET_PARAM;
Kelvin Lief579432018-12-18 22:18:41 +00006813 if (llvm::find(MapModifiers, OMPC_MAP_MODIFIER_always)
6814 != MapModifiers.end())
Samuel Antao86ace552016-04-27 22:40:57 +00006815 Bits |= OMP_MAP_ALWAYS;
6816 return Bits;
6817 }
6818
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006819 /// Return true if the provided expression is a final array section. A
Samuel Antao86ace552016-04-27 22:40:57 +00006820 /// final array section, is one whose length can't be proved to be one.
6821 bool isFinalArraySectionExpression(const Expr *E) const {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006822 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antao86ace552016-04-27 22:40:57 +00006823
6824 // It is not an array section and therefore not a unity-size one.
6825 if (!OASE)
6826 return false;
6827
6828 // An array section with no colon always refer to a single element.
6829 if (OASE->getColonLoc().isInvalid())
6830 return false;
6831
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006832 const Expr *Length = OASE->getLength();
Samuel Antao86ace552016-04-27 22:40:57 +00006833
6834 // If we don't have a length we have to check if the array has size 1
6835 // for this dimension. Also, we should always expect a length if the
6836 // base type is pointer.
6837 if (!Length) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00006838 QualType BaseQTy = OMPArraySectionExpr::getBaseOriginalType(
6839 OASE->getBase()->IgnoreParenImpCasts())
6840 .getCanonicalType();
6841 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antao86ace552016-04-27 22:40:57 +00006842 return ATy->getSize().getSExtValue() != 1;
6843 // If we don't have a constant dimension length, we have to consider
6844 // the current section as having any size, so it is not necessarily
6845 // unitary. If it happen to be unity size, that's user fault.
6846 return true;
6847 }
6848
6849 // Check if the length evaluates to 1.
Fangrui Song407659a2018-11-30 23:41:18 +00006850 Expr::EvalResult Result;
6851 if (!Length->EvaluateAsInt(Result, CGF.getContext()))
Samuel Antao86ace552016-04-27 22:40:57 +00006852 return true; // Can have more that size 1.
6853
Fangrui Song407659a2018-11-30 23:41:18 +00006854 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antao86ace552016-04-27 22:40:57 +00006855 return ConstLength.getSExtValue() != 1;
6856 }
6857
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006858 /// Generate the base pointers, section pointers, sizes and map type
Samuel Antao86ace552016-04-27 22:40:57 +00006859 /// bits for the provided map type, map modifier, and expression components.
6860 /// \a IsFirstComponent should be set to true if the provided set of
6861 /// components is the first associated with a capture.
6862 void generateInfoForComponentList(
Kelvin Lief579432018-12-18 22:18:41 +00006863 OpenMPMapClauseKind MapType,
6864 ArrayRef<OpenMPMapModifierKind> MapModifiers,
Samuel Antao86ace552016-04-27 22:40:57 +00006865 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
Samuel Antaocc10b852016-07-28 14:23:26 +00006866 MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers,
Samuel Antao86ace552016-04-27 22:40:57 +00006867 MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types,
Alexey Bataevb3638132018-07-19 16:34:13 +00006868 StructRangeInfoTy &PartialStruct, bool IsFirstComponentList,
Alexey Bataeve82445f2018-09-20 13:54:02 +00006869 bool IsImplicit,
6870 ArrayRef<OMPClauseMappableExprCommon::MappableExprComponentListRef>
6871 OverlappedElements = llvm::None) const {
Samuel Antao86ace552016-04-27 22:40:57 +00006872 // The following summarizes what has to be generated for each map and the
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00006873 // types below. The generated information is expressed in this order:
Samuel Antao86ace552016-04-27 22:40:57 +00006874 // base pointer, section pointer, size, flags
6875 // (to add to the ones that come from the map type and modifier).
6876 //
6877 // double d;
6878 // int i[100];
6879 // float *p;
6880 //
6881 // struct S1 {
6882 // int i;
6883 // float f[50];
6884 // }
6885 // struct S2 {
6886 // int i;
6887 // float f[50];
6888 // S1 s;
6889 // double *p;
6890 // struct S2 *ps;
6891 // }
6892 // S2 s;
6893 // S2 *ps;
6894 //
6895 // map(d)
Alexey Bataevb3638132018-07-19 16:34:13 +00006896 // &d, &d, sizeof(double), TARGET_PARAM | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006897 //
6898 // map(i)
Alexey Bataevb3638132018-07-19 16:34:13 +00006899 // &i, &i, 100*sizeof(int), TARGET_PARAM | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006900 //
6901 // map(i[1:23])
Alexey Bataevb3638132018-07-19 16:34:13 +00006902 // &i(=&i[0]), &i[1], 23*sizeof(int), TARGET_PARAM | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006903 //
6904 // map(p)
Alexey Bataevb3638132018-07-19 16:34:13 +00006905 // &p, &p, sizeof(float*), TARGET_PARAM | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006906 //
6907 // map(p[1:24])
Alexey Bataevb3638132018-07-19 16:34:13 +00006908 // p, &p[1], 24*sizeof(float), TARGET_PARAM | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006909 //
6910 // map(s)
Alexey Bataevb3638132018-07-19 16:34:13 +00006911 // &s, &s, sizeof(S2), TARGET_PARAM | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006912 //
6913 // map(s.i)
Alexey Bataevb3638132018-07-19 16:34:13 +00006914 // &s, &(s.i), sizeof(int), TARGET_PARAM | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006915 //
6916 // map(s.s.f)
Alexey Bataevb3638132018-07-19 16:34:13 +00006917 // &s, &(s.s.f[0]), 50*sizeof(float), TARGET_PARAM | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006918 //
6919 // map(s.p)
Alexey Bataevb3638132018-07-19 16:34:13 +00006920 // &s, &(s.p), sizeof(double*), TARGET_PARAM | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006921 //
Alexey Bataevb3638132018-07-19 16:34:13 +00006922 // map(to: s.p[:22])
6923 // &s, &(s.p), sizeof(double*), TARGET_PARAM (*)
6924 // &s, &(s.p), sizeof(double*), MEMBER_OF(1) (**)
6925 // &(s.p), &(s.p[0]), 22*sizeof(double),
6926 // MEMBER_OF(1) | PTR_AND_OBJ | TO (***)
6927 // (*) alloc space for struct members, only this is a target parameter
6928 // (**) map the pointer (nothing to be mapped in this example) (the compiler
6929 // optimizes this entry out, same in the examples below)
6930 // (***) map the pointee (map: to)
Samuel Antao86ace552016-04-27 22:40:57 +00006931 //
6932 // map(s.ps)
Alexey Bataevb3638132018-07-19 16:34:13 +00006933 // &s, &(s.ps), sizeof(S2*), TARGET_PARAM | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006934 //
Alexey Bataevb3638132018-07-19 16:34:13 +00006935 // map(from: s.ps->s.i)
6936 // &s, &(s.ps), sizeof(S2*), TARGET_PARAM
6937 // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1)
6938 // &(s.ps), &(s.ps->s.i), sizeof(int), MEMBER_OF(1) | PTR_AND_OBJ | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006939 //
Alexey Bataevb3638132018-07-19 16:34:13 +00006940 // map(to: s.ps->ps)
6941 // &s, &(s.ps), sizeof(S2*), TARGET_PARAM
6942 // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1)
6943 // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ | TO
Samuel Antao86ace552016-04-27 22:40:57 +00006944 //
6945 // map(s.ps->ps->ps)
Alexey Bataevb3638132018-07-19 16:34:13 +00006946 // &s, &(s.ps), sizeof(S2*), TARGET_PARAM
6947 // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1)
6948 // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ
6949 // &(s.ps->ps), &(s.ps->ps->ps), sizeof(S2*), PTR_AND_OBJ | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006950 //
Alexey Bataevb3638132018-07-19 16:34:13 +00006951 // map(to: s.ps->ps->s.f[:22])
6952 // &s, &(s.ps), sizeof(S2*), TARGET_PARAM
6953 // &s, &(s.ps), sizeof(S2*), MEMBER_OF(1)
6954 // &(s.ps), &(s.ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ
6955 // &(s.ps->ps), &(s.ps->ps->s.f[0]), 22*sizeof(float), PTR_AND_OBJ | TO
Samuel Antao86ace552016-04-27 22:40:57 +00006956 //
6957 // map(ps)
Alexey Bataevb3638132018-07-19 16:34:13 +00006958 // &ps, &ps, sizeof(S2*), TARGET_PARAM | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006959 //
6960 // map(ps->i)
Alexey Bataevb3638132018-07-19 16:34:13 +00006961 // ps, &(ps->i), sizeof(int), TARGET_PARAM | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006962 //
6963 // map(ps->s.f)
Alexey Bataevb3638132018-07-19 16:34:13 +00006964 // ps, &(ps->s.f[0]), 50*sizeof(float), TARGET_PARAM | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006965 //
Alexey Bataevb3638132018-07-19 16:34:13 +00006966 // map(from: ps->p)
6967 // ps, &(ps->p), sizeof(double*), TARGET_PARAM | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006968 //
Alexey Bataevb3638132018-07-19 16:34:13 +00006969 // map(to: ps->p[:22])
6970 // ps, &(ps->p), sizeof(double*), TARGET_PARAM
6971 // ps, &(ps->p), sizeof(double*), MEMBER_OF(1)
6972 // &(ps->p), &(ps->p[0]), 22*sizeof(double), MEMBER_OF(1) | PTR_AND_OBJ | TO
Samuel Antao86ace552016-04-27 22:40:57 +00006973 //
6974 // map(ps->ps)
Alexey Bataevb3638132018-07-19 16:34:13 +00006975 // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006976 //
Alexey Bataevb3638132018-07-19 16:34:13 +00006977 // map(from: ps->ps->s.i)
6978 // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM
6979 // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1)
6980 // &(ps->ps), &(ps->ps->s.i), sizeof(int), MEMBER_OF(1) | PTR_AND_OBJ | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006981 //
Alexey Bataevb3638132018-07-19 16:34:13 +00006982 // map(from: ps->ps->ps)
6983 // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM
6984 // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1)
6985 // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006986 //
6987 // map(ps->ps->ps->ps)
Alexey Bataevb3638132018-07-19 16:34:13 +00006988 // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM
6989 // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1)
6990 // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ
6991 // &(ps->ps->ps), &(ps->ps->ps->ps), sizeof(S2*), PTR_AND_OBJ | TO | FROM
Samuel Antao86ace552016-04-27 22:40:57 +00006992 //
Alexey Bataevb3638132018-07-19 16:34:13 +00006993 // map(to: ps->ps->ps->s.f[:22])
6994 // ps, &(ps->ps), sizeof(S2*), TARGET_PARAM
6995 // ps, &(ps->ps), sizeof(S2*), MEMBER_OF(1)
6996 // &(ps->ps), &(ps->ps->ps), sizeof(S2*), MEMBER_OF(1) | PTR_AND_OBJ
6997 // &(ps->ps->ps), &(ps->ps->ps->s.f[0]), 22*sizeof(float), PTR_AND_OBJ | TO
6998 //
6999 // map(to: s.f[:22]) map(from: s.p[:33])
7000 // &s, &(s.f[0]), 50*sizeof(float) + sizeof(struct S1) +
7001 // sizeof(double*) (**), TARGET_PARAM
7002 // &s, &(s.f[0]), 22*sizeof(float), MEMBER_OF(1) | TO
7003 // &s, &(s.p), sizeof(double*), MEMBER_OF(1)
7004 // &(s.p), &(s.p[0]), 33*sizeof(double), MEMBER_OF(1) | PTR_AND_OBJ | FROM
7005 // (*) allocate contiguous space needed to fit all mapped members even if
7006 // we allocate space for members not mapped (in this example,
7007 // s.f[22..49] and s.s are not mapped, yet we must allocate space for
7008 // them as well because they fall between &s.f[0] and &s.p)
7009 //
7010 // map(from: s.f[:22]) map(to: ps->p[:33])
7011 // &s, &(s.f[0]), 22*sizeof(float), TARGET_PARAM | FROM
7012 // ps, &(ps->p), sizeof(S2*), TARGET_PARAM
7013 // ps, &(ps->p), sizeof(double*), MEMBER_OF(2) (*)
7014 // &(ps->p), &(ps->p[0]), 33*sizeof(double), MEMBER_OF(2) | PTR_AND_OBJ | TO
7015 // (*) the struct this entry pertains to is the 2nd element in the list of
7016 // arguments, hence MEMBER_OF(2)
7017 //
7018 // map(from: s.f[:22], s.s) map(to: ps->p[:33])
7019 // &s, &(s.f[0]), 50*sizeof(float) + sizeof(struct S1), TARGET_PARAM
7020 // &s, &(s.f[0]), 22*sizeof(float), MEMBER_OF(1) | FROM
7021 // &s, &(s.s), sizeof(struct S1), MEMBER_OF(1) | FROM
7022 // ps, &(ps->p), sizeof(S2*), TARGET_PARAM
7023 // ps, &(ps->p), sizeof(double*), MEMBER_OF(4) (*)
7024 // &(ps->p), &(ps->p[0]), 33*sizeof(double), MEMBER_OF(4) | PTR_AND_OBJ | TO
7025 // (*) the struct this entry pertains to is the 4th element in the list
7026 // of arguments, hence MEMBER_OF(4)
Samuel Antao86ace552016-04-27 22:40:57 +00007027
7028 // Track if the map information being generated is the first for a capture.
7029 bool IsCaptureFirstInfo = IsFirstComponentList;
Alexey Bataev92327c52018-03-26 16:40:55 +00007030 bool IsLink = false; // Is this variable a "declare target link"?
Samuel Antao86ace552016-04-27 22:40:57 +00007031
7032 // Scan the components from the base to the complete expression.
7033 auto CI = Components.rbegin();
7034 auto CE = Components.rend();
7035 auto I = CI;
7036
7037 // Track if the map information being generated is the first for a list of
7038 // components.
7039 bool IsExpressionFirstInfo = true;
Alexey Bataevb3638132018-07-19 16:34:13 +00007040 Address BP = Address::invalid();
Patrick Lystere13b1e32019-01-02 19:28:48 +00007041 const Expr *AssocExpr = I->getAssociatedExpression();
7042 const auto *AE = dyn_cast<ArraySubscriptExpr>(AssocExpr);
7043 const auto *OASE = dyn_cast<OMPArraySectionExpr>(AssocExpr);
Samuel Antao86ace552016-04-27 22:40:57 +00007044
Patrick Lystere13b1e32019-01-02 19:28:48 +00007045 if (isa<MemberExpr>(AssocExpr)) {
Samuel Antao86ace552016-04-27 22:40:57 +00007046 // The base is the 'this' pointer. The content of the pointer is going
7047 // to be the base of the field being mapped.
Alexey Bataevb3638132018-07-19 16:34:13 +00007048 BP = CGF.LoadCXXThisAddress();
Patrick Lystere13b1e32019-01-02 19:28:48 +00007049 } else if ((AE && isa<CXXThisExpr>(AE->getBase()->IgnoreParenImpCasts())) ||
7050 (OASE &&
7051 isa<CXXThisExpr>(OASE->getBase()->IgnoreParenImpCasts()))) {
7052 BP = CGF.EmitOMPSharedLValue(AssocExpr).getAddress();
Samuel Antao86ace552016-04-27 22:40:57 +00007053 } else {
7054 // The base is the reference to the variable.
7055 // BP = &Var.
Patrick Lystere13b1e32019-01-02 19:28:48 +00007056 BP = CGF.EmitOMPSharedLValue(AssocExpr).getAddress();
Alexey Bataev92327c52018-03-26 16:40:55 +00007057 if (const auto *VD =
7058 dyn_cast_or_null<VarDecl>(I->getAssociatedDeclaration())) {
7059 if (llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00007060 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataev2c1dffe2018-04-16 20:34:41 +00007061 if (*Res == OMPDeclareTargetDeclAttr::MT_Link) {
7062 IsLink = true;
Alexey Bataevb3638132018-07-19 16:34:13 +00007063 BP = CGF.CGM.getOpenMPRuntime().getAddrOfDeclareTargetLink(VD);
Alexey Bataev2c1dffe2018-04-16 20:34:41 +00007064 }
Alexey Bataev92327c52018-03-26 16:40:55 +00007065 }
Samuel Antao86ace552016-04-27 22:40:57 +00007066
7067 // If the variable is a pointer and is being dereferenced (i.e. is not
Nico Webera6916892016-06-10 18:53:04 +00007068 // the last component), the base has to be the pointer itself, not its
Samuel Antao403ffd42016-07-27 22:49:49 +00007069 // reference. References are ignored for mapping purposes.
7070 QualType Ty =
7071 I->getAssociatedDeclaration()->getType().getNonReferenceType();
7072 if (Ty->isAnyPointerType() && std::next(I) != CE) {
Alexey Bataevb3638132018-07-19 16:34:13 +00007073 BP = CGF.EmitLoadOfPointer(BP, Ty->castAs<PointerType>());
Samuel Antao86ace552016-04-27 22:40:57 +00007074
7075 // We do not need to generate individual map information for the
7076 // pointer, it can be associated with the combined storage.
7077 ++I;
7078 }
7079 }
7080
Alexey Bataevb3638132018-07-19 16:34:13 +00007081 // Track whether a component of the list should be marked as MEMBER_OF some
7082 // combined entry (for partial structs). Only the first PTR_AND_OBJ entry
7083 // in a component list should be marked as MEMBER_OF, all subsequent entries
7084 // do not belong to the base struct. E.g.
7085 // struct S2 s;
7086 // s.ps->ps->ps->f[:]
7087 // (1) (2) (3) (4)
7088 // ps(1) is a member pointer, ps(2) is a pointee of ps(1), so it is a
7089 // PTR_AND_OBJ entry; the PTR is ps(1), so MEMBER_OF the base struct. ps(3)
7090 // is the pointee of ps(2) which is not member of struct s, so it should not
7091 // be marked as such (it is still PTR_AND_OBJ).
7092 // The variable is initialized to false so that PTR_AND_OBJ entries which
7093 // are not struct members are not considered (e.g. array of pointers to
7094 // data).
7095 bool ShouldBeMemberOf = false;
7096
7097 // Variable keeping track of whether or not we have encountered a component
7098 // in the component list which is a member expression. Useful when we have a
7099 // pointer or a final array section, in which case it is the previous
7100 // component in the list which tells us whether we have a member expression.
7101 // E.g. X.f[:]
7102 // While processing the final array section "[:]" it is "f" which tells us
7103 // whether we are dealing with a member of a declared struct.
7104 const MemberExpr *EncounteredME = nullptr;
7105
Samuel Antao86ace552016-04-27 22:40:57 +00007106 for (; I != CE; ++I) {
Alexey Bataevb3638132018-07-19 16:34:13 +00007107 // If the current component is member of a struct (parent struct) mark it.
7108 if (!EncounteredME) {
7109 EncounteredME = dyn_cast<MemberExpr>(I->getAssociatedExpression());
7110 // If we encounter a PTR_AND_OBJ entry from now on it should be marked
7111 // as MEMBER_OF the parent struct.
7112 if (EncounteredME)
7113 ShouldBeMemberOf = true;
7114 }
7115
Samuel Antao86ace552016-04-27 22:40:57 +00007116 auto Next = std::next(I);
7117
7118 // We need to generate the addresses and sizes if this is the last
7119 // component, if the component is a pointer or if it is an array section
7120 // whose length can't be proved to be one. If this is a pointer, it
7121 // becomes the base address for the following components.
7122
7123 // A final array section, is one whose length can't be proved to be one.
7124 bool IsFinalArraySection =
7125 isFinalArraySectionExpression(I->getAssociatedExpression());
7126
7127 // Get information on whether the element is a pointer. Have to do a
7128 // special treatment for array sections given that they are built-in
7129 // types.
7130 const auto *OASE =
7131 dyn_cast<OMPArraySectionExpr>(I->getAssociatedExpression());
7132 bool IsPointer =
Alexey Bataevb3638132018-07-19 16:34:13 +00007133 (OASE && OMPArraySectionExpr::getBaseOriginalType(OASE)
7134 .getCanonicalType()
7135 ->isAnyPointerType()) ||
Samuel Antao86ace552016-04-27 22:40:57 +00007136 I->getAssociatedExpression()->getType()->isAnyPointerType();
7137
7138 if (Next == CE || IsPointer || IsFinalArraySection) {
Samuel Antao86ace552016-04-27 22:40:57 +00007139 // If this is not the last component, we expect the pointer to be
7140 // associated with an array expression or member expression.
7141 assert((Next == CE ||
7142 isa<MemberExpr>(Next->getAssociatedExpression()) ||
7143 isa<ArraySubscriptExpr>(Next->getAssociatedExpression()) ||
7144 isa<OMPArraySectionExpr>(Next->getAssociatedExpression())) &&
7145 "Unexpected expression");
7146
Alexey Bataevb3638132018-07-19 16:34:13 +00007147 Address LB =
7148 CGF.EmitOMPSharedLValue(I->getAssociatedExpression()).getAddress();
Samuel Antao86ace552016-04-27 22:40:57 +00007149
Alexey Bataevb3638132018-07-19 16:34:13 +00007150 // If this component is a pointer inside the base struct then we don't
7151 // need to create any entry for it - it will be combined with the object
7152 // it is pointing to into a single PTR_AND_OBJ entry.
7153 bool IsMemberPointer =
7154 IsPointer && EncounteredME &&
7155 (dyn_cast<MemberExpr>(I->getAssociatedExpression()) ==
7156 EncounteredME);
Alexey Bataeve82445f2018-09-20 13:54:02 +00007157 if (!OverlappedElements.empty()) {
7158 // Handle base element with the info for overlapped elements.
7159 assert(!PartialStruct.Base.isValid() && "The base element is set.");
7160 assert(Next == CE &&
7161 "Expected last element for the overlapped elements.");
7162 assert(!IsPointer &&
7163 "Unexpected base element with the pointer type.");
7164 // Mark the whole struct as the struct that requires allocation on the
7165 // device.
7166 PartialStruct.LowestElem = {0, LB};
7167 CharUnits TypeSize = CGF.getContext().getTypeSizeInChars(
7168 I->getAssociatedExpression()->getType());
7169 Address HB = CGF.Builder.CreateConstGEP(
7170 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(LB,
7171 CGF.VoidPtrTy),
7172 TypeSize.getQuantity() - 1, CharUnits::One());
7173 PartialStruct.HighestElem = {
7174 std::numeric_limits<decltype(
7175 PartialStruct.HighestElem.first)>::max(),
7176 HB};
7177 PartialStruct.Base = BP;
7178 // Emit data for non-overlapped data.
7179 OpenMPOffloadMappingFlags Flags =
7180 OMP_MAP_MEMBER_OF |
Kelvin Lief579432018-12-18 22:18:41 +00007181 getMapTypeBits(MapType, MapModifiers, IsImplicit,
Alexey Bataeve82445f2018-09-20 13:54:02 +00007182 /*AddPtrFlag=*/false,
7183 /*AddIsTargetParamFlag=*/false);
7184 LB = BP;
7185 llvm::Value *Size = nullptr;
7186 // Do bitcopy of all non-overlapped structure elements.
7187 for (OMPClauseMappableExprCommon::MappableExprComponentListRef
7188 Component : OverlappedElements) {
7189 Address ComponentLB = Address::invalid();
7190 for (const OMPClauseMappableExprCommon::MappableComponent &MC :
7191 Component) {
7192 if (MC.getAssociatedDeclaration()) {
7193 ComponentLB =
7194 CGF.EmitOMPSharedLValue(MC.getAssociatedExpression())
7195 .getAddress();
7196 Size = CGF.Builder.CreatePtrDiff(
7197 CGF.EmitCastToVoidPtr(ComponentLB.getPointer()),
7198 CGF.EmitCastToVoidPtr(LB.getPointer()));
7199 break;
7200 }
7201 }
7202 BasePointers.push_back(BP.getPointer());
7203 Pointers.push_back(LB.getPointer());
7204 Sizes.push_back(Size);
7205 Types.push_back(Flags);
7206 LB = CGF.Builder.CreateConstGEP(ComponentLB, 1,
7207 CGF.getPointerSize());
7208 }
7209 BasePointers.push_back(BP.getPointer());
7210 Pointers.push_back(LB.getPointer());
7211 Size = CGF.Builder.CreatePtrDiff(
7212 CGF.EmitCastToVoidPtr(
7213 CGF.Builder.CreateConstGEP(HB, 1, CharUnits::One())
7214 .getPointer()),
7215 CGF.EmitCastToVoidPtr(LB.getPointer()));
7216 Sizes.push_back(Size);
7217 Types.push_back(Flags);
7218 break;
7219 }
7220 llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression());
Alexey Bataevb3638132018-07-19 16:34:13 +00007221 if (!IsMemberPointer) {
7222 BasePointers.push_back(BP.getPointer());
7223 Pointers.push_back(LB.getPointer());
7224 Sizes.push_back(Size);
Samuel Antao03a3cec2016-07-27 22:52:16 +00007225
Alexey Bataevb3638132018-07-19 16:34:13 +00007226 // We need to add a pointer flag for each map that comes from the
7227 // same expression except for the first one. We also need to signal
7228 // this map is the first one that relates with the current capture
7229 // (there is a set of entries for each capture).
7230 OpenMPOffloadMappingFlags Flags = getMapTypeBits(
Kelvin Lief579432018-12-18 22:18:41 +00007231 MapType, MapModifiers, IsImplicit,
Alexey Bataevb3638132018-07-19 16:34:13 +00007232 !IsExpressionFirstInfo || IsLink, IsCaptureFirstInfo && !IsLink);
7233
7234 if (!IsExpressionFirstInfo) {
7235 // If we have a PTR_AND_OBJ pair where the OBJ is a pointer as well,
7236 // then we reset the TO/FROM/ALWAYS/DELETE flags.
7237 if (IsPointer)
7238 Flags &= ~(OMP_MAP_TO | OMP_MAP_FROM | OMP_MAP_ALWAYS |
7239 OMP_MAP_DELETE);
7240
7241 if (ShouldBeMemberOf) {
7242 // Set placeholder value MEMBER_OF=FFFF to indicate that the flag
7243 // should be later updated with the correct value of MEMBER_OF.
7244 Flags |= OMP_MAP_MEMBER_OF;
7245 // From now on, all subsequent PTR_AND_OBJ entries should not be
7246 // marked as MEMBER_OF.
7247 ShouldBeMemberOf = false;
7248 }
7249 }
7250
7251 Types.push_back(Flags);
Samuel Antao03a3cec2016-07-27 22:52:16 +00007252 }
7253
Alexey Bataevb3638132018-07-19 16:34:13 +00007254 // If we have encountered a member expression so far, keep track of the
7255 // mapped member. If the parent is "*this", then the value declaration
7256 // is nullptr.
7257 if (EncounteredME) {
7258 const auto *FD = dyn_cast<FieldDecl>(EncounteredME->getMemberDecl());
7259 unsigned FieldIndex = FD->getFieldIndex();
Samuel Antao03a3cec2016-07-27 22:52:16 +00007260
Alexey Bataevb3638132018-07-19 16:34:13 +00007261 // Update info about the lowest and highest elements for this struct
7262 if (!PartialStruct.Base.isValid()) {
7263 PartialStruct.LowestElem = {FieldIndex, LB};
7264 PartialStruct.HighestElem = {FieldIndex, LB};
7265 PartialStruct.Base = BP;
7266 } else if (FieldIndex < PartialStruct.LowestElem.first) {
7267 PartialStruct.LowestElem = {FieldIndex, LB};
7268 } else if (FieldIndex > PartialStruct.HighestElem.first) {
7269 PartialStruct.HighestElem = {FieldIndex, LB};
7270 }
7271 }
Samuel Antao86ace552016-04-27 22:40:57 +00007272
7273 // If we have a final array section, we are done with this expression.
7274 if (IsFinalArraySection)
7275 break;
7276
7277 // The pointer becomes the base for the next element.
7278 if (Next != CE)
7279 BP = LB;
7280
7281 IsExpressionFirstInfo = false;
7282 IsCaptureFirstInfo = false;
Samuel Antao86ace552016-04-27 22:40:57 +00007283 }
7284 }
7285 }
7286
Alexey Bataevb3638132018-07-19 16:34:13 +00007287 /// Return the adjusted map modifiers if the declaration a capture refers to
7288 /// appears in a first-private clause. This is expected to be used only with
7289 /// directives that start with 'target'.
7290 MappableExprsHandler::OpenMPOffloadMappingFlags
7291 getMapModifiersForPrivateClauses(const CapturedStmt::Capture &Cap) const {
7292 assert(Cap.capturesVariable() && "Expected capture by reference only!");
7293
7294 // A first private variable captured by reference will use only the
7295 // 'private ptr' and 'map to' flag. Return the right flags if the captured
7296 // declaration is known as first-private in this handler.
7297 if (FirstPrivateDecls.count(Cap.getCapturedVar()))
7298 return MappableExprsHandler::OMP_MAP_PRIVATE |
7299 MappableExprsHandler::OMP_MAP_TO;
7300 return MappableExprsHandler::OMP_MAP_TO |
7301 MappableExprsHandler::OMP_MAP_FROM;
7302 }
7303
7304 static OpenMPOffloadMappingFlags getMemberOfFlag(unsigned Position) {
7305 // Member of is given by the 16 MSB of the flag, so rotate by 48 bits.
7306 return static_cast<OpenMPOffloadMappingFlags>(((uint64_t)Position + 1)
7307 << 48);
7308 }
7309
7310 static void setCorrectMemberOfFlag(OpenMPOffloadMappingFlags &Flags,
7311 OpenMPOffloadMappingFlags MemberOfFlag) {
7312 // If the entry is PTR_AND_OBJ but has not been marked with the special
7313 // placeholder value 0xFFFF in the MEMBER_OF field, then it should not be
7314 // marked as MEMBER_OF.
7315 if ((Flags & OMP_MAP_PTR_AND_OBJ) &&
7316 ((Flags & OMP_MAP_MEMBER_OF) != OMP_MAP_MEMBER_OF))
7317 return;
7318
7319 // Reset the placeholder value to prepare the flag for the assignment of the
7320 // proper MEMBER_OF value.
7321 Flags &= ~OMP_MAP_MEMBER_OF;
7322 Flags |= MemberOfFlag;
7323 }
7324
Alexey Bataeve82445f2018-09-20 13:54:02 +00007325 void getPlainLayout(const CXXRecordDecl *RD,
7326 llvm::SmallVectorImpl<const FieldDecl *> &Layout,
7327 bool AsBase) const {
7328 const CGRecordLayout &RL = CGF.getTypes().getCGRecordLayout(RD);
7329
7330 llvm::StructType *St =
7331 AsBase ? RL.getBaseSubobjectLLVMType() : RL.getLLVMType();
7332
7333 unsigned NumElements = St->getNumElements();
7334 llvm::SmallVector<
7335 llvm::PointerUnion<const CXXRecordDecl *, const FieldDecl *>, 4>
7336 RecordLayout(NumElements);
7337
7338 // Fill bases.
7339 for (const auto &I : RD->bases()) {
7340 if (I.isVirtual())
7341 continue;
7342 const auto *Base = I.getType()->getAsCXXRecordDecl();
7343 // Ignore empty bases.
7344 if (Base->isEmpty() || CGF.getContext()
7345 .getASTRecordLayout(Base)
7346 .getNonVirtualSize()
7347 .isZero())
7348 continue;
7349
7350 unsigned FieldIndex = RL.getNonVirtualBaseLLVMFieldNo(Base);
7351 RecordLayout[FieldIndex] = Base;
7352 }
7353 // Fill in virtual bases.
7354 for (const auto &I : RD->vbases()) {
7355 const auto *Base = I.getType()->getAsCXXRecordDecl();
7356 // Ignore empty bases.
7357 if (Base->isEmpty())
7358 continue;
7359 unsigned FieldIndex = RL.getVirtualBaseIndex(Base);
7360 if (RecordLayout[FieldIndex])
7361 continue;
7362 RecordLayout[FieldIndex] = Base;
7363 }
7364 // Fill in all the fields.
7365 assert(!RD->isUnion() && "Unexpected union.");
7366 for (const auto *Field : RD->fields()) {
7367 // Fill in non-bitfields. (Bitfields always use a zero pattern, which we
7368 // will fill in later.)
7369 if (!Field->isBitField()) {
7370 unsigned FieldIndex = RL.getLLVMFieldNo(Field);
7371 RecordLayout[FieldIndex] = Field;
7372 }
7373 }
7374 for (const llvm::PointerUnion<const CXXRecordDecl *, const FieldDecl *>
7375 &Data : RecordLayout) {
7376 if (Data.isNull())
7377 continue;
7378 if (const auto *Base = Data.dyn_cast<const CXXRecordDecl *>())
7379 getPlainLayout(Base, Layout, /*AsBase=*/true);
7380 else
7381 Layout.push_back(Data.get<const FieldDecl *>());
7382 }
7383 }
7384
Alexey Bataevb3638132018-07-19 16:34:13 +00007385public:
7386 MappableExprsHandler(const OMPExecutableDirective &Dir, CodeGenFunction &CGF)
7387 : CurDir(Dir), CGF(CGF) {
7388 // Extract firstprivate clause information.
7389 for (const auto *C : Dir.getClausesOfKind<OMPFirstprivateClause>())
7390 for (const auto *D : C->varlists())
7391 FirstPrivateDecls.insert(
7392 cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl());
7393 // Extract device pointer clause information.
7394 for (const auto *C : Dir.getClausesOfKind<OMPIsDevicePtrClause>())
7395 for (auto L : C->component_lists())
7396 DevPointersMap[L.first].push_back(L.second);
7397 }
7398
7399 /// Generate code for the combined entry if we have a partially mapped struct
7400 /// and take care of the mapping flags of the arguments corresponding to
7401 /// individual struct members.
7402 void emitCombinedEntry(MapBaseValuesArrayTy &BasePointers,
7403 MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes,
7404 MapFlagsArrayTy &Types, MapFlagsArrayTy &CurTypes,
7405 const StructRangeInfoTy &PartialStruct) const {
7406 // Base is the base of the struct
7407 BasePointers.push_back(PartialStruct.Base.getPointer());
7408 // Pointer is the address of the lowest element
7409 llvm::Value *LB = PartialStruct.LowestElem.second.getPointer();
7410 Pointers.push_back(LB);
7411 // Size is (addr of {highest+1} element) - (addr of lowest element)
7412 llvm::Value *HB = PartialStruct.HighestElem.second.getPointer();
7413 llvm::Value *HAddr = CGF.Builder.CreateConstGEP1_32(HB, /*Idx0=*/1);
7414 llvm::Value *CLAddr = CGF.Builder.CreatePointerCast(LB, CGF.VoidPtrTy);
7415 llvm::Value *CHAddr = CGF.Builder.CreatePointerCast(HAddr, CGF.VoidPtrTy);
7416 llvm::Value *Diff = CGF.Builder.CreatePtrDiff(CHAddr, CLAddr);
7417 llvm::Value *Size = CGF.Builder.CreateIntCast(Diff, CGF.SizeTy,
7418 /*isSinged=*/false);
7419 Sizes.push_back(Size);
7420 // Map type is always TARGET_PARAM
7421 Types.push_back(OMP_MAP_TARGET_PARAM);
7422 // Remove TARGET_PARAM flag from the first element
7423 (*CurTypes.begin()) &= ~OMP_MAP_TARGET_PARAM;
7424
7425 // All other current entries will be MEMBER_OF the combined entry
7426 // (except for PTR_AND_OBJ entries which do not have a placeholder value
7427 // 0xFFFF in the MEMBER_OF field).
7428 OpenMPOffloadMappingFlags MemberOfFlag =
7429 getMemberOfFlag(BasePointers.size() - 1);
7430 for (auto &M : CurTypes)
7431 setCorrectMemberOfFlag(M, MemberOfFlag);
7432 }
7433
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007434 /// Generate all the base pointers, section pointers, sizes and map
Samuel Antaocc10b852016-07-28 14:23:26 +00007435 /// types for the extracted mappable expressions. Also, for each item that
7436 /// relates with a device pointer, a pair of the relevant declaration and
7437 /// index where it occurs is appended to the device pointers info array.
7438 void generateAllInfo(MapBaseValuesArrayTy &BasePointers,
Samuel Antao86ace552016-04-27 22:40:57 +00007439 MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes,
7440 MapFlagsArrayTy &Types) const {
Samuel Antao86ace552016-04-27 22:40:57 +00007441 // We have to process the component lists that relate with the same
7442 // declaration in a single chunk so that we can generate the map flags
7443 // correctly. Therefore, we organize all lists in a map.
Alexey Bataev5d1c3f62017-06-27 15:46:42 +00007444 llvm::MapVector<const ValueDecl *, SmallVector<MapInfo, 8>> Info;
Samuel Antao8d2d7302016-05-26 18:30:22 +00007445
7446 // Helper function to fill the information map for the different supported
7447 // clauses.
Samuel Antaocc10b852016-07-28 14:23:26 +00007448 auto &&InfoGen = [&Info](
7449 const ValueDecl *D,
7450 OMPClauseMappableExprCommon::MappableExprComponentListRef L,
Kelvin Lief579432018-12-18 22:18:41 +00007451 OpenMPMapClauseKind MapType,
7452 ArrayRef<OpenMPMapModifierKind> MapModifiers,
Alexey Bataevb3638132018-07-19 16:34:13 +00007453 bool ReturnDevicePointer, bool IsImplicit) {
Samuel Antaocc10b852016-07-28 14:23:26 +00007454 const ValueDecl *VD =
7455 D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
Kelvin Lief579432018-12-18 22:18:41 +00007456 Info[VD].emplace_back(L, MapType, MapModifiers, ReturnDevicePointer,
Alexey Bataevf47c4b42017-09-26 13:47:31 +00007457 IsImplicit);
Samuel Antaocc10b852016-07-28 14:23:26 +00007458 };
Samuel Antao8d2d7302016-05-26 18:30:22 +00007459
Paul Robinson78fb1322016-08-01 22:12:46 +00007460 // FIXME: MSVC 2013 seems to require this-> to find member CurDir.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00007461 for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>())
7462 for (const auto &L : C->component_lists()) {
Kelvin Lief579432018-12-18 22:18:41 +00007463 InfoGen(L.first, L.second, C->getMapType(), C->getMapTypeModifiers(),
Alexey Bataevb3638132018-07-19 16:34:13 +00007464 /*ReturnDevicePointer=*/false, C->isImplicit());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00007465 }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00007466 for (const auto *C : this->CurDir.getClausesOfKind<OMPToClause>())
7467 for (const auto &L : C->component_lists()) {
Kelvin Lief579432018-12-18 22:18:41 +00007468 InfoGen(L.first, L.second, OMPC_MAP_to, llvm::None,
Alexey Bataevb3638132018-07-19 16:34:13 +00007469 /*ReturnDevicePointer=*/false, C->isImplicit());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00007470 }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00007471 for (const auto *C : this->CurDir.getClausesOfKind<OMPFromClause>())
7472 for (const auto &L : C->component_lists()) {
Kelvin Lief579432018-12-18 22:18:41 +00007473 InfoGen(L.first, L.second, OMPC_MAP_from, llvm::None,
Alexey Bataevb3638132018-07-19 16:34:13 +00007474 /*ReturnDevicePointer=*/false, C->isImplicit());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00007475 }
Samuel Antao86ace552016-04-27 22:40:57 +00007476
Samuel Antaocc10b852016-07-28 14:23:26 +00007477 // Look at the use_device_ptr clause information and mark the existing map
7478 // entries as such. If there is no map information for an entry in the
7479 // use_device_ptr list, we create one with map type 'alloc' and zero size
Alexey Bataevb3638132018-07-19 16:34:13 +00007480 // section. It is the user fault if that was not mapped before. If there is
7481 // no map information and the pointer is a struct member, then we defer the
7482 // emission of that entry until the whole struct has been processed.
7483 llvm::MapVector<const ValueDecl *, SmallVector<DeferredDevicePtrEntryTy, 4>>
7484 DeferredInfo;
7485
Paul Robinson78fb1322016-08-01 22:12:46 +00007486 // FIXME: MSVC 2013 seems to require this-> to find member CurDir.
Alexey Bataevb3638132018-07-19 16:34:13 +00007487 for (const auto *C :
7488 this->CurDir.getClausesOfKind<OMPUseDevicePtrClause>()) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00007489 for (const auto &L : C->component_lists()) {
Samuel Antaocc10b852016-07-28 14:23:26 +00007490 assert(!L.second.empty() && "Not expecting empty list of components!");
7491 const ValueDecl *VD = L.second.back().getAssociatedDeclaration();
7492 VD = cast<ValueDecl>(VD->getCanonicalDecl());
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00007493 const Expr *IE = L.second.back().getAssociatedExpression();
Samuel Antaocc10b852016-07-28 14:23:26 +00007494 // If the first component is a member expression, we have to look into
7495 // 'this', which maps to null in the map of map information. Otherwise
7496 // look directly for the information.
7497 auto It = Info.find(isa<MemberExpr>(IE) ? nullptr : VD);
7498
7499 // We potentially have map information for this declaration already.
7500 // Look for the first set of components that refer to it.
7501 if (It != Info.end()) {
7502 auto CI = std::find_if(
7503 It->second.begin(), It->second.end(), [VD](const MapInfo &MI) {
7504 return MI.Components.back().getAssociatedDeclaration() == VD;
7505 });
7506 // If we found a map entry, signal that the pointer has to be returned
7507 // and move on to the next declaration.
7508 if (CI != It->second.end()) {
Alexey Bataevb3638132018-07-19 16:34:13 +00007509 CI->ReturnDevicePointer = true;
Samuel Antaocc10b852016-07-28 14:23:26 +00007510 continue;
7511 }
7512 }
7513
7514 // We didn't find any match in our map information - generate a zero
Alexey Bataevb3638132018-07-19 16:34:13 +00007515 // size array section - if the pointer is a struct member we defer this
7516 // action until the whole struct has been processed.
Paul Robinson78fb1322016-08-01 22:12:46 +00007517 // FIXME: MSVC 2013 seems to require this-> to find member CGF.
Alexey Bataevb3638132018-07-19 16:34:13 +00007518 if (isa<MemberExpr>(IE)) {
7519 // Insert the pointer into Info to be processed by
7520 // generateInfoForComponentList. Because it is a member pointer
7521 // without a pointee, no entry will be generated for it, therefore
7522 // we need to generate one after the whole struct has been processed.
7523 // Nonetheless, generateInfoForComponentList must be called to take
7524 // the pointer into account for the calculation of the range of the
7525 // partial struct.
Kelvin Lief579432018-12-18 22:18:41 +00007526 InfoGen(nullptr, L.second, OMPC_MAP_unknown, llvm::None,
Alexey Bataevb3638132018-07-19 16:34:13 +00007527 /*ReturnDevicePointer=*/false, C->isImplicit());
7528 DeferredInfo[nullptr].emplace_back(IE, VD);
7529 } else {
7530 llvm::Value *Ptr = this->CGF.EmitLoadOfScalar(
7531 this->CGF.EmitLValue(IE), IE->getExprLoc());
7532 BasePointers.emplace_back(Ptr, VD);
7533 Pointers.push_back(Ptr);
7534 Sizes.push_back(llvm::Constant::getNullValue(this->CGF.SizeTy));
7535 Types.push_back(OMP_MAP_RETURN_PARAM | OMP_MAP_TARGET_PARAM);
7536 }
Samuel Antaocc10b852016-07-28 14:23:26 +00007537 }
Alexey Bataevb3638132018-07-19 16:34:13 +00007538 }
Samuel Antaocc10b852016-07-28 14:23:26 +00007539
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00007540 for (const auto &M : Info) {
Samuel Antao86ace552016-04-27 22:40:57 +00007541 // We need to know when we generate information for the first component
7542 // associated with a capture, because the mapping flags depend on it.
7543 bool IsFirstComponentList = true;
Alexey Bataevb3638132018-07-19 16:34:13 +00007544
7545 // Temporary versions of arrays
7546 MapBaseValuesArrayTy CurBasePointers;
7547 MapValuesArrayTy CurPointers;
7548 MapValuesArrayTy CurSizes;
7549 MapFlagsArrayTy CurTypes;
7550 StructRangeInfoTy PartialStruct;
7551
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00007552 for (const MapInfo &L : M.second) {
Samuel Antao86ace552016-04-27 22:40:57 +00007553 assert(!L.Components.empty() &&
7554 "Not expecting declaration with no component lists.");
Samuel Antaocc10b852016-07-28 14:23:26 +00007555
7556 // Remember the current base pointer index.
Alexey Bataevb3638132018-07-19 16:34:13 +00007557 unsigned CurrentBasePointersIdx = CurBasePointers.size();
Paul Robinson78fb1322016-08-01 22:12:46 +00007558 // FIXME: MSVC 2013 seems to require this-> to find the member method.
Alexey Bataevf47c4b42017-09-26 13:47:31 +00007559 this->generateInfoForComponentList(
Kelvin Lief579432018-12-18 22:18:41 +00007560 L.MapType, L.MapModifiers, L.Components, CurBasePointers,
Alexey Bataevb3638132018-07-19 16:34:13 +00007561 CurPointers, CurSizes, CurTypes, PartialStruct,
7562 IsFirstComponentList, L.IsImplicit);
Samuel Antaocc10b852016-07-28 14:23:26 +00007563
7564 // If this entry relates with a device pointer, set the relevant
7565 // declaration and add the 'return pointer' flag.
Alexey Bataevb3638132018-07-19 16:34:13 +00007566 if (L.ReturnDevicePointer) {
7567 assert(CurBasePointers.size() > CurrentBasePointersIdx &&
Samuel Antaocc10b852016-07-28 14:23:26 +00007568 "Unexpected number of mapped base pointers.");
7569
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00007570 const ValueDecl *RelevantVD =
7571 L.Components.back().getAssociatedDeclaration();
Samuel Antaocc10b852016-07-28 14:23:26 +00007572 assert(RelevantVD &&
7573 "No relevant declaration related with device pointer??");
7574
Alexey Bataevb3638132018-07-19 16:34:13 +00007575 CurBasePointers[CurrentBasePointersIdx].setDevicePtrDecl(RelevantVD);
7576 CurTypes[CurrentBasePointersIdx] |= OMP_MAP_RETURN_PARAM;
Samuel Antaocc10b852016-07-28 14:23:26 +00007577 }
Samuel Antao86ace552016-04-27 22:40:57 +00007578 IsFirstComponentList = false;
7579 }
Alexey Bataevb3638132018-07-19 16:34:13 +00007580
7581 // Append any pending zero-length pointers which are struct members and
7582 // used with use_device_ptr.
7583 auto CI = DeferredInfo.find(M.first);
7584 if (CI != DeferredInfo.end()) {
7585 for (const DeferredDevicePtrEntryTy &L : CI->second) {
7586 llvm::Value *BasePtr = this->CGF.EmitLValue(L.IE).getPointer();
7587 llvm::Value *Ptr = this->CGF.EmitLoadOfScalar(
7588 this->CGF.EmitLValue(L.IE), L.IE->getExprLoc());
7589 CurBasePointers.emplace_back(BasePtr, L.VD);
7590 CurPointers.push_back(Ptr);
7591 CurSizes.push_back(llvm::Constant::getNullValue(this->CGF.SizeTy));
7592 // Entry is PTR_AND_OBJ and RETURN_PARAM. Also, set the placeholder
7593 // value MEMBER_OF=FFFF so that the entry is later updated with the
7594 // correct value of MEMBER_OF.
7595 CurTypes.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_RETURN_PARAM |
7596 OMP_MAP_MEMBER_OF);
7597 }
7598 }
7599
7600 // If there is an entry in PartialStruct it means we have a struct with
7601 // individual members mapped. Emit an extra combined entry.
7602 if (PartialStruct.Base.isValid())
7603 emitCombinedEntry(BasePointers, Pointers, Sizes, Types, CurTypes,
7604 PartialStruct);
7605
7606 // We need to append the results of this capture to what we already have.
7607 BasePointers.append(CurBasePointers.begin(), CurBasePointers.end());
7608 Pointers.append(CurPointers.begin(), CurPointers.end());
7609 Sizes.append(CurSizes.begin(), CurSizes.end());
7610 Types.append(CurTypes.begin(), CurTypes.end());
Samuel Antao86ace552016-04-27 22:40:57 +00007611 }
7612 }
7613
Alexey Bataev60705422018-10-30 15:50:12 +00007614 /// Emit capture info for lambdas for variables captured by reference.
Alexey Bataev969dbc02018-11-08 15:47:39 +00007615 void generateInfoForLambdaCaptures(
7616 const ValueDecl *VD, llvm::Value *Arg, MapBaseValuesArrayTy &BasePointers,
7617 MapValuesArrayTy &Pointers, MapValuesArrayTy &Sizes,
7618 MapFlagsArrayTy &Types,
7619 llvm::DenseMap<llvm::Value *, llvm::Value *> &LambdaPointers) const {
Alexey Bataev60705422018-10-30 15:50:12 +00007620 const auto *RD = VD->getType()
7621 .getCanonicalType()
7622 .getNonReferenceType()
7623 ->getAsCXXRecordDecl();
7624 if (!RD || !RD->isLambda())
7625 return;
7626 Address VDAddr = Address(Arg, CGF.getContext().getDeclAlign(VD));
7627 LValue VDLVal = CGF.MakeAddrLValue(
7628 VDAddr, VD->getType().getCanonicalType().getNonReferenceType());
7629 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
7630 FieldDecl *ThisCapture = nullptr;
7631 RD->getCaptureFields(Captures, ThisCapture);
7632 if (ThisCapture) {
7633 LValue ThisLVal =
7634 CGF.EmitLValueForFieldInitialization(VDLVal, ThisCapture);
Alexey Bataev969dbc02018-11-08 15:47:39 +00007635 LValue ThisLValVal = CGF.EmitLValueForField(VDLVal, ThisCapture);
7636 LambdaPointers.try_emplace(ThisLVal.getPointer(), VDLVal.getPointer());
7637 BasePointers.push_back(ThisLVal.getPointer());
7638 Pointers.push_back(ThisLValVal.getPointer());
Alexey Bataev60705422018-10-30 15:50:12 +00007639 Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy));
Alexey Bataev2dc07d02018-11-02 15:25:06 +00007640 Types.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL |
Alexey Bataev60705422018-10-30 15:50:12 +00007641 OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT);
7642 }
7643 for (const LambdaCapture &LC : RD->captures()) {
7644 if (LC.getCaptureKind() != LCK_ByRef)
7645 continue;
7646 const VarDecl *VD = LC.getCapturedVar();
7647 auto It = Captures.find(VD);
7648 assert(It != Captures.end() && "Found lambda capture without field.");
7649 LValue VarLVal = CGF.EmitLValueForFieldInitialization(VDLVal, It->second);
Alexey Bataev969dbc02018-11-08 15:47:39 +00007650 LValue VarLValVal = CGF.EmitLValueForField(VDLVal, It->second);
7651 LambdaPointers.try_emplace(VarLVal.getPointer(), VDLVal.getPointer());
7652 BasePointers.push_back(VarLVal.getPointer());
7653 Pointers.push_back(VarLValVal.getPointer());
Alexey Bataev60705422018-10-30 15:50:12 +00007654 Sizes.push_back(CGF.getTypeSize(
7655 VD->getType().getCanonicalType().getNonReferenceType()));
Alexey Bataev2dc07d02018-11-02 15:25:06 +00007656 Types.push_back(OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL |
Alexey Bataev60705422018-10-30 15:50:12 +00007657 OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT);
7658 }
7659 }
7660
7661 /// Set correct indices for lambdas captures.
Alexey Bataev969dbc02018-11-08 15:47:39 +00007662 void adjustMemberOfForLambdaCaptures(
7663 const llvm::DenseMap<llvm::Value *, llvm::Value *> &LambdaPointers,
7664 MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers,
7665 MapFlagsArrayTy &Types) const {
Alexey Bataev60705422018-10-30 15:50:12 +00007666 for (unsigned I = 0, E = Types.size(); I < E; ++I) {
7667 // Set correct member_of idx for all implicit lambda captures.
Alexey Bataev2dc07d02018-11-02 15:25:06 +00007668 if (Types[I] != (OMP_MAP_PTR_AND_OBJ | OMP_MAP_LITERAL |
Alexey Bataev60705422018-10-30 15:50:12 +00007669 OMP_MAP_MEMBER_OF | OMP_MAP_IMPLICIT))
7670 continue;
Alexey Bataev969dbc02018-11-08 15:47:39 +00007671 llvm::Value *BasePtr = LambdaPointers.lookup(*BasePointers[I]);
7672 assert(BasePtr && "Unable to find base lambda address.");
Alexey Bataev60705422018-10-30 15:50:12 +00007673 int TgtIdx = -1;
7674 for (unsigned J = I; J > 0; --J) {
7675 unsigned Idx = J - 1;
7676 if (Pointers[Idx] != BasePtr)
7677 continue;
7678 TgtIdx = Idx;
7679 break;
7680 }
7681 assert(TgtIdx != -1 && "Unable to find parent lambda.");
7682 // All other current entries will be MEMBER_OF the combined entry
7683 // (except for PTR_AND_OBJ entries which do not have a placeholder value
7684 // 0xFFFF in the MEMBER_OF field).
7685 OpenMPOffloadMappingFlags MemberOfFlag = getMemberOfFlag(TgtIdx);
7686 setCorrectMemberOfFlag(Types[I], MemberOfFlag);
7687 }
7688 }
7689
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007690 /// Generate the base pointers, section pointers, sizes and map types
Samuel Antao86ace552016-04-27 22:40:57 +00007691 /// associated to a given capture.
7692 void generateInfoForCapture(const CapturedStmt::Capture *Cap,
Samuel Antao6890b092016-07-28 14:25:09 +00007693 llvm::Value *Arg,
Samuel Antaocc10b852016-07-28 14:23:26 +00007694 MapBaseValuesArrayTy &BasePointers,
Samuel Antao86ace552016-04-27 22:40:57 +00007695 MapValuesArrayTy &Pointers,
Alexey Bataevb3638132018-07-19 16:34:13 +00007696 MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types,
7697 StructRangeInfoTy &PartialStruct) const {
Samuel Antao86ace552016-04-27 22:40:57 +00007698 assert(!Cap->capturesVariableArrayType() &&
7699 "Not expecting to generate map info for a variable array type!");
7700
Samuel Antao6890b092016-07-28 14:25:09 +00007701 // We need to know when we generating information for the first component
Alexey Bataevb3638132018-07-19 16:34:13 +00007702 const ValueDecl *VD = Cap->capturesThis()
7703 ? nullptr
7704 : Cap->getCapturedVar()->getCanonicalDecl();
Samuel Antao86ace552016-04-27 22:40:57 +00007705
Samuel Antao6890b092016-07-28 14:25:09 +00007706 // If this declaration appears in a is_device_ptr clause we just have to
7707 // pass the pointer by value. If it is a reference to a declaration, we just
Alexey Bataevb3638132018-07-19 16:34:13 +00007708 // pass its value.
7709 if (DevPointersMap.count(VD)) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00007710 BasePointers.emplace_back(Arg, VD);
Samuel Antao6890b092016-07-28 14:25:09 +00007711 Pointers.push_back(Arg);
7712 Sizes.push_back(CGF.getTypeSize(CGF.getContext().VoidPtrTy));
George Rokos065755d2017-11-07 18:27:04 +00007713 Types.push_back(OMP_MAP_LITERAL | OMP_MAP_TARGET_PARAM);
Samuel Antao6890b092016-07-28 14:25:09 +00007714 return;
7715 }
7716
Alexey Bataeve82445f2018-09-20 13:54:02 +00007717 using MapData =
7718 std::tuple<OMPClauseMappableExprCommon::MappableExprComponentListRef,
Kelvin Lief579432018-12-18 22:18:41 +00007719 OpenMPMapClauseKind, ArrayRef<OpenMPMapModifierKind>, bool>;
Alexey Bataeve82445f2018-09-20 13:54:02 +00007720 SmallVector<MapData, 4> DeclComponentLists;
Paul Robinson78fb1322016-08-01 22:12:46 +00007721 // FIXME: MSVC 2013 seems to require this-> to find member CurDir.
Alexey Bataeve82445f2018-09-20 13:54:02 +00007722 for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00007723 for (const auto &L : C->decl_component_lists(VD)) {
Samuel Antao86ace552016-04-27 22:40:57 +00007724 assert(L.first == VD &&
7725 "We got information for the wrong declaration??");
7726 assert(!L.second.empty() &&
7727 "Not expecting declaration with no component lists.");
Alexey Bataeve82445f2018-09-20 13:54:02 +00007728 DeclComponentLists.emplace_back(L.second, C->getMapType(),
Kelvin Lief579432018-12-18 22:18:41 +00007729 C->getMapTypeModifiers(),
Alexey Bataeve82445f2018-09-20 13:54:02 +00007730 C->isImplicit());
Samuel Antao86ace552016-04-27 22:40:57 +00007731 }
Alexey Bataeve82445f2018-09-20 13:54:02 +00007732 }
7733
7734 // Find overlapping elements (including the offset from the base element).
7735 llvm::SmallDenseMap<
7736 const MapData *,
7737 llvm::SmallVector<
7738 OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>,
7739 4>
7740 OverlappedData;
7741 size_t Count = 0;
7742 for (const MapData &L : DeclComponentLists) {
7743 OMPClauseMappableExprCommon::MappableExprComponentListRef Components;
7744 OpenMPMapClauseKind MapType;
Kelvin Lief579432018-12-18 22:18:41 +00007745 ArrayRef<OpenMPMapModifierKind> MapModifiers;
Alexey Bataeve82445f2018-09-20 13:54:02 +00007746 bool IsImplicit;
Kelvin Lief579432018-12-18 22:18:41 +00007747 std::tie(Components, MapType, MapModifiers, IsImplicit) = L;
Alexey Bataeve82445f2018-09-20 13:54:02 +00007748 ++Count;
7749 for (const MapData &L1 : makeArrayRef(DeclComponentLists).slice(Count)) {
7750 OMPClauseMappableExprCommon::MappableExprComponentListRef Components1;
Kelvin Lief579432018-12-18 22:18:41 +00007751 std::tie(Components1, MapType, MapModifiers, IsImplicit) = L1;
Alexey Bataeve82445f2018-09-20 13:54:02 +00007752 auto CI = Components.rbegin();
7753 auto CE = Components.rend();
7754 auto SI = Components1.rbegin();
7755 auto SE = Components1.rend();
7756 for (; CI != CE && SI != SE; ++CI, ++SI) {
7757 if (CI->getAssociatedExpression()->getStmtClass() !=
7758 SI->getAssociatedExpression()->getStmtClass())
7759 break;
7760 // Are we dealing with different variables/fields?
7761 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
7762 break;
7763 }
7764 // Found overlapping if, at least for one component, reached the head of
7765 // the components list.
7766 if (CI == CE || SI == SE) {
7767 assert((CI != CE || SI != SE) &&
7768 "Unexpected full match of the mapping components.");
7769 const MapData &BaseData = CI == CE ? L : L1;
7770 OMPClauseMappableExprCommon::MappableExprComponentListRef SubData =
7771 SI == SE ? Components : Components1;
Alexey Bataeve82445f2018-09-20 13:54:02 +00007772 auto &OverlappedElements = OverlappedData.FindAndConstruct(&BaseData);
7773 OverlappedElements.getSecond().push_back(SubData);
7774 }
7775 }
7776 }
7777 // Sort the overlapped elements for each item.
7778 llvm::SmallVector<const FieldDecl *, 4> Layout;
7779 if (!OverlappedData.empty()) {
7780 if (const auto *CRD =
7781 VD->getType().getCanonicalType()->getAsCXXRecordDecl())
7782 getPlainLayout(CRD, Layout, /*AsBase=*/false);
7783 else {
7784 const auto *RD = VD->getType().getCanonicalType()->getAsRecordDecl();
7785 Layout.append(RD->field_begin(), RD->field_end());
7786 }
7787 }
7788 for (auto &Pair : OverlappedData) {
7789 llvm::sort(
7790 Pair.getSecond(),
7791 [&Layout](
7792 OMPClauseMappableExprCommon::MappableExprComponentListRef First,
7793 OMPClauseMappableExprCommon::MappableExprComponentListRef
7794 Second) {
7795 auto CI = First.rbegin();
7796 auto CE = First.rend();
7797 auto SI = Second.rbegin();
7798 auto SE = Second.rend();
7799 for (; CI != CE && SI != SE; ++CI, ++SI) {
7800 if (CI->getAssociatedExpression()->getStmtClass() !=
7801 SI->getAssociatedExpression()->getStmtClass())
7802 break;
7803 // Are we dealing with different variables/fields?
7804 if (CI->getAssociatedDeclaration() !=
7805 SI->getAssociatedDeclaration())
7806 break;
7807 }
Richard Trieu5061e832018-09-21 21:20:33 +00007808
7809 // Lists contain the same elements.
7810 if (CI == CE && SI == SE)
7811 return false;
7812
7813 // List with less elements is less than list with more elements.
7814 if (CI == CE || SI == SE)
7815 return CI == CE;
7816
Alexey Bataeve82445f2018-09-20 13:54:02 +00007817 const auto *FD1 = cast<FieldDecl>(CI->getAssociatedDeclaration());
7818 const auto *FD2 = cast<FieldDecl>(SI->getAssociatedDeclaration());
7819 if (FD1->getParent() == FD2->getParent())
7820 return FD1->getFieldIndex() < FD2->getFieldIndex();
7821 const auto It =
7822 llvm::find_if(Layout, [FD1, FD2](const FieldDecl *FD) {
7823 return FD == FD1 || FD == FD2;
7824 });
7825 return *It == FD1;
7826 });
7827 }
7828
7829 // Associated with a capture, because the mapping flags depend on it.
7830 // Go through all of the elements with the overlapped elements.
7831 for (const auto &Pair : OverlappedData) {
7832 const MapData &L = *Pair.getFirst();
7833 OMPClauseMappableExprCommon::MappableExprComponentListRef Components;
7834 OpenMPMapClauseKind MapType;
Kelvin Lief579432018-12-18 22:18:41 +00007835 ArrayRef<OpenMPMapModifierKind> MapModifiers;
Alexey Bataeve82445f2018-09-20 13:54:02 +00007836 bool IsImplicit;
Kelvin Lief579432018-12-18 22:18:41 +00007837 std::tie(Components, MapType, MapModifiers, IsImplicit) = L;
Alexey Bataeve82445f2018-09-20 13:54:02 +00007838 ArrayRef<OMPClauseMappableExprCommon::MappableExprComponentListRef>
7839 OverlappedComponents = Pair.getSecond();
7840 bool IsFirstComponentList = true;
Kelvin Lief579432018-12-18 22:18:41 +00007841 generateInfoForComponentList(MapType, MapModifiers, Components,
Alexey Bataeve82445f2018-09-20 13:54:02 +00007842 BasePointers, Pointers, Sizes, Types,
7843 PartialStruct, IsFirstComponentList,
7844 IsImplicit, OverlappedComponents);
7845 }
7846 // Go through other elements without overlapped elements.
7847 bool IsFirstComponentList = OverlappedData.empty();
7848 for (const MapData &L : DeclComponentLists) {
7849 OMPClauseMappableExprCommon::MappableExprComponentListRef Components;
7850 OpenMPMapClauseKind MapType;
Kelvin Lief579432018-12-18 22:18:41 +00007851 ArrayRef<OpenMPMapModifierKind> MapModifiers;
Alexey Bataeve82445f2018-09-20 13:54:02 +00007852 bool IsImplicit;
Kelvin Lief579432018-12-18 22:18:41 +00007853 std::tie(Components, MapType, MapModifiers, IsImplicit) = L;
Alexey Bataeve82445f2018-09-20 13:54:02 +00007854 auto It = OverlappedData.find(&L);
7855 if (It == OverlappedData.end())
Kelvin Lief579432018-12-18 22:18:41 +00007856 generateInfoForComponentList(MapType, MapModifiers, Components,
Alexey Bataeve82445f2018-09-20 13:54:02 +00007857 BasePointers, Pointers, Sizes, Types,
7858 PartialStruct, IsFirstComponentList,
7859 IsImplicit);
7860 IsFirstComponentList = false;
7861 }
Alexey Bataevb3638132018-07-19 16:34:13 +00007862 }
Samuel Antao86ace552016-04-27 22:40:57 +00007863
Alexey Bataevb3638132018-07-19 16:34:13 +00007864 /// Generate the base pointers, section pointers, sizes and map types
7865 /// associated with the declare target link variables.
7866 void generateInfoForDeclareTargetLink(MapBaseValuesArrayTy &BasePointers,
7867 MapValuesArrayTy &Pointers,
7868 MapValuesArrayTy &Sizes,
7869 MapFlagsArrayTy &Types) const {
7870 // Map other list items in the map clause which are not captured variables
7871 // but "declare target link" global variables.,
7872 for (const auto *C : this->CurDir.getClausesOfKind<OMPMapClause>()) {
7873 for (const auto &L : C->component_lists()) {
7874 if (!L.first)
7875 continue;
7876 const auto *VD = dyn_cast<VarDecl>(L.first);
7877 if (!VD)
7878 continue;
7879 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00007880 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataevb3638132018-07-19 16:34:13 +00007881 if (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link)
7882 continue;
7883 StructRangeInfoTy PartialStruct;
7884 generateInfoForComponentList(
Kelvin Lief579432018-12-18 22:18:41 +00007885 C->getMapType(), C->getMapTypeModifiers(), L.second, BasePointers,
Alexey Bataevb3638132018-07-19 16:34:13 +00007886 Pointers, Sizes, Types, PartialStruct,
7887 /*IsFirstComponentList=*/true, C->isImplicit());
7888 assert(!PartialStruct.Base.isValid() &&
7889 "No partial structs for declare target link expected.");
7890 }
7891 }
Samuel Antao86ace552016-04-27 22:40:57 +00007892 }
Samuel Antaod486f842016-05-26 16:53:38 +00007893
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007894 /// Generate the default map information for a given capture \a CI,
Samuel Antaod486f842016-05-26 16:53:38 +00007895 /// record field declaration \a RI and captured value \a CV.
Samuel Antaocc10b852016-07-28 14:23:26 +00007896 void generateDefaultMapInfo(const CapturedStmt::Capture &CI,
7897 const FieldDecl &RI, llvm::Value *CV,
7898 MapBaseValuesArrayTy &CurBasePointers,
7899 MapValuesArrayTy &CurPointers,
7900 MapValuesArrayTy &CurSizes,
Alexey Bataevb3638132018-07-19 16:34:13 +00007901 MapFlagsArrayTy &CurMapTypes) const {
Samuel Antaod486f842016-05-26 16:53:38 +00007902 // Do the default mapping.
7903 if (CI.capturesThis()) {
7904 CurBasePointers.push_back(CV);
7905 CurPointers.push_back(CV);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00007906 const auto *PtrTy = cast<PointerType>(RI.getType().getTypePtr());
Samuel Antaod486f842016-05-26 16:53:38 +00007907 CurSizes.push_back(CGF.getTypeSize(PtrTy->getPointeeType()));
7908 // Default map type.
Samuel Antaocc10b852016-07-28 14:23:26 +00007909 CurMapTypes.push_back(OMP_MAP_TO | OMP_MAP_FROM);
Samuel Antaod486f842016-05-26 16:53:38 +00007910 } else if (CI.capturesVariableByCopy()) {
Samuel Antao6d004262016-06-16 18:39:34 +00007911 CurBasePointers.push_back(CV);
7912 CurPointers.push_back(CV);
Samuel Antaod486f842016-05-26 16:53:38 +00007913 if (!RI.getType()->isAnyPointerType()) {
Samuel Antao6d004262016-06-16 18:39:34 +00007914 // We have to signal to the runtime captures passed by value that are
7915 // not pointers.
George Rokos065755d2017-11-07 18:27:04 +00007916 CurMapTypes.push_back(OMP_MAP_LITERAL);
Samuel Antaod486f842016-05-26 16:53:38 +00007917 CurSizes.push_back(CGF.getTypeSize(RI.getType()));
7918 } else {
7919 // Pointers are implicitly mapped with a zero size and no flags
7920 // (other than first map that is added for all implicit maps).
Alexey Bataevb3638132018-07-19 16:34:13 +00007921 CurMapTypes.push_back(OMP_MAP_NONE);
Samuel Antaod486f842016-05-26 16:53:38 +00007922 CurSizes.push_back(llvm::Constant::getNullValue(CGF.SizeTy));
7923 }
7924 } else {
7925 assert(CI.capturesVariable() && "Expected captured reference.");
7926 CurBasePointers.push_back(CV);
7927 CurPointers.push_back(CV);
7928
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00007929 const auto *PtrTy = cast<ReferenceType>(RI.getType().getTypePtr());
Samuel Antaod486f842016-05-26 16:53:38 +00007930 QualType ElementType = PtrTy->getPointeeType();
7931 CurSizes.push_back(CGF.getTypeSize(ElementType));
7932 // The default map type for a scalar/complex type is 'to' because by
7933 // default the value doesn't have to be retrieved. For an aggregate
7934 // type, the default is 'tofrom'.
Alexey Bataevb3638132018-07-19 16:34:13 +00007935 CurMapTypes.push_back(getMapModifiersForPrivateClauses(CI));
Samuel Antaod486f842016-05-26 16:53:38 +00007936 }
George Rokos065755d2017-11-07 18:27:04 +00007937 // Every default map produces a single argument which is a target parameter.
7938 CurMapTypes.back() |= OMP_MAP_TARGET_PARAM;
Alexey Bataevb3638132018-07-19 16:34:13 +00007939
7940 // Add flag stating this is an implicit map.
7941 CurMapTypes.back() |= OMP_MAP_IMPLICIT;
Samuel Antaod486f842016-05-26 16:53:38 +00007942 }
Samuel Antao86ace552016-04-27 22:40:57 +00007943};
Samuel Antaodf158d52016-04-27 22:58:19 +00007944
7945enum OpenMPOffloadingReservedDeviceIDs {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007946 /// Device ID if the device was not defined, runtime should get it
Samuel Antaodf158d52016-04-27 22:58:19 +00007947 /// from environment variables in the spec.
7948 OMP_DEVICEID_UNDEF = -1,
7949};
7950} // anonymous namespace
7951
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00007952/// Emit the arrays used to pass the captures and map information to the
Samuel Antaodf158d52016-04-27 22:58:19 +00007953/// offloading runtime library. If there is no map or capture information,
7954/// return nullptr by reference.
7955static void
Samuel Antaocc10b852016-07-28 14:23:26 +00007956emitOffloadingArrays(CodeGenFunction &CGF,
7957 MappableExprsHandler::MapBaseValuesArrayTy &BasePointers,
Samuel Antaodf158d52016-04-27 22:58:19 +00007958 MappableExprsHandler::MapValuesArrayTy &Pointers,
7959 MappableExprsHandler::MapValuesArrayTy &Sizes,
Samuel Antaocc10b852016-07-28 14:23:26 +00007960 MappableExprsHandler::MapFlagsArrayTy &MapTypes,
7961 CGOpenMPRuntime::TargetDataInfo &Info) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00007962 CodeGenModule &CGM = CGF.CGM;
7963 ASTContext &Ctx = CGF.getContext();
Samuel Antaodf158d52016-04-27 22:58:19 +00007964
Samuel Antaocc10b852016-07-28 14:23:26 +00007965 // Reset the array information.
7966 Info.clearArrayInfo();
7967 Info.NumberOfPtrs = BasePointers.size();
Samuel Antaodf158d52016-04-27 22:58:19 +00007968
Samuel Antaocc10b852016-07-28 14:23:26 +00007969 if (Info.NumberOfPtrs) {
Samuel Antaodf158d52016-04-27 22:58:19 +00007970 // Detect if we have any capture size requiring runtime evaluation of the
7971 // size so that a constant array could be eventually used.
7972 bool hasRuntimeEvaluationCaptureSize = false;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00007973 for (llvm::Value *S : Sizes)
Samuel Antaodf158d52016-04-27 22:58:19 +00007974 if (!isa<llvm::Constant>(S)) {
7975 hasRuntimeEvaluationCaptureSize = true;
7976 break;
7977 }
7978
Samuel Antaocc10b852016-07-28 14:23:26 +00007979 llvm::APInt PointerNumAP(32, Info.NumberOfPtrs, /*isSigned=*/true);
Samuel Antaodf158d52016-04-27 22:58:19 +00007980 QualType PointerArrayType =
7981 Ctx.getConstantArrayType(Ctx.VoidPtrTy, PointerNumAP, ArrayType::Normal,
7982 /*IndexTypeQuals=*/0);
7983
Samuel Antaocc10b852016-07-28 14:23:26 +00007984 Info.BasePointersArray =
Samuel Antaodf158d52016-04-27 22:58:19 +00007985 CGF.CreateMemTemp(PointerArrayType, ".offload_baseptrs").getPointer();
Samuel Antaocc10b852016-07-28 14:23:26 +00007986 Info.PointersArray =
Samuel Antaodf158d52016-04-27 22:58:19 +00007987 CGF.CreateMemTemp(PointerArrayType, ".offload_ptrs").getPointer();
7988
7989 // If we don't have any VLA types or other types that require runtime
7990 // evaluation, we can use a constant array for the map sizes, otherwise we
7991 // need to fill up the arrays as we do for the pointers.
7992 if (hasRuntimeEvaluationCaptureSize) {
7993 QualType SizeArrayType = Ctx.getConstantArrayType(
7994 Ctx.getSizeType(), PointerNumAP, ArrayType::Normal,
7995 /*IndexTypeQuals=*/0);
Samuel Antaocc10b852016-07-28 14:23:26 +00007996 Info.SizesArray =
Samuel Antaodf158d52016-04-27 22:58:19 +00007997 CGF.CreateMemTemp(SizeArrayType, ".offload_sizes").getPointer();
7998 } else {
7999 // We expect all the sizes to be constant, so we collect them to create
8000 // a constant array.
8001 SmallVector<llvm::Constant *, 16> ConstSizes;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008002 for (llvm::Value *S : Sizes)
Samuel Antaodf158d52016-04-27 22:58:19 +00008003 ConstSizes.push_back(cast<llvm::Constant>(S));
8004
8005 auto *SizesArrayInit = llvm::ConstantArray::get(
8006 llvm::ArrayType::get(CGM.SizeTy, ConstSizes.size()), ConstSizes);
Alexey Bataev18fa2322018-05-02 14:20:50 +00008007 std::string Name = CGM.getOpenMPRuntime().getName({"offload_sizes"});
Samuel Antaodf158d52016-04-27 22:58:19 +00008008 auto *SizesArrayGbl = new llvm::GlobalVariable(
8009 CGM.getModule(), SizesArrayInit->getType(),
8010 /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage,
Alexey Bataev18fa2322018-05-02 14:20:50 +00008011 SizesArrayInit, Name);
Peter Collingbournebcf909d2016-06-14 21:02:05 +00008012 SizesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Samuel Antaocc10b852016-07-28 14:23:26 +00008013 Info.SizesArray = SizesArrayGbl;
Samuel Antaodf158d52016-04-27 22:58:19 +00008014 }
8015
8016 // The map types are always constant so we don't need to generate code to
8017 // fill arrays. Instead, we create an array constant.
Alexey Bataevb3638132018-07-19 16:34:13 +00008018 SmallVector<uint64_t, 4> Mapping(MapTypes.size(), 0);
8019 llvm::copy(MapTypes, Mapping.begin());
Samuel Antaodf158d52016-04-27 22:58:19 +00008020 llvm::Constant *MapTypesArrayInit =
Alexey Bataevb3638132018-07-19 16:34:13 +00008021 llvm::ConstantDataArray::get(CGF.Builder.getContext(), Mapping);
Alexey Bataev18fa2322018-05-02 14:20:50 +00008022 std::string MaptypesName =
8023 CGM.getOpenMPRuntime().getName({"offload_maptypes"});
Samuel Antaodf158d52016-04-27 22:58:19 +00008024 auto *MapTypesArrayGbl = new llvm::GlobalVariable(
8025 CGM.getModule(), MapTypesArrayInit->getType(),
8026 /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage,
Alexey Bataev18fa2322018-05-02 14:20:50 +00008027 MapTypesArrayInit, MaptypesName);
Peter Collingbournebcf909d2016-06-14 21:02:05 +00008028 MapTypesArrayGbl->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
Samuel Antaocc10b852016-07-28 14:23:26 +00008029 Info.MapTypesArray = MapTypesArrayGbl;
Samuel Antaodf158d52016-04-27 22:58:19 +00008030
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008031 for (unsigned I = 0; I < Info.NumberOfPtrs; ++I) {
8032 llvm::Value *BPVal = *BasePointers[I];
Samuel Antaodf158d52016-04-27 22:58:19 +00008033 llvm::Value *BP = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00008034 llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008035 Info.BasePointersArray, 0, I);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +00008036 BP = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
8037 BP, BPVal->getType()->getPointerTo(/*AddrSpace=*/0));
Samuel Antaodf158d52016-04-27 22:58:19 +00008038 Address BPAddr(BP, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy));
8039 CGF.Builder.CreateStore(BPVal, BPAddr);
8040
Samuel Antaocc10b852016-07-28 14:23:26 +00008041 if (Info.requiresDevicePointerInfo())
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008042 if (const ValueDecl *DevVD = BasePointers[I].getDevicePtrDecl())
Alexey Bataev43a919f2018-04-13 17:48:43 +00008043 Info.CaptureDeviceAddrMap.try_emplace(DevVD, BPAddr);
Samuel Antaocc10b852016-07-28 14:23:26 +00008044
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008045 llvm::Value *PVal = Pointers[I];
Samuel Antaodf158d52016-04-27 22:58:19 +00008046 llvm::Value *P = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00008047 llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008048 Info.PointersArray, 0, I);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +00008049 P = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
8050 P, PVal->getType()->getPointerTo(/*AddrSpace=*/0));
Samuel Antaodf158d52016-04-27 22:58:19 +00008051 Address PAddr(P, Ctx.getTypeAlignInChars(Ctx.VoidPtrTy));
8052 CGF.Builder.CreateStore(PVal, PAddr);
8053
8054 if (hasRuntimeEvaluationCaptureSize) {
8055 llvm::Value *S = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00008056 llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs),
8057 Info.SizesArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00008058 /*Idx0=*/0,
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008059 /*Idx1=*/I);
Samuel Antaodf158d52016-04-27 22:58:19 +00008060 Address SAddr(S, Ctx.getTypeAlignInChars(Ctx.getSizeType()));
8061 CGF.Builder.CreateStore(
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008062 CGF.Builder.CreateIntCast(Sizes[I], CGM.SizeTy, /*isSigned=*/true),
Samuel Antaodf158d52016-04-27 22:58:19 +00008063 SAddr);
8064 }
8065 }
8066 }
8067}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00008068/// Emit the arguments to be passed to the runtime library based on the
Samuel Antaodf158d52016-04-27 22:58:19 +00008069/// arrays of pointers, sizes and map types.
8070static void emitOffloadingArraysArgument(
8071 CodeGenFunction &CGF, llvm::Value *&BasePointersArrayArg,
8072 llvm::Value *&PointersArrayArg, llvm::Value *&SizesArrayArg,
Samuel Antaocc10b852016-07-28 14:23:26 +00008073 llvm::Value *&MapTypesArrayArg, CGOpenMPRuntime::TargetDataInfo &Info) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008074 CodeGenModule &CGM = CGF.CGM;
Samuel Antaocc10b852016-07-28 14:23:26 +00008075 if (Info.NumberOfPtrs) {
Samuel Antaodf158d52016-04-27 22:58:19 +00008076 BasePointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00008077 llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
8078 Info.BasePointersArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00008079 /*Idx0=*/0, /*Idx1=*/0);
8080 PointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00008081 llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
8082 Info.PointersArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00008083 /*Idx0=*/0,
8084 /*Idx1=*/0);
8085 SizesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
Samuel Antaocc10b852016-07-28 14:23:26 +00008086 llvm::ArrayType::get(CGM.SizeTy, Info.NumberOfPtrs), Info.SizesArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00008087 /*Idx0=*/0, /*Idx1=*/0);
8088 MapTypesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
George Rokos63bc9d62017-11-21 18:25:12 +00008089 llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs),
Samuel Antaocc10b852016-07-28 14:23:26 +00008090 Info.MapTypesArray,
Samuel Antaodf158d52016-04-27 22:58:19 +00008091 /*Idx0=*/0,
8092 /*Idx1=*/0);
8093 } else {
8094 BasePointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
8095 PointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
8096 SizesArrayArg = llvm::ConstantPointerNull::get(CGM.SizeTy->getPointerTo());
8097 MapTypesArrayArg =
George Rokos63bc9d62017-11-21 18:25:12 +00008098 llvm::ConstantPointerNull::get(CGM.Int64Ty->getPointerTo());
Samuel Antaodf158d52016-04-27 22:58:19 +00008099 }
Samuel Antao86ace552016-04-27 22:40:57 +00008100}
8101
Alexey Bataev7bb33532019-01-07 21:30:43 +00008102/// Checks if the expression is constant or does not have non-trivial function
8103/// calls.
8104static bool isTrivial(ASTContext &Ctx, const Expr * E) {
8105 // We can skip constant expressions.
8106 // We can skip expressions with trivial calls or simple expressions.
8107 return (E->isEvaluatable(Ctx, Expr::SE_AllowUndefinedBehavior) ||
8108 !E->hasNonTrivialCall(Ctx)) &&
8109 !E->HasSideEffects(Ctx, /*IncludePossibleEffects=*/true);
8110}
8111
8112/// Checks if the \p Body is the \a CompoundStmt and returns its child statement
8113/// iff there is only one that is not evaluatable at the compile time.
8114static const Stmt *getSingleCompoundChild(ASTContext &Ctx, const Stmt *Body) {
8115 if (const auto *C = dyn_cast<CompoundStmt>(Body)) {
8116 const Stmt *Child = nullptr;
8117 for (const Stmt *S : C->body()) {
8118 if (const auto *E = dyn_cast<Expr>(S)) {
8119 if (isTrivial(Ctx, E))
8120 continue;
8121 }
8122 // Some of the statements can be ignored.
8123 if (isa<AsmStmt>(S) || isa<NullStmt>(S) || isa<OMPFlushDirective>(S) ||
8124 isa<OMPBarrierDirective>(S) || isa<OMPTaskyieldDirective>(S))
8125 continue;
8126 // Analyze declarations.
8127 if (const auto *DS = dyn_cast<DeclStmt>(S)) {
8128 if (llvm::all_of(DS->decls(), [&Ctx](const Decl *D) {
8129 if (isa<EmptyDecl>(D) || isa<DeclContext>(D) ||
8130 isa<TypeDecl>(D) || isa<PragmaCommentDecl>(D) ||
8131 isa<PragmaDetectMismatchDecl>(D) || isa<UsingDecl>(D) ||
8132 isa<UsingDirectiveDecl>(D) ||
8133 isa<OMPDeclareReductionDecl>(D) ||
8134 isa<OMPThreadPrivateDecl>(D))
8135 return true;
8136 const auto *VD = dyn_cast<VarDecl>(D);
8137 if (!VD)
8138 return false;
8139 return VD->isConstexpr() ||
8140 ((VD->getType().isTrivialType(Ctx) ||
8141 VD->getType()->isReferenceType()) &&
8142 (!VD->hasInit() || isTrivial(Ctx, VD->getInit())));
8143 }))
8144 continue;
8145 }
8146 // Found multiple children - cannot get the one child only.
8147 if (Child)
8148 return Body;
8149 Child = S;
8150 }
8151 if (Child)
8152 return Child;
8153 }
8154 return Body;
8155}
8156
8157/// Check for inner distribute directive.
8158static const OMPExecutableDirective *
8159getNestedDistributeDirective(ASTContext &Ctx, const OMPExecutableDirective &D) {
8160 const auto *CS = D.getInnermostCapturedStmt();
8161 const auto *Body =
8162 CS->getCapturedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true);
8163 const Stmt *ChildStmt = getSingleCompoundChild(Ctx, Body);
8164
8165 if (const auto *NestedDir = dyn_cast<OMPExecutableDirective>(ChildStmt)) {
8166 OpenMPDirectiveKind DKind = NestedDir->getDirectiveKind();
8167 switch (D.getDirectiveKind()) {
8168 case OMPD_target:
8169 if (isOpenMPDistributeDirective(DKind))
8170 return NestedDir;
8171 if (DKind == OMPD_teams) {
8172 Body = NestedDir->getInnermostCapturedStmt()->IgnoreContainers(
8173 /*IgnoreCaptured=*/true);
8174 if (!Body)
8175 return nullptr;
8176 ChildStmt = getSingleCompoundChild(Ctx, Body);
8177 if (const auto *NND = dyn_cast<OMPExecutableDirective>(ChildStmt)) {
8178 DKind = NND->getDirectiveKind();
8179 if (isOpenMPDistributeDirective(DKind))
8180 return NND;
8181 }
8182 }
8183 return nullptr;
8184 case OMPD_target_teams:
8185 if (isOpenMPDistributeDirective(DKind))
8186 return NestedDir;
8187 return nullptr;
8188 case OMPD_target_parallel:
8189 case OMPD_target_simd:
8190 case OMPD_target_parallel_for:
8191 case OMPD_target_parallel_for_simd:
8192 return nullptr;
8193 case OMPD_target_teams_distribute:
8194 case OMPD_target_teams_distribute_simd:
8195 case OMPD_target_teams_distribute_parallel_for:
8196 case OMPD_target_teams_distribute_parallel_for_simd:
8197 case OMPD_parallel:
8198 case OMPD_for:
8199 case OMPD_parallel_for:
8200 case OMPD_parallel_sections:
8201 case OMPD_for_simd:
8202 case OMPD_parallel_for_simd:
8203 case OMPD_cancel:
8204 case OMPD_cancellation_point:
8205 case OMPD_ordered:
8206 case OMPD_threadprivate:
8207 case OMPD_task:
8208 case OMPD_simd:
8209 case OMPD_sections:
8210 case OMPD_section:
8211 case OMPD_single:
8212 case OMPD_master:
8213 case OMPD_critical:
8214 case OMPD_taskyield:
8215 case OMPD_barrier:
8216 case OMPD_taskwait:
8217 case OMPD_taskgroup:
8218 case OMPD_atomic:
8219 case OMPD_flush:
8220 case OMPD_teams:
8221 case OMPD_target_data:
8222 case OMPD_target_exit_data:
8223 case OMPD_target_enter_data:
8224 case OMPD_distribute:
8225 case OMPD_distribute_simd:
8226 case OMPD_distribute_parallel_for:
8227 case OMPD_distribute_parallel_for_simd:
8228 case OMPD_teams_distribute:
8229 case OMPD_teams_distribute_simd:
8230 case OMPD_teams_distribute_parallel_for:
8231 case OMPD_teams_distribute_parallel_for_simd:
8232 case OMPD_target_update:
8233 case OMPD_declare_simd:
8234 case OMPD_declare_target:
8235 case OMPD_end_declare_target:
8236 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008237 case OMPD_declare_mapper:
Alexey Bataev7bb33532019-01-07 21:30:43 +00008238 case OMPD_taskloop:
8239 case OMPD_taskloop_simd:
8240 case OMPD_requires:
8241 case OMPD_unknown:
8242 llvm_unreachable("Unexpected directive.");
8243 }
8244 }
8245
8246 return nullptr;
8247}
8248
8249void CGOpenMPRuntime::emitTargetNumIterationsCall(
8250 CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *Device,
8251 const llvm::function_ref<llvm::Value *(
8252 CodeGenFunction &CGF, const OMPLoopDirective &D)> &SizeEmitter) {
8253 OpenMPDirectiveKind Kind = D.getDirectiveKind();
8254 const OMPExecutableDirective *TD = &D;
8255 // Get nested teams distribute kind directive, if any.
8256 if (!isOpenMPDistributeDirective(Kind) || !isOpenMPTeamsDirective(Kind))
8257 TD = getNestedDistributeDirective(CGM.getContext(), D);
8258 if (!TD)
8259 return;
8260 const auto *LD = cast<OMPLoopDirective>(TD);
8261 auto &&CodeGen = [LD, &Device, &SizeEmitter, this](CodeGenFunction &CGF,
8262 PrePostActionTy &) {
8263 llvm::Value *NumIterations = SizeEmitter(CGF, *LD);
8264
8265 // Emit device ID if any.
8266 llvm::Value *DeviceID;
8267 if (Device)
8268 DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
8269 CGF.Int64Ty, /*isSigned=*/true);
8270 else
8271 DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF);
8272
8273 llvm::Value *Args[] = {DeviceID, NumIterations};
8274 CGF.EmitRuntimeCall(
8275 createRuntimeFunction(OMPRTL__kmpc_push_target_tripcount), Args);
8276 };
8277 emitInlinedDirective(CGF, OMPD_unknown, CodeGen);
8278}
8279
Samuel Antaobed3c462015-10-02 16:14:20 +00008280void CGOpenMPRuntime::emitTargetCall(CodeGenFunction &CGF,
8281 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00008282 llvm::Function *OutlinedFn,
Samuel Antaoee8fb302016-01-06 13:42:12 +00008283 llvm::Value *OutlinedFnID,
Alexey Bataev8451efa2018-01-15 19:06:12 +00008284 const Expr *IfCond, const Expr *Device) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00008285 if (!CGF.HaveInsertPoint())
8286 return;
Samuel Antaobed3c462015-10-02 16:14:20 +00008287
Samuel Antaoee8fb302016-01-06 13:42:12 +00008288 assert(OutlinedFn && "Invalid outlined function!");
8289
Alexey Bataev8451efa2018-01-15 19:06:12 +00008290 const bool RequiresOuterTask = D.hasClausesOfKind<OMPDependClause>();
8291 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
Alexey Bataev475a7442018-01-12 19:39:11 +00008292 const CapturedStmt &CS = *D.getCapturedStmt(OMPD_target);
Alexey Bataev8451efa2018-01-15 19:06:12 +00008293 auto &&ArgsCodegen = [&CS, &CapturedVars](CodeGenFunction &CGF,
8294 PrePostActionTy &) {
8295 CGF.GenerateOpenMPCapturedVars(CS, CapturedVars);
8296 };
8297 emitInlinedDirective(CGF, OMPD_unknown, ArgsCodegen);
Samuel Antao86ace552016-04-27 22:40:57 +00008298
Alexey Bataev8451efa2018-01-15 19:06:12 +00008299 CodeGenFunction::OMPTargetDataInfo InputInfo;
8300 llvm::Value *MapTypesArray = nullptr;
Samuel Antaobed3c462015-10-02 16:14:20 +00008301 // Fill up the pointer arrays and transfer execution to the device.
Alexey Bataev8451efa2018-01-15 19:06:12 +00008302 auto &&ThenGen = [this, Device, OutlinedFn, OutlinedFnID, &D, &InputInfo,
8303 &MapTypesArray, &CS, RequiresOuterTask,
8304 &CapturedVars](CodeGenFunction &CGF, PrePostActionTy &) {
Samuel Antaobed3c462015-10-02 16:14:20 +00008305 // On top of the arrays that were filled up, the target offloading call
8306 // takes as arguments the device id as well as the host pointer. The host
8307 // pointer is used by the runtime library to identify the current target
8308 // region, so it only has to be unique and not necessarily point to
8309 // anything. It could be the pointer to the outlined function that
8310 // implements the target region, but we aren't using that so that the
8311 // compiler doesn't need to keep that, and could therefore inline the host
8312 // function if proven worthwhile during optimization.
8313
Samuel Antaoee8fb302016-01-06 13:42:12 +00008314 // From this point on, we need to have an ID of the target region defined.
8315 assert(OutlinedFnID && "Invalid outlined function ID!");
Samuel Antaobed3c462015-10-02 16:14:20 +00008316
8317 // Emit device ID if any.
8318 llvm::Value *DeviceID;
George Rokos63bc9d62017-11-21 18:25:12 +00008319 if (Device) {
Samuel Antaobed3c462015-10-02 16:14:20 +00008320 DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
George Rokos63bc9d62017-11-21 18:25:12 +00008321 CGF.Int64Ty, /*isSigned=*/true);
8322 } else {
8323 DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF);
8324 }
Samuel Antaobed3c462015-10-02 16:14:20 +00008325
Samuel Antaodf158d52016-04-27 22:58:19 +00008326 // Emit the number of elements in the offloading arrays.
Alexey Bataev8451efa2018-01-15 19:06:12 +00008327 llvm::Value *PointerNum =
8328 CGF.Builder.getInt32(InputInfo.NumberOfTargetItems);
Samuel Antaodf158d52016-04-27 22:58:19 +00008329
Samuel Antaob68e2db2016-03-03 16:20:23 +00008330 // Return value of the runtime offloading call.
8331 llvm::Value *Return;
8332
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008333 llvm::Value *NumTeams = emitNumTeamsForTargetDirective(*this, CGF, D);
8334 llvm::Value *NumThreads = emitNumThreadsForTargetDirective(*this, CGF, D);
Samuel Antaob68e2db2016-03-03 16:20:23 +00008335
Alexey Bataeva9f77c62017-12-13 21:04:20 +00008336 bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>();
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008337 // The target region is an outlined function launched by the runtime
8338 // via calls __tgt_target() or __tgt_target_teams().
8339 //
8340 // __tgt_target() launches a target region with one team and one thread,
8341 // executing a serial region. This master thread may in turn launch
8342 // more threads within its team upon encountering a parallel region,
8343 // however, no additional teams can be launched on the device.
8344 //
8345 // __tgt_target_teams() launches a target region with one or more teams,
8346 // each with one or more threads. This call is required for target
8347 // constructs such as:
8348 // 'target teams'
8349 // 'target' / 'teams'
8350 // 'target teams distribute parallel for'
8351 // 'target parallel'
8352 // and so on.
8353 //
8354 // Note that on the host and CPU targets, the runtime implementation of
8355 // these calls simply call the outlined function without forking threads.
8356 // The outlined functions themselves have runtime calls to
8357 // __kmpc_fork_teams() and __kmpc_fork() for this purpose, codegen'd by
8358 // the compiler in emitTeamsCall() and emitParallelCall().
8359 //
8360 // In contrast, on the NVPTX target, the implementation of
8361 // __tgt_target_teams() launches a GPU kernel with the requested number
8362 // of teams and threads so no additional calls to the runtime are required.
Samuel Antaob68e2db2016-03-03 16:20:23 +00008363 if (NumTeams) {
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008364 // If we have NumTeams defined this means that we have an enclosed teams
8365 // region. Therefore we also expect to have NumThreads defined. These two
8366 // values should be defined in the presence of a teams directive,
8367 // regardless of having any clauses associated. If the user is using teams
8368 // but no clauses, these two values will be the default that should be
8369 // passed to the runtime library - a 32-bit integer with the value zero.
8370 assert(NumThreads && "Thread limit expression should be available along "
8371 "with number of teams.");
Alexey Bataev8451efa2018-01-15 19:06:12 +00008372 llvm::Value *OffloadingArgs[] = {DeviceID,
8373 OutlinedFnID,
8374 PointerNum,
8375 InputInfo.BasePointersArray.getPointer(),
8376 InputInfo.PointersArray.getPointer(),
8377 InputInfo.SizesArray.getPointer(),
8378 MapTypesArray,
8379 NumTeams,
8380 NumThreads};
Samuel Antaob68e2db2016-03-03 16:20:23 +00008381 Return = CGF.EmitRuntimeCall(
Alexey Bataev8451efa2018-01-15 19:06:12 +00008382 createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_teams_nowait
8383 : OMPRTL__tgt_target_teams),
Alexey Bataeva9f77c62017-12-13 21:04:20 +00008384 OffloadingArgs);
Samuel Antaob68e2db2016-03-03 16:20:23 +00008385 } else {
Alexey Bataev8451efa2018-01-15 19:06:12 +00008386 llvm::Value *OffloadingArgs[] = {DeviceID,
8387 OutlinedFnID,
8388 PointerNum,
8389 InputInfo.BasePointersArray.getPointer(),
8390 InputInfo.PointersArray.getPointer(),
8391 InputInfo.SizesArray.getPointer(),
8392 MapTypesArray};
Alexey Bataeva9f77c62017-12-13 21:04:20 +00008393 Return = CGF.EmitRuntimeCall(
Alexey Bataev8451efa2018-01-15 19:06:12 +00008394 createRuntimeFunction(HasNowait ? OMPRTL__tgt_target_nowait
8395 : OMPRTL__tgt_target),
Alexey Bataeva9f77c62017-12-13 21:04:20 +00008396 OffloadingArgs);
Samuel Antaob68e2db2016-03-03 16:20:23 +00008397 }
Samuel Antaobed3c462015-10-02 16:14:20 +00008398
Alexey Bataev2a007e02017-10-02 14:20:58 +00008399 // Check the error code and execute the host version if required.
8400 llvm::BasicBlock *OffloadFailedBlock =
8401 CGF.createBasicBlock("omp_offload.failed");
8402 llvm::BasicBlock *OffloadContBlock =
8403 CGF.createBasicBlock("omp_offload.cont");
8404 llvm::Value *Failed = CGF.Builder.CreateIsNotNull(Return);
8405 CGF.Builder.CreateCondBr(Failed, OffloadFailedBlock, OffloadContBlock);
8406
8407 CGF.EmitBlock(OffloadFailedBlock);
Alexey Bataev8451efa2018-01-15 19:06:12 +00008408 if (RequiresOuterTask) {
8409 CapturedVars.clear();
8410 CGF.GenerateOpenMPCapturedVars(CS, CapturedVars);
8411 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008412 emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars);
Alexey Bataev2a007e02017-10-02 14:20:58 +00008413 CGF.EmitBranch(OffloadContBlock);
8414
8415 CGF.EmitBlock(OffloadContBlock, /*IsFinished=*/true);
Samuel Antaobed3c462015-10-02 16:14:20 +00008416 };
8417
Samuel Antaoee8fb302016-01-06 13:42:12 +00008418 // Notify that the host version must be executed.
Alexey Bataev8451efa2018-01-15 19:06:12 +00008419 auto &&ElseGen = [this, &D, OutlinedFn, &CS, &CapturedVars,
8420 RequiresOuterTask](CodeGenFunction &CGF,
8421 PrePostActionTy &) {
8422 if (RequiresOuterTask) {
8423 CapturedVars.clear();
8424 CGF.GenerateOpenMPCapturedVars(CS, CapturedVars);
8425 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008426 emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedFn, CapturedVars);
Alexey Bataev8451efa2018-01-15 19:06:12 +00008427 };
8428
8429 auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray,
8430 &CapturedVars, RequiresOuterTask,
8431 &CS](CodeGenFunction &CGF, PrePostActionTy &) {
8432 // Fill up the arrays with all the captured variables.
8433 MappableExprsHandler::MapBaseValuesArrayTy BasePointers;
8434 MappableExprsHandler::MapValuesArrayTy Pointers;
8435 MappableExprsHandler::MapValuesArrayTy Sizes;
8436 MappableExprsHandler::MapFlagsArrayTy MapTypes;
8437
Alexey Bataev8451efa2018-01-15 19:06:12 +00008438 // Get mappable expression information.
8439 MappableExprsHandler MEHandler(D, CGF);
Alexey Bataev969dbc02018-11-08 15:47:39 +00008440 llvm::DenseMap<llvm::Value *, llvm::Value *> LambdaPointers;
Alexey Bataev8451efa2018-01-15 19:06:12 +00008441
8442 auto RI = CS.getCapturedRecordDecl()->field_begin();
8443 auto CV = CapturedVars.begin();
8444 for (CapturedStmt::const_capture_iterator CI = CS.capture_begin(),
8445 CE = CS.capture_end();
8446 CI != CE; ++CI, ++RI, ++CV) {
Alexey Bataevb3638132018-07-19 16:34:13 +00008447 MappableExprsHandler::MapBaseValuesArrayTy CurBasePointers;
8448 MappableExprsHandler::MapValuesArrayTy CurPointers;
8449 MappableExprsHandler::MapValuesArrayTy CurSizes;
8450 MappableExprsHandler::MapFlagsArrayTy CurMapTypes;
8451 MappableExprsHandler::StructRangeInfoTy PartialStruct;
Alexey Bataev8451efa2018-01-15 19:06:12 +00008452
8453 // VLA sizes are passed to the outlined region by copy and do not have map
8454 // information associated.
8455 if (CI->capturesVariableArrayType()) {
8456 CurBasePointers.push_back(*CV);
8457 CurPointers.push_back(*CV);
8458 CurSizes.push_back(CGF.getTypeSize(RI->getType()));
8459 // Copy to the device as an argument. No need to retrieve it.
8460 CurMapTypes.push_back(MappableExprsHandler::OMP_MAP_LITERAL |
8461 MappableExprsHandler::OMP_MAP_TARGET_PARAM);
8462 } else {
8463 // If we have any information in the map clause, we use it, otherwise we
8464 // just do a default mapping.
8465 MEHandler.generateInfoForCapture(CI, *CV, CurBasePointers, CurPointers,
Alexey Bataevb3638132018-07-19 16:34:13 +00008466 CurSizes, CurMapTypes, PartialStruct);
Alexey Bataev8451efa2018-01-15 19:06:12 +00008467 if (CurBasePointers.empty())
8468 MEHandler.generateDefaultMapInfo(*CI, **RI, *CV, CurBasePointers,
8469 CurPointers, CurSizes, CurMapTypes);
Alexey Bataev60705422018-10-30 15:50:12 +00008470 // Generate correct mapping for variables captured by reference in
8471 // lambdas.
8472 if (CI->capturesVariable())
Alexey Bataev969dbc02018-11-08 15:47:39 +00008473 MEHandler.generateInfoForLambdaCaptures(
8474 CI->getCapturedVar(), *CV, CurBasePointers, CurPointers, CurSizes,
8475 CurMapTypes, LambdaPointers);
Alexey Bataev8451efa2018-01-15 19:06:12 +00008476 }
8477 // We expect to have at least an element of information for this capture.
8478 assert(!CurBasePointers.empty() &&
8479 "Non-existing map pointer for capture!");
8480 assert(CurBasePointers.size() == CurPointers.size() &&
8481 CurBasePointers.size() == CurSizes.size() &&
8482 CurBasePointers.size() == CurMapTypes.size() &&
8483 "Inconsistent map information sizes!");
8484
Alexey Bataevb3638132018-07-19 16:34:13 +00008485 // If there is an entry in PartialStruct it means we have a struct with
8486 // individual members mapped. Emit an extra combined entry.
8487 if (PartialStruct.Base.isValid())
8488 MEHandler.emitCombinedEntry(BasePointers, Pointers, Sizes, MapTypes,
8489 CurMapTypes, PartialStruct);
8490
Alexey Bataev8451efa2018-01-15 19:06:12 +00008491 // We need to append the results of this capture to what we already have.
8492 BasePointers.append(CurBasePointers.begin(), CurBasePointers.end());
8493 Pointers.append(CurPointers.begin(), CurPointers.end());
8494 Sizes.append(CurSizes.begin(), CurSizes.end());
8495 MapTypes.append(CurMapTypes.begin(), CurMapTypes.end());
8496 }
Alexey Bataev60705422018-10-30 15:50:12 +00008497 // Adjust MEMBER_OF flags for the lambdas captures.
Alexey Bataev969dbc02018-11-08 15:47:39 +00008498 MEHandler.adjustMemberOfForLambdaCaptures(LambdaPointers, BasePointers,
8499 Pointers, MapTypes);
Alexey Bataev92327c52018-03-26 16:40:55 +00008500 // Map other list items in the map clause which are not captured variables
8501 // but "declare target link" global variables.
Alexey Bataevb3638132018-07-19 16:34:13 +00008502 MEHandler.generateInfoForDeclareTargetLink(BasePointers, Pointers, Sizes,
8503 MapTypes);
Alexey Bataev8451efa2018-01-15 19:06:12 +00008504
8505 TargetDataInfo Info;
8506 // Fill up the arrays and create the arguments.
8507 emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info);
8508 emitOffloadingArraysArgument(CGF, Info.BasePointersArray,
8509 Info.PointersArray, Info.SizesArray,
8510 Info.MapTypesArray, Info);
8511 InputInfo.NumberOfTargetItems = Info.NumberOfPtrs;
8512 InputInfo.BasePointersArray =
8513 Address(Info.BasePointersArray, CGM.getPointerAlign());
8514 InputInfo.PointersArray =
8515 Address(Info.PointersArray, CGM.getPointerAlign());
8516 InputInfo.SizesArray = Address(Info.SizesArray, CGM.getPointerAlign());
8517 MapTypesArray = Info.MapTypesArray;
8518 if (RequiresOuterTask)
8519 CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo);
8520 else
8521 emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen);
8522 };
8523
8524 auto &&TargetElseGen = [this, &ElseGen, &D, RequiresOuterTask](
8525 CodeGenFunction &CGF, PrePostActionTy &) {
8526 if (RequiresOuterTask) {
8527 CodeGenFunction::OMPTargetDataInfo InputInfo;
8528 CGF.EmitOMPTargetTaskBasedDirective(D, ElseGen, InputInfo);
8529 } else {
8530 emitInlinedDirective(CGF, D.getDirectiveKind(), ElseGen);
8531 }
Samuel Antaoee8fb302016-01-06 13:42:12 +00008532 };
8533
8534 // If we have a target function ID it means that we need to support
8535 // offloading, otherwise, just execute on the host. We need to execute on host
8536 // regardless of the conditional in the if clause if, e.g., the user do not
8537 // specify target triples.
8538 if (OutlinedFnID) {
Alexey Bataev8451efa2018-01-15 19:06:12 +00008539 if (IfCond) {
8540 emitOMPIfClause(CGF, IfCond, TargetThenGen, TargetElseGen);
8541 } else {
8542 RegionCodeGenTy ThenRCG(TargetThenGen);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00008543 ThenRCG(CGF);
Alexey Bataevf539faa2016-03-28 12:58:34 +00008544 }
8545 } else {
Alexey Bataev8451efa2018-01-15 19:06:12 +00008546 RegionCodeGenTy ElseRCG(TargetElseGen);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00008547 ElseRCG(CGF);
Alexey Bataevf539faa2016-03-28 12:58:34 +00008548 }
Samuel Antaobed3c462015-10-02 16:14:20 +00008549}
Samuel Antaoee8fb302016-01-06 13:42:12 +00008550
8551void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S,
8552 StringRef ParentName) {
8553 if (!S)
8554 return;
8555
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00008556 // Codegen OMP target directives that offload compute to the device.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008557 bool RequiresDeviceCodegen =
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00008558 isa<OMPExecutableDirective>(S) &&
8559 isOpenMPTargetExecutionDirective(
8560 cast<OMPExecutableDirective>(S)->getDirectiveKind());
Samuel Antaoee8fb302016-01-06 13:42:12 +00008561
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008562 if (RequiresDeviceCodegen) {
8563 const auto &E = *cast<OMPExecutableDirective>(S);
Samuel Antaoee8fb302016-01-06 13:42:12 +00008564 unsigned DeviceID;
8565 unsigned FileID;
8566 unsigned Line;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00008567 getTargetEntryUniqueInfo(CGM.getContext(), E.getBeginLoc(), DeviceID,
Samuel Antao2de62b02016-02-13 23:35:10 +00008568 FileID, Line);
Samuel Antaoee8fb302016-01-06 13:42:12 +00008569
8570 // Is this a target region that should not be emitted as an entry point? If
8571 // so just signal we are done with this target region.
Samuel Antao2de62b02016-02-13 23:35:10 +00008572 if (!OffloadEntriesInfoManager.hasTargetRegionEntryInfo(DeviceID, FileID,
8573 ParentName, Line))
Samuel Antaoee8fb302016-01-06 13:42:12 +00008574 return;
8575
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008576 switch (E.getDirectiveKind()) {
8577 case OMPD_target:
8578 CodeGenFunction::EmitOMPTargetDeviceFunction(CGM, ParentName,
8579 cast<OMPTargetDirective>(E));
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00008580 break;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008581 case OMPD_target_parallel:
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00008582 CodeGenFunction::EmitOMPTargetParallelDeviceFunction(
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008583 CGM, ParentName, cast<OMPTargetParallelDirective>(E));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00008584 break;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008585 case OMPD_target_teams:
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00008586 CodeGenFunction::EmitOMPTargetTeamsDeviceFunction(
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008587 CGM, ParentName, cast<OMPTargetTeamsDirective>(E));
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00008588 break;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008589 case OMPD_target_teams_distribute:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00008590 CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction(
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008591 CGM, ParentName, cast<OMPTargetTeamsDistributeDirective>(E));
Alexey Bataevdfa430f2017-12-08 15:03:50 +00008592 break;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008593 case OMPD_target_teams_distribute_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008594 CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction(
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008595 CGM, ParentName, cast<OMPTargetTeamsDistributeSimdDirective>(E));
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008596 break;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008597 case OMPD_target_parallel_for:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008598 CodeGenFunction::EmitOMPTargetParallelForDeviceFunction(
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008599 CGM, ParentName, cast<OMPTargetParallelForDirective>(E));
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008600 break;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008601 case OMPD_target_parallel_for_simd:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008602 CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction(
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008603 CGM, ParentName, cast<OMPTargetParallelForSimdDirective>(E));
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008604 break;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008605 case OMPD_target_simd:
Alexey Bataevf8365372017-11-17 17:57:25 +00008606 CodeGenFunction::EmitOMPTargetSimdDeviceFunction(
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008607 CGM, ParentName, cast<OMPTargetSimdDirective>(E));
Alexey Bataevf8365372017-11-17 17:57:25 +00008608 break;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008609 case OMPD_target_teams_distribute_parallel_for:
Carlo Bertolli52978c32018-01-03 21:12:44 +00008610 CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction(
8611 CGM, ParentName,
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008612 cast<OMPTargetTeamsDistributeParallelForDirective>(E));
Carlo Bertolli52978c32018-01-03 21:12:44 +00008613 break;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008614 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00008615 CodeGenFunction::
8616 EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction(
8617 CGM, ParentName,
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008618 cast<OMPTargetTeamsDistributeParallelForSimdDirective>(E));
Alexey Bataev647dd842018-01-15 20:59:40 +00008619 break;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008620 case OMPD_parallel:
8621 case OMPD_for:
8622 case OMPD_parallel_for:
8623 case OMPD_parallel_sections:
8624 case OMPD_for_simd:
8625 case OMPD_parallel_for_simd:
8626 case OMPD_cancel:
8627 case OMPD_cancellation_point:
8628 case OMPD_ordered:
8629 case OMPD_threadprivate:
8630 case OMPD_task:
8631 case OMPD_simd:
8632 case OMPD_sections:
8633 case OMPD_section:
8634 case OMPD_single:
8635 case OMPD_master:
8636 case OMPD_critical:
8637 case OMPD_taskyield:
8638 case OMPD_barrier:
8639 case OMPD_taskwait:
8640 case OMPD_taskgroup:
8641 case OMPD_atomic:
8642 case OMPD_flush:
8643 case OMPD_teams:
8644 case OMPD_target_data:
8645 case OMPD_target_exit_data:
8646 case OMPD_target_enter_data:
8647 case OMPD_distribute:
8648 case OMPD_distribute_simd:
8649 case OMPD_distribute_parallel_for:
8650 case OMPD_distribute_parallel_for_simd:
8651 case OMPD_teams_distribute:
8652 case OMPD_teams_distribute_simd:
8653 case OMPD_teams_distribute_parallel_for:
8654 case OMPD_teams_distribute_parallel_for_simd:
8655 case OMPD_target_update:
8656 case OMPD_declare_simd:
8657 case OMPD_declare_target:
8658 case OMPD_end_declare_target:
8659 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008660 case OMPD_declare_mapper:
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008661 case OMPD_taskloop:
8662 case OMPD_taskloop_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008663 case OMPD_requires:
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008664 case OMPD_unknown:
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00008665 llvm_unreachable("Unknown target directive for OpenMP device codegen.");
8666 }
Samuel Antaoee8fb302016-01-06 13:42:12 +00008667 return;
8668 }
8669
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008670 if (const auto *E = dyn_cast<OMPExecutableDirective>(S)) {
Alexey Bataev475a7442018-01-12 19:39:11 +00008671 if (!E->hasAssociatedStmt() || !E->getAssociatedStmt())
Samuel Antaoee8fb302016-01-06 13:42:12 +00008672 return;
8673
8674 scanForTargetRegionsFunctions(
Alexey Bataev475a7442018-01-12 19:39:11 +00008675 E->getInnermostCapturedStmt()->getCapturedStmt(), ParentName);
Samuel Antaoee8fb302016-01-06 13:42:12 +00008676 return;
8677 }
8678
8679 // If this is a lambda function, look into its body.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008680 if (const auto *L = dyn_cast<LambdaExpr>(S))
Samuel Antaoee8fb302016-01-06 13:42:12 +00008681 S = L->getBody();
8682
8683 // Keep looking for target regions recursively.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008684 for (const Stmt *II : S->children())
Samuel Antaoee8fb302016-01-06 13:42:12 +00008685 scanForTargetRegionsFunctions(II, ParentName);
Samuel Antaoee8fb302016-01-06 13:42:12 +00008686}
8687
8688bool CGOpenMPRuntime::emitTargetFunctions(GlobalDecl GD) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00008689 // If emitting code for the host, we do not process FD here. Instead we do
8690 // the normal code generation.
8691 if (!CGM.getLangOpts().OpenMPIsDevice)
8692 return false;
8693
Alexey Bataeve6aa4692018-09-13 16:54:05 +00008694 const ValueDecl *VD = cast<ValueDecl>(GD.getDecl());
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00008695 StringRef Name = CGM.getMangledName(GD);
8696 // Try to detect target regions in the function.
Alexey Bataeve6aa4692018-09-13 16:54:05 +00008697 if (const auto *FD = dyn_cast<FunctionDecl>(VD))
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00008698 scanForTargetRegionsFunctions(FD->getBody(), Name);
Samuel Antaoee8fb302016-01-06 13:42:12 +00008699
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00008700 // Do not to emit function if it is not marked as declare target.
Alexey Bataeve6aa4692018-09-13 16:54:05 +00008701 return !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) &&
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00008702 AlreadyEmittedTargetFunctions.count(Name) == 0;
Samuel Antaoee8fb302016-01-06 13:42:12 +00008703}
8704
8705bool CGOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl GD) {
8706 if (!CGM.getLangOpts().OpenMPIsDevice)
8707 return false;
8708
8709 // Check if there are Ctors/Dtors in this declaration and look for target
8710 // regions in it. We use the complete variant to produce the kernel name
8711 // mangling.
8712 QualType RDTy = cast<VarDecl>(GD.getDecl())->getType();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008713 if (const auto *RD = RDTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) {
8714 for (const CXXConstructorDecl *Ctor : RD->ctors()) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00008715 StringRef ParentName =
8716 CGM.getMangledName(GlobalDecl(Ctor, Ctor_Complete));
8717 scanForTargetRegionsFunctions(Ctor->getBody(), ParentName);
8718 }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008719 if (const CXXDestructorDecl *Dtor = RD->getDestructor()) {
Samuel Antaoee8fb302016-01-06 13:42:12 +00008720 StringRef ParentName =
8721 CGM.getMangledName(GlobalDecl(Dtor, Dtor_Complete));
8722 scanForTargetRegionsFunctions(Dtor->getBody(), ParentName);
8723 }
8724 }
8725
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00008726 // Do not to emit variable if it is not marked as declare target.
Alexey Bataev92327c52018-03-26 16:40:55 +00008727 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00008728 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
8729 cast<VarDecl>(GD.getDecl()));
Alexey Bataevbf8fe712018-08-07 16:14:36 +00008730 if (!Res || *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Alexey Bataevd01b7492018-08-15 19:45:12 +00008731 DeferredGlobalVariables.insert(cast<VarDecl>(GD.getDecl()));
Alexey Bataevbf8fe712018-08-07 16:14:36 +00008732 return true;
8733 }
8734 return false;
Samuel Antaoee8fb302016-01-06 13:42:12 +00008735}
8736
Alexey Bataev03f270c2018-03-30 18:31:07 +00008737void CGOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *VD,
8738 llvm::Constant *Addr) {
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00008739 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
8740 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
8741 if (!Res) {
8742 if (CGM.getLangOpts().OpenMPIsDevice) {
8743 // Register non-target variables being emitted in device code (debug info
8744 // may cause this).
8745 StringRef VarName = CGM.getMangledName(VD);
8746 EmittedNonTargetVariables.try_emplace(VarName, Addr);
Alexey Bataev03f270c2018-03-30 18:31:07 +00008747 }
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00008748 return;
Alexey Bataev03f270c2018-03-30 18:31:07 +00008749 }
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00008750 // Register declare target variables.
8751 OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryKind Flags;
8752 StringRef VarName;
8753 CharUnits VarSize;
8754 llvm::GlobalValue::LinkageTypes Linkage;
8755 switch (*Res) {
8756 case OMPDeclareTargetDeclAttr::MT_To:
8757 Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryTo;
8758 VarName = CGM.getMangledName(VD);
8759 if (VD->hasDefinition(CGM.getContext()) != VarDecl::DeclarationOnly) {
8760 VarSize = CGM.getContext().getTypeSizeInChars(VD->getType());
8761 assert(!VarSize.isZero() && "Expected non-zero size of the variable");
8762 } else {
8763 VarSize = CharUnits::Zero();
8764 }
8765 Linkage = CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false);
8766 // Temp solution to prevent optimizations of the internal variables.
8767 if (CGM.getLangOpts().OpenMPIsDevice && !VD->isExternallyVisible()) {
8768 std::string RefName = getName({VarName, "ref"});
8769 if (!CGM.GetGlobalValue(RefName)) {
8770 llvm::Constant *AddrRef =
8771 getOrCreateInternalVariable(Addr->getType(), RefName);
8772 auto *GVAddrRef = cast<llvm::GlobalVariable>(AddrRef);
8773 GVAddrRef->setConstant(/*Val=*/true);
8774 GVAddrRef->setLinkage(llvm::GlobalValue::InternalLinkage);
8775 GVAddrRef->setInitializer(Addr);
8776 CGM.addCompilerUsedGlobal(GVAddrRef);
8777 }
8778 }
8779 break;
8780 case OMPDeclareTargetDeclAttr::MT_Link:
8781 Flags = OffloadEntriesInfoManagerTy::OMPTargetGlobalVarEntryLink;
8782 if (CGM.getLangOpts().OpenMPIsDevice) {
8783 VarName = Addr->getName();
8784 Addr = nullptr;
8785 } else {
8786 VarName = getAddrOfDeclareTargetLink(VD).getName();
8787 Addr = cast<llvm::Constant>(getAddrOfDeclareTargetLink(VD).getPointer());
8788 }
8789 VarSize = CGM.getPointerSize();
8790 Linkage = llvm::GlobalValue::WeakAnyLinkage;
8791 break;
8792 }
8793 OffloadEntriesInfoManager.registerDeviceGlobalVarEntryInfo(
8794 VarName, Addr, VarSize, Flags, Linkage);
Alexey Bataev03f270c2018-03-30 18:31:07 +00008795}
8796
Samuel Antaoee8fb302016-01-06 13:42:12 +00008797bool CGOpenMPRuntime::emitTargetGlobal(GlobalDecl GD) {
Alexey Bataeve6aa4692018-09-13 16:54:05 +00008798 if (isa<FunctionDecl>(GD.getDecl()) ||
8799 isa<OMPDeclareReductionDecl>(GD.getDecl()))
Samuel Antaoee8fb302016-01-06 13:42:12 +00008800 return emitTargetFunctions(GD);
8801
8802 return emitTargetGlobalVariable(GD);
8803}
8804
Alexey Bataevbf8fe712018-08-07 16:14:36 +00008805void CGOpenMPRuntime::emitDeferredTargetDecls() const {
8806 for (const VarDecl *VD : DeferredGlobalVariables) {
8807 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00008808 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataevd01b7492018-08-15 19:45:12 +00008809 if (!Res)
8810 continue;
8811 if (*Res == OMPDeclareTargetDeclAttr::MT_To) {
Alexey Bataevbf8fe712018-08-07 16:14:36 +00008812 CGM.EmitGlobal(VD);
Alexey Bataevd01b7492018-08-15 19:45:12 +00008813 } else {
8814 assert(*Res == OMPDeclareTargetDeclAttr::MT_Link &&
8815 "Expected to or link clauses.");
8816 (void)CGM.getOpenMPRuntime().getAddrOfDeclareTargetLink(VD);
Alexey Bataevbf8fe712018-08-07 16:14:36 +00008817 }
8818 }
8819}
8820
Alexey Bataev60705422018-10-30 15:50:12 +00008821void CGOpenMPRuntime::adjustTargetSpecificDataForLambdas(
8822 CodeGenFunction &CGF, const OMPExecutableDirective &D) const {
8823 assert(isOpenMPTargetExecutionDirective(D.getDirectiveKind()) &&
8824 " Expected target-based directive.");
8825}
8826
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00008827CGOpenMPRuntime::DisableAutoDeclareTargetRAII::DisableAutoDeclareTargetRAII(
8828 CodeGenModule &CGM)
8829 : CGM(CGM) {
8830 if (CGM.getLangOpts().OpenMPIsDevice) {
8831 SavedShouldMarkAsGlobal = CGM.getOpenMPRuntime().ShouldMarkAsGlobal;
8832 CGM.getOpenMPRuntime().ShouldMarkAsGlobal = false;
8833 }
8834}
8835
8836CGOpenMPRuntime::DisableAutoDeclareTargetRAII::~DisableAutoDeclareTargetRAII() {
8837 if (CGM.getLangOpts().OpenMPIsDevice)
8838 CGM.getOpenMPRuntime().ShouldMarkAsGlobal = SavedShouldMarkAsGlobal;
8839}
8840
Alexey Bataev6d944102018-05-02 15:45:28 +00008841bool CGOpenMPRuntime::markAsGlobalTarget(GlobalDecl GD) {
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00008842 if (!CGM.getLangOpts().OpenMPIsDevice || !ShouldMarkAsGlobal)
8843 return true;
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00008844
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00008845 StringRef Name = CGM.getMangledName(GD);
Alexey Bataev6d944102018-05-02 15:45:28 +00008846 const auto *D = cast<FunctionDecl>(GD.getDecl());
Alexey Bataev34f8a702018-03-28 14:28:54 +00008847 // Do not to emit function if it is marked as declare target as it was already
8848 // emitted.
Alexey Bataev97b72212018-08-14 18:31:20 +00008849 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(D)) {
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00008850 if (D->hasBody() && AlreadyEmittedTargetFunctions.count(Name) == 0) {
8851 if (auto *F = dyn_cast_or_null<llvm::Function>(CGM.GetGlobalValue(Name)))
Alexey Bataev34f8a702018-03-28 14:28:54 +00008852 return !F->isDeclaration();
8853 return false;
8854 }
8855 return true;
8856 }
8857
Alexey Bataev2a6f3f52018-11-07 19:11:14 +00008858 return !AlreadyEmittedTargetFunctions.insert(Name).second;
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00008859}
8860
Samuel Antaoee8fb302016-01-06 13:42:12 +00008861llvm::Function *CGOpenMPRuntime::emitRegistrationFunction() {
8862 // If we have offloading in the current module, we need to emit the entries
8863 // now and register the offloading descriptor.
8864 createOffloadEntriesAndInfoMetadata();
8865
8866 // Create and register the offloading binary descriptors. This is the main
8867 // entity that captures all the information about offloading in the current
8868 // compilation unit.
8869 return createOffloadingBinaryDescriptorRegistration();
8870}
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00008871
8872void CGOpenMPRuntime::emitTeamsCall(CodeGenFunction &CGF,
8873 const OMPExecutableDirective &D,
8874 SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00008875 llvm::Function *OutlinedFn,
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00008876 ArrayRef<llvm::Value *> CapturedVars) {
8877 if (!CGF.HaveInsertPoint())
8878 return;
8879
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008880 llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00008881 CodeGenFunction::RunCleanupsScope Scope(CGF);
8882
8883 // Build call __kmpc_fork_teams(loc, n, microtask, var1, .., varn);
8884 llvm::Value *Args[] = {
8885 RTLoc,
8886 CGF.Builder.getInt32(CapturedVars.size()), // Number of captured vars
8887 CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy())};
8888 llvm::SmallVector<llvm::Value *, 16> RealArgs;
8889 RealArgs.append(std::begin(Args), std::end(Args));
8890 RealArgs.append(CapturedVars.begin(), CapturedVars.end());
8891
James Y Knight9871db02019-02-05 16:42:33 +00008892 llvm::FunctionCallee RTLFn = createRuntimeFunction(OMPRTL__kmpc_fork_teams);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00008893 CGF.EmitRuntimeCall(RTLFn, RealArgs);
8894}
8895
8896void CGOpenMPRuntime::emitNumTeamsClause(CodeGenFunction &CGF,
Carlo Bertollic6872252016-04-04 15:55:02 +00008897 const Expr *NumTeams,
8898 const Expr *ThreadLimit,
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00008899 SourceLocation Loc) {
8900 if (!CGF.HaveInsertPoint())
8901 return;
8902
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008903 llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00008904
Carlo Bertollic6872252016-04-04 15:55:02 +00008905 llvm::Value *NumTeamsVal =
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008906 NumTeams
Carlo Bertollic6872252016-04-04 15:55:02 +00008907 ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(NumTeams),
8908 CGF.CGM.Int32Ty, /* isSigned = */ true)
8909 : CGF.Builder.getInt32(0);
8910
8911 llvm::Value *ThreadLimitVal =
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008912 ThreadLimit
Carlo Bertollic6872252016-04-04 15:55:02 +00008913 ? CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(ThreadLimit),
8914 CGF.CGM.Int32Ty, /* isSigned = */ true)
8915 : CGF.Builder.getInt32(0);
8916
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00008917 // Build call __kmpc_push_num_teamss(&loc, global_tid, num_teams, thread_limit)
Carlo Bertollic6872252016-04-04 15:55:02 +00008918 llvm::Value *PushNumTeamsArgs[] = {RTLoc, getThreadID(CGF, Loc), NumTeamsVal,
8919 ThreadLimitVal};
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00008920 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_teams),
8921 PushNumTeamsArgs);
8922}
Samuel Antaodf158d52016-04-27 22:58:19 +00008923
Samuel Antaocc10b852016-07-28 14:23:26 +00008924void CGOpenMPRuntime::emitTargetDataCalls(
8925 CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
8926 const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) {
Samuel Antaodf158d52016-04-27 22:58:19 +00008927 if (!CGF.HaveInsertPoint())
8928 return;
8929
Samuel Antaocc10b852016-07-28 14:23:26 +00008930 // Action used to replace the default codegen action and turn privatization
8931 // off.
8932 PrePostActionTy NoPrivAction;
Samuel Antaodf158d52016-04-27 22:58:19 +00008933
8934 // Generate the code for the opening of the data environment. Capture all the
8935 // arguments of the runtime call by reference because they are used in the
8936 // closing of the region.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008937 auto &&BeginThenGen = [this, &D, Device, &Info,
8938 &CodeGen](CodeGenFunction &CGF, PrePostActionTy &) {
Samuel Antaodf158d52016-04-27 22:58:19 +00008939 // Fill up the arrays with all the mapped variables.
Samuel Antaocc10b852016-07-28 14:23:26 +00008940 MappableExprsHandler::MapBaseValuesArrayTy BasePointers;
Samuel Antaodf158d52016-04-27 22:58:19 +00008941 MappableExprsHandler::MapValuesArrayTy Pointers;
8942 MappableExprsHandler::MapValuesArrayTy Sizes;
8943 MappableExprsHandler::MapFlagsArrayTy MapTypes;
8944
8945 // Get map clause information.
8946 MappableExprsHandler MCHandler(D, CGF);
8947 MCHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes);
Samuel Antaodf158d52016-04-27 22:58:19 +00008948
8949 // Fill up the arrays and create the arguments.
Samuel Antaocc10b852016-07-28 14:23:26 +00008950 emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info);
Samuel Antaodf158d52016-04-27 22:58:19 +00008951
8952 llvm::Value *BasePointersArrayArg = nullptr;
8953 llvm::Value *PointersArrayArg = nullptr;
8954 llvm::Value *SizesArrayArg = nullptr;
8955 llvm::Value *MapTypesArrayArg = nullptr;
8956 emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg,
Samuel Antaocc10b852016-07-28 14:23:26 +00008957 SizesArrayArg, MapTypesArrayArg, Info);
Samuel Antaodf158d52016-04-27 22:58:19 +00008958
8959 // Emit device ID if any.
8960 llvm::Value *DeviceID = nullptr;
George Rokos63bc9d62017-11-21 18:25:12 +00008961 if (Device) {
Samuel Antaodf158d52016-04-27 22:58:19 +00008962 DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
George Rokos63bc9d62017-11-21 18:25:12 +00008963 CGF.Int64Ty, /*isSigned=*/true);
8964 } else {
8965 DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF);
8966 }
Samuel Antaodf158d52016-04-27 22:58:19 +00008967
8968 // Emit the number of elements in the offloading arrays.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00008969 llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs);
Samuel Antaodf158d52016-04-27 22:58:19 +00008970
8971 llvm::Value *OffloadingArgs[] = {
8972 DeviceID, PointerNum, BasePointersArrayArg,
8973 PointersArrayArg, SizesArrayArg, MapTypesArrayArg};
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008974 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_begin),
Samuel Antaodf158d52016-04-27 22:58:19 +00008975 OffloadingArgs);
Samuel Antaocc10b852016-07-28 14:23:26 +00008976
8977 // If device pointer privatization is required, emit the body of the region
8978 // here. It will have to be duplicated: with and without privatization.
8979 if (!Info.CaptureDeviceAddrMap.empty())
8980 CodeGen(CGF);
Samuel Antaodf158d52016-04-27 22:58:19 +00008981 };
8982
8983 // Generate code for the closing of the data region.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008984 auto &&EndThenGen = [this, Device, &Info](CodeGenFunction &CGF,
8985 PrePostActionTy &) {
Samuel Antaocc10b852016-07-28 14:23:26 +00008986 assert(Info.isValid() && "Invalid data environment closing arguments.");
Samuel Antaodf158d52016-04-27 22:58:19 +00008987
8988 llvm::Value *BasePointersArrayArg = nullptr;
8989 llvm::Value *PointersArrayArg = nullptr;
8990 llvm::Value *SizesArrayArg = nullptr;
8991 llvm::Value *MapTypesArrayArg = nullptr;
8992 emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg,
Samuel Antaocc10b852016-07-28 14:23:26 +00008993 SizesArrayArg, MapTypesArrayArg, Info);
Samuel Antaodf158d52016-04-27 22:58:19 +00008994
8995 // Emit device ID if any.
8996 llvm::Value *DeviceID = nullptr;
George Rokos63bc9d62017-11-21 18:25:12 +00008997 if (Device) {
Samuel Antaodf158d52016-04-27 22:58:19 +00008998 DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
George Rokos63bc9d62017-11-21 18:25:12 +00008999 CGF.Int64Ty, /*isSigned=*/true);
9000 } else {
9001 DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF);
9002 }
Samuel Antaodf158d52016-04-27 22:58:19 +00009003
9004 // Emit the number of elements in the offloading arrays.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009005 llvm::Value *PointerNum = CGF.Builder.getInt32(Info.NumberOfPtrs);
Samuel Antaodf158d52016-04-27 22:58:19 +00009006
9007 llvm::Value *OffloadingArgs[] = {
9008 DeviceID, PointerNum, BasePointersArrayArg,
9009 PointersArrayArg, SizesArrayArg, MapTypesArrayArg};
Alexey Bataevd2202ca2017-12-27 17:58:32 +00009010 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__tgt_target_data_end),
Samuel Antaodf158d52016-04-27 22:58:19 +00009011 OffloadingArgs);
9012 };
9013
Samuel Antaocc10b852016-07-28 14:23:26 +00009014 // If we need device pointer privatization, we need to emit the body of the
9015 // region with no privatization in the 'else' branch of the conditional.
9016 // Otherwise, we don't have to do anything.
9017 auto &&BeginElseGen = [&Info, &CodeGen, &NoPrivAction](CodeGenFunction &CGF,
9018 PrePostActionTy &) {
9019 if (!Info.CaptureDeviceAddrMap.empty()) {
9020 CodeGen.setAction(NoPrivAction);
9021 CodeGen(CGF);
9022 }
9023 };
9024
9025 // We don't have to do anything to close the region if the if clause evaluates
9026 // to false.
9027 auto &&EndElseGen = [](CodeGenFunction &CGF, PrePostActionTy &) {};
Samuel Antaodf158d52016-04-27 22:58:19 +00009028
9029 if (IfCond) {
Samuel Antaocc10b852016-07-28 14:23:26 +00009030 emitOMPIfClause(CGF, IfCond, BeginThenGen, BeginElseGen);
Samuel Antaodf158d52016-04-27 22:58:19 +00009031 } else {
Samuel Antaocc10b852016-07-28 14:23:26 +00009032 RegionCodeGenTy RCG(BeginThenGen);
9033 RCG(CGF);
Samuel Antaodf158d52016-04-27 22:58:19 +00009034 }
9035
Samuel Antaocc10b852016-07-28 14:23:26 +00009036 // If we don't require privatization of device pointers, we emit the body in
9037 // between the runtime calls. This avoids duplicating the body code.
9038 if (Info.CaptureDeviceAddrMap.empty()) {
9039 CodeGen.setAction(NoPrivAction);
9040 CodeGen(CGF);
9041 }
Samuel Antaodf158d52016-04-27 22:58:19 +00009042
9043 if (IfCond) {
Samuel Antaocc10b852016-07-28 14:23:26 +00009044 emitOMPIfClause(CGF, IfCond, EndThenGen, EndElseGen);
Samuel Antaodf158d52016-04-27 22:58:19 +00009045 } else {
Samuel Antaocc10b852016-07-28 14:23:26 +00009046 RegionCodeGenTy RCG(EndThenGen);
9047 RCG(CGF);
Samuel Antaodf158d52016-04-27 22:58:19 +00009048 }
9049}
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00009050
Samuel Antao8d2d7302016-05-26 18:30:22 +00009051void CGOpenMPRuntime::emitTargetDataStandAloneCall(
Samuel Antao8dd66282016-04-27 23:14:30 +00009052 CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
9053 const Expr *Device) {
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00009054 if (!CGF.HaveInsertPoint())
9055 return;
9056
Samuel Antao8dd66282016-04-27 23:14:30 +00009057 assert((isa<OMPTargetEnterDataDirective>(D) ||
Samuel Antao8d2d7302016-05-26 18:30:22 +00009058 isa<OMPTargetExitDataDirective>(D) ||
9059 isa<OMPTargetUpdateDirective>(D)) &&
9060 "Expecting either target enter, exit data, or update directives.");
Samuel Antao8dd66282016-04-27 23:14:30 +00009061
Alexey Bataevd2202ca2017-12-27 17:58:32 +00009062 CodeGenFunction::OMPTargetDataInfo InputInfo;
9063 llvm::Value *MapTypesArray = nullptr;
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00009064 // Generate the code for the opening of the data environment.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00009065 auto &&ThenGen = [this, &D, Device, &InputInfo,
9066 &MapTypesArray](CodeGenFunction &CGF, PrePostActionTy &) {
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00009067 // Emit device ID if any.
9068 llvm::Value *DeviceID = nullptr;
George Rokos63bc9d62017-11-21 18:25:12 +00009069 if (Device) {
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00009070 DeviceID = CGF.Builder.CreateIntCast(CGF.EmitScalarExpr(Device),
George Rokos63bc9d62017-11-21 18:25:12 +00009071 CGF.Int64Ty, /*isSigned=*/true);
9072 } else {
9073 DeviceID = CGF.Builder.getInt64(OMP_DEVICEID_UNDEF);
9074 }
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00009075
9076 // Emit the number of elements in the offloading arrays.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00009077 llvm::Constant *PointerNum =
9078 CGF.Builder.getInt32(InputInfo.NumberOfTargetItems);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00009079
Alexey Bataevd2202ca2017-12-27 17:58:32 +00009080 llvm::Value *OffloadingArgs[] = {DeviceID,
9081 PointerNum,
9082 InputInfo.BasePointersArray.getPointer(),
9083 InputInfo.PointersArray.getPointer(),
9084 InputInfo.SizesArray.getPointer(),
9085 MapTypesArray};
Samuel Antao8d2d7302016-05-26 18:30:22 +00009086
Samuel Antao8d2d7302016-05-26 18:30:22 +00009087 // Select the right runtime function call for each expected standalone
9088 // directive.
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +00009089 const bool HasNowait = D.hasClausesOfKind<OMPNowaitClause>();
Samuel Antao8d2d7302016-05-26 18:30:22 +00009090 OpenMPRTLFunction RTLFn;
9091 switch (D.getDirectiveKind()) {
Samuel Antao8d2d7302016-05-26 18:30:22 +00009092 case OMPD_target_enter_data:
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +00009093 RTLFn = HasNowait ? OMPRTL__tgt_target_data_begin_nowait
9094 : OMPRTL__tgt_target_data_begin;
Samuel Antao8d2d7302016-05-26 18:30:22 +00009095 break;
9096 case OMPD_target_exit_data:
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +00009097 RTLFn = HasNowait ? OMPRTL__tgt_target_data_end_nowait
9098 : OMPRTL__tgt_target_data_end;
Samuel Antao8d2d7302016-05-26 18:30:22 +00009099 break;
9100 case OMPD_target_update:
Alexey Bataev0cc6b8e2017-12-14 17:00:17 +00009101 RTLFn = HasNowait ? OMPRTL__tgt_target_data_update_nowait
9102 : OMPRTL__tgt_target_data_update;
Samuel Antao8d2d7302016-05-26 18:30:22 +00009103 break;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009104 case OMPD_parallel:
9105 case OMPD_for:
9106 case OMPD_parallel_for:
9107 case OMPD_parallel_sections:
9108 case OMPD_for_simd:
9109 case OMPD_parallel_for_simd:
9110 case OMPD_cancel:
9111 case OMPD_cancellation_point:
9112 case OMPD_ordered:
9113 case OMPD_threadprivate:
9114 case OMPD_task:
9115 case OMPD_simd:
9116 case OMPD_sections:
9117 case OMPD_section:
9118 case OMPD_single:
9119 case OMPD_master:
9120 case OMPD_critical:
9121 case OMPD_taskyield:
9122 case OMPD_barrier:
9123 case OMPD_taskwait:
9124 case OMPD_taskgroup:
9125 case OMPD_atomic:
9126 case OMPD_flush:
9127 case OMPD_teams:
9128 case OMPD_target_data:
9129 case OMPD_distribute:
9130 case OMPD_distribute_simd:
9131 case OMPD_distribute_parallel_for:
9132 case OMPD_distribute_parallel_for_simd:
9133 case OMPD_teams_distribute:
9134 case OMPD_teams_distribute_simd:
9135 case OMPD_teams_distribute_parallel_for:
9136 case OMPD_teams_distribute_parallel_for_simd:
9137 case OMPD_declare_simd:
9138 case OMPD_declare_target:
9139 case OMPD_end_declare_target:
9140 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00009141 case OMPD_declare_mapper:
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009142 case OMPD_taskloop:
9143 case OMPD_taskloop_simd:
9144 case OMPD_target:
9145 case OMPD_target_simd:
9146 case OMPD_target_teams_distribute:
9147 case OMPD_target_teams_distribute_simd:
9148 case OMPD_target_teams_distribute_parallel_for:
9149 case OMPD_target_teams_distribute_parallel_for_simd:
9150 case OMPD_target_teams:
9151 case OMPD_target_parallel:
9152 case OMPD_target_parallel_for:
9153 case OMPD_target_parallel_for_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00009154 case OMPD_requires:
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009155 case OMPD_unknown:
9156 llvm_unreachable("Unexpected standalone target data directive.");
9157 break;
Samuel Antao8d2d7302016-05-26 18:30:22 +00009158 }
Alexey Bataevd2202ca2017-12-27 17:58:32 +00009159 CGF.EmitRuntimeCall(createRuntimeFunction(RTLFn), OffloadingArgs);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00009160 };
9161
Alexey Bataevd2202ca2017-12-27 17:58:32 +00009162 auto &&TargetThenGen = [this, &ThenGen, &D, &InputInfo, &MapTypesArray](
9163 CodeGenFunction &CGF, PrePostActionTy &) {
9164 // Fill up the arrays with all the mapped variables.
9165 MappableExprsHandler::MapBaseValuesArrayTy BasePointers;
9166 MappableExprsHandler::MapValuesArrayTy Pointers;
9167 MappableExprsHandler::MapValuesArrayTy Sizes;
9168 MappableExprsHandler::MapFlagsArrayTy MapTypes;
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00009169
Alexey Bataevd2202ca2017-12-27 17:58:32 +00009170 // Get map clause information.
9171 MappableExprsHandler MEHandler(D, CGF);
9172 MEHandler.generateAllInfo(BasePointers, Pointers, Sizes, MapTypes);
9173
9174 TargetDataInfo Info;
9175 // Fill up the arrays and create the arguments.
9176 emitOffloadingArrays(CGF, BasePointers, Pointers, Sizes, MapTypes, Info);
9177 emitOffloadingArraysArgument(CGF, Info.BasePointersArray,
9178 Info.PointersArray, Info.SizesArray,
9179 Info.MapTypesArray, Info);
9180 InputInfo.NumberOfTargetItems = Info.NumberOfPtrs;
9181 InputInfo.BasePointersArray =
9182 Address(Info.BasePointersArray, CGM.getPointerAlign());
9183 InputInfo.PointersArray =
9184 Address(Info.PointersArray, CGM.getPointerAlign());
9185 InputInfo.SizesArray =
9186 Address(Info.SizesArray, CGM.getPointerAlign());
9187 MapTypesArray = Info.MapTypesArray;
9188 if (D.hasClausesOfKind<OMPDependClause>())
9189 CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo);
9190 else
Alexey Bataev768f1f22018-01-09 19:59:25 +00009191 emitInlinedDirective(CGF, D.getDirectiveKind(), ThenGen);
Alexey Bataevd2202ca2017-12-27 17:58:32 +00009192 };
9193
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009194 if (IfCond) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00009195 emitOMPIfClause(CGF, IfCond, TargetThenGen,
9196 [](CodeGenFunction &CGF, PrePostActionTy &) {});
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009197 } else {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00009198 RegionCodeGenTy ThenRCG(TargetThenGen);
9199 ThenRCG(CGF);
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00009200 }
9201}
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009202
9203namespace {
9204 /// Kind of parameter in a function with 'declare simd' directive.
9205 enum ParamKindTy { LinearWithVarStride, Linear, Uniform, Vector };
9206 /// Attribute set of the parameter.
9207 struct ParamAttrTy {
9208 ParamKindTy Kind = Vector;
9209 llvm::APSInt StrideOrArg;
9210 llvm::APSInt Alignment;
9211 };
9212} // namespace
9213
9214static unsigned evaluateCDTSize(const FunctionDecl *FD,
9215 ArrayRef<ParamAttrTy> ParamAttrs) {
9216 // Every vector variant of a SIMD-enabled function has a vector length (VLEN).
9217 // If OpenMP clause "simdlen" is used, the VLEN is the value of the argument
9218 // of that clause. The VLEN value must be power of 2.
9219 // In other case the notion of the function`s "characteristic data type" (CDT)
9220 // is used to compute the vector length.
9221 // CDT is defined in the following order:
9222 // a) For non-void function, the CDT is the return type.
9223 // b) If the function has any non-uniform, non-linear parameters, then the
9224 // CDT is the type of the first such parameter.
9225 // c) If the CDT determined by a) or b) above is struct, union, or class
9226 // type which is pass-by-value (except for the type that maps to the
9227 // built-in complex data type), the characteristic data type is int.
9228 // d) If none of the above three cases is applicable, the CDT is int.
9229 // The VLEN is then determined based on the CDT and the size of vector
9230 // register of that ISA for which current vector version is generated. The
9231 // VLEN is computed using the formula below:
9232 // VLEN = sizeof(vector_register) / sizeof(CDT),
9233 // where vector register size specified in section 3.2.1 Registers and the
9234 // Stack Frame of original AMD64 ABI document.
9235 QualType RetType = FD->getReturnType();
9236 if (RetType.isNull())
9237 return 0;
9238 ASTContext &C = FD->getASTContext();
9239 QualType CDT;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009240 if (!RetType.isNull() && !RetType->isVoidType()) {
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009241 CDT = RetType;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009242 } else {
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009243 unsigned Offset = 0;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009244 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009245 if (ParamAttrs[Offset].Kind == Vector)
9246 CDT = C.getPointerType(C.getRecordType(MD->getParent()));
9247 ++Offset;
9248 }
9249 if (CDT.isNull()) {
9250 for (unsigned I = 0, E = FD->getNumParams(); I < E; ++I) {
9251 if (ParamAttrs[I + Offset].Kind == Vector) {
9252 CDT = FD->getParamDecl(I)->getType();
9253 break;
9254 }
9255 }
9256 }
9257 }
9258 if (CDT.isNull())
9259 CDT = C.IntTy;
9260 CDT = CDT->getCanonicalTypeUnqualified();
9261 if (CDT->isRecordType() || CDT->isUnionType())
9262 CDT = C.IntTy;
9263 return C.getTypeSize(CDT);
9264}
9265
9266static void
9267emitX86DeclareSimdFunction(const FunctionDecl *FD, llvm::Function *Fn,
Benjamin Kramer81cb4b72016-11-24 16:01:20 +00009268 const llvm::APSInt &VLENVal,
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009269 ArrayRef<ParamAttrTy> ParamAttrs,
9270 OMPDeclareSimdDeclAttr::BranchStateTy State) {
9271 struct ISADataTy {
9272 char ISA;
9273 unsigned VecRegSize;
9274 };
9275 ISADataTy ISAData[] = {
9276 {
9277 'b', 128
9278 }, // SSE
9279 {
9280 'c', 256
9281 }, // AVX
9282 {
9283 'd', 256
9284 }, // AVX2
9285 {
9286 'e', 512
9287 }, // AVX512
9288 };
9289 llvm::SmallVector<char, 2> Masked;
9290 switch (State) {
9291 case OMPDeclareSimdDeclAttr::BS_Undefined:
9292 Masked.push_back('N');
9293 Masked.push_back('M');
9294 break;
9295 case OMPDeclareSimdDeclAttr::BS_Notinbranch:
9296 Masked.push_back('N');
9297 break;
9298 case OMPDeclareSimdDeclAttr::BS_Inbranch:
9299 Masked.push_back('M');
9300 break;
9301 }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009302 for (char Mask : Masked) {
9303 for (const ISADataTy &Data : ISAData) {
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009304 SmallString<256> Buffer;
9305 llvm::raw_svector_ostream Out(Buffer);
9306 Out << "_ZGV" << Data.ISA << Mask;
9307 if (!VLENVal) {
9308 Out << llvm::APSInt::getUnsigned(Data.VecRegSize /
9309 evaluateCDTSize(FD, ParamAttrs));
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009310 } else {
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009311 Out << VLENVal;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009312 }
9313 for (const ParamAttrTy &ParamAttr : ParamAttrs) {
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009314 switch (ParamAttr.Kind){
9315 case LinearWithVarStride:
9316 Out << 's' << ParamAttr.StrideOrArg;
9317 break;
9318 case Linear:
9319 Out << 'l';
9320 if (!!ParamAttr.StrideOrArg)
9321 Out << ParamAttr.StrideOrArg;
9322 break;
9323 case Uniform:
9324 Out << 'u';
9325 break;
9326 case Vector:
9327 Out << 'v';
9328 break;
9329 }
9330 if (!!ParamAttr.Alignment)
9331 Out << 'a' << ParamAttr.Alignment;
9332 }
9333 Out << '_' << Fn->getName();
9334 Fn->addFnAttr(Out.str());
9335 }
9336 }
9337}
9338
9339void CGOpenMPRuntime::emitDeclareSimdFunction(const FunctionDecl *FD,
9340 llvm::Function *Fn) {
9341 ASTContext &C = CGM.getContext();
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009342 FD = FD->getMostRecentDecl();
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009343 // Map params to their positions in function decl.
9344 llvm::DenseMap<const Decl *, unsigned> ParamPositions;
9345 if (isa<CXXMethodDecl>(FD))
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009346 ParamPositions.try_emplace(FD, 0);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009347 unsigned ParamPos = ParamPositions.size();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009348 for (const ParmVarDecl *P : FD->parameters()) {
9349 ParamPositions.try_emplace(P->getCanonicalDecl(), ParamPos);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009350 ++ParamPos;
9351 }
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009352 while (FD) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009353 for (const auto *Attr : FD->specific_attrs<OMPDeclareSimdDeclAttr>()) {
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009354 llvm::SmallVector<ParamAttrTy, 8> ParamAttrs(ParamPositions.size());
9355 // Mark uniform parameters.
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009356 for (const Expr *E : Attr->uniforms()) {
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009357 E = E->IgnoreParenImpCasts();
9358 unsigned Pos;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009359 if (isa<CXXThisExpr>(E)) {
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009360 Pos = ParamPositions[FD];
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009361 } else {
9362 const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl())
9363 ->getCanonicalDecl();
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009364 Pos = ParamPositions[PVD];
9365 }
9366 ParamAttrs[Pos].Kind = Uniform;
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009367 }
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009368 // Get alignment info.
9369 auto NI = Attr->alignments_begin();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009370 for (const Expr *E : Attr->aligneds()) {
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009371 E = E->IgnoreParenImpCasts();
9372 unsigned Pos;
9373 QualType ParmTy;
9374 if (isa<CXXThisExpr>(E)) {
9375 Pos = ParamPositions[FD];
9376 ParmTy = E->getType();
9377 } else {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009378 const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl())
9379 ->getCanonicalDecl();
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009380 Pos = ParamPositions[PVD];
9381 ParmTy = PVD->getType();
9382 }
9383 ParamAttrs[Pos].Alignment =
9384 (*NI)
9385 ? (*NI)->EvaluateKnownConstInt(C)
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009386 : llvm::APSInt::getUnsigned(
9387 C.toCharUnitsFromBits(C.getOpenMPDefaultSimdAlign(ParmTy))
9388 .getQuantity());
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009389 ++NI;
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009390 }
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009391 // Mark linear parameters.
9392 auto SI = Attr->steps_begin();
9393 auto MI = Attr->modifiers_begin();
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009394 for (const Expr *E : Attr->linears()) {
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009395 E = E->IgnoreParenImpCasts();
9396 unsigned Pos;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009397 if (isa<CXXThisExpr>(E)) {
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009398 Pos = ParamPositions[FD];
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009399 } else {
9400 const auto *PVD = cast<ParmVarDecl>(cast<DeclRefExpr>(E)->getDecl())
9401 ->getCanonicalDecl();
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009402 Pos = ParamPositions[PVD];
9403 }
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009404 ParamAttrTy &ParamAttr = ParamAttrs[Pos];
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009405 ParamAttr.Kind = Linear;
9406 if (*SI) {
Fangrui Song407659a2018-11-30 23:41:18 +00009407 Expr::EvalResult Result;
9408 if (!(*SI)->EvaluateAsInt(Result, C, Expr::SE_AllowSideEffects)) {
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009409 if (const auto *DRE =
9410 cast<DeclRefExpr>((*SI)->IgnoreParenImpCasts())) {
9411 if (const auto *StridePVD = cast<ParmVarDecl>(DRE->getDecl())) {
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009412 ParamAttr.Kind = LinearWithVarStride;
9413 ParamAttr.StrideOrArg = llvm::APSInt::getUnsigned(
9414 ParamPositions[StridePVD->getCanonicalDecl()]);
9415 }
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009416 }
Fangrui Song407659a2018-11-30 23:41:18 +00009417 } else {
9418 ParamAttr.StrideOrArg = Result.Val.getInt();
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009419 }
9420 }
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009421 ++SI;
9422 ++MI;
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009423 }
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009424 llvm::APSInt VLENVal;
9425 if (const Expr *VLEN = Attr->getSimdlen())
9426 VLENVal = VLEN->EvaluateKnownConstInt(C);
9427 OMPDeclareSimdDeclAttr::BranchStateTy State = Attr->getBranchState();
9428 if (CGM.getTriple().getArch() == llvm::Triple::x86 ||
9429 CGM.getTriple().getArch() == llvm::Triple::x86_64)
9430 emitX86DeclareSimdFunction(FD, Fn, VLENVal, ParamAttrs, State);
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009431 }
Alexey Bataev20cf67c2018-03-02 18:07:00 +00009432 FD = FD->getPreviousDecl();
Alexey Bataevc7a82b42016-05-06 09:40:08 +00009433 }
9434}
Alexey Bataev8b427062016-05-25 12:36:08 +00009435
9436namespace {
9437/// Cleanup action for doacross support.
9438class DoacrossCleanupTy final : public EHScopeStack::Cleanup {
9439public:
9440 static const int DoacrossFinArgs = 2;
9441
9442private:
James Y Knight9871db02019-02-05 16:42:33 +00009443 llvm::FunctionCallee RTLFn;
Alexey Bataev8b427062016-05-25 12:36:08 +00009444 llvm::Value *Args[DoacrossFinArgs];
9445
9446public:
James Y Knight9871db02019-02-05 16:42:33 +00009447 DoacrossCleanupTy(llvm::FunctionCallee RTLFn,
9448 ArrayRef<llvm::Value *> CallArgs)
Alexey Bataev8b427062016-05-25 12:36:08 +00009449 : RTLFn(RTLFn) {
9450 assert(CallArgs.size() == DoacrossFinArgs);
9451 std::copy(CallArgs.begin(), CallArgs.end(), std::begin(Args));
9452 }
9453 void Emit(CodeGenFunction &CGF, Flags /*flags*/) override {
9454 if (!CGF.HaveInsertPoint())
9455 return;
9456 CGF.EmitRuntimeCall(RTLFn, Args);
9457 }
9458};
9459} // namespace
9460
9461void CGOpenMPRuntime::emitDoacrossInit(CodeGenFunction &CGF,
Alexey Bataevf138fda2018-08-13 19:04:24 +00009462 const OMPLoopDirective &D,
9463 ArrayRef<Expr *> NumIterations) {
Alexey Bataev8b427062016-05-25 12:36:08 +00009464 if (!CGF.HaveInsertPoint())
9465 return;
9466
9467 ASTContext &C = CGM.getContext();
9468 QualType Int64Ty = C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/true);
9469 RecordDecl *RD;
9470 if (KmpDimTy.isNull()) {
9471 // Build struct kmp_dim { // loop bounds info casted to kmp_int64
9472 // kmp_int64 lo; // lower
9473 // kmp_int64 up; // upper
9474 // kmp_int64 st; // stride
9475 // };
9476 RD = C.buildImplicitRecord("kmp_dim");
9477 RD->startDefinition();
9478 addFieldToRecordDecl(C, RD, Int64Ty);
9479 addFieldToRecordDecl(C, RD, Int64Ty);
9480 addFieldToRecordDecl(C, RD, Int64Ty);
9481 RD->completeDefinition();
9482 KmpDimTy = C.getRecordType(RD);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009483 } else {
Alexey Bataev8b427062016-05-25 12:36:08 +00009484 RD = cast<RecordDecl>(KmpDimTy->getAsTagDecl());
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009485 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00009486 llvm::APInt Size(/*numBits=*/32, NumIterations.size());
9487 QualType ArrayTy =
9488 C.getConstantArrayType(KmpDimTy, Size, ArrayType::Normal, 0);
Alexey Bataev8b427062016-05-25 12:36:08 +00009489
Alexey Bataevf138fda2018-08-13 19:04:24 +00009490 Address DimsAddr = CGF.CreateMemTemp(ArrayTy, "dims");
9491 CGF.EmitNullInitialization(DimsAddr, ArrayTy);
Alexey Bataev8b427062016-05-25 12:36:08 +00009492 enum { LowerFD = 0, UpperFD, StrideFD };
9493 // Fill dims with data.
Alexey Bataevf138fda2018-08-13 19:04:24 +00009494 for (unsigned I = 0, E = NumIterations.size(); I < E; ++I) {
9495 LValue DimsLVal =
9496 CGF.MakeAddrLValue(CGF.Builder.CreateConstArrayGEP(
9497 DimsAddr, I, C.getTypeSizeInChars(KmpDimTy)),
9498 KmpDimTy);
9499 // dims.upper = num_iterations;
9500 LValue UpperLVal = CGF.EmitLValueForField(
9501 DimsLVal, *std::next(RD->field_begin(), UpperFD));
9502 llvm::Value *NumIterVal =
9503 CGF.EmitScalarConversion(CGF.EmitScalarExpr(NumIterations[I]),
9504 D.getNumIterations()->getType(), Int64Ty,
9505 D.getNumIterations()->getExprLoc());
9506 CGF.EmitStoreOfScalar(NumIterVal, UpperLVal);
9507 // dims.stride = 1;
9508 LValue StrideLVal = CGF.EmitLValueForField(
9509 DimsLVal, *std::next(RD->field_begin(), StrideFD));
9510 CGF.EmitStoreOfScalar(llvm::ConstantInt::getSigned(CGM.Int64Ty, /*V=*/1),
9511 StrideLVal);
9512 }
Alexey Bataev8b427062016-05-25 12:36:08 +00009513
9514 // Build call void __kmpc_doacross_init(ident_t *loc, kmp_int32 gtid,
9515 // kmp_int32 num_dims, struct kmp_dim * dims);
Alexey Bataevf138fda2018-08-13 19:04:24 +00009516 llvm::Value *Args[] = {
9517 emitUpdateLocation(CGF, D.getBeginLoc()),
9518 getThreadID(CGF, D.getBeginLoc()),
9519 llvm::ConstantInt::getSigned(CGM.Int32Ty, NumIterations.size()),
9520 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
9521 CGF.Builder
9522 .CreateConstArrayGEP(DimsAddr, 0, C.getTypeSizeInChars(KmpDimTy))
9523 .getPointer(),
9524 CGM.VoidPtrTy)};
Alexey Bataev8b427062016-05-25 12:36:08 +00009525
James Y Knight9871db02019-02-05 16:42:33 +00009526 llvm::FunctionCallee RTLFn =
9527 createRuntimeFunction(OMPRTL__kmpc_doacross_init);
Alexey Bataev8b427062016-05-25 12:36:08 +00009528 CGF.EmitRuntimeCall(RTLFn, Args);
9529 llvm::Value *FiniArgs[DoacrossCleanupTy::DoacrossFinArgs] = {
Stephen Kelly1c301dc2018-08-09 21:09:38 +00009530 emitUpdateLocation(CGF, D.getEndLoc()), getThreadID(CGF, D.getEndLoc())};
James Y Knight9871db02019-02-05 16:42:33 +00009531 llvm::FunctionCallee FiniRTLFn =
9532 createRuntimeFunction(OMPRTL__kmpc_doacross_fini);
Alexey Bataev8b427062016-05-25 12:36:08 +00009533 CGF.EHStack.pushCleanup<DoacrossCleanupTy>(NormalAndEHCleanup, FiniRTLFn,
9534 llvm::makeArrayRef(FiniArgs));
9535}
9536
9537void CGOpenMPRuntime::emitDoacrossOrdered(CodeGenFunction &CGF,
9538 const OMPDependClause *C) {
9539 QualType Int64Ty =
9540 CGM.getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
Alexey Bataevf138fda2018-08-13 19:04:24 +00009541 llvm::APInt Size(/*numBits=*/32, C->getNumLoops());
9542 QualType ArrayTy = CGM.getContext().getConstantArrayType(
9543 Int64Ty, Size, ArrayType::Normal, 0);
9544 Address CntAddr = CGF.CreateMemTemp(ArrayTy, ".cnt.addr");
9545 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) {
9546 const Expr *CounterVal = C->getLoopData(I);
9547 assert(CounterVal);
9548 llvm::Value *CntVal = CGF.EmitScalarConversion(
9549 CGF.EmitScalarExpr(CounterVal), CounterVal->getType(), Int64Ty,
9550 CounterVal->getExprLoc());
9551 CGF.EmitStoreOfScalar(
9552 CntVal,
9553 CGF.Builder.CreateConstArrayGEP(
9554 CntAddr, I, CGM.getContext().getTypeSizeInChars(Int64Ty)),
9555 /*Volatile=*/false, Int64Ty);
9556 }
9557 llvm::Value *Args[] = {
9558 emitUpdateLocation(CGF, C->getBeginLoc()),
9559 getThreadID(CGF, C->getBeginLoc()),
9560 CGF.Builder
9561 .CreateConstArrayGEP(CntAddr, 0,
9562 CGM.getContext().getTypeSizeInChars(Int64Ty))
9563 .getPointer()};
James Y Knight9871db02019-02-05 16:42:33 +00009564 llvm::FunctionCallee RTLFn;
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009565 if (C->getDependencyKind() == OMPC_DEPEND_source) {
Alexey Bataev8b427062016-05-25 12:36:08 +00009566 RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_post);
Alexey Bataeva4fa0b82018-04-16 17:59:34 +00009567 } else {
Alexey Bataev8b427062016-05-25 12:36:08 +00009568 assert(C->getDependencyKind() == OMPC_DEPEND_sink);
9569 RTLFn = createRuntimeFunction(OMPRTL__kmpc_doacross_wait);
9570 }
9571 CGF.EmitRuntimeCall(RTLFn, Args);
9572}
9573
Alexey Bataev7ef47a62018-02-22 18:33:31 +00009574void CGOpenMPRuntime::emitCall(CodeGenFunction &CGF, SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00009575 llvm::FunctionCallee Callee,
Alexey Bataev7ef47a62018-02-22 18:33:31 +00009576 ArrayRef<llvm::Value *> Args) const {
9577 assert(Loc.isValid() && "Outlined function call location must be valid.");
Alexey Bataev3c595a62017-08-14 15:01:03 +00009578 auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc);
9579
James Y Knight9871db02019-02-05 16:42:33 +00009580 if (auto *Fn = dyn_cast<llvm::Function>(Callee.getCallee())) {
Alexey Bataev2c7eee52017-08-04 19:10:54 +00009581 if (Fn->doesNotThrow()) {
Alexey Bataev3c595a62017-08-14 15:01:03 +00009582 CGF.EmitNounwindRuntimeCall(Fn, Args);
Alexey Bataev2c7eee52017-08-04 19:10:54 +00009583 return;
9584 }
9585 }
Alexey Bataev3c595a62017-08-14 15:01:03 +00009586 CGF.EmitRuntimeCall(Callee, Args);
9587}
9588
9589void CGOpenMPRuntime::emitOutlinedFunctionCall(
James Y Knight9871db02019-02-05 16:42:33 +00009590 CodeGenFunction &CGF, SourceLocation Loc, llvm::FunctionCallee OutlinedFn,
Alexey Bataev3c595a62017-08-14 15:01:03 +00009591 ArrayRef<llvm::Value *> Args) const {
Alexey Bataev7ef47a62018-02-22 18:33:31 +00009592 emitCall(CGF, Loc, OutlinedFn, Args);
Alexey Bataev2c7eee52017-08-04 19:10:54 +00009593}
Alexey Bataev3b8d5582017-08-08 18:04:06 +00009594
9595Address CGOpenMPRuntime::getParameterAddress(CodeGenFunction &CGF,
9596 const VarDecl *NativeParam,
9597 const VarDecl *TargetParam) const {
9598 return CGF.GetAddrOfLocalVar(NativeParam);
9599}
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00009600
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00009601Address CGOpenMPRuntime::getAddressOfLocalVariable(CodeGenFunction &CGF,
9602 const VarDecl *VD) {
9603 return Address::invalid();
9604}
9605
James Y Knight9871db02019-02-05 16:42:33 +00009606llvm::Function *CGOpenMPSIMDRuntime::emitParallelOutlinedFunction(
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00009607 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
9608 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
9609 llvm_unreachable("Not supported in SIMD-only mode");
9610}
9611
James Y Knight9871db02019-02-05 16:42:33 +00009612llvm::Function *CGOpenMPSIMDRuntime::emitTeamsOutlinedFunction(
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00009613 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
9614 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
9615 llvm_unreachable("Not supported in SIMD-only mode");
9616}
9617
James Y Knight9871db02019-02-05 16:42:33 +00009618llvm::Function *CGOpenMPSIMDRuntime::emitTaskOutlinedFunction(
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00009619 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
9620 const VarDecl *PartIDVar, const VarDecl *TaskTVar,
9621 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
9622 bool Tied, unsigned &NumberOfParts) {
9623 llvm_unreachable("Not supported in SIMD-only mode");
9624}
9625
9626void CGOpenMPSIMDRuntime::emitParallelCall(CodeGenFunction &CGF,
9627 SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00009628 llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00009629 ArrayRef<llvm::Value *> CapturedVars,
9630 const Expr *IfCond) {
9631 llvm_unreachable("Not supported in SIMD-only mode");
9632}
9633
9634void CGOpenMPSIMDRuntime::emitCriticalRegion(
9635 CodeGenFunction &CGF, StringRef CriticalName,
9636 const RegionCodeGenTy &CriticalOpGen, SourceLocation Loc,
9637 const Expr *Hint) {
9638 llvm_unreachable("Not supported in SIMD-only mode");
9639}
9640
9641void CGOpenMPSIMDRuntime::emitMasterRegion(CodeGenFunction &CGF,
9642 const RegionCodeGenTy &MasterOpGen,
9643 SourceLocation Loc) {
9644 llvm_unreachable("Not supported in SIMD-only mode");
9645}
9646
9647void CGOpenMPSIMDRuntime::emitTaskyieldCall(CodeGenFunction &CGF,
9648 SourceLocation Loc) {
9649 llvm_unreachable("Not supported in SIMD-only mode");
9650}
9651
9652void CGOpenMPSIMDRuntime::emitTaskgroupRegion(
9653 CodeGenFunction &CGF, const RegionCodeGenTy &TaskgroupOpGen,
9654 SourceLocation Loc) {
9655 llvm_unreachable("Not supported in SIMD-only mode");
9656}
9657
9658void CGOpenMPSIMDRuntime::emitSingleRegion(
9659 CodeGenFunction &CGF, const RegionCodeGenTy &SingleOpGen,
9660 SourceLocation Loc, ArrayRef<const Expr *> CopyprivateVars,
9661 ArrayRef<const Expr *> DestExprs, ArrayRef<const Expr *> SrcExprs,
9662 ArrayRef<const Expr *> AssignmentOps) {
9663 llvm_unreachable("Not supported in SIMD-only mode");
9664}
9665
9666void CGOpenMPSIMDRuntime::emitOrderedRegion(CodeGenFunction &CGF,
9667 const RegionCodeGenTy &OrderedOpGen,
9668 SourceLocation Loc,
9669 bool IsThreads) {
9670 llvm_unreachable("Not supported in SIMD-only mode");
9671}
9672
9673void CGOpenMPSIMDRuntime::emitBarrierCall(CodeGenFunction &CGF,
9674 SourceLocation Loc,
9675 OpenMPDirectiveKind Kind,
9676 bool EmitChecks,
9677 bool ForceSimpleCall) {
9678 llvm_unreachable("Not supported in SIMD-only mode");
9679}
9680
9681void CGOpenMPSIMDRuntime::emitForDispatchInit(
9682 CodeGenFunction &CGF, SourceLocation Loc,
9683 const OpenMPScheduleTy &ScheduleKind, unsigned IVSize, bool IVSigned,
9684 bool Ordered, const DispatchRTInput &DispatchValues) {
9685 llvm_unreachable("Not supported in SIMD-only mode");
9686}
9687
9688void CGOpenMPSIMDRuntime::emitForStaticInit(
9689 CodeGenFunction &CGF, SourceLocation Loc, OpenMPDirectiveKind DKind,
9690 const OpenMPScheduleTy &ScheduleKind, const StaticRTInput &Values) {
9691 llvm_unreachable("Not supported in SIMD-only mode");
9692}
9693
9694void CGOpenMPSIMDRuntime::emitDistributeStaticInit(
9695 CodeGenFunction &CGF, SourceLocation Loc,
9696 OpenMPDistScheduleClauseKind SchedKind, const StaticRTInput &Values) {
9697 llvm_unreachable("Not supported in SIMD-only mode");
9698}
9699
9700void CGOpenMPSIMDRuntime::emitForOrderedIterationEnd(CodeGenFunction &CGF,
9701 SourceLocation Loc,
9702 unsigned IVSize,
9703 bool IVSigned) {
9704 llvm_unreachable("Not supported in SIMD-only mode");
9705}
9706
9707void CGOpenMPSIMDRuntime::emitForStaticFinish(CodeGenFunction &CGF,
9708 SourceLocation Loc,
9709 OpenMPDirectiveKind DKind) {
9710 llvm_unreachable("Not supported in SIMD-only mode");
9711}
9712
9713llvm::Value *CGOpenMPSIMDRuntime::emitForNext(CodeGenFunction &CGF,
9714 SourceLocation Loc,
9715 unsigned IVSize, bool IVSigned,
9716 Address IL, Address LB,
9717 Address UB, Address ST) {
9718 llvm_unreachable("Not supported in SIMD-only mode");
9719}
9720
9721void CGOpenMPSIMDRuntime::emitNumThreadsClause(CodeGenFunction &CGF,
9722 llvm::Value *NumThreads,
9723 SourceLocation Loc) {
9724 llvm_unreachable("Not supported in SIMD-only mode");
9725}
9726
9727void CGOpenMPSIMDRuntime::emitProcBindClause(CodeGenFunction &CGF,
9728 OpenMPProcBindClauseKind ProcBind,
9729 SourceLocation Loc) {
9730 llvm_unreachable("Not supported in SIMD-only mode");
9731}
9732
9733Address CGOpenMPSIMDRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF,
9734 const VarDecl *VD,
9735 Address VDAddr,
9736 SourceLocation Loc) {
9737 llvm_unreachable("Not supported in SIMD-only mode");
9738}
9739
9740llvm::Function *CGOpenMPSIMDRuntime::emitThreadPrivateVarDefinition(
9741 const VarDecl *VD, Address VDAddr, SourceLocation Loc, bool PerformInit,
9742 CodeGenFunction *CGF) {
9743 llvm_unreachable("Not supported in SIMD-only mode");
9744}
9745
9746Address CGOpenMPSIMDRuntime::getAddrOfArtificialThreadPrivate(
9747 CodeGenFunction &CGF, QualType VarType, StringRef Name) {
9748 llvm_unreachable("Not supported in SIMD-only mode");
9749}
9750
9751void CGOpenMPSIMDRuntime::emitFlush(CodeGenFunction &CGF,
9752 ArrayRef<const Expr *> Vars,
9753 SourceLocation Loc) {
9754 llvm_unreachable("Not supported in SIMD-only mode");
9755}
9756
9757void CGOpenMPSIMDRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc,
9758 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00009759 llvm::Function *TaskFunction,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00009760 QualType SharedsTy, Address Shareds,
9761 const Expr *IfCond,
9762 const OMPTaskDataTy &Data) {
9763 llvm_unreachable("Not supported in SIMD-only mode");
9764}
9765
9766void CGOpenMPSIMDRuntime::emitTaskLoopCall(
9767 CodeGenFunction &CGF, SourceLocation Loc, const OMPLoopDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00009768 llvm::Function *TaskFunction, QualType SharedsTy, Address Shareds,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00009769 const Expr *IfCond, const OMPTaskDataTy &Data) {
9770 llvm_unreachable("Not supported in SIMD-only mode");
9771}
9772
9773void CGOpenMPSIMDRuntime::emitReduction(
9774 CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> Privates,
9775 ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs,
9776 ArrayRef<const Expr *> ReductionOps, ReductionOptionsTy Options) {
9777 assert(Options.SimpleReduction && "Only simple reduction is expected.");
9778 CGOpenMPRuntime::emitReduction(CGF, Loc, Privates, LHSExprs, RHSExprs,
9779 ReductionOps, Options);
9780}
9781
9782llvm::Value *CGOpenMPSIMDRuntime::emitTaskReductionInit(
9783 CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> LHSExprs,
9784 ArrayRef<const Expr *> RHSExprs, const OMPTaskDataTy &Data) {
9785 llvm_unreachable("Not supported in SIMD-only mode");
9786}
9787
9788void CGOpenMPSIMDRuntime::emitTaskReductionFixups(CodeGenFunction &CGF,
9789 SourceLocation Loc,
9790 ReductionCodeGen &RCG,
9791 unsigned N) {
9792 llvm_unreachable("Not supported in SIMD-only mode");
9793}
9794
9795Address CGOpenMPSIMDRuntime::getTaskReductionItem(CodeGenFunction &CGF,
9796 SourceLocation Loc,
9797 llvm::Value *ReductionsPtr,
9798 LValue SharedLVal) {
9799 llvm_unreachable("Not supported in SIMD-only mode");
9800}
9801
9802void CGOpenMPSIMDRuntime::emitTaskwaitCall(CodeGenFunction &CGF,
9803 SourceLocation Loc) {
9804 llvm_unreachable("Not supported in SIMD-only mode");
9805}
9806
9807void CGOpenMPSIMDRuntime::emitCancellationPointCall(
9808 CodeGenFunction &CGF, SourceLocation Loc,
9809 OpenMPDirectiveKind CancelRegion) {
9810 llvm_unreachable("Not supported in SIMD-only mode");
9811}
9812
9813void CGOpenMPSIMDRuntime::emitCancelCall(CodeGenFunction &CGF,
9814 SourceLocation Loc, const Expr *IfCond,
9815 OpenMPDirectiveKind CancelRegion) {
9816 llvm_unreachable("Not supported in SIMD-only mode");
9817}
9818
9819void CGOpenMPSIMDRuntime::emitTargetOutlinedFunction(
9820 const OMPExecutableDirective &D, StringRef ParentName,
9821 llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
9822 bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
9823 llvm_unreachable("Not supported in SIMD-only mode");
9824}
9825
9826void CGOpenMPSIMDRuntime::emitTargetCall(CodeGenFunction &CGF,
9827 const OMPExecutableDirective &D,
James Y Knight9871db02019-02-05 16:42:33 +00009828 llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00009829 llvm::Value *OutlinedFnID,
James Y Knight9871db02019-02-05 16:42:33 +00009830 const Expr *IfCond,
9831 const Expr *Device) {
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00009832 llvm_unreachable("Not supported in SIMD-only mode");
9833}
9834
9835bool CGOpenMPSIMDRuntime::emitTargetFunctions(GlobalDecl GD) {
9836 llvm_unreachable("Not supported in SIMD-only mode");
9837}
9838
9839bool CGOpenMPSIMDRuntime::emitTargetGlobalVariable(GlobalDecl GD) {
9840 llvm_unreachable("Not supported in SIMD-only mode");
9841}
9842
9843bool CGOpenMPSIMDRuntime::emitTargetGlobal(GlobalDecl GD) {
9844 return false;
9845}
9846
9847llvm::Function *CGOpenMPSIMDRuntime::emitRegistrationFunction() {
9848 return nullptr;
9849}
9850
9851void CGOpenMPSIMDRuntime::emitTeamsCall(CodeGenFunction &CGF,
9852 const OMPExecutableDirective &D,
9853 SourceLocation Loc,
James Y Knight9871db02019-02-05 16:42:33 +00009854 llvm::Function *OutlinedFn,
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00009855 ArrayRef<llvm::Value *> CapturedVars) {
9856 llvm_unreachable("Not supported in SIMD-only mode");
9857}
9858
9859void CGOpenMPSIMDRuntime::emitNumTeamsClause(CodeGenFunction &CGF,
9860 const Expr *NumTeams,
9861 const Expr *ThreadLimit,
9862 SourceLocation Loc) {
9863 llvm_unreachable("Not supported in SIMD-only mode");
9864}
9865
9866void CGOpenMPSIMDRuntime::emitTargetDataCalls(
9867 CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
9868 const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) {
9869 llvm_unreachable("Not supported in SIMD-only mode");
9870}
9871
9872void CGOpenMPSIMDRuntime::emitTargetDataStandAloneCall(
9873 CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
9874 const Expr *Device) {
9875 llvm_unreachable("Not supported in SIMD-only mode");
9876}
9877
9878void CGOpenMPSIMDRuntime::emitDoacrossInit(CodeGenFunction &CGF,
Alexey Bataevf138fda2018-08-13 19:04:24 +00009879 const OMPLoopDirective &D,
9880 ArrayRef<Expr *> NumIterations) {
Alexey Bataeva8a9153a2017-12-29 18:07:07 +00009881 llvm_unreachable("Not supported in SIMD-only mode");
9882}
9883
9884void CGOpenMPSIMDRuntime::emitDoacrossOrdered(CodeGenFunction &CGF,
9885 const OMPDependClause *C) {
9886 llvm_unreachable("Not supported in SIMD-only mode");
9887}
9888
9889const VarDecl *
9890CGOpenMPSIMDRuntime::translateParameter(const FieldDecl *FD,
9891 const VarDecl *NativeParam) const {
9892 llvm_unreachable("Not supported in SIMD-only mode");
9893}
9894
9895Address
9896CGOpenMPSIMDRuntime::getParameterAddress(CodeGenFunction &CGF,
9897 const VarDecl *NativeParam,
9898 const VarDecl *TargetParam) const {
9899 llvm_unreachable("Not supported in SIMD-only mode");
9900}