blob: c8c71dd83e97e3ae826e546eb2a2e7009e633b4a [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- CGStmtOpenMP.cpp - Emit LLVM Code from Statements ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit OpenMP nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Alexey Bataev3392d762016-02-16 11:18:12 +000014#include "CGCleanup.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "CGOpenMPRuntime.h"
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000018#include "TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000019#include "clang/AST/Stmt.h"
20#include "clang/AST/StmtOpenMP.h"
Alexey Bataev2bbf7212016-03-03 03:52:24 +000021#include "clang/AST/DeclOpenMP.h"
Alexey Bataeva839ddd2016-03-17 10:19:46 +000022#include "llvm/IR/CallSite.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000023using namespace clang;
24using namespace CodeGen;
25
Alexey Bataev3392d762016-02-16 11:18:12 +000026namespace {
27/// Lexical scope for OpenMP executable constructs, that handles correct codegen
28/// for captured expressions.
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000029class OMPLexicalScope : public CodeGenFunction::LexicalScope {
Alexey Bataev3392d762016-02-16 11:18:12 +000030 void emitPreInitStmt(CodeGenFunction &CGF, const OMPExecutableDirective &S) {
31 for (const auto *C : S.clauses()) {
32 if (auto *CPI = OMPClauseWithPreInit::get(C)) {
33 if (auto *PreInit = cast_or_null<DeclStmt>(CPI->getPreInitStmt())) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +000034 for (const auto *I : PreInit->decls()) {
35 if (!I->hasAttr<OMPCaptureNoInitAttr>())
36 CGF.EmitVarDecl(cast<VarDecl>(*I));
37 else {
38 CodeGenFunction::AutoVarEmission Emission =
39 CGF.EmitAutoVarAlloca(cast<VarDecl>(*I));
40 CGF.EmitAutoVarCleanups(Emission);
41 }
42 }
Alexey Bataev3392d762016-02-16 11:18:12 +000043 }
44 }
45 }
46 }
Alexey Bataev4ba78a42016-04-27 07:56:03 +000047 CodeGenFunction::OMPPrivateScope InlinedShareds;
48
49 static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) {
50 return CGF.LambdaCaptureFields.lookup(VD) ||
51 (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) ||
52 (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl));
53 }
Alexey Bataev3392d762016-02-16 11:18:12 +000054
Alexey Bataev3392d762016-02-16 11:18:12 +000055public:
Alexey Bataev4ba78a42016-04-27 07:56:03 +000056 OMPLexicalScope(CodeGenFunction &CGF, const OMPExecutableDirective &S,
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000057 bool AsInlined = false, bool EmitPreInitStmt = true)
Alexey Bataev4ba78a42016-04-27 07:56:03 +000058 : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()),
59 InlinedShareds(CGF) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000060 if (EmitPreInitStmt)
61 emitPreInitStmt(CGF, S);
Alexey Bataev4ba78a42016-04-27 07:56:03 +000062 if (AsInlined) {
63 if (S.hasAssociatedStmt()) {
64 auto *CS = cast<CapturedStmt>(S.getAssociatedStmt());
65 for (auto &C : CS->captures()) {
66 if (C.capturesVariable() || C.capturesVariableByCopy()) {
67 auto *VD = C.getCapturedVar();
68 DeclRefExpr DRE(const_cast<VarDecl *>(VD),
69 isCapturedVar(CGF, VD) ||
70 (CGF.CapturedStmtInfo &&
71 InlinedShareds.isGlobalVarCaptured(VD)),
72 VD->getType().getNonReferenceType(), VK_LValue,
73 SourceLocation());
74 InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address {
75 return CGF.EmitLValue(&DRE).getAddress();
76 });
77 }
78 }
79 (void)InlinedShareds.Privatize();
80 }
81 }
Alexey Bataev3392d762016-02-16 11:18:12 +000082 }
83};
Alexey Bataev14fa1c62016-03-29 05:34:15 +000084
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000085/// Lexical scope for OpenMP parallel construct, that handles correct codegen
86/// for captured expressions.
87class OMPParallelScope final : public OMPLexicalScope {
88 bool EmitPreInitStmt(const OMPExecutableDirective &S) {
89 OpenMPDirectiveKind Kind = S.getDirectiveKind();
Carlo Bertollib0ff0a62017-04-25 17:52:12 +000090 return !(isOpenMPTargetExecutionDirective(Kind) ||
91 isOpenMPLoopBoundSharingDirective(Kind)) &&
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000092 isOpenMPParallelDirective(Kind);
93 }
94
95public:
96 OMPParallelScope(CodeGenFunction &CGF, const OMPExecutableDirective &S)
97 : OMPLexicalScope(CGF, S,
98 /*AsInlined=*/false,
99 /*EmitPreInitStmt=*/EmitPreInitStmt(S)) {}
100};
101
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +0000102/// Lexical scope for OpenMP teams construct, that handles correct codegen
103/// for captured expressions.
104class OMPTeamsScope final : public OMPLexicalScope {
105 bool EmitPreInitStmt(const OMPExecutableDirective &S) {
106 OpenMPDirectiveKind Kind = S.getDirectiveKind();
107 return !isOpenMPTargetExecutionDirective(Kind) &&
108 isOpenMPTeamsDirective(Kind);
109 }
110
111public:
112 OMPTeamsScope(CodeGenFunction &CGF, const OMPExecutableDirective &S)
113 : OMPLexicalScope(CGF, S,
114 /*AsInlined=*/false,
115 /*EmitPreInitStmt=*/EmitPreInitStmt(S)) {}
116};
117
Alexey Bataev5a3af132016-03-29 08:58:54 +0000118/// Private scope for OpenMP loop-based directives, that supports capturing
119/// of used expression from loop statement.
120class OMPLoopScope : public CodeGenFunction::RunCleanupsScope {
121 void emitPreInitStmt(CodeGenFunction &CGF, const OMPLoopDirective &S) {
122 if (auto *LD = dyn_cast<OMPLoopDirective>(&S)) {
123 if (auto *PreInits = cast_or_null<DeclStmt>(LD->getPreInits())) {
124 for (const auto *I : PreInits->decls())
125 CGF.EmitVarDecl(cast<VarDecl>(*I));
126 }
127 }
128 }
129
130public:
131 OMPLoopScope(CodeGenFunction &CGF, const OMPLoopDirective &S)
132 : CodeGenFunction::RunCleanupsScope(CGF) {
133 emitPreInitStmt(CGF, S);
134 }
135};
136
Alexey Bataev3392d762016-02-16 11:18:12 +0000137} // namespace
138
Alexey Bataev1189bd02016-01-26 12:20:39 +0000139llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) {
140 auto &C = getContext();
141 llvm::Value *Size = nullptr;
142 auto SizeInChars = C.getTypeSizeInChars(Ty);
143 if (SizeInChars.isZero()) {
144 // getTypeSizeInChars() returns 0 for a VLA.
145 while (auto *VAT = C.getAsVariableArrayType(Ty)) {
146 llvm::Value *ArraySize;
147 std::tie(ArraySize, Ty) = getVLASize(VAT);
148 Size = Size ? Builder.CreateNUWMul(Size, ArraySize) : ArraySize;
149 }
150 SizeInChars = C.getTypeSizeInChars(Ty);
151 if (SizeInChars.isZero())
152 return llvm::ConstantInt::get(SizeTy, /*V=*/0);
153 Size = Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars));
154 } else
155 Size = CGM.getSize(SizeInChars);
156 return Size;
157}
158
Alexey Bataev2377fe92015-09-10 08:12:02 +0000159void CodeGenFunction::GenerateOpenMPCapturedVars(
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000160 const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) {
Alexey Bataev2377fe92015-09-10 08:12:02 +0000161 const RecordDecl *RD = S.getCapturedRecordDecl();
162 auto CurField = RD->field_begin();
163 auto CurCap = S.captures().begin();
164 for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(),
165 E = S.capture_init_end();
166 I != E; ++I, ++CurField, ++CurCap) {
167 if (CurField->hasCapturedVLAType()) {
168 auto VAT = CurField->getCapturedVLAType();
Samuel Antaobed3c462015-10-02 16:14:20 +0000169 auto *Val = VLASizeMap[VAT->getSizeExpr()];
Samuel Antaobed3c462015-10-02 16:14:20 +0000170 CapturedVars.push_back(Val);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000171 } else if (CurCap->capturesThis())
172 CapturedVars.push_back(CXXThisValue);
Samuel Antao6d004262016-06-16 18:39:34 +0000173 else if (CurCap->capturesVariableByCopy()) {
174 llvm::Value *CV =
175 EmitLoadOfLValue(EmitLValue(*I), SourceLocation()).getScalarVal();
176
177 // If the field is not a pointer, we need to save the actual value
178 // and load it as a void pointer.
179 if (!CurField->getType()->isAnyPointerType()) {
180 auto &Ctx = getContext();
181 auto DstAddr = CreateMemTemp(
182 Ctx.getUIntPtrType(),
183 Twine(CurCap->getCapturedVar()->getName()) + ".casted");
184 LValue DstLV = MakeAddrLValue(DstAddr, Ctx.getUIntPtrType());
185
186 auto *SrcAddrVal = EmitScalarConversion(
187 DstAddr.getPointer(), Ctx.getPointerType(Ctx.getUIntPtrType()),
188 Ctx.getPointerType(CurField->getType()), SourceLocation());
189 LValue SrcLV =
190 MakeNaturalAlignAddrLValue(SrcAddrVal, CurField->getType());
191
192 // Store the value using the source type pointer.
193 EmitStoreThroughLValue(RValue::get(CV), SrcLV);
194
195 // Load the value using the destination type pointer.
196 CV = EmitLoadOfLValue(DstLV, SourceLocation()).getScalarVal();
197 }
198 CapturedVars.push_back(CV);
199 } else {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000200 assert(CurCap->capturesVariable() && "Expected capture by reference.");
Alexey Bataev2377fe92015-09-10 08:12:02 +0000201 CapturedVars.push_back(EmitLValue(*I).getAddress().getPointer());
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000202 }
Alexey Bataev2377fe92015-09-10 08:12:02 +0000203 }
204}
205
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000206static Address castValueFromUintptr(CodeGenFunction &CGF, QualType DstType,
207 StringRef Name, LValue AddrLV,
208 bool isReferenceType = false) {
209 ASTContext &Ctx = CGF.getContext();
210
211 auto *CastedPtr = CGF.EmitScalarConversion(
212 AddrLV.getAddress().getPointer(), Ctx.getUIntPtrType(),
213 Ctx.getPointerType(DstType), SourceLocation());
214 auto TmpAddr =
215 CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType))
216 .getAddress();
217
218 // If we are dealing with references we need to return the address of the
219 // reference instead of the reference of the value.
220 if (isReferenceType) {
221 QualType RefType = Ctx.getLValueReferenceType(DstType);
222 auto *RefVal = TmpAddr.getPointer();
223 TmpAddr = CGF.CreateMemTemp(RefType, Twine(Name) + ".ref");
224 auto TmpLVal = CGF.MakeAddrLValue(TmpAddr, RefType);
Akira Hatanaka642f7992016-10-18 19:05:41 +0000225 CGF.EmitStoreThroughLValue(RValue::get(RefVal), TmpLVal, /*isInit*/ true);
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000226 }
227
228 return TmpAddr;
229}
230
Alexey Bataevf7ce1662017-04-10 19:16:45 +0000231static QualType getCanonicalParamType(ASTContext &C, QualType T) {
232 if (T->isLValueReferenceType()) {
233 return C.getLValueReferenceType(
234 getCanonicalParamType(C, T.getNonReferenceType()),
235 /*SpelledAsLValue=*/false);
236 }
237 if (T->isPointerType())
238 return C.getPointerType(getCanonicalParamType(C, T->getPointeeType()));
239 return C.getCanonicalParamType(T);
240}
241
Alexey Bataev2377fe92015-09-10 08:12:02 +0000242llvm::Function *
Samuel Antao6d004262016-06-16 18:39:34 +0000243CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S) {
Alexey Bataev2377fe92015-09-10 08:12:02 +0000244 assert(
245 CapturedStmtInfo &&
246 "CapturedStmtInfo should be set when generating the captured function");
247 const CapturedDecl *CD = S.getCapturedDecl();
248 const RecordDecl *RD = S.getCapturedRecordDecl();
249 assert(CD->hasBody() && "missing CapturedDecl body");
250
251 // Build the argument list.
252 ASTContext &Ctx = CGM.getContext();
253 FunctionArgList Args;
254 Args.append(CD->param_begin(),
255 std::next(CD->param_begin(), CD->getContextParamPosition()));
256 auto I = S.captures().begin();
257 for (auto *FD : RD->fields()) {
258 QualType ArgType = FD->getType();
259 IdentifierInfo *II = nullptr;
260 VarDecl *CapVar = nullptr;
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000261
262 // If this is a capture by copy and the type is not a pointer, the outlined
263 // function argument type should be uintptr and the value properly casted to
264 // uintptr. This is necessary given that the runtime library is only able to
265 // deal with pointers. We can pass in the same way the VLA type sizes to the
266 // outlined function.
Samuel Antao6d004262016-06-16 18:39:34 +0000267 if ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) ||
268 I->capturesVariableArrayType())
269 ArgType = Ctx.getUIntPtrType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000270
271 if (I->capturesVariable() || I->capturesVariableByCopy()) {
Alexey Bataev2377fe92015-09-10 08:12:02 +0000272 CapVar = I->getCapturedVar();
273 II = CapVar->getIdentifier();
274 } else if (I->capturesThis())
275 II = &getContext().Idents.get("this");
276 else {
277 assert(I->capturesVariableArrayType());
278 II = &getContext().Idents.get("vla");
279 }
Alexey Bataev2f5ed342016-10-13 09:52:46 +0000280 if (ArgType->isVariablyModifiedType()) {
Alexey Bataev2f5ed342016-10-13 09:52:46 +0000281 ArgType =
Alexey Bataevf7ce1662017-04-10 19:16:45 +0000282 getCanonicalParamType(getContext(), ArgType.getNonReferenceType());
Alexey Bataev2f5ed342016-10-13 09:52:46 +0000283 }
Alexey Bataev2377fe92015-09-10 08:12:02 +0000284 Args.push_back(ImplicitParamDecl::Create(getContext(), nullptr,
285 FD->getLocation(), II, ArgType));
286 ++I;
287 }
288 Args.append(
289 std::next(CD->param_begin(), CD->getContextParamPosition() + 1),
290 CD->param_end());
291
292 // Create the function declaration.
293 FunctionType::ExtInfo ExtInfo;
294 const CGFunctionInfo &FuncInfo =
John McCallc56a8b32016-03-11 04:30:31 +0000295 CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000296 llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo);
297
298 llvm::Function *F = llvm::Function::Create(
299 FuncLLVMTy, llvm::GlobalValue::InternalLinkage,
300 CapturedStmtInfo->getHelperName(), &CGM.getModule());
301 CGM.SetInternalFunctionAttributes(CD, F, FuncInfo);
302 if (CD->isNothrow())
303 F->addFnAttr(llvm::Attribute::NoUnwind);
304
305 // Generate the function.
306 StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args, CD->getLocation(),
307 CD->getBody()->getLocStart());
308 unsigned Cnt = CD->getContextParamPosition();
309 I = S.captures().begin();
310 for (auto *FD : RD->fields()) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000311 // If we are capturing a pointer by copy we don't need to do anything, just
312 // use the value that we get from the arguments.
313 if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) {
Samuel Antao403ffd42016-07-27 22:49:49 +0000314 const VarDecl *CurVD = I->getCapturedVar();
315 Address LocalAddr = GetAddrOfLocalVar(Args[Cnt]);
316 // If the variable is a reference we need to materialize it here.
317 if (CurVD->getType()->isReferenceType()) {
318 Address RefAddr = CreateMemTemp(CurVD->getType(), getPointerAlign(),
319 ".materialized_ref");
320 EmitStoreOfScalar(LocalAddr.getPointer(), RefAddr, /*Volatile=*/false,
321 CurVD->getType());
322 LocalAddr = RefAddr;
323 }
324 setAddrOfLocalVar(CurVD, LocalAddr);
Richard Trieucc3949d2016-02-18 22:34:54 +0000325 ++Cnt;
326 ++I;
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000327 continue;
328 }
329
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000330 LValueBaseInfo BaseInfo(AlignmentSource::Decl, false);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000331 LValue ArgLVal =
332 MakeAddrLValue(GetAddrOfLocalVar(Args[Cnt]), Args[Cnt]->getType(),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000333 BaseInfo);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000334 if (FD->hasCapturedVLAType()) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000335 LValue CastedArgLVal =
Samuel Antao6d004262016-06-16 18:39:34 +0000336 MakeAddrLValue(castValueFromUintptr(*this, FD->getType(),
337 Args[Cnt]->getName(), ArgLVal),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000338 FD->getType(), BaseInfo);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000339 auto *ExprArg =
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000340 EmitLoadOfLValue(CastedArgLVal, SourceLocation()).getScalarVal();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000341 auto VAT = FD->getCapturedVLAType();
342 VLASizeMap[VAT->getSizeExpr()] = ExprArg;
343 } else if (I->capturesVariable()) {
344 auto *Var = I->getCapturedVar();
345 QualType VarTy = Var->getType();
346 Address ArgAddr = ArgLVal.getAddress();
347 if (!VarTy->isReferenceType()) {
Alexey Bataev2f5ed342016-10-13 09:52:46 +0000348 if (ArgLVal.getType()->isLValueReferenceType()) {
349 ArgAddr = EmitLoadOfReference(
350 ArgAddr, ArgLVal.getType()->castAs<ReferenceType>());
Alexey Bataevac5eabb2016-11-07 11:16:04 +0000351 } else if (!VarTy->isVariablyModifiedType() || !VarTy->isPointerType()) {
Alexey Bataev2f5ed342016-10-13 09:52:46 +0000352 assert(ArgLVal.getType()->isPointerType());
353 ArgAddr = EmitLoadOfPointer(
354 ArgAddr, ArgLVal.getType()->castAs<PointerType>());
355 }
Alexey Bataev2377fe92015-09-10 08:12:02 +0000356 }
Alexey Bataevc71a4092015-09-11 10:29:41 +0000357 setAddrOfLocalVar(
358 Var, Address(ArgAddr.getPointer(), getContext().getDeclAlign(Var)));
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000359 } else if (I->capturesVariableByCopy()) {
360 assert(!FD->getType()->isAnyPointerType() &&
361 "Not expecting a captured pointer.");
362 auto *Var = I->getCapturedVar();
363 QualType VarTy = Var->getType();
Samuel Antao6d004262016-06-16 18:39:34 +0000364 setAddrOfLocalVar(Var, castValueFromUintptr(*this, FD->getType(),
365 Args[Cnt]->getName(), ArgLVal,
366 VarTy->isReferenceType()));
Alexey Bataev2377fe92015-09-10 08:12:02 +0000367 } else {
368 // If 'this' is captured, load it into CXXThisValue.
369 assert(I->capturesThis());
370 CXXThisValue =
371 EmitLoadOfLValue(ArgLVal, Args[Cnt]->getLocation()).getScalarVal();
372 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000373 ++Cnt;
374 ++I;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000375 }
376
Serge Pavlov3a561452015-12-06 14:32:39 +0000377 PGO.assignRegionCounters(GlobalDecl(CD), F);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000378 CapturedStmtInfo->EmitBody(*this, CD->getBody());
379 FinishFunction(CD->getBodyRBrace());
380
381 return F;
382}
383
Alexey Bataev9959db52014-05-06 10:08:46 +0000384//===----------------------------------------------------------------------===//
385// OpenMP Directive Emission
386//===----------------------------------------------------------------------===//
Alexey Bataev420d45b2015-04-14 05:11:24 +0000387void CodeGenFunction::EmitOMPAggregateAssign(
John McCall7f416cc2015-09-08 08:05:57 +0000388 Address DestAddr, Address SrcAddr, QualType OriginalType,
389 const llvm::function_ref<void(Address, Address)> &CopyGen) {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000390 // Perform element-by-element initialization.
391 QualType ElementTy;
John McCall7f416cc2015-09-08 08:05:57 +0000392
393 // Drill down to the base element type on both arrays.
Alexey Bataev420d45b2015-04-14 05:11:24 +0000394 auto ArrayTy = OriginalType->getAsArrayTypeUnsafe();
John McCall7f416cc2015-09-08 08:05:57 +0000395 auto NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr);
396 SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
397
398 auto SrcBegin = SrcAddr.getPointer();
399 auto DestBegin = DestAddr.getPointer();
Alexey Bataev420d45b2015-04-14 05:11:24 +0000400 // Cast from pointer to array type to pointer to single element.
Alexey Bataev420d45b2015-04-14 05:11:24 +0000401 auto DestEnd = Builder.CreateGEP(DestBegin, NumElements);
402 // The basic structure here is a while-do loop.
403 auto BodyBB = createBasicBlock("omp.arraycpy.body");
404 auto DoneBB = createBasicBlock("omp.arraycpy.done");
405 auto IsEmpty =
406 Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty");
407 Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000408
Alexey Bataev420d45b2015-04-14 05:11:24 +0000409 // Enter the loop body, making that address the current address.
410 auto EntryBB = Builder.GetInsertBlock();
411 EmitBlock(BodyBB);
John McCall7f416cc2015-09-08 08:05:57 +0000412
413 CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy);
414
415 llvm::PHINode *SrcElementPHI =
416 Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast");
417 SrcElementPHI->addIncoming(SrcBegin, EntryBB);
418 Address SrcElementCurrent =
419 Address(SrcElementPHI,
420 SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize));
421
422 llvm::PHINode *DestElementPHI =
423 Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast");
424 DestElementPHI->addIncoming(DestBegin, EntryBB);
425 Address DestElementCurrent =
426 Address(DestElementPHI,
427 DestAddr.getAlignment().alignmentOfArrayElement(ElementSize));
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000428
Alexey Bataev420d45b2015-04-14 05:11:24 +0000429 // Emit copy.
430 CopyGen(DestElementCurrent, SrcElementCurrent);
431
432 // Shift the address forward by one element.
433 auto DestElementNext = Builder.CreateConstGEP1_32(
John McCall7f416cc2015-09-08 08:05:57 +0000434 DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
Alexey Bataev420d45b2015-04-14 05:11:24 +0000435 auto SrcElementNext = Builder.CreateConstGEP1_32(
John McCall7f416cc2015-09-08 08:05:57 +0000436 SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element");
Alexey Bataev420d45b2015-04-14 05:11:24 +0000437 // Check whether we've reached the end.
438 auto Done =
439 Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
440 Builder.CreateCondBr(Done, DoneBB, BodyBB);
John McCall7f416cc2015-09-08 08:05:57 +0000441 DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock());
442 SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock());
Alexey Bataev420d45b2015-04-14 05:11:24 +0000443
444 // Done.
445 EmitBlock(DoneBB, /*IsFinished=*/true);
446}
447
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000448/// Check if the combiner is a call to UDR combiner and if it is so return the
449/// UDR decl used for reduction.
450static const OMPDeclareReductionDecl *
451getReductionInit(const Expr *ReductionOp) {
452 if (auto *CE = dyn_cast<CallExpr>(ReductionOp))
453 if (auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee()))
454 if (auto *DRE =
455 dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts()))
456 if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl()))
457 return DRD;
458 return nullptr;
459}
460
461static void emitInitWithReductionInitializer(CodeGenFunction &CGF,
462 const OMPDeclareReductionDecl *DRD,
463 const Expr *InitOp,
464 Address Private, Address Original,
465 QualType Ty) {
466 if (DRD->getInitializer()) {
467 std::pair<llvm::Function *, llvm::Function *> Reduction =
468 CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD);
469 auto *CE = cast<CallExpr>(InitOp);
470 auto *OVE = cast<OpaqueValueExpr>(CE->getCallee());
471 const Expr *LHS = CE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
472 const Expr *RHS = CE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
473 auto *LHSDRE = cast<DeclRefExpr>(cast<UnaryOperator>(LHS)->getSubExpr());
474 auto *RHSDRE = cast<DeclRefExpr>(cast<UnaryOperator>(RHS)->getSubExpr());
475 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
476 PrivateScope.addPrivate(cast<VarDecl>(LHSDRE->getDecl()),
477 [=]() -> Address { return Private; });
478 PrivateScope.addPrivate(cast<VarDecl>(RHSDRE->getDecl()),
479 [=]() -> Address { return Original; });
480 (void)PrivateScope.Privatize();
481 RValue Func = RValue::get(Reduction.second);
482 CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func);
483 CGF.EmitIgnoredExpr(InitOp);
484 } else {
485 llvm::Constant *Init = CGF.CGM.EmitNullConstant(Ty);
486 auto *GV = new llvm::GlobalVariable(
487 CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true,
488 llvm::GlobalValue::PrivateLinkage, Init, ".init");
489 LValue LV = CGF.MakeNaturalAlignAddrLValue(GV, Ty);
490 RValue InitRVal;
491 switch (CGF.getEvaluationKind(Ty)) {
492 case TEK_Scalar:
493 InitRVal = CGF.EmitLoadOfLValue(LV, SourceLocation());
494 break;
495 case TEK_Complex:
496 InitRVal =
497 RValue::getComplex(CGF.EmitLoadOfComplex(LV, SourceLocation()));
498 break;
499 case TEK_Aggregate:
500 InitRVal = RValue::getAggregate(LV.getAddress());
501 break;
502 }
503 OpaqueValueExpr OVE(SourceLocation(), Ty, VK_RValue);
504 CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE, InitRVal);
505 CGF.EmitAnyExprToMem(&OVE, Private, Ty.getQualifiers(),
506 /*IsInitializer=*/false);
507 }
508}
509
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000510/// \brief Emit initialization of arrays of complex types.
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000511/// \param DestAddr Address of the array.
512/// \param Type Type of array.
513/// \param Init Initial expression of array.
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000514/// \param SrcAddr Address of the original array.
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000515static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr,
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000516 QualType Type, const Expr *Init,
517 Address SrcAddr = Address::invalid()) {
518 auto *DRD = getReductionInit(Init);
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000519 // Perform element-by-element initialization.
520 QualType ElementTy;
521
522 // Drill down to the base element type on both arrays.
523 auto ArrayTy = Type->getAsArrayTypeUnsafe();
524 auto NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr);
525 DestAddr =
526 CGF.Builder.CreateElementBitCast(DestAddr, DestAddr.getElementType());
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000527 if (DRD)
528 SrcAddr =
529 CGF.Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000530
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000531 llvm::Value *SrcBegin = nullptr;
532 if (DRD)
533 SrcBegin = SrcAddr.getPointer();
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000534 auto DestBegin = DestAddr.getPointer();
535 // Cast from pointer to array type to pointer to single element.
536 auto DestEnd = CGF.Builder.CreateGEP(DestBegin, NumElements);
537 // The basic structure here is a while-do loop.
538 auto BodyBB = CGF.createBasicBlock("omp.arrayinit.body");
539 auto DoneBB = CGF.createBasicBlock("omp.arrayinit.done");
540 auto IsEmpty =
541 CGF.Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arrayinit.isempty");
542 CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
543
544 // Enter the loop body, making that address the current address.
545 auto EntryBB = CGF.Builder.GetInsertBlock();
546 CGF.EmitBlock(BodyBB);
547
548 CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy);
549
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000550 llvm::PHINode *SrcElementPHI = nullptr;
551 Address SrcElementCurrent = Address::invalid();
552 if (DRD) {
553 SrcElementPHI = CGF.Builder.CreatePHI(SrcBegin->getType(), 2,
554 "omp.arraycpy.srcElementPast");
555 SrcElementPHI->addIncoming(SrcBegin, EntryBB);
556 SrcElementCurrent =
557 Address(SrcElementPHI,
558 SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize));
559 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000560 llvm::PHINode *DestElementPHI = CGF.Builder.CreatePHI(
561 DestBegin->getType(), 2, "omp.arraycpy.destElementPast");
562 DestElementPHI->addIncoming(DestBegin, EntryBB);
563 Address DestElementCurrent =
564 Address(DestElementPHI,
565 DestAddr.getAlignment().alignmentOfArrayElement(ElementSize));
566
567 // Emit copy.
568 {
569 CodeGenFunction::RunCleanupsScope InitScope(CGF);
Alexey Bataev8fbae8cf2016-04-27 11:38:05 +0000570 if (DRD && (DRD->getInitializer() || !Init)) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000571 emitInitWithReductionInitializer(CGF, DRD, Init, DestElementCurrent,
572 SrcElementCurrent, ElementTy);
573 } else
574 CGF.EmitAnyExprToMem(Init, DestElementCurrent, ElementTy.getQualifiers(),
575 /*IsInitializer=*/false);
576 }
577
578 if (DRD) {
579 // Shift the address forward by one element.
580 auto SrcElementNext = CGF.Builder.CreateConstGEP1_32(
581 SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
582 SrcElementPHI->addIncoming(SrcElementNext, CGF.Builder.GetInsertBlock());
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000583 }
584
585 // Shift the address forward by one element.
586 auto DestElementNext = CGF.Builder.CreateConstGEP1_32(
587 DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
588 // Check whether we've reached the end.
589 auto Done =
590 CGF.Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
591 CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB);
592 DestElementPHI->addIncoming(DestElementNext, CGF.Builder.GetInsertBlock());
593
594 // Done.
595 CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
596}
597
John McCall7f416cc2015-09-08 08:05:57 +0000598void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr,
599 Address SrcAddr, const VarDecl *DestVD,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000600 const VarDecl *SrcVD, const Expr *Copy) {
601 if (OriginalType->isArrayType()) {
602 auto *BO = dyn_cast<BinaryOperator>(Copy);
603 if (BO && BO->getOpcode() == BO_Assign) {
604 // Perform simple memcpy for simple copying.
John McCall7f416cc2015-09-08 08:05:57 +0000605 EmitAggregateAssign(DestAddr, SrcAddr, OriginalType);
Alexey Bataev420d45b2015-04-14 05:11:24 +0000606 } else {
607 // For arrays with complex element types perform element by element
608 // copying.
John McCall7f416cc2015-09-08 08:05:57 +0000609 EmitOMPAggregateAssign(
Alexey Bataev420d45b2015-04-14 05:11:24 +0000610 DestAddr, SrcAddr, OriginalType,
John McCall7f416cc2015-09-08 08:05:57 +0000611 [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000612 // Working with the single array element, so have to remap
613 // destination and source variables to corresponding array
614 // elements.
John McCall7f416cc2015-09-08 08:05:57 +0000615 CodeGenFunction::OMPPrivateScope Remap(*this);
616 Remap.addPrivate(DestVD, [DestElement]() -> Address {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000617 return DestElement;
618 });
619 Remap.addPrivate(
John McCall7f416cc2015-09-08 08:05:57 +0000620 SrcVD, [SrcElement]() -> Address { return SrcElement; });
Alexey Bataev420d45b2015-04-14 05:11:24 +0000621 (void)Remap.Privatize();
John McCall7f416cc2015-09-08 08:05:57 +0000622 EmitIgnoredExpr(Copy);
Alexey Bataev420d45b2015-04-14 05:11:24 +0000623 });
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000624 }
Alexey Bataev420d45b2015-04-14 05:11:24 +0000625 } else {
626 // Remap pseudo source variable to private copy.
John McCall7f416cc2015-09-08 08:05:57 +0000627 CodeGenFunction::OMPPrivateScope Remap(*this);
628 Remap.addPrivate(SrcVD, [SrcAddr]() -> Address { return SrcAddr; });
629 Remap.addPrivate(DestVD, [DestAddr]() -> Address { return DestAddr; });
Alexey Bataev420d45b2015-04-14 05:11:24 +0000630 (void)Remap.Privatize();
631 // Emit copying of the whole variable.
John McCall7f416cc2015-09-08 08:05:57 +0000632 EmitIgnoredExpr(Copy);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000633 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000634}
635
Alexey Bataev69c62a92015-04-15 04:52:20 +0000636bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
637 OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000638 if (!HaveInsertPoint())
639 return false;
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000640 bool FirstprivateIsLastprivate = false;
641 llvm::DenseSet<const VarDecl *> Lastprivates;
642 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
643 for (const auto *D : C->varlists())
644 Lastprivates.insert(
645 cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl());
646 }
Alexey Bataev69c62a92015-04-15 04:52:20 +0000647 llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate;
Alexey Bataev9afe5752016-05-24 07:40:12 +0000648 CGCapturedStmtInfo CapturesInfo(cast<CapturedStmt>(*D.getAssociatedStmt()));
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000649 for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) {
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000650 auto IRef = C->varlist_begin();
651 auto InitsRef = C->inits().begin();
652 for (auto IInit : C->private_copies()) {
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000653 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000654 bool ThisFirstprivateIsLastprivate =
655 Lastprivates.count(OrigVD->getCanonicalDecl()) > 0;
Alexey Bataev9afe5752016-05-24 07:40:12 +0000656 auto *CapFD = CapturesInfo.lookup(OrigVD);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000657 auto *FD = CapturedStmtInfo->lookup(OrigVD);
Alexey Bataev9afe5752016-05-24 07:40:12 +0000658 if (!ThisFirstprivateIsLastprivate && FD && (FD == CapFD) &&
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000659 !FD->getType()->isReferenceType()) {
660 EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl());
661 ++IRef;
662 ++InitsRef;
663 continue;
664 }
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000665 FirstprivateIsLastprivate =
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000666 FirstprivateIsLastprivate || ThisFirstprivateIsLastprivate;
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000667 if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000668 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
669 auto *VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl());
670 bool IsRegistered;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000671 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
672 /*RefersToEnclosingVariableOrCapture=*/FD != nullptr,
673 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
John McCall7f416cc2015-09-08 08:05:57 +0000674 Address OriginalAddr = EmitLValue(&DRE).getAddress();
Alexey Bataevfeddd642016-04-22 09:05:03 +0000675 QualType Type = VD->getType();
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000676 if (Type->isArrayType()) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000677 // Emit VarDecl with copy init for arrays.
678 // Get the address of the original variable captured in current
679 // captured region.
John McCall7f416cc2015-09-08 08:05:57 +0000680 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000681 auto Emission = EmitAutoVarAlloca(*VD);
682 auto *Init = VD->getInit();
683 if (!isa<CXXConstructExpr>(Init) || isTrivialInitializer(Init)) {
684 // Perform simple memcpy.
685 EmitAggregateAssign(Emission.getAllocatedAddress(), OriginalAddr,
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000686 Type);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000687 } else {
688 EmitOMPAggregateAssign(
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000689 Emission.getAllocatedAddress(), OriginalAddr, Type,
John McCall7f416cc2015-09-08 08:05:57 +0000690 [this, VDInit, Init](Address DestElement,
691 Address SrcElement) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000692 // Clean up any temporaries needed by the initialization.
693 RunCleanupsScope InitScope(*this);
694 // Emit initialization for single element.
John McCall7f416cc2015-09-08 08:05:57 +0000695 setAddrOfLocalVar(VDInit, SrcElement);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000696 EmitAnyExprToMem(Init, DestElement,
697 Init->getType().getQualifiers(),
698 /*IsInitializer*/ false);
699 LocalDeclMap.erase(VDInit);
700 });
701 }
702 EmitAutoVarCleanups(Emission);
703 return Emission.getAllocatedAddress();
704 });
705 } else {
John McCall7f416cc2015-09-08 08:05:57 +0000706 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000707 // Emit private VarDecl with copy init.
708 // Remap temp VDInit variable to the address of the original
709 // variable
710 // (for proper handling of captured global variables).
John McCall7f416cc2015-09-08 08:05:57 +0000711 setAddrOfLocalVar(VDInit, OriginalAddr);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000712 EmitDecl(*VD);
713 LocalDeclMap.erase(VDInit);
714 return GetAddrOfLocalVar(VD);
715 });
716 }
717 assert(IsRegistered &&
718 "firstprivate var already registered as private");
719 // Silence the warning about unused variable.
720 (void)IsRegistered;
721 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000722 ++IRef;
723 ++InitsRef;
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000724 }
725 }
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000726 return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000727}
728
Alexey Bataev03b340a2014-10-21 03:16:40 +0000729void CodeGenFunction::EmitOMPPrivateClause(
730 const OMPExecutableDirective &D,
731 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000732 if (!HaveInsertPoint())
733 return;
Alexey Bataev50a64582015-04-22 12:24:45 +0000734 llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000735 for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) {
Alexey Bataev03b340a2014-10-21 03:16:40 +0000736 auto IRef = C->varlist_begin();
737 for (auto IInit : C->private_copies()) {
738 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev50a64582015-04-22 12:24:45 +0000739 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
740 auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
741 bool IsRegistered =
John McCall7f416cc2015-09-08 08:05:57 +0000742 PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev50a64582015-04-22 12:24:45 +0000743 // Emit private VarDecl with copy init.
744 EmitDecl(*VD);
745 return GetAddrOfLocalVar(VD);
746 });
747 assert(IsRegistered && "private var already registered as private");
748 // Silence the warning about unused variable.
749 (void)IsRegistered;
750 }
Alexey Bataev03b340a2014-10-21 03:16:40 +0000751 ++IRef;
752 }
753 }
754}
755
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000756bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000757 if (!HaveInsertPoint())
758 return false;
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000759 // threadprivate_var1 = master_threadprivate_var1;
760 // operator=(threadprivate_var2, master_threadprivate_var2);
761 // ...
762 // __kmpc_barrier(&loc, global_tid);
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000763 llvm::DenseSet<const VarDecl *> CopiedVars;
764 llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000765 for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) {
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000766 auto IRef = C->varlist_begin();
767 auto ISrcRef = C->source_exprs().begin();
768 auto IDestRef = C->destination_exprs().begin();
769 for (auto *AssignOp : C->assignment_ops()) {
770 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000771 QualType Type = VD->getType();
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000772 if (CopiedVars.insert(VD->getCanonicalDecl()).second) {
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000773 // Get the address of the master variable. If we are emitting code with
774 // TLS support, the address is passed from the master as field in the
775 // captured declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000776 Address MasterAddr = Address::invalid();
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000777 if (getLangOpts().OpenMPUseTLS &&
778 getContext().getTargetInfo().isTLSSupported()) {
779 assert(CapturedStmtInfo->lookup(VD) &&
780 "Copyin threadprivates should have been captured!");
781 DeclRefExpr DRE(const_cast<VarDecl *>(VD), true, (*IRef)->getType(),
782 VK_LValue, (*IRef)->getExprLoc());
783 MasterAddr = EmitLValue(&DRE).getAddress();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000784 LocalDeclMap.erase(VD);
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000785 } else {
John McCall7f416cc2015-09-08 08:05:57 +0000786 MasterAddr =
787 Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD)
788 : CGM.GetAddrOfGlobal(VD),
789 getContext().getDeclAlign(VD));
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000790 }
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000791 // Get the address of the threadprivate variable.
John McCall7f416cc2015-09-08 08:05:57 +0000792 Address PrivateAddr = EmitLValue(*IRef).getAddress();
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000793 if (CopiedVars.size() == 1) {
794 // At first check if current thread is a master thread. If it is, no
795 // need to copy data.
796 CopyBegin = createBasicBlock("copyin.not.master");
797 CopyEnd = createBasicBlock("copyin.not.master.end");
798 Builder.CreateCondBr(
799 Builder.CreateICmpNE(
John McCall7f416cc2015-09-08 08:05:57 +0000800 Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy),
801 Builder.CreatePtrToInt(PrivateAddr.getPointer(), CGM.IntPtrTy)),
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000802 CopyBegin, CopyEnd);
803 EmitBlock(CopyBegin);
804 }
805 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
806 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +0000807 EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp);
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000808 }
809 ++IRef;
810 ++ISrcRef;
811 ++IDestRef;
812 }
813 }
814 if (CopyEnd) {
815 // Exit out of copying procedure for non-master thread.
816 EmitBlock(CopyEnd, /*IsFinished=*/true);
817 return true;
818 }
819 return false;
820}
821
Alexey Bataev38e89532015-04-16 04:54:05 +0000822bool CodeGenFunction::EmitOMPLastprivateClauseInit(
823 const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000824 if (!HaveInsertPoint())
825 return false;
Alexey Bataev38e89532015-04-16 04:54:05 +0000826 bool HasAtLeastOneLastprivate = false;
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000827 llvm::DenseSet<const VarDecl *> SIMDLCVs;
828 if (isOpenMPSimdDirective(D.getDirectiveKind())) {
829 auto *LoopDirective = cast<OMPLoopDirective>(&D);
830 for (auto *C : LoopDirective->counters()) {
831 SIMDLCVs.insert(
832 cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl());
833 }
834 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000835 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000836 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
Alexey Bataevd130fd12015-05-13 10:23:02 +0000837 HasAtLeastOneLastprivate = true;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000838 if (isOpenMPTaskLoopDirective(D.getDirectiveKind()))
839 break;
Alexey Bataev38e89532015-04-16 04:54:05 +0000840 auto IRef = C->varlist_begin();
841 auto IDestRef = C->destination_exprs().begin();
842 for (auto *IInit : C->private_copies()) {
843 // Keep the address of the original variable for future update at the end
844 // of the loop.
845 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataevf93095a2016-05-05 08:46:22 +0000846 // Taskloops do not require additional initialization, it is done in
847 // runtime support library.
Alexey Bataev38e89532015-04-16 04:54:05 +0000848 if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) {
849 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +0000850 PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() -> Address {
Alexey Bataev38e89532015-04-16 04:54:05 +0000851 DeclRefExpr DRE(
852 const_cast<VarDecl *>(OrigVD),
853 /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup(
854 OrigVD) != nullptr,
855 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
856 return EmitLValue(&DRE).getAddress();
857 });
858 // Check if the variable is also a firstprivate: in this case IInit is
859 // not generated. Initialization of this variable will happen in codegen
860 // for 'firstprivate' clause.
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000861 if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) {
Alexey Bataevd130fd12015-05-13 10:23:02 +0000862 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
Alexey Bataevf93095a2016-05-05 08:46:22 +0000863 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
864 // Emit private VarDecl with copy init.
865 EmitDecl(*VD);
866 return GetAddrOfLocalVar(VD);
867 });
Alexey Bataevd130fd12015-05-13 10:23:02 +0000868 assert(IsRegistered &&
869 "lastprivate var already registered as private");
870 (void)IsRegistered;
871 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000872 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000873 ++IRef;
874 ++IDestRef;
Alexey Bataev38e89532015-04-16 04:54:05 +0000875 }
876 }
877 return HasAtLeastOneLastprivate;
878}
879
880void CodeGenFunction::EmitOMPLastprivateClauseFinal(
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000881 const OMPExecutableDirective &D, bool NoFinals,
882 llvm::Value *IsLastIterCond) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000883 if (!HaveInsertPoint())
884 return;
Alexey Bataev38e89532015-04-16 04:54:05 +0000885 // Emit following code:
886 // if (<IsLastIterCond>) {
887 // orig_var1 = private_orig_var1;
888 // ...
889 // orig_varn = private_orig_varn;
890 // }
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000891 llvm::BasicBlock *ThenBB = nullptr;
892 llvm::BasicBlock *DoneBB = nullptr;
893 if (IsLastIterCond) {
894 ThenBB = createBasicBlock(".omp.lastprivate.then");
895 DoneBB = createBasicBlock(".omp.lastprivate.done");
896 Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB);
897 EmitBlock(ThenBB);
898 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000899 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
900 llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates;
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000901 if (auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) {
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000902 auto IC = LoopDirective->counters().begin();
903 for (auto F : LoopDirective->finals()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000904 auto *D =
905 cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl();
906 if (NoFinals)
907 AlreadyEmittedVars.insert(D);
908 else
909 LoopCountersAndUpdates[D] = F;
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000910 ++IC;
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000911 }
912 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000913 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
914 auto IRef = C->varlist_begin();
915 auto ISrcRef = C->source_exprs().begin();
916 auto IDestRef = C->destination_exprs().begin();
917 for (auto *AssignOp : C->assignment_ops()) {
918 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
919 QualType Type = PrivateVD->getType();
920 auto *CanonicalVD = PrivateVD->getCanonicalDecl();
921 if (AlreadyEmittedVars.insert(CanonicalVD).second) {
922 // If lastprivate variable is a loop control variable for loop-based
923 // directive, update its value before copyin back to original
924 // variable.
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000925 if (auto *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD))
926 EmitIgnoredExpr(FinalExpr);
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000927 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
928 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
929 // Get the address of the original variable.
930 Address OriginalAddr = GetAddrOfLocalVar(DestVD);
931 // Get the address of the private variable.
932 Address PrivateAddr = GetAddrOfLocalVar(PrivateVD);
933 if (auto RefTy = PrivateVD->getType()->getAs<ReferenceType>())
934 PrivateAddr =
John McCall7f416cc2015-09-08 08:05:57 +0000935 Address(Builder.CreateLoad(PrivateAddr),
936 getNaturalTypeAlignment(RefTy->getPointeeType()));
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000937 EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp);
Alexey Bataev38e89532015-04-16 04:54:05 +0000938 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000939 ++IRef;
940 ++ISrcRef;
941 ++IDestRef;
Alexey Bataev38e89532015-04-16 04:54:05 +0000942 }
Alexey Bataev005248a2016-02-25 05:25:57 +0000943 if (auto *PostUpdate = C->getPostUpdateExpr())
944 EmitIgnoredExpr(PostUpdate);
Alexey Bataev38e89532015-04-16 04:54:05 +0000945 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000946 if (IsLastIterCond)
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000947 EmitBlock(DoneBB, /*IsFinished=*/true);
Alexey Bataev38e89532015-04-16 04:54:05 +0000948}
949
Alexey Bataev31300ed2016-02-04 11:27:03 +0000950static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
951 LValue BaseLV, llvm::Value *Addr) {
952 Address Tmp = Address::invalid();
953 Address TopTmp = Address::invalid();
954 Address MostTopTmp = Address::invalid();
955 BaseTy = BaseTy.getNonReferenceType();
956 while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) &&
957 !CGF.getContext().hasSameType(BaseTy, ElTy)) {
958 Tmp = CGF.CreateMemTemp(BaseTy);
959 if (TopTmp.isValid())
960 CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp);
961 else
962 MostTopTmp = Tmp;
963 TopTmp = Tmp;
964 BaseTy = BaseTy->getPointeeType();
965 }
966 llvm::Type *Ty = BaseLV.getPointer()->getType();
967 if (Tmp.isValid())
968 Ty = Tmp.getElementType();
969 Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty);
970 if (Tmp.isValid()) {
971 CGF.Builder.CreateStore(Addr, Tmp);
972 return MostTopTmp;
973 }
974 return Address(Addr, BaseLV.getAlignment());
975}
976
977static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
978 LValue BaseLV) {
979 BaseTy = BaseTy.getNonReferenceType();
980 while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) &&
981 !CGF.getContext().hasSameType(BaseTy, ElTy)) {
982 if (auto *PtrTy = BaseTy->getAs<PointerType>())
983 BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy);
984 else {
985 BaseLV = CGF.EmitLoadOfReferenceLValue(BaseLV.getAddress(),
986 BaseTy->castAs<ReferenceType>());
987 }
988 BaseTy = BaseTy->getPointeeType();
989 }
990 return CGF.MakeAddrLValue(
991 Address(
992 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
993 BaseLV.getPointer(), CGF.ConvertTypeForMem(ElTy)->getPointerTo()),
994 BaseLV.getAlignment()),
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000995 BaseLV.getType(), BaseLV.getBaseInfo());
Alexey Bataev31300ed2016-02-04 11:27:03 +0000996}
997
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000998void CodeGenFunction::EmitOMPReductionClauseInit(
999 const OMPExecutableDirective &D,
1000 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001001 if (!HaveInsertPoint())
1002 return;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001003 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001004 auto ILHS = C->lhs_exprs().begin();
1005 auto IRHS = C->rhs_exprs().begin();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001006 auto IPriv = C->privates().begin();
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001007 auto IRed = C->reduction_ops().begin();
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001008 for (auto IRef : C->varlists()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001009 auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001010 auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
1011 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl());
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001012 auto *DRD = getReductionInit(*IRed);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001013 if (auto *OASE = dyn_cast<OMPArraySectionExpr>(IRef)) {
1014 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
1015 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
1016 Base = TempOASE->getBase()->IgnoreParenImpCasts();
1017 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
1018 Base = TempASE->getBase()->IgnoreParenImpCasts();
1019 auto *DE = cast<DeclRefExpr>(Base);
1020 auto *OrigVD = cast<VarDecl>(DE->getDecl());
1021 auto OASELValueLB = EmitOMPArraySectionExpr(OASE);
1022 auto OASELValueUB =
1023 EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false);
1024 auto OriginalBaseLValue = EmitLValue(DE);
Alexey Bataev31300ed2016-02-04 11:27:03 +00001025 LValue BaseLValue =
1026 loadToBegin(*this, OrigVD->getType(), OASELValueLB.getType(),
1027 OriginalBaseLValue);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001028 // Store the address of the original variable associated with the LHS
1029 // implicit variable.
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00001030 PrivateScope.addPrivate(LHSVD, [OASELValueLB]() -> Address {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001031 return OASELValueLB.getAddress();
1032 });
1033 // Emit reduction copy.
1034 bool IsRegistered = PrivateScope.addPrivate(
Alexey Bataev31300ed2016-02-04 11:27:03 +00001035 OrigVD, [this, OrigVD, PrivateVD, BaseLValue, OASELValueLB,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001036 OASELValueUB, OriginalBaseLValue, DRD, IRed]() -> Address {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001037 // Emit VarDecl with copy init for arrays.
1038 // Get the address of the original variable captured in current
1039 // captured region.
1040 auto *Size = Builder.CreatePtrDiff(OASELValueUB.getPointer(),
1041 OASELValueLB.getPointer());
1042 Size = Builder.CreateNUWAdd(
1043 Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1));
1044 CodeGenFunction::OpaqueValueMapping OpaqueMap(
1045 *this, cast<OpaqueValueExpr>(
1046 getContext()
1047 .getAsVariableArrayType(PrivateVD->getType())
1048 ->getSizeExpr()),
1049 RValue::get(Size));
1050 EmitVariablyModifiedType(PrivateVD->getType());
1051 auto Emission = EmitAutoVarAlloca(*PrivateVD);
1052 auto Addr = Emission.getAllocatedAddress();
1053 auto *Init = PrivateVD->getInit();
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001054 EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(),
1055 DRD ? *IRed : Init,
1056 OASELValueLB.getAddress());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001057 EmitAutoVarCleanups(Emission);
1058 // Emit private VarDecl with reduction init.
1059 auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(),
1060 OASELValueLB.getPointer());
1061 auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset);
Alexey Bataev31300ed2016-02-04 11:27:03 +00001062 return castToBase(*this, OrigVD->getType(),
1063 OASELValueLB.getType(), OriginalBaseLValue,
1064 Ptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001065 });
1066 assert(IsRegistered && "private var already registered as private");
1067 // Silence the warning about unused variable.
1068 (void)IsRegistered;
1069 PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address {
1070 return GetAddrOfLocalVar(PrivateVD);
1071 });
1072 } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(IRef)) {
1073 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
1074 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
1075 Base = TempASE->getBase()->IgnoreParenImpCasts();
1076 auto *DE = cast<DeclRefExpr>(Base);
1077 auto *OrigVD = cast<VarDecl>(DE->getDecl());
1078 auto ASELValue = EmitLValue(ASE);
1079 auto OriginalBaseLValue = EmitLValue(DE);
Alexey Bataev31300ed2016-02-04 11:27:03 +00001080 LValue BaseLValue = loadToBegin(
1081 *this, OrigVD->getType(), ASELValue.getType(), OriginalBaseLValue);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001082 // Store the address of the original variable associated with the LHS
1083 // implicit variable.
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00001084 PrivateScope.addPrivate(
1085 LHSVD, [ASELValue]() -> Address { return ASELValue.getAddress(); });
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001086 // Emit reduction copy.
1087 bool IsRegistered = PrivateScope.addPrivate(
Alexey Bataev31300ed2016-02-04 11:27:03 +00001088 OrigVD, [this, OrigVD, PrivateVD, BaseLValue, ASELValue,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001089 OriginalBaseLValue, DRD, IRed]() -> Address {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001090 // Emit private VarDecl with reduction init.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001091 AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD);
1092 auto Addr = Emission.getAllocatedAddress();
Alexey Bataev8fbae8cf2016-04-27 11:38:05 +00001093 if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001094 emitInitWithReductionInitializer(*this, DRD, *IRed, Addr,
1095 ASELValue.getAddress(),
1096 ASELValue.getType());
1097 } else
1098 EmitAutoVarInit(Emission);
1099 EmitAutoVarCleanups(Emission);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001100 auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(),
1101 ASELValue.getPointer());
1102 auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset);
Alexey Bataev31300ed2016-02-04 11:27:03 +00001103 return castToBase(*this, OrigVD->getType(), ASELValue.getType(),
1104 OriginalBaseLValue, Ptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001105 });
1106 assert(IsRegistered && "private var already registered as private");
1107 // Silence the warning about unused variable.
1108 (void)IsRegistered;
Alexey Bataev1189bd02016-01-26 12:20:39 +00001109 PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address {
1110 return Builder.CreateElementBitCast(
1111 GetAddrOfLocalVar(PrivateVD), ConvertTypeForMem(RHSVD->getType()),
1112 "rhs.begin");
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001113 });
1114 } else {
1115 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl());
Alexey Bataev1189bd02016-01-26 12:20:39 +00001116 QualType Type = PrivateVD->getType();
1117 if (getContext().getAsArrayType(Type)) {
1118 // Store the address of the original variable associated with the LHS
1119 // implicit variable.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001120 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
1121 CapturedStmtInfo->lookup(OrigVD) != nullptr,
1122 IRef->getType(), VK_LValue, IRef->getExprLoc());
Alexey Bataev1189bd02016-01-26 12:20:39 +00001123 Address OriginalAddr = EmitLValue(&DRE).getAddress();
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001124 PrivateScope.addPrivate(LHSVD, [this, &OriginalAddr,
Alexey Bataev1189bd02016-01-26 12:20:39 +00001125 LHSVD]() -> Address {
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001126 OriginalAddr = Builder.CreateElementBitCast(
1127 OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin");
1128 return OriginalAddr;
Alexey Bataev1189bd02016-01-26 12:20:39 +00001129 });
1130 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
1131 if (Type->isVariablyModifiedType()) {
1132 CodeGenFunction::OpaqueValueMapping OpaqueMap(
1133 *this, cast<OpaqueValueExpr>(
1134 getContext()
1135 .getAsVariableArrayType(PrivateVD->getType())
1136 ->getSizeExpr()),
1137 RValue::get(
1138 getTypeSize(OrigVD->getType().getNonReferenceType())));
1139 EmitVariablyModifiedType(Type);
1140 }
1141 auto Emission = EmitAutoVarAlloca(*PrivateVD);
1142 auto Addr = Emission.getAllocatedAddress();
1143 auto *Init = PrivateVD->getInit();
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001144 EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(),
1145 DRD ? *IRed : Init, OriginalAddr);
Alexey Bataev1189bd02016-01-26 12:20:39 +00001146 EmitAutoVarCleanups(Emission);
1147 return Emission.getAllocatedAddress();
1148 });
1149 assert(IsRegistered && "private var already registered as private");
1150 // Silence the warning about unused variable.
1151 (void)IsRegistered;
1152 PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address {
1153 return Builder.CreateElementBitCast(
1154 GetAddrOfLocalVar(PrivateVD),
1155 ConvertTypeForMem(RHSVD->getType()), "rhs.begin");
1156 });
1157 } else {
1158 // Store the address of the original variable associated with the LHS
1159 // implicit variable.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001160 Address OriginalAddr = Address::invalid();
1161 PrivateScope.addPrivate(LHSVD, [this, OrigVD, IRef,
1162 &OriginalAddr]() -> Address {
Alexey Bataev1189bd02016-01-26 12:20:39 +00001163 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
1164 CapturedStmtInfo->lookup(OrigVD) != nullptr,
1165 IRef->getType(), VK_LValue, IRef->getExprLoc());
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001166 OriginalAddr = EmitLValue(&DRE).getAddress();
1167 return OriginalAddr;
Alexey Bataev1189bd02016-01-26 12:20:39 +00001168 });
1169 // Emit reduction copy.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001170 bool IsRegistered = PrivateScope.addPrivate(
1171 OrigVD, [this, PrivateVD, OriginalAddr, DRD, IRed]() -> Address {
Alexey Bataev1189bd02016-01-26 12:20:39 +00001172 // Emit private VarDecl with reduction init.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001173 AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD);
1174 auto Addr = Emission.getAllocatedAddress();
Alexey Bataev8fbae8cf2016-04-27 11:38:05 +00001175 if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001176 emitInitWithReductionInitializer(*this, DRD, *IRed, Addr,
1177 OriginalAddr,
1178 PrivateVD->getType());
1179 } else
1180 EmitAutoVarInit(Emission);
1181 EmitAutoVarCleanups(Emission);
1182 return Addr;
Alexey Bataev1189bd02016-01-26 12:20:39 +00001183 });
1184 assert(IsRegistered && "private var already registered as private");
1185 // Silence the warning about unused variable.
1186 (void)IsRegistered;
1187 PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address {
1188 return GetAddrOfLocalVar(PrivateVD);
1189 });
1190 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001191 }
Richard Trieucc3949d2016-02-18 22:34:54 +00001192 ++ILHS;
1193 ++IRHS;
1194 ++IPriv;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001195 ++IRed;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001196 }
1197 }
1198}
1199
1200void CodeGenFunction::EmitOMPReductionClauseFinal(
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001201 const OMPExecutableDirective &D, const OpenMPDirectiveKind ReductionKind) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001202 if (!HaveInsertPoint())
1203 return;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001204 llvm::SmallVector<const Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001205 llvm::SmallVector<const Expr *, 8> LHSExprs;
1206 llvm::SmallVector<const Expr *, 8> RHSExprs;
1207 llvm::SmallVector<const Expr *, 8> ReductionOps;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001208 bool HasAtLeastOneReduction = false;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001209 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001210 HasAtLeastOneReduction = true;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001211 Privates.append(C->privates().begin(), C->privates().end());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001212 LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end());
1213 RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end());
1214 ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end());
1215 }
1216 if (HasAtLeastOneReduction) {
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001217 bool WithNowait = D.getSingleClause<OMPNowaitClause>() ||
1218 isOpenMPParallelDirective(D.getDirectiveKind()) ||
1219 D.getDirectiveKind() == OMPD_simd;
1220 bool SimpleReduction = D.getDirectiveKind() == OMPD_simd;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001221 // Emit nowait reduction if nowait clause is present or directive is a
1222 // parallel directive (it always has implicit barrier).
1223 CGM.getOpenMPRuntime().emitReduction(
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001224 *this, D.getLocEnd(), Privates, LHSExprs, RHSExprs, ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001225 {WithNowait, SimpleReduction, ReductionKind});
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001226 }
1227}
1228
Alexey Bataev61205072016-03-02 04:57:40 +00001229static void emitPostUpdateForReductionClause(
1230 CodeGenFunction &CGF, const OMPExecutableDirective &D,
1231 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
1232 if (!CGF.HaveInsertPoint())
1233 return;
1234 llvm::BasicBlock *DoneBB = nullptr;
1235 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
1236 if (auto *PostUpdate = C->getPostUpdateExpr()) {
1237 if (!DoneBB) {
1238 if (auto *Cond = CondGen(CGF)) {
1239 // If the first post-update expression is found, emit conditional
1240 // block if it was requested.
1241 auto *ThenBB = CGF.createBasicBlock(".omp.reduction.pu");
1242 DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done");
1243 CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1244 CGF.EmitBlock(ThenBB);
1245 }
1246 }
1247 CGF.EmitIgnoredExpr(PostUpdate);
1248 }
1249 }
1250 if (DoneBB)
1251 CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
1252}
1253
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001254namespace {
1255/// Codegen lambda for appending distribute lower and upper bounds to outlined
1256/// parallel function. This is necessary for combined constructs such as
1257/// 'distribute parallel for'
1258typedef llvm::function_ref<void(CodeGenFunction &,
1259 const OMPExecutableDirective &,
1260 llvm::SmallVectorImpl<llvm::Value *> &)>
1261 CodeGenBoundParametersTy;
1262} // anonymous namespace
1263
1264static void emitCommonOMPParallelDirective(
1265 CodeGenFunction &CGF, const OMPExecutableDirective &S,
1266 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1267 const CodeGenBoundParametersTy &CodeGenBoundParameters) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00001268 const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel);
1269 auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction(
1270 S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001271 if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) {
Alexey Bataev1d677132015-04-22 13:57:31 +00001272 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
Alexey Bataev1d677132015-04-22 13:57:31 +00001273 auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
1274 /*IgnoreResultAssign*/ true);
1275 CGF.CGM.getOpenMPRuntime().emitNumThreadsClause(
1276 CGF, NumThreads, NumThreadsClause->getLocStart());
1277 }
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001278 if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001279 CodeGenFunction::RunCleanupsScope ProcBindScope(CGF);
Alexey Bataev7f210c62015-06-18 13:40:03 +00001280 CGF.CGM.getOpenMPRuntime().emitProcBindClause(
1281 CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getLocStart());
1282 }
Alexey Bataev1d677132015-04-22 13:57:31 +00001283 const Expr *IfCond = nullptr;
Alexey Bataev7371aa32015-09-03 08:45:56 +00001284 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
1285 if (C->getNameModifier() == OMPD_unknown ||
1286 C->getNameModifier() == OMPD_parallel) {
1287 IfCond = C->getCondition();
1288 break;
1289 }
Alexey Bataev1d677132015-04-22 13:57:31 +00001290 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001291
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00001292 OMPParallelScope Scope(CGF, S);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001293 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001294 // Combining 'distribute' with 'for' requires sharing each 'distribute' chunk
1295 // lower and upper bounds with the pragma 'for' chunking mechanism.
1296 // The following lambda takes care of appending the lower and upper bound
1297 // parameters when necessary
1298 CodeGenBoundParameters(CGF, S, CapturedVars);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001299 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
Alexey Bataev1d677132015-04-22 13:57:31 +00001300 CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +00001301 CapturedVars, IfCond);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001302}
1303
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001304static void emitEmptyBoundParameters(CodeGenFunction &,
1305 const OMPExecutableDirective &,
1306 llvm::SmallVectorImpl<llvm::Value *> &) {}
1307
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001308void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001309 // Emit parallel region as a standalone region.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001310 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001311 OMPPrivateScope PrivateScope(CGF);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001312 bool Copyins = CGF.EmitOMPCopyinClause(S);
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00001313 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
1314 if (Copyins) {
Alexey Bataev69c62a92015-04-15 04:52:20 +00001315 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00001316 // propagation master's thread values of threadprivate variables to local
1317 // instances of that variables of all other implicit threads.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001318 CGF.CGM.getOpenMPRuntime().emitBarrierCall(
1319 CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
1320 /*ForceSimpleCall=*/true);
Alexey Bataev69c62a92015-04-15 04:52:20 +00001321 }
1322 CGF.EmitOMPPrivateClause(S, PrivateScope);
1323 CGF.EmitOMPReductionClauseInit(S, PrivateScope);
1324 (void)PrivateScope.Privatize();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001325 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001326 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001327 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001328 emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen,
1329 emitEmptyBoundParameters);
Alexey Bataev61205072016-03-02 04:57:40 +00001330 emitPostUpdateForReductionClause(
1331 *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev9959db52014-05-06 10:08:46 +00001332}
Alexander Musman515ad8c2014-05-22 08:54:05 +00001333
Alexey Bataev0f34da12015-07-02 04:17:07 +00001334void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D,
1335 JumpDest LoopExit) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00001336 RunCleanupsScope BodyScope(*this);
1337 // Update counters values on current iteration.
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001338 for (auto I : D.updates()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00001339 EmitIgnoredExpr(I);
1340 }
Alexander Musman3276a272015-03-21 10:12:56 +00001341 // Update the linear variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001342 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001343 for (auto *U : C->updates())
Alexander Musman3276a272015-03-21 10:12:56 +00001344 EmitIgnoredExpr(U);
Alexander Musman3276a272015-03-21 10:12:56 +00001345 }
1346
Alexander Musmana5f070a2014-10-01 06:03:56 +00001347 // On a continue in the body, jump to the end.
Alexander Musmand196ef22014-10-07 08:57:09 +00001348 auto Continue = getJumpDestInCurrentScope("omp.body.continue");
Alexey Bataev0f34da12015-07-02 04:17:07 +00001349 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
Alexander Musmana5f070a2014-10-01 06:03:56 +00001350 // Emit loop body.
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001351 EmitStmt(D.getBody());
Alexander Musmana5f070a2014-10-01 06:03:56 +00001352 // The end (updates/cleanups).
1353 EmitBlock(Continue.getBlock());
1354 BreakContinueStack.pop_back();
Alexander Musmana5f070a2014-10-01 06:03:56 +00001355}
1356
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001357void CodeGenFunction::EmitOMPInnerLoop(
1358 const Stmt &S, bool RequiresCleanup, const Expr *LoopCond,
1359 const Expr *IncExpr,
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001360 const llvm::function_ref<void(CodeGenFunction &)> &BodyGen,
1361 const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) {
Alexander Musmand196ef22014-10-07 08:57:09 +00001362 auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001363
1364 // Start the loop with a block that tests the condition.
Alexander Musmand196ef22014-10-07 08:57:09 +00001365 auto CondBlock = createBasicBlock("omp.inner.for.cond");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001366 EmitBlock(CondBlock);
Amara Emerson652795d2016-11-10 14:44:30 +00001367 const SourceRange &R = S.getSourceRange();
1368 LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()),
1369 SourceLocToDebugLoc(R.getEnd()));
Alexander Musmana5f070a2014-10-01 06:03:56 +00001370
1371 // If there are any cleanups between here and the loop-exit scope,
1372 // create a block to stage a loop exit along.
1373 auto ExitBlock = LoopExit.getBlock();
Alexey Bataev2df54a02015-03-12 08:53:29 +00001374 if (RequiresCleanup)
Alexander Musmand196ef22014-10-07 08:57:09 +00001375 ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001376
Alexander Musmand196ef22014-10-07 08:57:09 +00001377 auto LoopBody = createBasicBlock("omp.inner.for.body");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001378
Alexey Bataev2df54a02015-03-12 08:53:29 +00001379 // Emit condition.
Justin Bogner66242d62015-04-23 23:06:47 +00001380 EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S));
Alexander Musmana5f070a2014-10-01 06:03:56 +00001381 if (ExitBlock != LoopExit.getBlock()) {
1382 EmitBlock(ExitBlock);
1383 EmitBranchThroughCleanup(LoopExit);
1384 }
1385
1386 EmitBlock(LoopBody);
Justin Bogner66242d62015-04-23 23:06:47 +00001387 incrementProfileCounter(&S);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001388
1389 // Create a block for the increment.
Alexander Musmand196ef22014-10-07 08:57:09 +00001390 auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001391 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1392
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001393 BodyGen(*this);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001394
1395 // Emit "IV = IV + 1" and a back-edge to the condition block.
1396 EmitBlock(Continue.getBlock());
Alexey Bataev2df54a02015-03-12 08:53:29 +00001397 EmitIgnoredExpr(IncExpr);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001398 PostIncGen(*this);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001399 BreakContinueStack.pop_back();
1400 EmitBranch(CondBlock);
1401 LoopStack.pop();
1402 // Emit the fall-through block.
1403 EmitBlock(LoopExit.getBlock());
1404}
1405
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001406void CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001407 if (!HaveInsertPoint())
1408 return;
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001409 // Emit inits for the linear variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001410 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001411 for (auto *Init : C->inits()) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001412 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl());
Alexey Bataevef549a82016-03-09 09:49:09 +00001413 if (auto *Ref = dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) {
1414 AutoVarEmission Emission = EmitAutoVarAlloca(*VD);
1415 auto *OrigVD = cast<VarDecl>(Ref->getDecl());
1416 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
1417 CapturedStmtInfo->lookup(OrigVD) != nullptr,
1418 VD->getInit()->getType(), VK_LValue,
1419 VD->getInit()->getExprLoc());
1420 EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(),
1421 VD->getType()),
1422 /*capturedByInit=*/false);
1423 EmitAutoVarCleanups(Emission);
1424 } else
1425 EmitVarDecl(*VD);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001426 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001427 // Emit the linear steps for the linear clauses.
1428 // If a step is not constant, it is pre-calculated before the loop.
1429 if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep()))
1430 if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) {
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001431 EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl()));
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001432 // Emit calculation of the linear step.
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001433 EmitIgnoredExpr(CS);
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001434 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001435 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001436}
1437
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001438void CodeGenFunction::EmitOMPLinearClauseFinal(
1439 const OMPLoopDirective &D,
Alexey Bataevef549a82016-03-09 09:49:09 +00001440 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001441 if (!HaveInsertPoint())
Alexey Bataev8ef31412015-12-18 07:58:25 +00001442 return;
Alexey Bataevef549a82016-03-09 09:49:09 +00001443 llvm::BasicBlock *DoneBB = nullptr;
Alexander Musman3276a272015-03-21 10:12:56 +00001444 // Emit the final values of the linear variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001445 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataev39f915b82015-05-08 10:41:21 +00001446 auto IC = C->varlist_begin();
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001447 for (auto *F : C->finals()) {
Alexey Bataevef549a82016-03-09 09:49:09 +00001448 if (!DoneBB) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001449 if (auto *Cond = CondGen(*this)) {
Alexey Bataevef549a82016-03-09 09:49:09 +00001450 // If the first post-update expression is found, emit conditional
1451 // block if it was requested.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001452 auto *ThenBB = createBasicBlock(".omp.linear.pu");
1453 DoneBB = createBasicBlock(".omp.linear.pu.done");
1454 Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1455 EmitBlock(ThenBB);
Alexey Bataevef549a82016-03-09 09:49:09 +00001456 }
1457 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00001458 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl());
1459 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001460 CapturedStmtInfo->lookup(OrigVD) != nullptr,
Alexey Bataev39f915b82015-05-08 10:41:21 +00001461 (*IC)->getType(), VK_LValue, (*IC)->getExprLoc());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001462 Address OrigAddr = EmitLValue(&DRE).getAddress();
1463 CodeGenFunction::OMPPrivateScope VarScope(*this);
1464 VarScope.addPrivate(OrigVD, [OrigAddr]() -> Address { return OrigAddr; });
Alexey Bataev39f915b82015-05-08 10:41:21 +00001465 (void)VarScope.Privatize();
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001466 EmitIgnoredExpr(F);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001467 ++IC;
Alexander Musman3276a272015-03-21 10:12:56 +00001468 }
Alexey Bataev78849fb2016-03-09 09:49:00 +00001469 if (auto *PostUpdate = C->getPostUpdateExpr())
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001470 EmitIgnoredExpr(PostUpdate);
Alexander Musman3276a272015-03-21 10:12:56 +00001471 }
Alexey Bataevef549a82016-03-09 09:49:09 +00001472 if (DoneBB)
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001473 EmitBlock(DoneBB, /*IsFinished=*/true);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001474}
1475
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001476static void emitAlignedClause(CodeGenFunction &CGF,
1477 const OMPExecutableDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001478 if (!CGF.HaveInsertPoint())
1479 return;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001480 for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001481 unsigned ClauseAlignment = 0;
1482 if (auto AlignmentExpr = Clause->getAlignment()) {
1483 auto AlignmentCI =
1484 cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr));
1485 ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue());
Alexander Musman09184fe2014-09-30 05:29:28 +00001486 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001487 for (auto E : Clause->varlists()) {
1488 unsigned Alignment = ClauseAlignment;
1489 if (Alignment == 0) {
1490 // OpenMP [2.8.1, Description]
1491 // If no optional parameter is specified, implementation-defined default
1492 // alignments for SIMD instructions on the target platforms are assumed.
1493 Alignment =
Alexey Bataev00396512015-07-02 03:40:19 +00001494 CGF.getContext()
1495 .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign(
1496 E->getType()->getPointeeType()))
1497 .getQuantity();
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001498 }
1499 assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) &&
1500 "alignment is not power of 2");
1501 if (Alignment != 0) {
1502 llvm::Value *PtrValue = CGF.EmitScalarExpr(E);
1503 CGF.EmitAlignmentAssumption(PtrValue, Alignment);
1504 }
Alexander Musman09184fe2014-09-30 05:29:28 +00001505 }
1506 }
1507}
1508
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001509void CodeGenFunction::EmitOMPPrivateLoopCounters(
1510 const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) {
1511 if (!HaveInsertPoint())
Alexey Bataev8ef31412015-12-18 07:58:25 +00001512 return;
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001513 auto I = S.private_counters().begin();
1514 for (auto *E : S.counters()) {
Alexey Bataeva8899172015-08-06 12:30:57 +00001515 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1516 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001517 (void)LoopScope.addPrivate(VD, [&]() -> Address {
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001518 // Emit var without initialization.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001519 if (!LocalDeclMap.count(PrivateVD)) {
1520 auto VarEmission = EmitAutoVarAlloca(*PrivateVD);
1521 EmitAutoVarCleanups(VarEmission);
1522 }
1523 DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD),
1524 /*RefersToEnclosingVariableOrCapture=*/false,
1525 (*I)->getType(), VK_LValue, (*I)->getExprLoc());
1526 return EmitLValue(&DRE).getAddress();
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001527 });
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001528 if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) ||
1529 VD->hasGlobalStorage()) {
1530 (void)LoopScope.addPrivate(PrivateVD, [&]() -> Address {
1531 DeclRefExpr DRE(const_cast<VarDecl *>(VD),
1532 LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD),
1533 E->getType(), VK_LValue, E->getExprLoc());
1534 return EmitLValue(&DRE).getAddress();
1535 });
1536 }
Alexey Bataeva8899172015-08-06 12:30:57 +00001537 ++I;
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001538 }
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001539}
1540
Alexey Bataev62dbb972015-04-22 11:59:37 +00001541static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S,
1542 const Expr *Cond, llvm::BasicBlock *TrueBlock,
1543 llvm::BasicBlock *FalseBlock, uint64_t TrueCount) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001544 if (!CGF.HaveInsertPoint())
1545 return;
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001546 {
1547 CodeGenFunction::OMPPrivateScope PreCondScope(CGF);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001548 CGF.EmitOMPPrivateLoopCounters(S, PreCondScope);
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001549 (void)PreCondScope.Privatize();
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001550 // Get initial values of real counters.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00001551 for (auto I : S.inits()) {
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001552 CGF.EmitIgnoredExpr(I);
1553 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00001554 }
1555 // Check that loop is executed at least one time.
1556 CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount);
1557}
1558
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001559void CodeGenFunction::EmitOMPLinearClause(
1560 const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) {
1561 if (!HaveInsertPoint())
Alexey Bataev8ef31412015-12-18 07:58:25 +00001562 return;
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001563 llvm::DenseSet<const VarDecl *> SIMDLCVs;
1564 if (isOpenMPSimdDirective(D.getDirectiveKind())) {
1565 auto *LoopDirective = cast<OMPLoopDirective>(&D);
1566 for (auto *C : LoopDirective->counters()) {
1567 SIMDLCVs.insert(
1568 cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl());
1569 }
1570 }
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001571 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001572 auto CurPrivate = C->privates().begin();
Alexey Bataevc925aa32015-04-27 08:00:32 +00001573 for (auto *E : C->varlists()) {
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001574 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1575 auto *PrivateVD =
1576 cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001577 if (!SIMDLCVs.count(VD->getCanonicalDecl())) {
1578 bool IsRegistered = PrivateScope.addPrivate(VD, [&]() -> Address {
1579 // Emit private VarDecl with copy init.
1580 EmitVarDecl(*PrivateVD);
1581 return GetAddrOfLocalVar(PrivateVD);
1582 });
1583 assert(IsRegistered && "linear var already registered as private");
1584 // Silence the warning about unused variable.
1585 (void)IsRegistered;
1586 } else
1587 EmitVarDecl(*PrivateVD);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001588 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00001589 }
1590 }
1591}
1592
Alexey Bataev45bfad52015-08-21 12:19:04 +00001593static void emitSimdlenSafelenClause(CodeGenFunction &CGF,
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001594 const OMPExecutableDirective &D,
1595 bool IsMonotonic) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001596 if (!CGF.HaveInsertPoint())
1597 return;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001598 if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) {
Alexey Bataev45bfad52015-08-21 12:19:04 +00001599 RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(),
1600 /*ignoreResult=*/true);
1601 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
1602 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
1603 // In presence of finite 'safelen', it may be unsafe to mark all
1604 // the memory instructions parallel, because loop-carried
1605 // dependences of 'safelen' iterations are possible.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001606 if (!IsMonotonic)
1607 CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>());
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001608 } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001609 RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(),
1610 /*ignoreResult=*/true);
1611 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
Tyler Nowickida46d0e2015-07-14 23:03:09 +00001612 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001613 // In presence of finite 'safelen', it may be unsafe to mark all
1614 // the memory instructions parallel, because loop-carried
1615 // dependences of 'safelen' iterations are possible.
1616 CGF.LoopStack.setParallel(false);
1617 }
1618}
1619
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001620void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D,
1621 bool IsMonotonic) {
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001622 // Walk clauses and process safelen/lastprivate.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001623 LoopStack.setParallel(!IsMonotonic);
Tyler Nowickida46d0e2015-07-14 23:03:09 +00001624 LoopStack.setVectorizeEnable(true);
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001625 emitSimdlenSafelenClause(*this, D, IsMonotonic);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001626}
1627
Alexey Bataevef549a82016-03-09 09:49:09 +00001628void CodeGenFunction::EmitOMPSimdFinal(
1629 const OMPLoopDirective &D,
1630 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001631 if (!HaveInsertPoint())
1632 return;
Alexey Bataevef549a82016-03-09 09:49:09 +00001633 llvm::BasicBlock *DoneBB = nullptr;
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001634 auto IC = D.counters().begin();
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001635 auto IPC = D.private_counters().begin();
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001636 for (auto F : D.finals()) {
1637 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001638 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl());
1639 auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD);
1640 if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) ||
1641 OrigVD->hasGlobalStorage() || CED) {
Alexey Bataevef549a82016-03-09 09:49:09 +00001642 if (!DoneBB) {
1643 if (auto *Cond = CondGen(*this)) {
1644 // If the first post-update expression is found, emit conditional
1645 // block if it was requested.
1646 auto *ThenBB = createBasicBlock(".omp.final.then");
1647 DoneBB = createBasicBlock(".omp.final.done");
1648 Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1649 EmitBlock(ThenBB);
1650 }
1651 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001652 Address OrigAddr = Address::invalid();
1653 if (CED)
1654 OrigAddr = EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress();
1655 else {
1656 DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD),
1657 /*RefersToEnclosingVariableOrCapture=*/false,
1658 (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc());
1659 OrigAddr = EmitLValue(&DRE).getAddress();
1660 }
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001661 OMPPrivateScope VarScope(*this);
1662 VarScope.addPrivate(OrigVD,
John McCall7f416cc2015-09-08 08:05:57 +00001663 [OrigAddr]() -> Address { return OrigAddr; });
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001664 (void)VarScope.Privatize();
1665 EmitIgnoredExpr(F);
1666 }
1667 ++IC;
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001668 ++IPC;
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001669 }
Alexey Bataevef549a82016-03-09 09:49:09 +00001670 if (DoneBB)
1671 EmitBlock(DoneBB, /*IsFinished=*/true);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001672}
1673
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001674static void emitOMPLoopBodyWithStopPoint(CodeGenFunction &CGF,
1675 const OMPLoopDirective &S,
1676 CodeGenFunction::JumpDest LoopExit) {
1677 CGF.EmitOMPLoopBody(S, LoopExit);
1678 CGF.EmitStopPoint(&S);
Hans Wennborged129ae2017-04-27 17:02:25 +00001679}
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001680
Alexander Musman515ad8c2014-05-22 08:54:05 +00001681void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001682 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00001683 OMPLoopScope PreInitScope(CGF, S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001684 // if (PreCond) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001685 // for (IV in 0..LastIteration) BODY;
1686 // <Final counter/linear vars updates>;
Alexey Bataev62dbb972015-04-22 11:59:37 +00001687 // }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001688 //
Alexander Musmana5f070a2014-10-01 06:03:56 +00001689
Alexey Bataev62dbb972015-04-22 11:59:37 +00001690 // Emit: if (PreCond) - begin.
1691 // If the condition constant folds and can be elided, avoid emitting the
1692 // whole loop.
1693 bool CondConstant;
1694 llvm::BasicBlock *ContBlock = nullptr;
1695 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
1696 if (!CondConstant)
1697 return;
1698 } else {
Alexey Bataev62dbb972015-04-22 11:59:37 +00001699 auto *ThenBlock = CGF.createBasicBlock("simd.if.then");
1700 ContBlock = CGF.createBasicBlock("simd.if.end");
Justin Bogner66242d62015-04-23 23:06:47 +00001701 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
1702 CGF.getProfileCount(&S));
Alexey Bataev62dbb972015-04-22 11:59:37 +00001703 CGF.EmitBlock(ThenBlock);
Justin Bogner66242d62015-04-23 23:06:47 +00001704 CGF.incrementProfileCounter(&S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001705 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001706
1707 // Emit the loop iteration variable.
1708 const Expr *IVExpr = S.getIterationVariable();
1709 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
1710 CGF.EmitVarDecl(*IVDecl);
1711 CGF.EmitIgnoredExpr(S.getInit());
1712
1713 // Emit the iterations count variable.
1714 // If it is not a variable, Sema decided to calculate iterations count on
Alexey Bataev7a228ff2015-05-21 07:59:51 +00001715 // each iteration (e.g., it is foldable into a constant).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001716 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
1717 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
1718 // Emit calculation of the iterations count.
1719 CGF.EmitIgnoredExpr(S.getCalcLastIteration());
Alexander Musmana5f070a2014-10-01 06:03:56 +00001720 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001721
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001722 CGF.EmitOMPSimdInit(S);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001723
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001724 emitAlignedClause(CGF, S);
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001725 CGF.EmitOMPLinearClauseInit(S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001726 {
1727 OMPPrivateScope LoopScope(CGF);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001728 CGF.EmitOMPPrivateLoopCounters(S, LoopScope);
1729 CGF.EmitOMPLinearClause(S, LoopScope);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001730 CGF.EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00001731 CGF.EmitOMPReductionClauseInit(S, LoopScope);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001732 bool HasLastprivateClause =
1733 CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001734 (void)LoopScope.Privatize();
Alexey Bataev0f34da12015-07-02 04:17:07 +00001735 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
1736 S.getInc(),
Alexey Bataev62dbb972015-04-22 11:59:37 +00001737 [&S](CodeGenFunction &CGF) {
Alexey Bataev0f34da12015-07-02 04:17:07 +00001738 CGF.EmitOMPLoopBody(S, JumpDest());
Alexey Bataev62dbb972015-04-22 11:59:37 +00001739 CGF.EmitStopPoint(&S);
1740 },
1741 [](CodeGenFunction &) {});
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001742 CGF.EmitOMPSimdFinal(
1743 S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataevfc087ec2015-06-16 13:14:42 +00001744 // Emit final copy of the lastprivate variables at the end of loops.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001745 if (HasLastprivateClause)
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001746 CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001747 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_simd);
Alexey Bataev61205072016-03-02 04:57:40 +00001748 emitPostUpdateForReductionClause(
1749 CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev62dbb972015-04-22 11:59:37 +00001750 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001751 CGF.EmitOMPLinearClauseFinal(
Alexey Bataevef549a82016-03-09 09:49:09 +00001752 S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev62dbb972015-04-22 11:59:37 +00001753 // Emit: if (PreCond) - end.
1754 if (ContBlock) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001755 CGF.EmitBranch(ContBlock);
1756 CGF.EmitBlock(ContBlock, true);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001757 }
1758 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00001759 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001760 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
Alexander Musman515ad8c2014-05-22 08:54:05 +00001761}
1762
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001763void CodeGenFunction::EmitOMPOuterLoop(
1764 bool DynamicOrOrdered, bool IsMonotonic, const OMPLoopDirective &S,
1765 CodeGenFunction::OMPPrivateScope &LoopScope,
1766 const CodeGenFunction::OMPLoopArguments &LoopArgs,
1767 const CodeGenFunction::CodeGenLoopTy &CodeGenLoop,
1768 const CodeGenFunction::CodeGenOrderedTy &CodeGenOrdered) {
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001769 auto &RT = CGM.getOpenMPRuntime();
Alexander Musman92bdaab2015-03-12 13:37:50 +00001770
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001771 const Expr *IVExpr = S.getIterationVariable();
1772 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1773 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1774
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001775 auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end");
1776
1777 // Start the loop with a block that tests the condition.
1778 auto CondBlock = createBasicBlock("omp.dispatch.cond");
1779 EmitBlock(CondBlock);
Amara Emerson652795d2016-11-10 14:44:30 +00001780 const SourceRange &R = S.getSourceRange();
1781 LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()),
1782 SourceLocToDebugLoc(R.getEnd()));
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001783
1784 llvm::Value *BoolCondVal = nullptr;
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001785 if (!DynamicOrOrdered) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001786 // UB = min(UB, GlobalUB) or
1787 // UB = min(UB, PrevUB) for combined loop sharing constructs (e.g.
1788 // 'distribute parallel for')
1789 EmitIgnoredExpr(LoopArgs.EUB);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001790 // IV = LB
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001791 EmitIgnoredExpr(LoopArgs.Init);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001792 // IV < UB
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001793 BoolCondVal = EvaluateExprAsBool(LoopArgs.Cond);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001794 } else {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001795 BoolCondVal =
1796 RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned, LoopArgs.IL,
1797 LoopArgs.LB, LoopArgs.UB, LoopArgs.ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001798 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001799
1800 // If there are any cleanups between here and the loop-exit scope,
1801 // create a block to stage a loop exit along.
1802 auto ExitBlock = LoopExit.getBlock();
1803 if (LoopScope.requiresCleanups())
1804 ExitBlock = createBasicBlock("omp.dispatch.cleanup");
1805
1806 auto LoopBody = createBasicBlock("omp.dispatch.body");
1807 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
1808 if (ExitBlock != LoopExit.getBlock()) {
1809 EmitBlock(ExitBlock);
1810 EmitBranchThroughCleanup(LoopExit);
1811 }
1812 EmitBlock(LoopBody);
1813
Alexander Musman92bdaab2015-03-12 13:37:50 +00001814 // Emit "IV = LB" (in case of static schedule, we have already calculated new
1815 // LB for loop condition and emitted it above).
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001816 if (DynamicOrOrdered)
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001817 EmitIgnoredExpr(LoopArgs.Init);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001818
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001819 // Create a block for the increment.
1820 auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc");
1821 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1822
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001823 // Generate !llvm.loop.parallel metadata for loads and stores for loops
1824 // with dynamic/guided scheduling and without ordered clause.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001825 if (!isOpenMPSimdDirective(S.getDirectiveKind()))
1826 LoopStack.setParallel(!IsMonotonic);
1827 else
1828 EmitOMPSimdInit(S, IsMonotonic);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001829
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001830 SourceLocation Loc = S.getLocStart();
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001831
1832 // when 'distribute' is not combined with a 'for':
1833 // while (idx <= UB) { BODY; ++idx; }
1834 // when 'distribute' is combined with a 'for'
1835 // (e.g. 'distribute parallel for')
1836 // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; }
1837 EmitOMPInnerLoop(
1838 S, LoopScope.requiresCleanups(), LoopArgs.Cond, LoopArgs.IncExpr,
1839 [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) {
1840 CodeGenLoop(CGF, S, LoopExit);
1841 },
1842 [IVSize, IVSigned, Loc, &CodeGenOrdered](CodeGenFunction &CGF) {
1843 CodeGenOrdered(CGF, Loc, IVSize, IVSigned);
1844 });
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001845
1846 EmitBlock(Continue.getBlock());
1847 BreakContinueStack.pop_back();
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001848 if (!DynamicOrOrdered) {
Alexander Musman92bdaab2015-03-12 13:37:50 +00001849 // Emit "LB = LB + Stride", "UB = UB + Stride".
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001850 EmitIgnoredExpr(LoopArgs.NextLB);
1851 EmitIgnoredExpr(LoopArgs.NextUB);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001852 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001853
1854 EmitBranch(CondBlock);
1855 LoopStack.pop();
1856 // Emit the fall-through block.
1857 EmitBlock(LoopExit.getBlock());
1858
1859 // Tell the runtime we are done.
Alexey Bataev957d8562016-11-17 15:12:05 +00001860 auto &&CodeGen = [DynamicOrOrdered, &S](CodeGenFunction &CGF) {
1861 if (!DynamicOrOrdered)
1862 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd());
1863 };
1864 OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001865}
1866
1867void CodeGenFunction::EmitOMPForOuterLoop(
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001868 const OpenMPScheduleTy &ScheduleKind, bool IsMonotonic,
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001869 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001870 const OMPLoopArguments &LoopArgs,
1871 const CodeGenDispatchBoundsTy &CGDispatchBounds) {
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001872 auto &RT = CGM.getOpenMPRuntime();
1873
1874 // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime).
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001875 const bool DynamicOrOrdered =
1876 Ordered || RT.isDynamic(ScheduleKind.Schedule);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001877
1878 assert((Ordered ||
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001879 !RT.isStaticNonchunked(ScheduleKind.Schedule,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001880 LoopArgs.Chunk != nullptr)) &&
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001881 "static non-chunked schedule does not need outer loop");
1882
1883 // Emit outer loop.
1884 //
1885 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1886 // When schedule(dynamic,chunk_size) is specified, the iterations are
1887 // distributed to threads in the team in chunks as the threads request them.
1888 // Each thread executes a chunk of iterations, then requests another chunk,
1889 // until no chunks remain to be distributed. Each chunk contains chunk_size
1890 // iterations, except for the last chunk to be distributed, which may have
1891 // fewer iterations. When no chunk_size is specified, it defaults to 1.
1892 //
1893 // When schedule(guided,chunk_size) is specified, the iterations are assigned
1894 // to threads in the team in chunks as the executing threads request them.
1895 // Each thread executes a chunk of iterations, then requests another chunk,
1896 // until no chunks remain to be assigned. For a chunk_size of 1, the size of
1897 // each chunk is proportional to the number of unassigned iterations divided
1898 // by the number of threads in the team, decreasing to 1. For a chunk_size
1899 // with value k (greater than 1), the size of each chunk is determined in the
1900 // same way, with the restriction that the chunks do not contain fewer than k
1901 // iterations (except for the last chunk to be assigned, which may have fewer
1902 // than k iterations).
1903 //
1904 // When schedule(auto) is specified, the decision regarding scheduling is
1905 // delegated to the compiler and/or runtime system. The programmer gives the
1906 // implementation the freedom to choose any possible mapping of iterations to
1907 // threads in the team.
1908 //
1909 // When schedule(runtime) is specified, the decision regarding scheduling is
1910 // deferred until run time, and the schedule and chunk size are taken from the
1911 // run-sched-var ICV. If the ICV is set to auto, the schedule is
1912 // implementation defined
1913 //
1914 // while(__kmpc_dispatch_next(&LB, &UB)) {
1915 // idx = LB;
1916 // while (idx <= UB) { BODY; ++idx;
1917 // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only.
1918 // } // inner loop
1919 // }
1920 //
1921 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1922 // When schedule(static, chunk_size) is specified, iterations are divided into
1923 // chunks of size chunk_size, and the chunks are assigned to the threads in
1924 // the team in a round-robin fashion in the order of the thread number.
1925 //
1926 // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) {
1927 // while (idx <= UB) { BODY; ++idx; } // inner loop
1928 // LB = LB + ST;
1929 // UB = UB + ST;
1930 // }
1931 //
1932
1933 const Expr *IVExpr = S.getIterationVariable();
1934 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1935 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1936
1937 if (DynamicOrOrdered) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001938 auto DispatchBounds = CGDispatchBounds(*this, S, LoopArgs.LB, LoopArgs.UB);
1939 llvm::Value *LBVal = DispatchBounds.first;
1940 llvm::Value *UBVal = DispatchBounds.second;
1941 CGOpenMPRuntime::DispatchRTInput DipatchRTInputValues = {LBVal, UBVal,
1942 LoopArgs.Chunk};
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001943 RT.emitForDispatchInit(*this, S.getLocStart(), ScheduleKind, IVSize,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001944 IVSigned, Ordered, DipatchRTInputValues);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001945 } else {
1946 RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind, IVSize, IVSigned,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001947 Ordered, LoopArgs.IL, LoopArgs.LB, LoopArgs.UB,
1948 LoopArgs.ST, LoopArgs.Chunk);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001949 }
1950
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001951 auto &&CodeGenOrdered = [Ordered](CodeGenFunction &CGF, SourceLocation Loc,
1952 const unsigned IVSize,
1953 const bool IVSigned) {
1954 if (Ordered) {
1955 CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(CGF, Loc, IVSize,
1956 IVSigned);
1957 }
1958 };
1959
1960 OMPLoopArguments OuterLoopArgs(LoopArgs.LB, LoopArgs.UB, LoopArgs.ST,
1961 LoopArgs.IL, LoopArgs.Chunk, LoopArgs.EUB);
1962 OuterLoopArgs.IncExpr = S.getInc();
1963 OuterLoopArgs.Init = S.getInit();
1964 OuterLoopArgs.Cond = S.getCond();
1965 OuterLoopArgs.NextLB = S.getNextLowerBound();
1966 OuterLoopArgs.NextUB = S.getNextUpperBound();
1967 EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, OuterLoopArgs,
1968 emitOMPLoopBodyWithStopPoint, CodeGenOrdered);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001969}
1970
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001971static void emitEmptyOrdered(CodeGenFunction &, SourceLocation Loc,
1972 const unsigned IVSize, const bool IVSigned) {}
1973
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001974void CodeGenFunction::EmitOMPDistributeOuterLoop(
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001975 OpenMPDistScheduleClauseKind ScheduleKind, const OMPLoopDirective &S,
1976 OMPPrivateScope &LoopScope, const OMPLoopArguments &LoopArgs,
1977 const CodeGenLoopTy &CodeGenLoopContent) {
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001978
1979 auto &RT = CGM.getOpenMPRuntime();
1980
1981 // Emit outer loop.
1982 // Same behavior as a OMPForOuterLoop, except that schedule cannot be
1983 // dynamic
1984 //
1985
1986 const Expr *IVExpr = S.getIterationVariable();
1987 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1988 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1989
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001990 RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind, IVSize,
1991 IVSigned, /* Ordered = */ false, LoopArgs.IL,
1992 LoopArgs.LB, LoopArgs.UB, LoopArgs.ST,
1993 LoopArgs.Chunk);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001994
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001995 // for combined 'distribute' and 'for' the increment expression of distribute
1996 // is store in DistInc. For 'distribute' alone, it is in Inc.
1997 Expr *IncExpr;
1998 if (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()))
1999 IncExpr = S.getDistInc();
2000 else
2001 IncExpr = S.getInc();
2002
2003 // this routine is shared by 'omp distribute parallel for' and
2004 // 'omp distribute': select the right EUB expression depending on the
2005 // directive
2006 OMPLoopArguments OuterLoopArgs;
2007 OuterLoopArgs.LB = LoopArgs.LB;
2008 OuterLoopArgs.UB = LoopArgs.UB;
2009 OuterLoopArgs.ST = LoopArgs.ST;
2010 OuterLoopArgs.IL = LoopArgs.IL;
2011 OuterLoopArgs.Chunk = LoopArgs.Chunk;
2012 OuterLoopArgs.EUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
2013 ? S.getCombinedEnsureUpperBound()
2014 : S.getEnsureUpperBound();
2015 OuterLoopArgs.IncExpr = IncExpr;
2016 OuterLoopArgs.Init = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
2017 ? S.getCombinedInit()
2018 : S.getInit();
2019 OuterLoopArgs.Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
2020 ? S.getCombinedCond()
2021 : S.getCond();
2022 OuterLoopArgs.NextLB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
2023 ? S.getCombinedNextLowerBound()
2024 : S.getNextLowerBound();
2025 OuterLoopArgs.NextUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
2026 ? S.getCombinedNextUpperBound()
2027 : S.getNextUpperBound();
2028
2029 EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false, S,
2030 LoopScope, OuterLoopArgs, CodeGenLoopContent,
2031 emitEmptyOrdered);
2032}
2033
2034/// Emit a helper variable and return corresponding lvalue.
2035static LValue EmitOMPHelperVar(CodeGenFunction &CGF,
2036 const DeclRefExpr *Helper) {
2037 auto VDecl = cast<VarDecl>(Helper->getDecl());
2038 CGF.EmitVarDecl(*VDecl);
2039 return CGF.EmitLValue(Helper);
2040}
2041
2042static std::pair<LValue, LValue>
2043emitDistributeParallelForInnerBounds(CodeGenFunction &CGF,
2044 const OMPExecutableDirective &S) {
2045 const OMPLoopDirective &LS = cast<OMPLoopDirective>(S);
2046 LValue LB =
2047 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable()));
2048 LValue UB =
2049 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable()));
2050
2051 // When composing 'distribute' with 'for' (e.g. as in 'distribute
2052 // parallel for') we need to use the 'distribute'
2053 // chunk lower and upper bounds rather than the whole loop iteration
2054 // space. These are parameters to the outlined function for 'parallel'
2055 // and we copy the bounds of the previous schedule into the
2056 // the current ones.
2057 LValue PrevLB = CGF.EmitLValue(LS.getPrevLowerBoundVariable());
2058 LValue PrevUB = CGF.EmitLValue(LS.getPrevUpperBoundVariable());
2059 llvm::Value *PrevLBVal = CGF.EmitLoadOfScalar(PrevLB, SourceLocation());
2060 PrevLBVal = CGF.EmitScalarConversion(
2061 PrevLBVal, LS.getPrevLowerBoundVariable()->getType(),
2062 LS.getIterationVariable()->getType(), SourceLocation());
2063 llvm::Value *PrevUBVal = CGF.EmitLoadOfScalar(PrevUB, SourceLocation());
2064 PrevUBVal = CGF.EmitScalarConversion(
2065 PrevUBVal, LS.getPrevUpperBoundVariable()->getType(),
2066 LS.getIterationVariable()->getType(), SourceLocation());
2067
2068 CGF.EmitStoreOfScalar(PrevLBVal, LB);
2069 CGF.EmitStoreOfScalar(PrevUBVal, UB);
2070
2071 return {LB, UB};
2072}
2073
2074/// if the 'for' loop has a dispatch schedule (e.g. dynamic, guided) then
2075/// we need to use the LB and UB expressions generated by the worksharing
2076/// code generation support, whereas in non combined situations we would
2077/// just emit 0 and the LastIteration expression
2078/// This function is necessary due to the difference of the LB and UB
2079/// types for the RT emission routines for 'for_static_init' and
2080/// 'for_dispatch_init'
2081static std::pair<llvm::Value *, llvm::Value *>
2082emitDistributeParallelForDispatchBounds(CodeGenFunction &CGF,
2083 const OMPExecutableDirective &S,
2084 Address LB, Address UB) {
2085 const OMPLoopDirective &LS = cast<OMPLoopDirective>(S);
2086 const Expr *IVExpr = LS.getIterationVariable();
2087 // when implementing a dynamic schedule for a 'for' combined with a
2088 // 'distribute' (e.g. 'distribute parallel for'), the 'for' loop
2089 // is not normalized as each team only executes its own assigned
2090 // distribute chunk
2091 QualType IteratorTy = IVExpr->getType();
2092 llvm::Value *LBVal = CGF.EmitLoadOfScalar(LB, /*Volatile=*/false, IteratorTy,
2093 SourceLocation());
2094 llvm::Value *UBVal = CGF.EmitLoadOfScalar(UB, /*Volatile=*/false, IteratorTy,
2095 SourceLocation());
2096 return {LBVal, UBVal};
Hans Wennborged129ae2017-04-27 17:02:25 +00002097}
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002098
2099static void emitDistributeParallelForDistributeInnerBoundParams(
2100 CodeGenFunction &CGF, const OMPExecutableDirective &S,
2101 llvm::SmallVectorImpl<llvm::Value *> &CapturedVars) {
2102 const auto &Dir = cast<OMPLoopDirective>(S);
2103 LValue LB =
2104 CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedLowerBoundVariable()));
2105 auto LBCast = CGF.Builder.CreateIntCast(
2106 CGF.Builder.CreateLoad(LB.getAddress()), CGF.SizeTy, /*isSigned=*/false);
2107 CapturedVars.push_back(LBCast);
2108 LValue UB =
2109 CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedUpperBoundVariable()));
2110
2111 auto UBCast = CGF.Builder.CreateIntCast(
2112 CGF.Builder.CreateLoad(UB.getAddress()), CGF.SizeTy, /*isSigned=*/false);
2113 CapturedVars.push_back(UBCast);
Hans Wennborged129ae2017-04-27 17:02:25 +00002114}
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002115
2116static void
2117emitInnerParallelForWhenCombined(CodeGenFunction &CGF,
2118 const OMPLoopDirective &S,
2119 CodeGenFunction::JumpDest LoopExit) {
2120 auto &&CGInlinedWorksharingLoop = [&S](CodeGenFunction &CGF,
2121 PrePostActionTy &) {
2122 CGF.EmitOMPWorksharingLoop(S, S.getPrevEnsureUpperBound(),
2123 emitDistributeParallelForInnerBounds,
2124 emitDistributeParallelForDispatchBounds);
2125 };
2126
2127 emitCommonOMPParallelDirective(
2128 CGF, S, OMPD_for, CGInlinedWorksharingLoop,
2129 emitDistributeParallelForDistributeInnerBoundParams);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002130}
2131
Carlo Bertolli9925f152016-06-27 14:55:37 +00002132void CodeGenFunction::EmitOMPDistributeParallelForDirective(
2133 const OMPDistributeParallelForDirective &S) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002134 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2135 CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined,
2136 S.getDistInc());
2137 };
Carlo Bertolli9925f152016-06-27 14:55:37 +00002138 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002139 OMPCancelStackRAII CancelRegion(*this, OMPD_distribute_parallel_for,
2140 /*HasCancel=*/false);
2141 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen,
2142 /*HasCancel=*/false);
Carlo Bertolli9925f152016-06-27 14:55:37 +00002143}
2144
Kelvin Li4a39add2016-07-05 05:00:15 +00002145void CodeGenFunction::EmitOMPDistributeParallelForSimdDirective(
2146 const OMPDistributeParallelForSimdDirective &S) {
2147 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2148 CGM.getOpenMPRuntime().emitInlinedDirective(
2149 *this, OMPD_distribute_parallel_for_simd,
2150 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2151 OMPLoopScope PreInitScope(CGF, S);
2152 CGF.EmitStmt(
2153 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2154 });
2155}
Kelvin Li787f3fc2016-07-06 04:45:38 +00002156
2157void CodeGenFunction::EmitOMPDistributeSimdDirective(
2158 const OMPDistributeSimdDirective &S) {
2159 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2160 CGM.getOpenMPRuntime().emitInlinedDirective(
2161 *this, OMPD_distribute_simd,
2162 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2163 OMPLoopScope PreInitScope(CGF, S);
2164 CGF.EmitStmt(
2165 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2166 });
2167}
2168
Kelvin Lia579b912016-07-14 02:54:56 +00002169void CodeGenFunction::EmitOMPTargetParallelForSimdDirective(
2170 const OMPTargetParallelForSimdDirective &S) {
2171 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2172 CGM.getOpenMPRuntime().emitInlinedDirective(
2173 *this, OMPD_target_parallel_for_simd,
2174 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2175 OMPLoopScope PreInitScope(CGF, S);
2176 CGF.EmitStmt(
2177 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2178 });
2179}
2180
Kelvin Li986330c2016-07-20 22:57:10 +00002181void CodeGenFunction::EmitOMPTargetSimdDirective(
2182 const OMPTargetSimdDirective &S) {
2183 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2184 CGM.getOpenMPRuntime().emitInlinedDirective(
2185 *this, OMPD_target_simd, [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2186 OMPLoopScope PreInitScope(CGF, S);
2187 CGF.EmitStmt(
2188 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2189 });
2190}
2191
Kelvin Li02532872016-08-05 14:37:37 +00002192void CodeGenFunction::EmitOMPTeamsDistributeDirective(
2193 const OMPTeamsDistributeDirective &S) {
2194 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2195 CGM.getOpenMPRuntime().emitInlinedDirective(
2196 *this, OMPD_teams_distribute,
2197 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2198 OMPLoopScope PreInitScope(CGF, S);
2199 CGF.EmitStmt(
2200 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2201 });
2202}
2203
Kelvin Li4e325f72016-10-25 12:50:55 +00002204void CodeGenFunction::EmitOMPTeamsDistributeSimdDirective(
2205 const OMPTeamsDistributeSimdDirective &S) {
2206 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2207 CGM.getOpenMPRuntime().emitInlinedDirective(
2208 *this, OMPD_teams_distribute_simd,
2209 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2210 OMPLoopScope PreInitScope(CGF, S);
2211 CGF.EmitStmt(
2212 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2213 });
2214}
2215
Kelvin Li579e41c2016-11-30 23:51:03 +00002216void CodeGenFunction::EmitOMPTeamsDistributeParallelForSimdDirective(
2217 const OMPTeamsDistributeParallelForSimdDirective &S) {
2218 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2219 CGM.getOpenMPRuntime().emitInlinedDirective(
2220 *this, OMPD_teams_distribute_parallel_for_simd,
2221 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2222 OMPLoopScope PreInitScope(CGF, S);
2223 CGF.EmitStmt(
2224 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2225 });
2226}
Kelvin Li4e325f72016-10-25 12:50:55 +00002227
Kelvin Li7ade93f2016-12-09 03:24:30 +00002228void CodeGenFunction::EmitOMPTeamsDistributeParallelForDirective(
2229 const OMPTeamsDistributeParallelForDirective &S) {
2230 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2231 CGM.getOpenMPRuntime().emitInlinedDirective(
2232 *this, OMPD_teams_distribute_parallel_for,
2233 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2234 OMPLoopScope PreInitScope(CGF, S);
2235 CGF.EmitStmt(
2236 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2237 });
2238}
2239
Kelvin Li83c451e2016-12-25 04:52:54 +00002240void CodeGenFunction::EmitOMPTargetTeamsDistributeDirective(
2241 const OMPTargetTeamsDistributeDirective &S) {
Kelvin Li26fd21a2016-12-28 17:57:07 +00002242 CGM.getOpenMPRuntime().emitInlinedDirective(
2243 *this, OMPD_target_teams_distribute,
Kelvin Li83c451e2016-12-25 04:52:54 +00002244 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Kelvin Li26fd21a2016-12-28 17:57:07 +00002245 CGF.EmitStmt(
2246 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Kelvin Li83c451e2016-12-25 04:52:54 +00002247 });
2248}
2249
Kelvin Li80e8f562016-12-29 22:16:30 +00002250void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDirective(
2251 const OMPTargetTeamsDistributeParallelForDirective &S) {
2252 CGM.getOpenMPRuntime().emitInlinedDirective(
2253 *this, OMPD_target_teams_distribute_parallel_for,
2254 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2255 CGF.EmitStmt(
2256 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2257 });
2258}
2259
Kelvin Li1851df52017-01-03 05:23:48 +00002260void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDirective(
2261 const OMPTargetTeamsDistributeParallelForSimdDirective &S) {
2262 CGM.getOpenMPRuntime().emitInlinedDirective(
2263 *this, OMPD_target_teams_distribute_parallel_for_simd,
2264 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2265 CGF.EmitStmt(
2266 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2267 });
2268}
2269
Kelvin Lida681182017-01-10 18:08:18 +00002270void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDirective(
2271 const OMPTargetTeamsDistributeSimdDirective &S) {
2272 CGM.getOpenMPRuntime().emitInlinedDirective(
2273 *this, OMPD_target_teams_distribute_simd,
2274 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2275 CGF.EmitStmt(
2276 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2277 });
2278}
2279
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002280namespace {
2281 struct ScheduleKindModifiersTy {
2282 OpenMPScheduleClauseKind Kind;
2283 OpenMPScheduleClauseModifier M1;
2284 OpenMPScheduleClauseModifier M2;
2285 ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind,
2286 OpenMPScheduleClauseModifier M1,
2287 OpenMPScheduleClauseModifier M2)
2288 : Kind(Kind), M1(M1), M2(M2) {}
2289 };
2290} // namespace
2291
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002292bool CodeGenFunction::EmitOMPWorksharingLoop(
2293 const OMPLoopDirective &S, Expr *EUB,
2294 const CodeGenLoopBoundsTy &CodeGenLoopBounds,
2295 const CodeGenDispatchBoundsTy &CGDispatchBounds) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002296 // Emit the loop iteration variable.
2297 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
2298 auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
2299 EmitVarDecl(*IVDecl);
2300
2301 // Emit the iterations count variable.
2302 // If it is not a variable, Sema decided to calculate iterations count on each
2303 // iteration (e.g., it is foldable into a constant).
2304 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
2305 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
2306 // Emit calculation of the iterations count.
2307 EmitIgnoredExpr(S.getCalcLastIteration());
2308 }
2309
2310 auto &RT = CGM.getOpenMPRuntime();
2311
Alexey Bataev38e89532015-04-16 04:54:05 +00002312 bool HasLastprivateClause;
Alexander Musmanc6388682014-12-15 07:07:06 +00002313 // Check pre-condition.
2314 {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002315 OMPLoopScope PreInitScope(*this, S);
Alexander Musmanc6388682014-12-15 07:07:06 +00002316 // Skip the entire loop if we don't meet the precondition.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002317 // If the condition constant folds and can be elided, avoid emitting the
2318 // whole loop.
2319 bool CondConstant;
2320 llvm::BasicBlock *ContBlock = nullptr;
2321 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
2322 if (!CondConstant)
2323 return false;
2324 } else {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002325 auto *ThenBlock = createBasicBlock("omp.precond.then");
2326 ContBlock = createBasicBlock("omp.precond.end");
2327 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
Justin Bogner66242d62015-04-23 23:06:47 +00002328 getProfileCount(&S));
Alexey Bataev62dbb972015-04-22 11:59:37 +00002329 EmitBlock(ThenBlock);
Justin Bogner66242d62015-04-23 23:06:47 +00002330 incrementProfileCounter(&S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00002331 }
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00002332
Alexey Bataev8b427062016-05-25 12:36:08 +00002333 bool Ordered = false;
2334 if (auto *OrderedClause = S.getSingleClause<OMPOrderedClause>()) {
2335 if (OrderedClause->getNumForLoops())
2336 RT.emitDoacrossInit(*this, S);
2337 else
2338 Ordered = true;
2339 }
2340
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002341 llvm::DenseSet<const Expr *> EmittedFinals;
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00002342 emitAlignedClause(*this, S);
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00002343 EmitOMPLinearClauseInit(S);
Alexey Bataevef549a82016-03-09 09:49:09 +00002344 // Emit helper vars inits.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002345
2346 std::pair<LValue, LValue> Bounds = CodeGenLoopBounds(*this, S);
2347 LValue LB = Bounds.first;
2348 LValue UB = Bounds.second;
Alexey Bataevef549a82016-03-09 09:49:09 +00002349 LValue ST =
2350 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
2351 LValue IL =
2352 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
2353
Alexander Musmanc6388682014-12-15 07:07:06 +00002354 // Emit 'then' code.
2355 {
Alexander Musmanc6388682014-12-15 07:07:06 +00002356 OMPPrivateScope LoopScope(*this);
Alexey Bataev69c62a92015-04-15 04:52:20 +00002357 if (EmitOMPFirstprivateClause(S, LoopScope)) {
2358 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00002359 // initialization of firstprivate variables and post-update of
2360 // lastprivate variables.
Alexey Bataev25e5b442015-09-15 12:52:43 +00002361 CGM.getOpenMPRuntime().emitBarrierCall(
2362 *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
2363 /*ForceSimpleCall=*/true);
Alexey Bataev69c62a92015-04-15 04:52:20 +00002364 }
Alexey Bataev50a64582015-04-22 12:24:45 +00002365 EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev38e89532015-04-16 04:54:05 +00002366 HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev7ebe5fd2015-04-22 13:43:03 +00002367 EmitOMPReductionClauseInit(S, LoopScope);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002368 EmitOMPPrivateLoopCounters(S, LoopScope);
2369 EmitOMPLinearClause(S, LoopScope);
Alexander Musman7931b982015-03-16 07:14:41 +00002370 (void)LoopScope.Privatize();
Alexander Musmanc6388682014-12-15 07:07:06 +00002371
2372 // Detect the loop schedule kind and chunk.
Alexey Bataev3392d762016-02-16 11:18:12 +00002373 llvm::Value *Chunk = nullptr;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002374 OpenMPScheduleTy ScheduleKind;
Alexey Bataev3392d762016-02-16 11:18:12 +00002375 if (auto *C = S.getSingleClause<OMPScheduleClause>()) {
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002376 ScheduleKind.Schedule = C->getScheduleKind();
2377 ScheduleKind.M1 = C->getFirstScheduleModifier();
2378 ScheduleKind.M2 = C->getSecondScheduleModifier();
Alexey Bataev3392d762016-02-16 11:18:12 +00002379 if (const auto *Ch = C->getChunkSize()) {
2380 Chunk = EmitScalarExpr(Ch);
2381 Chunk = EmitScalarConversion(Chunk, Ch->getType(),
2382 S.getIterationVariable()->getType(),
2383 S.getLocStart());
2384 }
2385 }
Alexander Musmanc6388682014-12-15 07:07:06 +00002386 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
2387 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002388 // OpenMP 4.5, 2.7.1 Loop Construct, Description.
2389 // If the static schedule kind is specified or if the ordered clause is
2390 // specified, and if no monotonic modifier is specified, the effect will
2391 // be as if the monotonic modifier was specified.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002392 if (RT.isStaticNonchunked(ScheduleKind.Schedule,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002393 /* Chunked */ Chunk != nullptr) &&
2394 !Ordered) {
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002395 if (isOpenMPSimdDirective(S.getDirectiveKind()))
2396 EmitOMPSimdInit(S, /*IsMonotonic=*/true);
Alexander Musmanc6388682014-12-15 07:07:06 +00002397 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
2398 // When no chunk_size is specified, the iteration space is divided into
2399 // chunks that are approximately equal in size, and at most one chunk is
2400 // distributed to each thread. Note that the size of the chunks is
2401 // unspecified in this case.
John McCall7f416cc2015-09-08 08:05:57 +00002402 RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind,
2403 IVSize, IVSigned, Ordered,
2404 IL.getAddress(), LB.getAddress(),
2405 UB.getAddress(), ST.getAddress());
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002406 auto LoopExit =
2407 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
Alexander Musmanc6388682014-12-15 07:07:06 +00002408 // UB = min(UB, GlobalUB);
2409 EmitIgnoredExpr(S.getEnsureUpperBound());
2410 // IV = LB;
2411 EmitIgnoredExpr(S.getInit());
2412 // while (idx <= UB) { BODY; ++idx; }
Alexey Bataevae05c292015-06-16 11:59:36 +00002413 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
2414 S.getInc(),
Alexey Bataev0f34da12015-07-02 04:17:07 +00002415 [&S, LoopExit](CodeGenFunction &CGF) {
2416 CGF.EmitOMPLoopBody(S, LoopExit);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002417 CGF.EmitStopPoint(&S);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002418 },
2419 [](CodeGenFunction &) {});
Alexey Bataev0f34da12015-07-02 04:17:07 +00002420 EmitBlock(LoopExit.getBlock());
Alexander Musmanc6388682014-12-15 07:07:06 +00002421 // Tell the runtime we are done.
Alexey Bataev957d8562016-11-17 15:12:05 +00002422 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
2423 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd());
2424 };
2425 OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002426 } else {
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002427 const bool IsMonotonic =
2428 Ordered || ScheduleKind.Schedule == OMPC_SCHEDULE_static ||
2429 ScheduleKind.Schedule == OMPC_SCHEDULE_unknown ||
2430 ScheduleKind.M1 == OMPC_SCHEDULE_MODIFIER_monotonic ||
2431 ScheduleKind.M2 == OMPC_SCHEDULE_MODIFIER_monotonic;
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002432 // Emit the outer loop, which requests its work chunk [LB..UB] from
2433 // runtime and runs the inner loop to process it.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002434 const OMPLoopArguments LoopArguments(LB.getAddress(), UB.getAddress(),
2435 ST.getAddress(), IL.getAddress(),
2436 Chunk, EUB);
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002437 EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002438 LoopArguments, CGDispatchBounds);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002439 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002440 if (isOpenMPSimdDirective(S.getDirectiveKind())) {
2441 EmitOMPSimdFinal(S,
2442 [&](CodeGenFunction &CGF) -> llvm::Value * {
2443 return CGF.Builder.CreateIsNotNull(
2444 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2445 });
2446 }
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002447 EmitOMPReductionClauseFinal(
2448 S, /*ReductionKind=*/isOpenMPSimdDirective(S.getDirectiveKind())
2449 ? /*Parallel and Simd*/ OMPD_parallel_for_simd
2450 : /*Parallel only*/ OMPD_parallel);
Alexey Bataev61205072016-03-02 04:57:40 +00002451 // Emit post-update of the reduction variables if IsLastIter != 0.
2452 emitPostUpdateForReductionClause(
2453 *this, S, [&](CodeGenFunction &CGF) -> llvm::Value * {
2454 return CGF.Builder.CreateIsNotNull(
2455 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2456 });
Alexey Bataev38e89532015-04-16 04:54:05 +00002457 // Emit final copy of the lastprivate variables if IsLastIter != 0.
2458 if (HasLastprivateClause)
2459 EmitOMPLastprivateClauseFinal(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002460 S, isOpenMPSimdDirective(S.getDirectiveKind()),
2461 Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart())));
Alexander Musmanc6388682014-12-15 07:07:06 +00002462 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002463 EmitOMPLinearClauseFinal(S, [&](CodeGenFunction &CGF) -> llvm::Value * {
Alexey Bataevef549a82016-03-09 09:49:09 +00002464 return CGF.Builder.CreateIsNotNull(
2465 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2466 });
Alexander Musmanc6388682014-12-15 07:07:06 +00002467 // We're now done with the loop, so jump to the continuation block.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002468 if (ContBlock) {
2469 EmitBranch(ContBlock);
2470 EmitBlock(ContBlock, true);
2471 }
Alexander Musmanc6388682014-12-15 07:07:06 +00002472 }
Alexey Bataev38e89532015-04-16 04:54:05 +00002473 return HasLastprivateClause;
Alexander Musmanc6388682014-12-15 07:07:06 +00002474}
2475
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002476/// The following two functions generate expressions for the loop lower
2477/// and upper bounds in case of static and dynamic (dispatch) schedule
2478/// of the associated 'for' or 'distribute' loop.
2479static std::pair<LValue, LValue>
2480emitForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S) {
2481 const OMPLoopDirective &LS = cast<OMPLoopDirective>(S);
2482 LValue LB =
2483 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable()));
2484 LValue UB =
2485 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable()));
2486 return {LB, UB};
2487}
2488
2489/// When dealing with dispatch schedules (e.g. dynamic, guided) we do not
2490/// consider the lower and upper bound expressions generated by the
2491/// worksharing loop support, but we use 0 and the iteration space size as
2492/// constants
2493static std::pair<llvm::Value *, llvm::Value *>
2494emitDispatchForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S,
2495 Address LB, Address UB) {
2496 const OMPLoopDirective &LS = cast<OMPLoopDirective>(S);
2497 const Expr *IVExpr = LS.getIterationVariable();
2498 const unsigned IVSize = CGF.getContext().getTypeSize(IVExpr->getType());
2499 llvm::Value *LBVal = CGF.Builder.getIntN(IVSize, 0);
2500 llvm::Value *UBVal = CGF.EmitScalarExpr(LS.getLastIteration());
2501 return {LBVal, UBVal};
2502}
2503
Alexander Musmanc6388682014-12-15 07:07:06 +00002504void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) {
Alexey Bataev38e89532015-04-16 04:54:05 +00002505 bool HasLastprivates = false;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002506 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF,
2507 PrePostActionTy &) {
Alexey Bataev957d8562016-11-17 15:12:05 +00002508 OMPCancelStackRAII CancelRegion(CGF, OMPD_for, S.hasCancel());
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002509 HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(),
2510 emitForLoopBounds,
2511 emitDispatchForLoopBounds);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002512 };
Alexey Bataev3392d762016-02-16 11:18:12 +00002513 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002514 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002515 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen,
2516 S.hasCancel());
2517 }
Alexander Musmanc6388682014-12-15 07:07:06 +00002518
2519 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002520 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) {
Alexey Bataevf2685682015-03-30 04:30:22 +00002521 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
2522 }
Alexey Bataevf29276e2014-06-18 04:14:57 +00002523}
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002524
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002525void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002526 bool HasLastprivates = false;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002527 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF,
2528 PrePostActionTy &) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002529 HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(),
2530 emitForLoopBounds,
2531 emitDispatchForLoopBounds);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002532 };
Alexey Bataev3392d762016-02-16 11:18:12 +00002533 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002534 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002535 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
2536 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002537
2538 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002539 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002540 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
2541 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00002542}
2543
Alexey Bataev2df54a02015-03-12 08:53:29 +00002544static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty,
2545 const Twine &Name,
2546 llvm::Value *Init = nullptr) {
John McCall7f416cc2015-09-08 08:05:57 +00002547 auto LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty);
Alexey Bataev2df54a02015-03-12 08:53:29 +00002548 if (Init)
Akira Hatanaka642f7992016-10-18 19:05:41 +00002549 CGF.EmitStoreThroughLValue(RValue::get(Init), LVal, /*isInit*/ true);
Alexey Bataev2df54a02015-03-12 08:53:29 +00002550 return LVal;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002551}
2552
Alexey Bataev3392d762016-02-16 11:18:12 +00002553void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) {
Alexey Bataev2df54a02015-03-12 08:53:29 +00002554 auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt();
2555 auto *CS = dyn_cast<CompoundStmt>(Stmt);
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002556 bool HasLastprivates = false;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002557 auto &&CodeGen = [&S, Stmt, CS, &HasLastprivates](CodeGenFunction &CGF,
2558 PrePostActionTy &) {
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002559 auto &C = CGF.CGM.getContext();
2560 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
2561 // Emit helper vars inits.
2562 LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.",
2563 CGF.Builder.getInt32(0));
2564 auto *GlobalUBVal = CS != nullptr ? CGF.Builder.getInt32(CS->size() - 1)
2565 : CGF.Builder.getInt32(0);
2566 LValue UB =
2567 createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal);
2568 LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.",
2569 CGF.Builder.getInt32(1));
2570 LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.",
2571 CGF.Builder.getInt32(0));
2572 // Loop counter.
2573 LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv.");
2574 OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
2575 CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV);
2576 OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
2577 CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB);
2578 // Generate condition for loop.
2579 BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue,
Adam Nemet484aa452017-03-27 19:17:25 +00002580 OK_Ordinary, S.getLocStart(), FPOptions());
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002581 // Increment for loop counter.
2582 UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary,
2583 S.getLocStart());
2584 auto BodyGen = [Stmt, CS, &S, &IV](CodeGenFunction &CGF) {
2585 // Iterate through all sections and emit a switch construct:
2586 // switch (IV) {
2587 // case 0:
2588 // <SectionStmt[0]>;
2589 // break;
2590 // ...
2591 // case <NumSection> - 1:
2592 // <SectionStmt[<NumSection> - 1]>;
2593 // break;
2594 // }
2595 // .omp.sections.exit:
2596 auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit");
2597 auto *SwitchStmt = CGF.Builder.CreateSwitch(
2598 CGF.EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB,
2599 CS == nullptr ? 1 : CS->size());
2600 if (CS) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002601 unsigned CaseNumber = 0;
Benjamin Kramer642f1732015-07-02 21:03:14 +00002602 for (auto *SubStmt : CS->children()) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002603 auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
2604 CGF.EmitBlock(CaseBB);
2605 SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB);
Benjamin Kramer642f1732015-07-02 21:03:14 +00002606 CGF.EmitStmt(SubStmt);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002607 CGF.EmitBranch(ExitBB);
Benjamin Kramer642f1732015-07-02 21:03:14 +00002608 ++CaseNumber;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002609 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002610 } else {
2611 auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
2612 CGF.EmitBlock(CaseBB);
2613 SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB);
2614 CGF.EmitStmt(Stmt);
2615 CGF.EmitBranch(ExitBB);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002616 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002617 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
Alexey Bataev2df54a02015-03-12 08:53:29 +00002618 };
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002619
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002620 CodeGenFunction::OMPPrivateScope LoopScope(CGF);
2621 if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) {
Alexey Bataev9efc03b2015-04-27 04:34:03 +00002622 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00002623 // initialization of firstprivate variables and post-update of lastprivate
2624 // variables.
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002625 CGF.CGM.getOpenMPRuntime().emitBarrierCall(
2626 CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
2627 /*ForceSimpleCall=*/true);
Alexey Bataev9efc03b2015-04-27 04:34:03 +00002628 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002629 CGF.EmitOMPPrivateClause(S, LoopScope);
2630 HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
2631 CGF.EmitOMPReductionClauseInit(S, LoopScope);
2632 (void)LoopScope.Privatize();
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002633
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002634 // Emit static non-chunked loop.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002635 OpenMPScheduleTy ScheduleKind;
2636 ScheduleKind.Schedule = OMPC_SCHEDULE_static;
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002637 CGF.CGM.getOpenMPRuntime().emitForStaticInit(
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002638 CGF, S.getLocStart(), ScheduleKind, /*IVSize=*/32,
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002639 /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(), LB.getAddress(),
2640 UB.getAddress(), ST.getAddress());
2641 // UB = min(UB, GlobalUB);
2642 auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart());
2643 auto *MinUBGlobalUB = CGF.Builder.CreateSelect(
2644 CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal);
2645 CGF.EmitStoreOfScalar(MinUBGlobalUB, UB);
2646 // IV = LB;
2647 CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV);
2648 // while (idx <= UB) { BODY; ++idx; }
2649 CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen,
2650 [](CodeGenFunction &) {});
2651 // Tell the runtime we are done.
Alexey Bataev957d8562016-11-17 15:12:05 +00002652 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
2653 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd());
2654 };
2655 CGF.OMPCancelStack.emitExit(CGF, S.getDirectiveKind(), CodeGen);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002656 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel);
Alexey Bataev61205072016-03-02 04:57:40 +00002657 // Emit post-update of the reduction variables if IsLastIter != 0.
2658 emitPostUpdateForReductionClause(
2659 CGF, S, [&](CodeGenFunction &CGF) -> llvm::Value * {
2660 return CGF.Builder.CreateIsNotNull(
2661 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2662 });
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002663
2664 // Emit final copy of the lastprivate variables if IsLastIter != 0.
2665 if (HasLastprivates)
2666 CGF.EmitOMPLastprivateClauseFinal(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002667 S, /*NoFinals=*/false,
2668 CGF.Builder.CreateIsNotNull(
2669 CGF.EmitLoadOfScalar(IL, S.getLocStart())));
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002670 };
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002671
2672 bool HasCancel = false;
2673 if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S))
2674 HasCancel = OSD->hasCancel();
2675 else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S))
2676 HasCancel = OPSD->hasCancel();
Alexey Bataev957d8562016-11-17 15:12:05 +00002677 OMPCancelStackRAII CancelRegion(*this, S.getDirectiveKind(), HasCancel);
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002678 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen,
2679 HasCancel);
2680 // Emit barrier for lastprivates only if 'sections' directive has 'nowait'
2681 // clause. Otherwise the barrier will be generated by the codegen for the
2682 // directive.
2683 if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) {
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002684 // Emit implicit barrier to synchronize threads and avoid data races on
2685 // initialization of firstprivate variables.
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002686 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
2687 OMPD_unknown);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002688 }
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002689}
Alexey Bataev2df54a02015-03-12 08:53:29 +00002690
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002691void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
Alexey Bataev3392d762016-02-16 11:18:12 +00002692 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002693 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002694 EmitSections(S);
2695 }
Alexey Bataev2df54a02015-03-12 08:53:29 +00002696 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002697 if (!S.getSingleClause<OMPNowaitClause>()) {
Alexey Bataev3392d762016-02-16 11:18:12 +00002698 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
2699 OMPD_sections);
Alexey Bataevf2685682015-03-30 04:30:22 +00002700 }
Alexey Bataev2df54a02015-03-12 08:53:29 +00002701}
2702
2703void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002704 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002705 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002706 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002707 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev25e5b442015-09-15 12:52:43 +00002708 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen,
2709 S.hasCancel());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002710}
2711
Alexey Bataev6956e2e2015-02-05 06:35:41 +00002712void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00002713 llvm::SmallVector<const Expr *, 8> CopyprivateVars;
Alexey Bataev420d45b2015-04-14 05:11:24 +00002714 llvm::SmallVector<const Expr *, 8> DestExprs;
Alexey Bataeva63048e2015-03-23 06:18:07 +00002715 llvm::SmallVector<const Expr *, 8> SrcExprs;
Alexey Bataeva63048e2015-03-23 06:18:07 +00002716 llvm::SmallVector<const Expr *, 8> AssignmentOps;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002717 // Check if there are any 'copyprivate' clauses associated with this
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00002718 // 'single' construct.
Alexey Bataeva63048e2015-03-23 06:18:07 +00002719 // Build a list of copyprivate variables along with helper expressions
2720 // (<source>, <destination>, <destination>=<source> expressions)
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002721 for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00002722 CopyprivateVars.append(C->varlists().begin(), C->varlists().end());
Alexey Bataev420d45b2015-04-14 05:11:24 +00002723 DestExprs.append(C->destination_exprs().begin(),
2724 C->destination_exprs().end());
Alexey Bataeva63048e2015-03-23 06:18:07 +00002725 SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end());
Alexey Bataeva63048e2015-03-23 06:18:07 +00002726 AssignmentOps.append(C->assignment_ops().begin(),
2727 C->assignment_ops().end());
2728 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002729 // Emit code for 'single' region along with 'copyprivate' clauses
2730 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2731 Action.Enter(CGF);
2732 OMPPrivateScope SingleScope(CGF);
2733 (void)CGF.EmitOMPFirstprivateClause(S, SingleScope);
2734 CGF.EmitOMPPrivateClause(S, SingleScope);
2735 (void)SingleScope.Privatize();
2736 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2737 };
Alexey Bataev3392d762016-02-16 11:18:12 +00002738 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002739 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002740 CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(),
2741 CopyprivateVars, DestExprs,
2742 SrcExprs, AssignmentOps);
2743 }
2744 // Emit an implicit barrier at the end (to avoid data race on firstprivate
2745 // init or if no 'nowait' clause was specified and no 'copyprivate' clause).
Alexey Bataev417089f2016-02-17 13:19:37 +00002746 if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) {
Alexey Bataev5521d782015-04-24 04:21:15 +00002747 CGM.getOpenMPRuntime().emitBarrierCall(
2748 *this, S.getLocStart(),
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002749 S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single);
Alexey Bataevf2685682015-03-30 04:30:22 +00002750 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002751}
2752
Alexey Bataev8d690652014-12-04 07:23:53 +00002753void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002754 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2755 Action.Enter(CGF);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002756 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002757 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002758 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002759 CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart());
Alexander Musman80c22892014-07-17 08:54:58 +00002760}
2761
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002762void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002763 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2764 Action.Enter(CGF);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002765 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002766 };
Alexey Bataevfc57d162015-12-15 10:55:09 +00002767 Expr *Hint = nullptr;
2768 if (auto *HintClause = S.getSingleClause<OMPHintClause>())
2769 Hint = HintClause->getHint();
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002770 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataevfc57d162015-12-15 10:55:09 +00002771 CGM.getOpenMPRuntime().emitCriticalRegion(*this,
2772 S.getDirectiveName().getAsString(),
2773 CodeGen, S.getLocStart(), Hint);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002774}
2775
Alexey Bataev671605e2015-04-13 05:28:11 +00002776void CodeGenFunction::EmitOMPParallelForDirective(
2777 const OMPParallelForDirective &S) {
2778 // Emit directive as a combined directive that consists of two implicit
2779 // directives: 'parallel' with 'for' directive.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002780 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev957d8562016-11-17 15:12:05 +00002781 OMPCancelStackRAII CancelRegion(CGF, OMPD_parallel_for, S.hasCancel());
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002782 CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds,
2783 emitDispatchForLoopBounds);
Alexey Bataev671605e2015-04-13 05:28:11 +00002784 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002785 emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen,
2786 emitEmptyBoundParameters);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002787}
2788
Alexander Musmane4e893b2014-09-23 09:33:00 +00002789void CodeGenFunction::EmitOMPParallelForSimdDirective(
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00002790 const OMPParallelForSimdDirective &S) {
2791 // Emit directive as a combined directive that consists of two implicit
2792 // directives: 'parallel' with 'for' directive.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002793 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002794 CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds,
2795 emitDispatchForLoopBounds);
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00002796 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002797 emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen,
2798 emitEmptyBoundParameters);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002799}
2800
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002801void CodeGenFunction::EmitOMPParallelSectionsDirective(
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002802 const OMPParallelSectionsDirective &S) {
2803 // Emit directive as a combined directive that consists of two implicit
2804 // directives: 'parallel' with 'sections' directive.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002805 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2806 CGF.EmitSections(S);
2807 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002808 emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen,
2809 emitEmptyBoundParameters);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002810}
2811
Alexey Bataev7292c292016-04-25 12:22:29 +00002812void CodeGenFunction::EmitOMPTaskBasedDirective(const OMPExecutableDirective &S,
2813 const RegionCodeGenTy &BodyGen,
2814 const TaskGenTy &TaskGen,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002815 OMPTaskDataTy &Data) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002816 // Emit outlined function for task construct.
2817 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
Alexey Bataev62b63b12015-03-10 07:28:44 +00002818 auto *I = CS->getCapturedDecl()->param_begin();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002819 auto *PartId = std::next(I);
Alexey Bataev48591dd2016-04-20 04:01:36 +00002820 auto *TaskT = std::next(I, 4);
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002821 // Check if the task is final
2822 if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) {
2823 // If the condition constant folds and can be elided, try to avoid emitting
2824 // the condition and the dead arm of the if/else.
2825 auto *Cond = Clause->getCondition();
2826 bool CondConstant;
2827 if (ConstantFoldsToSimpleInteger(Cond, CondConstant))
2828 Data.Final.setInt(CondConstant);
2829 else
2830 Data.Final.setPointer(EvaluateExprAsBool(Cond));
2831 } else {
2832 // By default the task is not final.
2833 Data.Final.setInt(/*IntVal=*/false);
2834 }
Alexey Bataev1e1e2862016-05-10 12:21:02 +00002835 // Check if the task has 'priority' clause.
2836 if (const auto *Clause = S.getSingleClause<OMPPriorityClause>()) {
Alexey Bataev1e1e2862016-05-10 12:21:02 +00002837 auto *Prio = Clause->getPriority();
Alexey Bataev5140e742016-07-19 04:21:09 +00002838 Data.Priority.setInt(/*IntVal=*/true);
Alexey Bataevad537bb2016-05-30 09:06:50 +00002839 Data.Priority.setPointer(EmitScalarConversion(
2840 EmitScalarExpr(Prio), Prio->getType(),
2841 getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1),
2842 Prio->getExprLoc()));
Alexey Bataev1e1e2862016-05-10 12:21:02 +00002843 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00002844 // The first function argument for tasks is a thread id, the second one is a
2845 // part id (0 for tied tasks, >=0 for untied task).
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002846 llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
2847 // Get list of private variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002848 for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002849 auto IRef = C->varlist_begin();
2850 for (auto *IInit : C->private_copies()) {
2851 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2852 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
Alexey Bataev7292c292016-04-25 12:22:29 +00002853 Data.PrivateVars.push_back(*IRef);
2854 Data.PrivateCopies.push_back(IInit);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002855 }
2856 ++IRef;
2857 }
2858 }
2859 EmittedAsPrivate.clear();
2860 // Get list of firstprivate variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002861 for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002862 auto IRef = C->varlist_begin();
2863 auto IElemInitRef = C->inits().begin();
2864 for (auto *IInit : C->private_copies()) {
2865 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2866 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
Alexey Bataev7292c292016-04-25 12:22:29 +00002867 Data.FirstprivateVars.push_back(*IRef);
2868 Data.FirstprivateCopies.push_back(IInit);
2869 Data.FirstprivateInits.push_back(*IElemInitRef);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002870 }
Richard Trieucc3949d2016-02-18 22:34:54 +00002871 ++IRef;
2872 ++IElemInitRef;
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002873 }
2874 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00002875 // Get list of lastprivate variables (for taskloops).
2876 llvm::DenseMap<const VarDecl *, const DeclRefExpr *> LastprivateDstsOrigs;
2877 for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) {
2878 auto IRef = C->varlist_begin();
2879 auto ID = C->destination_exprs().begin();
2880 for (auto *IInit : C->private_copies()) {
2881 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2882 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
2883 Data.LastprivateVars.push_back(*IRef);
2884 Data.LastprivateCopies.push_back(IInit);
2885 }
2886 LastprivateDstsOrigs.insert(
2887 {cast<VarDecl>(cast<DeclRefExpr>(*ID)->getDecl()),
2888 cast<DeclRefExpr>(*IRef)});
2889 ++IRef;
2890 ++ID;
2891 }
2892 }
Alexey Bataev1d2353d2015-06-24 11:01:36 +00002893 // Build list of dependences.
Alexey Bataev7292c292016-04-25 12:22:29 +00002894 for (const auto *C : S.getClausesOfKind<OMPDependClause>())
2895 for (auto *IRef : C->varlists())
2896 Data.Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef));
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00002897 auto &&CodeGen = [&Data, CS, &BodyGen, &LastprivateDstsOrigs](
Alexey Bataevf93095a2016-05-05 08:46:22 +00002898 CodeGenFunction &CGF, PrePostActionTy &Action) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002899 // Set proper addresses for generated private copies.
Alexey Bataev7292c292016-04-25 12:22:29 +00002900 OMPPrivateScope Scope(CGF);
Alexey Bataevf93095a2016-05-05 08:46:22 +00002901 if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty() ||
2902 !Data.LastprivateVars.empty()) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00002903 auto *CopyFn = CGF.Builder.CreateLoad(
2904 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3)));
2905 auto *PrivatesPtr = CGF.Builder.CreateLoad(
2906 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2)));
2907 // Map privates.
2908 llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs;
2909 llvm::SmallVector<llvm::Value *, 16> CallArgs;
2910 CallArgs.push_back(PrivatesPtr);
Alexey Bataev7292c292016-04-25 12:22:29 +00002911 for (auto *E : Data.PrivateVars) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00002912 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2913 Address PrivatePtr = CGF.CreateMemTemp(
2914 CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr");
2915 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
2916 CallArgs.push_back(PrivatePtr.getPointer());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002917 }
Alexey Bataev7292c292016-04-25 12:22:29 +00002918 for (auto *E : Data.FirstprivateVars) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00002919 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2920 Address PrivatePtr =
2921 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()),
2922 ".firstpriv.ptr.addr");
2923 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
2924 CallArgs.push_back(PrivatePtr.getPointer());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002925 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00002926 for (auto *E : Data.LastprivateVars) {
2927 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2928 Address PrivatePtr =
2929 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()),
2930 ".lastpriv.ptr.addr");
2931 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
2932 CallArgs.push_back(PrivatePtr.getPointer());
2933 }
Alexey Bataev48591dd2016-04-20 04:01:36 +00002934 CGF.EmitRuntimeCall(CopyFn, CallArgs);
Alexey Bataevf93095a2016-05-05 08:46:22 +00002935 for (auto &&Pair : LastprivateDstsOrigs) {
2936 auto *OrigVD = cast<VarDecl>(Pair.second->getDecl());
2937 DeclRefExpr DRE(
2938 const_cast<VarDecl *>(OrigVD),
2939 /*RefersToEnclosingVariableOrCapture=*/CGF.CapturedStmtInfo->lookup(
2940 OrigVD) != nullptr,
2941 Pair.second->getType(), VK_LValue, Pair.second->getExprLoc());
2942 Scope.addPrivate(Pair.first, [&CGF, &DRE]() {
2943 return CGF.EmitLValue(&DRE).getAddress();
2944 });
2945 }
Alexey Bataev48591dd2016-04-20 04:01:36 +00002946 for (auto &&Pair : PrivatePtrs) {
2947 Address Replacement(CGF.Builder.CreateLoad(Pair.second),
2948 CGF.getContext().getDeclAlign(Pair.first));
2949 Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; });
2950 }
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002951 }
Alexey Bataev48591dd2016-04-20 04:01:36 +00002952 (void)Scope.Privatize();
2953
2954 Action.Enter(CGF);
Alexey Bataev7292c292016-04-25 12:22:29 +00002955 BodyGen(CGF);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002956 };
Alexey Bataev7292c292016-04-25 12:22:29 +00002957 auto *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction(
2958 S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied,
2959 Data.NumberOfParts);
2960 OMPLexicalScope Scope(*this, S);
2961 TaskGen(*this, OutlinedFn, Data);
2962}
2963
2964void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) {
2965 // Emit outlined function for task construct.
2966 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
2967 auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002968 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
Alexey Bataev1d677132015-04-22 13:57:31 +00002969 const Expr *IfCond = nullptr;
Alexey Bataev7371aa32015-09-03 08:45:56 +00002970 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
2971 if (C->getNameModifier() == OMPD_unknown ||
2972 C->getNameModifier() == OMPD_task) {
2973 IfCond = C->getCondition();
2974 break;
2975 }
Alexey Bataev1d677132015-04-22 13:57:31 +00002976 }
Alexey Bataev7292c292016-04-25 12:22:29 +00002977
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002978 OMPTaskDataTy Data;
2979 // Check if we should emit tied or untied task.
2980 Data.Tied = !S.getSingleClause<OMPUntiedClause>();
Alexey Bataev7292c292016-04-25 12:22:29 +00002981 auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) {
2982 CGF.EmitStmt(CS->getCapturedStmt());
2983 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002984 auto &&TaskGen = [&S, SharedsTy, CapturedStruct,
Alexey Bataev7292c292016-04-25 12:22:29 +00002985 IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002986 const OMPTaskDataTy &Data) {
2987 CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getLocStart(), S, OutlinedFn,
2988 SharedsTy, CapturedStruct, IfCond,
2989 Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00002990 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002991 EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002992}
2993
Alexey Bataev9f797f32015-02-05 05:57:51 +00002994void CodeGenFunction::EmitOMPTaskyieldDirective(
2995 const OMPTaskyieldDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002996 CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart());
Alexey Bataev68446b72014-07-18 07:47:19 +00002997}
2998
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00002999void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) {
Alexey Bataevf2685682015-03-30 04:30:22 +00003000 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier);
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003001}
3002
Alexey Bataev8b8e2022015-04-27 05:22:09 +00003003void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) {
3004 CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart());
Alexey Bataev2df347a2014-07-18 10:17:07 +00003005}
3006
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003007void CodeGenFunction::EmitOMPTaskgroupDirective(
3008 const OMPTaskgroupDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003009 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3010 Action.Enter(CGF);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003011 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003012 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00003013 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003014 CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getLocStart());
3015}
3016
Alexey Bataevcc37cc12014-11-20 04:34:54 +00003017void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003018 CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003019 if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003020 return llvm::makeArrayRef(FlushClause->varlist_begin(),
3021 FlushClause->varlist_end());
3022 }
3023 return llvm::None;
3024 }(), S.getLocStart());
Alexey Bataev6125da92014-07-21 11:26:11 +00003025}
3026
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003027void CodeGenFunction::EmitOMPDistributeLoop(const OMPLoopDirective &S,
3028 const CodeGenLoopTy &CodeGenLoop,
3029 Expr *IncExpr) {
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003030 // Emit the loop iteration variable.
3031 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
3032 auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
3033 EmitVarDecl(*IVDecl);
3034
3035 // Emit the iterations count variable.
3036 // If it is not a variable, Sema decided to calculate iterations count on each
3037 // iteration (e.g., it is foldable into a constant).
3038 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
3039 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
3040 // Emit calculation of the iterations count.
3041 EmitIgnoredExpr(S.getCalcLastIteration());
3042 }
3043
3044 auto &RT = CGM.getOpenMPRuntime();
3045
Carlo Bertolli962bb802017-01-03 18:24:42 +00003046 bool HasLastprivateClause = false;
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003047 // Check pre-condition.
3048 {
Alexey Bataev5a3af132016-03-29 08:58:54 +00003049 OMPLoopScope PreInitScope(*this, S);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003050 // Skip the entire loop if we don't meet the precondition.
3051 // If the condition constant folds and can be elided, avoid emitting the
3052 // whole loop.
3053 bool CondConstant;
3054 llvm::BasicBlock *ContBlock = nullptr;
3055 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
3056 if (!CondConstant)
3057 return;
3058 } else {
3059 auto *ThenBlock = createBasicBlock("omp.precond.then");
3060 ContBlock = createBasicBlock("omp.precond.end");
3061 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
3062 getProfileCount(&S));
3063 EmitBlock(ThenBlock);
3064 incrementProfileCounter(&S);
3065 }
3066
3067 // Emit 'then' code.
3068 {
3069 // Emit helper vars inits.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003070
3071 LValue LB = EmitOMPHelperVar(
3072 *this, cast<DeclRefExpr>(
3073 (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3074 ? S.getCombinedLowerBoundVariable()
3075 : S.getLowerBoundVariable())));
3076 LValue UB = EmitOMPHelperVar(
3077 *this, cast<DeclRefExpr>(
3078 (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3079 ? S.getCombinedUpperBoundVariable()
3080 : S.getUpperBoundVariable())));
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003081 LValue ST =
3082 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
3083 LValue IL =
3084 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
3085
3086 OMPPrivateScope LoopScope(*this);
Carlo Bertolli962bb802017-01-03 18:24:42 +00003087 if (EmitOMPFirstprivateClause(S, LoopScope)) {
3088 // Emit implicit barrier to synchronize threads and avoid data races on
3089 // initialization of firstprivate variables and post-update of
3090 // lastprivate variables.
3091 CGM.getOpenMPRuntime().emitBarrierCall(
3092 *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
3093 /*ForceSimpleCall=*/true);
3094 }
3095 EmitOMPPrivateClause(S, LoopScope);
3096 HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003097 EmitOMPPrivateLoopCounters(S, LoopScope);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003098 (void)LoopScope.Privatize();
3099
3100 // Detect the distribute schedule kind and chunk.
3101 llvm::Value *Chunk = nullptr;
3102 OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown;
3103 if (auto *C = S.getSingleClause<OMPDistScheduleClause>()) {
3104 ScheduleKind = C->getDistScheduleKind();
3105 if (const auto *Ch = C->getChunkSize()) {
3106 Chunk = EmitScalarExpr(Ch);
3107 Chunk = EmitScalarConversion(Chunk, Ch->getType(),
3108 S.getIterationVariable()->getType(),
3109 S.getLocStart());
3110 }
3111 }
3112 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
3113 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
3114
3115 // OpenMP [2.10.8, distribute Construct, Description]
3116 // If dist_schedule is specified, kind must be static. If specified,
3117 // iterations are divided into chunks of size chunk_size, chunks are
3118 // assigned to the teams of the league in a round-robin fashion in the
3119 // order of the team number. When no chunk_size is specified, the
3120 // iteration space is divided into chunks that are approximately equal
3121 // in size, and at most one chunk is distributed to each team of the
3122 // league. The size of the chunks is unspecified in this case.
3123 if (RT.isStaticNonchunked(ScheduleKind,
3124 /* Chunked */ Chunk != nullptr)) {
3125 RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind,
3126 IVSize, IVSigned, /* Ordered = */ false,
3127 IL.getAddress(), LB.getAddress(),
3128 UB.getAddress(), ST.getAddress());
3129 auto LoopExit =
3130 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
3131 // UB = min(UB, GlobalUB);
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003132 EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3133 ? S.getCombinedEnsureUpperBound()
3134 : S.getEnsureUpperBound());
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003135 // IV = LB;
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003136 EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3137 ? S.getCombinedInit()
3138 : S.getInit());
3139
3140 Expr *Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3141 ? S.getCombinedCond()
3142 : S.getCond();
3143
3144 // for distribute alone, codegen
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003145 // while (idx <= UB) { BODY; ++idx; }
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003146 // when combined with 'for' (e.g. as in 'distribute parallel for')
3147 // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; }
3148 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), Cond, IncExpr,
3149 [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) {
3150 CodeGenLoop(CGF, S, LoopExit);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003151 },
3152 [](CodeGenFunction &) {});
3153 EmitBlock(LoopExit.getBlock());
3154 // Tell the runtime we are done.
3155 RT.emitForStaticFinish(*this, S.getLocStart());
3156 } else {
3157 // Emit the outer loop, which requests its work chunk [LB..UB] from
3158 // runtime and runs the inner loop to process it.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003159 const OMPLoopArguments LoopArguments = {
3160 LB.getAddress(), UB.getAddress(), ST.getAddress(), IL.getAddress(),
3161 Chunk};
3162 EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope, LoopArguments,
3163 CodeGenLoop);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003164 }
Carlo Bertolli962bb802017-01-03 18:24:42 +00003165
3166 // Emit final copy of the lastprivate variables if IsLastIter != 0.
3167 if (HasLastprivateClause)
3168 EmitOMPLastprivateClauseFinal(
3169 S, /*NoFinals=*/false,
3170 Builder.CreateIsNotNull(
3171 EmitLoadOfScalar(IL, S.getLocStart())));
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003172 }
3173
3174 // We're now done with the loop, so jump to the continuation block.
3175 if (ContBlock) {
3176 EmitBranch(ContBlock);
3177 EmitBlock(ContBlock, true);
3178 }
3179 }
3180}
3181
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003182void CodeGenFunction::EmitOMPDistributeDirective(
3183 const OMPDistributeDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003184 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003185
3186 CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc());
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003187 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00003188 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003189 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen,
3190 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003191}
3192
Alexey Bataev5f600d62015-09-29 03:48:57 +00003193static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM,
3194 const CapturedStmt *S) {
3195 CodeGenFunction CGF(CGM, /*suppressNewContext=*/true);
3196 CodeGenFunction::CGCapturedStmtInfo CapStmtInfo;
3197 CGF.CapturedStmtInfo = &CapStmtInfo;
3198 auto *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S);
3199 Fn->addFnAttr(llvm::Attribute::NoInline);
3200 return Fn;
3201}
3202
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003203void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) {
Alexey Bataev8b427062016-05-25 12:36:08 +00003204 if (!S.getAssociatedStmt()) {
3205 for (const auto *DC : S.getClausesOfKind<OMPDependClause>())
3206 CGM.getOpenMPRuntime().emitDoacrossOrdered(*this, DC);
Alexey Bataev8ef31412015-12-18 07:58:25 +00003207 return;
Alexey Bataev8b427062016-05-25 12:36:08 +00003208 }
Alexey Bataev5f600d62015-09-29 03:48:57 +00003209 auto *C = S.getSingleClause<OMPSIMDClause>();
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003210 auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF,
3211 PrePostActionTy &Action) {
Alexey Bataev5f600d62015-09-29 03:48:57 +00003212 if (C) {
3213 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
3214 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
3215 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
3216 auto *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS);
3217 CGF.EmitNounwindRuntimeCall(OutlinedFn, CapturedVars);
3218 } else {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003219 Action.Enter(CGF);
Alexey Bataev5f600d62015-09-29 03:48:57 +00003220 CGF.EmitStmt(
3221 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3222 }
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003223 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00003224 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev5f600d62015-09-29 03:48:57 +00003225 CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart(), !C);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003226}
3227
Alexey Bataevb57056f2015-01-22 06:17:56 +00003228static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003229 QualType SrcType, QualType DestType,
3230 SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00003231 assert(CGF.hasScalarEvaluationKind(DestType) &&
3232 "DestType must have scalar evaluation kind.");
3233 assert(!Val.isAggregate() && "Must be a scalar or complex.");
3234 return Val.isScalar()
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003235 ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType,
3236 Loc)
Alexey Bataevb57056f2015-01-22 06:17:56 +00003237 : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003238 DestType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003239}
3240
3241static CodeGenFunction::ComplexPairTy
3242convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003243 QualType DestType, SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00003244 assert(CGF.getEvaluationKind(DestType) == TEK_Complex &&
3245 "DestType must have complex evaluation kind.");
3246 CodeGenFunction::ComplexPairTy ComplexVal;
3247 if (Val.isScalar()) {
3248 // Convert the input element to the element type of the complex.
3249 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003250 auto ScalarVal = CGF.EmitScalarConversion(Val.getScalarVal(), SrcType,
3251 DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003252 ComplexVal = CodeGenFunction::ComplexPairTy(
3253 ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType()));
3254 } else {
3255 assert(Val.isComplex() && "Must be a scalar or complex.");
3256 auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType();
3257 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
3258 ComplexVal.first = CGF.EmitScalarConversion(
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003259 Val.getComplexVal().first, SrcElementType, DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003260 ComplexVal.second = CGF.EmitScalarConversion(
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003261 Val.getComplexVal().second, SrcElementType, DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003262 }
3263 return ComplexVal;
3264}
3265
Alexey Bataev5e018f92015-04-23 06:35:10 +00003266static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst,
3267 LValue LVal, RValue RVal) {
3268 if (LVal.isGlobalReg()) {
3269 CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal);
3270 } else {
JF Bastien92f4ef12016-04-06 17:26:42 +00003271 CGF.EmitAtomicStore(RVal, LVal,
3272 IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
3273 : llvm::AtomicOrdering::Monotonic,
Alexey Bataev5e018f92015-04-23 06:35:10 +00003274 LVal.isVolatile(), /*IsInit=*/false);
3275 }
3276}
3277
Alexey Bataev8524d152016-01-21 12:35:58 +00003278void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal,
3279 QualType RValTy, SourceLocation Loc) {
3280 switch (getEvaluationKind(LVal.getType())) {
Alexey Bataev5e018f92015-04-23 06:35:10 +00003281 case TEK_Scalar:
Alexey Bataev8524d152016-01-21 12:35:58 +00003282 EmitStoreThroughLValue(RValue::get(convertToScalarValue(
3283 *this, RVal, RValTy, LVal.getType(), Loc)),
3284 LVal);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003285 break;
3286 case TEK_Complex:
Alexey Bataev8524d152016-01-21 12:35:58 +00003287 EmitStoreOfComplex(
3288 convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal,
Alexey Bataev5e018f92015-04-23 06:35:10 +00003289 /*isInit=*/false);
3290 break;
3291 case TEK_Aggregate:
3292 llvm_unreachable("Must be a scalar or complex.");
3293 }
3294}
3295
Alexey Bataevb57056f2015-01-22 06:17:56 +00003296static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst,
3297 const Expr *X, const Expr *V,
3298 SourceLocation Loc) {
3299 // v = x;
3300 assert(V->isLValue() && "V of 'omp atomic read' is not lvalue");
3301 assert(X->isLValue() && "X of 'omp atomic read' is not lvalue");
3302 LValue XLValue = CGF.EmitLValue(X);
3303 LValue VLValue = CGF.EmitLValue(V);
David Majnemera5b195a2015-02-14 01:35:12 +00003304 RValue Res = XLValue.isGlobalReg()
3305 ? CGF.EmitLoadOfLValue(XLValue, Loc)
JF Bastien92f4ef12016-04-06 17:26:42 +00003306 : CGF.EmitAtomicLoad(
3307 XLValue, Loc,
3308 IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
3309 : llvm::AtomicOrdering::Monotonic,
3310 XLValue.isVolatile());
Alexey Bataevb57056f2015-01-22 06:17:56 +00003311 // OpenMP, 2.12.6, atomic Construct
3312 // Any atomic construct with a seq_cst clause forces the atomically
3313 // performed operation to include an implicit flush operation without a
3314 // list.
3315 if (IsSeqCst)
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003316 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
Alexey Bataev8524d152016-01-21 12:35:58 +00003317 CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003318}
3319
Alexey Bataevb8329262015-02-27 06:33:30 +00003320static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst,
3321 const Expr *X, const Expr *E,
3322 SourceLocation Loc) {
3323 // x = expr;
3324 assert(X->isLValue() && "X of 'omp atomic write' is not lvalue");
Alexey Bataev5e018f92015-04-23 06:35:10 +00003325 emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E));
Alexey Bataevb8329262015-02-27 06:33:30 +00003326 // OpenMP, 2.12.6, atomic Construct
3327 // Any atomic construct with a seq_cst clause forces the atomically
3328 // performed operation to include an implicit flush operation without a
3329 // list.
3330 if (IsSeqCst)
3331 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
3332}
3333
Benjamin Kramer439ee9d2015-05-01 13:59:53 +00003334static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X,
3335 RValue Update,
3336 BinaryOperatorKind BO,
3337 llvm::AtomicOrdering AO,
3338 bool IsXLHSInRHSPart) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003339 auto &Context = CGF.CGM.getContext();
3340 // Allow atomicrmw only if 'x' and 'update' are integer values, lvalue for 'x'
Alexey Bataevb4505a72015-03-30 05:20:59 +00003341 // expression is simple and atomic is allowed for the given type for the
3342 // target platform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003343 if (BO == BO_Comma || !Update.isScalar() ||
Alexey Bataev9d541a72015-05-08 11:47:16 +00003344 !Update.getScalarVal()->getType()->isIntegerTy() ||
3345 !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) &&
3346 (Update.getScalarVal()->getType() !=
John McCall7f416cc2015-09-08 08:05:57 +00003347 X.getAddress().getElementType())) ||
3348 !X.getAddress().getElementType()->isIntegerTy() ||
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003349 !Context.getTargetInfo().hasBuiltinAtomic(
3350 Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment())))
Alexey Bataev5e018f92015-04-23 06:35:10 +00003351 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003352
3353 llvm::AtomicRMWInst::BinOp RMWOp;
3354 switch (BO) {
3355 case BO_Add:
3356 RMWOp = llvm::AtomicRMWInst::Add;
3357 break;
3358 case BO_Sub:
3359 if (!IsXLHSInRHSPart)
Alexey Bataev5e018f92015-04-23 06:35:10 +00003360 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003361 RMWOp = llvm::AtomicRMWInst::Sub;
3362 break;
3363 case BO_And:
3364 RMWOp = llvm::AtomicRMWInst::And;
3365 break;
3366 case BO_Or:
3367 RMWOp = llvm::AtomicRMWInst::Or;
3368 break;
3369 case BO_Xor:
3370 RMWOp = llvm::AtomicRMWInst::Xor;
3371 break;
3372 case BO_LT:
3373 RMWOp = X.getType()->hasSignedIntegerRepresentation()
3374 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min
3375 : llvm::AtomicRMWInst::Max)
3376 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin
3377 : llvm::AtomicRMWInst::UMax);
3378 break;
3379 case BO_GT:
3380 RMWOp = X.getType()->hasSignedIntegerRepresentation()
3381 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max
3382 : llvm::AtomicRMWInst::Min)
3383 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax
3384 : llvm::AtomicRMWInst::UMin);
3385 break;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003386 case BO_Assign:
3387 RMWOp = llvm::AtomicRMWInst::Xchg;
3388 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003389 case BO_Mul:
3390 case BO_Div:
3391 case BO_Rem:
3392 case BO_Shl:
3393 case BO_Shr:
3394 case BO_LAnd:
3395 case BO_LOr:
Alexey Bataev5e018f92015-04-23 06:35:10 +00003396 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003397 case BO_PtrMemD:
3398 case BO_PtrMemI:
3399 case BO_LE:
3400 case BO_GE:
3401 case BO_EQ:
3402 case BO_NE:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003403 case BO_AddAssign:
3404 case BO_SubAssign:
3405 case BO_AndAssign:
3406 case BO_OrAssign:
3407 case BO_XorAssign:
3408 case BO_MulAssign:
3409 case BO_DivAssign:
3410 case BO_RemAssign:
3411 case BO_ShlAssign:
3412 case BO_ShrAssign:
3413 case BO_Comma:
3414 llvm_unreachable("Unsupported atomic update operation");
3415 }
3416 auto *UpdateVal = Update.getScalarVal();
3417 if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) {
3418 UpdateVal = CGF.Builder.CreateIntCast(
John McCall7f416cc2015-09-08 08:05:57 +00003419 IC, X.getAddress().getElementType(),
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003420 X.getType()->hasSignedIntegerRepresentation());
3421 }
John McCall7f416cc2015-09-08 08:05:57 +00003422 auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003423 return std::make_pair(true, RValue::get(Res));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003424}
3425
Alexey Bataev5e018f92015-04-23 06:35:10 +00003426std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr(
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003427 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
3428 llvm::AtomicOrdering AO, SourceLocation Loc,
3429 const llvm::function_ref<RValue(RValue)> &CommonGen) {
3430 // Update expressions are allowed to have the following forms:
3431 // x binop= expr; -> xrval + expr;
3432 // x++, ++x -> xrval + 1;
3433 // x--, --x -> xrval - 1;
3434 // x = x binop expr; -> xrval binop expr
3435 // x = expr Op x; - > expr binop xrval;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003436 auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart);
3437 if (!Res.first) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003438 if (X.isGlobalReg()) {
3439 // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop
3440 // 'xrval'.
3441 EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X);
3442 } else {
3443 // Perform compare-and-swap procedure.
3444 EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified());
Alexey Bataevb4505a72015-03-30 05:20:59 +00003445 }
3446 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00003447 return Res;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003448}
3449
3450static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst,
3451 const Expr *X, const Expr *E,
3452 const Expr *UE, bool IsXLHSInRHSPart,
3453 SourceLocation Loc) {
3454 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
3455 "Update expr in 'atomic update' must be a binary operator.");
3456 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
3457 // Update expressions are allowed to have the following forms:
3458 // x binop= expr; -> xrval + expr;
3459 // x++, ++x -> xrval + 1;
3460 // x--, --x -> xrval - 1;
3461 // x = x binop expr; -> xrval binop expr
3462 // x = expr Op x; - > expr binop xrval;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003463 assert(X->isLValue() && "X of 'omp atomic update' is not lvalue");
Alexey Bataevb4505a72015-03-30 05:20:59 +00003464 LValue XLValue = CGF.EmitLValue(X);
3465 RValue ExprRValue = CGF.EmitAnyExpr(E);
JF Bastien92f4ef12016-04-06 17:26:42 +00003466 auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
3467 : llvm::AtomicOrdering::Monotonic;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003468 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
3469 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
3470 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
3471 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
3472 auto Gen =
3473 [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue {
3474 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
3475 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
3476 return CGF.EmitAnyExpr(UE);
3477 };
Alexey Bataev5e018f92015-04-23 06:35:10 +00003478 (void)CGF.EmitOMPAtomicSimpleUpdateExpr(
3479 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
3480 // OpenMP, 2.12.6, atomic Construct
3481 // Any atomic construct with a seq_cst clause forces the atomically
3482 // performed operation to include an implicit flush operation without a
3483 // list.
3484 if (IsSeqCst)
3485 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
3486}
3487
3488static RValue convertToType(CodeGenFunction &CGF, RValue Value,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003489 QualType SourceType, QualType ResType,
3490 SourceLocation Loc) {
Alexey Bataev5e018f92015-04-23 06:35:10 +00003491 switch (CGF.getEvaluationKind(ResType)) {
3492 case TEK_Scalar:
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003493 return RValue::get(
3494 convertToScalarValue(CGF, Value, SourceType, ResType, Loc));
Alexey Bataev5e018f92015-04-23 06:35:10 +00003495 case TEK_Complex: {
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003496 auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003497 return RValue::getComplex(Res.first, Res.second);
3498 }
3499 case TEK_Aggregate:
3500 break;
3501 }
3502 llvm_unreachable("Must be a scalar or complex.");
3503}
3504
3505static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst,
3506 bool IsPostfixUpdate, const Expr *V,
3507 const Expr *X, const Expr *E,
3508 const Expr *UE, bool IsXLHSInRHSPart,
3509 SourceLocation Loc) {
3510 assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue");
3511 assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue");
3512 RValue NewVVal;
3513 LValue VLValue = CGF.EmitLValue(V);
3514 LValue XLValue = CGF.EmitLValue(X);
3515 RValue ExprRValue = CGF.EmitAnyExpr(E);
JF Bastien92f4ef12016-04-06 17:26:42 +00003516 auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
3517 : llvm::AtomicOrdering::Monotonic;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003518 QualType NewVValType;
3519 if (UE) {
3520 // 'x' is updated with some additional value.
3521 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
3522 "Update expr in 'atomic capture' must be a binary operator.");
3523 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
3524 // Update expressions are allowed to have the following forms:
3525 // x binop= expr; -> xrval + expr;
3526 // x++, ++x -> xrval + 1;
3527 // x--, --x -> xrval - 1;
3528 // x = x binop expr; -> xrval binop expr
3529 // x = expr Op x; - > expr binop xrval;
3530 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
3531 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
3532 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
3533 NewVValType = XRValExpr->getType();
3534 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
3535 auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr,
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00003536 IsPostfixUpdate](RValue XRValue) -> RValue {
Alexey Bataev5e018f92015-04-23 06:35:10 +00003537 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
3538 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
3539 RValue Res = CGF.EmitAnyExpr(UE);
3540 NewVVal = IsPostfixUpdate ? XRValue : Res;
3541 return Res;
3542 };
3543 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
3544 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
3545 if (Res.first) {
3546 // 'atomicrmw' instruction was generated.
3547 if (IsPostfixUpdate) {
3548 // Use old value from 'atomicrmw'.
3549 NewVVal = Res.second;
3550 } else {
3551 // 'atomicrmw' does not provide new value, so evaluate it using old
3552 // value of 'x'.
3553 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
3554 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second);
3555 NewVVal = CGF.EmitAnyExpr(UE);
3556 }
3557 }
3558 } else {
3559 // 'x' is simply rewritten with some 'expr'.
3560 NewVValType = X->getType().getNonReferenceType();
3561 ExprRValue = convertToType(CGF, ExprRValue, E->getType(),
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003562 X->getType().getNonReferenceType(), Loc);
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00003563 auto &&Gen = [&NewVVal, ExprRValue](RValue XRValue) -> RValue {
Alexey Bataev5e018f92015-04-23 06:35:10 +00003564 NewVVal = XRValue;
3565 return ExprRValue;
3566 };
3567 // Try to perform atomicrmw xchg, otherwise simple exchange.
3568 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
3569 XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO,
3570 Loc, Gen);
3571 if (Res.first) {
3572 // 'atomicrmw' instruction was generated.
3573 NewVVal = IsPostfixUpdate ? Res.second : ExprRValue;
3574 }
3575 }
3576 // Emit post-update store to 'v' of old/new 'x' value.
Alexey Bataev8524d152016-01-21 12:35:58 +00003577 CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc);
Alexey Bataevb4505a72015-03-30 05:20:59 +00003578 // OpenMP, 2.12.6, atomic Construct
3579 // Any atomic construct with a seq_cst clause forces the atomically
3580 // performed operation to include an implicit flush operation without a
3581 // list.
3582 if (IsSeqCst)
3583 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
3584}
3585
Alexey Bataevb57056f2015-01-22 06:17:56 +00003586static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind,
Alexey Bataev5e018f92015-04-23 06:35:10 +00003587 bool IsSeqCst, bool IsPostfixUpdate,
3588 const Expr *X, const Expr *V, const Expr *E,
3589 const Expr *UE, bool IsXLHSInRHSPart,
3590 SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00003591 switch (Kind) {
3592 case OMPC_read:
3593 EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc);
3594 break;
3595 case OMPC_write:
Alexey Bataevb8329262015-02-27 06:33:30 +00003596 EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc);
3597 break;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003598 case OMPC_unknown:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003599 case OMPC_update:
Alexey Bataevb4505a72015-03-30 05:20:59 +00003600 EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc);
3601 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00003602 case OMPC_capture:
Alexey Bataev5e018f92015-04-23 06:35:10 +00003603 EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE,
3604 IsXLHSInRHSPart, Loc);
3605 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00003606 case OMPC_if:
3607 case OMPC_final:
3608 case OMPC_num_threads:
3609 case OMPC_private:
3610 case OMPC_firstprivate:
3611 case OMPC_lastprivate:
3612 case OMPC_reduction:
3613 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00003614 case OMPC_simdlen:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003615 case OMPC_collapse:
3616 case OMPC_default:
3617 case OMPC_seq_cst:
3618 case OMPC_shared:
3619 case OMPC_linear:
3620 case OMPC_aligned:
3621 case OMPC_copyin:
3622 case OMPC_copyprivate:
3623 case OMPC_flush:
3624 case OMPC_proc_bind:
3625 case OMPC_schedule:
3626 case OMPC_ordered:
3627 case OMPC_nowait:
3628 case OMPC_untied:
3629 case OMPC_threadprivate:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00003630 case OMPC_depend:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003631 case OMPC_mergeable:
Michael Wonge710d542015-08-07 16:16:36 +00003632 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00003633 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003634 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00003635 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00003636 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00003637 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00003638 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00003639 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00003640 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00003641 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00003642 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00003643 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00003644 case OMPC_defaultmap:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003645 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00003646 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00003647 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00003648 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00003649 case OMPC_is_device_ptr:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003650 llvm_unreachable("Clause is not allowed in 'omp atomic'.");
3651 }
3652}
3653
3654void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003655 bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>();
Alexey Bataevb57056f2015-01-22 06:17:56 +00003656 OpenMPClauseKind Kind = OMPC_unknown;
3657 for (auto *C : S.clauses()) {
3658 // Find first clause (skip seq_cst clause, if it is first).
3659 if (C->getClauseKind() != OMPC_seq_cst) {
3660 Kind = C->getClauseKind();
3661 break;
3662 }
3663 }
Alexey Bataev10fec572015-03-11 04:48:56 +00003664
3665 const auto *CS =
3666 S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003667 if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) {
Alexey Bataev10fec572015-03-11 04:48:56 +00003668 enterFullExpression(EWC);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003669 }
3670 // Processing for statements under 'atomic capture'.
3671 if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) {
3672 for (const auto *C : Compound->body()) {
3673 if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) {
3674 enterFullExpression(EWC);
3675 }
3676 }
3677 }
Alexey Bataev10fec572015-03-11 04:48:56 +00003678
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003679 auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF,
3680 PrePostActionTy &) {
Alexey Bataev33c56402015-12-14 09:26:19 +00003681 CGF.EmitStopPoint(CS);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003682 EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(),
3683 S.getV(), S.getExpr(), S.getUpdateExpr(),
3684 S.isXLHSInRHSPart(), S.getLocStart());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00003685 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00003686 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003687 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen);
Alexey Bataev0162e452014-07-22 10:10:35 +00003688}
3689
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003690static void emitCommonOMPTargetDirective(CodeGenFunction &CGF,
3691 const OMPExecutableDirective &S,
3692 const RegionCodeGenTy &CodeGen) {
3693 assert(isOpenMPTargetExecutionDirective(S.getDirectiveKind()));
3694 CodeGenModule &CGM = CGF.CGM;
Samuel Antaobed3c462015-10-02 16:14:20 +00003695 const CapturedStmt &CS = *cast<CapturedStmt>(S.getAssociatedStmt());
3696
Samuel Antaoee8fb302016-01-06 13:42:12 +00003697 llvm::Function *Fn = nullptr;
3698 llvm::Constant *FnID = nullptr;
Samuel Antaobed3c462015-10-02 16:14:20 +00003699
Samuel Antaobed3c462015-10-02 16:14:20 +00003700 const Expr *IfCond = nullptr;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003701 // Check for the at most one if clause associated with the target region.
3702 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
3703 if (C->getNameModifier() == OMPD_unknown ||
3704 C->getNameModifier() == OMPD_target) {
3705 IfCond = C->getCondition();
3706 break;
3707 }
Samuel Antaobed3c462015-10-02 16:14:20 +00003708 }
3709
3710 // Check if we have any device clause associated with the directive.
3711 const Expr *Device = nullptr;
3712 if (auto *C = S.getSingleClause<OMPDeviceClause>()) {
3713 Device = C->getDevice();
3714 }
3715
Samuel Antaoee8fb302016-01-06 13:42:12 +00003716 // Check if we have an if clause whose conditional always evaluates to false
3717 // or if we do not have any targets specified. If so the target region is not
3718 // an offload entry point.
3719 bool IsOffloadEntry = true;
3720 if (IfCond) {
3721 bool Val;
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003722 if (CGF.ConstantFoldsToSimpleInteger(IfCond, Val) && !Val)
Samuel Antaoee8fb302016-01-06 13:42:12 +00003723 IsOffloadEntry = false;
3724 }
3725 if (CGM.getLangOpts().OMPTargetTriples.empty())
3726 IsOffloadEntry = false;
3727
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003728 assert(CGF.CurFuncDecl && "No parent declaration for target region!");
Samuel Antaoee8fb302016-01-06 13:42:12 +00003729 StringRef ParentName;
3730 // In case we have Ctors/Dtors we use the complete type variant to produce
3731 // the mangling of the device outlined kernel.
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003732 if (auto *D = dyn_cast<CXXConstructorDecl>(CGF.CurFuncDecl))
Samuel Antaoee8fb302016-01-06 13:42:12 +00003733 ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete));
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003734 else if (auto *D = dyn_cast<CXXDestructorDecl>(CGF.CurFuncDecl))
Samuel Antaoee8fb302016-01-06 13:42:12 +00003735 ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete));
3736 else
3737 ParentName =
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003738 CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CGF.CurFuncDecl)));
Samuel Antaoee8fb302016-01-06 13:42:12 +00003739
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003740 // Emit target region as a standalone region.
3741 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(S, ParentName, Fn, FnID,
3742 IsOffloadEntry, CodeGen);
3743 OMPLexicalScope Scope(CGF, S);
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003744 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
3745 CGF.GenerateOpenMPCapturedVars(CS, CapturedVars);
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003746 CGM.getOpenMPRuntime().emitTargetCall(CGF, S, Fn, FnID, IfCond, Device,
Samuel Antaobed3c462015-10-02 16:14:20 +00003747 CapturedVars);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003748}
3749
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003750static void emitTargetRegion(CodeGenFunction &CGF, const OMPTargetDirective &S,
3751 PrePostActionTy &Action) {
3752 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
3753 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
3754 CGF.EmitOMPPrivateClause(S, PrivateScope);
3755 (void)PrivateScope.Privatize();
3756
3757 Action.Enter(CGF);
3758 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3759}
3760
3761void CodeGenFunction::EmitOMPTargetDeviceFunction(CodeGenModule &CGM,
3762 StringRef ParentName,
3763 const OMPTargetDirective &S) {
3764 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3765 emitTargetRegion(CGF, S, Action);
3766 };
3767 llvm::Function *Fn;
3768 llvm::Constant *Addr;
3769 // Emit target region as a standalone region.
3770 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
3771 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
3772 assert(Fn && Addr && "Target device function emission failed.");
3773}
3774
3775void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) {
3776 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3777 emitTargetRegion(CGF, S, Action);
3778 };
3779 emitCommonOMPTargetDirective(*this, S, CodeGen);
3780}
3781
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003782static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF,
3783 const OMPExecutableDirective &S,
3784 OpenMPDirectiveKind InnermostKind,
3785 const RegionCodeGenTy &CodeGen) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003786 const CapturedStmt *CS = S.getCapturedStmt(OMPD_teams);
3787 auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitTeamsOutlinedFunction(
3788 S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
Samuel Antaob68e2db2016-03-03 16:20:23 +00003789
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003790 const OMPNumTeamsClause *NT = S.getSingleClause<OMPNumTeamsClause>();
3791 const OMPThreadLimitClause *TL = S.getSingleClause<OMPThreadLimitClause>();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003792 if (NT || TL) {
Carlo Bertollic6872252016-04-04 15:55:02 +00003793 Expr *NumTeams = (NT) ? NT->getNumTeams() : nullptr;
3794 Expr *ThreadLimit = (TL) ? TL->getThreadLimit() : nullptr;
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003795
Carlo Bertollic6872252016-04-04 15:55:02 +00003796 CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit,
3797 S.getLocStart());
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003798 }
3799
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003800 OMPTeamsScope Scope(CGF, S);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003801 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
3802 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003803 CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getLocStart(), OutlinedFn,
3804 CapturedVars);
3805}
3806
3807void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) {
Kelvin Li51336dd2016-12-15 17:55:32 +00003808 // Emit teams region as a standalone region.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003809 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003810 OMPPrivateScope PrivateScope(CGF);
Carlo Bertolli6ad7b5a2016-03-03 22:09:40 +00003811 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
3812 CGF.EmitOMPPrivateClause(S, PrivateScope);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003813 CGF.EmitOMPReductionClauseInit(S, PrivateScope);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003814 (void)PrivateScope.Privatize();
3815 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003816 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003817 };
3818 emitCommonOMPTeamsDirective(*this, S, OMPD_teams, CodeGen);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003819 emitPostUpdateForReductionClause(
3820 *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev13314bf2014-10-09 04:18:56 +00003821}
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003822
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003823static void emitTargetTeamsRegion(CodeGenFunction &CGF, PrePostActionTy &Action,
3824 const OMPTargetTeamsDirective &S) {
3825 auto *CS = S.getCapturedStmt(OMPD_teams);
3826 Action.Enter(CGF);
3827 auto &&CodeGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) {
3828 // TODO: Add support for clauses.
3829 CGF.EmitStmt(CS->getCapturedStmt());
3830 };
3831 emitCommonOMPTeamsDirective(CGF, S, OMPD_teams, CodeGen);
3832}
3833
3834void CodeGenFunction::EmitOMPTargetTeamsDeviceFunction(
3835 CodeGenModule &CGM, StringRef ParentName,
3836 const OMPTargetTeamsDirective &S) {
3837 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3838 emitTargetTeamsRegion(CGF, Action, S);
3839 };
3840 llvm::Function *Fn;
3841 llvm::Constant *Addr;
3842 // Emit target region as a standalone region.
3843 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
3844 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
3845 assert(Fn && Addr && "Target device function emission failed.");
3846}
3847
3848void CodeGenFunction::EmitOMPTargetTeamsDirective(
3849 const OMPTargetTeamsDirective &S) {
3850 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3851 emitTargetTeamsRegion(CGF, Action, S);
3852 };
3853 emitCommonOMPTargetDirective(*this, S, CodeGen);
3854}
3855
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003856void CodeGenFunction::EmitOMPCancellationPointDirective(
3857 const OMPCancellationPointDirective &S) {
Alexey Bataev0f34da12015-07-02 04:17:07 +00003858 CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getLocStart(),
3859 S.getCancelRegion());
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003860}
3861
Alexey Bataev80909872015-07-02 11:25:17 +00003862void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) {
Alexey Bataev87933c72015-09-18 08:07:34 +00003863 const Expr *IfCond = nullptr;
3864 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
3865 if (C->getNameModifier() == OMPD_unknown ||
3866 C->getNameModifier() == OMPD_cancel) {
3867 IfCond = C->getCondition();
3868 break;
3869 }
3870 }
3871 CGM.getOpenMPRuntime().emitCancelCall(*this, S.getLocStart(), IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00003872 S.getCancelRegion());
Alexey Bataev80909872015-07-02 11:25:17 +00003873}
3874
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003875CodeGenFunction::JumpDest
3876CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) {
Alexey Bataev957d8562016-11-17 15:12:05 +00003877 if (Kind == OMPD_parallel || Kind == OMPD_task ||
3878 Kind == OMPD_target_parallel)
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003879 return ReturnBlock;
Alexey Bataev25e5b442015-09-15 12:52:43 +00003880 assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections ||
Alexey Bataev957d8562016-11-17 15:12:05 +00003881 Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for ||
3882 Kind == OMPD_distribute_parallel_for ||
3883 Kind == OMPD_target_parallel_for);
3884 return OMPCancelStack.getExitBlock();
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003885}
Michael Wong65f367f2015-07-21 13:44:28 +00003886
Samuel Antaocc10b852016-07-28 14:23:26 +00003887void CodeGenFunction::EmitOMPUseDevicePtrClause(
3888 const OMPClause &NC, OMPPrivateScope &PrivateScope,
3889 const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap) {
3890 const auto &C = cast<OMPUseDevicePtrClause>(NC);
3891 auto OrigVarIt = C.varlist_begin();
3892 auto InitIt = C.inits().begin();
3893 for (auto PvtVarIt : C.private_copies()) {
3894 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*OrigVarIt)->getDecl());
3895 auto *InitVD = cast<VarDecl>(cast<DeclRefExpr>(*InitIt)->getDecl());
3896 auto *PvtVD = cast<VarDecl>(cast<DeclRefExpr>(PvtVarIt)->getDecl());
3897
3898 // In order to identify the right initializer we need to match the
3899 // declaration used by the mapping logic. In some cases we may get
3900 // OMPCapturedExprDecl that refers to the original declaration.
3901 const ValueDecl *MatchingVD = OrigVD;
3902 if (auto *OED = dyn_cast<OMPCapturedExprDecl>(MatchingVD)) {
3903 // OMPCapturedExprDecl are used to privative fields of the current
3904 // structure.
3905 auto *ME = cast<MemberExpr>(OED->getInit());
3906 assert(isa<CXXThisExpr>(ME->getBase()) &&
3907 "Base should be the current struct!");
3908 MatchingVD = ME->getMemberDecl();
3909 }
3910
3911 // If we don't have information about the current list item, move on to
3912 // the next one.
3913 auto InitAddrIt = CaptureDeviceAddrMap.find(MatchingVD);
3914 if (InitAddrIt == CaptureDeviceAddrMap.end())
3915 continue;
3916
3917 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
3918 // Initialize the temporary initialization variable with the address we
3919 // get from the runtime library. We have to cast the source address
3920 // because it is always a void *. References are materialized in the
3921 // privatization scope, so the initialization here disregards the fact
3922 // the original variable is a reference.
3923 QualType AddrQTy =
3924 getContext().getPointerType(OrigVD->getType().getNonReferenceType());
3925 llvm::Type *AddrTy = ConvertTypeForMem(AddrQTy);
3926 Address InitAddr = Builder.CreateBitCast(InitAddrIt->second, AddrTy);
3927 setAddrOfLocalVar(InitVD, InitAddr);
3928
3929 // Emit private declaration, it will be initialized by the value we
3930 // declaration we just added to the local declarations map.
3931 EmitDecl(*PvtVD);
3932
3933 // The initialization variables reached its purpose in the emission
3934 // ofthe previous declaration, so we don't need it anymore.
3935 LocalDeclMap.erase(InitVD);
3936
3937 // Return the address of the private variable.
3938 return GetAddrOfLocalVar(PvtVD);
3939 });
3940 assert(IsRegistered && "firstprivate var already registered as private");
3941 // Silence the warning about unused variable.
3942 (void)IsRegistered;
3943
3944 ++OrigVarIt;
3945 ++InitIt;
3946 }
3947}
3948
Michael Wong65f367f2015-07-21 13:44:28 +00003949// Generate the instructions for '#pragma omp target data' directive.
3950void CodeGenFunction::EmitOMPTargetDataDirective(
3951 const OMPTargetDataDirective &S) {
Samuel Antaocc10b852016-07-28 14:23:26 +00003952 CGOpenMPRuntime::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true);
3953
3954 // Create a pre/post action to signal the privatization of the device pointer.
3955 // This action can be replaced by the OpenMP runtime code generation to
3956 // deactivate privatization.
3957 bool PrivatizeDevicePointers = false;
3958 class DevicePointerPrivActionTy : public PrePostActionTy {
3959 bool &PrivatizeDevicePointers;
3960
3961 public:
3962 explicit DevicePointerPrivActionTy(bool &PrivatizeDevicePointers)
3963 : PrePostActionTy(), PrivatizeDevicePointers(PrivatizeDevicePointers) {}
3964 void Enter(CodeGenFunction &CGF) override {
3965 PrivatizeDevicePointers = true;
3966 }
Samuel Antaodf158d52016-04-27 22:58:19 +00003967 };
Samuel Antaocc10b852016-07-28 14:23:26 +00003968 DevicePointerPrivActionTy PrivAction(PrivatizeDevicePointers);
3969
3970 auto &&CodeGen = [&S, &Info, &PrivatizeDevicePointers](
3971 CodeGenFunction &CGF, PrePostActionTy &Action) {
3972 auto &&InnermostCodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
3973 CGF.EmitStmt(
3974 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3975 };
3976
3977 // Codegen that selects wheather to generate the privatization code or not.
3978 auto &&PrivCodeGen = [&S, &Info, &PrivatizeDevicePointers,
3979 &InnermostCodeGen](CodeGenFunction &CGF,
3980 PrePostActionTy &Action) {
3981 RegionCodeGenTy RCG(InnermostCodeGen);
3982 PrivatizeDevicePointers = false;
3983
3984 // Call the pre-action to change the status of PrivatizeDevicePointers if
3985 // needed.
3986 Action.Enter(CGF);
3987
3988 if (PrivatizeDevicePointers) {
3989 OMPPrivateScope PrivateScope(CGF);
3990 // Emit all instances of the use_device_ptr clause.
3991 for (const auto *C : S.getClausesOfKind<OMPUseDevicePtrClause>())
3992 CGF.EmitOMPUseDevicePtrClause(*C, PrivateScope,
3993 Info.CaptureDeviceAddrMap);
3994 (void)PrivateScope.Privatize();
3995 RCG(CGF);
3996 } else
3997 RCG(CGF);
3998 };
3999
4000 // Forward the provided action to the privatization codegen.
4001 RegionCodeGenTy PrivRCG(PrivCodeGen);
4002 PrivRCG.setAction(Action);
4003
4004 // Notwithstanding the body of the region is emitted as inlined directive,
4005 // we don't use an inline scope as changes in the references inside the
4006 // region are expected to be visible outside, so we do not privative them.
4007 OMPLexicalScope Scope(CGF, S);
4008 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_target_data,
4009 PrivRCG);
4010 };
4011
4012 RegionCodeGenTy RCG(CodeGen);
Samuel Antaodf158d52016-04-27 22:58:19 +00004013
4014 // If we don't have target devices, don't bother emitting the data mapping
4015 // code.
4016 if (CGM.getLangOpts().OMPTargetTriples.empty()) {
Samuel Antaocc10b852016-07-28 14:23:26 +00004017 RCG(*this);
Samuel Antaodf158d52016-04-27 22:58:19 +00004018 return;
4019 }
4020
4021 // Check if we have any if clause associated with the directive.
4022 const Expr *IfCond = nullptr;
4023 if (auto *C = S.getSingleClause<OMPIfClause>())
4024 IfCond = C->getCondition();
4025
4026 // Check if we have any device clause associated with the directive.
4027 const Expr *Device = nullptr;
4028 if (auto *C = S.getSingleClause<OMPDeviceClause>())
4029 Device = C->getDevice();
4030
Samuel Antaocc10b852016-07-28 14:23:26 +00004031 // Set the action to signal privatization of device pointers.
4032 RCG.setAction(PrivAction);
4033
4034 // Emit region code.
4035 CGM.getOpenMPRuntime().emitTargetDataCalls(*this, S, IfCond, Device, RCG,
4036 Info);
Michael Wong65f367f2015-07-21 13:44:28 +00004037}
Alexey Bataev49f6e782015-12-01 04:18:41 +00004038
Samuel Antaodf67fc42016-01-19 19:15:56 +00004039void CodeGenFunction::EmitOMPTargetEnterDataDirective(
4040 const OMPTargetEnterDataDirective &S) {
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00004041 // If we don't have target devices, don't bother emitting the data mapping
4042 // code.
4043 if (CGM.getLangOpts().OMPTargetTriples.empty())
4044 return;
4045
4046 // Check if we have any if clause associated with the directive.
4047 const Expr *IfCond = nullptr;
4048 if (auto *C = S.getSingleClause<OMPIfClause>())
4049 IfCond = C->getCondition();
4050
4051 // Check if we have any device clause associated with the directive.
4052 const Expr *Device = nullptr;
4053 if (auto *C = S.getSingleClause<OMPDeviceClause>())
4054 Device = C->getDevice();
4055
Samuel Antao8d2d7302016-05-26 18:30:22 +00004056 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
Samuel Antaodf67fc42016-01-19 19:15:56 +00004057}
4058
Samuel Antao72590762016-01-19 20:04:50 +00004059void CodeGenFunction::EmitOMPTargetExitDataDirective(
4060 const OMPTargetExitDataDirective &S) {
Samuel Antao8dd66282016-04-27 23:14:30 +00004061 // If we don't have target devices, don't bother emitting the data mapping
4062 // code.
4063 if (CGM.getLangOpts().OMPTargetTriples.empty())
4064 return;
4065
4066 // Check if we have any if clause associated with the directive.
4067 const Expr *IfCond = nullptr;
4068 if (auto *C = S.getSingleClause<OMPIfClause>())
4069 IfCond = C->getCondition();
4070
4071 // Check if we have any device clause associated with the directive.
4072 const Expr *Device = nullptr;
4073 if (auto *C = S.getSingleClause<OMPDeviceClause>())
4074 Device = C->getDevice();
4075
Samuel Antao8d2d7302016-05-26 18:30:22 +00004076 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
Samuel Antao72590762016-01-19 20:04:50 +00004077}
4078
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004079static void emitTargetParallelRegion(CodeGenFunction &CGF,
4080 const OMPTargetParallelDirective &S,
4081 PrePostActionTy &Action) {
4082 // Get the captured statement associated with the 'parallel' region.
4083 auto *CS = S.getCapturedStmt(OMPD_parallel);
4084 Action.Enter(CGF);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00004085 auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &) {
4086 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
4087 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
4088 CGF.EmitOMPPrivateClause(S, PrivateScope);
4089 CGF.EmitOMPReductionClauseInit(S, PrivateScope);
4090 (void)PrivateScope.Privatize();
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004091 // TODO: Add support for clauses.
4092 CGF.EmitStmt(CS->getCapturedStmt());
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00004093 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004094 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00004095 emitCommonOMPParallelDirective(CGF, S, OMPD_parallel, CodeGen,
4096 emitEmptyBoundParameters);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00004097 emitPostUpdateForReductionClause(
4098 CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004099}
4100
4101void CodeGenFunction::EmitOMPTargetParallelDeviceFunction(
4102 CodeGenModule &CGM, StringRef ParentName,
4103 const OMPTargetParallelDirective &S) {
4104 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
4105 emitTargetParallelRegion(CGF, S, Action);
4106 };
4107 llvm::Function *Fn;
4108 llvm::Constant *Addr;
4109 // Emit target region as a standalone region.
4110 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
4111 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
4112 assert(Fn && Addr && "Target device function emission failed.");
4113}
4114
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00004115void CodeGenFunction::EmitOMPTargetParallelDirective(
4116 const OMPTargetParallelDirective &S) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004117 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
4118 emitTargetParallelRegion(CGF, S, Action);
4119 };
4120 emitCommonOMPTargetDirective(*this, S, CodeGen);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00004121}
4122
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00004123void CodeGenFunction::EmitOMPTargetParallelForDirective(
4124 const OMPTargetParallelForDirective &S) {
4125 // TODO: codegen for target parallel for.
4126}
4127
Alexey Bataev7292c292016-04-25 12:22:29 +00004128/// Emit a helper variable and return corresponding lvalue.
4129static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper,
4130 const ImplicitParamDecl *PVD,
4131 CodeGenFunction::OMPPrivateScope &Privates) {
4132 auto *VDecl = cast<VarDecl>(Helper->getDecl());
4133 Privates.addPrivate(
4134 VDecl, [&CGF, PVD]() -> Address { return CGF.GetAddrOfLocalVar(PVD); });
4135}
4136
4137void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) {
4138 assert(isOpenMPTaskLoopDirective(S.getDirectiveKind()));
4139 // Emit outlined function for task construct.
4140 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
4141 auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
4142 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
4143 const Expr *IfCond = nullptr;
4144 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
4145 if (C->getNameModifier() == OMPD_unknown ||
4146 C->getNameModifier() == OMPD_taskloop) {
4147 IfCond = C->getCondition();
4148 break;
4149 }
4150 }
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004151
4152 OMPTaskDataTy Data;
4153 // Check if taskloop must be emitted without taskgroup.
4154 Data.Nogroup = S.getSingleClause<OMPNogroupClause>();
Alexey Bataev7292c292016-04-25 12:22:29 +00004155 // TODO: Check if we should emit tied or untied task.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004156 Data.Tied = true;
4157 // Set scheduling for taskloop
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00004158 if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) {
4159 // grainsize clause
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004160 Data.Schedule.setInt(/*IntVal=*/false);
4161 Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize()));
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00004162 } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) {
4163 // num_tasks clause
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004164 Data.Schedule.setInt(/*IntVal=*/true);
4165 Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks()));
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00004166 }
Alexey Bataev7292c292016-04-25 12:22:29 +00004167
4168 auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) {
4169 // if (PreCond) {
4170 // for (IV in 0..LastIteration) BODY;
4171 // <Final counter/linear vars updates>;
4172 // }
4173 //
4174
4175 // Emit: if (PreCond) - begin.
4176 // If the condition constant folds and can be elided, avoid emitting the
4177 // whole loop.
4178 bool CondConstant;
4179 llvm::BasicBlock *ContBlock = nullptr;
4180 OMPLoopScope PreInitScope(CGF, S);
4181 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
4182 if (!CondConstant)
4183 return;
4184 } else {
4185 auto *ThenBlock = CGF.createBasicBlock("taskloop.if.then");
4186 ContBlock = CGF.createBasicBlock("taskloop.if.end");
4187 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
4188 CGF.getProfileCount(&S));
4189 CGF.EmitBlock(ThenBlock);
4190 CGF.incrementProfileCounter(&S);
4191 }
4192
Alexey Bataev1e73ef32016-04-28 12:14:51 +00004193 if (isOpenMPSimdDirective(S.getDirectiveKind()))
4194 CGF.EmitOMPSimdInit(S);
4195
Alexey Bataev7292c292016-04-25 12:22:29 +00004196 OMPPrivateScope LoopScope(CGF);
4197 // Emit helper vars inits.
4198 enum { LowerBound = 5, UpperBound, Stride, LastIter };
4199 auto *I = CS->getCapturedDecl()->param_begin();
4200 auto *LBP = std::next(I, LowerBound);
4201 auto *UBP = std::next(I, UpperBound);
4202 auto *STP = std::next(I, Stride);
4203 auto *LIP = std::next(I, LastIter);
4204 mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP,
4205 LoopScope);
4206 mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP,
4207 LoopScope);
4208 mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope);
4209 mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP,
4210 LoopScope);
4211 CGF.EmitOMPPrivateLoopCounters(S, LoopScope);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004212 bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev7292c292016-04-25 12:22:29 +00004213 (void)LoopScope.Privatize();
4214 // Emit the loop iteration variable.
4215 const Expr *IVExpr = S.getIterationVariable();
4216 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
4217 CGF.EmitVarDecl(*IVDecl);
4218 CGF.EmitIgnoredExpr(S.getInit());
4219
4220 // Emit the iterations count variable.
4221 // If it is not a variable, Sema decided to calculate iterations count on
4222 // each iteration (e.g., it is foldable into a constant).
4223 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
4224 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
4225 // Emit calculation of the iterations count.
4226 CGF.EmitIgnoredExpr(S.getCalcLastIteration());
4227 }
4228
4229 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
4230 S.getInc(),
4231 [&S](CodeGenFunction &CGF) {
4232 CGF.EmitOMPLoopBody(S, JumpDest());
4233 CGF.EmitStopPoint(&S);
4234 },
4235 [](CodeGenFunction &) {});
4236 // Emit: if (PreCond) - end.
4237 if (ContBlock) {
4238 CGF.EmitBranch(ContBlock);
4239 CGF.EmitBlock(ContBlock, true);
4240 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004241 // Emit final copy of the lastprivate variables if IsLastIter != 0.
4242 if (HasLastprivateClause) {
4243 CGF.EmitOMPLastprivateClauseFinal(
4244 S, isOpenMPSimdDirective(S.getDirectiveKind()),
4245 CGF.Builder.CreateIsNotNull(CGF.EmitLoadOfScalar(
4246 CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false,
4247 (*LIP)->getType(), S.getLocStart())));
4248 }
Alexey Bataev7292c292016-04-25 12:22:29 +00004249 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004250 auto &&TaskGen = [&S, SharedsTy, CapturedStruct,
4251 IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn,
4252 const OMPTaskDataTy &Data) {
Alexey Bataev7292c292016-04-25 12:22:29 +00004253 auto &&CodeGen = [&](CodeGenFunction &CGF, PrePostActionTy &) {
4254 OMPLoopScope PreInitScope(CGF, S);
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004255 CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getLocStart(), S,
4256 OutlinedFn, SharedsTy,
4257 CapturedStruct, IfCond, Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00004258 };
4259 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop,
4260 CodeGen);
4261 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004262 EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00004263}
4264
Alexey Bataev49f6e782015-12-01 04:18:41 +00004265void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) {
Alexey Bataev7292c292016-04-25 12:22:29 +00004266 EmitOMPTaskLoopBasedDirective(S);
Alexey Bataev49f6e782015-12-01 04:18:41 +00004267}
4268
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004269void CodeGenFunction::EmitOMPTaskLoopSimdDirective(
4270 const OMPTaskLoopSimdDirective &S) {
Alexey Bataev1e73ef32016-04-28 12:14:51 +00004271 EmitOMPTaskLoopBasedDirective(S);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004272}
Samuel Antao686c70c2016-05-26 17:30:50 +00004273
4274// Generate the instructions for '#pragma omp target update' directive.
4275void CodeGenFunction::EmitOMPTargetUpdateDirective(
4276 const OMPTargetUpdateDirective &S) {
Samuel Antao8d2d7302016-05-26 18:30:22 +00004277 // If we don't have target devices, don't bother emitting the data mapping
4278 // code.
4279 if (CGM.getLangOpts().OMPTargetTriples.empty())
4280 return;
4281
4282 // Check if we have any if clause associated with the directive.
4283 const Expr *IfCond = nullptr;
4284 if (auto *C = S.getSingleClause<OMPIfClause>())
4285 IfCond = C->getCondition();
4286
4287 // Check if we have any device clause associated with the directive.
4288 const Expr *Device = nullptr;
4289 if (auto *C = S.getSingleClause<OMPDeviceClause>())
4290 Device = C->getDevice();
4291
4292 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
Samuel Antao686c70c2016-05-26 17:30:50 +00004293}