blob: f2b99428588bf1eddf210d0989e56adff415c076 [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.
Alexey Bataev4ba78a42016-04-27 07:56:03 +000029class OMPLexicalScope final : 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,
57 bool AsInlined = false)
58 : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()),
59 InlinedShareds(CGF) {
Alexey Bataev3392d762016-02-16 11:18:12 +000060 emitPreInitStmt(CGF, S);
Alexey Bataev4ba78a42016-04-27 07:56:03 +000061 if (AsInlined) {
62 if (S.hasAssociatedStmt()) {
63 auto *CS = cast<CapturedStmt>(S.getAssociatedStmt());
64 for (auto &C : CS->captures()) {
65 if (C.capturesVariable() || C.capturesVariableByCopy()) {
66 auto *VD = C.getCapturedVar();
67 DeclRefExpr DRE(const_cast<VarDecl *>(VD),
68 isCapturedVar(CGF, VD) ||
69 (CGF.CapturedStmtInfo &&
70 InlinedShareds.isGlobalVarCaptured(VD)),
71 VD->getType().getNonReferenceType(), VK_LValue,
72 SourceLocation());
73 InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address {
74 return CGF.EmitLValue(&DRE).getAddress();
75 });
76 }
77 }
78 (void)InlinedShareds.Privatize();
79 }
80 }
Alexey Bataev3392d762016-02-16 11:18:12 +000081 }
82};
Alexey Bataev14fa1c62016-03-29 05:34:15 +000083
Alexey Bataev5a3af132016-03-29 08:58:54 +000084/// Private scope for OpenMP loop-based directives, that supports capturing
85/// of used expression from loop statement.
86class OMPLoopScope : public CodeGenFunction::RunCleanupsScope {
87 void emitPreInitStmt(CodeGenFunction &CGF, const OMPLoopDirective &S) {
88 if (auto *LD = dyn_cast<OMPLoopDirective>(&S)) {
89 if (auto *PreInits = cast_or_null<DeclStmt>(LD->getPreInits())) {
90 for (const auto *I : PreInits->decls())
91 CGF.EmitVarDecl(cast<VarDecl>(*I));
92 }
93 }
94 }
95
96public:
97 OMPLoopScope(CodeGenFunction &CGF, const OMPLoopDirective &S)
98 : CodeGenFunction::RunCleanupsScope(CGF) {
99 emitPreInitStmt(CGF, S);
100 }
101};
102
Alexey Bataev3392d762016-02-16 11:18:12 +0000103} // namespace
104
Alexey Bataev1189bd02016-01-26 12:20:39 +0000105llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) {
106 auto &C = getContext();
107 llvm::Value *Size = nullptr;
108 auto SizeInChars = C.getTypeSizeInChars(Ty);
109 if (SizeInChars.isZero()) {
110 // getTypeSizeInChars() returns 0 for a VLA.
111 while (auto *VAT = C.getAsVariableArrayType(Ty)) {
112 llvm::Value *ArraySize;
113 std::tie(ArraySize, Ty) = getVLASize(VAT);
114 Size = Size ? Builder.CreateNUWMul(Size, ArraySize) : ArraySize;
115 }
116 SizeInChars = C.getTypeSizeInChars(Ty);
117 if (SizeInChars.isZero())
118 return llvm::ConstantInt::get(SizeTy, /*V=*/0);
119 Size = Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars));
120 } else
121 Size = CGM.getSize(SizeInChars);
122 return Size;
123}
124
Alexey Bataev2377fe92015-09-10 08:12:02 +0000125void CodeGenFunction::GenerateOpenMPCapturedVars(
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000126 const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) {
Alexey Bataev2377fe92015-09-10 08:12:02 +0000127 const RecordDecl *RD = S.getCapturedRecordDecl();
128 auto CurField = RD->field_begin();
129 auto CurCap = S.captures().begin();
130 for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(),
131 E = S.capture_init_end();
132 I != E; ++I, ++CurField, ++CurCap) {
133 if (CurField->hasCapturedVLAType()) {
134 auto VAT = CurField->getCapturedVLAType();
Samuel Antaobed3c462015-10-02 16:14:20 +0000135 auto *Val = VLASizeMap[VAT->getSizeExpr()];
Samuel Antaobed3c462015-10-02 16:14:20 +0000136 CapturedVars.push_back(Val);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000137 } else if (CurCap->capturesThis())
138 CapturedVars.push_back(CXXThisValue);
Samuel Antao6d004262016-06-16 18:39:34 +0000139 else if (CurCap->capturesVariableByCopy()) {
140 llvm::Value *CV =
141 EmitLoadOfLValue(EmitLValue(*I), SourceLocation()).getScalarVal();
142
143 // If the field is not a pointer, we need to save the actual value
144 // and load it as a void pointer.
145 if (!CurField->getType()->isAnyPointerType()) {
146 auto &Ctx = getContext();
147 auto DstAddr = CreateMemTemp(
148 Ctx.getUIntPtrType(),
149 Twine(CurCap->getCapturedVar()->getName()) + ".casted");
150 LValue DstLV = MakeAddrLValue(DstAddr, Ctx.getUIntPtrType());
151
152 auto *SrcAddrVal = EmitScalarConversion(
153 DstAddr.getPointer(), Ctx.getPointerType(Ctx.getUIntPtrType()),
154 Ctx.getPointerType(CurField->getType()), SourceLocation());
155 LValue SrcLV =
156 MakeNaturalAlignAddrLValue(SrcAddrVal, CurField->getType());
157
158 // Store the value using the source type pointer.
159 EmitStoreThroughLValue(RValue::get(CV), SrcLV);
160
161 // Load the value using the destination type pointer.
162 CV = EmitLoadOfLValue(DstLV, SourceLocation()).getScalarVal();
163 }
164 CapturedVars.push_back(CV);
165 } else {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000166 assert(CurCap->capturesVariable() && "Expected capture by reference.");
Alexey Bataev2377fe92015-09-10 08:12:02 +0000167 CapturedVars.push_back(EmitLValue(*I).getAddress().getPointer());
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000168 }
Alexey Bataev2377fe92015-09-10 08:12:02 +0000169 }
170}
171
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000172static Address castValueFromUintptr(CodeGenFunction &CGF, QualType DstType,
173 StringRef Name, LValue AddrLV,
174 bool isReferenceType = false) {
175 ASTContext &Ctx = CGF.getContext();
176
177 auto *CastedPtr = CGF.EmitScalarConversion(
178 AddrLV.getAddress().getPointer(), Ctx.getUIntPtrType(),
179 Ctx.getPointerType(DstType), SourceLocation());
180 auto TmpAddr =
181 CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType))
182 .getAddress();
183
184 // If we are dealing with references we need to return the address of the
185 // reference instead of the reference of the value.
186 if (isReferenceType) {
187 QualType RefType = Ctx.getLValueReferenceType(DstType);
188 auto *RefVal = TmpAddr.getPointer();
189 TmpAddr = CGF.CreateMemTemp(RefType, Twine(Name) + ".ref");
190 auto TmpLVal = CGF.MakeAddrLValue(TmpAddr, RefType);
191 CGF.EmitScalarInit(RefVal, TmpLVal);
192 }
193
194 return TmpAddr;
195}
196
Alexey Bataev2377fe92015-09-10 08:12:02 +0000197llvm::Function *
Samuel Antao6d004262016-06-16 18:39:34 +0000198CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S) {
Alexey Bataev2377fe92015-09-10 08:12:02 +0000199 assert(
200 CapturedStmtInfo &&
201 "CapturedStmtInfo should be set when generating the captured function");
202 const CapturedDecl *CD = S.getCapturedDecl();
203 const RecordDecl *RD = S.getCapturedRecordDecl();
204 assert(CD->hasBody() && "missing CapturedDecl body");
205
206 // Build the argument list.
207 ASTContext &Ctx = CGM.getContext();
208 FunctionArgList Args;
209 Args.append(CD->param_begin(),
210 std::next(CD->param_begin(), CD->getContextParamPosition()));
211 auto I = S.captures().begin();
212 for (auto *FD : RD->fields()) {
213 QualType ArgType = FD->getType();
214 IdentifierInfo *II = nullptr;
215 VarDecl *CapVar = nullptr;
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000216
217 // If this is a capture by copy and the type is not a pointer, the outlined
218 // function argument type should be uintptr and the value properly casted to
219 // uintptr. This is necessary given that the runtime library is only able to
220 // deal with pointers. We can pass in the same way the VLA type sizes to the
221 // outlined function.
Samuel Antao6d004262016-06-16 18:39:34 +0000222 if ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) ||
223 I->capturesVariableArrayType())
224 ArgType = Ctx.getUIntPtrType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000225
226 if (I->capturesVariable() || I->capturesVariableByCopy()) {
Alexey Bataev2377fe92015-09-10 08:12:02 +0000227 CapVar = I->getCapturedVar();
228 II = CapVar->getIdentifier();
229 } else if (I->capturesThis())
230 II = &getContext().Idents.get("this");
231 else {
232 assert(I->capturesVariableArrayType());
233 II = &getContext().Idents.get("vla");
234 }
235 if (ArgType->isVariablyModifiedType())
236 ArgType = getContext().getVariableArrayDecayedType(ArgType);
237 Args.push_back(ImplicitParamDecl::Create(getContext(), nullptr,
238 FD->getLocation(), II, ArgType));
239 ++I;
240 }
241 Args.append(
242 std::next(CD->param_begin(), CD->getContextParamPosition() + 1),
243 CD->param_end());
244
245 // Create the function declaration.
246 FunctionType::ExtInfo ExtInfo;
247 const CGFunctionInfo &FuncInfo =
John McCallc56a8b32016-03-11 04:30:31 +0000248 CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000249 llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo);
250
251 llvm::Function *F = llvm::Function::Create(
252 FuncLLVMTy, llvm::GlobalValue::InternalLinkage,
253 CapturedStmtInfo->getHelperName(), &CGM.getModule());
254 CGM.SetInternalFunctionAttributes(CD, F, FuncInfo);
255 if (CD->isNothrow())
256 F->addFnAttr(llvm::Attribute::NoUnwind);
257
258 // Generate the function.
259 StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args, CD->getLocation(),
260 CD->getBody()->getLocStart());
261 unsigned Cnt = CD->getContextParamPosition();
262 I = S.captures().begin();
263 for (auto *FD : RD->fields()) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000264 // If we are capturing a pointer by copy we don't need to do anything, just
265 // use the value that we get from the arguments.
266 if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) {
Samuel Antao403ffd42016-07-27 22:49:49 +0000267 const VarDecl *CurVD = I->getCapturedVar();
268 Address LocalAddr = GetAddrOfLocalVar(Args[Cnt]);
269 // If the variable is a reference we need to materialize it here.
270 if (CurVD->getType()->isReferenceType()) {
271 Address RefAddr = CreateMemTemp(CurVD->getType(), getPointerAlign(),
272 ".materialized_ref");
273 EmitStoreOfScalar(LocalAddr.getPointer(), RefAddr, /*Volatile=*/false,
274 CurVD->getType());
275 LocalAddr = RefAddr;
276 }
277 setAddrOfLocalVar(CurVD, LocalAddr);
Richard Trieucc3949d2016-02-18 22:34:54 +0000278 ++Cnt;
279 ++I;
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000280 continue;
281 }
282
Alexey Bataev2377fe92015-09-10 08:12:02 +0000283 LValue ArgLVal =
284 MakeAddrLValue(GetAddrOfLocalVar(Args[Cnt]), Args[Cnt]->getType(),
285 AlignmentSource::Decl);
286 if (FD->hasCapturedVLAType()) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000287 LValue CastedArgLVal =
Samuel Antao6d004262016-06-16 18:39:34 +0000288 MakeAddrLValue(castValueFromUintptr(*this, FD->getType(),
289 Args[Cnt]->getName(), ArgLVal),
290 FD->getType(), AlignmentSource::Decl);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000291 auto *ExprArg =
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000292 EmitLoadOfLValue(CastedArgLVal, SourceLocation()).getScalarVal();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000293 auto VAT = FD->getCapturedVLAType();
294 VLASizeMap[VAT->getSizeExpr()] = ExprArg;
295 } else if (I->capturesVariable()) {
296 auto *Var = I->getCapturedVar();
297 QualType VarTy = Var->getType();
298 Address ArgAddr = ArgLVal.getAddress();
299 if (!VarTy->isReferenceType()) {
300 ArgAddr = EmitLoadOfReference(
301 ArgAddr, ArgLVal.getType()->castAs<ReferenceType>());
302 }
Alexey Bataevc71a4092015-09-11 10:29:41 +0000303 setAddrOfLocalVar(
304 Var, Address(ArgAddr.getPointer(), getContext().getDeclAlign(Var)));
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000305 } else if (I->capturesVariableByCopy()) {
306 assert(!FD->getType()->isAnyPointerType() &&
307 "Not expecting a captured pointer.");
308 auto *Var = I->getCapturedVar();
309 QualType VarTy = Var->getType();
Samuel Antao6d004262016-06-16 18:39:34 +0000310 setAddrOfLocalVar(Var, castValueFromUintptr(*this, FD->getType(),
311 Args[Cnt]->getName(), ArgLVal,
312 VarTy->isReferenceType()));
Alexey Bataev2377fe92015-09-10 08:12:02 +0000313 } else {
314 // If 'this' is captured, load it into CXXThisValue.
315 assert(I->capturesThis());
316 CXXThisValue =
317 EmitLoadOfLValue(ArgLVal, Args[Cnt]->getLocation()).getScalarVal();
318 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000319 ++Cnt;
320 ++I;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000321 }
322
Serge Pavlov3a561452015-12-06 14:32:39 +0000323 PGO.assignRegionCounters(GlobalDecl(CD), F);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000324 CapturedStmtInfo->EmitBody(*this, CD->getBody());
325 FinishFunction(CD->getBodyRBrace());
326
327 return F;
328}
329
Alexey Bataev9959db52014-05-06 10:08:46 +0000330//===----------------------------------------------------------------------===//
331// OpenMP Directive Emission
332//===----------------------------------------------------------------------===//
Alexey Bataev420d45b2015-04-14 05:11:24 +0000333void CodeGenFunction::EmitOMPAggregateAssign(
John McCall7f416cc2015-09-08 08:05:57 +0000334 Address DestAddr, Address SrcAddr, QualType OriginalType,
335 const llvm::function_ref<void(Address, Address)> &CopyGen) {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000336 // Perform element-by-element initialization.
337 QualType ElementTy;
John McCall7f416cc2015-09-08 08:05:57 +0000338
339 // Drill down to the base element type on both arrays.
Alexey Bataev420d45b2015-04-14 05:11:24 +0000340 auto ArrayTy = OriginalType->getAsArrayTypeUnsafe();
John McCall7f416cc2015-09-08 08:05:57 +0000341 auto NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr);
342 SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
343
344 auto SrcBegin = SrcAddr.getPointer();
345 auto DestBegin = DestAddr.getPointer();
Alexey Bataev420d45b2015-04-14 05:11:24 +0000346 // Cast from pointer to array type to pointer to single element.
Alexey Bataev420d45b2015-04-14 05:11:24 +0000347 auto DestEnd = Builder.CreateGEP(DestBegin, NumElements);
348 // The basic structure here is a while-do loop.
349 auto BodyBB = createBasicBlock("omp.arraycpy.body");
350 auto DoneBB = createBasicBlock("omp.arraycpy.done");
351 auto IsEmpty =
352 Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty");
353 Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000354
Alexey Bataev420d45b2015-04-14 05:11:24 +0000355 // Enter the loop body, making that address the current address.
356 auto EntryBB = Builder.GetInsertBlock();
357 EmitBlock(BodyBB);
John McCall7f416cc2015-09-08 08:05:57 +0000358
359 CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy);
360
361 llvm::PHINode *SrcElementPHI =
362 Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast");
363 SrcElementPHI->addIncoming(SrcBegin, EntryBB);
364 Address SrcElementCurrent =
365 Address(SrcElementPHI,
366 SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize));
367
368 llvm::PHINode *DestElementPHI =
369 Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast");
370 DestElementPHI->addIncoming(DestBegin, EntryBB);
371 Address DestElementCurrent =
372 Address(DestElementPHI,
373 DestAddr.getAlignment().alignmentOfArrayElement(ElementSize));
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000374
Alexey Bataev420d45b2015-04-14 05:11:24 +0000375 // Emit copy.
376 CopyGen(DestElementCurrent, SrcElementCurrent);
377
378 // Shift the address forward by one element.
379 auto DestElementNext = Builder.CreateConstGEP1_32(
John McCall7f416cc2015-09-08 08:05:57 +0000380 DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
Alexey Bataev420d45b2015-04-14 05:11:24 +0000381 auto SrcElementNext = Builder.CreateConstGEP1_32(
John McCall7f416cc2015-09-08 08:05:57 +0000382 SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element");
Alexey Bataev420d45b2015-04-14 05:11:24 +0000383 // Check whether we've reached the end.
384 auto Done =
385 Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
386 Builder.CreateCondBr(Done, DoneBB, BodyBB);
John McCall7f416cc2015-09-08 08:05:57 +0000387 DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock());
388 SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock());
Alexey Bataev420d45b2015-04-14 05:11:24 +0000389
390 // Done.
391 EmitBlock(DoneBB, /*IsFinished=*/true);
392}
393
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000394/// Check if the combiner is a call to UDR combiner and if it is so return the
395/// UDR decl used for reduction.
396static const OMPDeclareReductionDecl *
397getReductionInit(const Expr *ReductionOp) {
398 if (auto *CE = dyn_cast<CallExpr>(ReductionOp))
399 if (auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee()))
400 if (auto *DRE =
401 dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts()))
402 if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl()))
403 return DRD;
404 return nullptr;
405}
406
407static void emitInitWithReductionInitializer(CodeGenFunction &CGF,
408 const OMPDeclareReductionDecl *DRD,
409 const Expr *InitOp,
410 Address Private, Address Original,
411 QualType Ty) {
412 if (DRD->getInitializer()) {
413 std::pair<llvm::Function *, llvm::Function *> Reduction =
414 CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD);
415 auto *CE = cast<CallExpr>(InitOp);
416 auto *OVE = cast<OpaqueValueExpr>(CE->getCallee());
417 const Expr *LHS = CE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
418 const Expr *RHS = CE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
419 auto *LHSDRE = cast<DeclRefExpr>(cast<UnaryOperator>(LHS)->getSubExpr());
420 auto *RHSDRE = cast<DeclRefExpr>(cast<UnaryOperator>(RHS)->getSubExpr());
421 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
422 PrivateScope.addPrivate(cast<VarDecl>(LHSDRE->getDecl()),
423 [=]() -> Address { return Private; });
424 PrivateScope.addPrivate(cast<VarDecl>(RHSDRE->getDecl()),
425 [=]() -> Address { return Original; });
426 (void)PrivateScope.Privatize();
427 RValue Func = RValue::get(Reduction.second);
428 CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func);
429 CGF.EmitIgnoredExpr(InitOp);
430 } else {
431 llvm::Constant *Init = CGF.CGM.EmitNullConstant(Ty);
432 auto *GV = new llvm::GlobalVariable(
433 CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true,
434 llvm::GlobalValue::PrivateLinkage, Init, ".init");
435 LValue LV = CGF.MakeNaturalAlignAddrLValue(GV, Ty);
436 RValue InitRVal;
437 switch (CGF.getEvaluationKind(Ty)) {
438 case TEK_Scalar:
439 InitRVal = CGF.EmitLoadOfLValue(LV, SourceLocation());
440 break;
441 case TEK_Complex:
442 InitRVal =
443 RValue::getComplex(CGF.EmitLoadOfComplex(LV, SourceLocation()));
444 break;
445 case TEK_Aggregate:
446 InitRVal = RValue::getAggregate(LV.getAddress());
447 break;
448 }
449 OpaqueValueExpr OVE(SourceLocation(), Ty, VK_RValue);
450 CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE, InitRVal);
451 CGF.EmitAnyExprToMem(&OVE, Private, Ty.getQualifiers(),
452 /*IsInitializer=*/false);
453 }
454}
455
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000456/// \brief Emit initialization of arrays of complex types.
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000457/// \param DestAddr Address of the array.
458/// \param Type Type of array.
459/// \param Init Initial expression of array.
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000460/// \param SrcAddr Address of the original array.
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000461static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr,
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000462 QualType Type, const Expr *Init,
463 Address SrcAddr = Address::invalid()) {
464 auto *DRD = getReductionInit(Init);
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000465 // Perform element-by-element initialization.
466 QualType ElementTy;
467
468 // Drill down to the base element type on both arrays.
469 auto ArrayTy = Type->getAsArrayTypeUnsafe();
470 auto NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr);
471 DestAddr =
472 CGF.Builder.CreateElementBitCast(DestAddr, DestAddr.getElementType());
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000473 if (DRD)
474 SrcAddr =
475 CGF.Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000476
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000477 llvm::Value *SrcBegin = nullptr;
478 if (DRD)
479 SrcBegin = SrcAddr.getPointer();
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000480 auto DestBegin = DestAddr.getPointer();
481 // Cast from pointer to array type to pointer to single element.
482 auto DestEnd = CGF.Builder.CreateGEP(DestBegin, NumElements);
483 // The basic structure here is a while-do loop.
484 auto BodyBB = CGF.createBasicBlock("omp.arrayinit.body");
485 auto DoneBB = CGF.createBasicBlock("omp.arrayinit.done");
486 auto IsEmpty =
487 CGF.Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arrayinit.isempty");
488 CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
489
490 // Enter the loop body, making that address the current address.
491 auto EntryBB = CGF.Builder.GetInsertBlock();
492 CGF.EmitBlock(BodyBB);
493
494 CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy);
495
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000496 llvm::PHINode *SrcElementPHI = nullptr;
497 Address SrcElementCurrent = Address::invalid();
498 if (DRD) {
499 SrcElementPHI = CGF.Builder.CreatePHI(SrcBegin->getType(), 2,
500 "omp.arraycpy.srcElementPast");
501 SrcElementPHI->addIncoming(SrcBegin, EntryBB);
502 SrcElementCurrent =
503 Address(SrcElementPHI,
504 SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize));
505 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000506 llvm::PHINode *DestElementPHI = CGF.Builder.CreatePHI(
507 DestBegin->getType(), 2, "omp.arraycpy.destElementPast");
508 DestElementPHI->addIncoming(DestBegin, EntryBB);
509 Address DestElementCurrent =
510 Address(DestElementPHI,
511 DestAddr.getAlignment().alignmentOfArrayElement(ElementSize));
512
513 // Emit copy.
514 {
515 CodeGenFunction::RunCleanupsScope InitScope(CGF);
Alexey Bataev8fbae8cf2016-04-27 11:38:05 +0000516 if (DRD && (DRD->getInitializer() || !Init)) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000517 emitInitWithReductionInitializer(CGF, DRD, Init, DestElementCurrent,
518 SrcElementCurrent, ElementTy);
519 } else
520 CGF.EmitAnyExprToMem(Init, DestElementCurrent, ElementTy.getQualifiers(),
521 /*IsInitializer=*/false);
522 }
523
524 if (DRD) {
525 // Shift the address forward by one element.
526 auto SrcElementNext = CGF.Builder.CreateConstGEP1_32(
527 SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
528 SrcElementPHI->addIncoming(SrcElementNext, CGF.Builder.GetInsertBlock());
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000529 }
530
531 // Shift the address forward by one element.
532 auto DestElementNext = CGF.Builder.CreateConstGEP1_32(
533 DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
534 // Check whether we've reached the end.
535 auto Done =
536 CGF.Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
537 CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB);
538 DestElementPHI->addIncoming(DestElementNext, CGF.Builder.GetInsertBlock());
539
540 // Done.
541 CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
542}
543
John McCall7f416cc2015-09-08 08:05:57 +0000544void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr,
545 Address SrcAddr, const VarDecl *DestVD,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000546 const VarDecl *SrcVD, const Expr *Copy) {
547 if (OriginalType->isArrayType()) {
548 auto *BO = dyn_cast<BinaryOperator>(Copy);
549 if (BO && BO->getOpcode() == BO_Assign) {
550 // Perform simple memcpy for simple copying.
John McCall7f416cc2015-09-08 08:05:57 +0000551 EmitAggregateAssign(DestAddr, SrcAddr, OriginalType);
Alexey Bataev420d45b2015-04-14 05:11:24 +0000552 } else {
553 // For arrays with complex element types perform element by element
554 // copying.
John McCall7f416cc2015-09-08 08:05:57 +0000555 EmitOMPAggregateAssign(
Alexey Bataev420d45b2015-04-14 05:11:24 +0000556 DestAddr, SrcAddr, OriginalType,
John McCall7f416cc2015-09-08 08:05:57 +0000557 [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000558 // Working with the single array element, so have to remap
559 // destination and source variables to corresponding array
560 // elements.
John McCall7f416cc2015-09-08 08:05:57 +0000561 CodeGenFunction::OMPPrivateScope Remap(*this);
562 Remap.addPrivate(DestVD, [DestElement]() -> Address {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000563 return DestElement;
564 });
565 Remap.addPrivate(
John McCall7f416cc2015-09-08 08:05:57 +0000566 SrcVD, [SrcElement]() -> Address { return SrcElement; });
Alexey Bataev420d45b2015-04-14 05:11:24 +0000567 (void)Remap.Privatize();
John McCall7f416cc2015-09-08 08:05:57 +0000568 EmitIgnoredExpr(Copy);
Alexey Bataev420d45b2015-04-14 05:11:24 +0000569 });
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000570 }
Alexey Bataev420d45b2015-04-14 05:11:24 +0000571 } else {
572 // Remap pseudo source variable to private copy.
John McCall7f416cc2015-09-08 08:05:57 +0000573 CodeGenFunction::OMPPrivateScope Remap(*this);
574 Remap.addPrivate(SrcVD, [SrcAddr]() -> Address { return SrcAddr; });
575 Remap.addPrivate(DestVD, [DestAddr]() -> Address { return DestAddr; });
Alexey Bataev420d45b2015-04-14 05:11:24 +0000576 (void)Remap.Privatize();
577 // Emit copying of the whole variable.
John McCall7f416cc2015-09-08 08:05:57 +0000578 EmitIgnoredExpr(Copy);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000579 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000580}
581
Alexey Bataev69c62a92015-04-15 04:52:20 +0000582bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
583 OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000584 if (!HaveInsertPoint())
585 return false;
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000586 bool FirstprivateIsLastprivate = false;
587 llvm::DenseSet<const VarDecl *> Lastprivates;
588 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
589 for (const auto *D : C->varlists())
590 Lastprivates.insert(
591 cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl());
592 }
Alexey Bataev69c62a92015-04-15 04:52:20 +0000593 llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate;
Alexey Bataev9afe5752016-05-24 07:40:12 +0000594 CGCapturedStmtInfo CapturesInfo(cast<CapturedStmt>(*D.getAssociatedStmt()));
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000595 for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) {
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000596 auto IRef = C->varlist_begin();
597 auto InitsRef = C->inits().begin();
598 for (auto IInit : C->private_copies()) {
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000599 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000600 bool ThisFirstprivateIsLastprivate =
601 Lastprivates.count(OrigVD->getCanonicalDecl()) > 0;
Alexey Bataev9afe5752016-05-24 07:40:12 +0000602 auto *CapFD = CapturesInfo.lookup(OrigVD);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000603 auto *FD = CapturedStmtInfo->lookup(OrigVD);
Alexey Bataev9afe5752016-05-24 07:40:12 +0000604 if (!ThisFirstprivateIsLastprivate && FD && (FD == CapFD) &&
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000605 !FD->getType()->isReferenceType()) {
606 EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl());
607 ++IRef;
608 ++InitsRef;
609 continue;
610 }
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000611 FirstprivateIsLastprivate =
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000612 FirstprivateIsLastprivate || ThisFirstprivateIsLastprivate;
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000613 if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000614 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
615 auto *VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl());
616 bool IsRegistered;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000617 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
618 /*RefersToEnclosingVariableOrCapture=*/FD != nullptr,
619 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
John McCall7f416cc2015-09-08 08:05:57 +0000620 Address OriginalAddr = EmitLValue(&DRE).getAddress();
Alexey Bataevfeddd642016-04-22 09:05:03 +0000621 QualType Type = VD->getType();
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000622 if (Type->isArrayType()) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000623 // Emit VarDecl with copy init for arrays.
624 // Get the address of the original variable captured in current
625 // captured region.
John McCall7f416cc2015-09-08 08:05:57 +0000626 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000627 auto Emission = EmitAutoVarAlloca(*VD);
628 auto *Init = VD->getInit();
629 if (!isa<CXXConstructExpr>(Init) || isTrivialInitializer(Init)) {
630 // Perform simple memcpy.
631 EmitAggregateAssign(Emission.getAllocatedAddress(), OriginalAddr,
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000632 Type);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000633 } else {
634 EmitOMPAggregateAssign(
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000635 Emission.getAllocatedAddress(), OriginalAddr, Type,
John McCall7f416cc2015-09-08 08:05:57 +0000636 [this, VDInit, Init](Address DestElement,
637 Address SrcElement) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000638 // Clean up any temporaries needed by the initialization.
639 RunCleanupsScope InitScope(*this);
640 // Emit initialization for single element.
John McCall7f416cc2015-09-08 08:05:57 +0000641 setAddrOfLocalVar(VDInit, SrcElement);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000642 EmitAnyExprToMem(Init, DestElement,
643 Init->getType().getQualifiers(),
644 /*IsInitializer*/ false);
645 LocalDeclMap.erase(VDInit);
646 });
647 }
648 EmitAutoVarCleanups(Emission);
649 return Emission.getAllocatedAddress();
650 });
651 } else {
John McCall7f416cc2015-09-08 08:05:57 +0000652 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000653 // Emit private VarDecl with copy init.
654 // Remap temp VDInit variable to the address of the original
655 // variable
656 // (for proper handling of captured global variables).
John McCall7f416cc2015-09-08 08:05:57 +0000657 setAddrOfLocalVar(VDInit, OriginalAddr);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000658 EmitDecl(*VD);
659 LocalDeclMap.erase(VDInit);
660 return GetAddrOfLocalVar(VD);
661 });
662 }
663 assert(IsRegistered &&
664 "firstprivate var already registered as private");
665 // Silence the warning about unused variable.
666 (void)IsRegistered;
667 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000668 ++IRef;
669 ++InitsRef;
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000670 }
671 }
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000672 return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000673}
674
Alexey Bataev03b340a2014-10-21 03:16:40 +0000675void CodeGenFunction::EmitOMPPrivateClause(
676 const OMPExecutableDirective &D,
677 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000678 if (!HaveInsertPoint())
679 return;
Alexey Bataev50a64582015-04-22 12:24:45 +0000680 llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000681 for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) {
Alexey Bataev03b340a2014-10-21 03:16:40 +0000682 auto IRef = C->varlist_begin();
683 for (auto IInit : C->private_copies()) {
684 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev50a64582015-04-22 12:24:45 +0000685 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
686 auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
687 bool IsRegistered =
John McCall7f416cc2015-09-08 08:05:57 +0000688 PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev50a64582015-04-22 12:24:45 +0000689 // Emit private VarDecl with copy init.
690 EmitDecl(*VD);
691 return GetAddrOfLocalVar(VD);
692 });
693 assert(IsRegistered && "private var already registered as private");
694 // Silence the warning about unused variable.
695 (void)IsRegistered;
696 }
Alexey Bataev03b340a2014-10-21 03:16:40 +0000697 ++IRef;
698 }
699 }
700}
701
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000702bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000703 if (!HaveInsertPoint())
704 return false;
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000705 // threadprivate_var1 = master_threadprivate_var1;
706 // operator=(threadprivate_var2, master_threadprivate_var2);
707 // ...
708 // __kmpc_barrier(&loc, global_tid);
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000709 llvm::DenseSet<const VarDecl *> CopiedVars;
710 llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000711 for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) {
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000712 auto IRef = C->varlist_begin();
713 auto ISrcRef = C->source_exprs().begin();
714 auto IDestRef = C->destination_exprs().begin();
715 for (auto *AssignOp : C->assignment_ops()) {
716 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000717 QualType Type = VD->getType();
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000718 if (CopiedVars.insert(VD->getCanonicalDecl()).second) {
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000719 // Get the address of the master variable. If we are emitting code with
720 // TLS support, the address is passed from the master as field in the
721 // captured declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000722 Address MasterAddr = Address::invalid();
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000723 if (getLangOpts().OpenMPUseTLS &&
724 getContext().getTargetInfo().isTLSSupported()) {
725 assert(CapturedStmtInfo->lookup(VD) &&
726 "Copyin threadprivates should have been captured!");
727 DeclRefExpr DRE(const_cast<VarDecl *>(VD), true, (*IRef)->getType(),
728 VK_LValue, (*IRef)->getExprLoc());
729 MasterAddr = EmitLValue(&DRE).getAddress();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000730 LocalDeclMap.erase(VD);
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000731 } else {
John McCall7f416cc2015-09-08 08:05:57 +0000732 MasterAddr =
733 Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD)
734 : CGM.GetAddrOfGlobal(VD),
735 getContext().getDeclAlign(VD));
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000736 }
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000737 // Get the address of the threadprivate variable.
John McCall7f416cc2015-09-08 08:05:57 +0000738 Address PrivateAddr = EmitLValue(*IRef).getAddress();
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000739 if (CopiedVars.size() == 1) {
740 // At first check if current thread is a master thread. If it is, no
741 // need to copy data.
742 CopyBegin = createBasicBlock("copyin.not.master");
743 CopyEnd = createBasicBlock("copyin.not.master.end");
744 Builder.CreateCondBr(
745 Builder.CreateICmpNE(
John McCall7f416cc2015-09-08 08:05:57 +0000746 Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy),
747 Builder.CreatePtrToInt(PrivateAddr.getPointer(), CGM.IntPtrTy)),
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000748 CopyBegin, CopyEnd);
749 EmitBlock(CopyBegin);
750 }
751 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
752 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +0000753 EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp);
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000754 }
755 ++IRef;
756 ++ISrcRef;
757 ++IDestRef;
758 }
759 }
760 if (CopyEnd) {
761 // Exit out of copying procedure for non-master thread.
762 EmitBlock(CopyEnd, /*IsFinished=*/true);
763 return true;
764 }
765 return false;
766}
767
Alexey Bataev38e89532015-04-16 04:54:05 +0000768bool CodeGenFunction::EmitOMPLastprivateClauseInit(
769 const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000770 if (!HaveInsertPoint())
771 return false;
Alexey Bataev38e89532015-04-16 04:54:05 +0000772 bool HasAtLeastOneLastprivate = false;
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000773 llvm::DenseSet<const VarDecl *> SIMDLCVs;
774 if (isOpenMPSimdDirective(D.getDirectiveKind())) {
775 auto *LoopDirective = cast<OMPLoopDirective>(&D);
776 for (auto *C : LoopDirective->counters()) {
777 SIMDLCVs.insert(
778 cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl());
779 }
780 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000781 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000782 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
Alexey Bataevd130fd12015-05-13 10:23:02 +0000783 HasAtLeastOneLastprivate = true;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000784 if (isOpenMPTaskLoopDirective(D.getDirectiveKind()))
785 break;
Alexey Bataev38e89532015-04-16 04:54:05 +0000786 auto IRef = C->varlist_begin();
787 auto IDestRef = C->destination_exprs().begin();
788 for (auto *IInit : C->private_copies()) {
789 // Keep the address of the original variable for future update at the end
790 // of the loop.
791 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataevf93095a2016-05-05 08:46:22 +0000792 // Taskloops do not require additional initialization, it is done in
793 // runtime support library.
Alexey Bataev38e89532015-04-16 04:54:05 +0000794 if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) {
795 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +0000796 PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() -> Address {
Alexey Bataev38e89532015-04-16 04:54:05 +0000797 DeclRefExpr DRE(
798 const_cast<VarDecl *>(OrigVD),
799 /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup(
800 OrigVD) != nullptr,
801 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
802 return EmitLValue(&DRE).getAddress();
803 });
804 // Check if the variable is also a firstprivate: in this case IInit is
805 // not generated. Initialization of this variable will happen in codegen
806 // for 'firstprivate' clause.
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000807 if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) {
Alexey Bataevd130fd12015-05-13 10:23:02 +0000808 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
Alexey Bataevf93095a2016-05-05 08:46:22 +0000809 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
810 // Emit private VarDecl with copy init.
811 EmitDecl(*VD);
812 return GetAddrOfLocalVar(VD);
813 });
Alexey Bataevd130fd12015-05-13 10:23:02 +0000814 assert(IsRegistered &&
815 "lastprivate var already registered as private");
816 (void)IsRegistered;
817 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000818 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000819 ++IRef;
820 ++IDestRef;
Alexey Bataev38e89532015-04-16 04:54:05 +0000821 }
822 }
823 return HasAtLeastOneLastprivate;
824}
825
826void CodeGenFunction::EmitOMPLastprivateClauseFinal(
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000827 const OMPExecutableDirective &D, bool NoFinals,
828 llvm::Value *IsLastIterCond) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000829 if (!HaveInsertPoint())
830 return;
Alexey Bataev38e89532015-04-16 04:54:05 +0000831 // Emit following code:
832 // if (<IsLastIterCond>) {
833 // orig_var1 = private_orig_var1;
834 // ...
835 // orig_varn = private_orig_varn;
836 // }
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000837 llvm::BasicBlock *ThenBB = nullptr;
838 llvm::BasicBlock *DoneBB = nullptr;
839 if (IsLastIterCond) {
840 ThenBB = createBasicBlock(".omp.lastprivate.then");
841 DoneBB = createBasicBlock(".omp.lastprivate.done");
842 Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB);
843 EmitBlock(ThenBB);
844 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000845 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
846 llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates;
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000847 if (auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) {
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000848 auto IC = LoopDirective->counters().begin();
849 for (auto F : LoopDirective->finals()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000850 auto *D =
851 cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl();
852 if (NoFinals)
853 AlreadyEmittedVars.insert(D);
854 else
855 LoopCountersAndUpdates[D] = F;
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000856 ++IC;
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000857 }
858 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000859 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
860 auto IRef = C->varlist_begin();
861 auto ISrcRef = C->source_exprs().begin();
862 auto IDestRef = C->destination_exprs().begin();
863 for (auto *AssignOp : C->assignment_ops()) {
864 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
865 QualType Type = PrivateVD->getType();
866 auto *CanonicalVD = PrivateVD->getCanonicalDecl();
867 if (AlreadyEmittedVars.insert(CanonicalVD).second) {
868 // If lastprivate variable is a loop control variable for loop-based
869 // directive, update its value before copyin back to original
870 // variable.
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000871 if (auto *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD))
872 EmitIgnoredExpr(FinalExpr);
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000873 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
874 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
875 // Get the address of the original variable.
876 Address OriginalAddr = GetAddrOfLocalVar(DestVD);
877 // Get the address of the private variable.
878 Address PrivateAddr = GetAddrOfLocalVar(PrivateVD);
879 if (auto RefTy = PrivateVD->getType()->getAs<ReferenceType>())
880 PrivateAddr =
John McCall7f416cc2015-09-08 08:05:57 +0000881 Address(Builder.CreateLoad(PrivateAddr),
882 getNaturalTypeAlignment(RefTy->getPointeeType()));
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000883 EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp);
Alexey Bataev38e89532015-04-16 04:54:05 +0000884 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000885 ++IRef;
886 ++ISrcRef;
887 ++IDestRef;
Alexey Bataev38e89532015-04-16 04:54:05 +0000888 }
Alexey Bataev005248a2016-02-25 05:25:57 +0000889 if (auto *PostUpdate = C->getPostUpdateExpr())
890 EmitIgnoredExpr(PostUpdate);
Alexey Bataev38e89532015-04-16 04:54:05 +0000891 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000892 if (IsLastIterCond)
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000893 EmitBlock(DoneBB, /*IsFinished=*/true);
Alexey Bataev38e89532015-04-16 04:54:05 +0000894}
895
Alexey Bataev31300ed2016-02-04 11:27:03 +0000896static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
897 LValue BaseLV, llvm::Value *Addr) {
898 Address Tmp = Address::invalid();
899 Address TopTmp = Address::invalid();
900 Address MostTopTmp = Address::invalid();
901 BaseTy = BaseTy.getNonReferenceType();
902 while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) &&
903 !CGF.getContext().hasSameType(BaseTy, ElTy)) {
904 Tmp = CGF.CreateMemTemp(BaseTy);
905 if (TopTmp.isValid())
906 CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp);
907 else
908 MostTopTmp = Tmp;
909 TopTmp = Tmp;
910 BaseTy = BaseTy->getPointeeType();
911 }
912 llvm::Type *Ty = BaseLV.getPointer()->getType();
913 if (Tmp.isValid())
914 Ty = Tmp.getElementType();
915 Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty);
916 if (Tmp.isValid()) {
917 CGF.Builder.CreateStore(Addr, Tmp);
918 return MostTopTmp;
919 }
920 return Address(Addr, BaseLV.getAlignment());
921}
922
923static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
924 LValue BaseLV) {
925 BaseTy = BaseTy.getNonReferenceType();
926 while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) &&
927 !CGF.getContext().hasSameType(BaseTy, ElTy)) {
928 if (auto *PtrTy = BaseTy->getAs<PointerType>())
929 BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy);
930 else {
931 BaseLV = CGF.EmitLoadOfReferenceLValue(BaseLV.getAddress(),
932 BaseTy->castAs<ReferenceType>());
933 }
934 BaseTy = BaseTy->getPointeeType();
935 }
936 return CGF.MakeAddrLValue(
937 Address(
938 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
939 BaseLV.getPointer(), CGF.ConvertTypeForMem(ElTy)->getPointerTo()),
940 BaseLV.getAlignment()),
941 BaseLV.getType(), BaseLV.getAlignmentSource());
942}
943
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000944void CodeGenFunction::EmitOMPReductionClauseInit(
945 const OMPExecutableDirective &D,
946 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000947 if (!HaveInsertPoint())
948 return;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000949 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000950 auto ILHS = C->lhs_exprs().begin();
951 auto IRHS = C->rhs_exprs().begin();
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000952 auto IPriv = C->privates().begin();
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000953 auto IRed = C->reduction_ops().begin();
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000954 for (auto IRef : C->varlists()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000955 auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000956 auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
957 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl());
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000958 auto *DRD = getReductionInit(*IRed);
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000959 if (auto *OASE = dyn_cast<OMPArraySectionExpr>(IRef)) {
960 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
961 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
962 Base = TempOASE->getBase()->IgnoreParenImpCasts();
963 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
964 Base = TempASE->getBase()->IgnoreParenImpCasts();
965 auto *DE = cast<DeclRefExpr>(Base);
966 auto *OrigVD = cast<VarDecl>(DE->getDecl());
967 auto OASELValueLB = EmitOMPArraySectionExpr(OASE);
968 auto OASELValueUB =
969 EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false);
970 auto OriginalBaseLValue = EmitLValue(DE);
Alexey Bataev31300ed2016-02-04 11:27:03 +0000971 LValue BaseLValue =
972 loadToBegin(*this, OrigVD->getType(), OASELValueLB.getType(),
973 OriginalBaseLValue);
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000974 // Store the address of the original variable associated with the LHS
975 // implicit variable.
976 PrivateScope.addPrivate(LHSVD, [this, OASELValueLB]() -> Address {
977 return OASELValueLB.getAddress();
978 });
979 // Emit reduction copy.
980 bool IsRegistered = PrivateScope.addPrivate(
Alexey Bataev31300ed2016-02-04 11:27:03 +0000981 OrigVD, [this, OrigVD, PrivateVD, BaseLValue, OASELValueLB,
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000982 OASELValueUB, OriginalBaseLValue, DRD, IRed]() -> Address {
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000983 // Emit VarDecl with copy init for arrays.
984 // Get the address of the original variable captured in current
985 // captured region.
986 auto *Size = Builder.CreatePtrDiff(OASELValueUB.getPointer(),
987 OASELValueLB.getPointer());
988 Size = Builder.CreateNUWAdd(
989 Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1));
990 CodeGenFunction::OpaqueValueMapping OpaqueMap(
991 *this, cast<OpaqueValueExpr>(
992 getContext()
993 .getAsVariableArrayType(PrivateVD->getType())
994 ->getSizeExpr()),
995 RValue::get(Size));
996 EmitVariablyModifiedType(PrivateVD->getType());
997 auto Emission = EmitAutoVarAlloca(*PrivateVD);
998 auto Addr = Emission.getAllocatedAddress();
999 auto *Init = PrivateVD->getInit();
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001000 EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(),
1001 DRD ? *IRed : Init,
1002 OASELValueLB.getAddress());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001003 EmitAutoVarCleanups(Emission);
1004 // Emit private VarDecl with reduction init.
1005 auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(),
1006 OASELValueLB.getPointer());
1007 auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset);
Alexey Bataev31300ed2016-02-04 11:27:03 +00001008 return castToBase(*this, OrigVD->getType(),
1009 OASELValueLB.getType(), OriginalBaseLValue,
1010 Ptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001011 });
1012 assert(IsRegistered && "private var already registered as private");
1013 // Silence the warning about unused variable.
1014 (void)IsRegistered;
1015 PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address {
1016 return GetAddrOfLocalVar(PrivateVD);
1017 });
1018 } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(IRef)) {
1019 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
1020 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
1021 Base = TempASE->getBase()->IgnoreParenImpCasts();
1022 auto *DE = cast<DeclRefExpr>(Base);
1023 auto *OrigVD = cast<VarDecl>(DE->getDecl());
1024 auto ASELValue = EmitLValue(ASE);
1025 auto OriginalBaseLValue = EmitLValue(DE);
Alexey Bataev31300ed2016-02-04 11:27:03 +00001026 LValue BaseLValue = loadToBegin(
1027 *this, OrigVD->getType(), ASELValue.getType(), OriginalBaseLValue);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001028 // Store the address of the original variable associated with the LHS
1029 // implicit variable.
1030 PrivateScope.addPrivate(LHSVD, [this, ASELValue]() -> Address {
1031 return ASELValue.getAddress();
1032 });
1033 // Emit reduction copy.
1034 bool IsRegistered = PrivateScope.addPrivate(
Alexey Bataev31300ed2016-02-04 11:27:03 +00001035 OrigVD, [this, OrigVD, PrivateVD, BaseLValue, ASELValue,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001036 OriginalBaseLValue, DRD, IRed]() -> Address {
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001037 // Emit private VarDecl with reduction init.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001038 AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD);
1039 auto Addr = Emission.getAllocatedAddress();
Alexey Bataev8fbae8cf2016-04-27 11:38:05 +00001040 if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001041 emitInitWithReductionInitializer(*this, DRD, *IRed, Addr,
1042 ASELValue.getAddress(),
1043 ASELValue.getType());
1044 } else
1045 EmitAutoVarInit(Emission);
1046 EmitAutoVarCleanups(Emission);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001047 auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(),
1048 ASELValue.getPointer());
1049 auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset);
Alexey Bataev31300ed2016-02-04 11:27:03 +00001050 return castToBase(*this, OrigVD->getType(), ASELValue.getType(),
1051 OriginalBaseLValue, Ptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001052 });
1053 assert(IsRegistered && "private var already registered as private");
1054 // Silence the warning about unused variable.
1055 (void)IsRegistered;
Alexey Bataev1189bd02016-01-26 12:20:39 +00001056 PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address {
1057 return Builder.CreateElementBitCast(
1058 GetAddrOfLocalVar(PrivateVD), ConvertTypeForMem(RHSVD->getType()),
1059 "rhs.begin");
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001060 });
1061 } else {
1062 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl());
Alexey Bataev1189bd02016-01-26 12:20:39 +00001063 QualType Type = PrivateVD->getType();
1064 if (getContext().getAsArrayType(Type)) {
1065 // Store the address of the original variable associated with the LHS
1066 // implicit variable.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001067 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
1068 CapturedStmtInfo->lookup(OrigVD) != nullptr,
1069 IRef->getType(), VK_LValue, IRef->getExprLoc());
Alexey Bataev1189bd02016-01-26 12:20:39 +00001070 Address OriginalAddr = EmitLValue(&DRE).getAddress();
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001071 PrivateScope.addPrivate(LHSVD, [this, &OriginalAddr,
Alexey Bataev1189bd02016-01-26 12:20:39 +00001072 LHSVD]() -> Address {
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001073 OriginalAddr = Builder.CreateElementBitCast(
1074 OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin");
1075 return OriginalAddr;
Alexey Bataev1189bd02016-01-26 12:20:39 +00001076 });
1077 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
1078 if (Type->isVariablyModifiedType()) {
1079 CodeGenFunction::OpaqueValueMapping OpaqueMap(
1080 *this, cast<OpaqueValueExpr>(
1081 getContext()
1082 .getAsVariableArrayType(PrivateVD->getType())
1083 ->getSizeExpr()),
1084 RValue::get(
1085 getTypeSize(OrigVD->getType().getNonReferenceType())));
1086 EmitVariablyModifiedType(Type);
1087 }
1088 auto Emission = EmitAutoVarAlloca(*PrivateVD);
1089 auto Addr = Emission.getAllocatedAddress();
1090 auto *Init = PrivateVD->getInit();
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001091 EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(),
1092 DRD ? *IRed : Init, OriginalAddr);
Alexey Bataev1189bd02016-01-26 12:20:39 +00001093 EmitAutoVarCleanups(Emission);
1094 return Emission.getAllocatedAddress();
1095 });
1096 assert(IsRegistered && "private var already registered as private");
1097 // Silence the warning about unused variable.
1098 (void)IsRegistered;
1099 PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address {
1100 return Builder.CreateElementBitCast(
1101 GetAddrOfLocalVar(PrivateVD),
1102 ConvertTypeForMem(RHSVD->getType()), "rhs.begin");
1103 });
1104 } else {
1105 // Store the address of the original variable associated with the LHS
1106 // implicit variable.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001107 Address OriginalAddr = Address::invalid();
1108 PrivateScope.addPrivate(LHSVD, [this, OrigVD, IRef,
1109 &OriginalAddr]() -> Address {
Alexey Bataev1189bd02016-01-26 12:20:39 +00001110 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
1111 CapturedStmtInfo->lookup(OrigVD) != nullptr,
1112 IRef->getType(), VK_LValue, IRef->getExprLoc());
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001113 OriginalAddr = EmitLValue(&DRE).getAddress();
1114 return OriginalAddr;
Alexey Bataev1189bd02016-01-26 12:20:39 +00001115 });
1116 // Emit reduction copy.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001117 bool IsRegistered = PrivateScope.addPrivate(
1118 OrigVD, [this, PrivateVD, OriginalAddr, DRD, IRed]() -> Address {
Alexey Bataev1189bd02016-01-26 12:20:39 +00001119 // Emit private VarDecl with reduction init.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001120 AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD);
1121 auto Addr = Emission.getAllocatedAddress();
Alexey Bataev8fbae8cf2016-04-27 11:38:05 +00001122 if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001123 emitInitWithReductionInitializer(*this, DRD, *IRed, Addr,
1124 OriginalAddr,
1125 PrivateVD->getType());
1126 } else
1127 EmitAutoVarInit(Emission);
1128 EmitAutoVarCleanups(Emission);
1129 return Addr;
Alexey Bataev1189bd02016-01-26 12:20:39 +00001130 });
1131 assert(IsRegistered && "private var already registered as private");
1132 // Silence the warning about unused variable.
1133 (void)IsRegistered;
1134 PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address {
1135 return GetAddrOfLocalVar(PrivateVD);
1136 });
1137 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001138 }
Richard Trieucc3949d2016-02-18 22:34:54 +00001139 ++ILHS;
1140 ++IRHS;
1141 ++IPriv;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00001142 ++IRed;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001143 }
1144 }
1145}
1146
1147void CodeGenFunction::EmitOMPReductionClauseFinal(
1148 const OMPExecutableDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001149 if (!HaveInsertPoint())
1150 return;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001151 llvm::SmallVector<const Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001152 llvm::SmallVector<const Expr *, 8> LHSExprs;
1153 llvm::SmallVector<const Expr *, 8> RHSExprs;
1154 llvm::SmallVector<const Expr *, 8> ReductionOps;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001155 bool HasAtLeastOneReduction = false;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001156 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001157 HasAtLeastOneReduction = true;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001158 Privates.append(C->privates().begin(), C->privates().end());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001159 LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end());
1160 RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end());
1161 ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end());
1162 }
1163 if (HasAtLeastOneReduction) {
1164 // Emit nowait reduction if nowait clause is present or directive is a
1165 // parallel directive (it always has implicit barrier).
1166 CGM.getOpenMPRuntime().emitReduction(
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001167 *this, D.getLocEnd(), Privates, LHSExprs, RHSExprs, ReductionOps,
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001168 D.getSingleClause<OMPNowaitClause>() ||
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00001169 isOpenMPParallelDirective(D.getDirectiveKind()) ||
1170 D.getDirectiveKind() == OMPD_simd,
1171 D.getDirectiveKind() == OMPD_simd);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001172 }
1173}
1174
Alexey Bataev61205072016-03-02 04:57:40 +00001175static void emitPostUpdateForReductionClause(
1176 CodeGenFunction &CGF, const OMPExecutableDirective &D,
1177 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
1178 if (!CGF.HaveInsertPoint())
1179 return;
1180 llvm::BasicBlock *DoneBB = nullptr;
1181 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
1182 if (auto *PostUpdate = C->getPostUpdateExpr()) {
1183 if (!DoneBB) {
1184 if (auto *Cond = CondGen(CGF)) {
1185 // If the first post-update expression is found, emit conditional
1186 // block if it was requested.
1187 auto *ThenBB = CGF.createBasicBlock(".omp.reduction.pu");
1188 DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done");
1189 CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1190 CGF.EmitBlock(ThenBB);
1191 }
1192 }
1193 CGF.EmitIgnoredExpr(PostUpdate);
1194 }
1195 }
1196 if (DoneBB)
1197 CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
1198}
1199
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001200static void emitCommonOMPParallelDirective(CodeGenFunction &CGF,
1201 const OMPExecutableDirective &S,
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001202 OpenMPDirectiveKind InnermostKind,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001203 const RegionCodeGenTy &CodeGen) {
Alexey Bataev18095712014-10-10 12:19:54 +00001204 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001205 auto OutlinedFn = CGF.CGM.getOpenMPRuntime().
1206 emitParallelOrTeamsOutlinedFunction(S,
1207 *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001208 if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) {
Alexey Bataev1d677132015-04-22 13:57:31 +00001209 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
Alexey Bataev1d677132015-04-22 13:57:31 +00001210 auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
1211 /*IgnoreResultAssign*/ true);
1212 CGF.CGM.getOpenMPRuntime().emitNumThreadsClause(
1213 CGF, NumThreads, NumThreadsClause->getLocStart());
1214 }
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001215 if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001216 CodeGenFunction::RunCleanupsScope ProcBindScope(CGF);
Alexey Bataev7f210c62015-06-18 13:40:03 +00001217 CGF.CGM.getOpenMPRuntime().emitProcBindClause(
1218 CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getLocStart());
1219 }
Alexey Bataev1d677132015-04-22 13:57:31 +00001220 const Expr *IfCond = nullptr;
Alexey Bataev7371aa32015-09-03 08:45:56 +00001221 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
1222 if (C->getNameModifier() == OMPD_unknown ||
1223 C->getNameModifier() == OMPD_parallel) {
1224 IfCond = C->getCondition();
1225 break;
1226 }
Alexey Bataev1d677132015-04-22 13:57:31 +00001227 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001228
1229 OMPLexicalScope Scope(CGF, S);
1230 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
1231 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
Alexey Bataev1d677132015-04-22 13:57:31 +00001232 CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +00001233 CapturedVars, IfCond);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001234}
1235
1236void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001237 // Emit parallel region as a standalone region.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001238 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001239 OMPPrivateScope PrivateScope(CGF);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001240 bool Copyins = CGF.EmitOMPCopyinClause(S);
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00001241 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
1242 if (Copyins) {
Alexey Bataev69c62a92015-04-15 04:52:20 +00001243 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00001244 // propagation master's thread values of threadprivate variables to local
1245 // instances of that variables of all other implicit threads.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001246 CGF.CGM.getOpenMPRuntime().emitBarrierCall(
1247 CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
1248 /*ForceSimpleCall=*/true);
Alexey Bataev69c62a92015-04-15 04:52:20 +00001249 }
1250 CGF.EmitOMPPrivateClause(S, PrivateScope);
1251 CGF.EmitOMPReductionClauseInit(S, PrivateScope);
1252 (void)PrivateScope.Privatize();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001253 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001254 CGF.EmitOMPReductionClauseFinal(S);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001255 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001256 emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen);
Alexey Bataev61205072016-03-02 04:57:40 +00001257 emitPostUpdateForReductionClause(
1258 *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev9959db52014-05-06 10:08:46 +00001259}
Alexander Musman515ad8c2014-05-22 08:54:05 +00001260
Alexey Bataev0f34da12015-07-02 04:17:07 +00001261void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D,
1262 JumpDest LoopExit) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00001263 RunCleanupsScope BodyScope(*this);
1264 // Update counters values on current iteration.
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001265 for (auto I : D.updates()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00001266 EmitIgnoredExpr(I);
1267 }
Alexander Musman3276a272015-03-21 10:12:56 +00001268 // Update the linear variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001269 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001270 for (auto *U : C->updates())
Alexander Musman3276a272015-03-21 10:12:56 +00001271 EmitIgnoredExpr(U);
Alexander Musman3276a272015-03-21 10:12:56 +00001272 }
1273
Alexander Musmana5f070a2014-10-01 06:03:56 +00001274 // On a continue in the body, jump to the end.
Alexander Musmand196ef22014-10-07 08:57:09 +00001275 auto Continue = getJumpDestInCurrentScope("omp.body.continue");
Alexey Bataev0f34da12015-07-02 04:17:07 +00001276 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
Alexander Musmana5f070a2014-10-01 06:03:56 +00001277 // Emit loop body.
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001278 EmitStmt(D.getBody());
Alexander Musmana5f070a2014-10-01 06:03:56 +00001279 // The end (updates/cleanups).
1280 EmitBlock(Continue.getBlock());
1281 BreakContinueStack.pop_back();
Alexander Musmana5f070a2014-10-01 06:03:56 +00001282}
1283
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001284void CodeGenFunction::EmitOMPInnerLoop(
1285 const Stmt &S, bool RequiresCleanup, const Expr *LoopCond,
1286 const Expr *IncExpr,
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001287 const llvm::function_ref<void(CodeGenFunction &)> &BodyGen,
1288 const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) {
Alexander Musmand196ef22014-10-07 08:57:09 +00001289 auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001290
1291 // Start the loop with a block that tests the condition.
Alexander Musmand196ef22014-10-07 08:57:09 +00001292 auto CondBlock = createBasicBlock("omp.inner.for.cond");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001293 EmitBlock(CondBlock);
Hal Finkelc07e19b2016-05-25 21:53:24 +00001294 LoopStack.push(CondBlock, Builder.getCurrentDebugLocation());
Alexander Musmana5f070a2014-10-01 06:03:56 +00001295
1296 // If there are any cleanups between here and the loop-exit scope,
1297 // create a block to stage a loop exit along.
1298 auto ExitBlock = LoopExit.getBlock();
Alexey Bataev2df54a02015-03-12 08:53:29 +00001299 if (RequiresCleanup)
Alexander Musmand196ef22014-10-07 08:57:09 +00001300 ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001301
Alexander Musmand196ef22014-10-07 08:57:09 +00001302 auto LoopBody = createBasicBlock("omp.inner.for.body");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001303
Alexey Bataev2df54a02015-03-12 08:53:29 +00001304 // Emit condition.
Justin Bogner66242d62015-04-23 23:06:47 +00001305 EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S));
Alexander Musmana5f070a2014-10-01 06:03:56 +00001306 if (ExitBlock != LoopExit.getBlock()) {
1307 EmitBlock(ExitBlock);
1308 EmitBranchThroughCleanup(LoopExit);
1309 }
1310
1311 EmitBlock(LoopBody);
Justin Bogner66242d62015-04-23 23:06:47 +00001312 incrementProfileCounter(&S);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001313
1314 // Create a block for the increment.
Alexander Musmand196ef22014-10-07 08:57:09 +00001315 auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001316 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1317
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001318 BodyGen(*this);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001319
1320 // Emit "IV = IV + 1" and a back-edge to the condition block.
1321 EmitBlock(Continue.getBlock());
Alexey Bataev2df54a02015-03-12 08:53:29 +00001322 EmitIgnoredExpr(IncExpr);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001323 PostIncGen(*this);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001324 BreakContinueStack.pop_back();
1325 EmitBranch(CondBlock);
1326 LoopStack.pop();
1327 // Emit the fall-through block.
1328 EmitBlock(LoopExit.getBlock());
1329}
1330
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001331void CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001332 if (!HaveInsertPoint())
1333 return;
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001334 // Emit inits for the linear variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001335 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001336 for (auto *Init : C->inits()) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001337 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl());
Alexey Bataevef549a82016-03-09 09:49:09 +00001338 if (auto *Ref = dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) {
1339 AutoVarEmission Emission = EmitAutoVarAlloca(*VD);
1340 auto *OrigVD = cast<VarDecl>(Ref->getDecl());
1341 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
1342 CapturedStmtInfo->lookup(OrigVD) != nullptr,
1343 VD->getInit()->getType(), VK_LValue,
1344 VD->getInit()->getExprLoc());
1345 EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(),
1346 VD->getType()),
1347 /*capturedByInit=*/false);
1348 EmitAutoVarCleanups(Emission);
1349 } else
1350 EmitVarDecl(*VD);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001351 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001352 // Emit the linear steps for the linear clauses.
1353 // If a step is not constant, it is pre-calculated before the loop.
1354 if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep()))
1355 if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) {
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001356 EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl()));
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001357 // Emit calculation of the linear step.
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001358 EmitIgnoredExpr(CS);
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001359 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001360 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001361}
1362
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001363void CodeGenFunction::EmitOMPLinearClauseFinal(
1364 const OMPLoopDirective &D,
Alexey Bataevef549a82016-03-09 09:49:09 +00001365 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001366 if (!HaveInsertPoint())
Alexey Bataev8ef31412015-12-18 07:58:25 +00001367 return;
Alexey Bataevef549a82016-03-09 09:49:09 +00001368 llvm::BasicBlock *DoneBB = nullptr;
Alexander Musman3276a272015-03-21 10:12:56 +00001369 // Emit the final values of the linear variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001370 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataev39f915b82015-05-08 10:41:21 +00001371 auto IC = C->varlist_begin();
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001372 for (auto *F : C->finals()) {
Alexey Bataevef549a82016-03-09 09:49:09 +00001373 if (!DoneBB) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001374 if (auto *Cond = CondGen(*this)) {
Alexey Bataevef549a82016-03-09 09:49:09 +00001375 // If the first post-update expression is found, emit conditional
1376 // block if it was requested.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001377 auto *ThenBB = createBasicBlock(".omp.linear.pu");
1378 DoneBB = createBasicBlock(".omp.linear.pu.done");
1379 Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1380 EmitBlock(ThenBB);
Alexey Bataevef549a82016-03-09 09:49:09 +00001381 }
1382 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00001383 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl());
1384 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001385 CapturedStmtInfo->lookup(OrigVD) != nullptr,
Alexey Bataev39f915b82015-05-08 10:41:21 +00001386 (*IC)->getType(), VK_LValue, (*IC)->getExprLoc());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001387 Address OrigAddr = EmitLValue(&DRE).getAddress();
1388 CodeGenFunction::OMPPrivateScope VarScope(*this);
1389 VarScope.addPrivate(OrigVD, [OrigAddr]() -> Address { return OrigAddr; });
Alexey Bataev39f915b82015-05-08 10:41:21 +00001390 (void)VarScope.Privatize();
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001391 EmitIgnoredExpr(F);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001392 ++IC;
Alexander Musman3276a272015-03-21 10:12:56 +00001393 }
Alexey Bataev78849fb2016-03-09 09:49:00 +00001394 if (auto *PostUpdate = C->getPostUpdateExpr())
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001395 EmitIgnoredExpr(PostUpdate);
Alexander Musman3276a272015-03-21 10:12:56 +00001396 }
Alexey Bataevef549a82016-03-09 09:49:09 +00001397 if (DoneBB)
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001398 EmitBlock(DoneBB, /*IsFinished=*/true);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001399}
1400
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001401static void emitAlignedClause(CodeGenFunction &CGF,
1402 const OMPExecutableDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001403 if (!CGF.HaveInsertPoint())
1404 return;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001405 for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001406 unsigned ClauseAlignment = 0;
1407 if (auto AlignmentExpr = Clause->getAlignment()) {
1408 auto AlignmentCI =
1409 cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr));
1410 ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue());
Alexander Musman09184fe2014-09-30 05:29:28 +00001411 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001412 for (auto E : Clause->varlists()) {
1413 unsigned Alignment = ClauseAlignment;
1414 if (Alignment == 0) {
1415 // OpenMP [2.8.1, Description]
1416 // If no optional parameter is specified, implementation-defined default
1417 // alignments for SIMD instructions on the target platforms are assumed.
1418 Alignment =
Alexey Bataev00396512015-07-02 03:40:19 +00001419 CGF.getContext()
1420 .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign(
1421 E->getType()->getPointeeType()))
1422 .getQuantity();
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001423 }
1424 assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) &&
1425 "alignment is not power of 2");
1426 if (Alignment != 0) {
1427 llvm::Value *PtrValue = CGF.EmitScalarExpr(E);
1428 CGF.EmitAlignmentAssumption(PtrValue, Alignment);
1429 }
Alexander Musman09184fe2014-09-30 05:29:28 +00001430 }
1431 }
1432}
1433
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001434void CodeGenFunction::EmitOMPPrivateLoopCounters(
1435 const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) {
1436 if (!HaveInsertPoint())
Alexey Bataev8ef31412015-12-18 07:58:25 +00001437 return;
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001438 auto I = S.private_counters().begin();
1439 for (auto *E : S.counters()) {
Alexey Bataeva8899172015-08-06 12:30:57 +00001440 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1441 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001442 (void)LoopScope.addPrivate(VD, [&]() -> Address {
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001443 // Emit var without initialization.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001444 if (!LocalDeclMap.count(PrivateVD)) {
1445 auto VarEmission = EmitAutoVarAlloca(*PrivateVD);
1446 EmitAutoVarCleanups(VarEmission);
1447 }
1448 DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD),
1449 /*RefersToEnclosingVariableOrCapture=*/false,
1450 (*I)->getType(), VK_LValue, (*I)->getExprLoc());
1451 return EmitLValue(&DRE).getAddress();
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001452 });
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001453 if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) ||
1454 VD->hasGlobalStorage()) {
1455 (void)LoopScope.addPrivate(PrivateVD, [&]() -> Address {
1456 DeclRefExpr DRE(const_cast<VarDecl *>(VD),
1457 LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD),
1458 E->getType(), VK_LValue, E->getExprLoc());
1459 return EmitLValue(&DRE).getAddress();
1460 });
1461 }
Alexey Bataeva8899172015-08-06 12:30:57 +00001462 ++I;
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001463 }
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001464}
1465
Alexey Bataev62dbb972015-04-22 11:59:37 +00001466static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S,
1467 const Expr *Cond, llvm::BasicBlock *TrueBlock,
1468 llvm::BasicBlock *FalseBlock, uint64_t TrueCount) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001469 if (!CGF.HaveInsertPoint())
1470 return;
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001471 {
1472 CodeGenFunction::OMPPrivateScope PreCondScope(CGF);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001473 CGF.EmitOMPPrivateLoopCounters(S, PreCondScope);
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001474 (void)PreCondScope.Privatize();
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001475 // Get initial values of real counters.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00001476 for (auto I : S.inits()) {
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001477 CGF.EmitIgnoredExpr(I);
1478 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00001479 }
1480 // Check that loop is executed at least one time.
1481 CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount);
1482}
1483
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001484void CodeGenFunction::EmitOMPLinearClause(
1485 const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) {
1486 if (!HaveInsertPoint())
Alexey Bataev8ef31412015-12-18 07:58:25 +00001487 return;
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001488 llvm::DenseSet<const VarDecl *> SIMDLCVs;
1489 if (isOpenMPSimdDirective(D.getDirectiveKind())) {
1490 auto *LoopDirective = cast<OMPLoopDirective>(&D);
1491 for (auto *C : LoopDirective->counters()) {
1492 SIMDLCVs.insert(
1493 cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl());
1494 }
1495 }
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001496 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001497 auto CurPrivate = C->privates().begin();
Alexey Bataevc925aa32015-04-27 08:00:32 +00001498 for (auto *E : C->varlists()) {
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001499 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1500 auto *PrivateVD =
1501 cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001502 if (!SIMDLCVs.count(VD->getCanonicalDecl())) {
1503 bool IsRegistered = PrivateScope.addPrivate(VD, [&]() -> Address {
1504 // Emit private VarDecl with copy init.
1505 EmitVarDecl(*PrivateVD);
1506 return GetAddrOfLocalVar(PrivateVD);
1507 });
1508 assert(IsRegistered && "linear var already registered as private");
1509 // Silence the warning about unused variable.
1510 (void)IsRegistered;
1511 } else
1512 EmitVarDecl(*PrivateVD);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001513 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00001514 }
1515 }
1516}
1517
Alexey Bataev45bfad52015-08-21 12:19:04 +00001518static void emitSimdlenSafelenClause(CodeGenFunction &CGF,
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001519 const OMPExecutableDirective &D,
1520 bool IsMonotonic) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001521 if (!CGF.HaveInsertPoint())
1522 return;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001523 if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) {
Alexey Bataev45bfad52015-08-21 12:19:04 +00001524 RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(),
1525 /*ignoreResult=*/true);
1526 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
1527 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
1528 // In presence of finite 'safelen', it may be unsafe to mark all
1529 // the memory instructions parallel, because loop-carried
1530 // dependences of 'safelen' iterations are possible.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001531 if (!IsMonotonic)
1532 CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>());
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001533 } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001534 RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(),
1535 /*ignoreResult=*/true);
1536 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
Tyler Nowickida46d0e2015-07-14 23:03:09 +00001537 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001538 // In presence of finite 'safelen', it may be unsafe to mark all
1539 // the memory instructions parallel, because loop-carried
1540 // dependences of 'safelen' iterations are possible.
1541 CGF.LoopStack.setParallel(false);
1542 }
1543}
1544
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001545void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D,
1546 bool IsMonotonic) {
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001547 // Walk clauses and process safelen/lastprivate.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001548 LoopStack.setParallel(!IsMonotonic);
Tyler Nowickida46d0e2015-07-14 23:03:09 +00001549 LoopStack.setVectorizeEnable(true);
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001550 emitSimdlenSafelenClause(*this, D, IsMonotonic);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001551}
1552
Alexey Bataevef549a82016-03-09 09:49:09 +00001553void CodeGenFunction::EmitOMPSimdFinal(
1554 const OMPLoopDirective &D,
1555 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001556 if (!HaveInsertPoint())
1557 return;
Alexey Bataevef549a82016-03-09 09:49:09 +00001558 llvm::BasicBlock *DoneBB = nullptr;
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001559 auto IC = D.counters().begin();
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001560 auto IPC = D.private_counters().begin();
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001561 for (auto F : D.finals()) {
1562 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001563 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl());
1564 auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD);
1565 if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) ||
1566 OrigVD->hasGlobalStorage() || CED) {
Alexey Bataevef549a82016-03-09 09:49:09 +00001567 if (!DoneBB) {
1568 if (auto *Cond = CondGen(*this)) {
1569 // If the first post-update expression is found, emit conditional
1570 // block if it was requested.
1571 auto *ThenBB = createBasicBlock(".omp.final.then");
1572 DoneBB = createBasicBlock(".omp.final.done");
1573 Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1574 EmitBlock(ThenBB);
1575 }
1576 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001577 Address OrigAddr = Address::invalid();
1578 if (CED)
1579 OrigAddr = EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress();
1580 else {
1581 DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD),
1582 /*RefersToEnclosingVariableOrCapture=*/false,
1583 (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc());
1584 OrigAddr = EmitLValue(&DRE).getAddress();
1585 }
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001586 OMPPrivateScope VarScope(*this);
1587 VarScope.addPrivate(OrigVD,
John McCall7f416cc2015-09-08 08:05:57 +00001588 [OrigAddr]() -> Address { return OrigAddr; });
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001589 (void)VarScope.Privatize();
1590 EmitIgnoredExpr(F);
1591 }
1592 ++IC;
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001593 ++IPC;
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001594 }
Alexey Bataevef549a82016-03-09 09:49:09 +00001595 if (DoneBB)
1596 EmitBlock(DoneBB, /*IsFinished=*/true);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001597}
1598
Alexander Musman515ad8c2014-05-22 08:54:05 +00001599void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001600 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00001601 OMPLoopScope PreInitScope(CGF, S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001602 // if (PreCond) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001603 // for (IV in 0..LastIteration) BODY;
1604 // <Final counter/linear vars updates>;
Alexey Bataev62dbb972015-04-22 11:59:37 +00001605 // }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001606 //
Alexander Musmana5f070a2014-10-01 06:03:56 +00001607
Alexey Bataev62dbb972015-04-22 11:59:37 +00001608 // Emit: if (PreCond) - begin.
1609 // If the condition constant folds and can be elided, avoid emitting the
1610 // whole loop.
1611 bool CondConstant;
1612 llvm::BasicBlock *ContBlock = nullptr;
1613 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
1614 if (!CondConstant)
1615 return;
1616 } else {
Alexey Bataev62dbb972015-04-22 11:59:37 +00001617 auto *ThenBlock = CGF.createBasicBlock("simd.if.then");
1618 ContBlock = CGF.createBasicBlock("simd.if.end");
Justin Bogner66242d62015-04-23 23:06:47 +00001619 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
1620 CGF.getProfileCount(&S));
Alexey Bataev62dbb972015-04-22 11:59:37 +00001621 CGF.EmitBlock(ThenBlock);
Justin Bogner66242d62015-04-23 23:06:47 +00001622 CGF.incrementProfileCounter(&S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001623 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001624
1625 // Emit the loop iteration variable.
1626 const Expr *IVExpr = S.getIterationVariable();
1627 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
1628 CGF.EmitVarDecl(*IVDecl);
1629 CGF.EmitIgnoredExpr(S.getInit());
1630
1631 // Emit the iterations count variable.
1632 // If it is not a variable, Sema decided to calculate iterations count on
Alexey Bataev7a228ff2015-05-21 07:59:51 +00001633 // each iteration (e.g., it is foldable into a constant).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001634 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
1635 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
1636 // Emit calculation of the iterations count.
1637 CGF.EmitIgnoredExpr(S.getCalcLastIteration());
Alexander Musmana5f070a2014-10-01 06:03:56 +00001638 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001639
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001640 CGF.EmitOMPSimdInit(S);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001641
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001642 emitAlignedClause(CGF, S);
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001643 CGF.EmitOMPLinearClauseInit(S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001644 {
1645 OMPPrivateScope LoopScope(CGF);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001646 CGF.EmitOMPPrivateLoopCounters(S, LoopScope);
1647 CGF.EmitOMPLinearClause(S, LoopScope);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001648 CGF.EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00001649 CGF.EmitOMPReductionClauseInit(S, LoopScope);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001650 bool HasLastprivateClause =
1651 CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001652 (void)LoopScope.Privatize();
Alexey Bataev0f34da12015-07-02 04:17:07 +00001653 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
1654 S.getInc(),
Alexey Bataev62dbb972015-04-22 11:59:37 +00001655 [&S](CodeGenFunction &CGF) {
Alexey Bataev0f34da12015-07-02 04:17:07 +00001656 CGF.EmitOMPLoopBody(S, JumpDest());
Alexey Bataev62dbb972015-04-22 11:59:37 +00001657 CGF.EmitStopPoint(&S);
1658 },
1659 [](CodeGenFunction &) {});
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001660 CGF.EmitOMPSimdFinal(
1661 S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataevfc087ec2015-06-16 13:14:42 +00001662 // Emit final copy of the lastprivate variables at the end of loops.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001663 if (HasLastprivateClause)
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001664 CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true);
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00001665 CGF.EmitOMPReductionClauseFinal(S);
Alexey Bataev61205072016-03-02 04:57:40 +00001666 emitPostUpdateForReductionClause(
1667 CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev62dbb972015-04-22 11:59:37 +00001668 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001669 CGF.EmitOMPLinearClauseFinal(
Alexey Bataevef549a82016-03-09 09:49:09 +00001670 S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev62dbb972015-04-22 11:59:37 +00001671 // Emit: if (PreCond) - end.
1672 if (ContBlock) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001673 CGF.EmitBranch(ContBlock);
1674 CGF.EmitBlock(ContBlock, true);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001675 }
1676 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00001677 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001678 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
Alexander Musman515ad8c2014-05-22 08:54:05 +00001679}
1680
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001681void CodeGenFunction::EmitOMPOuterLoop(bool DynamicOrOrdered, bool IsMonotonic,
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001682 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered,
1683 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) {
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001684 auto &RT = CGM.getOpenMPRuntime();
Alexander Musman92bdaab2015-03-12 13:37:50 +00001685
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001686 const Expr *IVExpr = S.getIterationVariable();
1687 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1688 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1689
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001690 auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end");
1691
1692 // Start the loop with a block that tests the condition.
1693 auto CondBlock = createBasicBlock("omp.dispatch.cond");
1694 EmitBlock(CondBlock);
Hal Finkelc07e19b2016-05-25 21:53:24 +00001695 LoopStack.push(CondBlock, Builder.getCurrentDebugLocation());
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001696
1697 llvm::Value *BoolCondVal = nullptr;
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001698 if (!DynamicOrOrdered) {
Alexander Musman92bdaab2015-03-12 13:37:50 +00001699 // UB = min(UB, GlobalUB)
1700 EmitIgnoredExpr(S.getEnsureUpperBound());
1701 // IV = LB
1702 EmitIgnoredExpr(S.getInit());
1703 // IV < UB
Alexey Bataevae05c292015-06-16 11:59:36 +00001704 BoolCondVal = EvaluateExprAsBool(S.getCond());
Alexander Musman92bdaab2015-03-12 13:37:50 +00001705 } else {
Alexey Bataev7292c292016-04-25 12:22:29 +00001706 BoolCondVal = RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned, IL,
1707 LB, UB, ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001708 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001709
1710 // If there are any cleanups between here and the loop-exit scope,
1711 // create a block to stage a loop exit along.
1712 auto ExitBlock = LoopExit.getBlock();
1713 if (LoopScope.requiresCleanups())
1714 ExitBlock = createBasicBlock("omp.dispatch.cleanup");
1715
1716 auto LoopBody = createBasicBlock("omp.dispatch.body");
1717 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
1718 if (ExitBlock != LoopExit.getBlock()) {
1719 EmitBlock(ExitBlock);
1720 EmitBranchThroughCleanup(LoopExit);
1721 }
1722 EmitBlock(LoopBody);
1723
Alexander Musman92bdaab2015-03-12 13:37:50 +00001724 // Emit "IV = LB" (in case of static schedule, we have already calculated new
1725 // LB for loop condition and emitted it above).
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001726 if (DynamicOrOrdered)
Alexander Musman92bdaab2015-03-12 13:37:50 +00001727 EmitIgnoredExpr(S.getInit());
1728
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001729 // Create a block for the increment.
1730 auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc");
1731 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1732
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001733 // Generate !llvm.loop.parallel metadata for loads and stores for loops
1734 // with dynamic/guided scheduling and without ordered clause.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001735 if (!isOpenMPSimdDirective(S.getDirectiveKind()))
1736 LoopStack.setParallel(!IsMonotonic);
1737 else
1738 EmitOMPSimdInit(S, IsMonotonic);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001739
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001740 SourceLocation Loc = S.getLocStart();
Alexey Bataev0f34da12015-07-02 04:17:07 +00001741 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(),
1742 [&S, LoopExit](CodeGenFunction &CGF) {
1743 CGF.EmitOMPLoopBody(S, LoopExit);
1744 CGF.EmitStopPoint(&S);
1745 },
1746 [Ordered, IVSize, IVSigned, Loc](CodeGenFunction &CGF) {
1747 if (Ordered) {
1748 CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(
1749 CGF, Loc, IVSize, IVSigned);
1750 }
1751 });
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001752
1753 EmitBlock(Continue.getBlock());
1754 BreakContinueStack.pop_back();
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001755 if (!DynamicOrOrdered) {
Alexander Musman92bdaab2015-03-12 13:37:50 +00001756 // Emit "LB = LB + Stride", "UB = UB + Stride".
1757 EmitIgnoredExpr(S.getNextLowerBound());
1758 EmitIgnoredExpr(S.getNextUpperBound());
1759 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001760
1761 EmitBranch(CondBlock);
1762 LoopStack.pop();
1763 // Emit the fall-through block.
1764 EmitBlock(LoopExit.getBlock());
1765
1766 // Tell the runtime we are done.
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001767 if (!DynamicOrOrdered)
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001768 RT.emitForStaticFinish(*this, S.getLocEnd());
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001769
1770}
1771
1772void CodeGenFunction::EmitOMPForOuterLoop(
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001773 const OpenMPScheduleTy &ScheduleKind, bool IsMonotonic,
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001774 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered,
1775 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) {
1776 auto &RT = CGM.getOpenMPRuntime();
1777
1778 // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime).
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001779 const bool DynamicOrOrdered =
1780 Ordered || RT.isDynamic(ScheduleKind.Schedule);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001781
1782 assert((Ordered ||
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001783 !RT.isStaticNonchunked(ScheduleKind.Schedule,
1784 /*Chunked=*/Chunk != nullptr)) &&
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001785 "static non-chunked schedule does not need outer loop");
1786
1787 // Emit outer loop.
1788 //
1789 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1790 // When schedule(dynamic,chunk_size) is specified, the iterations are
1791 // distributed to threads in the team in chunks as the threads request them.
1792 // Each thread executes a chunk of iterations, then requests another chunk,
1793 // until no chunks remain to be distributed. Each chunk contains chunk_size
1794 // iterations, except for the last chunk to be distributed, which may have
1795 // fewer iterations. When no chunk_size is specified, it defaults to 1.
1796 //
1797 // When schedule(guided,chunk_size) is specified, the iterations are assigned
1798 // to threads in the team in chunks as the executing threads request them.
1799 // Each thread executes a chunk of iterations, then requests another chunk,
1800 // until no chunks remain to be assigned. For a chunk_size of 1, the size of
1801 // each chunk is proportional to the number of unassigned iterations divided
1802 // by the number of threads in the team, decreasing to 1. For a chunk_size
1803 // with value k (greater than 1), the size of each chunk is determined in the
1804 // same way, with the restriction that the chunks do not contain fewer than k
1805 // iterations (except for the last chunk to be assigned, which may have fewer
1806 // than k iterations).
1807 //
1808 // When schedule(auto) is specified, the decision regarding scheduling is
1809 // delegated to the compiler and/or runtime system. The programmer gives the
1810 // implementation the freedom to choose any possible mapping of iterations to
1811 // threads in the team.
1812 //
1813 // When schedule(runtime) is specified, the decision regarding scheduling is
1814 // deferred until run time, and the schedule and chunk size are taken from the
1815 // run-sched-var ICV. If the ICV is set to auto, the schedule is
1816 // implementation defined
1817 //
1818 // while(__kmpc_dispatch_next(&LB, &UB)) {
1819 // idx = LB;
1820 // while (idx <= UB) { BODY; ++idx;
1821 // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only.
1822 // } // inner loop
1823 // }
1824 //
1825 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1826 // When schedule(static, chunk_size) is specified, iterations are divided into
1827 // chunks of size chunk_size, and the chunks are assigned to the threads in
1828 // the team in a round-robin fashion in the order of the thread number.
1829 //
1830 // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) {
1831 // while (idx <= UB) { BODY; ++idx; } // inner loop
1832 // LB = LB + ST;
1833 // UB = UB + ST;
1834 // }
1835 //
1836
1837 const Expr *IVExpr = S.getIterationVariable();
1838 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1839 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1840
1841 if (DynamicOrOrdered) {
1842 llvm::Value *UBVal = EmitScalarExpr(S.getLastIteration());
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001843 RT.emitForDispatchInit(*this, S.getLocStart(), ScheduleKind, IVSize,
1844 IVSigned, Ordered, UBVal, Chunk);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001845 } else {
1846 RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind, IVSize, IVSigned,
1847 Ordered, IL, LB, UB, ST, Chunk);
1848 }
1849
Carlo Bertolli0ff587d2016-03-07 16:19:13 +00001850 EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, Ordered, LB, UB,
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001851 ST, IL, Chunk);
1852}
1853
1854void CodeGenFunction::EmitOMPDistributeOuterLoop(
1855 OpenMPDistScheduleClauseKind ScheduleKind,
1856 const OMPDistributeDirective &S, OMPPrivateScope &LoopScope,
1857 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) {
1858
1859 auto &RT = CGM.getOpenMPRuntime();
1860
1861 // Emit outer loop.
1862 // Same behavior as a OMPForOuterLoop, except that schedule cannot be
1863 // dynamic
1864 //
1865
1866 const Expr *IVExpr = S.getIterationVariable();
1867 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1868 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1869
1870 RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind,
1871 IVSize, IVSigned, /* Ordered = */ false,
1872 IL, LB, UB, ST, Chunk);
1873
1874 EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false,
1875 S, LoopScope, /* Ordered = */ false, LB, UB, ST, IL, Chunk);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001876}
1877
Carlo Bertolli9925f152016-06-27 14:55:37 +00001878void CodeGenFunction::EmitOMPDistributeParallelForDirective(
1879 const OMPDistributeParallelForDirective &S) {
1880 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
1881 CGM.getOpenMPRuntime().emitInlinedDirective(
1882 *this, OMPD_distribute_parallel_for,
1883 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
1884 OMPLoopScope PreInitScope(CGF, S);
1885 CGF.EmitStmt(
1886 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1887 });
1888}
1889
Kelvin Li4a39add2016-07-05 05:00:15 +00001890void CodeGenFunction::EmitOMPDistributeParallelForSimdDirective(
1891 const OMPDistributeParallelForSimdDirective &S) {
1892 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
1893 CGM.getOpenMPRuntime().emitInlinedDirective(
1894 *this, OMPD_distribute_parallel_for_simd,
1895 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
1896 OMPLoopScope PreInitScope(CGF, S);
1897 CGF.EmitStmt(
1898 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1899 });
1900}
Kelvin Li787f3fc2016-07-06 04:45:38 +00001901
1902void CodeGenFunction::EmitOMPDistributeSimdDirective(
1903 const OMPDistributeSimdDirective &S) {
1904 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
1905 CGM.getOpenMPRuntime().emitInlinedDirective(
1906 *this, OMPD_distribute_simd,
1907 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
1908 OMPLoopScope PreInitScope(CGF, S);
1909 CGF.EmitStmt(
1910 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1911 });
1912}
1913
Kelvin Lia579b912016-07-14 02:54:56 +00001914void CodeGenFunction::EmitOMPTargetParallelForSimdDirective(
1915 const OMPTargetParallelForSimdDirective &S) {
1916 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
1917 CGM.getOpenMPRuntime().emitInlinedDirective(
1918 *this, OMPD_target_parallel_for_simd,
1919 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
1920 OMPLoopScope PreInitScope(CGF, S);
1921 CGF.EmitStmt(
1922 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1923 });
1924}
1925
Kelvin Li986330c2016-07-20 22:57:10 +00001926void CodeGenFunction::EmitOMPTargetSimdDirective(
1927 const OMPTargetSimdDirective &S) {
1928 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
1929 CGM.getOpenMPRuntime().emitInlinedDirective(
1930 *this, OMPD_target_simd, [&S](CodeGenFunction &CGF, PrePostActionTy &) {
1931 OMPLoopScope PreInitScope(CGF, S);
1932 CGF.EmitStmt(
1933 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1934 });
1935}
1936
Kelvin Li02532872016-08-05 14:37:37 +00001937void CodeGenFunction::EmitOMPTeamsDistributeDirective(
1938 const OMPTeamsDistributeDirective &S) {
1939 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
1940 CGM.getOpenMPRuntime().emitInlinedDirective(
1941 *this, OMPD_teams_distribute,
1942 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
1943 OMPLoopScope PreInitScope(CGF, S);
1944 CGF.EmitStmt(
1945 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1946 });
1947}
1948
Alexander Musmanc6388682014-12-15 07:07:06 +00001949/// \brief Emit a helper variable and return corresponding lvalue.
1950static LValue EmitOMPHelperVar(CodeGenFunction &CGF,
1951 const DeclRefExpr *Helper) {
1952 auto VDecl = cast<VarDecl>(Helper->getDecl());
1953 CGF.EmitVarDecl(*VDecl);
1954 return CGF.EmitLValue(Helper);
1955}
1956
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001957namespace {
1958 struct ScheduleKindModifiersTy {
1959 OpenMPScheduleClauseKind Kind;
1960 OpenMPScheduleClauseModifier M1;
1961 OpenMPScheduleClauseModifier M2;
1962 ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind,
1963 OpenMPScheduleClauseModifier M1,
1964 OpenMPScheduleClauseModifier M2)
1965 : Kind(Kind), M1(M1), M2(M2) {}
1966 };
1967} // namespace
1968
Alexey Bataev38e89532015-04-16 04:54:05 +00001969bool CodeGenFunction::EmitOMPWorksharingLoop(const OMPLoopDirective &S) {
Alexander Musmanc6388682014-12-15 07:07:06 +00001970 // Emit the loop iteration variable.
1971 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
1972 auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
1973 EmitVarDecl(*IVDecl);
1974
1975 // Emit the iterations count variable.
1976 // If it is not a variable, Sema decided to calculate iterations count on each
1977 // iteration (e.g., it is foldable into a constant).
1978 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
1979 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
1980 // Emit calculation of the iterations count.
1981 EmitIgnoredExpr(S.getCalcLastIteration());
1982 }
1983
1984 auto &RT = CGM.getOpenMPRuntime();
1985
Alexey Bataev38e89532015-04-16 04:54:05 +00001986 bool HasLastprivateClause;
Alexander Musmanc6388682014-12-15 07:07:06 +00001987 // Check pre-condition.
1988 {
Alexey Bataev5a3af132016-03-29 08:58:54 +00001989 OMPLoopScope PreInitScope(*this, S);
Alexander Musmanc6388682014-12-15 07:07:06 +00001990 // Skip the entire loop if we don't meet the precondition.
Alexey Bataev62dbb972015-04-22 11:59:37 +00001991 // If the condition constant folds and can be elided, avoid emitting the
1992 // whole loop.
1993 bool CondConstant;
1994 llvm::BasicBlock *ContBlock = nullptr;
1995 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
1996 if (!CondConstant)
1997 return false;
1998 } else {
Alexey Bataev62dbb972015-04-22 11:59:37 +00001999 auto *ThenBlock = createBasicBlock("omp.precond.then");
2000 ContBlock = createBasicBlock("omp.precond.end");
2001 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
Justin Bogner66242d62015-04-23 23:06:47 +00002002 getProfileCount(&S));
Alexey Bataev62dbb972015-04-22 11:59:37 +00002003 EmitBlock(ThenBlock);
Justin Bogner66242d62015-04-23 23:06:47 +00002004 incrementProfileCounter(&S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00002005 }
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00002006
Alexey Bataev8b427062016-05-25 12:36:08 +00002007 bool Ordered = false;
2008 if (auto *OrderedClause = S.getSingleClause<OMPOrderedClause>()) {
2009 if (OrderedClause->getNumForLoops())
2010 RT.emitDoacrossInit(*this, S);
2011 else
2012 Ordered = true;
2013 }
2014
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002015 llvm::DenseSet<const Expr *> EmittedFinals;
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00002016 emitAlignedClause(*this, S);
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00002017 EmitOMPLinearClauseInit(S);
Alexey Bataevef549a82016-03-09 09:49:09 +00002018 // Emit helper vars inits.
2019 LValue LB =
2020 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable()));
2021 LValue UB =
2022 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable()));
2023 LValue ST =
2024 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
2025 LValue IL =
2026 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
2027
Alexander Musmanc6388682014-12-15 07:07:06 +00002028 // Emit 'then' code.
2029 {
Alexander Musmanc6388682014-12-15 07:07:06 +00002030 OMPPrivateScope LoopScope(*this);
Alexey Bataev69c62a92015-04-15 04:52:20 +00002031 if (EmitOMPFirstprivateClause(S, LoopScope)) {
2032 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00002033 // initialization of firstprivate variables and post-update of
2034 // lastprivate variables.
Alexey Bataev25e5b442015-09-15 12:52:43 +00002035 CGM.getOpenMPRuntime().emitBarrierCall(
2036 *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
2037 /*ForceSimpleCall=*/true);
Alexey Bataev69c62a92015-04-15 04:52:20 +00002038 }
Alexey Bataev50a64582015-04-22 12:24:45 +00002039 EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev38e89532015-04-16 04:54:05 +00002040 HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev7ebe5fd2015-04-22 13:43:03 +00002041 EmitOMPReductionClauseInit(S, LoopScope);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002042 EmitOMPPrivateLoopCounters(S, LoopScope);
2043 EmitOMPLinearClause(S, LoopScope);
Alexander Musman7931b982015-03-16 07:14:41 +00002044 (void)LoopScope.Privatize();
Alexander Musmanc6388682014-12-15 07:07:06 +00002045
2046 // Detect the loop schedule kind and chunk.
Alexey Bataev3392d762016-02-16 11:18:12 +00002047 llvm::Value *Chunk = nullptr;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002048 OpenMPScheduleTy ScheduleKind;
Alexey Bataev3392d762016-02-16 11:18:12 +00002049 if (auto *C = S.getSingleClause<OMPScheduleClause>()) {
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002050 ScheduleKind.Schedule = C->getScheduleKind();
2051 ScheduleKind.M1 = C->getFirstScheduleModifier();
2052 ScheduleKind.M2 = C->getSecondScheduleModifier();
Alexey Bataev3392d762016-02-16 11:18:12 +00002053 if (const auto *Ch = C->getChunkSize()) {
2054 Chunk = EmitScalarExpr(Ch);
2055 Chunk = EmitScalarConversion(Chunk, Ch->getType(),
2056 S.getIterationVariable()->getType(),
2057 S.getLocStart());
2058 }
2059 }
Alexander Musmanc6388682014-12-15 07:07:06 +00002060 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
2061 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002062 // OpenMP 4.5, 2.7.1 Loop Construct, Description.
2063 // If the static schedule kind is specified or if the ordered clause is
2064 // specified, and if no monotonic modifier is specified, the effect will
2065 // be as if the monotonic modifier was specified.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002066 if (RT.isStaticNonchunked(ScheduleKind.Schedule,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002067 /* Chunked */ Chunk != nullptr) &&
2068 !Ordered) {
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002069 if (isOpenMPSimdDirective(S.getDirectiveKind()))
2070 EmitOMPSimdInit(S, /*IsMonotonic=*/true);
Alexander Musmanc6388682014-12-15 07:07:06 +00002071 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
2072 // When no chunk_size is specified, the iteration space is divided into
2073 // chunks that are approximately equal in size, and at most one chunk is
2074 // distributed to each thread. Note that the size of the chunks is
2075 // unspecified in this case.
John McCall7f416cc2015-09-08 08:05:57 +00002076 RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind,
2077 IVSize, IVSigned, Ordered,
2078 IL.getAddress(), LB.getAddress(),
2079 UB.getAddress(), ST.getAddress());
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002080 auto LoopExit =
2081 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
Alexander Musmanc6388682014-12-15 07:07:06 +00002082 // UB = min(UB, GlobalUB);
2083 EmitIgnoredExpr(S.getEnsureUpperBound());
2084 // IV = LB;
2085 EmitIgnoredExpr(S.getInit());
2086 // while (idx <= UB) { BODY; ++idx; }
Alexey Bataevae05c292015-06-16 11:59:36 +00002087 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
2088 S.getInc(),
Alexey Bataev0f34da12015-07-02 04:17:07 +00002089 [&S, LoopExit](CodeGenFunction &CGF) {
2090 CGF.EmitOMPLoopBody(S, LoopExit);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002091 CGF.EmitStopPoint(&S);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002092 },
2093 [](CodeGenFunction &) {});
Alexey Bataev0f34da12015-07-02 04:17:07 +00002094 EmitBlock(LoopExit.getBlock());
Alexander Musmanc6388682014-12-15 07:07:06 +00002095 // Tell the runtime we are done.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002096 RT.emitForStaticFinish(*this, S.getLocStart());
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002097 } else {
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002098 const bool IsMonotonic =
2099 Ordered || ScheduleKind.Schedule == OMPC_SCHEDULE_static ||
2100 ScheduleKind.Schedule == OMPC_SCHEDULE_unknown ||
2101 ScheduleKind.M1 == OMPC_SCHEDULE_MODIFIER_monotonic ||
2102 ScheduleKind.M2 == OMPC_SCHEDULE_MODIFIER_monotonic;
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002103 // Emit the outer loop, which requests its work chunk [LB..UB] from
2104 // runtime and runs the inner loop to process it.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002105 EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002106 LB.getAddress(), UB.getAddress(), ST.getAddress(),
2107 IL.getAddress(), Chunk);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002108 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002109 if (isOpenMPSimdDirective(S.getDirectiveKind())) {
2110 EmitOMPSimdFinal(S,
2111 [&](CodeGenFunction &CGF) -> llvm::Value * {
2112 return CGF.Builder.CreateIsNotNull(
2113 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2114 });
2115 }
Alexey Bataev7ebe5fd2015-04-22 13:43:03 +00002116 EmitOMPReductionClauseFinal(S);
Alexey Bataev61205072016-03-02 04:57:40 +00002117 // Emit post-update of the reduction variables if IsLastIter != 0.
2118 emitPostUpdateForReductionClause(
2119 *this, S, [&](CodeGenFunction &CGF) -> llvm::Value * {
2120 return CGF.Builder.CreateIsNotNull(
2121 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2122 });
Alexey Bataev38e89532015-04-16 04:54:05 +00002123 // Emit final copy of the lastprivate variables if IsLastIter != 0.
2124 if (HasLastprivateClause)
2125 EmitOMPLastprivateClauseFinal(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002126 S, isOpenMPSimdDirective(S.getDirectiveKind()),
2127 Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart())));
Alexander Musmanc6388682014-12-15 07:07:06 +00002128 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002129 EmitOMPLinearClauseFinal(S, [&](CodeGenFunction &CGF) -> llvm::Value * {
Alexey Bataevef549a82016-03-09 09:49:09 +00002130 return CGF.Builder.CreateIsNotNull(
2131 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2132 });
Alexander Musmanc6388682014-12-15 07:07:06 +00002133 // We're now done with the loop, so jump to the continuation block.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002134 if (ContBlock) {
2135 EmitBranch(ContBlock);
2136 EmitBlock(ContBlock, true);
2137 }
Alexander Musmanc6388682014-12-15 07:07:06 +00002138 }
Alexey Bataev38e89532015-04-16 04:54:05 +00002139 return HasLastprivateClause;
Alexander Musmanc6388682014-12-15 07:07:06 +00002140}
2141
2142void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) {
Alexey Bataev38e89532015-04-16 04:54:05 +00002143 bool HasLastprivates = false;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002144 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF,
2145 PrePostActionTy &) {
2146 HasLastprivates = CGF.EmitOMPWorksharingLoop(S);
2147 };
Alexey Bataev3392d762016-02-16 11:18:12 +00002148 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002149 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002150 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen,
2151 S.hasCancel());
2152 }
Alexander Musmanc6388682014-12-15 07:07:06 +00002153
2154 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002155 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) {
Alexey Bataevf2685682015-03-30 04:30:22 +00002156 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
2157 }
Alexey Bataevf29276e2014-06-18 04:14:57 +00002158}
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002159
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002160void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002161 bool HasLastprivates = false;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002162 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF,
2163 PrePostActionTy &) {
2164 HasLastprivates = CGF.EmitOMPWorksharingLoop(S);
2165 };
Alexey Bataev3392d762016-02-16 11:18:12 +00002166 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002167 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002168 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
2169 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002170
2171 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002172 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002173 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
2174 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00002175}
2176
Alexey Bataev2df54a02015-03-12 08:53:29 +00002177static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty,
2178 const Twine &Name,
2179 llvm::Value *Init = nullptr) {
John McCall7f416cc2015-09-08 08:05:57 +00002180 auto LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty);
Alexey Bataev2df54a02015-03-12 08:53:29 +00002181 if (Init)
2182 CGF.EmitScalarInit(Init, LVal);
2183 return LVal;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002184}
2185
Alexey Bataev3392d762016-02-16 11:18:12 +00002186void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) {
Alexey Bataev2df54a02015-03-12 08:53:29 +00002187 auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt();
2188 auto *CS = dyn_cast<CompoundStmt>(Stmt);
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002189 bool HasLastprivates = false;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002190 auto &&CodeGen = [&S, Stmt, CS, &HasLastprivates](CodeGenFunction &CGF,
2191 PrePostActionTy &) {
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002192 auto &C = CGF.CGM.getContext();
2193 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
2194 // Emit helper vars inits.
2195 LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.",
2196 CGF.Builder.getInt32(0));
2197 auto *GlobalUBVal = CS != nullptr ? CGF.Builder.getInt32(CS->size() - 1)
2198 : CGF.Builder.getInt32(0);
2199 LValue UB =
2200 createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal);
2201 LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.",
2202 CGF.Builder.getInt32(1));
2203 LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.",
2204 CGF.Builder.getInt32(0));
2205 // Loop counter.
2206 LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv.");
2207 OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
2208 CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV);
2209 OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
2210 CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB);
2211 // Generate condition for loop.
2212 BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue,
2213 OK_Ordinary, S.getLocStart(),
2214 /*fpContractable=*/false);
2215 // Increment for loop counter.
2216 UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary,
2217 S.getLocStart());
2218 auto BodyGen = [Stmt, CS, &S, &IV](CodeGenFunction &CGF) {
2219 // Iterate through all sections and emit a switch construct:
2220 // switch (IV) {
2221 // case 0:
2222 // <SectionStmt[0]>;
2223 // break;
2224 // ...
2225 // case <NumSection> - 1:
2226 // <SectionStmt[<NumSection> - 1]>;
2227 // break;
2228 // }
2229 // .omp.sections.exit:
2230 auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit");
2231 auto *SwitchStmt = CGF.Builder.CreateSwitch(
2232 CGF.EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB,
2233 CS == nullptr ? 1 : CS->size());
2234 if (CS) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002235 unsigned CaseNumber = 0;
Benjamin Kramer642f1732015-07-02 21:03:14 +00002236 for (auto *SubStmt : CS->children()) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002237 auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
2238 CGF.EmitBlock(CaseBB);
2239 SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB);
Benjamin Kramer642f1732015-07-02 21:03:14 +00002240 CGF.EmitStmt(SubStmt);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002241 CGF.EmitBranch(ExitBB);
Benjamin Kramer642f1732015-07-02 21:03:14 +00002242 ++CaseNumber;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002243 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002244 } else {
2245 auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
2246 CGF.EmitBlock(CaseBB);
2247 SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB);
2248 CGF.EmitStmt(Stmt);
2249 CGF.EmitBranch(ExitBB);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002250 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002251 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
Alexey Bataev2df54a02015-03-12 08:53:29 +00002252 };
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002253
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002254 CodeGenFunction::OMPPrivateScope LoopScope(CGF);
2255 if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) {
Alexey Bataev9efc03b2015-04-27 04:34:03 +00002256 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00002257 // initialization of firstprivate variables and post-update of lastprivate
2258 // variables.
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002259 CGF.CGM.getOpenMPRuntime().emitBarrierCall(
2260 CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
2261 /*ForceSimpleCall=*/true);
Alexey Bataev9efc03b2015-04-27 04:34:03 +00002262 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002263 CGF.EmitOMPPrivateClause(S, LoopScope);
2264 HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
2265 CGF.EmitOMPReductionClauseInit(S, LoopScope);
2266 (void)LoopScope.Privatize();
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002267
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002268 // Emit static non-chunked loop.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002269 OpenMPScheduleTy ScheduleKind;
2270 ScheduleKind.Schedule = OMPC_SCHEDULE_static;
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002271 CGF.CGM.getOpenMPRuntime().emitForStaticInit(
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002272 CGF, S.getLocStart(), ScheduleKind, /*IVSize=*/32,
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002273 /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(), LB.getAddress(),
2274 UB.getAddress(), ST.getAddress());
2275 // UB = min(UB, GlobalUB);
2276 auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart());
2277 auto *MinUBGlobalUB = CGF.Builder.CreateSelect(
2278 CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal);
2279 CGF.EmitStoreOfScalar(MinUBGlobalUB, UB);
2280 // IV = LB;
2281 CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV);
2282 // while (idx <= UB) { BODY; ++idx; }
2283 CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen,
2284 [](CodeGenFunction &) {});
2285 // Tell the runtime we are done.
2286 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocStart());
2287 CGF.EmitOMPReductionClauseFinal(S);
Alexey Bataev61205072016-03-02 04:57:40 +00002288 // Emit post-update of the reduction variables if IsLastIter != 0.
2289 emitPostUpdateForReductionClause(
2290 CGF, S, [&](CodeGenFunction &CGF) -> llvm::Value * {
2291 return CGF.Builder.CreateIsNotNull(
2292 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2293 });
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002294
2295 // Emit final copy of the lastprivate variables if IsLastIter != 0.
2296 if (HasLastprivates)
2297 CGF.EmitOMPLastprivateClauseFinal(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002298 S, /*NoFinals=*/false,
2299 CGF.Builder.CreateIsNotNull(
2300 CGF.EmitLoadOfScalar(IL, S.getLocStart())));
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002301 };
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002302
2303 bool HasCancel = false;
2304 if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S))
2305 HasCancel = OSD->hasCancel();
2306 else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S))
2307 HasCancel = OPSD->hasCancel();
2308 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen,
2309 HasCancel);
2310 // Emit barrier for lastprivates only if 'sections' directive has 'nowait'
2311 // clause. Otherwise the barrier will be generated by the codegen for the
2312 // directive.
2313 if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) {
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002314 // Emit implicit barrier to synchronize threads and avoid data races on
2315 // initialization of firstprivate variables.
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002316 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
2317 OMPD_unknown);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002318 }
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002319}
Alexey Bataev2df54a02015-03-12 08:53:29 +00002320
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002321void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
Alexey Bataev3392d762016-02-16 11:18:12 +00002322 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002323 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002324 EmitSections(S);
2325 }
Alexey Bataev2df54a02015-03-12 08:53:29 +00002326 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002327 if (!S.getSingleClause<OMPNowaitClause>()) {
Alexey Bataev3392d762016-02-16 11:18:12 +00002328 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
2329 OMPD_sections);
Alexey Bataevf2685682015-03-30 04:30:22 +00002330 }
Alexey Bataev2df54a02015-03-12 08:53:29 +00002331}
2332
2333void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002334 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002335 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002336 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002337 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev25e5b442015-09-15 12:52:43 +00002338 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen,
2339 S.hasCancel());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002340}
2341
Alexey Bataev6956e2e2015-02-05 06:35:41 +00002342void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00002343 llvm::SmallVector<const Expr *, 8> CopyprivateVars;
Alexey Bataev420d45b2015-04-14 05:11:24 +00002344 llvm::SmallVector<const Expr *, 8> DestExprs;
Alexey Bataeva63048e2015-03-23 06:18:07 +00002345 llvm::SmallVector<const Expr *, 8> SrcExprs;
Alexey Bataeva63048e2015-03-23 06:18:07 +00002346 llvm::SmallVector<const Expr *, 8> AssignmentOps;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002347 // Check if there are any 'copyprivate' clauses associated with this
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00002348 // 'single' construct.
Alexey Bataeva63048e2015-03-23 06:18:07 +00002349 // Build a list of copyprivate variables along with helper expressions
2350 // (<source>, <destination>, <destination>=<source> expressions)
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002351 for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00002352 CopyprivateVars.append(C->varlists().begin(), C->varlists().end());
Alexey Bataev420d45b2015-04-14 05:11:24 +00002353 DestExprs.append(C->destination_exprs().begin(),
2354 C->destination_exprs().end());
Alexey Bataeva63048e2015-03-23 06:18:07 +00002355 SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end());
Alexey Bataeva63048e2015-03-23 06:18:07 +00002356 AssignmentOps.append(C->assignment_ops().begin(),
2357 C->assignment_ops().end());
2358 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002359 // Emit code for 'single' region along with 'copyprivate' clauses
2360 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2361 Action.Enter(CGF);
2362 OMPPrivateScope SingleScope(CGF);
2363 (void)CGF.EmitOMPFirstprivateClause(S, SingleScope);
2364 CGF.EmitOMPPrivateClause(S, SingleScope);
2365 (void)SingleScope.Privatize();
2366 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2367 };
Alexey Bataev3392d762016-02-16 11:18:12 +00002368 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002369 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002370 CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(),
2371 CopyprivateVars, DestExprs,
2372 SrcExprs, AssignmentOps);
2373 }
2374 // Emit an implicit barrier at the end (to avoid data race on firstprivate
2375 // init or if no 'nowait' clause was specified and no 'copyprivate' clause).
Alexey Bataev417089f2016-02-17 13:19:37 +00002376 if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) {
Alexey Bataev5521d782015-04-24 04:21:15 +00002377 CGM.getOpenMPRuntime().emitBarrierCall(
2378 *this, S.getLocStart(),
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002379 S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single);
Alexey Bataevf2685682015-03-30 04:30:22 +00002380 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002381}
2382
Alexey Bataev8d690652014-12-04 07:23:53 +00002383void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002384 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2385 Action.Enter(CGF);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002386 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002387 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002388 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002389 CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart());
Alexander Musman80c22892014-07-17 08:54:58 +00002390}
2391
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002392void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002393 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2394 Action.Enter(CGF);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002395 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002396 };
Alexey Bataevfc57d162015-12-15 10:55:09 +00002397 Expr *Hint = nullptr;
2398 if (auto *HintClause = S.getSingleClause<OMPHintClause>())
2399 Hint = HintClause->getHint();
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002400 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataevfc57d162015-12-15 10:55:09 +00002401 CGM.getOpenMPRuntime().emitCriticalRegion(*this,
2402 S.getDirectiveName().getAsString(),
2403 CodeGen, S.getLocStart(), Hint);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002404}
2405
Alexey Bataev671605e2015-04-13 05:28:11 +00002406void CodeGenFunction::EmitOMPParallelForDirective(
2407 const OMPParallelForDirective &S) {
2408 // Emit directive as a combined directive that consists of two implicit
2409 // directives: 'parallel' with 'for' directive.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002410 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev671605e2015-04-13 05:28:11 +00002411 CGF.EmitOMPWorksharingLoop(S);
Alexey Bataev671605e2015-04-13 05:28:11 +00002412 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002413 emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002414}
2415
Alexander Musmane4e893b2014-09-23 09:33:00 +00002416void CodeGenFunction::EmitOMPParallelForSimdDirective(
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00002417 const OMPParallelForSimdDirective &S) {
2418 // Emit directive as a combined directive that consists of two implicit
2419 // directives: 'parallel' with 'for' directive.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002420 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00002421 CGF.EmitOMPWorksharingLoop(S);
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00002422 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002423 emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002424}
2425
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002426void CodeGenFunction::EmitOMPParallelSectionsDirective(
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002427 const OMPParallelSectionsDirective &S) {
2428 // Emit directive as a combined directive that consists of two implicit
2429 // directives: 'parallel' with 'sections' directive.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002430 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2431 CGF.EmitSections(S);
2432 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002433 emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002434}
2435
Alexey Bataev7292c292016-04-25 12:22:29 +00002436void CodeGenFunction::EmitOMPTaskBasedDirective(const OMPExecutableDirective &S,
2437 const RegionCodeGenTy &BodyGen,
2438 const TaskGenTy &TaskGen,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002439 OMPTaskDataTy &Data) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002440 // Emit outlined function for task construct.
2441 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
Alexey Bataev62b63b12015-03-10 07:28:44 +00002442 auto *I = CS->getCapturedDecl()->param_begin();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002443 auto *PartId = std::next(I);
Alexey Bataev48591dd2016-04-20 04:01:36 +00002444 auto *TaskT = std::next(I, 4);
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002445 // Check if the task is final
2446 if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) {
2447 // If the condition constant folds and can be elided, try to avoid emitting
2448 // the condition and the dead arm of the if/else.
2449 auto *Cond = Clause->getCondition();
2450 bool CondConstant;
2451 if (ConstantFoldsToSimpleInteger(Cond, CondConstant))
2452 Data.Final.setInt(CondConstant);
2453 else
2454 Data.Final.setPointer(EvaluateExprAsBool(Cond));
2455 } else {
2456 // By default the task is not final.
2457 Data.Final.setInt(/*IntVal=*/false);
2458 }
Alexey Bataev1e1e2862016-05-10 12:21:02 +00002459 // Check if the task has 'priority' clause.
2460 if (const auto *Clause = S.getSingleClause<OMPPriorityClause>()) {
Alexey Bataev1e1e2862016-05-10 12:21:02 +00002461 auto *Prio = Clause->getPriority();
Alexey Bataev5140e742016-07-19 04:21:09 +00002462 Data.Priority.setInt(/*IntVal=*/true);
Alexey Bataevad537bb2016-05-30 09:06:50 +00002463 Data.Priority.setPointer(EmitScalarConversion(
2464 EmitScalarExpr(Prio), Prio->getType(),
2465 getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1),
2466 Prio->getExprLoc()));
Alexey Bataev1e1e2862016-05-10 12:21:02 +00002467 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00002468 // The first function argument for tasks is a thread id, the second one is a
2469 // part id (0 for tied tasks, >=0 for untied task).
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002470 llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
2471 // Get list of private variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002472 for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002473 auto IRef = C->varlist_begin();
2474 for (auto *IInit : C->private_copies()) {
2475 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2476 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
Alexey Bataev7292c292016-04-25 12:22:29 +00002477 Data.PrivateVars.push_back(*IRef);
2478 Data.PrivateCopies.push_back(IInit);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002479 }
2480 ++IRef;
2481 }
2482 }
2483 EmittedAsPrivate.clear();
2484 // Get list of firstprivate variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002485 for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002486 auto IRef = C->varlist_begin();
2487 auto IElemInitRef = C->inits().begin();
2488 for (auto *IInit : C->private_copies()) {
2489 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2490 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
Alexey Bataev7292c292016-04-25 12:22:29 +00002491 Data.FirstprivateVars.push_back(*IRef);
2492 Data.FirstprivateCopies.push_back(IInit);
2493 Data.FirstprivateInits.push_back(*IElemInitRef);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002494 }
Richard Trieucc3949d2016-02-18 22:34:54 +00002495 ++IRef;
2496 ++IElemInitRef;
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002497 }
2498 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00002499 // Get list of lastprivate variables (for taskloops).
2500 llvm::DenseMap<const VarDecl *, const DeclRefExpr *> LastprivateDstsOrigs;
2501 for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) {
2502 auto IRef = C->varlist_begin();
2503 auto ID = C->destination_exprs().begin();
2504 for (auto *IInit : C->private_copies()) {
2505 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2506 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
2507 Data.LastprivateVars.push_back(*IRef);
2508 Data.LastprivateCopies.push_back(IInit);
2509 }
2510 LastprivateDstsOrigs.insert(
2511 {cast<VarDecl>(cast<DeclRefExpr>(*ID)->getDecl()),
2512 cast<DeclRefExpr>(*IRef)});
2513 ++IRef;
2514 ++ID;
2515 }
2516 }
Alexey Bataev1d2353d2015-06-24 11:01:36 +00002517 // Build list of dependences.
Alexey Bataev7292c292016-04-25 12:22:29 +00002518 for (const auto *C : S.getClausesOfKind<OMPDependClause>())
2519 for (auto *IRef : C->varlists())
2520 Data.Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef));
Alexey Bataevf93095a2016-05-05 08:46:22 +00002521 auto &&CodeGen = [PartId, &S, &Data, CS, &BodyGen, &LastprivateDstsOrigs](
2522 CodeGenFunction &CGF, PrePostActionTy &Action) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002523 // Set proper addresses for generated private copies.
Alexey Bataev7292c292016-04-25 12:22:29 +00002524 OMPPrivateScope Scope(CGF);
Alexey Bataevf93095a2016-05-05 08:46:22 +00002525 if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty() ||
2526 !Data.LastprivateVars.empty()) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00002527 auto *CopyFn = CGF.Builder.CreateLoad(
2528 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3)));
2529 auto *PrivatesPtr = CGF.Builder.CreateLoad(
2530 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2)));
2531 // Map privates.
2532 llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs;
2533 llvm::SmallVector<llvm::Value *, 16> CallArgs;
2534 CallArgs.push_back(PrivatesPtr);
Alexey Bataev7292c292016-04-25 12:22:29 +00002535 for (auto *E : Data.PrivateVars) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00002536 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2537 Address PrivatePtr = CGF.CreateMemTemp(
2538 CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr");
2539 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
2540 CallArgs.push_back(PrivatePtr.getPointer());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002541 }
Alexey Bataev7292c292016-04-25 12:22:29 +00002542 for (auto *E : Data.FirstprivateVars) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00002543 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2544 Address PrivatePtr =
2545 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()),
2546 ".firstpriv.ptr.addr");
2547 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
2548 CallArgs.push_back(PrivatePtr.getPointer());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002549 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00002550 for (auto *E : Data.LastprivateVars) {
2551 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2552 Address PrivatePtr =
2553 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()),
2554 ".lastpriv.ptr.addr");
2555 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
2556 CallArgs.push_back(PrivatePtr.getPointer());
2557 }
Alexey Bataev48591dd2016-04-20 04:01:36 +00002558 CGF.EmitRuntimeCall(CopyFn, CallArgs);
Alexey Bataevf93095a2016-05-05 08:46:22 +00002559 for (auto &&Pair : LastprivateDstsOrigs) {
2560 auto *OrigVD = cast<VarDecl>(Pair.second->getDecl());
2561 DeclRefExpr DRE(
2562 const_cast<VarDecl *>(OrigVD),
2563 /*RefersToEnclosingVariableOrCapture=*/CGF.CapturedStmtInfo->lookup(
2564 OrigVD) != nullptr,
2565 Pair.second->getType(), VK_LValue, Pair.second->getExprLoc());
2566 Scope.addPrivate(Pair.first, [&CGF, &DRE]() {
2567 return CGF.EmitLValue(&DRE).getAddress();
2568 });
2569 }
Alexey Bataev48591dd2016-04-20 04:01:36 +00002570 for (auto &&Pair : PrivatePtrs) {
2571 Address Replacement(CGF.Builder.CreateLoad(Pair.second),
2572 CGF.getContext().getDeclAlign(Pair.first));
2573 Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; });
2574 }
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002575 }
Alexey Bataev48591dd2016-04-20 04:01:36 +00002576 (void)Scope.Privatize();
2577
2578 Action.Enter(CGF);
Alexey Bataev7292c292016-04-25 12:22:29 +00002579 BodyGen(CGF);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002580 };
Alexey Bataev7292c292016-04-25 12:22:29 +00002581 auto *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction(
2582 S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied,
2583 Data.NumberOfParts);
2584 OMPLexicalScope Scope(*this, S);
2585 TaskGen(*this, OutlinedFn, Data);
2586}
2587
2588void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) {
2589 // Emit outlined function for task construct.
2590 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
2591 auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002592 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
Alexey Bataev1d677132015-04-22 13:57:31 +00002593 const Expr *IfCond = nullptr;
Alexey Bataev7371aa32015-09-03 08:45:56 +00002594 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
2595 if (C->getNameModifier() == OMPD_unknown ||
2596 C->getNameModifier() == OMPD_task) {
2597 IfCond = C->getCondition();
2598 break;
2599 }
Alexey Bataev1d677132015-04-22 13:57:31 +00002600 }
Alexey Bataev7292c292016-04-25 12:22:29 +00002601
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002602 OMPTaskDataTy Data;
2603 // Check if we should emit tied or untied task.
2604 Data.Tied = !S.getSingleClause<OMPUntiedClause>();
Alexey Bataev7292c292016-04-25 12:22:29 +00002605 auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) {
2606 CGF.EmitStmt(CS->getCapturedStmt());
2607 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002608 auto &&TaskGen = [&S, SharedsTy, CapturedStruct,
Alexey Bataev7292c292016-04-25 12:22:29 +00002609 IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002610 const OMPTaskDataTy &Data) {
2611 CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getLocStart(), S, OutlinedFn,
2612 SharedsTy, CapturedStruct, IfCond,
2613 Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00002614 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002615 EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002616}
2617
Alexey Bataev9f797f32015-02-05 05:57:51 +00002618void CodeGenFunction::EmitOMPTaskyieldDirective(
2619 const OMPTaskyieldDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002620 CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart());
Alexey Bataev68446b72014-07-18 07:47:19 +00002621}
2622
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00002623void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) {
Alexey Bataevf2685682015-03-30 04:30:22 +00002624 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier);
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002625}
2626
Alexey Bataev8b8e2022015-04-27 05:22:09 +00002627void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) {
2628 CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart());
Alexey Bataev2df347a2014-07-18 10:17:07 +00002629}
2630
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002631void CodeGenFunction::EmitOMPTaskgroupDirective(
2632 const OMPTaskgroupDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002633 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2634 Action.Enter(CGF);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002635 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002636 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002637 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002638 CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getLocStart());
2639}
2640
Alexey Bataevcc37cc12014-11-20 04:34:54 +00002641void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002642 CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002643 if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002644 return llvm::makeArrayRef(FlushClause->varlist_begin(),
2645 FlushClause->varlist_end());
2646 }
2647 return llvm::None;
2648 }(), S.getLocStart());
Alexey Bataev6125da92014-07-21 11:26:11 +00002649}
2650
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002651void CodeGenFunction::EmitOMPDistributeLoop(const OMPDistributeDirective &S) {
2652 // Emit the loop iteration variable.
2653 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
2654 auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
2655 EmitVarDecl(*IVDecl);
2656
2657 // Emit the iterations count variable.
2658 // If it is not a variable, Sema decided to calculate iterations count on each
2659 // iteration (e.g., it is foldable into a constant).
2660 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
2661 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
2662 // Emit calculation of the iterations count.
2663 EmitIgnoredExpr(S.getCalcLastIteration());
2664 }
2665
2666 auto &RT = CGM.getOpenMPRuntime();
2667
2668 // Check pre-condition.
2669 {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002670 OMPLoopScope PreInitScope(*this, S);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002671 // Skip the entire loop if we don't meet the precondition.
2672 // If the condition constant folds and can be elided, avoid emitting the
2673 // whole loop.
2674 bool CondConstant;
2675 llvm::BasicBlock *ContBlock = nullptr;
2676 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
2677 if (!CondConstant)
2678 return;
2679 } else {
2680 auto *ThenBlock = createBasicBlock("omp.precond.then");
2681 ContBlock = createBasicBlock("omp.precond.end");
2682 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
2683 getProfileCount(&S));
2684 EmitBlock(ThenBlock);
2685 incrementProfileCounter(&S);
2686 }
2687
2688 // Emit 'then' code.
2689 {
2690 // Emit helper vars inits.
2691 LValue LB =
2692 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable()));
2693 LValue UB =
2694 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable()));
2695 LValue ST =
2696 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
2697 LValue IL =
2698 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
2699
2700 OMPPrivateScope LoopScope(*this);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002701 EmitOMPPrivateLoopCounters(S, LoopScope);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002702 (void)LoopScope.Privatize();
2703
2704 // Detect the distribute schedule kind and chunk.
2705 llvm::Value *Chunk = nullptr;
2706 OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown;
2707 if (auto *C = S.getSingleClause<OMPDistScheduleClause>()) {
2708 ScheduleKind = C->getDistScheduleKind();
2709 if (const auto *Ch = C->getChunkSize()) {
2710 Chunk = EmitScalarExpr(Ch);
2711 Chunk = EmitScalarConversion(Chunk, Ch->getType(),
2712 S.getIterationVariable()->getType(),
2713 S.getLocStart());
2714 }
2715 }
2716 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
2717 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
2718
2719 // OpenMP [2.10.8, distribute Construct, Description]
2720 // If dist_schedule is specified, kind must be static. If specified,
2721 // iterations are divided into chunks of size chunk_size, chunks are
2722 // assigned to the teams of the league in a round-robin fashion in the
2723 // order of the team number. When no chunk_size is specified, the
2724 // iteration space is divided into chunks that are approximately equal
2725 // in size, and at most one chunk is distributed to each team of the
2726 // league. The size of the chunks is unspecified in this case.
2727 if (RT.isStaticNonchunked(ScheduleKind,
2728 /* Chunked */ Chunk != nullptr)) {
2729 RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind,
2730 IVSize, IVSigned, /* Ordered = */ false,
2731 IL.getAddress(), LB.getAddress(),
2732 UB.getAddress(), ST.getAddress());
2733 auto LoopExit =
2734 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
2735 // UB = min(UB, GlobalUB);
2736 EmitIgnoredExpr(S.getEnsureUpperBound());
2737 // IV = LB;
2738 EmitIgnoredExpr(S.getInit());
2739 // while (idx <= UB) { BODY; ++idx; }
2740 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
2741 S.getInc(),
2742 [&S, LoopExit](CodeGenFunction &CGF) {
2743 CGF.EmitOMPLoopBody(S, LoopExit);
2744 CGF.EmitStopPoint(&S);
2745 },
2746 [](CodeGenFunction &) {});
2747 EmitBlock(LoopExit.getBlock());
2748 // Tell the runtime we are done.
2749 RT.emitForStaticFinish(*this, S.getLocStart());
2750 } else {
2751 // Emit the outer loop, which requests its work chunk [LB..UB] from
2752 // runtime and runs the inner loop to process it.
2753 EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope,
2754 LB.getAddress(), UB.getAddress(), ST.getAddress(),
2755 IL.getAddress(), Chunk);
2756 }
2757 }
2758
2759 // We're now done with the loop, so jump to the continuation block.
2760 if (ContBlock) {
2761 EmitBranch(ContBlock);
2762 EmitBlock(ContBlock, true);
2763 }
2764 }
2765}
2766
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002767void CodeGenFunction::EmitOMPDistributeDirective(
2768 const OMPDistributeDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002769 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002770 CGF.EmitOMPDistributeLoop(S);
2771 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002772 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002773 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen,
2774 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002775}
2776
Alexey Bataev5f600d62015-09-29 03:48:57 +00002777static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM,
2778 const CapturedStmt *S) {
2779 CodeGenFunction CGF(CGM, /*suppressNewContext=*/true);
2780 CodeGenFunction::CGCapturedStmtInfo CapStmtInfo;
2781 CGF.CapturedStmtInfo = &CapStmtInfo;
2782 auto *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S);
2783 Fn->addFnAttr(llvm::Attribute::NoInline);
2784 return Fn;
2785}
2786
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002787void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) {
Alexey Bataev8b427062016-05-25 12:36:08 +00002788 if (!S.getAssociatedStmt()) {
2789 for (const auto *DC : S.getClausesOfKind<OMPDependClause>())
2790 CGM.getOpenMPRuntime().emitDoacrossOrdered(*this, DC);
Alexey Bataev8ef31412015-12-18 07:58:25 +00002791 return;
Alexey Bataev8b427062016-05-25 12:36:08 +00002792 }
Alexey Bataev5f600d62015-09-29 03:48:57 +00002793 auto *C = S.getSingleClause<OMPSIMDClause>();
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002794 auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF,
2795 PrePostActionTy &Action) {
Alexey Bataev5f600d62015-09-29 03:48:57 +00002796 if (C) {
2797 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
2798 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
2799 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
2800 auto *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS);
2801 CGF.EmitNounwindRuntimeCall(OutlinedFn, CapturedVars);
2802 } else {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002803 Action.Enter(CGF);
Alexey Bataev5f600d62015-09-29 03:48:57 +00002804 CGF.EmitStmt(
2805 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2806 }
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002807 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002808 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev5f600d62015-09-29 03:48:57 +00002809 CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart(), !C);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002810}
2811
Alexey Bataevb57056f2015-01-22 06:17:56 +00002812static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002813 QualType SrcType, QualType DestType,
2814 SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00002815 assert(CGF.hasScalarEvaluationKind(DestType) &&
2816 "DestType must have scalar evaluation kind.");
2817 assert(!Val.isAggregate() && "Must be a scalar or complex.");
2818 return Val.isScalar()
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002819 ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType,
2820 Loc)
Alexey Bataevb57056f2015-01-22 06:17:56 +00002821 : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002822 DestType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00002823}
2824
2825static CodeGenFunction::ComplexPairTy
2826convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002827 QualType DestType, SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00002828 assert(CGF.getEvaluationKind(DestType) == TEK_Complex &&
2829 "DestType must have complex evaluation kind.");
2830 CodeGenFunction::ComplexPairTy ComplexVal;
2831 if (Val.isScalar()) {
2832 // Convert the input element to the element type of the complex.
2833 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002834 auto ScalarVal = CGF.EmitScalarConversion(Val.getScalarVal(), SrcType,
2835 DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00002836 ComplexVal = CodeGenFunction::ComplexPairTy(
2837 ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType()));
2838 } else {
2839 assert(Val.isComplex() && "Must be a scalar or complex.");
2840 auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType();
2841 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
2842 ComplexVal.first = CGF.EmitScalarConversion(
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002843 Val.getComplexVal().first, SrcElementType, DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00002844 ComplexVal.second = CGF.EmitScalarConversion(
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002845 Val.getComplexVal().second, SrcElementType, DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00002846 }
2847 return ComplexVal;
2848}
2849
Alexey Bataev5e018f92015-04-23 06:35:10 +00002850static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst,
2851 LValue LVal, RValue RVal) {
2852 if (LVal.isGlobalReg()) {
2853 CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal);
2854 } else {
JF Bastien92f4ef12016-04-06 17:26:42 +00002855 CGF.EmitAtomicStore(RVal, LVal,
2856 IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
2857 : llvm::AtomicOrdering::Monotonic,
Alexey Bataev5e018f92015-04-23 06:35:10 +00002858 LVal.isVolatile(), /*IsInit=*/false);
2859 }
2860}
2861
Alexey Bataev8524d152016-01-21 12:35:58 +00002862void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal,
2863 QualType RValTy, SourceLocation Loc) {
2864 switch (getEvaluationKind(LVal.getType())) {
Alexey Bataev5e018f92015-04-23 06:35:10 +00002865 case TEK_Scalar:
Alexey Bataev8524d152016-01-21 12:35:58 +00002866 EmitStoreThroughLValue(RValue::get(convertToScalarValue(
2867 *this, RVal, RValTy, LVal.getType(), Loc)),
2868 LVal);
Alexey Bataev5e018f92015-04-23 06:35:10 +00002869 break;
2870 case TEK_Complex:
Alexey Bataev8524d152016-01-21 12:35:58 +00002871 EmitStoreOfComplex(
2872 convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal,
Alexey Bataev5e018f92015-04-23 06:35:10 +00002873 /*isInit=*/false);
2874 break;
2875 case TEK_Aggregate:
2876 llvm_unreachable("Must be a scalar or complex.");
2877 }
2878}
2879
Alexey Bataevb57056f2015-01-22 06:17:56 +00002880static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst,
2881 const Expr *X, const Expr *V,
2882 SourceLocation Loc) {
2883 // v = x;
2884 assert(V->isLValue() && "V of 'omp atomic read' is not lvalue");
2885 assert(X->isLValue() && "X of 'omp atomic read' is not lvalue");
2886 LValue XLValue = CGF.EmitLValue(X);
2887 LValue VLValue = CGF.EmitLValue(V);
David Majnemera5b195a2015-02-14 01:35:12 +00002888 RValue Res = XLValue.isGlobalReg()
2889 ? CGF.EmitLoadOfLValue(XLValue, Loc)
JF Bastien92f4ef12016-04-06 17:26:42 +00002890 : CGF.EmitAtomicLoad(
2891 XLValue, Loc,
2892 IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
2893 : llvm::AtomicOrdering::Monotonic,
2894 XLValue.isVolatile());
Alexey Bataevb57056f2015-01-22 06:17:56 +00002895 // OpenMP, 2.12.6, atomic Construct
2896 // Any atomic construct with a seq_cst clause forces the atomically
2897 // performed operation to include an implicit flush operation without a
2898 // list.
2899 if (IsSeqCst)
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002900 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
Alexey Bataev8524d152016-01-21 12:35:58 +00002901 CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00002902}
2903
Alexey Bataevb8329262015-02-27 06:33:30 +00002904static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst,
2905 const Expr *X, const Expr *E,
2906 SourceLocation Loc) {
2907 // x = expr;
2908 assert(X->isLValue() && "X of 'omp atomic write' is not lvalue");
Alexey Bataev5e018f92015-04-23 06:35:10 +00002909 emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E));
Alexey Bataevb8329262015-02-27 06:33:30 +00002910 // OpenMP, 2.12.6, atomic Construct
2911 // Any atomic construct with a seq_cst clause forces the atomically
2912 // performed operation to include an implicit flush operation without a
2913 // list.
2914 if (IsSeqCst)
2915 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
2916}
2917
Benjamin Kramer439ee9d2015-05-01 13:59:53 +00002918static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X,
2919 RValue Update,
2920 BinaryOperatorKind BO,
2921 llvm::AtomicOrdering AO,
2922 bool IsXLHSInRHSPart) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002923 auto &Context = CGF.CGM.getContext();
2924 // Allow atomicrmw only if 'x' and 'update' are integer values, lvalue for 'x'
Alexey Bataevb4505a72015-03-30 05:20:59 +00002925 // expression is simple and atomic is allowed for the given type for the
2926 // target platform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002927 if (BO == BO_Comma || !Update.isScalar() ||
Alexey Bataev9d541a72015-05-08 11:47:16 +00002928 !Update.getScalarVal()->getType()->isIntegerTy() ||
2929 !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) &&
2930 (Update.getScalarVal()->getType() !=
John McCall7f416cc2015-09-08 08:05:57 +00002931 X.getAddress().getElementType())) ||
2932 !X.getAddress().getElementType()->isIntegerTy() ||
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002933 !Context.getTargetInfo().hasBuiltinAtomic(
2934 Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment())))
Alexey Bataev5e018f92015-04-23 06:35:10 +00002935 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002936
2937 llvm::AtomicRMWInst::BinOp RMWOp;
2938 switch (BO) {
2939 case BO_Add:
2940 RMWOp = llvm::AtomicRMWInst::Add;
2941 break;
2942 case BO_Sub:
2943 if (!IsXLHSInRHSPart)
Alexey Bataev5e018f92015-04-23 06:35:10 +00002944 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002945 RMWOp = llvm::AtomicRMWInst::Sub;
2946 break;
2947 case BO_And:
2948 RMWOp = llvm::AtomicRMWInst::And;
2949 break;
2950 case BO_Or:
2951 RMWOp = llvm::AtomicRMWInst::Or;
2952 break;
2953 case BO_Xor:
2954 RMWOp = llvm::AtomicRMWInst::Xor;
2955 break;
2956 case BO_LT:
2957 RMWOp = X.getType()->hasSignedIntegerRepresentation()
2958 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min
2959 : llvm::AtomicRMWInst::Max)
2960 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin
2961 : llvm::AtomicRMWInst::UMax);
2962 break;
2963 case BO_GT:
2964 RMWOp = X.getType()->hasSignedIntegerRepresentation()
2965 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max
2966 : llvm::AtomicRMWInst::Min)
2967 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax
2968 : llvm::AtomicRMWInst::UMin);
2969 break;
Alexey Bataev5e018f92015-04-23 06:35:10 +00002970 case BO_Assign:
2971 RMWOp = llvm::AtomicRMWInst::Xchg;
2972 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002973 case BO_Mul:
2974 case BO_Div:
2975 case BO_Rem:
2976 case BO_Shl:
2977 case BO_Shr:
2978 case BO_LAnd:
2979 case BO_LOr:
Alexey Bataev5e018f92015-04-23 06:35:10 +00002980 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002981 case BO_PtrMemD:
2982 case BO_PtrMemI:
2983 case BO_LE:
2984 case BO_GE:
2985 case BO_EQ:
2986 case BO_NE:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002987 case BO_AddAssign:
2988 case BO_SubAssign:
2989 case BO_AndAssign:
2990 case BO_OrAssign:
2991 case BO_XorAssign:
2992 case BO_MulAssign:
2993 case BO_DivAssign:
2994 case BO_RemAssign:
2995 case BO_ShlAssign:
2996 case BO_ShrAssign:
2997 case BO_Comma:
2998 llvm_unreachable("Unsupported atomic update operation");
2999 }
3000 auto *UpdateVal = Update.getScalarVal();
3001 if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) {
3002 UpdateVal = CGF.Builder.CreateIntCast(
John McCall7f416cc2015-09-08 08:05:57 +00003003 IC, X.getAddress().getElementType(),
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003004 X.getType()->hasSignedIntegerRepresentation());
3005 }
John McCall7f416cc2015-09-08 08:05:57 +00003006 auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003007 return std::make_pair(true, RValue::get(Res));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003008}
3009
Alexey Bataev5e018f92015-04-23 06:35:10 +00003010std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr(
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003011 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
3012 llvm::AtomicOrdering AO, SourceLocation Loc,
3013 const llvm::function_ref<RValue(RValue)> &CommonGen) {
3014 // Update expressions are allowed to have the following forms:
3015 // x binop= expr; -> xrval + expr;
3016 // x++, ++x -> xrval + 1;
3017 // x--, --x -> xrval - 1;
3018 // x = x binop expr; -> xrval binop expr
3019 // x = expr Op x; - > expr binop xrval;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003020 auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart);
3021 if (!Res.first) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003022 if (X.isGlobalReg()) {
3023 // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop
3024 // 'xrval'.
3025 EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X);
3026 } else {
3027 // Perform compare-and-swap procedure.
3028 EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified());
Alexey Bataevb4505a72015-03-30 05:20:59 +00003029 }
3030 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00003031 return Res;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003032}
3033
3034static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst,
3035 const Expr *X, const Expr *E,
3036 const Expr *UE, bool IsXLHSInRHSPart,
3037 SourceLocation Loc) {
3038 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
3039 "Update expr in 'atomic update' must be a binary operator.");
3040 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
3041 // Update expressions are allowed to have the following forms:
3042 // x binop= expr; -> xrval + expr;
3043 // x++, ++x -> xrval + 1;
3044 // x--, --x -> xrval - 1;
3045 // x = x binop expr; -> xrval binop expr
3046 // x = expr Op x; - > expr binop xrval;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003047 assert(X->isLValue() && "X of 'omp atomic update' is not lvalue");
Alexey Bataevb4505a72015-03-30 05:20:59 +00003048 LValue XLValue = CGF.EmitLValue(X);
3049 RValue ExprRValue = CGF.EmitAnyExpr(E);
JF Bastien92f4ef12016-04-06 17:26:42 +00003050 auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
3051 : llvm::AtomicOrdering::Monotonic;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003052 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
3053 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
3054 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
3055 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
3056 auto Gen =
3057 [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue {
3058 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
3059 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
3060 return CGF.EmitAnyExpr(UE);
3061 };
Alexey Bataev5e018f92015-04-23 06:35:10 +00003062 (void)CGF.EmitOMPAtomicSimpleUpdateExpr(
3063 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
3064 // OpenMP, 2.12.6, atomic Construct
3065 // Any atomic construct with a seq_cst clause forces the atomically
3066 // performed operation to include an implicit flush operation without a
3067 // list.
3068 if (IsSeqCst)
3069 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
3070}
3071
3072static RValue convertToType(CodeGenFunction &CGF, RValue Value,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003073 QualType SourceType, QualType ResType,
3074 SourceLocation Loc) {
Alexey Bataev5e018f92015-04-23 06:35:10 +00003075 switch (CGF.getEvaluationKind(ResType)) {
3076 case TEK_Scalar:
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003077 return RValue::get(
3078 convertToScalarValue(CGF, Value, SourceType, ResType, Loc));
Alexey Bataev5e018f92015-04-23 06:35:10 +00003079 case TEK_Complex: {
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003080 auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003081 return RValue::getComplex(Res.first, Res.second);
3082 }
3083 case TEK_Aggregate:
3084 break;
3085 }
3086 llvm_unreachable("Must be a scalar or complex.");
3087}
3088
3089static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst,
3090 bool IsPostfixUpdate, const Expr *V,
3091 const Expr *X, const Expr *E,
3092 const Expr *UE, bool IsXLHSInRHSPart,
3093 SourceLocation Loc) {
3094 assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue");
3095 assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue");
3096 RValue NewVVal;
3097 LValue VLValue = CGF.EmitLValue(V);
3098 LValue XLValue = CGF.EmitLValue(X);
3099 RValue ExprRValue = CGF.EmitAnyExpr(E);
JF Bastien92f4ef12016-04-06 17:26:42 +00003100 auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
3101 : llvm::AtomicOrdering::Monotonic;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003102 QualType NewVValType;
3103 if (UE) {
3104 // 'x' is updated with some additional value.
3105 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
3106 "Update expr in 'atomic capture' must be a binary operator.");
3107 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
3108 // Update expressions are allowed to have the following forms:
3109 // x binop= expr; -> xrval + expr;
3110 // x++, ++x -> xrval + 1;
3111 // x--, --x -> xrval - 1;
3112 // x = x binop expr; -> xrval binop expr
3113 // x = expr Op x; - > expr binop xrval;
3114 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
3115 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
3116 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
3117 NewVValType = XRValExpr->getType();
3118 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
3119 auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr,
3120 IsSeqCst, IsPostfixUpdate](RValue XRValue) -> RValue {
3121 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
3122 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
3123 RValue Res = CGF.EmitAnyExpr(UE);
3124 NewVVal = IsPostfixUpdate ? XRValue : Res;
3125 return Res;
3126 };
3127 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
3128 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
3129 if (Res.first) {
3130 // 'atomicrmw' instruction was generated.
3131 if (IsPostfixUpdate) {
3132 // Use old value from 'atomicrmw'.
3133 NewVVal = Res.second;
3134 } else {
3135 // 'atomicrmw' does not provide new value, so evaluate it using old
3136 // value of 'x'.
3137 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
3138 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second);
3139 NewVVal = CGF.EmitAnyExpr(UE);
3140 }
3141 }
3142 } else {
3143 // 'x' is simply rewritten with some 'expr'.
3144 NewVValType = X->getType().getNonReferenceType();
3145 ExprRValue = convertToType(CGF, ExprRValue, E->getType(),
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003146 X->getType().getNonReferenceType(), Loc);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003147 auto &&Gen = [&CGF, &NewVVal, ExprRValue](RValue XRValue) -> RValue {
3148 NewVVal = XRValue;
3149 return ExprRValue;
3150 };
3151 // Try to perform atomicrmw xchg, otherwise simple exchange.
3152 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
3153 XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO,
3154 Loc, Gen);
3155 if (Res.first) {
3156 // 'atomicrmw' instruction was generated.
3157 NewVVal = IsPostfixUpdate ? Res.second : ExprRValue;
3158 }
3159 }
3160 // Emit post-update store to 'v' of old/new 'x' value.
Alexey Bataev8524d152016-01-21 12:35:58 +00003161 CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc);
Alexey Bataevb4505a72015-03-30 05:20:59 +00003162 // OpenMP, 2.12.6, atomic Construct
3163 // Any atomic construct with a seq_cst clause forces the atomically
3164 // performed operation to include an implicit flush operation without a
3165 // list.
3166 if (IsSeqCst)
3167 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
3168}
3169
Alexey Bataevb57056f2015-01-22 06:17:56 +00003170static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind,
Alexey Bataev5e018f92015-04-23 06:35:10 +00003171 bool IsSeqCst, bool IsPostfixUpdate,
3172 const Expr *X, const Expr *V, const Expr *E,
3173 const Expr *UE, bool IsXLHSInRHSPart,
3174 SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00003175 switch (Kind) {
3176 case OMPC_read:
3177 EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc);
3178 break;
3179 case OMPC_write:
Alexey Bataevb8329262015-02-27 06:33:30 +00003180 EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc);
3181 break;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003182 case OMPC_unknown:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003183 case OMPC_update:
Alexey Bataevb4505a72015-03-30 05:20:59 +00003184 EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc);
3185 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00003186 case OMPC_capture:
Alexey Bataev5e018f92015-04-23 06:35:10 +00003187 EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE,
3188 IsXLHSInRHSPart, Loc);
3189 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00003190 case OMPC_if:
3191 case OMPC_final:
3192 case OMPC_num_threads:
3193 case OMPC_private:
3194 case OMPC_firstprivate:
3195 case OMPC_lastprivate:
3196 case OMPC_reduction:
3197 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00003198 case OMPC_simdlen:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003199 case OMPC_collapse:
3200 case OMPC_default:
3201 case OMPC_seq_cst:
3202 case OMPC_shared:
3203 case OMPC_linear:
3204 case OMPC_aligned:
3205 case OMPC_copyin:
3206 case OMPC_copyprivate:
3207 case OMPC_flush:
3208 case OMPC_proc_bind:
3209 case OMPC_schedule:
3210 case OMPC_ordered:
3211 case OMPC_nowait:
3212 case OMPC_untied:
3213 case OMPC_threadprivate:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00003214 case OMPC_depend:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003215 case OMPC_mergeable:
Michael Wonge710d542015-08-07 16:16:36 +00003216 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00003217 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003218 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00003219 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00003220 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00003221 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00003222 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00003223 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00003224 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00003225 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00003226 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00003227 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00003228 case OMPC_defaultmap:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003229 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00003230 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00003231 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00003232 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00003233 case OMPC_is_device_ptr:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003234 llvm_unreachable("Clause is not allowed in 'omp atomic'.");
3235 }
3236}
3237
3238void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003239 bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>();
Alexey Bataevb57056f2015-01-22 06:17:56 +00003240 OpenMPClauseKind Kind = OMPC_unknown;
3241 for (auto *C : S.clauses()) {
3242 // Find first clause (skip seq_cst clause, if it is first).
3243 if (C->getClauseKind() != OMPC_seq_cst) {
3244 Kind = C->getClauseKind();
3245 break;
3246 }
3247 }
Alexey Bataev10fec572015-03-11 04:48:56 +00003248
3249 const auto *CS =
3250 S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003251 if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) {
Alexey Bataev10fec572015-03-11 04:48:56 +00003252 enterFullExpression(EWC);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003253 }
3254 // Processing for statements under 'atomic capture'.
3255 if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) {
3256 for (const auto *C : Compound->body()) {
3257 if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) {
3258 enterFullExpression(EWC);
3259 }
3260 }
3261 }
Alexey Bataev10fec572015-03-11 04:48:56 +00003262
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003263 auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF,
3264 PrePostActionTy &) {
Alexey Bataev33c56402015-12-14 09:26:19 +00003265 CGF.EmitStopPoint(CS);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003266 EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(),
3267 S.getV(), S.getExpr(), S.getUpdateExpr(),
3268 S.isXLHSInRHSPart(), S.getLocStart());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00003269 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00003270 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003271 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen);
Alexey Bataev0162e452014-07-22 10:10:35 +00003272}
3273
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003274std::pair<llvm::Function * /*OutlinedFn*/, llvm::Constant * /*OutlinedFnID*/>
3275CodeGenFunction::EmitOMPTargetDirectiveOutlinedFunction(
3276 CodeGenModule &CGM, const OMPTargetDirective &S, StringRef ParentName,
3277 bool IsOffloadEntry) {
3278 llvm::Function *OutlinedFn = nullptr;
3279 llvm::Constant *OutlinedFnID = nullptr;
3280 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3281 OMPPrivateScope PrivateScope(CGF);
3282 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
3283 CGF.EmitOMPPrivateClause(S, PrivateScope);
3284 (void)PrivateScope.Privatize();
3285
3286 Action.Enter(CGF);
3287 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3288 };
3289 // Emit target region as a standalone region.
3290 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
3291 S, ParentName, OutlinedFn, OutlinedFnID, IsOffloadEntry, CodeGen);
3292 return std::make_pair(OutlinedFn, OutlinedFnID);
3293}
3294
Samuel Antaobed3c462015-10-02 16:14:20 +00003295void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) {
Samuel Antaobed3c462015-10-02 16:14:20 +00003296 const CapturedStmt &CS = *cast<CapturedStmt>(S.getAssociatedStmt());
3297
3298 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
Samuel Antao4af1b7b2015-12-02 17:44:43 +00003299 GenerateOpenMPCapturedVars(CS, CapturedVars);
Samuel Antaobed3c462015-10-02 16:14:20 +00003300
Samuel Antaoee8fb302016-01-06 13:42:12 +00003301 llvm::Function *Fn = nullptr;
3302 llvm::Constant *FnID = nullptr;
Samuel Antaobed3c462015-10-02 16:14:20 +00003303
3304 // Check if we have any if clause associated with the directive.
3305 const Expr *IfCond = nullptr;
3306
3307 if (auto *C = S.getSingleClause<OMPIfClause>()) {
3308 IfCond = C->getCondition();
3309 }
3310
3311 // Check if we have any device clause associated with the directive.
3312 const Expr *Device = nullptr;
3313 if (auto *C = S.getSingleClause<OMPDeviceClause>()) {
3314 Device = C->getDevice();
3315 }
3316
Samuel Antaoee8fb302016-01-06 13:42:12 +00003317 // Check if we have an if clause whose conditional always evaluates to false
3318 // or if we do not have any targets specified. If so the target region is not
3319 // an offload entry point.
3320 bool IsOffloadEntry = true;
3321 if (IfCond) {
3322 bool Val;
3323 if (ConstantFoldsToSimpleInteger(IfCond, Val) && !Val)
3324 IsOffloadEntry = false;
3325 }
3326 if (CGM.getLangOpts().OMPTargetTriples.empty())
3327 IsOffloadEntry = false;
3328
3329 assert(CurFuncDecl && "No parent declaration for target region!");
3330 StringRef ParentName;
3331 // In case we have Ctors/Dtors we use the complete type variant to produce
3332 // the mangling of the device outlined kernel.
3333 if (auto *D = dyn_cast<CXXConstructorDecl>(CurFuncDecl))
3334 ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete));
3335 else if (auto *D = dyn_cast<CXXDestructorDecl>(CurFuncDecl))
3336 ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete));
3337 else
3338 ParentName =
3339 CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CurFuncDecl)));
3340
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003341 std::tie(Fn, FnID) = EmitOMPTargetDirectiveOutlinedFunction(
3342 CGM, S, ParentName, IsOffloadEntry);
3343 OMPLexicalScope Scope(*this, S);
Samuel Antaoee8fb302016-01-06 13:42:12 +00003344 CGM.getOpenMPRuntime().emitTargetCall(*this, S, Fn, FnID, IfCond, Device,
Samuel Antaobed3c462015-10-02 16:14:20 +00003345 CapturedVars);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003346}
3347
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003348static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF,
3349 const OMPExecutableDirective &S,
3350 OpenMPDirectiveKind InnermostKind,
3351 const RegionCodeGenTy &CodeGen) {
3352 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003353 auto OutlinedFn = CGF.CGM.getOpenMPRuntime().
3354 emitParallelOrTeamsOutlinedFunction(S,
3355 *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
Samuel Antaob68e2db2016-03-03 16:20:23 +00003356
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003357 const OMPTeamsDirective &TD = *dyn_cast<OMPTeamsDirective>(&S);
3358 const OMPNumTeamsClause *NT = TD.getSingleClause<OMPNumTeamsClause>();
3359 const OMPThreadLimitClause *TL = TD.getSingleClause<OMPThreadLimitClause>();
3360 if (NT || TL) {
Carlo Bertollic6872252016-04-04 15:55:02 +00003361 Expr *NumTeams = (NT) ? NT->getNumTeams() : nullptr;
3362 Expr *ThreadLimit = (TL) ? TL->getThreadLimit() : nullptr;
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003363
Carlo Bertollic6872252016-04-04 15:55:02 +00003364 CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit,
3365 S.getLocStart());
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003366 }
3367
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003368 OMPLexicalScope Scope(CGF, S);
3369 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
3370 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003371 CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getLocStart(), OutlinedFn,
3372 CapturedVars);
3373}
3374
3375void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) {
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003376 // Emit parallel region as a standalone region.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003377 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003378 OMPPrivateScope PrivateScope(CGF);
Carlo Bertolli6ad7b5a2016-03-03 22:09:40 +00003379 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
3380 CGF.EmitOMPPrivateClause(S, PrivateScope);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003381 (void)PrivateScope.Privatize();
3382 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3383 };
3384 emitCommonOMPTeamsDirective(*this, S, OMPD_teams, CodeGen);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003385}
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003386
3387void CodeGenFunction::EmitOMPCancellationPointDirective(
3388 const OMPCancellationPointDirective &S) {
Alexey Bataev0f34da12015-07-02 04:17:07 +00003389 CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getLocStart(),
3390 S.getCancelRegion());
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003391}
3392
Alexey Bataev80909872015-07-02 11:25:17 +00003393void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) {
Alexey Bataev87933c72015-09-18 08:07:34 +00003394 const Expr *IfCond = nullptr;
3395 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
3396 if (C->getNameModifier() == OMPD_unknown ||
3397 C->getNameModifier() == OMPD_cancel) {
3398 IfCond = C->getCondition();
3399 break;
3400 }
3401 }
3402 CGM.getOpenMPRuntime().emitCancelCall(*this, S.getLocStart(), IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00003403 S.getCancelRegion());
Alexey Bataev80909872015-07-02 11:25:17 +00003404}
3405
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003406CodeGenFunction::JumpDest
3407CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) {
3408 if (Kind == OMPD_parallel || Kind == OMPD_task)
3409 return ReturnBlock;
Alexey Bataev25e5b442015-09-15 12:52:43 +00003410 assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections ||
Alexey Bataev3015bcc2016-01-22 08:56:50 +00003411 Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for);
Alexey Bataev25e5b442015-09-15 12:52:43 +00003412 return BreakContinueStack.back().BreakBlock;
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003413}
Michael Wong65f367f2015-07-21 13:44:28 +00003414
Samuel Antaocc10b852016-07-28 14:23:26 +00003415void CodeGenFunction::EmitOMPUseDevicePtrClause(
3416 const OMPClause &NC, OMPPrivateScope &PrivateScope,
3417 const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap) {
3418 const auto &C = cast<OMPUseDevicePtrClause>(NC);
3419 auto OrigVarIt = C.varlist_begin();
3420 auto InitIt = C.inits().begin();
3421 for (auto PvtVarIt : C.private_copies()) {
3422 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*OrigVarIt)->getDecl());
3423 auto *InitVD = cast<VarDecl>(cast<DeclRefExpr>(*InitIt)->getDecl());
3424 auto *PvtVD = cast<VarDecl>(cast<DeclRefExpr>(PvtVarIt)->getDecl());
3425
3426 // In order to identify the right initializer we need to match the
3427 // declaration used by the mapping logic. In some cases we may get
3428 // OMPCapturedExprDecl that refers to the original declaration.
3429 const ValueDecl *MatchingVD = OrigVD;
3430 if (auto *OED = dyn_cast<OMPCapturedExprDecl>(MatchingVD)) {
3431 // OMPCapturedExprDecl are used to privative fields of the current
3432 // structure.
3433 auto *ME = cast<MemberExpr>(OED->getInit());
3434 assert(isa<CXXThisExpr>(ME->getBase()) &&
3435 "Base should be the current struct!");
3436 MatchingVD = ME->getMemberDecl();
3437 }
3438
3439 // If we don't have information about the current list item, move on to
3440 // the next one.
3441 auto InitAddrIt = CaptureDeviceAddrMap.find(MatchingVD);
3442 if (InitAddrIt == CaptureDeviceAddrMap.end())
3443 continue;
3444
3445 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
3446 // Initialize the temporary initialization variable with the address we
3447 // get from the runtime library. We have to cast the source address
3448 // because it is always a void *. References are materialized in the
3449 // privatization scope, so the initialization here disregards the fact
3450 // the original variable is a reference.
3451 QualType AddrQTy =
3452 getContext().getPointerType(OrigVD->getType().getNonReferenceType());
3453 llvm::Type *AddrTy = ConvertTypeForMem(AddrQTy);
3454 Address InitAddr = Builder.CreateBitCast(InitAddrIt->second, AddrTy);
3455 setAddrOfLocalVar(InitVD, InitAddr);
3456
3457 // Emit private declaration, it will be initialized by the value we
3458 // declaration we just added to the local declarations map.
3459 EmitDecl(*PvtVD);
3460
3461 // The initialization variables reached its purpose in the emission
3462 // ofthe previous declaration, so we don't need it anymore.
3463 LocalDeclMap.erase(InitVD);
3464
3465 // Return the address of the private variable.
3466 return GetAddrOfLocalVar(PvtVD);
3467 });
3468 assert(IsRegistered && "firstprivate var already registered as private");
3469 // Silence the warning about unused variable.
3470 (void)IsRegistered;
3471
3472 ++OrigVarIt;
3473 ++InitIt;
3474 }
3475}
3476
Michael Wong65f367f2015-07-21 13:44:28 +00003477// Generate the instructions for '#pragma omp target data' directive.
3478void CodeGenFunction::EmitOMPTargetDataDirective(
3479 const OMPTargetDataDirective &S) {
Samuel Antaocc10b852016-07-28 14:23:26 +00003480 CGOpenMPRuntime::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true);
3481
3482 // Create a pre/post action to signal the privatization of the device pointer.
3483 // This action can be replaced by the OpenMP runtime code generation to
3484 // deactivate privatization.
3485 bool PrivatizeDevicePointers = false;
3486 class DevicePointerPrivActionTy : public PrePostActionTy {
3487 bool &PrivatizeDevicePointers;
3488
3489 public:
3490 explicit DevicePointerPrivActionTy(bool &PrivatizeDevicePointers)
3491 : PrePostActionTy(), PrivatizeDevicePointers(PrivatizeDevicePointers) {}
3492 void Enter(CodeGenFunction &CGF) override {
3493 PrivatizeDevicePointers = true;
3494 }
Samuel Antaodf158d52016-04-27 22:58:19 +00003495 };
Samuel Antaocc10b852016-07-28 14:23:26 +00003496 DevicePointerPrivActionTy PrivAction(PrivatizeDevicePointers);
3497
3498 auto &&CodeGen = [&S, &Info, &PrivatizeDevicePointers](
3499 CodeGenFunction &CGF, PrePostActionTy &Action) {
3500 auto &&InnermostCodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
3501 CGF.EmitStmt(
3502 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3503 };
3504
3505 // Codegen that selects wheather to generate the privatization code or not.
3506 auto &&PrivCodeGen = [&S, &Info, &PrivatizeDevicePointers,
3507 &InnermostCodeGen](CodeGenFunction &CGF,
3508 PrePostActionTy &Action) {
3509 RegionCodeGenTy RCG(InnermostCodeGen);
3510 PrivatizeDevicePointers = false;
3511
3512 // Call the pre-action to change the status of PrivatizeDevicePointers if
3513 // needed.
3514 Action.Enter(CGF);
3515
3516 if (PrivatizeDevicePointers) {
3517 OMPPrivateScope PrivateScope(CGF);
3518 // Emit all instances of the use_device_ptr clause.
3519 for (const auto *C : S.getClausesOfKind<OMPUseDevicePtrClause>())
3520 CGF.EmitOMPUseDevicePtrClause(*C, PrivateScope,
3521 Info.CaptureDeviceAddrMap);
3522 (void)PrivateScope.Privatize();
3523 RCG(CGF);
3524 } else
3525 RCG(CGF);
3526 };
3527
3528 // Forward the provided action to the privatization codegen.
3529 RegionCodeGenTy PrivRCG(PrivCodeGen);
3530 PrivRCG.setAction(Action);
3531
3532 // Notwithstanding the body of the region is emitted as inlined directive,
3533 // we don't use an inline scope as changes in the references inside the
3534 // region are expected to be visible outside, so we do not privative them.
3535 OMPLexicalScope Scope(CGF, S);
3536 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_target_data,
3537 PrivRCG);
3538 };
3539
3540 RegionCodeGenTy RCG(CodeGen);
Samuel Antaodf158d52016-04-27 22:58:19 +00003541
3542 // If we don't have target devices, don't bother emitting the data mapping
3543 // code.
3544 if (CGM.getLangOpts().OMPTargetTriples.empty()) {
Samuel Antaocc10b852016-07-28 14:23:26 +00003545 RCG(*this);
Samuel Antaodf158d52016-04-27 22:58:19 +00003546 return;
3547 }
3548
3549 // Check if we have any if clause associated with the directive.
3550 const Expr *IfCond = nullptr;
3551 if (auto *C = S.getSingleClause<OMPIfClause>())
3552 IfCond = C->getCondition();
3553
3554 // Check if we have any device clause associated with the directive.
3555 const Expr *Device = nullptr;
3556 if (auto *C = S.getSingleClause<OMPDeviceClause>())
3557 Device = C->getDevice();
3558
Samuel Antaocc10b852016-07-28 14:23:26 +00003559 // Set the action to signal privatization of device pointers.
3560 RCG.setAction(PrivAction);
3561
3562 // Emit region code.
3563 CGM.getOpenMPRuntime().emitTargetDataCalls(*this, S, IfCond, Device, RCG,
3564 Info);
Michael Wong65f367f2015-07-21 13:44:28 +00003565}
Alexey Bataev49f6e782015-12-01 04:18:41 +00003566
Samuel Antaodf67fc42016-01-19 19:15:56 +00003567void CodeGenFunction::EmitOMPTargetEnterDataDirective(
3568 const OMPTargetEnterDataDirective &S) {
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00003569 // If we don't have target devices, don't bother emitting the data mapping
3570 // code.
3571 if (CGM.getLangOpts().OMPTargetTriples.empty())
3572 return;
3573
3574 // Check if we have any if clause associated with the directive.
3575 const Expr *IfCond = nullptr;
3576 if (auto *C = S.getSingleClause<OMPIfClause>())
3577 IfCond = C->getCondition();
3578
3579 // Check if we have any device clause associated with the directive.
3580 const Expr *Device = nullptr;
3581 if (auto *C = S.getSingleClause<OMPDeviceClause>())
3582 Device = C->getDevice();
3583
Samuel Antao8d2d7302016-05-26 18:30:22 +00003584 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003585}
3586
Samuel Antao72590762016-01-19 20:04:50 +00003587void CodeGenFunction::EmitOMPTargetExitDataDirective(
3588 const OMPTargetExitDataDirective &S) {
Samuel Antao8dd66282016-04-27 23:14:30 +00003589 // If we don't have target devices, don't bother emitting the data mapping
3590 // code.
3591 if (CGM.getLangOpts().OMPTargetTriples.empty())
3592 return;
3593
3594 // Check if we have any if clause associated with the directive.
3595 const Expr *IfCond = nullptr;
3596 if (auto *C = S.getSingleClause<OMPIfClause>())
3597 IfCond = C->getCondition();
3598
3599 // Check if we have any device clause associated with the directive.
3600 const Expr *Device = nullptr;
3601 if (auto *C = S.getSingleClause<OMPDeviceClause>())
3602 Device = C->getDevice();
3603
Samuel Antao8d2d7302016-05-26 18:30:22 +00003604 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
Samuel Antao72590762016-01-19 20:04:50 +00003605}
3606
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003607void CodeGenFunction::EmitOMPTargetParallelDirective(
3608 const OMPTargetParallelDirective &S) {
3609 // TODO: codegen for target parallel.
3610}
3611
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003612void CodeGenFunction::EmitOMPTargetParallelForDirective(
3613 const OMPTargetParallelForDirective &S) {
3614 // TODO: codegen for target parallel for.
3615}
3616
Alexey Bataev7292c292016-04-25 12:22:29 +00003617/// Emit a helper variable and return corresponding lvalue.
3618static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper,
3619 const ImplicitParamDecl *PVD,
3620 CodeGenFunction::OMPPrivateScope &Privates) {
3621 auto *VDecl = cast<VarDecl>(Helper->getDecl());
3622 Privates.addPrivate(
3623 VDecl, [&CGF, PVD]() -> Address { return CGF.GetAddrOfLocalVar(PVD); });
3624}
3625
3626void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) {
3627 assert(isOpenMPTaskLoopDirective(S.getDirectiveKind()));
3628 // Emit outlined function for task construct.
3629 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
3630 auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
3631 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
3632 const Expr *IfCond = nullptr;
3633 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
3634 if (C->getNameModifier() == OMPD_unknown ||
3635 C->getNameModifier() == OMPD_taskloop) {
3636 IfCond = C->getCondition();
3637 break;
3638 }
3639 }
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003640
3641 OMPTaskDataTy Data;
3642 // Check if taskloop must be emitted without taskgroup.
3643 Data.Nogroup = S.getSingleClause<OMPNogroupClause>();
Alexey Bataev7292c292016-04-25 12:22:29 +00003644 // TODO: Check if we should emit tied or untied task.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003645 Data.Tied = true;
3646 // Set scheduling for taskloop
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00003647 if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) {
3648 // grainsize clause
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003649 Data.Schedule.setInt(/*IntVal=*/false);
3650 Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize()));
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00003651 } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) {
3652 // num_tasks clause
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003653 Data.Schedule.setInt(/*IntVal=*/true);
3654 Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks()));
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00003655 }
Alexey Bataev7292c292016-04-25 12:22:29 +00003656
3657 auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) {
3658 // if (PreCond) {
3659 // for (IV in 0..LastIteration) BODY;
3660 // <Final counter/linear vars updates>;
3661 // }
3662 //
3663
3664 // Emit: if (PreCond) - begin.
3665 // If the condition constant folds and can be elided, avoid emitting the
3666 // whole loop.
3667 bool CondConstant;
3668 llvm::BasicBlock *ContBlock = nullptr;
3669 OMPLoopScope PreInitScope(CGF, S);
3670 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
3671 if (!CondConstant)
3672 return;
3673 } else {
3674 auto *ThenBlock = CGF.createBasicBlock("taskloop.if.then");
3675 ContBlock = CGF.createBasicBlock("taskloop.if.end");
3676 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
3677 CGF.getProfileCount(&S));
3678 CGF.EmitBlock(ThenBlock);
3679 CGF.incrementProfileCounter(&S);
3680 }
3681
Alexey Bataev1e73ef32016-04-28 12:14:51 +00003682 if (isOpenMPSimdDirective(S.getDirectiveKind()))
3683 CGF.EmitOMPSimdInit(S);
3684
Alexey Bataev7292c292016-04-25 12:22:29 +00003685 OMPPrivateScope LoopScope(CGF);
3686 // Emit helper vars inits.
3687 enum { LowerBound = 5, UpperBound, Stride, LastIter };
3688 auto *I = CS->getCapturedDecl()->param_begin();
3689 auto *LBP = std::next(I, LowerBound);
3690 auto *UBP = std::next(I, UpperBound);
3691 auto *STP = std::next(I, Stride);
3692 auto *LIP = std::next(I, LastIter);
3693 mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP,
3694 LoopScope);
3695 mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP,
3696 LoopScope);
3697 mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope);
3698 mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP,
3699 LoopScope);
3700 CGF.EmitOMPPrivateLoopCounters(S, LoopScope);
Alexey Bataevf93095a2016-05-05 08:46:22 +00003701 bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev7292c292016-04-25 12:22:29 +00003702 (void)LoopScope.Privatize();
3703 // Emit the loop iteration variable.
3704 const Expr *IVExpr = S.getIterationVariable();
3705 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
3706 CGF.EmitVarDecl(*IVDecl);
3707 CGF.EmitIgnoredExpr(S.getInit());
3708
3709 // Emit the iterations count variable.
3710 // If it is not a variable, Sema decided to calculate iterations count on
3711 // each iteration (e.g., it is foldable into a constant).
3712 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
3713 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
3714 // Emit calculation of the iterations count.
3715 CGF.EmitIgnoredExpr(S.getCalcLastIteration());
3716 }
3717
3718 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
3719 S.getInc(),
3720 [&S](CodeGenFunction &CGF) {
3721 CGF.EmitOMPLoopBody(S, JumpDest());
3722 CGF.EmitStopPoint(&S);
3723 },
3724 [](CodeGenFunction &) {});
3725 // Emit: if (PreCond) - end.
3726 if (ContBlock) {
3727 CGF.EmitBranch(ContBlock);
3728 CGF.EmitBlock(ContBlock, true);
3729 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00003730 // Emit final copy of the lastprivate variables if IsLastIter != 0.
3731 if (HasLastprivateClause) {
3732 CGF.EmitOMPLastprivateClauseFinal(
3733 S, isOpenMPSimdDirective(S.getDirectiveKind()),
3734 CGF.Builder.CreateIsNotNull(CGF.EmitLoadOfScalar(
3735 CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false,
3736 (*LIP)->getType(), S.getLocStart())));
3737 }
Alexey Bataev7292c292016-04-25 12:22:29 +00003738 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003739 auto &&TaskGen = [&S, SharedsTy, CapturedStruct,
3740 IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn,
3741 const OMPTaskDataTy &Data) {
Alexey Bataev7292c292016-04-25 12:22:29 +00003742 auto &&CodeGen = [&](CodeGenFunction &CGF, PrePostActionTy &) {
3743 OMPLoopScope PreInitScope(CGF, S);
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003744 CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getLocStart(), S,
3745 OutlinedFn, SharedsTy,
3746 CapturedStruct, IfCond, Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00003747 };
3748 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop,
3749 CodeGen);
3750 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00003751 EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00003752}
3753
Alexey Bataev49f6e782015-12-01 04:18:41 +00003754void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) {
Alexey Bataev7292c292016-04-25 12:22:29 +00003755 EmitOMPTaskLoopBasedDirective(S);
Alexey Bataev49f6e782015-12-01 04:18:41 +00003756}
3757
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003758void CodeGenFunction::EmitOMPTaskLoopSimdDirective(
3759 const OMPTaskLoopSimdDirective &S) {
Alexey Bataev1e73ef32016-04-28 12:14:51 +00003760 EmitOMPTaskLoopBasedDirective(S);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003761}
Samuel Antao686c70c2016-05-26 17:30:50 +00003762
3763// Generate the instructions for '#pragma omp target update' directive.
3764void CodeGenFunction::EmitOMPTargetUpdateDirective(
3765 const OMPTargetUpdateDirective &S) {
Samuel Antao8d2d7302016-05-26 18:30:22 +00003766 // If we don't have target devices, don't bother emitting the data mapping
3767 // code.
3768 if (CGM.getLangOpts().OMPTargetTriples.empty())
3769 return;
3770
3771 // Check if we have any if clause associated with the directive.
3772 const Expr *IfCond = nullptr;
3773 if (auto *C = S.getSingleClause<OMPIfClause>())
3774 IfCond = C->getCondition();
3775
3776 // Check if we have any device clause associated with the directive.
3777 const Expr *Device = nullptr;
3778 if (auto *C = S.getSingleClause<OMPDeviceClause>())
3779 Device = C->getDevice();
3780
3781 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
Samuel Antao686c70c2016-05-26 17:30:50 +00003782}