blob: 8984aca7b70dd0caa5616eac1a8360fcba63cada [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 Bataev9959db52014-05-06 10:08:46 +000022using namespace clang;
23using namespace CodeGen;
24
Alexey Bataev3392d762016-02-16 11:18:12 +000025namespace {
26/// Lexical scope for OpenMP executable constructs, that handles correct codegen
27/// for captured expressions.
28class OMPLexicalScope {
29 CodeGenFunction::LexicalScope Scope;
30 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 }
47
Carlo Bertolli6ad7b5a2016-03-03 22:09:40 +000048 class PostUpdateCleanup final : public EHScopeStack::Cleanup {
49 const OMPExecutableDirective &S;
50
51 public:
52 PostUpdateCleanup(const OMPExecutableDirective &S) : S(S) {}
53
54 void Emit(CodeGenFunction &CGF, Flags /*flags*/) override {
55 if (!CGF.HaveInsertPoint())
56 return;
57 (void)S;
58 // TODO: add cleanups for clauses that require post update.
59 }
60 };
61
Alexey Bataev3392d762016-02-16 11:18:12 +000062public:
63 OMPLexicalScope(CodeGenFunction &CGF, const OMPExecutableDirective &S)
64 : Scope(CGF, S.getSourceRange()) {
65 emitPreInitStmt(CGF, S);
Carlo Bertolli6ad7b5a2016-03-03 22:09:40 +000066 CGF.EHStack.pushCleanup<PostUpdateCleanup>(NormalAndEHCleanup, S);
Alexey Bataev3392d762016-02-16 11:18:12 +000067 }
68};
69} // namespace
70
Alexey Bataev1189bd02016-01-26 12:20:39 +000071llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) {
72 auto &C = getContext();
73 llvm::Value *Size = nullptr;
74 auto SizeInChars = C.getTypeSizeInChars(Ty);
75 if (SizeInChars.isZero()) {
76 // getTypeSizeInChars() returns 0 for a VLA.
77 while (auto *VAT = C.getAsVariableArrayType(Ty)) {
78 llvm::Value *ArraySize;
79 std::tie(ArraySize, Ty) = getVLASize(VAT);
80 Size = Size ? Builder.CreateNUWMul(Size, ArraySize) : ArraySize;
81 }
82 SizeInChars = C.getTypeSizeInChars(Ty);
83 if (SizeInChars.isZero())
84 return llvm::ConstantInt::get(SizeTy, /*V=*/0);
85 Size = Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars));
86 } else
87 Size = CGM.getSize(SizeInChars);
88 return Size;
89}
90
Alexey Bataev2377fe92015-09-10 08:12:02 +000091void CodeGenFunction::GenerateOpenMPCapturedVars(
Samuel Antao4af1b7b2015-12-02 17:44:43 +000092 const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) {
Alexey Bataev2377fe92015-09-10 08:12:02 +000093 const RecordDecl *RD = S.getCapturedRecordDecl();
94 auto CurField = RD->field_begin();
95 auto CurCap = S.captures().begin();
96 for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(),
97 E = S.capture_init_end();
98 I != E; ++I, ++CurField, ++CurCap) {
99 if (CurField->hasCapturedVLAType()) {
100 auto VAT = CurField->getCapturedVLAType();
Samuel Antaobed3c462015-10-02 16:14:20 +0000101 auto *Val = VLASizeMap[VAT->getSizeExpr()];
Samuel Antaobed3c462015-10-02 16:14:20 +0000102 CapturedVars.push_back(Val);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000103 } else if (CurCap->capturesThis())
104 CapturedVars.push_back(CXXThisValue);
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000105 else if (CurCap->capturesVariableByCopy())
106 CapturedVars.push_back(
107 EmitLoadOfLValue(EmitLValue(*I), SourceLocation()).getScalarVal());
108 else {
109 assert(CurCap->capturesVariable() && "Expected capture by reference.");
Alexey Bataev2377fe92015-09-10 08:12:02 +0000110 CapturedVars.push_back(EmitLValue(*I).getAddress().getPointer());
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000111 }
Alexey Bataev2377fe92015-09-10 08:12:02 +0000112 }
113}
114
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000115static Address castValueFromUintptr(CodeGenFunction &CGF, QualType DstType,
116 StringRef Name, LValue AddrLV,
117 bool isReferenceType = false) {
118 ASTContext &Ctx = CGF.getContext();
119
120 auto *CastedPtr = CGF.EmitScalarConversion(
121 AddrLV.getAddress().getPointer(), Ctx.getUIntPtrType(),
122 Ctx.getPointerType(DstType), SourceLocation());
123 auto TmpAddr =
124 CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType))
125 .getAddress();
126
127 // If we are dealing with references we need to return the address of the
128 // reference instead of the reference of the value.
129 if (isReferenceType) {
130 QualType RefType = Ctx.getLValueReferenceType(DstType);
131 auto *RefVal = TmpAddr.getPointer();
132 TmpAddr = CGF.CreateMemTemp(RefType, Twine(Name) + ".ref");
133 auto TmpLVal = CGF.MakeAddrLValue(TmpAddr, RefType);
134 CGF.EmitScalarInit(RefVal, TmpLVal);
135 }
136
137 return TmpAddr;
138}
139
Alexey Bataev2377fe92015-09-10 08:12:02 +0000140llvm::Function *
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000141CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S) {
Alexey Bataev2377fe92015-09-10 08:12:02 +0000142 assert(
143 CapturedStmtInfo &&
144 "CapturedStmtInfo should be set when generating the captured function");
145 const CapturedDecl *CD = S.getCapturedDecl();
146 const RecordDecl *RD = S.getCapturedRecordDecl();
147 assert(CD->hasBody() && "missing CapturedDecl body");
148
149 // Build the argument list.
150 ASTContext &Ctx = CGM.getContext();
151 FunctionArgList Args;
152 Args.append(CD->param_begin(),
153 std::next(CD->param_begin(), CD->getContextParamPosition()));
154 auto I = S.captures().begin();
155 for (auto *FD : RD->fields()) {
156 QualType ArgType = FD->getType();
157 IdentifierInfo *II = nullptr;
158 VarDecl *CapVar = nullptr;
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000159
160 // If this is a capture by copy and the type is not a pointer, the outlined
161 // function argument type should be uintptr and the value properly casted to
162 // uintptr. This is necessary given that the runtime library is only able to
163 // deal with pointers. We can pass in the same way the VLA type sizes to the
164 // outlined function.
165 if ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) ||
166 I->capturesVariableArrayType())
167 ArgType = Ctx.getUIntPtrType();
168
169 if (I->capturesVariable() || I->capturesVariableByCopy()) {
Alexey Bataev2377fe92015-09-10 08:12:02 +0000170 CapVar = I->getCapturedVar();
171 II = CapVar->getIdentifier();
172 } else if (I->capturesThis())
173 II = &getContext().Idents.get("this");
174 else {
175 assert(I->capturesVariableArrayType());
176 II = &getContext().Idents.get("vla");
177 }
178 if (ArgType->isVariablyModifiedType())
179 ArgType = getContext().getVariableArrayDecayedType(ArgType);
180 Args.push_back(ImplicitParamDecl::Create(getContext(), nullptr,
181 FD->getLocation(), II, ArgType));
182 ++I;
183 }
184 Args.append(
185 std::next(CD->param_begin(), CD->getContextParamPosition() + 1),
186 CD->param_end());
187
188 // Create the function declaration.
189 FunctionType::ExtInfo ExtInfo;
190 const CGFunctionInfo &FuncInfo =
191 CGM.getTypes().arrangeFreeFunctionDeclaration(Ctx.VoidTy, Args, ExtInfo,
192 /*IsVariadic=*/false);
193 llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo);
194
195 llvm::Function *F = llvm::Function::Create(
196 FuncLLVMTy, llvm::GlobalValue::InternalLinkage,
197 CapturedStmtInfo->getHelperName(), &CGM.getModule());
198 CGM.SetInternalFunctionAttributes(CD, F, FuncInfo);
199 if (CD->isNothrow())
200 F->addFnAttr(llvm::Attribute::NoUnwind);
201
202 // Generate the function.
203 StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args, CD->getLocation(),
204 CD->getBody()->getLocStart());
205 unsigned Cnt = CD->getContextParamPosition();
206 I = S.captures().begin();
207 for (auto *FD : RD->fields()) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000208 // If we are capturing a pointer by copy we don't need to do anything, just
209 // use the value that we get from the arguments.
210 if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) {
211 setAddrOfLocalVar(I->getCapturedVar(), GetAddrOfLocalVar(Args[Cnt]));
Richard Trieucc3949d2016-02-18 22:34:54 +0000212 ++Cnt;
213 ++I;
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000214 continue;
215 }
216
Alexey Bataev2377fe92015-09-10 08:12:02 +0000217 LValue ArgLVal =
218 MakeAddrLValue(GetAddrOfLocalVar(Args[Cnt]), Args[Cnt]->getType(),
219 AlignmentSource::Decl);
220 if (FD->hasCapturedVLAType()) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000221 LValue CastedArgLVal =
222 MakeAddrLValue(castValueFromUintptr(*this, FD->getType(),
223 Args[Cnt]->getName(), ArgLVal),
224 FD->getType(), AlignmentSource::Decl);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000225 auto *ExprArg =
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000226 EmitLoadOfLValue(CastedArgLVal, SourceLocation()).getScalarVal();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000227 auto VAT = FD->getCapturedVLAType();
228 VLASizeMap[VAT->getSizeExpr()] = ExprArg;
229 } else if (I->capturesVariable()) {
230 auto *Var = I->getCapturedVar();
231 QualType VarTy = Var->getType();
232 Address ArgAddr = ArgLVal.getAddress();
233 if (!VarTy->isReferenceType()) {
234 ArgAddr = EmitLoadOfReference(
235 ArgAddr, ArgLVal.getType()->castAs<ReferenceType>());
236 }
Alexey Bataevc71a4092015-09-11 10:29:41 +0000237 setAddrOfLocalVar(
238 Var, Address(ArgAddr.getPointer(), getContext().getDeclAlign(Var)));
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000239 } else if (I->capturesVariableByCopy()) {
240 assert(!FD->getType()->isAnyPointerType() &&
241 "Not expecting a captured pointer.");
242 auto *Var = I->getCapturedVar();
243 QualType VarTy = Var->getType();
244 setAddrOfLocalVar(I->getCapturedVar(),
245 castValueFromUintptr(*this, FD->getType(),
246 Args[Cnt]->getName(), ArgLVal,
247 VarTy->isReferenceType()));
Alexey Bataev2377fe92015-09-10 08:12:02 +0000248 } else {
249 // If 'this' is captured, load it into CXXThisValue.
250 assert(I->capturesThis());
251 CXXThisValue =
252 EmitLoadOfLValue(ArgLVal, Args[Cnt]->getLocation()).getScalarVal();
253 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000254 ++Cnt;
255 ++I;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000256 }
257
Serge Pavlov3a561452015-12-06 14:32:39 +0000258 PGO.assignRegionCounters(GlobalDecl(CD), F);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000259 CapturedStmtInfo->EmitBody(*this, CD->getBody());
260 FinishFunction(CD->getBodyRBrace());
261
262 return F;
263}
264
Alexey Bataev9959db52014-05-06 10:08:46 +0000265//===----------------------------------------------------------------------===//
266// OpenMP Directive Emission
267//===----------------------------------------------------------------------===//
Alexey Bataev420d45b2015-04-14 05:11:24 +0000268void CodeGenFunction::EmitOMPAggregateAssign(
John McCall7f416cc2015-09-08 08:05:57 +0000269 Address DestAddr, Address SrcAddr, QualType OriginalType,
270 const llvm::function_ref<void(Address, Address)> &CopyGen) {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000271 // Perform element-by-element initialization.
272 QualType ElementTy;
John McCall7f416cc2015-09-08 08:05:57 +0000273
274 // Drill down to the base element type on both arrays.
Alexey Bataev420d45b2015-04-14 05:11:24 +0000275 auto ArrayTy = OriginalType->getAsArrayTypeUnsafe();
John McCall7f416cc2015-09-08 08:05:57 +0000276 auto NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr);
277 SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
278
279 auto SrcBegin = SrcAddr.getPointer();
280 auto DestBegin = DestAddr.getPointer();
Alexey Bataev420d45b2015-04-14 05:11:24 +0000281 // Cast from pointer to array type to pointer to single element.
Alexey Bataev420d45b2015-04-14 05:11:24 +0000282 auto DestEnd = Builder.CreateGEP(DestBegin, NumElements);
283 // The basic structure here is a while-do loop.
284 auto BodyBB = createBasicBlock("omp.arraycpy.body");
285 auto DoneBB = createBasicBlock("omp.arraycpy.done");
286 auto IsEmpty =
287 Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty");
288 Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000289
Alexey Bataev420d45b2015-04-14 05:11:24 +0000290 // Enter the loop body, making that address the current address.
291 auto EntryBB = Builder.GetInsertBlock();
292 EmitBlock(BodyBB);
John McCall7f416cc2015-09-08 08:05:57 +0000293
294 CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy);
295
296 llvm::PHINode *SrcElementPHI =
297 Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast");
298 SrcElementPHI->addIncoming(SrcBegin, EntryBB);
299 Address SrcElementCurrent =
300 Address(SrcElementPHI,
301 SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize));
302
303 llvm::PHINode *DestElementPHI =
304 Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast");
305 DestElementPHI->addIncoming(DestBegin, EntryBB);
306 Address DestElementCurrent =
307 Address(DestElementPHI,
308 DestAddr.getAlignment().alignmentOfArrayElement(ElementSize));
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000309
Alexey Bataev420d45b2015-04-14 05:11:24 +0000310 // Emit copy.
311 CopyGen(DestElementCurrent, SrcElementCurrent);
312
313 // Shift the address forward by one element.
314 auto DestElementNext = Builder.CreateConstGEP1_32(
John McCall7f416cc2015-09-08 08:05:57 +0000315 DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
Alexey Bataev420d45b2015-04-14 05:11:24 +0000316 auto SrcElementNext = Builder.CreateConstGEP1_32(
John McCall7f416cc2015-09-08 08:05:57 +0000317 SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element");
Alexey Bataev420d45b2015-04-14 05:11:24 +0000318 // Check whether we've reached the end.
319 auto Done =
320 Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
321 Builder.CreateCondBr(Done, DoneBB, BodyBB);
John McCall7f416cc2015-09-08 08:05:57 +0000322 DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock());
323 SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock());
Alexey Bataev420d45b2015-04-14 05:11:24 +0000324
325 // Done.
326 EmitBlock(DoneBB, /*IsFinished=*/true);
327}
328
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000329/// \brief Emit initialization of arrays of complex types.
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000330/// \param DestAddr Address of the array.
331/// \param Type Type of array.
332/// \param Init Initial expression of array.
333static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr,
334 QualType Type, const Expr *Init) {
335 // Perform element-by-element initialization.
336 QualType ElementTy;
337
338 // Drill down to the base element type on both arrays.
339 auto ArrayTy = Type->getAsArrayTypeUnsafe();
340 auto NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr);
341 DestAddr =
342 CGF.Builder.CreateElementBitCast(DestAddr, DestAddr.getElementType());
343
344 auto DestBegin = DestAddr.getPointer();
345 // Cast from pointer to array type to pointer to single element.
346 auto DestEnd = CGF.Builder.CreateGEP(DestBegin, NumElements);
347 // The basic structure here is a while-do loop.
348 auto BodyBB = CGF.createBasicBlock("omp.arrayinit.body");
349 auto DoneBB = CGF.createBasicBlock("omp.arrayinit.done");
350 auto IsEmpty =
351 CGF.Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arrayinit.isempty");
352 CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
353
354 // Enter the loop body, making that address the current address.
355 auto EntryBB = CGF.Builder.GetInsertBlock();
356 CGF.EmitBlock(BodyBB);
357
358 CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy);
359
360 llvm::PHINode *DestElementPHI = CGF.Builder.CreatePHI(
361 DestBegin->getType(), 2, "omp.arraycpy.destElementPast");
362 DestElementPHI->addIncoming(DestBegin, EntryBB);
363 Address DestElementCurrent =
364 Address(DestElementPHI,
365 DestAddr.getAlignment().alignmentOfArrayElement(ElementSize));
366
367 // Emit copy.
368 {
369 CodeGenFunction::RunCleanupsScope InitScope(CGF);
370 CGF.EmitAnyExprToMem(Init, DestElementCurrent, ElementTy.getQualifiers(),
371 /*IsInitializer=*/false);
372 }
373
374 // Shift the address forward by one element.
375 auto DestElementNext = CGF.Builder.CreateConstGEP1_32(
376 DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
377 // Check whether we've reached the end.
378 auto Done =
379 CGF.Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
380 CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB);
381 DestElementPHI->addIncoming(DestElementNext, CGF.Builder.GetInsertBlock());
382
383 // Done.
384 CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
385}
386
John McCall7f416cc2015-09-08 08:05:57 +0000387void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr,
388 Address SrcAddr, const VarDecl *DestVD,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000389 const VarDecl *SrcVD, const Expr *Copy) {
390 if (OriginalType->isArrayType()) {
391 auto *BO = dyn_cast<BinaryOperator>(Copy);
392 if (BO && BO->getOpcode() == BO_Assign) {
393 // Perform simple memcpy for simple copying.
John McCall7f416cc2015-09-08 08:05:57 +0000394 EmitAggregateAssign(DestAddr, SrcAddr, OriginalType);
Alexey Bataev420d45b2015-04-14 05:11:24 +0000395 } else {
396 // For arrays with complex element types perform element by element
397 // copying.
John McCall7f416cc2015-09-08 08:05:57 +0000398 EmitOMPAggregateAssign(
Alexey Bataev420d45b2015-04-14 05:11:24 +0000399 DestAddr, SrcAddr, OriginalType,
John McCall7f416cc2015-09-08 08:05:57 +0000400 [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000401 // Working with the single array element, so have to remap
402 // destination and source variables to corresponding array
403 // elements.
John McCall7f416cc2015-09-08 08:05:57 +0000404 CodeGenFunction::OMPPrivateScope Remap(*this);
405 Remap.addPrivate(DestVD, [DestElement]() -> Address {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000406 return DestElement;
407 });
408 Remap.addPrivate(
John McCall7f416cc2015-09-08 08:05:57 +0000409 SrcVD, [SrcElement]() -> Address { return SrcElement; });
Alexey Bataev420d45b2015-04-14 05:11:24 +0000410 (void)Remap.Privatize();
John McCall7f416cc2015-09-08 08:05:57 +0000411 EmitIgnoredExpr(Copy);
Alexey Bataev420d45b2015-04-14 05:11:24 +0000412 });
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000413 }
Alexey Bataev420d45b2015-04-14 05:11:24 +0000414 } else {
415 // Remap pseudo source variable to private copy.
John McCall7f416cc2015-09-08 08:05:57 +0000416 CodeGenFunction::OMPPrivateScope Remap(*this);
417 Remap.addPrivate(SrcVD, [SrcAddr]() -> Address { return SrcAddr; });
418 Remap.addPrivate(DestVD, [DestAddr]() -> Address { return DestAddr; });
Alexey Bataev420d45b2015-04-14 05:11:24 +0000419 (void)Remap.Privatize();
420 // Emit copying of the whole variable.
John McCall7f416cc2015-09-08 08:05:57 +0000421 EmitIgnoredExpr(Copy);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000422 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000423}
424
Alexey Bataev69c62a92015-04-15 04:52:20 +0000425bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
426 OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000427 if (!HaveInsertPoint())
428 return false;
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000429 bool FirstprivateIsLastprivate = false;
430 llvm::DenseSet<const VarDecl *> Lastprivates;
431 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
432 for (const auto *D : C->varlists())
433 Lastprivates.insert(
434 cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl());
435 }
Alexey Bataev69c62a92015-04-15 04:52:20 +0000436 llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000437 for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) {
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000438 auto IRef = C->varlist_begin();
439 auto InitsRef = C->inits().begin();
440 for (auto IInit : C->private_copies()) {
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000441 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000442 FirstprivateIsLastprivate =
443 FirstprivateIsLastprivate ||
444 (Lastprivates.count(OrigVD->getCanonicalDecl()) > 0);
445 if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000446 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
447 auto *VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl());
448 bool IsRegistered;
449 DeclRefExpr DRE(
450 const_cast<VarDecl *>(OrigVD),
451 /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup(
452 OrigVD) != nullptr,
453 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
John McCall7f416cc2015-09-08 08:05:57 +0000454 Address OriginalAddr = EmitLValue(&DRE).getAddress();
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000455 QualType Type = OrigVD->getType();
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000456 if (Type->isArrayType()) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000457 // Emit VarDecl with copy init for arrays.
458 // Get the address of the original variable captured in current
459 // captured region.
John McCall7f416cc2015-09-08 08:05:57 +0000460 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000461 auto Emission = EmitAutoVarAlloca(*VD);
462 auto *Init = VD->getInit();
463 if (!isa<CXXConstructExpr>(Init) || isTrivialInitializer(Init)) {
464 // Perform simple memcpy.
465 EmitAggregateAssign(Emission.getAllocatedAddress(), OriginalAddr,
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000466 Type);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000467 } else {
468 EmitOMPAggregateAssign(
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000469 Emission.getAllocatedAddress(), OriginalAddr, Type,
John McCall7f416cc2015-09-08 08:05:57 +0000470 [this, VDInit, Init](Address DestElement,
471 Address SrcElement) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000472 // Clean up any temporaries needed by the initialization.
473 RunCleanupsScope InitScope(*this);
474 // Emit initialization for single element.
John McCall7f416cc2015-09-08 08:05:57 +0000475 setAddrOfLocalVar(VDInit, SrcElement);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000476 EmitAnyExprToMem(Init, DestElement,
477 Init->getType().getQualifiers(),
478 /*IsInitializer*/ false);
479 LocalDeclMap.erase(VDInit);
480 });
481 }
482 EmitAutoVarCleanups(Emission);
483 return Emission.getAllocatedAddress();
484 });
485 } else {
John McCall7f416cc2015-09-08 08:05:57 +0000486 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000487 // Emit private VarDecl with copy init.
488 // Remap temp VDInit variable to the address of the original
489 // variable
490 // (for proper handling of captured global variables).
John McCall7f416cc2015-09-08 08:05:57 +0000491 setAddrOfLocalVar(VDInit, OriginalAddr);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000492 EmitDecl(*VD);
493 LocalDeclMap.erase(VDInit);
494 return GetAddrOfLocalVar(VD);
495 });
496 }
497 assert(IsRegistered &&
498 "firstprivate var already registered as private");
499 // Silence the warning about unused variable.
500 (void)IsRegistered;
501 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000502 ++IRef;
503 ++InitsRef;
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000504 }
505 }
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000506 return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000507}
508
Alexey Bataev03b340a2014-10-21 03:16:40 +0000509void CodeGenFunction::EmitOMPPrivateClause(
510 const OMPExecutableDirective &D,
511 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000512 if (!HaveInsertPoint())
513 return;
Alexey Bataev50a64582015-04-22 12:24:45 +0000514 llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000515 for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) {
Alexey Bataev03b340a2014-10-21 03:16:40 +0000516 auto IRef = C->varlist_begin();
517 for (auto IInit : C->private_copies()) {
518 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev50a64582015-04-22 12:24:45 +0000519 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
520 auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
521 bool IsRegistered =
John McCall7f416cc2015-09-08 08:05:57 +0000522 PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev50a64582015-04-22 12:24:45 +0000523 // Emit private VarDecl with copy init.
524 EmitDecl(*VD);
525 return GetAddrOfLocalVar(VD);
526 });
527 assert(IsRegistered && "private var already registered as private");
528 // Silence the warning about unused variable.
529 (void)IsRegistered;
530 }
Alexey Bataev03b340a2014-10-21 03:16:40 +0000531 ++IRef;
532 }
533 }
534}
535
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000536bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000537 if (!HaveInsertPoint())
538 return false;
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000539 // threadprivate_var1 = master_threadprivate_var1;
540 // operator=(threadprivate_var2, master_threadprivate_var2);
541 // ...
542 // __kmpc_barrier(&loc, global_tid);
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000543 llvm::DenseSet<const VarDecl *> CopiedVars;
544 llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000545 for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) {
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000546 auto IRef = C->varlist_begin();
547 auto ISrcRef = C->source_exprs().begin();
548 auto IDestRef = C->destination_exprs().begin();
549 for (auto *AssignOp : C->assignment_ops()) {
550 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000551 QualType Type = VD->getType();
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000552 if (CopiedVars.insert(VD->getCanonicalDecl()).second) {
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000553 // Get the address of the master variable. If we are emitting code with
554 // TLS support, the address is passed from the master as field in the
555 // captured declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000556 Address MasterAddr = Address::invalid();
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000557 if (getLangOpts().OpenMPUseTLS &&
558 getContext().getTargetInfo().isTLSSupported()) {
559 assert(CapturedStmtInfo->lookup(VD) &&
560 "Copyin threadprivates should have been captured!");
561 DeclRefExpr DRE(const_cast<VarDecl *>(VD), true, (*IRef)->getType(),
562 VK_LValue, (*IRef)->getExprLoc());
563 MasterAddr = EmitLValue(&DRE).getAddress();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000564 LocalDeclMap.erase(VD);
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000565 } else {
John McCall7f416cc2015-09-08 08:05:57 +0000566 MasterAddr =
567 Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD)
568 : CGM.GetAddrOfGlobal(VD),
569 getContext().getDeclAlign(VD));
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000570 }
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000571 // Get the address of the threadprivate variable.
John McCall7f416cc2015-09-08 08:05:57 +0000572 Address PrivateAddr = EmitLValue(*IRef).getAddress();
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000573 if (CopiedVars.size() == 1) {
574 // At first check if current thread is a master thread. If it is, no
575 // need to copy data.
576 CopyBegin = createBasicBlock("copyin.not.master");
577 CopyEnd = createBasicBlock("copyin.not.master.end");
578 Builder.CreateCondBr(
579 Builder.CreateICmpNE(
John McCall7f416cc2015-09-08 08:05:57 +0000580 Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy),
581 Builder.CreatePtrToInt(PrivateAddr.getPointer(), CGM.IntPtrTy)),
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000582 CopyBegin, CopyEnd);
583 EmitBlock(CopyBegin);
584 }
585 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
586 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +0000587 EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp);
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000588 }
589 ++IRef;
590 ++ISrcRef;
591 ++IDestRef;
592 }
593 }
594 if (CopyEnd) {
595 // Exit out of copying procedure for non-master thread.
596 EmitBlock(CopyEnd, /*IsFinished=*/true);
597 return true;
598 }
599 return false;
600}
601
Alexey Bataev38e89532015-04-16 04:54:05 +0000602bool CodeGenFunction::EmitOMPLastprivateClauseInit(
603 const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000604 if (!HaveInsertPoint())
605 return false;
Alexey Bataev38e89532015-04-16 04:54:05 +0000606 bool HasAtLeastOneLastprivate = false;
607 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000608 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
Alexey Bataevd130fd12015-05-13 10:23:02 +0000609 HasAtLeastOneLastprivate = true;
Alexey Bataev38e89532015-04-16 04:54:05 +0000610 auto IRef = C->varlist_begin();
611 auto IDestRef = C->destination_exprs().begin();
612 for (auto *IInit : C->private_copies()) {
613 // Keep the address of the original variable for future update at the end
614 // of the loop.
615 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
616 if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) {
617 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +0000618 PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() -> Address {
Alexey Bataev38e89532015-04-16 04:54:05 +0000619 DeclRefExpr DRE(
620 const_cast<VarDecl *>(OrigVD),
621 /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup(
622 OrigVD) != nullptr,
623 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
624 return EmitLValue(&DRE).getAddress();
625 });
626 // Check if the variable is also a firstprivate: in this case IInit is
627 // not generated. Initialization of this variable will happen in codegen
628 // for 'firstprivate' clause.
Alexey Bataevd130fd12015-05-13 10:23:02 +0000629 if (IInit) {
630 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
631 bool IsRegistered =
John McCall7f416cc2015-09-08 08:05:57 +0000632 PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataevd130fd12015-05-13 10:23:02 +0000633 // Emit private VarDecl with copy init.
634 EmitDecl(*VD);
635 return GetAddrOfLocalVar(VD);
636 });
637 assert(IsRegistered &&
638 "lastprivate var already registered as private");
639 (void)IsRegistered;
640 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000641 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000642 ++IRef;
643 ++IDestRef;
Alexey Bataev38e89532015-04-16 04:54:05 +0000644 }
645 }
646 return HasAtLeastOneLastprivate;
647}
648
649void CodeGenFunction::EmitOMPLastprivateClauseFinal(
650 const OMPExecutableDirective &D, llvm::Value *IsLastIterCond) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000651 if (!HaveInsertPoint())
652 return;
Alexey Bataev38e89532015-04-16 04:54:05 +0000653 // Emit following code:
654 // if (<IsLastIterCond>) {
655 // orig_var1 = private_orig_var1;
656 // ...
657 // orig_varn = private_orig_varn;
658 // }
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000659 llvm::BasicBlock *ThenBB = nullptr;
660 llvm::BasicBlock *DoneBB = nullptr;
661 if (IsLastIterCond) {
662 ThenBB = createBasicBlock(".omp.lastprivate.then");
663 DoneBB = createBasicBlock(".omp.lastprivate.done");
664 Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB);
665 EmitBlock(ThenBB);
666 }
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000667 llvm::DenseMap<const Decl *, const Expr *> LoopCountersAndUpdates;
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000668 if (auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) {
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000669 auto IC = LoopDirective->counters().begin();
670 for (auto F : LoopDirective->finals()) {
671 auto *D = cast<DeclRefExpr>(*IC)->getDecl()->getCanonicalDecl();
672 LoopCountersAndUpdates[D] = F;
673 ++IC;
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000674 }
675 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000676 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
677 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
678 auto IRef = C->varlist_begin();
679 auto ISrcRef = C->source_exprs().begin();
680 auto IDestRef = C->destination_exprs().begin();
681 for (auto *AssignOp : C->assignment_ops()) {
682 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
683 QualType Type = PrivateVD->getType();
684 auto *CanonicalVD = PrivateVD->getCanonicalDecl();
685 if (AlreadyEmittedVars.insert(CanonicalVD).second) {
686 // If lastprivate variable is a loop control variable for loop-based
687 // directive, update its value before copyin back to original
688 // variable.
689 if (auto *UpExpr = LoopCountersAndUpdates.lookup(CanonicalVD))
690 EmitIgnoredExpr(UpExpr);
691 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
692 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
693 // Get the address of the original variable.
694 Address OriginalAddr = GetAddrOfLocalVar(DestVD);
695 // Get the address of the private variable.
696 Address PrivateAddr = GetAddrOfLocalVar(PrivateVD);
697 if (auto RefTy = PrivateVD->getType()->getAs<ReferenceType>())
698 PrivateAddr =
John McCall7f416cc2015-09-08 08:05:57 +0000699 Address(Builder.CreateLoad(PrivateAddr),
700 getNaturalTypeAlignment(RefTy->getPointeeType()));
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000701 EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp);
Alexey Bataev38e89532015-04-16 04:54:05 +0000702 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000703 ++IRef;
704 ++ISrcRef;
705 ++IDestRef;
Alexey Bataev38e89532015-04-16 04:54:05 +0000706 }
Alexey Bataev005248a2016-02-25 05:25:57 +0000707 if (auto *PostUpdate = C->getPostUpdateExpr())
708 EmitIgnoredExpr(PostUpdate);
Alexey Bataev38e89532015-04-16 04:54:05 +0000709 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000710 if (IsLastIterCond)
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000711 EmitBlock(DoneBB, /*IsFinished=*/true);
Alexey Bataev38e89532015-04-16 04:54:05 +0000712}
713
Alexey Bataev31300ed2016-02-04 11:27:03 +0000714static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
715 LValue BaseLV, llvm::Value *Addr) {
716 Address Tmp = Address::invalid();
717 Address TopTmp = Address::invalid();
718 Address MostTopTmp = Address::invalid();
719 BaseTy = BaseTy.getNonReferenceType();
720 while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) &&
721 !CGF.getContext().hasSameType(BaseTy, ElTy)) {
722 Tmp = CGF.CreateMemTemp(BaseTy);
723 if (TopTmp.isValid())
724 CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp);
725 else
726 MostTopTmp = Tmp;
727 TopTmp = Tmp;
728 BaseTy = BaseTy->getPointeeType();
729 }
730 llvm::Type *Ty = BaseLV.getPointer()->getType();
731 if (Tmp.isValid())
732 Ty = Tmp.getElementType();
733 Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty);
734 if (Tmp.isValid()) {
735 CGF.Builder.CreateStore(Addr, Tmp);
736 return MostTopTmp;
737 }
738 return Address(Addr, BaseLV.getAlignment());
739}
740
741static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
742 LValue BaseLV) {
743 BaseTy = BaseTy.getNonReferenceType();
744 while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) &&
745 !CGF.getContext().hasSameType(BaseTy, ElTy)) {
746 if (auto *PtrTy = BaseTy->getAs<PointerType>())
747 BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy);
748 else {
749 BaseLV = CGF.EmitLoadOfReferenceLValue(BaseLV.getAddress(),
750 BaseTy->castAs<ReferenceType>());
751 }
752 BaseTy = BaseTy->getPointeeType();
753 }
754 return CGF.MakeAddrLValue(
755 Address(
756 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
757 BaseLV.getPointer(), CGF.ConvertTypeForMem(ElTy)->getPointerTo()),
758 BaseLV.getAlignment()),
759 BaseLV.getType(), BaseLV.getAlignmentSource());
760}
761
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000762void CodeGenFunction::EmitOMPReductionClauseInit(
763 const OMPExecutableDirective &D,
764 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000765 if (!HaveInsertPoint())
766 return;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000767 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000768 auto ILHS = C->lhs_exprs().begin();
769 auto IRHS = C->rhs_exprs().begin();
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000770 auto IPriv = C->privates().begin();
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000771 for (auto IRef : C->varlists()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000772 auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000773 auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
774 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl());
775 if (auto *OASE = dyn_cast<OMPArraySectionExpr>(IRef)) {
776 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
777 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
778 Base = TempOASE->getBase()->IgnoreParenImpCasts();
779 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
780 Base = TempASE->getBase()->IgnoreParenImpCasts();
781 auto *DE = cast<DeclRefExpr>(Base);
782 auto *OrigVD = cast<VarDecl>(DE->getDecl());
783 auto OASELValueLB = EmitOMPArraySectionExpr(OASE);
784 auto OASELValueUB =
785 EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false);
786 auto OriginalBaseLValue = EmitLValue(DE);
Alexey Bataev31300ed2016-02-04 11:27:03 +0000787 LValue BaseLValue =
788 loadToBegin(*this, OrigVD->getType(), OASELValueLB.getType(),
789 OriginalBaseLValue);
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000790 // Store the address of the original variable associated with the LHS
791 // implicit variable.
792 PrivateScope.addPrivate(LHSVD, [this, OASELValueLB]() -> Address {
793 return OASELValueLB.getAddress();
794 });
795 // Emit reduction copy.
796 bool IsRegistered = PrivateScope.addPrivate(
Alexey Bataev31300ed2016-02-04 11:27:03 +0000797 OrigVD, [this, OrigVD, PrivateVD, BaseLValue, OASELValueLB,
798 OASELValueUB, OriginalBaseLValue]() -> Address {
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000799 // Emit VarDecl with copy init for arrays.
800 // Get the address of the original variable captured in current
801 // captured region.
802 auto *Size = Builder.CreatePtrDiff(OASELValueUB.getPointer(),
803 OASELValueLB.getPointer());
804 Size = Builder.CreateNUWAdd(
805 Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1));
806 CodeGenFunction::OpaqueValueMapping OpaqueMap(
807 *this, cast<OpaqueValueExpr>(
808 getContext()
809 .getAsVariableArrayType(PrivateVD->getType())
810 ->getSizeExpr()),
811 RValue::get(Size));
812 EmitVariablyModifiedType(PrivateVD->getType());
813 auto Emission = EmitAutoVarAlloca(*PrivateVD);
814 auto Addr = Emission.getAllocatedAddress();
815 auto *Init = PrivateVD->getInit();
816 EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(), Init);
817 EmitAutoVarCleanups(Emission);
818 // Emit private VarDecl with reduction init.
819 auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(),
820 OASELValueLB.getPointer());
821 auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset);
Alexey Bataev31300ed2016-02-04 11:27:03 +0000822 return castToBase(*this, OrigVD->getType(),
823 OASELValueLB.getType(), OriginalBaseLValue,
824 Ptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000825 });
826 assert(IsRegistered && "private var already registered as private");
827 // Silence the warning about unused variable.
828 (void)IsRegistered;
829 PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address {
830 return GetAddrOfLocalVar(PrivateVD);
831 });
832 } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(IRef)) {
833 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
834 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
835 Base = TempASE->getBase()->IgnoreParenImpCasts();
836 auto *DE = cast<DeclRefExpr>(Base);
837 auto *OrigVD = cast<VarDecl>(DE->getDecl());
838 auto ASELValue = EmitLValue(ASE);
839 auto OriginalBaseLValue = EmitLValue(DE);
Alexey Bataev31300ed2016-02-04 11:27:03 +0000840 LValue BaseLValue = loadToBegin(
841 *this, OrigVD->getType(), ASELValue.getType(), OriginalBaseLValue);
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000842 // Store the address of the original variable associated with the LHS
843 // implicit variable.
844 PrivateScope.addPrivate(LHSVD, [this, ASELValue]() -> Address {
845 return ASELValue.getAddress();
846 });
847 // Emit reduction copy.
848 bool IsRegistered = PrivateScope.addPrivate(
Alexey Bataev31300ed2016-02-04 11:27:03 +0000849 OrigVD, [this, OrigVD, PrivateVD, BaseLValue, ASELValue,
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000850 OriginalBaseLValue]() -> Address {
851 // Emit private VarDecl with reduction init.
852 EmitDecl(*PrivateVD);
853 auto Addr = GetAddrOfLocalVar(PrivateVD);
854 auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(),
855 ASELValue.getPointer());
856 auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset);
Alexey Bataev31300ed2016-02-04 11:27:03 +0000857 return castToBase(*this, OrigVD->getType(), ASELValue.getType(),
858 OriginalBaseLValue, Ptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000859 });
860 assert(IsRegistered && "private var already registered as private");
861 // Silence the warning about unused variable.
862 (void)IsRegistered;
Alexey Bataev1189bd02016-01-26 12:20:39 +0000863 PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address {
864 return Builder.CreateElementBitCast(
865 GetAddrOfLocalVar(PrivateVD), ConvertTypeForMem(RHSVD->getType()),
866 "rhs.begin");
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000867 });
868 } else {
869 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl());
Alexey Bataev1189bd02016-01-26 12:20:39 +0000870 QualType Type = PrivateVD->getType();
871 if (getContext().getAsArrayType(Type)) {
872 // Store the address of the original variable associated with the LHS
873 // implicit variable.
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000874 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
875 CapturedStmtInfo->lookup(OrigVD) != nullptr,
876 IRef->getType(), VK_LValue, IRef->getExprLoc());
Alexey Bataev1189bd02016-01-26 12:20:39 +0000877 Address OriginalAddr = EmitLValue(&DRE).getAddress();
878 PrivateScope.addPrivate(LHSVD, [this, OriginalAddr,
879 LHSVD]() -> Address {
880 return Builder.CreateElementBitCast(
881 OriginalAddr, ConvertTypeForMem(LHSVD->getType()),
882 "lhs.begin");
883 });
884 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
885 if (Type->isVariablyModifiedType()) {
886 CodeGenFunction::OpaqueValueMapping OpaqueMap(
887 *this, cast<OpaqueValueExpr>(
888 getContext()
889 .getAsVariableArrayType(PrivateVD->getType())
890 ->getSizeExpr()),
891 RValue::get(
892 getTypeSize(OrigVD->getType().getNonReferenceType())));
893 EmitVariablyModifiedType(Type);
894 }
895 auto Emission = EmitAutoVarAlloca(*PrivateVD);
896 auto Addr = Emission.getAllocatedAddress();
897 auto *Init = PrivateVD->getInit();
898 EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(), Init);
899 EmitAutoVarCleanups(Emission);
900 return Emission.getAllocatedAddress();
901 });
902 assert(IsRegistered && "private var already registered as private");
903 // Silence the warning about unused variable.
904 (void)IsRegistered;
905 PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address {
906 return Builder.CreateElementBitCast(
907 GetAddrOfLocalVar(PrivateVD),
908 ConvertTypeForMem(RHSVD->getType()), "rhs.begin");
909 });
910 } else {
911 // Store the address of the original variable associated with the LHS
912 // implicit variable.
913 PrivateScope.addPrivate(LHSVD, [this, OrigVD, IRef]() -> Address {
914 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
915 CapturedStmtInfo->lookup(OrigVD) != nullptr,
916 IRef->getType(), VK_LValue, IRef->getExprLoc());
917 return EmitLValue(&DRE).getAddress();
918 });
919 // Emit reduction copy.
920 bool IsRegistered =
921 PrivateScope.addPrivate(OrigVD, [this, PrivateVD]() -> Address {
922 // Emit private VarDecl with reduction init.
923 EmitDecl(*PrivateVD);
924 return GetAddrOfLocalVar(PrivateVD);
925 });
926 assert(IsRegistered && "private var already registered as private");
927 // Silence the warning about unused variable.
928 (void)IsRegistered;
929 PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address {
930 return GetAddrOfLocalVar(PrivateVD);
931 });
932 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000933 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000934 ++ILHS;
935 ++IRHS;
936 ++IPriv;
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000937 }
938 }
939}
940
941void CodeGenFunction::EmitOMPReductionClauseFinal(
942 const OMPExecutableDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000943 if (!HaveInsertPoint())
944 return;
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000945 llvm::SmallVector<const Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000946 llvm::SmallVector<const Expr *, 8> LHSExprs;
947 llvm::SmallVector<const Expr *, 8> RHSExprs;
948 llvm::SmallVector<const Expr *, 8> ReductionOps;
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000949 bool HasAtLeastOneReduction = false;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000950 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000951 HasAtLeastOneReduction = true;
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000952 Privates.append(C->privates().begin(), C->privates().end());
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000953 LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end());
954 RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end());
955 ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end());
956 }
957 if (HasAtLeastOneReduction) {
958 // Emit nowait reduction if nowait clause is present or directive is a
959 // parallel directive (it always has implicit barrier).
960 CGM.getOpenMPRuntime().emitReduction(
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000961 *this, D.getLocEnd(), Privates, LHSExprs, RHSExprs, ReductionOps,
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000962 D.getSingleClause<OMPNowaitClause>() ||
Alexey Bataev89e7e8e2015-06-17 06:21:39 +0000963 isOpenMPParallelDirective(D.getDirectiveKind()) ||
964 D.getDirectiveKind() == OMPD_simd,
965 D.getDirectiveKind() == OMPD_simd);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000966 }
967}
968
Alexey Bataev61205072016-03-02 04:57:40 +0000969static void emitPostUpdateForReductionClause(
970 CodeGenFunction &CGF, const OMPExecutableDirective &D,
971 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
972 if (!CGF.HaveInsertPoint())
973 return;
974 llvm::BasicBlock *DoneBB = nullptr;
975 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
976 if (auto *PostUpdate = C->getPostUpdateExpr()) {
977 if (!DoneBB) {
978 if (auto *Cond = CondGen(CGF)) {
979 // If the first post-update expression is found, emit conditional
980 // block if it was requested.
981 auto *ThenBB = CGF.createBasicBlock(".omp.reduction.pu");
982 DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done");
983 CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB);
984 CGF.EmitBlock(ThenBB);
985 }
986 }
987 CGF.EmitIgnoredExpr(PostUpdate);
988 }
989 }
990 if (DoneBB)
991 CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
992}
993
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000994static void emitCommonOMPParallelDirective(CodeGenFunction &CGF,
995 const OMPExecutableDirective &S,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000996 OpenMPDirectiveKind InnermostKind,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000997 const RegionCodeGenTy &CodeGen) {
Alexey Bataev18095712014-10-10 12:19:54 +0000998 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
Alexey Bataev2377fe92015-09-10 08:12:02 +0000999 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
1000 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00001001 auto OutlinedFn = CGF.CGM.getOpenMPRuntime().
1002 emitParallelOrTeamsOutlinedFunction(S,
1003 *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001004 if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) {
Alexey Bataev1d677132015-04-22 13:57:31 +00001005 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
Alexey Bataev1d677132015-04-22 13:57:31 +00001006 auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
1007 /*IgnoreResultAssign*/ true);
1008 CGF.CGM.getOpenMPRuntime().emitNumThreadsClause(
1009 CGF, NumThreads, NumThreadsClause->getLocStart());
1010 }
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001011 if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) {
Alexey Bataev7f210c62015-06-18 13:40:03 +00001012 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
Alexey Bataev7f210c62015-06-18 13:40:03 +00001013 CGF.CGM.getOpenMPRuntime().emitProcBindClause(
1014 CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getLocStart());
1015 }
Alexey Bataev1d677132015-04-22 13:57:31 +00001016 const Expr *IfCond = nullptr;
Alexey Bataev7371aa32015-09-03 08:45:56 +00001017 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
1018 if (C->getNameModifier() == OMPD_unknown ||
1019 C->getNameModifier() == OMPD_parallel) {
1020 IfCond = C->getCondition();
1021 break;
1022 }
Alexey Bataev1d677132015-04-22 13:57:31 +00001023 }
1024 CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +00001025 CapturedVars, IfCond);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001026}
1027
1028void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
Alexey Bataev3392d762016-02-16 11:18:12 +00001029 OMPLexicalScope Scope(*this, S);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001030 // Emit parallel region as a standalone region.
1031 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1032 OMPPrivateScope PrivateScope(CGF);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001033 bool Copyins = CGF.EmitOMPCopyinClause(S);
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00001034 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
1035 if (Copyins) {
Alexey Bataev69c62a92015-04-15 04:52:20 +00001036 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00001037 // propagation master's thread values of threadprivate variables to local
1038 // instances of that variables of all other implicit threads.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001039 CGF.CGM.getOpenMPRuntime().emitBarrierCall(
1040 CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
1041 /*ForceSimpleCall=*/true);
Alexey Bataev69c62a92015-04-15 04:52:20 +00001042 }
1043 CGF.EmitOMPPrivateClause(S, PrivateScope);
1044 CGF.EmitOMPReductionClauseInit(S, PrivateScope);
1045 (void)PrivateScope.Privatize();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001046 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001047 CGF.EmitOMPReductionClauseFinal(S);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001048 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001049 emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen);
Alexey Bataev61205072016-03-02 04:57:40 +00001050 emitPostUpdateForReductionClause(
1051 *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev9959db52014-05-06 10:08:46 +00001052}
Alexander Musman515ad8c2014-05-22 08:54:05 +00001053
Alexey Bataev0f34da12015-07-02 04:17:07 +00001054void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D,
1055 JumpDest LoopExit) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00001056 RunCleanupsScope BodyScope(*this);
1057 // Update counters values on current iteration.
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001058 for (auto I : D.updates()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00001059 EmitIgnoredExpr(I);
1060 }
Alexander Musman3276a272015-03-21 10:12:56 +00001061 // Update the linear variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001062 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexander Musman3276a272015-03-21 10:12:56 +00001063 for (auto U : C->updates()) {
1064 EmitIgnoredExpr(U);
1065 }
1066 }
1067
Alexander Musmana5f070a2014-10-01 06:03:56 +00001068 // On a continue in the body, jump to the end.
Alexander Musmand196ef22014-10-07 08:57:09 +00001069 auto Continue = getJumpDestInCurrentScope("omp.body.continue");
Alexey Bataev0f34da12015-07-02 04:17:07 +00001070 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
Alexander Musmana5f070a2014-10-01 06:03:56 +00001071 // Emit loop body.
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001072 EmitStmt(D.getBody());
Alexander Musmana5f070a2014-10-01 06:03:56 +00001073 // The end (updates/cleanups).
1074 EmitBlock(Continue.getBlock());
1075 BreakContinueStack.pop_back();
Alexander Musmana5f070a2014-10-01 06:03:56 +00001076}
1077
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001078void CodeGenFunction::EmitOMPInnerLoop(
1079 const Stmt &S, bool RequiresCleanup, const Expr *LoopCond,
1080 const Expr *IncExpr,
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001081 const llvm::function_ref<void(CodeGenFunction &)> &BodyGen,
1082 const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) {
Alexander Musmand196ef22014-10-07 08:57:09 +00001083 auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001084
1085 // Start the loop with a block that tests the condition.
Alexander Musmand196ef22014-10-07 08:57:09 +00001086 auto CondBlock = createBasicBlock("omp.inner.for.cond");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001087 EmitBlock(CondBlock);
1088 LoopStack.push(CondBlock);
1089
1090 // If there are any cleanups between here and the loop-exit scope,
1091 // create a block to stage a loop exit along.
1092 auto ExitBlock = LoopExit.getBlock();
Alexey Bataev2df54a02015-03-12 08:53:29 +00001093 if (RequiresCleanup)
Alexander Musmand196ef22014-10-07 08:57:09 +00001094 ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001095
Alexander Musmand196ef22014-10-07 08:57:09 +00001096 auto LoopBody = createBasicBlock("omp.inner.for.body");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001097
Alexey Bataev2df54a02015-03-12 08:53:29 +00001098 // Emit condition.
Justin Bogner66242d62015-04-23 23:06:47 +00001099 EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S));
Alexander Musmana5f070a2014-10-01 06:03:56 +00001100 if (ExitBlock != LoopExit.getBlock()) {
1101 EmitBlock(ExitBlock);
1102 EmitBranchThroughCleanup(LoopExit);
1103 }
1104
1105 EmitBlock(LoopBody);
Justin Bogner66242d62015-04-23 23:06:47 +00001106 incrementProfileCounter(&S);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001107
1108 // Create a block for the increment.
Alexander Musmand196ef22014-10-07 08:57:09 +00001109 auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001110 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1111
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001112 BodyGen(*this);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001113
1114 // Emit "IV = IV + 1" and a back-edge to the condition block.
1115 EmitBlock(Continue.getBlock());
Alexey Bataev2df54a02015-03-12 08:53:29 +00001116 EmitIgnoredExpr(IncExpr);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001117 PostIncGen(*this);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001118 BreakContinueStack.pop_back();
1119 EmitBranch(CondBlock);
1120 LoopStack.pop();
1121 // Emit the fall-through block.
1122 EmitBlock(LoopExit.getBlock());
1123}
1124
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001125void CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001126 if (!HaveInsertPoint())
1127 return;
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001128 // Emit inits for the linear variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001129 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001130 for (auto Init : C->inits()) {
1131 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl());
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001132 auto *OrigVD = cast<VarDecl>(
1133 cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())->getDecl());
1134 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
1135 CapturedStmtInfo->lookup(OrigVD) != nullptr,
1136 VD->getInit()->getType(), VK_LValue,
1137 VD->getInit()->getExprLoc());
1138 AutoVarEmission Emission = EmitAutoVarAlloca(*VD);
1139 EmitExprAsInit(&DRE, VD,
John McCall7f416cc2015-09-08 08:05:57 +00001140 MakeAddrLValue(Emission.getAllocatedAddress(), VD->getType()),
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001141 /*capturedByInit=*/false);
1142 EmitAutoVarCleanups(Emission);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001143 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001144 // Emit the linear steps for the linear clauses.
1145 // If a step is not constant, it is pre-calculated before the loop.
1146 if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep()))
1147 if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) {
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001148 EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl()));
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001149 // Emit calculation of the linear step.
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001150 EmitIgnoredExpr(CS);
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001151 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001152 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001153}
1154
1155static void emitLinearClauseFinal(CodeGenFunction &CGF,
1156 const OMPLoopDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001157 if (!CGF.HaveInsertPoint())
1158 return;
Alexander Musman3276a272015-03-21 10:12:56 +00001159 // Emit the final values of the linear variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001160 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataev39f915b82015-05-08 10:41:21 +00001161 auto IC = C->varlist_begin();
Alexander Musman3276a272015-03-21 10:12:56 +00001162 for (auto F : C->finals()) {
Alexey Bataev39f915b82015-05-08 10:41:21 +00001163 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl());
1164 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001165 CGF.CapturedStmtInfo->lookup(OrigVD) != nullptr,
Alexey Bataev39f915b82015-05-08 10:41:21 +00001166 (*IC)->getType(), VK_LValue, (*IC)->getExprLoc());
John McCall7f416cc2015-09-08 08:05:57 +00001167 Address OrigAddr = CGF.EmitLValue(&DRE).getAddress();
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001168 CodeGenFunction::OMPPrivateScope VarScope(CGF);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001169 VarScope.addPrivate(OrigVD,
John McCall7f416cc2015-09-08 08:05:57 +00001170 [OrigAddr]() -> Address { return OrigAddr; });
Alexey Bataev39f915b82015-05-08 10:41:21 +00001171 (void)VarScope.Privatize();
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001172 CGF.EmitIgnoredExpr(F);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001173 ++IC;
Alexander Musman3276a272015-03-21 10:12:56 +00001174 }
1175 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001176}
1177
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001178static void emitAlignedClause(CodeGenFunction &CGF,
1179 const OMPExecutableDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001180 if (!CGF.HaveInsertPoint())
1181 return;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001182 for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001183 unsigned ClauseAlignment = 0;
1184 if (auto AlignmentExpr = Clause->getAlignment()) {
1185 auto AlignmentCI =
1186 cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr));
1187 ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue());
Alexander Musman09184fe2014-09-30 05:29:28 +00001188 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001189 for (auto E : Clause->varlists()) {
1190 unsigned Alignment = ClauseAlignment;
1191 if (Alignment == 0) {
1192 // OpenMP [2.8.1, Description]
1193 // If no optional parameter is specified, implementation-defined default
1194 // alignments for SIMD instructions on the target platforms are assumed.
1195 Alignment =
Alexey Bataev00396512015-07-02 03:40:19 +00001196 CGF.getContext()
1197 .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign(
1198 E->getType()->getPointeeType()))
1199 .getQuantity();
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001200 }
1201 assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) &&
1202 "alignment is not power of 2");
1203 if (Alignment != 0) {
1204 llvm::Value *PtrValue = CGF.EmitScalarExpr(E);
1205 CGF.EmitAlignmentAssumption(PtrValue, Alignment);
1206 }
Alexander Musman09184fe2014-09-30 05:29:28 +00001207 }
1208 }
1209}
1210
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001211static void emitPrivateLoopCounters(CodeGenFunction &CGF,
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001212 CodeGenFunction::OMPPrivateScope &LoopScope,
Alexey Bataeva8899172015-08-06 12:30:57 +00001213 ArrayRef<Expr *> Counters,
1214 ArrayRef<Expr *> PrivateCounters) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001215 if (!CGF.HaveInsertPoint())
1216 return;
Alexey Bataeva8899172015-08-06 12:30:57 +00001217 auto I = PrivateCounters.begin();
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001218 for (auto *E : Counters) {
Alexey Bataeva8899172015-08-06 12:30:57 +00001219 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1220 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +00001221 Address Addr = Address::invalid();
1222 (void)LoopScope.addPrivate(PrivateVD, [&]() -> Address {
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001223 // Emit var without initialization.
Alexey Bataeva8899172015-08-06 12:30:57 +00001224 auto VarEmission = CGF.EmitAutoVarAlloca(*PrivateVD);
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001225 CGF.EmitAutoVarCleanups(VarEmission);
Alexey Bataeva8899172015-08-06 12:30:57 +00001226 Addr = VarEmission.getAllocatedAddress();
1227 return Addr;
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001228 });
John McCall7f416cc2015-09-08 08:05:57 +00001229 (void)LoopScope.addPrivate(VD, [&]() -> Address { return Addr; });
Alexey Bataeva8899172015-08-06 12:30:57 +00001230 ++I;
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001231 }
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001232}
1233
Alexey Bataev62dbb972015-04-22 11:59:37 +00001234static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S,
1235 const Expr *Cond, llvm::BasicBlock *TrueBlock,
1236 llvm::BasicBlock *FalseBlock, uint64_t TrueCount) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001237 if (!CGF.HaveInsertPoint())
1238 return;
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001239 {
1240 CodeGenFunction::OMPPrivateScope PreCondScope(CGF);
Alexey Bataeva8899172015-08-06 12:30:57 +00001241 emitPrivateLoopCounters(CGF, PreCondScope, S.counters(),
1242 S.private_counters());
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001243 (void)PreCondScope.Privatize();
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001244 // Get initial values of real counters.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00001245 for (auto I : S.inits()) {
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001246 CGF.EmitIgnoredExpr(I);
1247 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00001248 }
1249 // Check that loop is executed at least one time.
1250 CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount);
1251}
1252
Alexander Musman3276a272015-03-21 10:12:56 +00001253static void
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001254emitPrivateLinearVars(CodeGenFunction &CGF, const OMPExecutableDirective &D,
Alexander Musman3276a272015-03-21 10:12:56 +00001255 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001256 if (!CGF.HaveInsertPoint())
1257 return;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001258 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001259 auto CurPrivate = C->privates().begin();
Alexey Bataevc925aa32015-04-27 08:00:32 +00001260 for (auto *E : C->varlists()) {
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001261 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1262 auto *PrivateVD =
1263 cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +00001264 bool IsRegistered = PrivateScope.addPrivate(VD, [&]() -> Address {
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001265 // Emit private VarDecl with copy init.
1266 CGF.EmitVarDecl(*PrivateVD);
1267 return CGF.GetAddrOfLocalVar(PrivateVD);
Alexander Musman3276a272015-03-21 10:12:56 +00001268 });
1269 assert(IsRegistered && "linear var already registered as private");
1270 // Silence the warning about unused variable.
1271 (void)IsRegistered;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001272 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00001273 }
1274 }
1275}
1276
Alexey Bataev45bfad52015-08-21 12:19:04 +00001277static void emitSimdlenSafelenClause(CodeGenFunction &CGF,
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001278 const OMPExecutableDirective &D,
1279 bool IsMonotonic) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001280 if (!CGF.HaveInsertPoint())
1281 return;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001282 if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) {
Alexey Bataev45bfad52015-08-21 12:19:04 +00001283 RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(),
1284 /*ignoreResult=*/true);
1285 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
1286 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
1287 // In presence of finite 'safelen', it may be unsafe to mark all
1288 // the memory instructions parallel, because loop-carried
1289 // dependences of 'safelen' iterations are possible.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001290 if (!IsMonotonic)
1291 CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>());
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001292 } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001293 RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(),
1294 /*ignoreResult=*/true);
1295 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
Tyler Nowickida46d0e2015-07-14 23:03:09 +00001296 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001297 // In presence of finite 'safelen', it may be unsafe to mark all
1298 // the memory instructions parallel, because loop-carried
1299 // dependences of 'safelen' iterations are possible.
1300 CGF.LoopStack.setParallel(false);
1301 }
1302}
1303
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001304void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D,
1305 bool IsMonotonic) {
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001306 // Walk clauses and process safelen/lastprivate.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001307 LoopStack.setParallel(!IsMonotonic);
Tyler Nowickida46d0e2015-07-14 23:03:09 +00001308 LoopStack.setVectorizeEnable(true);
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001309 emitSimdlenSafelenClause(*this, D, IsMonotonic);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001310}
1311
1312void CodeGenFunction::EmitOMPSimdFinal(const OMPLoopDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001313 if (!HaveInsertPoint())
1314 return;
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001315 auto IC = D.counters().begin();
1316 for (auto F : D.finals()) {
1317 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +00001318 if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD)) {
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001319 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
1320 CapturedStmtInfo->lookup(OrigVD) != nullptr,
1321 (*IC)->getType(), VK_LValue, (*IC)->getExprLoc());
John McCall7f416cc2015-09-08 08:05:57 +00001322 Address OrigAddr = EmitLValue(&DRE).getAddress();
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001323 OMPPrivateScope VarScope(*this);
1324 VarScope.addPrivate(OrigVD,
John McCall7f416cc2015-09-08 08:05:57 +00001325 [OrigAddr]() -> Address { return OrigAddr; });
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001326 (void)VarScope.Privatize();
1327 EmitIgnoredExpr(F);
1328 }
1329 ++IC;
1330 }
1331 emitLinearClauseFinal(*this, D);
1332}
1333
Alexander Musman515ad8c2014-05-22 08:54:05 +00001334void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001335 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00001336 // if (PreCond) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001337 // for (IV in 0..LastIteration) BODY;
1338 // <Final counter/linear vars updates>;
Alexey Bataev62dbb972015-04-22 11:59:37 +00001339 // }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001340 //
Alexander Musmana5f070a2014-10-01 06:03:56 +00001341
Alexey Bataev62dbb972015-04-22 11:59:37 +00001342 // Emit: if (PreCond) - begin.
1343 // If the condition constant folds and can be elided, avoid emitting the
1344 // whole loop.
1345 bool CondConstant;
1346 llvm::BasicBlock *ContBlock = nullptr;
1347 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
1348 if (!CondConstant)
1349 return;
1350 } else {
Alexey Bataev62dbb972015-04-22 11:59:37 +00001351 auto *ThenBlock = CGF.createBasicBlock("simd.if.then");
1352 ContBlock = CGF.createBasicBlock("simd.if.end");
Justin Bogner66242d62015-04-23 23:06:47 +00001353 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
1354 CGF.getProfileCount(&S));
Alexey Bataev62dbb972015-04-22 11:59:37 +00001355 CGF.EmitBlock(ThenBlock);
Justin Bogner66242d62015-04-23 23:06:47 +00001356 CGF.incrementProfileCounter(&S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001357 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001358
1359 // Emit the loop iteration variable.
1360 const Expr *IVExpr = S.getIterationVariable();
1361 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
1362 CGF.EmitVarDecl(*IVDecl);
1363 CGF.EmitIgnoredExpr(S.getInit());
1364
1365 // Emit the iterations count variable.
1366 // If it is not a variable, Sema decided to calculate iterations count on
Alexey Bataev7a228ff2015-05-21 07:59:51 +00001367 // each iteration (e.g., it is foldable into a constant).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001368 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
1369 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
1370 // Emit calculation of the iterations count.
1371 CGF.EmitIgnoredExpr(S.getCalcLastIteration());
Alexander Musmana5f070a2014-10-01 06:03:56 +00001372 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001373
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001374 CGF.EmitOMPSimdInit(S);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001375
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001376 emitAlignedClause(CGF, S);
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001377 CGF.EmitOMPLinearClauseInit(S);
Alexey Bataevfc087ec2015-06-16 13:14:42 +00001378 bool HasLastprivateClause;
Alexey Bataev62dbb972015-04-22 11:59:37 +00001379 {
1380 OMPPrivateScope LoopScope(CGF);
Alexey Bataeva8899172015-08-06 12:30:57 +00001381 emitPrivateLoopCounters(CGF, LoopScope, S.counters(),
1382 S.private_counters());
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001383 emitPrivateLinearVars(CGF, S, LoopScope);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001384 CGF.EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00001385 CGF.EmitOMPReductionClauseInit(S, LoopScope);
Alexey Bataevfc087ec2015-06-16 13:14:42 +00001386 HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001387 (void)LoopScope.Privatize();
Alexey Bataev0f34da12015-07-02 04:17:07 +00001388 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
1389 S.getInc(),
Alexey Bataev62dbb972015-04-22 11:59:37 +00001390 [&S](CodeGenFunction &CGF) {
Alexey Bataev0f34da12015-07-02 04:17:07 +00001391 CGF.EmitOMPLoopBody(S, JumpDest());
Alexey Bataev62dbb972015-04-22 11:59:37 +00001392 CGF.EmitStopPoint(&S);
1393 },
1394 [](CodeGenFunction &) {});
Alexey Bataevfc087ec2015-06-16 13:14:42 +00001395 // Emit final copy of the lastprivate variables at the end of loops.
1396 if (HasLastprivateClause) {
1397 CGF.EmitOMPLastprivateClauseFinal(S);
1398 }
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00001399 CGF.EmitOMPReductionClauseFinal(S);
Alexey Bataev61205072016-03-02 04:57:40 +00001400 emitPostUpdateForReductionClause(
1401 CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev62dbb972015-04-22 11:59:37 +00001402 }
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001403 CGF.EmitOMPSimdFinal(S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001404 // Emit: if (PreCond) - end.
1405 if (ContBlock) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001406 CGF.EmitBranch(ContBlock);
1407 CGF.EmitBlock(ContBlock, true);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001408 }
1409 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001410 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
Alexander Musman515ad8c2014-05-22 08:54:05 +00001411}
1412
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001413void CodeGenFunction::EmitOMPOuterLoop(bool DynamicOrOrdered, bool IsMonotonic,
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001414 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered,
1415 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) {
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001416 auto &RT = CGM.getOpenMPRuntime();
Alexander Musman92bdaab2015-03-12 13:37:50 +00001417
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001418 const Expr *IVExpr = S.getIterationVariable();
1419 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1420 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1421
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001422 auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end");
1423
1424 // Start the loop with a block that tests the condition.
1425 auto CondBlock = createBasicBlock("omp.dispatch.cond");
1426 EmitBlock(CondBlock);
1427 LoopStack.push(CondBlock);
1428
1429 llvm::Value *BoolCondVal = nullptr;
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001430 if (!DynamicOrOrdered) {
Alexander Musman92bdaab2015-03-12 13:37:50 +00001431 // UB = min(UB, GlobalUB)
1432 EmitIgnoredExpr(S.getEnsureUpperBound());
1433 // IV = LB
1434 EmitIgnoredExpr(S.getInit());
1435 // IV < UB
Alexey Bataevae05c292015-06-16 11:59:36 +00001436 BoolCondVal = EvaluateExprAsBool(S.getCond());
Alexander Musman92bdaab2015-03-12 13:37:50 +00001437 } else {
1438 BoolCondVal = RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned,
1439 IL, LB, UB, ST);
1440 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001441
1442 // If there are any cleanups between here and the loop-exit scope,
1443 // create a block to stage a loop exit along.
1444 auto ExitBlock = LoopExit.getBlock();
1445 if (LoopScope.requiresCleanups())
1446 ExitBlock = createBasicBlock("omp.dispatch.cleanup");
1447
1448 auto LoopBody = createBasicBlock("omp.dispatch.body");
1449 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
1450 if (ExitBlock != LoopExit.getBlock()) {
1451 EmitBlock(ExitBlock);
1452 EmitBranchThroughCleanup(LoopExit);
1453 }
1454 EmitBlock(LoopBody);
1455
Alexander Musman92bdaab2015-03-12 13:37:50 +00001456 // Emit "IV = LB" (in case of static schedule, we have already calculated new
1457 // LB for loop condition and emitted it above).
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001458 if (DynamicOrOrdered)
Alexander Musman92bdaab2015-03-12 13:37:50 +00001459 EmitIgnoredExpr(S.getInit());
1460
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001461 // Create a block for the increment.
1462 auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc");
1463 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1464
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001465 // Generate !llvm.loop.parallel metadata for loads and stores for loops
1466 // with dynamic/guided scheduling and without ordered clause.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001467 if (!isOpenMPSimdDirective(S.getDirectiveKind()))
1468 LoopStack.setParallel(!IsMonotonic);
1469 else
1470 EmitOMPSimdInit(S, IsMonotonic);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001471
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001472 SourceLocation Loc = S.getLocStart();
Alexey Bataev0f34da12015-07-02 04:17:07 +00001473 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(),
1474 [&S, LoopExit](CodeGenFunction &CGF) {
1475 CGF.EmitOMPLoopBody(S, LoopExit);
1476 CGF.EmitStopPoint(&S);
1477 },
1478 [Ordered, IVSize, IVSigned, Loc](CodeGenFunction &CGF) {
1479 if (Ordered) {
1480 CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(
1481 CGF, Loc, IVSize, IVSigned);
1482 }
1483 });
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001484
1485 EmitBlock(Continue.getBlock());
1486 BreakContinueStack.pop_back();
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001487 if (!DynamicOrOrdered) {
Alexander Musman92bdaab2015-03-12 13:37:50 +00001488 // Emit "LB = LB + Stride", "UB = UB + Stride".
1489 EmitIgnoredExpr(S.getNextLowerBound());
1490 EmitIgnoredExpr(S.getNextUpperBound());
1491 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001492
1493 EmitBranch(CondBlock);
1494 LoopStack.pop();
1495 // Emit the fall-through block.
1496 EmitBlock(LoopExit.getBlock());
1497
1498 // Tell the runtime we are done.
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001499 if (!DynamicOrOrdered)
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001500 RT.emitForStaticFinish(*this, S.getLocEnd());
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001501
1502}
1503
1504void CodeGenFunction::EmitOMPForOuterLoop(
1505 OpenMPScheduleClauseKind ScheduleKind, bool IsMonotonic,
1506 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered,
1507 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) {
1508 auto &RT = CGM.getOpenMPRuntime();
1509
1510 // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime).
1511 const bool DynamicOrOrdered = Ordered || RT.isDynamic(ScheduleKind);
1512
1513 assert((Ordered ||
1514 !RT.isStaticNonchunked(ScheduleKind, /*Chunked=*/Chunk != nullptr)) &&
1515 "static non-chunked schedule does not need outer loop");
1516
1517 // Emit outer loop.
1518 //
1519 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1520 // When schedule(dynamic,chunk_size) is specified, the iterations are
1521 // distributed to threads in the team in chunks as the threads request them.
1522 // Each thread executes a chunk of iterations, then requests another chunk,
1523 // until no chunks remain to be distributed. Each chunk contains chunk_size
1524 // iterations, except for the last chunk to be distributed, which may have
1525 // fewer iterations. When no chunk_size is specified, it defaults to 1.
1526 //
1527 // When schedule(guided,chunk_size) is specified, the iterations are assigned
1528 // to threads in the team in chunks as the executing threads request them.
1529 // Each thread executes a chunk of iterations, then requests another chunk,
1530 // until no chunks remain to be assigned. For a chunk_size of 1, the size of
1531 // each chunk is proportional to the number of unassigned iterations divided
1532 // by the number of threads in the team, decreasing to 1. For a chunk_size
1533 // with value k (greater than 1), the size of each chunk is determined in the
1534 // same way, with the restriction that the chunks do not contain fewer than k
1535 // iterations (except for the last chunk to be assigned, which may have fewer
1536 // than k iterations).
1537 //
1538 // When schedule(auto) is specified, the decision regarding scheduling is
1539 // delegated to the compiler and/or runtime system. The programmer gives the
1540 // implementation the freedom to choose any possible mapping of iterations to
1541 // threads in the team.
1542 //
1543 // When schedule(runtime) is specified, the decision regarding scheduling is
1544 // deferred until run time, and the schedule and chunk size are taken from the
1545 // run-sched-var ICV. If the ICV is set to auto, the schedule is
1546 // implementation defined
1547 //
1548 // while(__kmpc_dispatch_next(&LB, &UB)) {
1549 // idx = LB;
1550 // while (idx <= UB) { BODY; ++idx;
1551 // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only.
1552 // } // inner loop
1553 // }
1554 //
1555 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1556 // When schedule(static, chunk_size) is specified, iterations are divided into
1557 // chunks of size chunk_size, and the chunks are assigned to the threads in
1558 // the team in a round-robin fashion in the order of the thread number.
1559 //
1560 // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) {
1561 // while (idx <= UB) { BODY; ++idx; } // inner loop
1562 // LB = LB + ST;
1563 // UB = UB + ST;
1564 // }
1565 //
1566
1567 const Expr *IVExpr = S.getIterationVariable();
1568 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1569 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1570
1571 if (DynamicOrOrdered) {
1572 llvm::Value *UBVal = EmitScalarExpr(S.getLastIteration());
1573 RT.emitForDispatchInit(*this, S.getLocStart(), ScheduleKind,
1574 IVSize, IVSigned, Ordered, UBVal, Chunk);
1575 } else {
1576 RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind, IVSize, IVSigned,
1577 Ordered, IL, LB, UB, ST, Chunk);
1578 }
1579
1580 EmitOMPOuterLoop(IsMonotonic, DynamicOrOrdered, S, LoopScope, Ordered, LB, UB,
1581 ST, IL, Chunk);
1582}
1583
1584void CodeGenFunction::EmitOMPDistributeOuterLoop(
1585 OpenMPDistScheduleClauseKind ScheduleKind,
1586 const OMPDistributeDirective &S, OMPPrivateScope &LoopScope,
1587 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) {
1588
1589 auto &RT = CGM.getOpenMPRuntime();
1590
1591 // Emit outer loop.
1592 // Same behavior as a OMPForOuterLoop, except that schedule cannot be
1593 // dynamic
1594 //
1595
1596 const Expr *IVExpr = S.getIterationVariable();
1597 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1598 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1599
1600 RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind,
1601 IVSize, IVSigned, /* Ordered = */ false,
1602 IL, LB, UB, ST, Chunk);
1603
1604 EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false,
1605 S, LoopScope, /* Ordered = */ false, LB, UB, ST, IL, Chunk);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001606}
1607
Alexander Musmanc6388682014-12-15 07:07:06 +00001608/// \brief Emit a helper variable and return corresponding lvalue.
1609static LValue EmitOMPHelperVar(CodeGenFunction &CGF,
1610 const DeclRefExpr *Helper) {
1611 auto VDecl = cast<VarDecl>(Helper->getDecl());
1612 CGF.EmitVarDecl(*VDecl);
1613 return CGF.EmitLValue(Helper);
1614}
1615
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001616namespace {
1617 struct ScheduleKindModifiersTy {
1618 OpenMPScheduleClauseKind Kind;
1619 OpenMPScheduleClauseModifier M1;
1620 OpenMPScheduleClauseModifier M2;
1621 ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind,
1622 OpenMPScheduleClauseModifier M1,
1623 OpenMPScheduleClauseModifier M2)
1624 : Kind(Kind), M1(M1), M2(M2) {}
1625 };
1626} // namespace
1627
Alexey Bataev38e89532015-04-16 04:54:05 +00001628bool CodeGenFunction::EmitOMPWorksharingLoop(const OMPLoopDirective &S) {
Alexander Musmanc6388682014-12-15 07:07:06 +00001629 // Emit the loop iteration variable.
1630 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
1631 auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
1632 EmitVarDecl(*IVDecl);
1633
1634 // Emit the iterations count variable.
1635 // If it is not a variable, Sema decided to calculate iterations count on each
1636 // iteration (e.g., it is foldable into a constant).
1637 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
1638 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
1639 // Emit calculation of the iterations count.
1640 EmitIgnoredExpr(S.getCalcLastIteration());
1641 }
1642
1643 auto &RT = CGM.getOpenMPRuntime();
1644
Alexey Bataev38e89532015-04-16 04:54:05 +00001645 bool HasLastprivateClause;
Alexander Musmanc6388682014-12-15 07:07:06 +00001646 // Check pre-condition.
1647 {
1648 // Skip the entire loop if we don't meet the precondition.
Alexey Bataev62dbb972015-04-22 11:59:37 +00001649 // If the condition constant folds and can be elided, avoid emitting the
1650 // whole loop.
1651 bool CondConstant;
1652 llvm::BasicBlock *ContBlock = nullptr;
1653 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
1654 if (!CondConstant)
1655 return false;
1656 } else {
Alexey Bataev62dbb972015-04-22 11:59:37 +00001657 auto *ThenBlock = createBasicBlock("omp.precond.then");
1658 ContBlock = createBasicBlock("omp.precond.end");
1659 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
Justin Bogner66242d62015-04-23 23:06:47 +00001660 getProfileCount(&S));
Alexey Bataev62dbb972015-04-22 11:59:37 +00001661 EmitBlock(ThenBlock);
Justin Bogner66242d62015-04-23 23:06:47 +00001662 incrementProfileCounter(&S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001663 }
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001664
1665 emitAlignedClause(*this, S);
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001666 EmitOMPLinearClauseInit(S);
Alexander Musmanc6388682014-12-15 07:07:06 +00001667 // Emit 'then' code.
1668 {
1669 // Emit helper vars inits.
1670 LValue LB =
1671 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable()));
1672 LValue UB =
1673 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable()));
1674 LValue ST =
1675 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
1676 LValue IL =
1677 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
1678
1679 OMPPrivateScope LoopScope(*this);
Alexey Bataev69c62a92015-04-15 04:52:20 +00001680 if (EmitOMPFirstprivateClause(S, LoopScope)) {
1681 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00001682 // initialization of firstprivate variables and post-update of
1683 // lastprivate variables.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001684 CGM.getOpenMPRuntime().emitBarrierCall(
1685 *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
1686 /*ForceSimpleCall=*/true);
Alexey Bataev69c62a92015-04-15 04:52:20 +00001687 }
Alexey Bataev50a64582015-04-22 12:24:45 +00001688 EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev38e89532015-04-16 04:54:05 +00001689 HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev7ebe5fd2015-04-22 13:43:03 +00001690 EmitOMPReductionClauseInit(S, LoopScope);
Alexey Bataeva8899172015-08-06 12:30:57 +00001691 emitPrivateLoopCounters(*this, LoopScope, S.counters(),
1692 S.private_counters());
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001693 emitPrivateLinearVars(*this, S, LoopScope);
Alexander Musman7931b982015-03-16 07:14:41 +00001694 (void)LoopScope.Privatize();
Alexander Musmanc6388682014-12-15 07:07:06 +00001695
1696 // Detect the loop schedule kind and chunk.
Alexey Bataev3392d762016-02-16 11:18:12 +00001697 llvm::Value *Chunk = nullptr;
1698 OpenMPScheduleClauseKind ScheduleKind = OMPC_SCHEDULE_unknown;
1699 OpenMPScheduleClauseModifier M1 = OMPC_SCHEDULE_MODIFIER_unknown;
1700 OpenMPScheduleClauseModifier M2 = OMPC_SCHEDULE_MODIFIER_unknown;
1701 if (auto *C = S.getSingleClause<OMPScheduleClause>()) {
1702 ScheduleKind = C->getScheduleKind();
1703 M1 = C->getFirstScheduleModifier();
1704 M2 = C->getSecondScheduleModifier();
1705 if (const auto *Ch = C->getChunkSize()) {
1706 Chunk = EmitScalarExpr(Ch);
1707 Chunk = EmitScalarConversion(Chunk, Ch->getType(),
1708 S.getIterationVariable()->getType(),
1709 S.getLocStart());
1710 }
1711 }
Alexander Musmanc6388682014-12-15 07:07:06 +00001712 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1713 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001714 const bool Ordered = S.getSingleClause<OMPOrderedClause>() != nullptr;
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001715 // OpenMP 4.5, 2.7.1 Loop Construct, Description.
1716 // If the static schedule kind is specified or if the ordered clause is
1717 // specified, and if no monotonic modifier is specified, the effect will
1718 // be as if the monotonic modifier was specified.
Alexander Musmanc6388682014-12-15 07:07:06 +00001719 if (RT.isStaticNonchunked(ScheduleKind,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001720 /* Chunked */ Chunk != nullptr) &&
1721 !Ordered) {
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001722 if (isOpenMPSimdDirective(S.getDirectiveKind()))
1723 EmitOMPSimdInit(S, /*IsMonotonic=*/true);
Alexander Musmanc6388682014-12-15 07:07:06 +00001724 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1725 // When no chunk_size is specified, the iteration space is divided into
1726 // chunks that are approximately equal in size, and at most one chunk is
1727 // distributed to each thread. Note that the size of the chunks is
1728 // unspecified in this case.
John McCall7f416cc2015-09-08 08:05:57 +00001729 RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind,
1730 IVSize, IVSigned, Ordered,
1731 IL.getAddress(), LB.getAddress(),
1732 UB.getAddress(), ST.getAddress());
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001733 auto LoopExit =
1734 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
Alexander Musmanc6388682014-12-15 07:07:06 +00001735 // UB = min(UB, GlobalUB);
1736 EmitIgnoredExpr(S.getEnsureUpperBound());
1737 // IV = LB;
1738 EmitIgnoredExpr(S.getInit());
1739 // while (idx <= UB) { BODY; ++idx; }
Alexey Bataevae05c292015-06-16 11:59:36 +00001740 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
1741 S.getInc(),
Alexey Bataev0f34da12015-07-02 04:17:07 +00001742 [&S, LoopExit](CodeGenFunction &CGF) {
1743 CGF.EmitOMPLoopBody(S, LoopExit);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001744 CGF.EmitStopPoint(&S);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001745 },
1746 [](CodeGenFunction &) {});
Alexey Bataev0f34da12015-07-02 04:17:07 +00001747 EmitBlock(LoopExit.getBlock());
Alexander Musmanc6388682014-12-15 07:07:06 +00001748 // Tell the runtime we are done.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001749 RT.emitForStaticFinish(*this, S.getLocStart());
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001750 } else {
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001751 const bool IsMonotonic = Ordered ||
1752 ScheduleKind == OMPC_SCHEDULE_static ||
1753 ScheduleKind == OMPC_SCHEDULE_unknown ||
1754 M1 == OMPC_SCHEDULE_MODIFIER_monotonic ||
1755 M2 == OMPC_SCHEDULE_MODIFIER_monotonic;
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001756 // Emit the outer loop, which requests its work chunk [LB..UB] from
1757 // runtime and runs the inner loop to process it.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001758 EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001759 LB.getAddress(), UB.getAddress(), ST.getAddress(),
1760 IL.getAddress(), Chunk);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001761 }
Alexey Bataev7ebe5fd2015-04-22 13:43:03 +00001762 EmitOMPReductionClauseFinal(S);
Alexey Bataev61205072016-03-02 04:57:40 +00001763 // Emit post-update of the reduction variables if IsLastIter != 0.
1764 emitPostUpdateForReductionClause(
1765 *this, S, [&](CodeGenFunction &CGF) -> llvm::Value * {
1766 return CGF.Builder.CreateIsNotNull(
1767 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
1768 });
Alexey Bataev38e89532015-04-16 04:54:05 +00001769 // Emit final copy of the lastprivate variables if IsLastIter != 0.
1770 if (HasLastprivateClause)
1771 EmitOMPLastprivateClauseFinal(
1772 S, Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart())));
Alexander Musmanc6388682014-12-15 07:07:06 +00001773 }
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001774 if (isOpenMPSimdDirective(S.getDirectiveKind())) {
1775 EmitOMPSimdFinal(S);
1776 }
Alexander Musmanc6388682014-12-15 07:07:06 +00001777 // We're now done with the loop, so jump to the continuation block.
Alexey Bataev62dbb972015-04-22 11:59:37 +00001778 if (ContBlock) {
1779 EmitBranch(ContBlock);
1780 EmitBlock(ContBlock, true);
1781 }
Alexander Musmanc6388682014-12-15 07:07:06 +00001782 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001783 return HasLastprivateClause;
Alexander Musmanc6388682014-12-15 07:07:06 +00001784}
1785
1786void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001787 bool HasLastprivates = false;
Alexey Bataev3392d762016-02-16 11:18:12 +00001788 {
1789 OMPLexicalScope Scope(*this, S);
1790 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF) {
1791 HasLastprivates = CGF.EmitOMPWorksharingLoop(S);
1792 };
1793 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen,
1794 S.hasCancel());
1795 }
Alexander Musmanc6388682014-12-15 07:07:06 +00001796
1797 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001798 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) {
Alexey Bataevf2685682015-03-30 04:30:22 +00001799 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
1800 }
Alexey Bataevf29276e2014-06-18 04:14:57 +00001801}
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001802
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001803void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001804 bool HasLastprivates = false;
Alexey Bataev3392d762016-02-16 11:18:12 +00001805 {
1806 OMPLexicalScope Scope(*this, S);
1807 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF) {
1808 HasLastprivates = CGF.EmitOMPWorksharingLoop(S);
1809 };
1810 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
1811 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001812
1813 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001814 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001815 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
1816 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001817}
1818
Alexey Bataev2df54a02015-03-12 08:53:29 +00001819static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty,
1820 const Twine &Name,
1821 llvm::Value *Init = nullptr) {
John McCall7f416cc2015-09-08 08:05:57 +00001822 auto LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty);
Alexey Bataev2df54a02015-03-12 08:53:29 +00001823 if (Init)
1824 CGF.EmitScalarInit(Init, LVal);
1825 return LVal;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001826}
1827
Alexey Bataev3392d762016-02-16 11:18:12 +00001828void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) {
Alexey Bataev2df54a02015-03-12 08:53:29 +00001829 auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt();
1830 auto *CS = dyn_cast<CompoundStmt>(Stmt);
Alexey Bataev3015bcc2016-01-22 08:56:50 +00001831 bool HasLastprivates = false;
1832 auto &&CodeGen = [&S, Stmt, CS, &HasLastprivates](CodeGenFunction &CGF) {
1833 auto &C = CGF.CGM.getContext();
1834 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1835 // Emit helper vars inits.
1836 LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.",
1837 CGF.Builder.getInt32(0));
1838 auto *GlobalUBVal = CS != nullptr ? CGF.Builder.getInt32(CS->size() - 1)
1839 : CGF.Builder.getInt32(0);
1840 LValue UB =
1841 createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal);
1842 LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.",
1843 CGF.Builder.getInt32(1));
1844 LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.",
1845 CGF.Builder.getInt32(0));
1846 // Loop counter.
1847 LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv.");
1848 OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
1849 CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV);
1850 OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
1851 CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB);
1852 // Generate condition for loop.
1853 BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue,
1854 OK_Ordinary, S.getLocStart(),
1855 /*fpContractable=*/false);
1856 // Increment for loop counter.
1857 UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary,
1858 S.getLocStart());
1859 auto BodyGen = [Stmt, CS, &S, &IV](CodeGenFunction &CGF) {
1860 // Iterate through all sections and emit a switch construct:
1861 // switch (IV) {
1862 // case 0:
1863 // <SectionStmt[0]>;
1864 // break;
1865 // ...
1866 // case <NumSection> - 1:
1867 // <SectionStmt[<NumSection> - 1]>;
1868 // break;
1869 // }
1870 // .omp.sections.exit:
1871 auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit");
1872 auto *SwitchStmt = CGF.Builder.CreateSwitch(
1873 CGF.EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB,
1874 CS == nullptr ? 1 : CS->size());
1875 if (CS) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001876 unsigned CaseNumber = 0;
Benjamin Kramer642f1732015-07-02 21:03:14 +00001877 for (auto *SubStmt : CS->children()) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001878 auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
1879 CGF.EmitBlock(CaseBB);
1880 SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB);
Benjamin Kramer642f1732015-07-02 21:03:14 +00001881 CGF.EmitStmt(SubStmt);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001882 CGF.EmitBranch(ExitBB);
Benjamin Kramer642f1732015-07-02 21:03:14 +00001883 ++CaseNumber;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001884 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00001885 } else {
1886 auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
1887 CGF.EmitBlock(CaseBB);
1888 SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB);
1889 CGF.EmitStmt(Stmt);
1890 CGF.EmitBranch(ExitBB);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001891 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00001892 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
Alexey Bataev2df54a02015-03-12 08:53:29 +00001893 };
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001894
Alexey Bataev3015bcc2016-01-22 08:56:50 +00001895 CodeGenFunction::OMPPrivateScope LoopScope(CGF);
1896 if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) {
Alexey Bataev9efc03b2015-04-27 04:34:03 +00001897 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00001898 // initialization of firstprivate variables and post-update of lastprivate
1899 // variables.
Alexey Bataev3015bcc2016-01-22 08:56:50 +00001900 CGF.CGM.getOpenMPRuntime().emitBarrierCall(
1901 CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
1902 /*ForceSimpleCall=*/true);
Alexey Bataev9efc03b2015-04-27 04:34:03 +00001903 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00001904 CGF.EmitOMPPrivateClause(S, LoopScope);
1905 HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
1906 CGF.EmitOMPReductionClauseInit(S, LoopScope);
1907 (void)LoopScope.Privatize();
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001908
Alexey Bataev3015bcc2016-01-22 08:56:50 +00001909 // Emit static non-chunked loop.
1910 CGF.CGM.getOpenMPRuntime().emitForStaticInit(
1911 CGF, S.getLocStart(), OMPC_SCHEDULE_static, /*IVSize=*/32,
1912 /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(), LB.getAddress(),
1913 UB.getAddress(), ST.getAddress());
1914 // UB = min(UB, GlobalUB);
1915 auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart());
1916 auto *MinUBGlobalUB = CGF.Builder.CreateSelect(
1917 CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal);
1918 CGF.EmitStoreOfScalar(MinUBGlobalUB, UB);
1919 // IV = LB;
1920 CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV);
1921 // while (idx <= UB) { BODY; ++idx; }
1922 CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen,
1923 [](CodeGenFunction &) {});
1924 // Tell the runtime we are done.
1925 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocStart());
1926 CGF.EmitOMPReductionClauseFinal(S);
Alexey Bataev61205072016-03-02 04:57:40 +00001927 // Emit post-update of the reduction variables if IsLastIter != 0.
1928 emitPostUpdateForReductionClause(
1929 CGF, S, [&](CodeGenFunction &CGF) -> llvm::Value * {
1930 return CGF.Builder.CreateIsNotNull(
1931 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
1932 });
Alexey Bataev3015bcc2016-01-22 08:56:50 +00001933
1934 // Emit final copy of the lastprivate variables if IsLastIter != 0.
1935 if (HasLastprivates)
1936 CGF.EmitOMPLastprivateClauseFinal(
1937 S, CGF.Builder.CreateIsNotNull(
1938 CGF.EmitLoadOfScalar(IL, S.getLocStart())));
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001939 };
Alexey Bataev3015bcc2016-01-22 08:56:50 +00001940
1941 bool HasCancel = false;
1942 if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S))
1943 HasCancel = OSD->hasCancel();
1944 else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S))
1945 HasCancel = OPSD->hasCancel();
1946 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen,
1947 HasCancel);
1948 // Emit barrier for lastprivates only if 'sections' directive has 'nowait'
1949 // clause. Otherwise the barrier will be generated by the codegen for the
1950 // directive.
1951 if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) {
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001952 // Emit implicit barrier to synchronize threads and avoid data races on
1953 // initialization of firstprivate variables.
Alexey Bataev3015bcc2016-01-22 08:56:50 +00001954 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
1955 OMPD_unknown);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001956 }
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001957}
Alexey Bataev2df54a02015-03-12 08:53:29 +00001958
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001959void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
Alexey Bataev3392d762016-02-16 11:18:12 +00001960 {
1961 OMPLexicalScope Scope(*this, S);
1962 EmitSections(S);
1963 }
Alexey Bataev2df54a02015-03-12 08:53:29 +00001964 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001965 if (!S.getSingleClause<OMPNowaitClause>()) {
Alexey Bataev3392d762016-02-16 11:18:12 +00001966 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
1967 OMPD_sections);
Alexey Bataevf2685682015-03-30 04:30:22 +00001968 }
Alexey Bataev2df54a02015-03-12 08:53:29 +00001969}
1970
1971void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) {
Alexey Bataev3392d762016-02-16 11:18:12 +00001972 OMPLexicalScope Scope(*this, S);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001973 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1974 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001975 };
Alexey Bataev25e5b442015-09-15 12:52:43 +00001976 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen,
1977 S.hasCancel());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001978}
1979
Alexey Bataev6956e2e2015-02-05 06:35:41 +00001980void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00001981 llvm::SmallVector<const Expr *, 8> CopyprivateVars;
Alexey Bataev420d45b2015-04-14 05:11:24 +00001982 llvm::SmallVector<const Expr *, 8> DestExprs;
Alexey Bataeva63048e2015-03-23 06:18:07 +00001983 llvm::SmallVector<const Expr *, 8> SrcExprs;
Alexey Bataeva63048e2015-03-23 06:18:07 +00001984 llvm::SmallVector<const Expr *, 8> AssignmentOps;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001985 // Check if there are any 'copyprivate' clauses associated with this
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00001986 // 'single' construct.
Alexey Bataeva63048e2015-03-23 06:18:07 +00001987 // Build a list of copyprivate variables along with helper expressions
1988 // (<source>, <destination>, <destination>=<source> expressions)
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001989 for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00001990 CopyprivateVars.append(C->varlists().begin(), C->varlists().end());
Alexey Bataev420d45b2015-04-14 05:11:24 +00001991 DestExprs.append(C->destination_exprs().begin(),
1992 C->destination_exprs().end());
Alexey Bataeva63048e2015-03-23 06:18:07 +00001993 SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end());
Alexey Bataeva63048e2015-03-23 06:18:07 +00001994 AssignmentOps.append(C->assignment_ops().begin(),
1995 C->assignment_ops().end());
1996 }
Alexey Bataev3392d762016-02-16 11:18:12 +00001997 {
1998 OMPLexicalScope Scope(*this, S);
1999 // Emit code for 'single' region along with 'copyprivate' clauses
Alexey Bataev417089f2016-02-17 13:19:37 +00002000 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
Alexey Bataev3392d762016-02-16 11:18:12 +00002001 CodeGenFunction::OMPPrivateScope SingleScope(CGF);
Alexey Bataev417089f2016-02-17 13:19:37 +00002002 (void)CGF.EmitOMPFirstprivateClause(S, SingleScope);
Alexey Bataev3392d762016-02-16 11:18:12 +00002003 CGF.EmitOMPPrivateClause(S, SingleScope);
2004 (void)SingleScope.Privatize();
Alexey Bataev3392d762016-02-16 11:18:12 +00002005 CGF.EmitStmt(
2006 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2007 };
2008 CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(),
2009 CopyprivateVars, DestExprs,
2010 SrcExprs, AssignmentOps);
2011 }
2012 // Emit an implicit barrier at the end (to avoid data race on firstprivate
2013 // init or if no 'nowait' clause was specified and no 'copyprivate' clause).
Alexey Bataev417089f2016-02-17 13:19:37 +00002014 if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) {
Alexey Bataev5521d782015-04-24 04:21:15 +00002015 CGM.getOpenMPRuntime().emitBarrierCall(
2016 *this, S.getLocStart(),
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002017 S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single);
Alexey Bataevf2685682015-03-30 04:30:22 +00002018 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002019}
2020
Alexey Bataev8d690652014-12-04 07:23:53 +00002021void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) {
Alexey Bataev3392d762016-02-16 11:18:12 +00002022 OMPLexicalScope Scope(*this, S);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002023 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
2024 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002025 };
2026 CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart());
Alexander Musman80c22892014-07-17 08:54:58 +00002027}
2028
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002029void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) {
Alexey Bataev3392d762016-02-16 11:18:12 +00002030 OMPLexicalScope Scope(*this, S);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002031 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
2032 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002033 };
Alexey Bataevfc57d162015-12-15 10:55:09 +00002034 Expr *Hint = nullptr;
2035 if (auto *HintClause = S.getSingleClause<OMPHintClause>())
2036 Hint = HintClause->getHint();
2037 CGM.getOpenMPRuntime().emitCriticalRegion(*this,
2038 S.getDirectiveName().getAsString(),
2039 CodeGen, S.getLocStart(), Hint);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002040}
2041
Alexey Bataev671605e2015-04-13 05:28:11 +00002042void CodeGenFunction::EmitOMPParallelForDirective(
2043 const OMPParallelForDirective &S) {
2044 // Emit directive as a combined directive that consists of two implicit
2045 // directives: 'parallel' with 'for' directive.
Alexey Bataev3392d762016-02-16 11:18:12 +00002046 OMPLexicalScope Scope(*this, S);
Alexey Bataev671605e2015-04-13 05:28:11 +00002047 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
2048 CGF.EmitOMPWorksharingLoop(S);
Alexey Bataev671605e2015-04-13 05:28:11 +00002049 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002050 emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002051}
2052
Alexander Musmane4e893b2014-09-23 09:33:00 +00002053void CodeGenFunction::EmitOMPParallelForSimdDirective(
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00002054 const OMPParallelForSimdDirective &S) {
2055 // Emit directive as a combined directive that consists of two implicit
2056 // directives: 'parallel' with 'for' directive.
Alexey Bataev3392d762016-02-16 11:18:12 +00002057 OMPLexicalScope Scope(*this, S);
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00002058 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
2059 CGF.EmitOMPWorksharingLoop(S);
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00002060 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002061 emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002062}
2063
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002064void CodeGenFunction::EmitOMPParallelSectionsDirective(
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002065 const OMPParallelSectionsDirective &S) {
2066 // Emit directive as a combined directive that consists of two implicit
2067 // directives: 'parallel' with 'sections' directive.
Alexey Bataev3392d762016-02-16 11:18:12 +00002068 OMPLexicalScope Scope(*this, S);
Alexey Bataev417089f2016-02-17 13:19:37 +00002069 auto &&CodeGen = [&S](CodeGenFunction &CGF) { CGF.EmitSections(S); };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002070 emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002071}
2072
Alexey Bataev62b63b12015-03-10 07:28:44 +00002073void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) {
2074 // Emit outlined function for task construct.
Alexey Bataev3392d762016-02-16 11:18:12 +00002075 OMPLexicalScope Scope(*this, S);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002076 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
2077 auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
2078 auto *I = CS->getCapturedDecl()->param_begin();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002079 auto *PartId = std::next(I);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002080 // The first function argument for tasks is a thread id, the second one is a
2081 // part id (0 for tied tasks, >=0 for untied task).
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002082 llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
2083 // Get list of private variables.
2084 llvm::SmallVector<const Expr *, 8> PrivateVars;
2085 llvm::SmallVector<const Expr *, 8> PrivateCopies;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002086 for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002087 auto IRef = C->varlist_begin();
2088 for (auto *IInit : C->private_copies()) {
2089 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2090 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
2091 PrivateVars.push_back(*IRef);
2092 PrivateCopies.push_back(IInit);
2093 }
2094 ++IRef;
2095 }
2096 }
2097 EmittedAsPrivate.clear();
2098 // Get list of firstprivate variables.
2099 llvm::SmallVector<const Expr *, 8> FirstprivateVars;
2100 llvm::SmallVector<const Expr *, 8> FirstprivateCopies;
2101 llvm::SmallVector<const Expr *, 8> FirstprivateInits;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002102 for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002103 auto IRef = C->varlist_begin();
2104 auto IElemInitRef = C->inits().begin();
2105 for (auto *IInit : C->private_copies()) {
2106 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2107 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
2108 FirstprivateVars.push_back(*IRef);
2109 FirstprivateCopies.push_back(IInit);
2110 FirstprivateInits.push_back(*IElemInitRef);
2111 }
Richard Trieucc3949d2016-02-18 22:34:54 +00002112 ++IRef;
2113 ++IElemInitRef;
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002114 }
2115 }
Alexey Bataev1d2353d2015-06-24 11:01:36 +00002116 // Build list of dependences.
2117 llvm::SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 8>
2118 Dependences;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002119 for (const auto *C : S.getClausesOfKind<OMPDependClause>()) {
Alexey Bataev1d2353d2015-06-24 11:01:36 +00002120 for (auto *IRef : C->varlists()) {
2121 Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef));
2122 }
2123 }
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002124 auto &&CodeGen = [PartId, &S, &PrivateVars, &FirstprivateVars](
2125 CodeGenFunction &CGF) {
2126 // Set proper addresses for generated private copies.
2127 auto *CS = cast<CapturedStmt>(S.getAssociatedStmt());
2128 OMPPrivateScope Scope(CGF);
2129 if (!PrivateVars.empty() || !FirstprivateVars.empty()) {
John McCall7f416cc2015-09-08 08:05:57 +00002130 auto *CopyFn = CGF.Builder.CreateLoad(
2131 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3)));
2132 auto *PrivatesPtr = CGF.Builder.CreateLoad(
2133 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2)));
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002134 // Map privates.
John McCall7f416cc2015-09-08 08:05:57 +00002135 llvm::SmallVector<std::pair<const VarDecl *, Address>, 16>
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002136 PrivatePtrs;
2137 llvm::SmallVector<llvm::Value *, 16> CallArgs;
2138 CallArgs.push_back(PrivatesPtr);
2139 for (auto *E : PrivateVars) {
2140 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +00002141 Address PrivatePtr =
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002142 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()));
2143 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
John McCall7f416cc2015-09-08 08:05:57 +00002144 CallArgs.push_back(PrivatePtr.getPointer());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002145 }
2146 for (auto *E : FirstprivateVars) {
2147 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +00002148 Address PrivatePtr =
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002149 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()));
2150 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
John McCall7f416cc2015-09-08 08:05:57 +00002151 CallArgs.push_back(PrivatePtr.getPointer());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002152 }
2153 CGF.EmitRuntimeCall(CopyFn, CallArgs);
2154 for (auto &&Pair : PrivatePtrs) {
John McCall7f416cc2015-09-08 08:05:57 +00002155 Address Replacement(CGF.Builder.CreateLoad(Pair.second),
2156 CGF.getContext().getDeclAlign(Pair.first));
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002157 Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; });
2158 }
2159 }
2160 (void)Scope.Privatize();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002161 if (*PartId) {
2162 // TODO: emit code for untied tasks.
2163 }
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002164 CGF.EmitStmt(CS->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002165 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002166 auto OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction(
2167 S, *I, OMPD_task, CodeGen);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002168 // Check if we should emit tied or untied task.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002169 bool Tied = !S.getSingleClause<OMPUntiedClause>();
Alexey Bataev62b63b12015-03-10 07:28:44 +00002170 // Check if the task is final
2171 llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002172 if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002173 // If the condition constant folds and can be elided, try to avoid emitting
2174 // the condition and the dead arm of the if/else.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002175 auto *Cond = Clause->getCondition();
Alexey Bataev62b63b12015-03-10 07:28:44 +00002176 bool CondConstant;
2177 if (ConstantFoldsToSimpleInteger(Cond, CondConstant))
2178 Final.setInt(CondConstant);
2179 else
2180 Final.setPointer(EvaluateExprAsBool(Cond));
2181 } else {
2182 // By default the task is not final.
2183 Final.setInt(/*IntVal=*/false);
2184 }
2185 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
Alexey Bataev1d677132015-04-22 13:57:31 +00002186 const Expr *IfCond = nullptr;
Alexey Bataev7371aa32015-09-03 08:45:56 +00002187 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
2188 if (C->getNameModifier() == OMPD_unknown ||
2189 C->getNameModifier() == OMPD_task) {
2190 IfCond = C->getCondition();
2191 break;
2192 }
Alexey Bataev1d677132015-04-22 13:57:31 +00002193 }
Alexey Bataev9e034042015-05-05 04:05:12 +00002194 CGM.getOpenMPRuntime().emitTaskCall(
2195 *this, S.getLocStart(), S, Tied, Final, OutlinedFn, SharedsTy,
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002196 CapturedStruct, IfCond, PrivateVars, PrivateCopies, FirstprivateVars,
Alexey Bataev1d2353d2015-06-24 11:01:36 +00002197 FirstprivateCopies, FirstprivateInits, Dependences);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002198}
2199
Alexey Bataev9f797f32015-02-05 05:57:51 +00002200void CodeGenFunction::EmitOMPTaskyieldDirective(
2201 const OMPTaskyieldDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002202 CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart());
Alexey Bataev68446b72014-07-18 07:47:19 +00002203}
2204
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00002205void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) {
Alexey Bataevf2685682015-03-30 04:30:22 +00002206 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier);
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002207}
2208
Alexey Bataev8b8e2022015-04-27 05:22:09 +00002209void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) {
2210 CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart());
Alexey Bataev2df347a2014-07-18 10:17:07 +00002211}
2212
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002213void CodeGenFunction::EmitOMPTaskgroupDirective(
2214 const OMPTaskgroupDirective &S) {
Alexey Bataev3392d762016-02-16 11:18:12 +00002215 OMPLexicalScope Scope(*this, S);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002216 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
2217 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002218 };
2219 CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getLocStart());
2220}
2221
Alexey Bataevcc37cc12014-11-20 04:34:54 +00002222void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002223 CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002224 if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002225 return llvm::makeArrayRef(FlushClause->varlist_begin(),
2226 FlushClause->varlist_end());
2227 }
2228 return llvm::None;
2229 }(), S.getLocStart());
Alexey Bataev6125da92014-07-21 11:26:11 +00002230}
2231
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002232void CodeGenFunction::EmitOMPDistributeLoop(const OMPDistributeDirective &S) {
2233 // Emit the loop iteration variable.
2234 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
2235 auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
2236 EmitVarDecl(*IVDecl);
2237
2238 // Emit the iterations count variable.
2239 // If it is not a variable, Sema decided to calculate iterations count on each
2240 // iteration (e.g., it is foldable into a constant).
2241 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
2242 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
2243 // Emit calculation of the iterations count.
2244 EmitIgnoredExpr(S.getCalcLastIteration());
2245 }
2246
2247 auto &RT = CGM.getOpenMPRuntime();
2248
2249 // Check pre-condition.
2250 {
2251 // Skip the entire loop if we don't meet the precondition.
2252 // If the condition constant folds and can be elided, avoid emitting the
2253 // whole loop.
2254 bool CondConstant;
2255 llvm::BasicBlock *ContBlock = nullptr;
2256 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
2257 if (!CondConstant)
2258 return;
2259 } else {
2260 auto *ThenBlock = createBasicBlock("omp.precond.then");
2261 ContBlock = createBasicBlock("omp.precond.end");
2262 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
2263 getProfileCount(&S));
2264 EmitBlock(ThenBlock);
2265 incrementProfileCounter(&S);
2266 }
2267
2268 // Emit 'then' code.
2269 {
2270 // Emit helper vars inits.
2271 LValue LB =
2272 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable()));
2273 LValue UB =
2274 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable()));
2275 LValue ST =
2276 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
2277 LValue IL =
2278 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
2279
2280 OMPPrivateScope LoopScope(*this);
2281 emitPrivateLoopCounters(*this, LoopScope, S.counters(),
2282 S.private_counters());
2283 (void)LoopScope.Privatize();
2284
2285 // Detect the distribute schedule kind and chunk.
2286 llvm::Value *Chunk = nullptr;
2287 OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown;
2288 if (auto *C = S.getSingleClause<OMPDistScheduleClause>()) {
2289 ScheduleKind = C->getDistScheduleKind();
2290 if (const auto *Ch = C->getChunkSize()) {
2291 Chunk = EmitScalarExpr(Ch);
2292 Chunk = EmitScalarConversion(Chunk, Ch->getType(),
2293 S.getIterationVariable()->getType(),
2294 S.getLocStart());
2295 }
2296 }
2297 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
2298 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
2299
2300 // OpenMP [2.10.8, distribute Construct, Description]
2301 // If dist_schedule is specified, kind must be static. If specified,
2302 // iterations are divided into chunks of size chunk_size, chunks are
2303 // assigned to the teams of the league in a round-robin fashion in the
2304 // order of the team number. When no chunk_size is specified, the
2305 // iteration space is divided into chunks that are approximately equal
2306 // in size, and at most one chunk is distributed to each team of the
2307 // league. The size of the chunks is unspecified in this case.
2308 if (RT.isStaticNonchunked(ScheduleKind,
2309 /* Chunked */ Chunk != nullptr)) {
2310 RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind,
2311 IVSize, IVSigned, /* Ordered = */ false,
2312 IL.getAddress(), LB.getAddress(),
2313 UB.getAddress(), ST.getAddress());
2314 auto LoopExit =
2315 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
2316 // UB = min(UB, GlobalUB);
2317 EmitIgnoredExpr(S.getEnsureUpperBound());
2318 // IV = LB;
2319 EmitIgnoredExpr(S.getInit());
2320 // while (idx <= UB) { BODY; ++idx; }
2321 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
2322 S.getInc(),
2323 [&S, LoopExit](CodeGenFunction &CGF) {
2324 CGF.EmitOMPLoopBody(S, LoopExit);
2325 CGF.EmitStopPoint(&S);
2326 },
2327 [](CodeGenFunction &) {});
2328 EmitBlock(LoopExit.getBlock());
2329 // Tell the runtime we are done.
2330 RT.emitForStaticFinish(*this, S.getLocStart());
2331 } else {
2332 // Emit the outer loop, which requests its work chunk [LB..UB] from
2333 // runtime and runs the inner loop to process it.
2334 EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope,
2335 LB.getAddress(), UB.getAddress(), ST.getAddress(),
2336 IL.getAddress(), Chunk);
2337 }
2338 }
2339
2340 // We're now done with the loop, so jump to the continuation block.
2341 if (ContBlock) {
2342 EmitBranch(ContBlock);
2343 EmitBlock(ContBlock, true);
2344 }
2345 }
2346}
2347
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002348void CodeGenFunction::EmitOMPDistributeDirective(
2349 const OMPDistributeDirective &S) {
Carlo Bertollifc35ad22016-03-07 16:04:49 +00002350 LexicalScope Scope(*this, S.getSourceRange());
2351 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
2352 CGF.EmitOMPDistributeLoop(S);
2353 };
2354 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen,
2355 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002356}
2357
Alexey Bataev5f600d62015-09-29 03:48:57 +00002358static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM,
2359 const CapturedStmt *S) {
2360 CodeGenFunction CGF(CGM, /*suppressNewContext=*/true);
2361 CodeGenFunction::CGCapturedStmtInfo CapStmtInfo;
2362 CGF.CapturedStmtInfo = &CapStmtInfo;
2363 auto *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S);
2364 Fn->addFnAttr(llvm::Attribute::NoInline);
2365 return Fn;
2366}
2367
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002368void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00002369 if (!S.getAssociatedStmt())
2370 return;
Alexey Bataev3392d762016-02-16 11:18:12 +00002371 OMPLexicalScope Scope(*this, S);
Alexey Bataev5f600d62015-09-29 03:48:57 +00002372 auto *C = S.getSingleClause<OMPSIMDClause>();
2373 auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF) {
2374 if (C) {
2375 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
2376 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
2377 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
2378 auto *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS);
2379 CGF.EmitNounwindRuntimeCall(OutlinedFn, CapturedVars);
2380 } else {
2381 CGF.EmitStmt(
2382 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2383 }
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002384 };
Alexey Bataev5f600d62015-09-29 03:48:57 +00002385 CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart(), !C);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002386}
2387
Alexey Bataevb57056f2015-01-22 06:17:56 +00002388static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002389 QualType SrcType, QualType DestType,
2390 SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00002391 assert(CGF.hasScalarEvaluationKind(DestType) &&
2392 "DestType must have scalar evaluation kind.");
2393 assert(!Val.isAggregate() && "Must be a scalar or complex.");
2394 return Val.isScalar()
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002395 ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType,
2396 Loc)
Alexey Bataevb57056f2015-01-22 06:17:56 +00002397 : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002398 DestType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00002399}
2400
2401static CodeGenFunction::ComplexPairTy
2402convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002403 QualType DestType, SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00002404 assert(CGF.getEvaluationKind(DestType) == TEK_Complex &&
2405 "DestType must have complex evaluation kind.");
2406 CodeGenFunction::ComplexPairTy ComplexVal;
2407 if (Val.isScalar()) {
2408 // Convert the input element to the element type of the complex.
2409 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002410 auto ScalarVal = CGF.EmitScalarConversion(Val.getScalarVal(), SrcType,
2411 DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00002412 ComplexVal = CodeGenFunction::ComplexPairTy(
2413 ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType()));
2414 } else {
2415 assert(Val.isComplex() && "Must be a scalar or complex.");
2416 auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType();
2417 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
2418 ComplexVal.first = CGF.EmitScalarConversion(
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002419 Val.getComplexVal().first, SrcElementType, DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00002420 ComplexVal.second = CGF.EmitScalarConversion(
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002421 Val.getComplexVal().second, SrcElementType, DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00002422 }
2423 return ComplexVal;
2424}
2425
Alexey Bataev5e018f92015-04-23 06:35:10 +00002426static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst,
2427 LValue LVal, RValue RVal) {
2428 if (LVal.isGlobalReg()) {
2429 CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal);
2430 } else {
2431 CGF.EmitAtomicStore(RVal, LVal, IsSeqCst ? llvm::SequentiallyConsistent
2432 : llvm::Monotonic,
2433 LVal.isVolatile(), /*IsInit=*/false);
2434 }
2435}
2436
Alexey Bataev8524d152016-01-21 12:35:58 +00002437void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal,
2438 QualType RValTy, SourceLocation Loc) {
2439 switch (getEvaluationKind(LVal.getType())) {
Alexey Bataev5e018f92015-04-23 06:35:10 +00002440 case TEK_Scalar:
Alexey Bataev8524d152016-01-21 12:35:58 +00002441 EmitStoreThroughLValue(RValue::get(convertToScalarValue(
2442 *this, RVal, RValTy, LVal.getType(), Loc)),
2443 LVal);
Alexey Bataev5e018f92015-04-23 06:35:10 +00002444 break;
2445 case TEK_Complex:
Alexey Bataev8524d152016-01-21 12:35:58 +00002446 EmitStoreOfComplex(
2447 convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal,
Alexey Bataev5e018f92015-04-23 06:35:10 +00002448 /*isInit=*/false);
2449 break;
2450 case TEK_Aggregate:
2451 llvm_unreachable("Must be a scalar or complex.");
2452 }
2453}
2454
Alexey Bataevb57056f2015-01-22 06:17:56 +00002455static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst,
2456 const Expr *X, const Expr *V,
2457 SourceLocation Loc) {
2458 // v = x;
2459 assert(V->isLValue() && "V of 'omp atomic read' is not lvalue");
2460 assert(X->isLValue() && "X of 'omp atomic read' is not lvalue");
2461 LValue XLValue = CGF.EmitLValue(X);
2462 LValue VLValue = CGF.EmitLValue(V);
David Majnemera5b195a2015-02-14 01:35:12 +00002463 RValue Res = XLValue.isGlobalReg()
2464 ? CGF.EmitLoadOfLValue(XLValue, Loc)
2465 : CGF.EmitAtomicLoad(XLValue, Loc,
2466 IsSeqCst ? llvm::SequentiallyConsistent
Alexey Bataevb8329262015-02-27 06:33:30 +00002467 : llvm::Monotonic,
2468 XLValue.isVolatile());
Alexey Bataevb57056f2015-01-22 06:17:56 +00002469 // OpenMP, 2.12.6, atomic Construct
2470 // Any atomic construct with a seq_cst clause forces the atomically
2471 // performed operation to include an implicit flush operation without a
2472 // list.
2473 if (IsSeqCst)
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002474 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
Alexey Bataev8524d152016-01-21 12:35:58 +00002475 CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00002476}
2477
Alexey Bataevb8329262015-02-27 06:33:30 +00002478static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst,
2479 const Expr *X, const Expr *E,
2480 SourceLocation Loc) {
2481 // x = expr;
2482 assert(X->isLValue() && "X of 'omp atomic write' is not lvalue");
Alexey Bataev5e018f92015-04-23 06:35:10 +00002483 emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E));
Alexey Bataevb8329262015-02-27 06:33:30 +00002484 // OpenMP, 2.12.6, atomic Construct
2485 // Any atomic construct with a seq_cst clause forces the atomically
2486 // performed operation to include an implicit flush operation without a
2487 // list.
2488 if (IsSeqCst)
2489 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
2490}
2491
Benjamin Kramer439ee9d2015-05-01 13:59:53 +00002492static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X,
2493 RValue Update,
2494 BinaryOperatorKind BO,
2495 llvm::AtomicOrdering AO,
2496 bool IsXLHSInRHSPart) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002497 auto &Context = CGF.CGM.getContext();
2498 // Allow atomicrmw only if 'x' and 'update' are integer values, lvalue for 'x'
Alexey Bataevb4505a72015-03-30 05:20:59 +00002499 // expression is simple and atomic is allowed for the given type for the
2500 // target platform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002501 if (BO == BO_Comma || !Update.isScalar() ||
Alexey Bataev9d541a72015-05-08 11:47:16 +00002502 !Update.getScalarVal()->getType()->isIntegerTy() ||
2503 !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) &&
2504 (Update.getScalarVal()->getType() !=
John McCall7f416cc2015-09-08 08:05:57 +00002505 X.getAddress().getElementType())) ||
2506 !X.getAddress().getElementType()->isIntegerTy() ||
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002507 !Context.getTargetInfo().hasBuiltinAtomic(
2508 Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment())))
Alexey Bataev5e018f92015-04-23 06:35:10 +00002509 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002510
2511 llvm::AtomicRMWInst::BinOp RMWOp;
2512 switch (BO) {
2513 case BO_Add:
2514 RMWOp = llvm::AtomicRMWInst::Add;
2515 break;
2516 case BO_Sub:
2517 if (!IsXLHSInRHSPart)
Alexey Bataev5e018f92015-04-23 06:35:10 +00002518 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002519 RMWOp = llvm::AtomicRMWInst::Sub;
2520 break;
2521 case BO_And:
2522 RMWOp = llvm::AtomicRMWInst::And;
2523 break;
2524 case BO_Or:
2525 RMWOp = llvm::AtomicRMWInst::Or;
2526 break;
2527 case BO_Xor:
2528 RMWOp = llvm::AtomicRMWInst::Xor;
2529 break;
2530 case BO_LT:
2531 RMWOp = X.getType()->hasSignedIntegerRepresentation()
2532 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min
2533 : llvm::AtomicRMWInst::Max)
2534 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin
2535 : llvm::AtomicRMWInst::UMax);
2536 break;
2537 case BO_GT:
2538 RMWOp = X.getType()->hasSignedIntegerRepresentation()
2539 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max
2540 : llvm::AtomicRMWInst::Min)
2541 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax
2542 : llvm::AtomicRMWInst::UMin);
2543 break;
Alexey Bataev5e018f92015-04-23 06:35:10 +00002544 case BO_Assign:
2545 RMWOp = llvm::AtomicRMWInst::Xchg;
2546 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002547 case BO_Mul:
2548 case BO_Div:
2549 case BO_Rem:
2550 case BO_Shl:
2551 case BO_Shr:
2552 case BO_LAnd:
2553 case BO_LOr:
Alexey Bataev5e018f92015-04-23 06:35:10 +00002554 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002555 case BO_PtrMemD:
2556 case BO_PtrMemI:
2557 case BO_LE:
2558 case BO_GE:
2559 case BO_EQ:
2560 case BO_NE:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002561 case BO_AddAssign:
2562 case BO_SubAssign:
2563 case BO_AndAssign:
2564 case BO_OrAssign:
2565 case BO_XorAssign:
2566 case BO_MulAssign:
2567 case BO_DivAssign:
2568 case BO_RemAssign:
2569 case BO_ShlAssign:
2570 case BO_ShrAssign:
2571 case BO_Comma:
2572 llvm_unreachable("Unsupported atomic update operation");
2573 }
2574 auto *UpdateVal = Update.getScalarVal();
2575 if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) {
2576 UpdateVal = CGF.Builder.CreateIntCast(
John McCall7f416cc2015-09-08 08:05:57 +00002577 IC, X.getAddress().getElementType(),
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002578 X.getType()->hasSignedIntegerRepresentation());
2579 }
John McCall7f416cc2015-09-08 08:05:57 +00002580 auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO);
Alexey Bataev5e018f92015-04-23 06:35:10 +00002581 return std::make_pair(true, RValue::get(Res));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002582}
2583
Alexey Bataev5e018f92015-04-23 06:35:10 +00002584std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr(
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002585 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
2586 llvm::AtomicOrdering AO, SourceLocation Loc,
2587 const llvm::function_ref<RValue(RValue)> &CommonGen) {
2588 // Update expressions are allowed to have the following forms:
2589 // x binop= expr; -> xrval + expr;
2590 // x++, ++x -> xrval + 1;
2591 // x--, --x -> xrval - 1;
2592 // x = x binop expr; -> xrval binop expr
2593 // x = expr Op x; - > expr binop xrval;
Alexey Bataev5e018f92015-04-23 06:35:10 +00002594 auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart);
2595 if (!Res.first) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002596 if (X.isGlobalReg()) {
2597 // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop
2598 // 'xrval'.
2599 EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X);
2600 } else {
2601 // Perform compare-and-swap procedure.
2602 EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified());
Alexey Bataevb4505a72015-03-30 05:20:59 +00002603 }
2604 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00002605 return Res;
Alexey Bataevb4505a72015-03-30 05:20:59 +00002606}
2607
2608static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst,
2609 const Expr *X, const Expr *E,
2610 const Expr *UE, bool IsXLHSInRHSPart,
2611 SourceLocation Loc) {
2612 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
2613 "Update expr in 'atomic update' must be a binary operator.");
2614 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
2615 // Update expressions are allowed to have the following forms:
2616 // x binop= expr; -> xrval + expr;
2617 // x++, ++x -> xrval + 1;
2618 // x--, --x -> xrval - 1;
2619 // x = x binop expr; -> xrval binop expr
2620 // x = expr Op x; - > expr binop xrval;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002621 assert(X->isLValue() && "X of 'omp atomic update' is not lvalue");
Alexey Bataevb4505a72015-03-30 05:20:59 +00002622 LValue XLValue = CGF.EmitLValue(X);
2623 RValue ExprRValue = CGF.EmitAnyExpr(E);
Alexey Bataevb4505a72015-03-30 05:20:59 +00002624 auto AO = IsSeqCst ? llvm::SequentiallyConsistent : llvm::Monotonic;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00002625 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
2626 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
2627 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
2628 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
2629 auto Gen =
2630 [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue {
2631 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
2632 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
2633 return CGF.EmitAnyExpr(UE);
2634 };
Alexey Bataev5e018f92015-04-23 06:35:10 +00002635 (void)CGF.EmitOMPAtomicSimpleUpdateExpr(
2636 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
2637 // OpenMP, 2.12.6, atomic Construct
2638 // Any atomic construct with a seq_cst clause forces the atomically
2639 // performed operation to include an implicit flush operation without a
2640 // list.
2641 if (IsSeqCst)
2642 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
2643}
2644
2645static RValue convertToType(CodeGenFunction &CGF, RValue Value,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002646 QualType SourceType, QualType ResType,
2647 SourceLocation Loc) {
Alexey Bataev5e018f92015-04-23 06:35:10 +00002648 switch (CGF.getEvaluationKind(ResType)) {
2649 case TEK_Scalar:
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002650 return RValue::get(
2651 convertToScalarValue(CGF, Value, SourceType, ResType, Loc));
Alexey Bataev5e018f92015-04-23 06:35:10 +00002652 case TEK_Complex: {
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002653 auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc);
Alexey Bataev5e018f92015-04-23 06:35:10 +00002654 return RValue::getComplex(Res.first, Res.second);
2655 }
2656 case TEK_Aggregate:
2657 break;
2658 }
2659 llvm_unreachable("Must be a scalar or complex.");
2660}
2661
2662static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst,
2663 bool IsPostfixUpdate, const Expr *V,
2664 const Expr *X, const Expr *E,
2665 const Expr *UE, bool IsXLHSInRHSPart,
2666 SourceLocation Loc) {
2667 assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue");
2668 assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue");
2669 RValue NewVVal;
2670 LValue VLValue = CGF.EmitLValue(V);
2671 LValue XLValue = CGF.EmitLValue(X);
2672 RValue ExprRValue = CGF.EmitAnyExpr(E);
2673 auto AO = IsSeqCst ? llvm::SequentiallyConsistent : llvm::Monotonic;
2674 QualType NewVValType;
2675 if (UE) {
2676 // 'x' is updated with some additional value.
2677 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
2678 "Update expr in 'atomic capture' must be a binary operator.");
2679 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
2680 // Update expressions are allowed to have the following forms:
2681 // x binop= expr; -> xrval + expr;
2682 // x++, ++x -> xrval + 1;
2683 // x--, --x -> xrval - 1;
2684 // x = x binop expr; -> xrval binop expr
2685 // x = expr Op x; - > expr binop xrval;
2686 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
2687 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
2688 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
2689 NewVValType = XRValExpr->getType();
2690 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
2691 auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr,
2692 IsSeqCst, IsPostfixUpdate](RValue XRValue) -> RValue {
2693 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
2694 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
2695 RValue Res = CGF.EmitAnyExpr(UE);
2696 NewVVal = IsPostfixUpdate ? XRValue : Res;
2697 return Res;
2698 };
2699 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
2700 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
2701 if (Res.first) {
2702 // 'atomicrmw' instruction was generated.
2703 if (IsPostfixUpdate) {
2704 // Use old value from 'atomicrmw'.
2705 NewVVal = Res.second;
2706 } else {
2707 // 'atomicrmw' does not provide new value, so evaluate it using old
2708 // value of 'x'.
2709 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
2710 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second);
2711 NewVVal = CGF.EmitAnyExpr(UE);
2712 }
2713 }
2714 } else {
2715 // 'x' is simply rewritten with some 'expr'.
2716 NewVValType = X->getType().getNonReferenceType();
2717 ExprRValue = convertToType(CGF, ExprRValue, E->getType(),
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00002718 X->getType().getNonReferenceType(), Loc);
Alexey Bataev5e018f92015-04-23 06:35:10 +00002719 auto &&Gen = [&CGF, &NewVVal, ExprRValue](RValue XRValue) -> RValue {
2720 NewVVal = XRValue;
2721 return ExprRValue;
2722 };
2723 // Try to perform atomicrmw xchg, otherwise simple exchange.
2724 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
2725 XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO,
2726 Loc, Gen);
2727 if (Res.first) {
2728 // 'atomicrmw' instruction was generated.
2729 NewVVal = IsPostfixUpdate ? Res.second : ExprRValue;
2730 }
2731 }
2732 // Emit post-update store to 'v' of old/new 'x' value.
Alexey Bataev8524d152016-01-21 12:35:58 +00002733 CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc);
Alexey Bataevb4505a72015-03-30 05:20:59 +00002734 // OpenMP, 2.12.6, atomic Construct
2735 // Any atomic construct with a seq_cst clause forces the atomically
2736 // performed operation to include an implicit flush operation without a
2737 // list.
2738 if (IsSeqCst)
2739 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
2740}
2741
Alexey Bataevb57056f2015-01-22 06:17:56 +00002742static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind,
Alexey Bataev5e018f92015-04-23 06:35:10 +00002743 bool IsSeqCst, bool IsPostfixUpdate,
2744 const Expr *X, const Expr *V, const Expr *E,
2745 const Expr *UE, bool IsXLHSInRHSPart,
2746 SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00002747 switch (Kind) {
2748 case OMPC_read:
2749 EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc);
2750 break;
2751 case OMPC_write:
Alexey Bataevb8329262015-02-27 06:33:30 +00002752 EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc);
2753 break;
Alexey Bataevb4505a72015-03-30 05:20:59 +00002754 case OMPC_unknown:
Alexey Bataevb57056f2015-01-22 06:17:56 +00002755 case OMPC_update:
Alexey Bataevb4505a72015-03-30 05:20:59 +00002756 EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc);
2757 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00002758 case OMPC_capture:
Alexey Bataev5e018f92015-04-23 06:35:10 +00002759 EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE,
2760 IsXLHSInRHSPart, Loc);
2761 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00002762 case OMPC_if:
2763 case OMPC_final:
2764 case OMPC_num_threads:
2765 case OMPC_private:
2766 case OMPC_firstprivate:
2767 case OMPC_lastprivate:
2768 case OMPC_reduction:
2769 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00002770 case OMPC_simdlen:
Alexey Bataevb57056f2015-01-22 06:17:56 +00002771 case OMPC_collapse:
2772 case OMPC_default:
2773 case OMPC_seq_cst:
2774 case OMPC_shared:
2775 case OMPC_linear:
2776 case OMPC_aligned:
2777 case OMPC_copyin:
2778 case OMPC_copyprivate:
2779 case OMPC_flush:
2780 case OMPC_proc_bind:
2781 case OMPC_schedule:
2782 case OMPC_ordered:
2783 case OMPC_nowait:
2784 case OMPC_untied:
2785 case OMPC_threadprivate:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002786 case OMPC_depend:
Alexey Bataevb57056f2015-01-22 06:17:56 +00002787 case OMPC_mergeable:
Michael Wonge710d542015-08-07 16:16:36 +00002788 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00002789 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002790 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00002791 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00002792 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00002793 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00002794 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00002795 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00002796 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00002797 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00002798 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00002799 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00002800 case OMPC_defaultmap:
Alexey Bataevb57056f2015-01-22 06:17:56 +00002801 llvm_unreachable("Clause is not allowed in 'omp atomic'.");
2802 }
2803}
2804
2805void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002806 bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>();
Alexey Bataevb57056f2015-01-22 06:17:56 +00002807 OpenMPClauseKind Kind = OMPC_unknown;
2808 for (auto *C : S.clauses()) {
2809 // Find first clause (skip seq_cst clause, if it is first).
2810 if (C->getClauseKind() != OMPC_seq_cst) {
2811 Kind = C->getClauseKind();
2812 break;
2813 }
2814 }
Alexey Bataev10fec572015-03-11 04:48:56 +00002815
2816 const auto *CS =
2817 S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev5e018f92015-04-23 06:35:10 +00002818 if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) {
Alexey Bataev10fec572015-03-11 04:48:56 +00002819 enterFullExpression(EWC);
Alexey Bataev5e018f92015-04-23 06:35:10 +00002820 }
2821 // Processing for statements under 'atomic capture'.
2822 if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) {
2823 for (const auto *C : Compound->body()) {
2824 if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) {
2825 enterFullExpression(EWC);
2826 }
2827 }
2828 }
Alexey Bataev10fec572015-03-11 04:48:56 +00002829
Alexey Bataev3392d762016-02-16 11:18:12 +00002830 OMPLexicalScope Scope(*this, S);
Alexey Bataev33c56402015-12-14 09:26:19 +00002831 auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF) {
2832 CGF.EmitStopPoint(CS);
Alexey Bataev5e018f92015-04-23 06:35:10 +00002833 EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(),
2834 S.getV(), S.getExpr(), S.getUpdateExpr(),
2835 S.isXLHSInRHSPart(), S.getLocStart());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002836 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002837 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen);
Alexey Bataev0162e452014-07-22 10:10:35 +00002838}
2839
Samuel Antaobed3c462015-10-02 16:14:20 +00002840void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) {
Alexey Bataev3392d762016-02-16 11:18:12 +00002841 OMPLexicalScope Scope(*this, S);
Samuel Antaobed3c462015-10-02 16:14:20 +00002842 const CapturedStmt &CS = *cast<CapturedStmt>(S.getAssociatedStmt());
2843
2844 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
Samuel Antao4af1b7b2015-12-02 17:44:43 +00002845 GenerateOpenMPCapturedVars(CS, CapturedVars);
Samuel Antaobed3c462015-10-02 16:14:20 +00002846
Samuel Antaoee8fb302016-01-06 13:42:12 +00002847 llvm::Function *Fn = nullptr;
2848 llvm::Constant *FnID = nullptr;
Samuel Antaobed3c462015-10-02 16:14:20 +00002849
2850 // Check if we have any if clause associated with the directive.
2851 const Expr *IfCond = nullptr;
2852
2853 if (auto *C = S.getSingleClause<OMPIfClause>()) {
2854 IfCond = C->getCondition();
2855 }
2856
2857 // Check if we have any device clause associated with the directive.
2858 const Expr *Device = nullptr;
2859 if (auto *C = S.getSingleClause<OMPDeviceClause>()) {
2860 Device = C->getDevice();
2861 }
2862
Samuel Antaoee8fb302016-01-06 13:42:12 +00002863 // Check if we have an if clause whose conditional always evaluates to false
2864 // or if we do not have any targets specified. If so the target region is not
2865 // an offload entry point.
2866 bool IsOffloadEntry = true;
2867 if (IfCond) {
2868 bool Val;
2869 if (ConstantFoldsToSimpleInteger(IfCond, Val) && !Val)
2870 IsOffloadEntry = false;
2871 }
2872 if (CGM.getLangOpts().OMPTargetTriples.empty())
2873 IsOffloadEntry = false;
2874
2875 assert(CurFuncDecl && "No parent declaration for target region!");
2876 StringRef ParentName;
2877 // In case we have Ctors/Dtors we use the complete type variant to produce
2878 // the mangling of the device outlined kernel.
2879 if (auto *D = dyn_cast<CXXConstructorDecl>(CurFuncDecl))
2880 ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete));
2881 else if (auto *D = dyn_cast<CXXDestructorDecl>(CurFuncDecl))
2882 ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete));
2883 else
2884 ParentName =
2885 CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CurFuncDecl)));
2886
2887 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(S, ParentName, Fn, FnID,
2888 IsOffloadEntry);
2889
2890 CGM.getOpenMPRuntime().emitTargetCall(*this, S, Fn, FnID, IfCond, Device,
Samuel Antaobed3c462015-10-02 16:14:20 +00002891 CapturedVars);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002892}
2893
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00002894static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF,
2895 const OMPExecutableDirective &S,
2896 OpenMPDirectiveKind InnermostKind,
2897 const RegionCodeGenTy &CodeGen) {
2898 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
2899 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
2900 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
2901 auto OutlinedFn = CGF.CGM.getOpenMPRuntime().
2902 emitParallelOrTeamsOutlinedFunction(S,
2903 *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
Samuel Antaob68e2db2016-03-03 16:20:23 +00002904
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00002905 const OMPTeamsDirective &TD = *dyn_cast<OMPTeamsDirective>(&S);
2906 const OMPNumTeamsClause *NT = TD.getSingleClause<OMPNumTeamsClause>();
2907 const OMPThreadLimitClause *TL = TD.getSingleClause<OMPThreadLimitClause>();
2908 if (NT || TL) {
2909 llvm::Value *NumTeamsVal = (NT) ? CGF.Builder.CreateIntCast(
2910 CGF.EmitScalarExpr(NT->getNumTeams()), CGF.CGM.Int32Ty,
2911 /* isSigned = */ true) :
2912 CGF.Builder.getInt32(0);
2913
2914 llvm::Value *ThreadLimitVal = (TL) ? CGF.Builder.CreateIntCast(
2915 CGF.EmitScalarExpr(TL->getThreadLimit()), CGF.CGM.Int32Ty,
2916 /* isSigned = */ true) :
2917 CGF.Builder.getInt32(0);
2918
2919 CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeamsVal,
2920 ThreadLimitVal, S.getLocStart());
2921 }
2922
2923 CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getLocStart(), OutlinedFn,
2924 CapturedVars);
2925}
2926
2927void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) {
2928 LexicalScope Scope(*this, S.getSourceRange());
2929 // Emit parallel region as a standalone region.
2930 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
2931 OMPPrivateScope PrivateScope(CGF);
Carlo Bertolli6ad7b5a2016-03-03 22:09:40 +00002932 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
2933 CGF.EmitOMPPrivateClause(S, PrivateScope);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00002934 (void)PrivateScope.Privatize();
2935 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2936 };
2937 emitCommonOMPTeamsDirective(*this, S, OMPD_teams, CodeGen);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002938}
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002939
2940void CodeGenFunction::EmitOMPCancellationPointDirective(
2941 const OMPCancellationPointDirective &S) {
Alexey Bataev0f34da12015-07-02 04:17:07 +00002942 CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getLocStart(),
2943 S.getCancelRegion());
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002944}
2945
Alexey Bataev80909872015-07-02 11:25:17 +00002946void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) {
Alexey Bataev87933c72015-09-18 08:07:34 +00002947 const Expr *IfCond = nullptr;
2948 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
2949 if (C->getNameModifier() == OMPD_unknown ||
2950 C->getNameModifier() == OMPD_cancel) {
2951 IfCond = C->getCondition();
2952 break;
2953 }
2954 }
2955 CGM.getOpenMPRuntime().emitCancelCall(*this, S.getLocStart(), IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00002956 S.getCancelRegion());
Alexey Bataev80909872015-07-02 11:25:17 +00002957}
2958
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002959CodeGenFunction::JumpDest
2960CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) {
2961 if (Kind == OMPD_parallel || Kind == OMPD_task)
2962 return ReturnBlock;
Alexey Bataev25e5b442015-09-15 12:52:43 +00002963 assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections ||
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002964 Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for);
Alexey Bataev25e5b442015-09-15 12:52:43 +00002965 return BreakContinueStack.back().BreakBlock;
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002966}
Michael Wong65f367f2015-07-21 13:44:28 +00002967
2968// Generate the instructions for '#pragma omp target data' directive.
2969void CodeGenFunction::EmitOMPTargetDataDirective(
2970 const OMPTargetDataDirective &S) {
Michael Wong65f367f2015-07-21 13:44:28 +00002971 // emit the code inside the construct for now
2972 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
Michael Wongb5c16982015-08-11 04:52:01 +00002973 CGM.getOpenMPRuntime().emitInlinedDirective(
2974 *this, OMPD_target_data,
2975 [&CS](CodeGenFunction &CGF) { CGF.EmitStmt(CS->getCapturedStmt()); });
Michael Wong65f367f2015-07-21 13:44:28 +00002976}
Alexey Bataev49f6e782015-12-01 04:18:41 +00002977
Samuel Antaodf67fc42016-01-19 19:15:56 +00002978void CodeGenFunction::EmitOMPTargetEnterDataDirective(
2979 const OMPTargetEnterDataDirective &S) {
2980 // TODO: codegen for target enter data.
2981}
2982
Samuel Antao72590762016-01-19 20:04:50 +00002983void CodeGenFunction::EmitOMPTargetExitDataDirective(
2984 const OMPTargetExitDataDirective &S) {
2985 // TODO: codegen for target exit data.
2986}
2987
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002988void CodeGenFunction::EmitOMPTargetParallelDirective(
2989 const OMPTargetParallelDirective &S) {
2990 // TODO: codegen for target parallel.
2991}
2992
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002993void CodeGenFunction::EmitOMPTargetParallelForDirective(
2994 const OMPTargetParallelForDirective &S) {
2995 // TODO: codegen for target parallel for.
2996}
2997
Alexey Bataev49f6e782015-12-01 04:18:41 +00002998void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) {
2999 // emit the code inside the construct for now
3000 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
3001 CGM.getOpenMPRuntime().emitInlinedDirective(
3002 *this, OMPD_taskloop,
3003 [&CS](CodeGenFunction &CGF) { CGF.EmitStmt(CS->getCapturedStmt()); });
3004}
3005
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003006void CodeGenFunction::EmitOMPTaskLoopSimdDirective(
3007 const OMPTaskLoopSimdDirective &S) {
3008 // emit the code inside the construct for now
3009 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
3010 CGM.getOpenMPRuntime().emitInlinedDirective(
3011 *this, OMPD_taskloop_simd,
3012 [&CS](CodeGenFunction &CGF) { CGF.EmitStmt(CS->getCapturedStmt()); });
3013}
3014