blob: cdec3e35f971eca8fc5f835c798d36c5b4f86fda [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- CGStmtOpenMP.cpp - Emit LLVM Code from Statements ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit OpenMP nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Alexey Bataev3392d762016-02-16 11:18:12 +000014#include "CGCleanup.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "CGOpenMPRuntime.h"
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000018#include "TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000019#include "clang/AST/Stmt.h"
20#include "clang/AST/StmtOpenMP.h"
Alexey Bataev2bbf7212016-03-03 03:52:24 +000021#include "clang/AST/DeclOpenMP.h"
Alexey Bataeva839ddd2016-03-17 10:19:46 +000022#include "llvm/IR/CallSite.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000023using namespace clang;
24using namespace CodeGen;
25
Alexey Bataev3392d762016-02-16 11:18:12 +000026namespace {
27/// Lexical scope for OpenMP executable constructs, that handles correct codegen
28/// for captured expressions.
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000029class OMPLexicalScope : public CodeGenFunction::LexicalScope {
Alexey Bataev3392d762016-02-16 11:18:12 +000030 void emitPreInitStmt(CodeGenFunction &CGF, const OMPExecutableDirective &S) {
31 for (const auto *C : S.clauses()) {
32 if (auto *CPI = OMPClauseWithPreInit::get(C)) {
33 if (auto *PreInit = cast_or_null<DeclStmt>(CPI->getPreInitStmt())) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +000034 for (const auto *I : PreInit->decls()) {
35 if (!I->hasAttr<OMPCaptureNoInitAttr>())
36 CGF.EmitVarDecl(cast<VarDecl>(*I));
37 else {
38 CodeGenFunction::AutoVarEmission Emission =
39 CGF.EmitAutoVarAlloca(cast<VarDecl>(*I));
40 CGF.EmitAutoVarCleanups(Emission);
41 }
42 }
Alexey Bataev3392d762016-02-16 11:18:12 +000043 }
44 }
45 }
46 }
Alexey Bataev4ba78a42016-04-27 07:56:03 +000047 CodeGenFunction::OMPPrivateScope InlinedShareds;
48
49 static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) {
50 return CGF.LambdaCaptureFields.lookup(VD) ||
51 (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) ||
52 (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl));
53 }
Alexey Bataev3392d762016-02-16 11:18:12 +000054
Alexey Bataev3392d762016-02-16 11:18:12 +000055public:
Alexey Bataev4ba78a42016-04-27 07:56:03 +000056 OMPLexicalScope(CodeGenFunction &CGF, const OMPExecutableDirective &S,
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000057 bool AsInlined = false, bool EmitPreInitStmt = true)
Alexey Bataev4ba78a42016-04-27 07:56:03 +000058 : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()),
59 InlinedShareds(CGF) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000060 if (EmitPreInitStmt)
61 emitPreInitStmt(CGF, S);
Alexey Bataev4ba78a42016-04-27 07:56:03 +000062 if (AsInlined) {
63 if (S.hasAssociatedStmt()) {
64 auto *CS = cast<CapturedStmt>(S.getAssociatedStmt());
65 for (auto &C : CS->captures()) {
66 if (C.capturesVariable() || C.capturesVariableByCopy()) {
67 auto *VD = C.getCapturedVar();
Alexey Bataev6a71f362017-08-22 17:54:52 +000068 assert(VD == VD->getCanonicalDecl() &&
69 "Canonical decl must be captured.");
Alexey Bataev4ba78a42016-04-27 07:56:03 +000070 DeclRefExpr DRE(const_cast<VarDecl *>(VD),
71 isCapturedVar(CGF, VD) ||
72 (CGF.CapturedStmtInfo &&
73 InlinedShareds.isGlobalVarCaptured(VD)),
74 VD->getType().getNonReferenceType(), VK_LValue,
75 SourceLocation());
76 InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address {
77 return CGF.EmitLValue(&DRE).getAddress();
78 });
79 }
80 }
81 (void)InlinedShareds.Privatize();
82 }
83 }
Alexey Bataev3392d762016-02-16 11:18:12 +000084 }
85};
Alexey Bataev14fa1c62016-03-29 05:34:15 +000086
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000087/// Lexical scope for OpenMP parallel construct, that handles correct codegen
88/// for captured expressions.
89class OMPParallelScope final : public OMPLexicalScope {
90 bool EmitPreInitStmt(const OMPExecutableDirective &S) {
91 OpenMPDirectiveKind Kind = S.getDirectiveKind();
Carlo Bertollib0ff0a62017-04-25 17:52:12 +000092 return !(isOpenMPTargetExecutionDirective(Kind) ||
93 isOpenMPLoopBoundSharingDirective(Kind)) &&
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +000094 isOpenMPParallelDirective(Kind);
95 }
96
97public:
98 OMPParallelScope(CodeGenFunction &CGF, const OMPExecutableDirective &S)
99 : OMPLexicalScope(CGF, S,
100 /*AsInlined=*/false,
101 /*EmitPreInitStmt=*/EmitPreInitStmt(S)) {}
102};
103
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +0000104/// Lexical scope for OpenMP teams construct, that handles correct codegen
105/// for captured expressions.
106class OMPTeamsScope final : public OMPLexicalScope {
107 bool EmitPreInitStmt(const OMPExecutableDirective &S) {
108 OpenMPDirectiveKind Kind = S.getDirectiveKind();
109 return !isOpenMPTargetExecutionDirective(Kind) &&
110 isOpenMPTeamsDirective(Kind);
111 }
112
113public:
114 OMPTeamsScope(CodeGenFunction &CGF, const OMPExecutableDirective &S)
115 : OMPLexicalScope(CGF, S,
116 /*AsInlined=*/false,
117 /*EmitPreInitStmt=*/EmitPreInitStmt(S)) {}
118};
119
Alexey Bataev5a3af132016-03-29 08:58:54 +0000120/// Private scope for OpenMP loop-based directives, that supports capturing
121/// of used expression from loop statement.
122class OMPLoopScope : public CodeGenFunction::RunCleanupsScope {
123 void emitPreInitStmt(CodeGenFunction &CGF, const OMPLoopDirective &S) {
124 if (auto *LD = dyn_cast<OMPLoopDirective>(&S)) {
125 if (auto *PreInits = cast_or_null<DeclStmt>(LD->getPreInits())) {
126 for (const auto *I : PreInits->decls())
127 CGF.EmitVarDecl(cast<VarDecl>(*I));
128 }
129 }
130 }
131
132public:
133 OMPLoopScope(CodeGenFunction &CGF, const OMPLoopDirective &S)
134 : CodeGenFunction::RunCleanupsScope(CGF) {
135 emitPreInitStmt(CGF, S);
136 }
137};
138
Alexey Bataev3392d762016-02-16 11:18:12 +0000139} // namespace
140
Alexey Bataevf47c4b42017-09-26 13:47:31 +0000141LValue CodeGenFunction::EmitOMPSharedLValue(const Expr *E) {
142 if (auto *OrigDRE = dyn_cast<DeclRefExpr>(E)) {
143 if (auto *OrigVD = dyn_cast<VarDecl>(OrigDRE->getDecl())) {
144 OrigVD = OrigVD->getCanonicalDecl();
145 bool IsCaptured =
146 LambdaCaptureFields.lookup(OrigVD) ||
147 (CapturedStmtInfo && CapturedStmtInfo->lookup(OrigVD)) ||
148 (CurCodeDecl && isa<BlockDecl>(CurCodeDecl));
149 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), IsCaptured,
150 OrigDRE->getType(), VK_LValue, OrigDRE->getExprLoc());
151 return EmitLValue(&DRE);
152 }
153 }
154 return EmitLValue(E);
155}
156
Alexey Bataev1189bd02016-01-26 12:20:39 +0000157llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) {
158 auto &C = getContext();
159 llvm::Value *Size = nullptr;
160 auto SizeInChars = C.getTypeSizeInChars(Ty);
161 if (SizeInChars.isZero()) {
162 // getTypeSizeInChars() returns 0 for a VLA.
163 while (auto *VAT = C.getAsVariableArrayType(Ty)) {
164 llvm::Value *ArraySize;
165 std::tie(ArraySize, Ty) = getVLASize(VAT);
166 Size = Size ? Builder.CreateNUWMul(Size, ArraySize) : ArraySize;
167 }
168 SizeInChars = C.getTypeSizeInChars(Ty);
169 if (SizeInChars.isZero())
170 return llvm::ConstantInt::get(SizeTy, /*V=*/0);
171 Size = Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars));
172 } else
173 Size = CGM.getSize(SizeInChars);
174 return Size;
175}
176
Alexey Bataev2377fe92015-09-10 08:12:02 +0000177void CodeGenFunction::GenerateOpenMPCapturedVars(
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000178 const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) {
Alexey Bataev2377fe92015-09-10 08:12:02 +0000179 const RecordDecl *RD = S.getCapturedRecordDecl();
180 auto CurField = RD->field_begin();
181 auto CurCap = S.captures().begin();
182 for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(),
183 E = S.capture_init_end();
184 I != E; ++I, ++CurField, ++CurCap) {
185 if (CurField->hasCapturedVLAType()) {
186 auto VAT = CurField->getCapturedVLAType();
Samuel Antaobed3c462015-10-02 16:14:20 +0000187 auto *Val = VLASizeMap[VAT->getSizeExpr()];
Samuel Antaobed3c462015-10-02 16:14:20 +0000188 CapturedVars.push_back(Val);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000189 } else if (CurCap->capturesThis())
190 CapturedVars.push_back(CXXThisValue);
Samuel Antao6d004262016-06-16 18:39:34 +0000191 else if (CurCap->capturesVariableByCopy()) {
192 llvm::Value *CV =
193 EmitLoadOfLValue(EmitLValue(*I), SourceLocation()).getScalarVal();
194
195 // If the field is not a pointer, we need to save the actual value
196 // and load it as a void pointer.
197 if (!CurField->getType()->isAnyPointerType()) {
198 auto &Ctx = getContext();
199 auto DstAddr = CreateMemTemp(
200 Ctx.getUIntPtrType(),
201 Twine(CurCap->getCapturedVar()->getName()) + ".casted");
202 LValue DstLV = MakeAddrLValue(DstAddr, Ctx.getUIntPtrType());
203
204 auto *SrcAddrVal = EmitScalarConversion(
205 DstAddr.getPointer(), Ctx.getPointerType(Ctx.getUIntPtrType()),
206 Ctx.getPointerType(CurField->getType()), SourceLocation());
207 LValue SrcLV =
208 MakeNaturalAlignAddrLValue(SrcAddrVal, CurField->getType());
209
210 // Store the value using the source type pointer.
211 EmitStoreThroughLValue(RValue::get(CV), SrcLV);
212
213 // Load the value using the destination type pointer.
214 CV = EmitLoadOfLValue(DstLV, SourceLocation()).getScalarVal();
215 }
216 CapturedVars.push_back(CV);
217 } else {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000218 assert(CurCap->capturesVariable() && "Expected capture by reference.");
Alexey Bataev2377fe92015-09-10 08:12:02 +0000219 CapturedVars.push_back(EmitLValue(*I).getAddress().getPointer());
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000220 }
Alexey Bataev2377fe92015-09-10 08:12:02 +0000221 }
222}
223
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000224static Address castValueFromUintptr(CodeGenFunction &CGF, QualType DstType,
225 StringRef Name, LValue AddrLV,
226 bool isReferenceType = false) {
227 ASTContext &Ctx = CGF.getContext();
228
229 auto *CastedPtr = CGF.EmitScalarConversion(
230 AddrLV.getAddress().getPointer(), Ctx.getUIntPtrType(),
231 Ctx.getPointerType(DstType), SourceLocation());
232 auto TmpAddr =
233 CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType))
234 .getAddress();
235
236 // If we are dealing with references we need to return the address of the
237 // reference instead of the reference of the value.
238 if (isReferenceType) {
239 QualType RefType = Ctx.getLValueReferenceType(DstType);
240 auto *RefVal = TmpAddr.getPointer();
241 TmpAddr = CGF.CreateMemTemp(RefType, Twine(Name) + ".ref");
242 auto TmpLVal = CGF.MakeAddrLValue(TmpAddr, RefType);
Akira Hatanaka642f7992016-10-18 19:05:41 +0000243 CGF.EmitStoreThroughLValue(RValue::get(RefVal), TmpLVal, /*isInit*/ true);
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000244 }
245
246 return TmpAddr;
247}
248
Alexey Bataevf7ce1662017-04-10 19:16:45 +0000249static QualType getCanonicalParamType(ASTContext &C, QualType T) {
250 if (T->isLValueReferenceType()) {
251 return C.getLValueReferenceType(
252 getCanonicalParamType(C, T.getNonReferenceType()),
253 /*SpelledAsLValue=*/false);
254 }
255 if (T->isPointerType())
256 return C.getPointerType(getCanonicalParamType(C, T->getPointeeType()));
257 return C.getCanonicalParamType(T);
258}
259
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000260namespace {
261 /// Contains required data for proper outlined function codegen.
262 struct FunctionOptions {
263 /// Captured statement for which the function is generated.
264 const CapturedStmt *S = nullptr;
265 /// true if cast to/from UIntPtr is required for variables captured by
266 /// value.
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000267 const bool UIntPtrCastRequired = true;
Alexey Bataeve754b182017-08-09 19:38:53 +0000268 /// true if only casted arguments must be registered as local args or VLA
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000269 /// sizes.
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000270 const bool RegisterCastedArgsOnly = false;
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000271 /// Name of the generated function.
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000272 const StringRef FunctionName;
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000273 explicit FunctionOptions(const CapturedStmt *S, bool UIntPtrCastRequired,
274 bool RegisterCastedArgsOnly,
Alexey Bataev4aa19052017-08-08 16:45:36 +0000275 StringRef FunctionName)
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000276 : S(S), UIntPtrCastRequired(UIntPtrCastRequired),
277 RegisterCastedArgsOnly(UIntPtrCastRequired && RegisterCastedArgsOnly),
Alexey Bataev4aa19052017-08-08 16:45:36 +0000278 FunctionName(FunctionName) {}
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000279 };
280}
281
Alexey Bataeve754b182017-08-09 19:38:53 +0000282static llvm::Function *emitOutlinedFunctionPrologue(
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000283 CodeGenFunction &CGF, FunctionArgList &Args,
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000284 llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>>
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000285 &LocalAddrs,
286 llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>>
287 &VLASizes,
288 llvm::Value *&CXXThisValue, const FunctionOptions &FO) {
289 const CapturedDecl *CD = FO.S->getCapturedDecl();
290 const RecordDecl *RD = FO.S->getCapturedRecordDecl();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000291 assert(CD->hasBody() && "missing CapturedDecl body");
292
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000293 CXXThisValue = nullptr;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000294 // Build the argument list.
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000295 CodeGenModule &CGM = CGF.CGM;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000296 ASTContext &Ctx = CGM.getContext();
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000297 FunctionArgList TargetArgs;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000298 Args.append(CD->param_begin(),
299 std::next(CD->param_begin(), CD->getContextParamPosition()));
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000300 TargetArgs.append(
301 CD->param_begin(),
302 std::next(CD->param_begin(), CD->getContextParamPosition()));
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000303 auto I = FO.S->captures().begin();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000304 for (auto *FD : RD->fields()) {
305 QualType ArgType = FD->getType();
306 IdentifierInfo *II = nullptr;
307 VarDecl *CapVar = nullptr;
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000308
309 // If this is a capture by copy and the type is not a pointer, the outlined
310 // function argument type should be uintptr and the value properly casted to
311 // uintptr. This is necessary given that the runtime library is only able to
312 // deal with pointers. We can pass in the same way the VLA type sizes to the
313 // outlined function.
Samuel Antao6d004262016-06-16 18:39:34 +0000314 if ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) ||
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000315 I->capturesVariableArrayType()) {
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000316 if (FO.UIntPtrCastRequired)
317 ArgType = Ctx.getUIntPtrType();
318 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000319
320 if (I->capturesVariable() || I->capturesVariableByCopy()) {
Alexey Bataev2377fe92015-09-10 08:12:02 +0000321 CapVar = I->getCapturedVar();
322 II = CapVar->getIdentifier();
323 } else if (I->capturesThis())
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000324 II = &Ctx.Idents.get("this");
Alexey Bataev2377fe92015-09-10 08:12:02 +0000325 else {
326 assert(I->capturesVariableArrayType());
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000327 II = &Ctx.Idents.get("vla");
Alexey Bataev2377fe92015-09-10 08:12:02 +0000328 }
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000329 if (ArgType->isVariablyModifiedType())
330 ArgType = getCanonicalParamType(Ctx, ArgType.getNonReferenceType());
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000331 auto *Arg =
332 ImplicitParamDecl::Create(Ctx, /*DC=*/nullptr, FD->getLocation(), II,
333 ArgType, ImplicitParamDecl::Other);
334 Args.emplace_back(Arg);
335 // Do not cast arguments if we emit function with non-original types.
336 TargetArgs.emplace_back(
337 FO.UIntPtrCastRequired
338 ? Arg
339 : CGM.getOpenMPRuntime().translateParameter(FD, Arg));
Alexey Bataev2377fe92015-09-10 08:12:02 +0000340 ++I;
341 }
342 Args.append(
343 std::next(CD->param_begin(), CD->getContextParamPosition() + 1),
344 CD->param_end());
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000345 TargetArgs.append(
346 std::next(CD->param_begin(), CD->getContextParamPosition() + 1),
347 CD->param_end());
Alexey Bataev2377fe92015-09-10 08:12:02 +0000348
349 // Create the function declaration.
350 FunctionType::ExtInfo ExtInfo;
351 const CGFunctionInfo &FuncInfo =
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000352 CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, TargetArgs);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000353 llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo);
354
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000355 llvm::Function *F =
356 llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage,
357 FO.FunctionName, &CGM.getModule());
Alexey Bataev2377fe92015-09-10 08:12:02 +0000358 CGM.SetInternalFunctionAttributes(CD, F, FuncInfo);
359 if (CD->isNothrow())
Alexey Bataev2c7eee52017-08-04 19:10:54 +0000360 F->setDoesNotThrow();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000361
362 // Generate the function.
Alexey Bataev6e01dc12017-08-14 16:03:47 +0000363 CGF.StartFunction(CD, Ctx.VoidTy, F, FuncInfo, TargetArgs,
364 FO.S->getLocStart(), CD->getBody()->getLocStart());
Alexey Bataev2377fe92015-09-10 08:12:02 +0000365 unsigned Cnt = CD->getContextParamPosition();
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000366 I = FO.S->captures().begin();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000367 for (auto *FD : RD->fields()) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000368 // Do not map arguments if we emit function with non-original types.
369 Address LocalAddr(Address::invalid());
370 if (!FO.UIntPtrCastRequired && Args[Cnt] != TargetArgs[Cnt]) {
371 LocalAddr = CGM.getOpenMPRuntime().getParameterAddress(CGF, Args[Cnt],
372 TargetArgs[Cnt]);
373 } else {
374 LocalAddr = CGF.GetAddrOfLocalVar(Args[Cnt]);
375 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000376 // If we are capturing a pointer by copy we don't need to do anything, just
377 // use the value that we get from the arguments.
378 if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) {
Samuel Antao403ffd42016-07-27 22:49:49 +0000379 const VarDecl *CurVD = I->getCapturedVar();
Samuel Antao403ffd42016-07-27 22:49:49 +0000380 // If the variable is a reference we need to materialize it here.
381 if (CurVD->getType()->isReferenceType()) {
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000382 Address RefAddr = CGF.CreateMemTemp(
383 CurVD->getType(), CGM.getPointerAlign(), ".materialized_ref");
384 CGF.EmitStoreOfScalar(LocalAddr.getPointer(), RefAddr,
385 /*Volatile=*/false, CurVD->getType());
Samuel Antao403ffd42016-07-27 22:49:49 +0000386 LocalAddr = RefAddr;
387 }
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000388 if (!FO.RegisterCastedArgsOnly)
389 LocalAddrs.insert({Args[Cnt], {CurVD, LocalAddr}});
Richard Trieucc3949d2016-02-18 22:34:54 +0000390 ++Cnt;
391 ++I;
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000392 continue;
393 }
394
Ivan A. Kosarev5f8c0ca2017-10-10 09:39:32 +0000395 LValue ArgLVal = CGF.MakeAddrLValue(LocalAddr, Args[Cnt]->getType(),
396 AlignmentSource::Decl);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000397 if (FD->hasCapturedVLAType()) {
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000398 if (FO.UIntPtrCastRequired) {
399 ArgLVal = CGF.MakeAddrLValue(castValueFromUintptr(CGF, FD->getType(),
400 Args[Cnt]->getName(),
401 ArgLVal),
Ivan A. Kosarev5f8c0ca2017-10-10 09:39:32 +0000402 FD->getType(), AlignmentSource::Decl);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000403 }
Alexey Bataev2377fe92015-09-10 08:12:02 +0000404 auto *ExprArg =
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000405 CGF.EmitLoadOfLValue(ArgLVal, SourceLocation()).getScalarVal();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000406 auto VAT = FD->getCapturedVLAType();
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000407 VLASizes.insert({Args[Cnt], {VAT->getSizeExpr(), ExprArg}});
Alexey Bataev2377fe92015-09-10 08:12:02 +0000408 } else if (I->capturesVariable()) {
409 auto *Var = I->getCapturedVar();
410 QualType VarTy = Var->getType();
411 Address ArgAddr = ArgLVal.getAddress();
412 if (!VarTy->isReferenceType()) {
Alexey Bataev2f5ed342016-10-13 09:52:46 +0000413 if (ArgLVal.getType()->isLValueReferenceType()) {
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000414 ArgAddr = CGF.EmitLoadOfReference(
Alexey Bataev2f5ed342016-10-13 09:52:46 +0000415 ArgAddr, ArgLVal.getType()->castAs<ReferenceType>());
Alexey Bataevac5eabb2016-11-07 11:16:04 +0000416 } else if (!VarTy->isVariablyModifiedType() || !VarTy->isPointerType()) {
Alexey Bataev2f5ed342016-10-13 09:52:46 +0000417 assert(ArgLVal.getType()->isPointerType());
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000418 ArgAddr = CGF.EmitLoadOfPointer(
Alexey Bataev2f5ed342016-10-13 09:52:46 +0000419 ArgAddr, ArgLVal.getType()->castAs<PointerType>());
420 }
Alexey Bataev2377fe92015-09-10 08:12:02 +0000421 }
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000422 if (!FO.RegisterCastedArgsOnly) {
423 LocalAddrs.insert(
424 {Args[Cnt],
425 {Var, Address(ArgAddr.getPointer(), Ctx.getDeclAlign(Var))}});
426 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000427 } else if (I->capturesVariableByCopy()) {
428 assert(!FD->getType()->isAnyPointerType() &&
429 "Not expecting a captured pointer.");
430 auto *Var = I->getCapturedVar();
431 QualType VarTy = Var->getType();
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000432 LocalAddrs.insert(
433 {Args[Cnt],
434 {Var,
435 FO.UIntPtrCastRequired
436 ? castValueFromUintptr(CGF, FD->getType(), Args[Cnt]->getName(),
437 ArgLVal, VarTy->isReferenceType())
438 : ArgLVal.getAddress()}});
Alexey Bataev2377fe92015-09-10 08:12:02 +0000439 } else {
440 // If 'this' is captured, load it into CXXThisValue.
441 assert(I->capturesThis());
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000442 CXXThisValue = CGF.EmitLoadOfLValue(ArgLVal, Args[Cnt]->getLocation())
443 .getScalarVal();
444 LocalAddrs.insert({Args[Cnt], {nullptr, ArgLVal.getAddress()}});
Alexey Bataev2377fe92015-09-10 08:12:02 +0000445 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000446 ++Cnt;
447 ++I;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000448 }
449
Alexey Bataeve754b182017-08-09 19:38:53 +0000450 return F;
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000451}
452
453llvm::Function *
454CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S) {
455 assert(
456 CapturedStmtInfo &&
457 "CapturedStmtInfo should be set when generating the captured function");
458 const CapturedDecl *CD = S.getCapturedDecl();
459 // Build the argument list.
460 bool NeedWrapperFunction =
461 getDebugInfo() &&
462 CGM.getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo;
463 FunctionArgList Args;
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000464 llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> LocalAddrs;
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000465 llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> VLASizes;
Alexey Bataeve754b182017-08-09 19:38:53 +0000466 SmallString<256> Buffer;
467 llvm::raw_svector_ostream Out(Buffer);
468 Out << CapturedStmtInfo->getHelperName();
469 if (NeedWrapperFunction)
470 Out << "_debug__";
Alexey Bataev4aa19052017-08-08 16:45:36 +0000471 FunctionOptions FO(&S, !NeedWrapperFunction, /*RegisterCastedArgsOnly=*/false,
Alexey Bataeve754b182017-08-09 19:38:53 +0000472 Out.str());
473 llvm::Function *F = emitOutlinedFunctionPrologue(*this, Args, LocalAddrs,
474 VLASizes, CXXThisValue, FO);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000475 for (const auto &LocalAddrPair : LocalAddrs) {
476 if (LocalAddrPair.second.first) {
477 setAddrOfLocalVar(LocalAddrPair.second.first,
478 LocalAddrPair.second.second);
479 }
480 }
481 for (const auto &VLASizePair : VLASizes)
482 VLASizeMap[VLASizePair.second.first] = VLASizePair.second.second;
Serge Pavlov3a561452015-12-06 14:32:39 +0000483 PGO.assignRegionCounters(GlobalDecl(CD), F);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000484 CapturedStmtInfo->EmitBody(*this, CD->getBody());
485 FinishFunction(CD->getBodyRBrace());
Alexey Bataeve754b182017-08-09 19:38:53 +0000486 if (!NeedWrapperFunction)
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000487 return F;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000488
Alexey Bataevefd884d2017-08-04 21:26:25 +0000489 FunctionOptions WrapperFO(&S, /*UIntPtrCastRequired=*/true,
Alexey Bataeve754b182017-08-09 19:38:53 +0000490 /*RegisterCastedArgsOnly=*/true,
491 CapturedStmtInfo->getHelperName());
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000492 CodeGenFunction WrapperCGF(CGM, /*suppressNewContext=*/true);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000493 Args.clear();
494 LocalAddrs.clear();
495 VLASizes.clear();
496 llvm::Function *WrapperF =
497 emitOutlinedFunctionPrologue(WrapperCGF, Args, LocalAddrs, VLASizes,
Alexey Bataeve754b182017-08-09 19:38:53 +0000498 WrapperCGF.CXXThisValue, WrapperFO);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000499 llvm::SmallVector<llvm::Value *, 4> CallArgs;
500 for (const auto *Arg : Args) {
501 llvm::Value *CallArg;
502 auto I = LocalAddrs.find(Arg);
503 if (I != LocalAddrs.end()) {
Alexey Bataev7ba57af2017-10-17 16:47:34 +0000504 LValue LV = WrapperCGF.MakeAddrLValue(
505 I->second.second,
506 I->second.first ? I->second.first->getType() : Arg->getType(),
507 AlignmentSource::Decl);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000508 CallArg = WrapperCGF.EmitLoadOfScalar(LV, SourceLocation());
509 } else {
510 auto EI = VLASizes.find(Arg);
511 if (EI != VLASizes.end())
512 CallArg = EI->second.second;
513 else {
514 LValue LV = WrapperCGF.MakeAddrLValue(WrapperCGF.GetAddrOfLocalVar(Arg),
Ivan A. Kosarev5f8c0ca2017-10-10 09:39:32 +0000515 Arg->getType(),
516 AlignmentSource::Decl);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000517 CallArg = WrapperCGF.EmitLoadOfScalar(LV, SourceLocation());
518 }
519 }
Alexey Bataev7ba57af2017-10-17 16:47:34 +0000520 CallArgs.emplace_back(WrapperCGF.EmitFromMemory(CallArg, Arg->getType()));
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000521 }
Alexey Bataev3c595a62017-08-14 15:01:03 +0000522 CGM.getOpenMPRuntime().emitOutlinedFunctionCall(WrapperCGF, S.getLocStart(),
523 F, CallArgs);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000524 WrapperCGF.FinishFunction();
525 return WrapperF;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000526}
527
Alexey Bataev9959db52014-05-06 10:08:46 +0000528//===----------------------------------------------------------------------===//
529// OpenMP Directive Emission
530//===----------------------------------------------------------------------===//
Alexey Bataev420d45b2015-04-14 05:11:24 +0000531void CodeGenFunction::EmitOMPAggregateAssign(
John McCall7f416cc2015-09-08 08:05:57 +0000532 Address DestAddr, Address SrcAddr, QualType OriginalType,
533 const llvm::function_ref<void(Address, Address)> &CopyGen) {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000534 // Perform element-by-element initialization.
535 QualType ElementTy;
John McCall7f416cc2015-09-08 08:05:57 +0000536
537 // Drill down to the base element type on both arrays.
Alexey Bataev420d45b2015-04-14 05:11:24 +0000538 auto ArrayTy = OriginalType->getAsArrayTypeUnsafe();
John McCall7f416cc2015-09-08 08:05:57 +0000539 auto NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr);
540 SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
541
542 auto SrcBegin = SrcAddr.getPointer();
543 auto DestBegin = DestAddr.getPointer();
Alexey Bataev420d45b2015-04-14 05:11:24 +0000544 // Cast from pointer to array type to pointer to single element.
Alexey Bataev420d45b2015-04-14 05:11:24 +0000545 auto DestEnd = Builder.CreateGEP(DestBegin, NumElements);
546 // The basic structure here is a while-do loop.
547 auto BodyBB = createBasicBlock("omp.arraycpy.body");
548 auto DoneBB = createBasicBlock("omp.arraycpy.done");
549 auto IsEmpty =
550 Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty");
551 Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000552
Alexey Bataev420d45b2015-04-14 05:11:24 +0000553 // Enter the loop body, making that address the current address.
554 auto EntryBB = Builder.GetInsertBlock();
555 EmitBlock(BodyBB);
John McCall7f416cc2015-09-08 08:05:57 +0000556
557 CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy);
558
559 llvm::PHINode *SrcElementPHI =
560 Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast");
561 SrcElementPHI->addIncoming(SrcBegin, EntryBB);
562 Address SrcElementCurrent =
563 Address(SrcElementPHI,
564 SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize));
565
566 llvm::PHINode *DestElementPHI =
567 Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast");
568 DestElementPHI->addIncoming(DestBegin, EntryBB);
569 Address DestElementCurrent =
570 Address(DestElementPHI,
571 DestAddr.getAlignment().alignmentOfArrayElement(ElementSize));
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000572
Alexey Bataev420d45b2015-04-14 05:11:24 +0000573 // Emit copy.
574 CopyGen(DestElementCurrent, SrcElementCurrent);
575
576 // Shift the address forward by one element.
577 auto DestElementNext = Builder.CreateConstGEP1_32(
John McCall7f416cc2015-09-08 08:05:57 +0000578 DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
Alexey Bataev420d45b2015-04-14 05:11:24 +0000579 auto SrcElementNext = Builder.CreateConstGEP1_32(
John McCall7f416cc2015-09-08 08:05:57 +0000580 SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element");
Alexey Bataev420d45b2015-04-14 05:11:24 +0000581 // Check whether we've reached the end.
582 auto Done =
583 Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
584 Builder.CreateCondBr(Done, DoneBB, BodyBB);
John McCall7f416cc2015-09-08 08:05:57 +0000585 DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock());
586 SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock());
Alexey Bataev420d45b2015-04-14 05:11:24 +0000587
588 // Done.
589 EmitBlock(DoneBB, /*IsFinished=*/true);
590}
591
John McCall7f416cc2015-09-08 08:05:57 +0000592void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr,
593 Address SrcAddr, const VarDecl *DestVD,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000594 const VarDecl *SrcVD, const Expr *Copy) {
595 if (OriginalType->isArrayType()) {
596 auto *BO = dyn_cast<BinaryOperator>(Copy);
597 if (BO && BO->getOpcode() == BO_Assign) {
598 // Perform simple memcpy for simple copying.
John McCall7f416cc2015-09-08 08:05:57 +0000599 EmitAggregateAssign(DestAddr, SrcAddr, OriginalType);
Alexey Bataev420d45b2015-04-14 05:11:24 +0000600 } else {
601 // For arrays with complex element types perform element by element
602 // copying.
John McCall7f416cc2015-09-08 08:05:57 +0000603 EmitOMPAggregateAssign(
Alexey Bataev420d45b2015-04-14 05:11:24 +0000604 DestAddr, SrcAddr, OriginalType,
John McCall7f416cc2015-09-08 08:05:57 +0000605 [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000606 // Working with the single array element, so have to remap
607 // destination and source variables to corresponding array
608 // elements.
John McCall7f416cc2015-09-08 08:05:57 +0000609 CodeGenFunction::OMPPrivateScope Remap(*this);
610 Remap.addPrivate(DestVD, [DestElement]() -> Address {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000611 return DestElement;
612 });
613 Remap.addPrivate(
John McCall7f416cc2015-09-08 08:05:57 +0000614 SrcVD, [SrcElement]() -> Address { return SrcElement; });
Alexey Bataev420d45b2015-04-14 05:11:24 +0000615 (void)Remap.Privatize();
John McCall7f416cc2015-09-08 08:05:57 +0000616 EmitIgnoredExpr(Copy);
Alexey Bataev420d45b2015-04-14 05:11:24 +0000617 });
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000618 }
Alexey Bataev420d45b2015-04-14 05:11:24 +0000619 } else {
620 // Remap pseudo source variable to private copy.
John McCall7f416cc2015-09-08 08:05:57 +0000621 CodeGenFunction::OMPPrivateScope Remap(*this);
622 Remap.addPrivate(SrcVD, [SrcAddr]() -> Address { return SrcAddr; });
623 Remap.addPrivate(DestVD, [DestAddr]() -> Address { return DestAddr; });
Alexey Bataev420d45b2015-04-14 05:11:24 +0000624 (void)Remap.Privatize();
625 // Emit copying of the whole variable.
John McCall7f416cc2015-09-08 08:05:57 +0000626 EmitIgnoredExpr(Copy);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000627 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000628}
629
Alexey Bataev69c62a92015-04-15 04:52:20 +0000630bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
631 OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000632 if (!HaveInsertPoint())
633 return false;
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000634 bool FirstprivateIsLastprivate = false;
635 llvm::DenseSet<const VarDecl *> Lastprivates;
636 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
637 for (const auto *D : C->varlists())
638 Lastprivates.insert(
639 cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl());
640 }
Alexey Bataev69c62a92015-04-15 04:52:20 +0000641 llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate;
Alexey Bataev9afe5752016-05-24 07:40:12 +0000642 CGCapturedStmtInfo CapturesInfo(cast<CapturedStmt>(*D.getAssociatedStmt()));
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000643 for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) {
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000644 auto IRef = C->varlist_begin();
645 auto InitsRef = C->inits().begin();
646 for (auto IInit : C->private_copies()) {
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000647 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000648 bool ThisFirstprivateIsLastprivate =
649 Lastprivates.count(OrigVD->getCanonicalDecl()) > 0;
Alexey Bataev9afe5752016-05-24 07:40:12 +0000650 auto *CapFD = CapturesInfo.lookup(OrigVD);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000651 auto *FD = CapturedStmtInfo->lookup(OrigVD);
Alexey Bataev9afe5752016-05-24 07:40:12 +0000652 if (!ThisFirstprivateIsLastprivate && FD && (FD == CapFD) &&
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000653 !FD->getType()->isReferenceType()) {
654 EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl());
655 ++IRef;
656 ++InitsRef;
657 continue;
658 }
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000659 FirstprivateIsLastprivate =
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000660 FirstprivateIsLastprivate || ThisFirstprivateIsLastprivate;
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000661 if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000662 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
663 auto *VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl());
664 bool IsRegistered;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000665 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
666 /*RefersToEnclosingVariableOrCapture=*/FD != nullptr,
667 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
John McCall7f416cc2015-09-08 08:05:57 +0000668 Address OriginalAddr = EmitLValue(&DRE).getAddress();
Alexey Bataevfeddd642016-04-22 09:05:03 +0000669 QualType Type = VD->getType();
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000670 if (Type->isArrayType()) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000671 // Emit VarDecl with copy init for arrays.
672 // Get the address of the original variable captured in current
673 // captured region.
John McCall7f416cc2015-09-08 08:05:57 +0000674 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000675 auto Emission = EmitAutoVarAlloca(*VD);
676 auto *Init = VD->getInit();
677 if (!isa<CXXConstructExpr>(Init) || isTrivialInitializer(Init)) {
678 // Perform simple memcpy.
679 EmitAggregateAssign(Emission.getAllocatedAddress(), OriginalAddr,
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000680 Type);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000681 } else {
682 EmitOMPAggregateAssign(
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000683 Emission.getAllocatedAddress(), OriginalAddr, Type,
John McCall7f416cc2015-09-08 08:05:57 +0000684 [this, VDInit, Init](Address DestElement,
685 Address SrcElement) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000686 // Clean up any temporaries needed by the initialization.
687 RunCleanupsScope InitScope(*this);
688 // Emit initialization for single element.
John McCall7f416cc2015-09-08 08:05:57 +0000689 setAddrOfLocalVar(VDInit, SrcElement);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000690 EmitAnyExprToMem(Init, DestElement,
691 Init->getType().getQualifiers(),
692 /*IsInitializer*/ false);
693 LocalDeclMap.erase(VDInit);
694 });
695 }
696 EmitAutoVarCleanups(Emission);
697 return Emission.getAllocatedAddress();
698 });
699 } else {
John McCall7f416cc2015-09-08 08:05:57 +0000700 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000701 // Emit private VarDecl with copy init.
702 // Remap temp VDInit variable to the address of the original
703 // variable
704 // (for proper handling of captured global variables).
John McCall7f416cc2015-09-08 08:05:57 +0000705 setAddrOfLocalVar(VDInit, OriginalAddr);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000706 EmitDecl(*VD);
707 LocalDeclMap.erase(VDInit);
708 return GetAddrOfLocalVar(VD);
709 });
710 }
711 assert(IsRegistered &&
712 "firstprivate var already registered as private");
713 // Silence the warning about unused variable.
714 (void)IsRegistered;
715 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000716 ++IRef;
717 ++InitsRef;
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000718 }
719 }
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000720 return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000721}
722
Alexey Bataev03b340a2014-10-21 03:16:40 +0000723void CodeGenFunction::EmitOMPPrivateClause(
724 const OMPExecutableDirective &D,
725 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000726 if (!HaveInsertPoint())
727 return;
Alexey Bataev50a64582015-04-22 12:24:45 +0000728 llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000729 for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) {
Alexey Bataev03b340a2014-10-21 03:16:40 +0000730 auto IRef = C->varlist_begin();
731 for (auto IInit : C->private_copies()) {
732 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev50a64582015-04-22 12:24:45 +0000733 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
734 auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
735 bool IsRegistered =
John McCall7f416cc2015-09-08 08:05:57 +0000736 PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev50a64582015-04-22 12:24:45 +0000737 // Emit private VarDecl with copy init.
738 EmitDecl(*VD);
739 return GetAddrOfLocalVar(VD);
740 });
741 assert(IsRegistered && "private var already registered as private");
742 // Silence the warning about unused variable.
743 (void)IsRegistered;
744 }
Alexey Bataev03b340a2014-10-21 03:16:40 +0000745 ++IRef;
746 }
747 }
748}
749
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000750bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000751 if (!HaveInsertPoint())
752 return false;
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000753 // threadprivate_var1 = master_threadprivate_var1;
754 // operator=(threadprivate_var2, master_threadprivate_var2);
755 // ...
756 // __kmpc_barrier(&loc, global_tid);
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000757 llvm::DenseSet<const VarDecl *> CopiedVars;
758 llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000759 for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) {
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000760 auto IRef = C->varlist_begin();
761 auto ISrcRef = C->source_exprs().begin();
762 auto IDestRef = C->destination_exprs().begin();
763 for (auto *AssignOp : C->assignment_ops()) {
764 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000765 QualType Type = VD->getType();
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000766 if (CopiedVars.insert(VD->getCanonicalDecl()).second) {
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000767 // Get the address of the master variable. If we are emitting code with
768 // TLS support, the address is passed from the master as field in the
769 // captured declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000770 Address MasterAddr = Address::invalid();
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000771 if (getLangOpts().OpenMPUseTLS &&
772 getContext().getTargetInfo().isTLSSupported()) {
773 assert(CapturedStmtInfo->lookup(VD) &&
774 "Copyin threadprivates should have been captured!");
775 DeclRefExpr DRE(const_cast<VarDecl *>(VD), true, (*IRef)->getType(),
776 VK_LValue, (*IRef)->getExprLoc());
777 MasterAddr = EmitLValue(&DRE).getAddress();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000778 LocalDeclMap.erase(VD);
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000779 } else {
John McCall7f416cc2015-09-08 08:05:57 +0000780 MasterAddr =
781 Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD)
782 : CGM.GetAddrOfGlobal(VD),
783 getContext().getDeclAlign(VD));
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000784 }
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000785 // Get the address of the threadprivate variable.
John McCall7f416cc2015-09-08 08:05:57 +0000786 Address PrivateAddr = EmitLValue(*IRef).getAddress();
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000787 if (CopiedVars.size() == 1) {
788 // At first check if current thread is a master thread. If it is, no
789 // need to copy data.
790 CopyBegin = createBasicBlock("copyin.not.master");
791 CopyEnd = createBasicBlock("copyin.not.master.end");
792 Builder.CreateCondBr(
793 Builder.CreateICmpNE(
John McCall7f416cc2015-09-08 08:05:57 +0000794 Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy),
795 Builder.CreatePtrToInt(PrivateAddr.getPointer(), CGM.IntPtrTy)),
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000796 CopyBegin, CopyEnd);
797 EmitBlock(CopyBegin);
798 }
799 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
800 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +0000801 EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp);
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000802 }
803 ++IRef;
804 ++ISrcRef;
805 ++IDestRef;
806 }
807 }
808 if (CopyEnd) {
809 // Exit out of copying procedure for non-master thread.
810 EmitBlock(CopyEnd, /*IsFinished=*/true);
811 return true;
812 }
813 return false;
814}
815
Alexey Bataev38e89532015-04-16 04:54:05 +0000816bool CodeGenFunction::EmitOMPLastprivateClauseInit(
817 const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000818 if (!HaveInsertPoint())
819 return false;
Alexey Bataev38e89532015-04-16 04:54:05 +0000820 bool HasAtLeastOneLastprivate = false;
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000821 llvm::DenseSet<const VarDecl *> SIMDLCVs;
822 if (isOpenMPSimdDirective(D.getDirectiveKind())) {
823 auto *LoopDirective = cast<OMPLoopDirective>(&D);
824 for (auto *C : LoopDirective->counters()) {
825 SIMDLCVs.insert(
826 cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl());
827 }
828 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000829 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000830 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
Alexey Bataevd130fd12015-05-13 10:23:02 +0000831 HasAtLeastOneLastprivate = true;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000832 if (isOpenMPTaskLoopDirective(D.getDirectiveKind()))
833 break;
Alexey Bataev38e89532015-04-16 04:54:05 +0000834 auto IRef = C->varlist_begin();
835 auto IDestRef = C->destination_exprs().begin();
836 for (auto *IInit : C->private_copies()) {
837 // Keep the address of the original variable for future update at the end
838 // of the loop.
839 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataevf93095a2016-05-05 08:46:22 +0000840 // Taskloops do not require additional initialization, it is done in
841 // runtime support library.
Alexey Bataev38e89532015-04-16 04:54:05 +0000842 if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) {
843 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +0000844 PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() -> Address {
Alexey Bataev38e89532015-04-16 04:54:05 +0000845 DeclRefExpr DRE(
846 const_cast<VarDecl *>(OrigVD),
847 /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup(
848 OrigVD) != nullptr,
849 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
850 return EmitLValue(&DRE).getAddress();
851 });
852 // Check if the variable is also a firstprivate: in this case IInit is
853 // not generated. Initialization of this variable will happen in codegen
854 // for 'firstprivate' clause.
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000855 if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) {
Alexey Bataevd130fd12015-05-13 10:23:02 +0000856 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
Alexey Bataevf93095a2016-05-05 08:46:22 +0000857 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
858 // Emit private VarDecl with copy init.
859 EmitDecl(*VD);
860 return GetAddrOfLocalVar(VD);
861 });
Alexey Bataevd130fd12015-05-13 10:23:02 +0000862 assert(IsRegistered &&
863 "lastprivate var already registered as private");
864 (void)IsRegistered;
865 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000866 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000867 ++IRef;
868 ++IDestRef;
Alexey Bataev38e89532015-04-16 04:54:05 +0000869 }
870 }
871 return HasAtLeastOneLastprivate;
872}
873
874void CodeGenFunction::EmitOMPLastprivateClauseFinal(
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000875 const OMPExecutableDirective &D, bool NoFinals,
876 llvm::Value *IsLastIterCond) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000877 if (!HaveInsertPoint())
878 return;
Alexey Bataev38e89532015-04-16 04:54:05 +0000879 // Emit following code:
880 // if (<IsLastIterCond>) {
881 // orig_var1 = private_orig_var1;
882 // ...
883 // orig_varn = private_orig_varn;
884 // }
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000885 llvm::BasicBlock *ThenBB = nullptr;
886 llvm::BasicBlock *DoneBB = nullptr;
887 if (IsLastIterCond) {
888 ThenBB = createBasicBlock(".omp.lastprivate.then");
889 DoneBB = createBasicBlock(".omp.lastprivate.done");
890 Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB);
891 EmitBlock(ThenBB);
892 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000893 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
894 llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates;
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000895 if (auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) {
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000896 auto IC = LoopDirective->counters().begin();
897 for (auto F : LoopDirective->finals()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000898 auto *D =
899 cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl();
900 if (NoFinals)
901 AlreadyEmittedVars.insert(D);
902 else
903 LoopCountersAndUpdates[D] = F;
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000904 ++IC;
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000905 }
906 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000907 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
908 auto IRef = C->varlist_begin();
909 auto ISrcRef = C->source_exprs().begin();
910 auto IDestRef = C->destination_exprs().begin();
911 for (auto *AssignOp : C->assignment_ops()) {
912 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
913 QualType Type = PrivateVD->getType();
914 auto *CanonicalVD = PrivateVD->getCanonicalDecl();
915 if (AlreadyEmittedVars.insert(CanonicalVD).second) {
916 // If lastprivate variable is a loop control variable for loop-based
917 // directive, update its value before copyin back to original
918 // variable.
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000919 if (auto *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD))
920 EmitIgnoredExpr(FinalExpr);
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000921 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
922 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
923 // Get the address of the original variable.
924 Address OriginalAddr = GetAddrOfLocalVar(DestVD);
925 // Get the address of the private variable.
926 Address PrivateAddr = GetAddrOfLocalVar(PrivateVD);
927 if (auto RefTy = PrivateVD->getType()->getAs<ReferenceType>())
928 PrivateAddr =
John McCall7f416cc2015-09-08 08:05:57 +0000929 Address(Builder.CreateLoad(PrivateAddr),
930 getNaturalTypeAlignment(RefTy->getPointeeType()));
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000931 EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp);
Alexey Bataev38e89532015-04-16 04:54:05 +0000932 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000933 ++IRef;
934 ++ISrcRef;
935 ++IDestRef;
Alexey Bataev38e89532015-04-16 04:54:05 +0000936 }
Alexey Bataev005248a2016-02-25 05:25:57 +0000937 if (auto *PostUpdate = C->getPostUpdateExpr())
938 EmitIgnoredExpr(PostUpdate);
Alexey Bataev38e89532015-04-16 04:54:05 +0000939 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000940 if (IsLastIterCond)
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000941 EmitBlock(DoneBB, /*IsFinished=*/true);
Alexey Bataev38e89532015-04-16 04:54:05 +0000942}
943
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000944void CodeGenFunction::EmitOMPReductionClauseInit(
945 const OMPExecutableDirective &D,
946 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000947 if (!HaveInsertPoint())
948 return;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000949 SmallVector<const Expr *, 4> Shareds;
950 SmallVector<const Expr *, 4> Privates;
951 SmallVector<const Expr *, 4> ReductionOps;
952 SmallVector<const Expr *, 4> LHSs;
953 SmallVector<const Expr *, 4> RHSs;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000954 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000955 auto IPriv = C->privates().begin();
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000956 auto IRed = C->reduction_ops().begin();
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000957 auto ILHS = C->lhs_exprs().begin();
958 auto IRHS = C->rhs_exprs().begin();
959 for (const auto *Ref : C->varlists()) {
960 Shareds.emplace_back(Ref);
961 Privates.emplace_back(*IPriv);
962 ReductionOps.emplace_back(*IRed);
963 LHSs.emplace_back(*ILHS);
964 RHSs.emplace_back(*IRHS);
965 std::advance(IPriv, 1);
966 std::advance(IRed, 1);
967 std::advance(ILHS, 1);
968 std::advance(IRHS, 1);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000969 }
970 }
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000971 ReductionCodeGen RedCG(Shareds, Privates, ReductionOps);
972 unsigned Count = 0;
973 auto ILHS = LHSs.begin();
974 auto IRHS = RHSs.begin();
975 auto IPriv = Privates.begin();
976 for (const auto *IRef : Shareds) {
977 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl());
978 // Emit private VarDecl with reduction init.
979 RedCG.emitSharedLValue(*this, Count);
980 RedCG.emitAggregateType(*this, Count);
981 auto Emission = EmitAutoVarAlloca(*PrivateVD);
982 RedCG.emitInitialization(*this, Count, Emission.getAllocatedAddress(),
983 RedCG.getSharedLValue(Count),
984 [&Emission](CodeGenFunction &CGF) {
985 CGF.EmitAutoVarInit(Emission);
986 return true;
987 });
988 EmitAutoVarCleanups(Emission);
989 Address BaseAddr = RedCG.adjustPrivateAddress(
990 *this, Count, Emission.getAllocatedAddress());
991 bool IsRegistered = PrivateScope.addPrivate(
992 RedCG.getBaseDecl(Count), [BaseAddr]() -> Address { return BaseAddr; });
993 assert(IsRegistered && "private var already registered as private");
994 // Silence the warning about unused variable.
995 (void)IsRegistered;
996
997 auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
998 auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
Eric Christopher7aba9782017-07-14 01:42:57 +0000999 if (isa<OMPArraySectionExpr>(IRef)) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001000 // Store the address of the original variable associated with the LHS
1001 // implicit variable.
1002 PrivateScope.addPrivate(LHSVD, [&RedCG, Count]() -> Address {
1003 return RedCG.getSharedLValue(Count).getAddress();
1004 });
1005 PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address {
1006 return GetAddrOfLocalVar(PrivateVD);
1007 });
Eric Christopher7aba9782017-07-14 01:42:57 +00001008 } else if (isa<ArraySubscriptExpr>(IRef)) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +00001009 // Store the address of the original variable associated with the LHS
1010 // implicit variable.
1011 PrivateScope.addPrivate(LHSVD, [&RedCG, Count]() -> Address {
1012 return RedCG.getSharedLValue(Count).getAddress();
1013 });
1014 PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address {
1015 return Builder.CreateElementBitCast(GetAddrOfLocalVar(PrivateVD),
1016 ConvertTypeForMem(RHSVD->getType()),
1017 "rhs.begin");
1018 });
1019 } else {
1020 QualType Type = PrivateVD->getType();
1021 bool IsArray = getContext().getAsArrayType(Type) != nullptr;
1022 Address OriginalAddr = RedCG.getSharedLValue(Count).getAddress();
1023 // Store the address of the original variable associated with the LHS
1024 // implicit variable.
1025 if (IsArray) {
1026 OriginalAddr = Builder.CreateElementBitCast(
1027 OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin");
1028 }
1029 PrivateScope.addPrivate(
1030 LHSVD, [OriginalAddr]() -> Address { return OriginalAddr; });
1031 PrivateScope.addPrivate(
1032 RHSVD, [this, PrivateVD, RHSVD, IsArray]() -> Address {
1033 return IsArray
1034 ? Builder.CreateElementBitCast(
1035 GetAddrOfLocalVar(PrivateVD),
1036 ConvertTypeForMem(RHSVD->getType()), "rhs.begin")
1037 : GetAddrOfLocalVar(PrivateVD);
1038 });
1039 }
1040 ++ILHS;
1041 ++IRHS;
1042 ++IPriv;
1043 ++Count;
1044 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001045}
1046
1047void CodeGenFunction::EmitOMPReductionClauseFinal(
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001048 const OMPExecutableDirective &D, const OpenMPDirectiveKind ReductionKind) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001049 if (!HaveInsertPoint())
1050 return;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001051 llvm::SmallVector<const Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001052 llvm::SmallVector<const Expr *, 8> LHSExprs;
1053 llvm::SmallVector<const Expr *, 8> RHSExprs;
1054 llvm::SmallVector<const Expr *, 8> ReductionOps;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001055 bool HasAtLeastOneReduction = false;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001056 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001057 HasAtLeastOneReduction = true;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001058 Privates.append(C->privates().begin(), C->privates().end());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001059 LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end());
1060 RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end());
1061 ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end());
1062 }
1063 if (HasAtLeastOneReduction) {
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001064 bool WithNowait = D.getSingleClause<OMPNowaitClause>() ||
1065 isOpenMPParallelDirective(D.getDirectiveKind()) ||
1066 D.getDirectiveKind() == OMPD_simd;
1067 bool SimpleReduction = D.getDirectiveKind() == OMPD_simd;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001068 // Emit nowait reduction if nowait clause is present or directive is a
1069 // parallel directive (it always has implicit barrier).
1070 CGM.getOpenMPRuntime().emitReduction(
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001071 *this, D.getLocEnd(), Privates, LHSExprs, RHSExprs, ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001072 {WithNowait, SimpleReduction, ReductionKind});
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001073 }
1074}
1075
Alexey Bataev61205072016-03-02 04:57:40 +00001076static void emitPostUpdateForReductionClause(
1077 CodeGenFunction &CGF, const OMPExecutableDirective &D,
1078 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
1079 if (!CGF.HaveInsertPoint())
1080 return;
1081 llvm::BasicBlock *DoneBB = nullptr;
1082 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
1083 if (auto *PostUpdate = C->getPostUpdateExpr()) {
1084 if (!DoneBB) {
1085 if (auto *Cond = CondGen(CGF)) {
1086 // If the first post-update expression is found, emit conditional
1087 // block if it was requested.
1088 auto *ThenBB = CGF.createBasicBlock(".omp.reduction.pu");
1089 DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done");
1090 CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1091 CGF.EmitBlock(ThenBB);
1092 }
1093 }
1094 CGF.EmitIgnoredExpr(PostUpdate);
1095 }
1096 }
1097 if (DoneBB)
1098 CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
1099}
1100
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001101namespace {
1102/// Codegen lambda for appending distribute lower and upper bounds to outlined
1103/// parallel function. This is necessary for combined constructs such as
1104/// 'distribute parallel for'
1105typedef llvm::function_ref<void(CodeGenFunction &,
1106 const OMPExecutableDirective &,
1107 llvm::SmallVectorImpl<llvm::Value *> &)>
1108 CodeGenBoundParametersTy;
1109} // anonymous namespace
1110
1111static void emitCommonOMPParallelDirective(
1112 CodeGenFunction &CGF, const OMPExecutableDirective &S,
1113 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1114 const CodeGenBoundParametersTy &CodeGenBoundParameters) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00001115 const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel);
1116 auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction(
1117 S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001118 if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) {
Alexey Bataev1d677132015-04-22 13:57:31 +00001119 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
Alexey Bataev1d677132015-04-22 13:57:31 +00001120 auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
1121 /*IgnoreResultAssign*/ true);
1122 CGF.CGM.getOpenMPRuntime().emitNumThreadsClause(
1123 CGF, NumThreads, NumThreadsClause->getLocStart());
1124 }
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001125 if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001126 CodeGenFunction::RunCleanupsScope ProcBindScope(CGF);
Alexey Bataev7f210c62015-06-18 13:40:03 +00001127 CGF.CGM.getOpenMPRuntime().emitProcBindClause(
1128 CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getLocStart());
1129 }
Alexey Bataev1d677132015-04-22 13:57:31 +00001130 const Expr *IfCond = nullptr;
Alexey Bataev7371aa32015-09-03 08:45:56 +00001131 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
1132 if (C->getNameModifier() == OMPD_unknown ||
1133 C->getNameModifier() == OMPD_parallel) {
1134 IfCond = C->getCondition();
1135 break;
1136 }
Alexey Bataev1d677132015-04-22 13:57:31 +00001137 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001138
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00001139 OMPParallelScope Scope(CGF, S);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001140 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001141 // Combining 'distribute' with 'for' requires sharing each 'distribute' chunk
1142 // lower and upper bounds with the pragma 'for' chunking mechanism.
1143 // The following lambda takes care of appending the lower and upper bound
1144 // parameters when necessary
1145 CodeGenBoundParameters(CGF, S, CapturedVars);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001146 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
Alexey Bataev1d677132015-04-22 13:57:31 +00001147 CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +00001148 CapturedVars, IfCond);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001149}
1150
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001151static void emitEmptyBoundParameters(CodeGenFunction &,
1152 const OMPExecutableDirective &,
1153 llvm::SmallVectorImpl<llvm::Value *> &) {}
1154
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001155void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001156 // Emit parallel region as a standalone region.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001157 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001158 OMPPrivateScope PrivateScope(CGF);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001159 bool Copyins = CGF.EmitOMPCopyinClause(S);
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00001160 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
1161 if (Copyins) {
Alexey Bataev69c62a92015-04-15 04:52:20 +00001162 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00001163 // propagation master's thread values of threadprivate variables to local
1164 // instances of that variables of all other implicit threads.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001165 CGF.CGM.getOpenMPRuntime().emitBarrierCall(
1166 CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
1167 /*ForceSimpleCall=*/true);
Alexey Bataev69c62a92015-04-15 04:52:20 +00001168 }
1169 CGF.EmitOMPPrivateClause(S, PrivateScope);
1170 CGF.EmitOMPReductionClauseInit(S, PrivateScope);
1171 (void)PrivateScope.Privatize();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001172 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001173 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001174 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001175 emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen,
1176 emitEmptyBoundParameters);
Alexey Bataev61205072016-03-02 04:57:40 +00001177 emitPostUpdateForReductionClause(
1178 *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev9959db52014-05-06 10:08:46 +00001179}
Alexander Musman515ad8c2014-05-22 08:54:05 +00001180
Alexey Bataev0f34da12015-07-02 04:17:07 +00001181void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D,
1182 JumpDest LoopExit) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00001183 RunCleanupsScope BodyScope(*this);
1184 // Update counters values on current iteration.
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001185 for (auto I : D.updates()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00001186 EmitIgnoredExpr(I);
1187 }
Alexander Musman3276a272015-03-21 10:12:56 +00001188 // Update the linear variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001189 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001190 for (auto *U : C->updates())
Alexander Musman3276a272015-03-21 10:12:56 +00001191 EmitIgnoredExpr(U);
Alexander Musman3276a272015-03-21 10:12:56 +00001192 }
1193
Alexander Musmana5f070a2014-10-01 06:03:56 +00001194 // On a continue in the body, jump to the end.
Alexander Musmand196ef22014-10-07 08:57:09 +00001195 auto Continue = getJumpDestInCurrentScope("omp.body.continue");
Alexey Bataev0f34da12015-07-02 04:17:07 +00001196 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
Alexander Musmana5f070a2014-10-01 06:03:56 +00001197 // Emit loop body.
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001198 EmitStmt(D.getBody());
Alexander Musmana5f070a2014-10-01 06:03:56 +00001199 // The end (updates/cleanups).
1200 EmitBlock(Continue.getBlock());
1201 BreakContinueStack.pop_back();
Alexander Musmana5f070a2014-10-01 06:03:56 +00001202}
1203
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001204void CodeGenFunction::EmitOMPInnerLoop(
1205 const Stmt &S, bool RequiresCleanup, const Expr *LoopCond,
1206 const Expr *IncExpr,
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001207 const llvm::function_ref<void(CodeGenFunction &)> &BodyGen,
1208 const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) {
Alexander Musmand196ef22014-10-07 08:57:09 +00001209 auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001210
1211 // Start the loop with a block that tests the condition.
Alexander Musmand196ef22014-10-07 08:57:09 +00001212 auto CondBlock = createBasicBlock("omp.inner.for.cond");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001213 EmitBlock(CondBlock);
Amara Emerson652795d2016-11-10 14:44:30 +00001214 const SourceRange &R = S.getSourceRange();
1215 LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()),
1216 SourceLocToDebugLoc(R.getEnd()));
Alexander Musmana5f070a2014-10-01 06:03:56 +00001217
1218 // If there are any cleanups between here and the loop-exit scope,
1219 // create a block to stage a loop exit along.
1220 auto ExitBlock = LoopExit.getBlock();
Alexey Bataev2df54a02015-03-12 08:53:29 +00001221 if (RequiresCleanup)
Alexander Musmand196ef22014-10-07 08:57:09 +00001222 ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001223
Alexander Musmand196ef22014-10-07 08:57:09 +00001224 auto LoopBody = createBasicBlock("omp.inner.for.body");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001225
Alexey Bataev2df54a02015-03-12 08:53:29 +00001226 // Emit condition.
Justin Bogner66242d62015-04-23 23:06:47 +00001227 EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S));
Alexander Musmana5f070a2014-10-01 06:03:56 +00001228 if (ExitBlock != LoopExit.getBlock()) {
1229 EmitBlock(ExitBlock);
1230 EmitBranchThroughCleanup(LoopExit);
1231 }
1232
1233 EmitBlock(LoopBody);
Justin Bogner66242d62015-04-23 23:06:47 +00001234 incrementProfileCounter(&S);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001235
1236 // Create a block for the increment.
Alexander Musmand196ef22014-10-07 08:57:09 +00001237 auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001238 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1239
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001240 BodyGen(*this);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001241
1242 // Emit "IV = IV + 1" and a back-edge to the condition block.
1243 EmitBlock(Continue.getBlock());
Alexey Bataev2df54a02015-03-12 08:53:29 +00001244 EmitIgnoredExpr(IncExpr);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001245 PostIncGen(*this);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001246 BreakContinueStack.pop_back();
1247 EmitBranch(CondBlock);
1248 LoopStack.pop();
1249 // Emit the fall-through block.
1250 EmitBlock(LoopExit.getBlock());
1251}
1252
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00001253bool CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001254 if (!HaveInsertPoint())
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00001255 return false;
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001256 // Emit inits for the linear variables.
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00001257 bool HasLinears = false;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001258 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001259 for (auto *Init : C->inits()) {
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00001260 HasLinears = true;
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001261 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl());
Alexey Bataevef549a82016-03-09 09:49:09 +00001262 if (auto *Ref = dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) {
1263 AutoVarEmission Emission = EmitAutoVarAlloca(*VD);
1264 auto *OrigVD = cast<VarDecl>(Ref->getDecl());
1265 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
1266 CapturedStmtInfo->lookup(OrigVD) != nullptr,
1267 VD->getInit()->getType(), VK_LValue,
1268 VD->getInit()->getExprLoc());
1269 EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(),
1270 VD->getType()),
1271 /*capturedByInit=*/false);
1272 EmitAutoVarCleanups(Emission);
1273 } else
1274 EmitVarDecl(*VD);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001275 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001276 // Emit the linear steps for the linear clauses.
1277 // If a step is not constant, it is pre-calculated before the loop.
1278 if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep()))
1279 if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) {
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001280 EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl()));
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001281 // Emit calculation of the linear step.
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001282 EmitIgnoredExpr(CS);
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001283 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001284 }
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00001285 return HasLinears;
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001286}
1287
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001288void CodeGenFunction::EmitOMPLinearClauseFinal(
1289 const OMPLoopDirective &D,
Alexey Bataevef549a82016-03-09 09:49:09 +00001290 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001291 if (!HaveInsertPoint())
Alexey Bataev8ef31412015-12-18 07:58:25 +00001292 return;
Alexey Bataevef549a82016-03-09 09:49:09 +00001293 llvm::BasicBlock *DoneBB = nullptr;
Alexander Musman3276a272015-03-21 10:12:56 +00001294 // Emit the final values of the linear variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001295 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataev39f915b82015-05-08 10:41:21 +00001296 auto IC = C->varlist_begin();
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001297 for (auto *F : C->finals()) {
Alexey Bataevef549a82016-03-09 09:49:09 +00001298 if (!DoneBB) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001299 if (auto *Cond = CondGen(*this)) {
Alexey Bataevef549a82016-03-09 09:49:09 +00001300 // If the first post-update expression is found, emit conditional
1301 // block if it was requested.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001302 auto *ThenBB = createBasicBlock(".omp.linear.pu");
1303 DoneBB = createBasicBlock(".omp.linear.pu.done");
1304 Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1305 EmitBlock(ThenBB);
Alexey Bataevef549a82016-03-09 09:49:09 +00001306 }
1307 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00001308 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl());
1309 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001310 CapturedStmtInfo->lookup(OrigVD) != nullptr,
Alexey Bataev39f915b82015-05-08 10:41:21 +00001311 (*IC)->getType(), VK_LValue, (*IC)->getExprLoc());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001312 Address OrigAddr = EmitLValue(&DRE).getAddress();
1313 CodeGenFunction::OMPPrivateScope VarScope(*this);
1314 VarScope.addPrivate(OrigVD, [OrigAddr]() -> Address { return OrigAddr; });
Alexey Bataev39f915b82015-05-08 10:41:21 +00001315 (void)VarScope.Privatize();
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001316 EmitIgnoredExpr(F);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001317 ++IC;
Alexander Musman3276a272015-03-21 10:12:56 +00001318 }
Alexey Bataev78849fb2016-03-09 09:49:00 +00001319 if (auto *PostUpdate = C->getPostUpdateExpr())
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001320 EmitIgnoredExpr(PostUpdate);
Alexander Musman3276a272015-03-21 10:12:56 +00001321 }
Alexey Bataevef549a82016-03-09 09:49:09 +00001322 if (DoneBB)
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001323 EmitBlock(DoneBB, /*IsFinished=*/true);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001324}
1325
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001326static void emitAlignedClause(CodeGenFunction &CGF,
1327 const OMPExecutableDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001328 if (!CGF.HaveInsertPoint())
1329 return;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001330 for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001331 unsigned ClauseAlignment = 0;
1332 if (auto AlignmentExpr = Clause->getAlignment()) {
1333 auto AlignmentCI =
1334 cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr));
1335 ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue());
Alexander Musman09184fe2014-09-30 05:29:28 +00001336 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001337 for (auto E : Clause->varlists()) {
1338 unsigned Alignment = ClauseAlignment;
1339 if (Alignment == 0) {
1340 // OpenMP [2.8.1, Description]
1341 // If no optional parameter is specified, implementation-defined default
1342 // alignments for SIMD instructions on the target platforms are assumed.
1343 Alignment =
Alexey Bataev00396512015-07-02 03:40:19 +00001344 CGF.getContext()
1345 .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign(
1346 E->getType()->getPointeeType()))
1347 .getQuantity();
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001348 }
1349 assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) &&
1350 "alignment is not power of 2");
1351 if (Alignment != 0) {
1352 llvm::Value *PtrValue = CGF.EmitScalarExpr(E);
1353 CGF.EmitAlignmentAssumption(PtrValue, Alignment);
1354 }
Alexander Musman09184fe2014-09-30 05:29:28 +00001355 }
1356 }
1357}
1358
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001359void CodeGenFunction::EmitOMPPrivateLoopCounters(
1360 const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) {
1361 if (!HaveInsertPoint())
Alexey Bataev8ef31412015-12-18 07:58:25 +00001362 return;
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001363 auto I = S.private_counters().begin();
1364 for (auto *E : S.counters()) {
Alexey Bataeva8899172015-08-06 12:30:57 +00001365 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1366 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001367 (void)LoopScope.addPrivate(VD, [&]() -> Address {
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001368 // Emit var without initialization.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001369 if (!LocalDeclMap.count(PrivateVD)) {
1370 auto VarEmission = EmitAutoVarAlloca(*PrivateVD);
1371 EmitAutoVarCleanups(VarEmission);
1372 }
1373 DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD),
1374 /*RefersToEnclosingVariableOrCapture=*/false,
1375 (*I)->getType(), VK_LValue, (*I)->getExprLoc());
1376 return EmitLValue(&DRE).getAddress();
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001377 });
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001378 if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) ||
1379 VD->hasGlobalStorage()) {
1380 (void)LoopScope.addPrivate(PrivateVD, [&]() -> Address {
1381 DeclRefExpr DRE(const_cast<VarDecl *>(VD),
1382 LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD),
1383 E->getType(), VK_LValue, E->getExprLoc());
1384 return EmitLValue(&DRE).getAddress();
1385 });
1386 }
Alexey Bataeva8899172015-08-06 12:30:57 +00001387 ++I;
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001388 }
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001389}
1390
Alexey Bataev62dbb972015-04-22 11:59:37 +00001391static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S,
1392 const Expr *Cond, llvm::BasicBlock *TrueBlock,
1393 llvm::BasicBlock *FalseBlock, uint64_t TrueCount) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001394 if (!CGF.HaveInsertPoint())
1395 return;
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001396 {
1397 CodeGenFunction::OMPPrivateScope PreCondScope(CGF);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001398 CGF.EmitOMPPrivateLoopCounters(S, PreCondScope);
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001399 (void)PreCondScope.Privatize();
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001400 // Get initial values of real counters.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00001401 for (auto I : S.inits()) {
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001402 CGF.EmitIgnoredExpr(I);
1403 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00001404 }
1405 // Check that loop is executed at least one time.
1406 CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount);
1407}
1408
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001409void CodeGenFunction::EmitOMPLinearClause(
1410 const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) {
1411 if (!HaveInsertPoint())
Alexey Bataev8ef31412015-12-18 07:58:25 +00001412 return;
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001413 llvm::DenseSet<const VarDecl *> SIMDLCVs;
1414 if (isOpenMPSimdDirective(D.getDirectiveKind())) {
1415 auto *LoopDirective = cast<OMPLoopDirective>(&D);
1416 for (auto *C : LoopDirective->counters()) {
1417 SIMDLCVs.insert(
1418 cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl());
1419 }
1420 }
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001421 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001422 auto CurPrivate = C->privates().begin();
Alexey Bataevc925aa32015-04-27 08:00:32 +00001423 for (auto *E : C->varlists()) {
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001424 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1425 auto *PrivateVD =
1426 cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001427 if (!SIMDLCVs.count(VD->getCanonicalDecl())) {
1428 bool IsRegistered = PrivateScope.addPrivate(VD, [&]() -> Address {
1429 // Emit private VarDecl with copy init.
1430 EmitVarDecl(*PrivateVD);
1431 return GetAddrOfLocalVar(PrivateVD);
1432 });
1433 assert(IsRegistered && "linear var already registered as private");
1434 // Silence the warning about unused variable.
1435 (void)IsRegistered;
1436 } else
1437 EmitVarDecl(*PrivateVD);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001438 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00001439 }
1440 }
1441}
1442
Alexey Bataev45bfad52015-08-21 12:19:04 +00001443static void emitSimdlenSafelenClause(CodeGenFunction &CGF,
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001444 const OMPExecutableDirective &D,
1445 bool IsMonotonic) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001446 if (!CGF.HaveInsertPoint())
1447 return;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001448 if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) {
Alexey Bataev45bfad52015-08-21 12:19:04 +00001449 RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(),
1450 /*ignoreResult=*/true);
1451 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
1452 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
1453 // In presence of finite 'safelen', it may be unsafe to mark all
1454 // the memory instructions parallel, because loop-carried
1455 // dependences of 'safelen' iterations are possible.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001456 if (!IsMonotonic)
1457 CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>());
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001458 } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001459 RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(),
1460 /*ignoreResult=*/true);
1461 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
Tyler Nowickida46d0e2015-07-14 23:03:09 +00001462 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001463 // In presence of finite 'safelen', it may be unsafe to mark all
1464 // the memory instructions parallel, because loop-carried
1465 // dependences of 'safelen' iterations are possible.
1466 CGF.LoopStack.setParallel(false);
1467 }
1468}
1469
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001470void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D,
1471 bool IsMonotonic) {
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001472 // Walk clauses and process safelen/lastprivate.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001473 LoopStack.setParallel(!IsMonotonic);
Tyler Nowickida46d0e2015-07-14 23:03:09 +00001474 LoopStack.setVectorizeEnable(true);
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001475 emitSimdlenSafelenClause(*this, D, IsMonotonic);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001476}
1477
Alexey Bataevef549a82016-03-09 09:49:09 +00001478void CodeGenFunction::EmitOMPSimdFinal(
1479 const OMPLoopDirective &D,
1480 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001481 if (!HaveInsertPoint())
1482 return;
Alexey Bataevef549a82016-03-09 09:49:09 +00001483 llvm::BasicBlock *DoneBB = nullptr;
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001484 auto IC = D.counters().begin();
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001485 auto IPC = D.private_counters().begin();
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001486 for (auto F : D.finals()) {
1487 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001488 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl());
1489 auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD);
1490 if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) ||
1491 OrigVD->hasGlobalStorage() || CED) {
Alexey Bataevef549a82016-03-09 09:49:09 +00001492 if (!DoneBB) {
1493 if (auto *Cond = CondGen(*this)) {
1494 // If the first post-update expression is found, emit conditional
1495 // block if it was requested.
1496 auto *ThenBB = createBasicBlock(".omp.final.then");
1497 DoneBB = createBasicBlock(".omp.final.done");
1498 Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1499 EmitBlock(ThenBB);
1500 }
1501 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001502 Address OrigAddr = Address::invalid();
1503 if (CED)
1504 OrigAddr = EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress();
1505 else {
1506 DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD),
1507 /*RefersToEnclosingVariableOrCapture=*/false,
1508 (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc());
1509 OrigAddr = EmitLValue(&DRE).getAddress();
1510 }
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001511 OMPPrivateScope VarScope(*this);
1512 VarScope.addPrivate(OrigVD,
John McCall7f416cc2015-09-08 08:05:57 +00001513 [OrigAddr]() -> Address { return OrigAddr; });
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001514 (void)VarScope.Privatize();
1515 EmitIgnoredExpr(F);
1516 }
1517 ++IC;
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001518 ++IPC;
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001519 }
Alexey Bataevef549a82016-03-09 09:49:09 +00001520 if (DoneBB)
1521 EmitBlock(DoneBB, /*IsFinished=*/true);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001522}
1523
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001524static void emitOMPLoopBodyWithStopPoint(CodeGenFunction &CGF,
1525 const OMPLoopDirective &S,
1526 CodeGenFunction::JumpDest LoopExit) {
1527 CGF.EmitOMPLoopBody(S, LoopExit);
1528 CGF.EmitStopPoint(&S);
Hans Wennborged129ae2017-04-27 17:02:25 +00001529}
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001530
Alexander Musman515ad8c2014-05-22 08:54:05 +00001531void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001532 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00001533 OMPLoopScope PreInitScope(CGF, S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001534 // if (PreCond) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001535 // for (IV in 0..LastIteration) BODY;
1536 // <Final counter/linear vars updates>;
Alexey Bataev62dbb972015-04-22 11:59:37 +00001537 // }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001538 //
Alexander Musmana5f070a2014-10-01 06:03:56 +00001539
Alexey Bataev62dbb972015-04-22 11:59:37 +00001540 // Emit: if (PreCond) - begin.
1541 // If the condition constant folds and can be elided, avoid emitting the
1542 // whole loop.
1543 bool CondConstant;
1544 llvm::BasicBlock *ContBlock = nullptr;
1545 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
1546 if (!CondConstant)
1547 return;
1548 } else {
Alexey Bataev62dbb972015-04-22 11:59:37 +00001549 auto *ThenBlock = CGF.createBasicBlock("simd.if.then");
1550 ContBlock = CGF.createBasicBlock("simd.if.end");
Justin Bogner66242d62015-04-23 23:06:47 +00001551 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
1552 CGF.getProfileCount(&S));
Alexey Bataev62dbb972015-04-22 11:59:37 +00001553 CGF.EmitBlock(ThenBlock);
Justin Bogner66242d62015-04-23 23:06:47 +00001554 CGF.incrementProfileCounter(&S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001555 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001556
1557 // Emit the loop iteration variable.
1558 const Expr *IVExpr = S.getIterationVariable();
1559 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
1560 CGF.EmitVarDecl(*IVDecl);
1561 CGF.EmitIgnoredExpr(S.getInit());
1562
1563 // Emit the iterations count variable.
1564 // If it is not a variable, Sema decided to calculate iterations count on
Alexey Bataev7a228ff2015-05-21 07:59:51 +00001565 // each iteration (e.g., it is foldable into a constant).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001566 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
1567 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
1568 // Emit calculation of the iterations count.
1569 CGF.EmitIgnoredExpr(S.getCalcLastIteration());
Alexander Musmana5f070a2014-10-01 06:03:56 +00001570 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001571
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001572 CGF.EmitOMPSimdInit(S);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001573
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001574 emitAlignedClause(CGF, S);
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00001575 (void)CGF.EmitOMPLinearClauseInit(S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001576 {
1577 OMPPrivateScope LoopScope(CGF);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001578 CGF.EmitOMPPrivateLoopCounters(S, LoopScope);
1579 CGF.EmitOMPLinearClause(S, LoopScope);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001580 CGF.EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00001581 CGF.EmitOMPReductionClauseInit(S, LoopScope);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001582 bool HasLastprivateClause =
1583 CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001584 (void)LoopScope.Privatize();
Alexey Bataev0f34da12015-07-02 04:17:07 +00001585 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
1586 S.getInc(),
Alexey Bataev62dbb972015-04-22 11:59:37 +00001587 [&S](CodeGenFunction &CGF) {
Alexey Bataev0f34da12015-07-02 04:17:07 +00001588 CGF.EmitOMPLoopBody(S, JumpDest());
Alexey Bataev62dbb972015-04-22 11:59:37 +00001589 CGF.EmitStopPoint(&S);
1590 },
1591 [](CodeGenFunction &) {});
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001592 CGF.EmitOMPSimdFinal(
1593 S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataevfc087ec2015-06-16 13:14:42 +00001594 // Emit final copy of the lastprivate variables at the end of loops.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001595 if (HasLastprivateClause)
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001596 CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001597 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_simd);
Alexey Bataev61205072016-03-02 04:57:40 +00001598 emitPostUpdateForReductionClause(
1599 CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev62dbb972015-04-22 11:59:37 +00001600 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001601 CGF.EmitOMPLinearClauseFinal(
Alexey Bataevef549a82016-03-09 09:49:09 +00001602 S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev62dbb972015-04-22 11:59:37 +00001603 // Emit: if (PreCond) - end.
1604 if (ContBlock) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001605 CGF.EmitBranch(ContBlock);
1606 CGF.EmitBlock(ContBlock, true);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001607 }
1608 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00001609 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001610 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
Alexander Musman515ad8c2014-05-22 08:54:05 +00001611}
1612
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001613void CodeGenFunction::EmitOMPOuterLoop(
1614 bool DynamicOrOrdered, bool IsMonotonic, const OMPLoopDirective &S,
1615 CodeGenFunction::OMPPrivateScope &LoopScope,
1616 const CodeGenFunction::OMPLoopArguments &LoopArgs,
1617 const CodeGenFunction::CodeGenLoopTy &CodeGenLoop,
1618 const CodeGenFunction::CodeGenOrderedTy &CodeGenOrdered) {
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001619 auto &RT = CGM.getOpenMPRuntime();
Alexander Musman92bdaab2015-03-12 13:37:50 +00001620
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001621 const Expr *IVExpr = S.getIterationVariable();
1622 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1623 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1624
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001625 auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end");
1626
1627 // Start the loop with a block that tests the condition.
1628 auto CondBlock = createBasicBlock("omp.dispatch.cond");
1629 EmitBlock(CondBlock);
Amara Emerson652795d2016-11-10 14:44:30 +00001630 const SourceRange &R = S.getSourceRange();
1631 LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()),
1632 SourceLocToDebugLoc(R.getEnd()));
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001633
1634 llvm::Value *BoolCondVal = nullptr;
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001635 if (!DynamicOrOrdered) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001636 // UB = min(UB, GlobalUB) or
1637 // UB = min(UB, PrevUB) for combined loop sharing constructs (e.g.
1638 // 'distribute parallel for')
1639 EmitIgnoredExpr(LoopArgs.EUB);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001640 // IV = LB
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001641 EmitIgnoredExpr(LoopArgs.Init);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001642 // IV < UB
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001643 BoolCondVal = EvaluateExprAsBool(LoopArgs.Cond);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001644 } else {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001645 BoolCondVal =
1646 RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned, LoopArgs.IL,
1647 LoopArgs.LB, LoopArgs.UB, LoopArgs.ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001648 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001649
1650 // If there are any cleanups between here and the loop-exit scope,
1651 // create a block to stage a loop exit along.
1652 auto ExitBlock = LoopExit.getBlock();
1653 if (LoopScope.requiresCleanups())
1654 ExitBlock = createBasicBlock("omp.dispatch.cleanup");
1655
1656 auto LoopBody = createBasicBlock("omp.dispatch.body");
1657 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
1658 if (ExitBlock != LoopExit.getBlock()) {
1659 EmitBlock(ExitBlock);
1660 EmitBranchThroughCleanup(LoopExit);
1661 }
1662 EmitBlock(LoopBody);
1663
Alexander Musman92bdaab2015-03-12 13:37:50 +00001664 // Emit "IV = LB" (in case of static schedule, we have already calculated new
1665 // LB for loop condition and emitted it above).
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001666 if (DynamicOrOrdered)
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001667 EmitIgnoredExpr(LoopArgs.Init);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001668
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001669 // Create a block for the increment.
1670 auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc");
1671 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1672
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001673 // Generate !llvm.loop.parallel metadata for loads and stores for loops
1674 // with dynamic/guided scheduling and without ordered clause.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001675 if (!isOpenMPSimdDirective(S.getDirectiveKind()))
1676 LoopStack.setParallel(!IsMonotonic);
1677 else
1678 EmitOMPSimdInit(S, IsMonotonic);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001679
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001680 SourceLocation Loc = S.getLocStart();
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001681
1682 // when 'distribute' is not combined with a 'for':
1683 // while (idx <= UB) { BODY; ++idx; }
1684 // when 'distribute' is combined with a 'for'
1685 // (e.g. 'distribute parallel for')
1686 // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; }
1687 EmitOMPInnerLoop(
1688 S, LoopScope.requiresCleanups(), LoopArgs.Cond, LoopArgs.IncExpr,
1689 [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) {
1690 CodeGenLoop(CGF, S, LoopExit);
1691 },
1692 [IVSize, IVSigned, Loc, &CodeGenOrdered](CodeGenFunction &CGF) {
1693 CodeGenOrdered(CGF, Loc, IVSize, IVSigned);
1694 });
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001695
1696 EmitBlock(Continue.getBlock());
1697 BreakContinueStack.pop_back();
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001698 if (!DynamicOrOrdered) {
Alexander Musman92bdaab2015-03-12 13:37:50 +00001699 // Emit "LB = LB + Stride", "UB = UB + Stride".
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001700 EmitIgnoredExpr(LoopArgs.NextLB);
1701 EmitIgnoredExpr(LoopArgs.NextUB);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001702 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001703
1704 EmitBranch(CondBlock);
1705 LoopStack.pop();
1706 // Emit the fall-through block.
1707 EmitBlock(LoopExit.getBlock());
1708
1709 // Tell the runtime we are done.
Alexey Bataev957d8562016-11-17 15:12:05 +00001710 auto &&CodeGen = [DynamicOrOrdered, &S](CodeGenFunction &CGF) {
1711 if (!DynamicOrOrdered)
Alexey Bataevf43f7142017-09-06 16:17:35 +00001712 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd(),
1713 S.getDirectiveKind());
Alexey Bataev957d8562016-11-17 15:12:05 +00001714 };
1715 OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001716}
1717
1718void CodeGenFunction::EmitOMPForOuterLoop(
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001719 const OpenMPScheduleTy &ScheduleKind, bool IsMonotonic,
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001720 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001721 const OMPLoopArguments &LoopArgs,
1722 const CodeGenDispatchBoundsTy &CGDispatchBounds) {
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001723 auto &RT = CGM.getOpenMPRuntime();
1724
1725 // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime).
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001726 const bool DynamicOrOrdered =
1727 Ordered || RT.isDynamic(ScheduleKind.Schedule);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001728
1729 assert((Ordered ||
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001730 !RT.isStaticNonchunked(ScheduleKind.Schedule,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001731 LoopArgs.Chunk != nullptr)) &&
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001732 "static non-chunked schedule does not need outer loop");
1733
1734 // Emit outer loop.
1735 //
1736 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1737 // When schedule(dynamic,chunk_size) is specified, the iterations are
1738 // distributed to threads in the team in chunks as the threads request them.
1739 // Each thread executes a chunk of iterations, then requests another chunk,
1740 // until no chunks remain to be distributed. Each chunk contains chunk_size
1741 // iterations, except for the last chunk to be distributed, which may have
1742 // fewer iterations. When no chunk_size is specified, it defaults to 1.
1743 //
1744 // When schedule(guided,chunk_size) is specified, the iterations are assigned
1745 // to threads in the team in chunks as the executing threads request them.
1746 // Each thread executes a chunk of iterations, then requests another chunk,
1747 // until no chunks remain to be assigned. For a chunk_size of 1, the size of
1748 // each chunk is proportional to the number of unassigned iterations divided
1749 // by the number of threads in the team, decreasing to 1. For a chunk_size
1750 // with value k (greater than 1), the size of each chunk is determined in the
1751 // same way, with the restriction that the chunks do not contain fewer than k
1752 // iterations (except for the last chunk to be assigned, which may have fewer
1753 // than k iterations).
1754 //
1755 // When schedule(auto) is specified, the decision regarding scheduling is
1756 // delegated to the compiler and/or runtime system. The programmer gives the
1757 // implementation the freedom to choose any possible mapping of iterations to
1758 // threads in the team.
1759 //
1760 // When schedule(runtime) is specified, the decision regarding scheduling is
1761 // deferred until run time, and the schedule and chunk size are taken from the
1762 // run-sched-var ICV. If the ICV is set to auto, the schedule is
1763 // implementation defined
1764 //
1765 // while(__kmpc_dispatch_next(&LB, &UB)) {
1766 // idx = LB;
1767 // while (idx <= UB) { BODY; ++idx;
1768 // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only.
1769 // } // inner loop
1770 // }
1771 //
1772 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1773 // When schedule(static, chunk_size) is specified, iterations are divided into
1774 // chunks of size chunk_size, and the chunks are assigned to the threads in
1775 // the team in a round-robin fashion in the order of the thread number.
1776 //
1777 // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) {
1778 // while (idx <= UB) { BODY; ++idx; } // inner loop
1779 // LB = LB + ST;
1780 // UB = UB + ST;
1781 // }
1782 //
1783
1784 const Expr *IVExpr = S.getIterationVariable();
1785 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1786 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1787
1788 if (DynamicOrOrdered) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001789 auto DispatchBounds = CGDispatchBounds(*this, S, LoopArgs.LB, LoopArgs.UB);
1790 llvm::Value *LBVal = DispatchBounds.first;
1791 llvm::Value *UBVal = DispatchBounds.second;
1792 CGOpenMPRuntime::DispatchRTInput DipatchRTInputValues = {LBVal, UBVal,
1793 LoopArgs.Chunk};
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001794 RT.emitForDispatchInit(*this, S.getLocStart(), ScheduleKind, IVSize,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001795 IVSigned, Ordered, DipatchRTInputValues);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001796 } else {
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001797 CGOpenMPRuntime::StaticRTInput StaticInit(
1798 IVSize, IVSigned, Ordered, LoopArgs.IL, LoopArgs.LB, LoopArgs.UB,
1799 LoopArgs.ST, LoopArgs.Chunk);
1800 RT.emitForStaticInit(*this, S.getLocStart(), S.getDirectiveKind(),
1801 ScheduleKind, StaticInit);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001802 }
1803
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001804 auto &&CodeGenOrdered = [Ordered](CodeGenFunction &CGF, SourceLocation Loc,
1805 const unsigned IVSize,
1806 const bool IVSigned) {
1807 if (Ordered) {
1808 CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(CGF, Loc, IVSize,
1809 IVSigned);
1810 }
1811 };
1812
1813 OMPLoopArguments OuterLoopArgs(LoopArgs.LB, LoopArgs.UB, LoopArgs.ST,
1814 LoopArgs.IL, LoopArgs.Chunk, LoopArgs.EUB);
1815 OuterLoopArgs.IncExpr = S.getInc();
1816 OuterLoopArgs.Init = S.getInit();
1817 OuterLoopArgs.Cond = S.getCond();
1818 OuterLoopArgs.NextLB = S.getNextLowerBound();
1819 OuterLoopArgs.NextUB = S.getNextUpperBound();
1820 EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, OuterLoopArgs,
1821 emitOMPLoopBodyWithStopPoint, CodeGenOrdered);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001822}
1823
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001824static void emitEmptyOrdered(CodeGenFunction &, SourceLocation Loc,
1825 const unsigned IVSize, const bool IVSigned) {}
1826
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001827void CodeGenFunction::EmitOMPDistributeOuterLoop(
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001828 OpenMPDistScheduleClauseKind ScheduleKind, const OMPLoopDirective &S,
1829 OMPPrivateScope &LoopScope, const OMPLoopArguments &LoopArgs,
1830 const CodeGenLoopTy &CodeGenLoopContent) {
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001831
1832 auto &RT = CGM.getOpenMPRuntime();
1833
1834 // Emit outer loop.
1835 // Same behavior as a OMPForOuterLoop, except that schedule cannot be
1836 // dynamic
1837 //
1838
1839 const Expr *IVExpr = S.getIterationVariable();
1840 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1841 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1842
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001843 CGOpenMPRuntime::StaticRTInput StaticInit(
1844 IVSize, IVSigned, /* Ordered = */ false, LoopArgs.IL, LoopArgs.LB,
1845 LoopArgs.UB, LoopArgs.ST, LoopArgs.Chunk);
1846 RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind, StaticInit);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001847
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001848 // for combined 'distribute' and 'for' the increment expression of distribute
1849 // is store in DistInc. For 'distribute' alone, it is in Inc.
1850 Expr *IncExpr;
1851 if (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()))
1852 IncExpr = S.getDistInc();
1853 else
1854 IncExpr = S.getInc();
1855
1856 // this routine is shared by 'omp distribute parallel for' and
1857 // 'omp distribute': select the right EUB expression depending on the
1858 // directive
1859 OMPLoopArguments OuterLoopArgs;
1860 OuterLoopArgs.LB = LoopArgs.LB;
1861 OuterLoopArgs.UB = LoopArgs.UB;
1862 OuterLoopArgs.ST = LoopArgs.ST;
1863 OuterLoopArgs.IL = LoopArgs.IL;
1864 OuterLoopArgs.Chunk = LoopArgs.Chunk;
1865 OuterLoopArgs.EUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
1866 ? S.getCombinedEnsureUpperBound()
1867 : S.getEnsureUpperBound();
1868 OuterLoopArgs.IncExpr = IncExpr;
1869 OuterLoopArgs.Init = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
1870 ? S.getCombinedInit()
1871 : S.getInit();
1872 OuterLoopArgs.Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
1873 ? S.getCombinedCond()
1874 : S.getCond();
1875 OuterLoopArgs.NextLB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
1876 ? S.getCombinedNextLowerBound()
1877 : S.getNextLowerBound();
1878 OuterLoopArgs.NextUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
1879 ? S.getCombinedNextUpperBound()
1880 : S.getNextUpperBound();
1881
1882 EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false, S,
1883 LoopScope, OuterLoopArgs, CodeGenLoopContent,
1884 emitEmptyOrdered);
1885}
1886
1887/// Emit a helper variable and return corresponding lvalue.
1888static LValue EmitOMPHelperVar(CodeGenFunction &CGF,
1889 const DeclRefExpr *Helper) {
1890 auto VDecl = cast<VarDecl>(Helper->getDecl());
1891 CGF.EmitVarDecl(*VDecl);
1892 return CGF.EmitLValue(Helper);
1893}
1894
1895static std::pair<LValue, LValue>
1896emitDistributeParallelForInnerBounds(CodeGenFunction &CGF,
1897 const OMPExecutableDirective &S) {
1898 const OMPLoopDirective &LS = cast<OMPLoopDirective>(S);
1899 LValue LB =
1900 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable()));
1901 LValue UB =
1902 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable()));
1903
1904 // When composing 'distribute' with 'for' (e.g. as in 'distribute
1905 // parallel for') we need to use the 'distribute'
1906 // chunk lower and upper bounds rather than the whole loop iteration
1907 // space. These are parameters to the outlined function for 'parallel'
1908 // and we copy the bounds of the previous schedule into the
1909 // the current ones.
1910 LValue PrevLB = CGF.EmitLValue(LS.getPrevLowerBoundVariable());
1911 LValue PrevUB = CGF.EmitLValue(LS.getPrevUpperBoundVariable());
1912 llvm::Value *PrevLBVal = CGF.EmitLoadOfScalar(PrevLB, SourceLocation());
1913 PrevLBVal = CGF.EmitScalarConversion(
1914 PrevLBVal, LS.getPrevLowerBoundVariable()->getType(),
1915 LS.getIterationVariable()->getType(), SourceLocation());
1916 llvm::Value *PrevUBVal = CGF.EmitLoadOfScalar(PrevUB, SourceLocation());
1917 PrevUBVal = CGF.EmitScalarConversion(
1918 PrevUBVal, LS.getPrevUpperBoundVariable()->getType(),
1919 LS.getIterationVariable()->getType(), SourceLocation());
1920
1921 CGF.EmitStoreOfScalar(PrevLBVal, LB);
1922 CGF.EmitStoreOfScalar(PrevUBVal, UB);
1923
1924 return {LB, UB};
1925}
1926
1927/// if the 'for' loop has a dispatch schedule (e.g. dynamic, guided) then
1928/// we need to use the LB and UB expressions generated by the worksharing
1929/// code generation support, whereas in non combined situations we would
1930/// just emit 0 and the LastIteration expression
1931/// This function is necessary due to the difference of the LB and UB
1932/// types for the RT emission routines for 'for_static_init' and
1933/// 'for_dispatch_init'
1934static std::pair<llvm::Value *, llvm::Value *>
1935emitDistributeParallelForDispatchBounds(CodeGenFunction &CGF,
1936 const OMPExecutableDirective &S,
1937 Address LB, Address UB) {
1938 const OMPLoopDirective &LS = cast<OMPLoopDirective>(S);
1939 const Expr *IVExpr = LS.getIterationVariable();
1940 // when implementing a dynamic schedule for a 'for' combined with a
1941 // 'distribute' (e.g. 'distribute parallel for'), the 'for' loop
1942 // is not normalized as each team only executes its own assigned
1943 // distribute chunk
1944 QualType IteratorTy = IVExpr->getType();
1945 llvm::Value *LBVal = CGF.EmitLoadOfScalar(LB, /*Volatile=*/false, IteratorTy,
1946 SourceLocation());
1947 llvm::Value *UBVal = CGF.EmitLoadOfScalar(UB, /*Volatile=*/false, IteratorTy,
1948 SourceLocation());
1949 return {LBVal, UBVal};
Hans Wennborged129ae2017-04-27 17:02:25 +00001950}
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001951
1952static void emitDistributeParallelForDistributeInnerBoundParams(
1953 CodeGenFunction &CGF, const OMPExecutableDirective &S,
1954 llvm::SmallVectorImpl<llvm::Value *> &CapturedVars) {
1955 const auto &Dir = cast<OMPLoopDirective>(S);
1956 LValue LB =
1957 CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedLowerBoundVariable()));
1958 auto LBCast = CGF.Builder.CreateIntCast(
1959 CGF.Builder.CreateLoad(LB.getAddress()), CGF.SizeTy, /*isSigned=*/false);
1960 CapturedVars.push_back(LBCast);
1961 LValue UB =
1962 CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedUpperBoundVariable()));
1963
1964 auto UBCast = CGF.Builder.CreateIntCast(
1965 CGF.Builder.CreateLoad(UB.getAddress()), CGF.SizeTy, /*isSigned=*/false);
1966 CapturedVars.push_back(UBCast);
Hans Wennborged129ae2017-04-27 17:02:25 +00001967}
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001968
1969static void
1970emitInnerParallelForWhenCombined(CodeGenFunction &CGF,
1971 const OMPLoopDirective &S,
1972 CodeGenFunction::JumpDest LoopExit) {
1973 auto &&CGInlinedWorksharingLoop = [&S](CodeGenFunction &CGF,
1974 PrePostActionTy &) {
1975 CGF.EmitOMPWorksharingLoop(S, S.getPrevEnsureUpperBound(),
1976 emitDistributeParallelForInnerBounds,
1977 emitDistributeParallelForDispatchBounds);
1978 };
1979
1980 emitCommonOMPParallelDirective(
1981 CGF, S, OMPD_for, CGInlinedWorksharingLoop,
1982 emitDistributeParallelForDistributeInnerBoundParams);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001983}
1984
Carlo Bertolli9925f152016-06-27 14:55:37 +00001985void CodeGenFunction::EmitOMPDistributeParallelForDirective(
1986 const OMPDistributeParallelForDirective &S) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001987 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
1988 CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined,
1989 S.getDistInc());
1990 };
Carlo Bertolli9925f152016-06-27 14:55:37 +00001991 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001992 OMPCancelStackRAII CancelRegion(*this, OMPD_distribute_parallel_for,
1993 /*HasCancel=*/false);
1994 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen,
1995 /*HasCancel=*/false);
Carlo Bertolli9925f152016-06-27 14:55:37 +00001996}
1997
Kelvin Li4a39add2016-07-05 05:00:15 +00001998void CodeGenFunction::EmitOMPDistributeParallelForSimdDirective(
1999 const OMPDistributeParallelForSimdDirective &S) {
2000 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2001 CGM.getOpenMPRuntime().emitInlinedDirective(
2002 *this, OMPD_distribute_parallel_for_simd,
2003 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2004 OMPLoopScope PreInitScope(CGF, S);
2005 CGF.EmitStmt(
2006 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2007 });
2008}
Kelvin Li787f3fc2016-07-06 04:45:38 +00002009
2010void CodeGenFunction::EmitOMPDistributeSimdDirective(
2011 const OMPDistributeSimdDirective &S) {
2012 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2013 CGM.getOpenMPRuntime().emitInlinedDirective(
2014 *this, OMPD_distribute_simd,
2015 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2016 OMPLoopScope PreInitScope(CGF, S);
2017 CGF.EmitStmt(
2018 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2019 });
2020}
2021
Kelvin Lia579b912016-07-14 02:54:56 +00002022void CodeGenFunction::EmitOMPTargetParallelForSimdDirective(
2023 const OMPTargetParallelForSimdDirective &S) {
2024 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2025 CGM.getOpenMPRuntime().emitInlinedDirective(
2026 *this, OMPD_target_parallel_for_simd,
2027 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2028 OMPLoopScope PreInitScope(CGF, S);
2029 CGF.EmitStmt(
2030 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2031 });
2032}
2033
Kelvin Li986330c2016-07-20 22:57:10 +00002034void CodeGenFunction::EmitOMPTargetSimdDirective(
2035 const OMPTargetSimdDirective &S) {
2036 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2037 CGM.getOpenMPRuntime().emitInlinedDirective(
2038 *this, OMPD_target_simd, [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2039 OMPLoopScope PreInitScope(CGF, S);
2040 CGF.EmitStmt(
2041 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2042 });
2043}
2044
Kelvin Li4e325f72016-10-25 12:50:55 +00002045void CodeGenFunction::EmitOMPTeamsDistributeSimdDirective(
2046 const OMPTeamsDistributeSimdDirective &S) {
2047 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2048 CGM.getOpenMPRuntime().emitInlinedDirective(
2049 *this, OMPD_teams_distribute_simd,
2050 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2051 OMPLoopScope PreInitScope(CGF, S);
2052 CGF.EmitStmt(
2053 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2054 });
2055}
2056
Kelvin Li579e41c2016-11-30 23:51:03 +00002057void CodeGenFunction::EmitOMPTeamsDistributeParallelForSimdDirective(
2058 const OMPTeamsDistributeParallelForSimdDirective &S) {
2059 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2060 CGM.getOpenMPRuntime().emitInlinedDirective(
2061 *this, OMPD_teams_distribute_parallel_for_simd,
2062 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2063 OMPLoopScope PreInitScope(CGF, S);
2064 CGF.EmitStmt(
2065 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2066 });
2067}
Kelvin Li4e325f72016-10-25 12:50:55 +00002068
Kelvin Li7ade93f2016-12-09 03:24:30 +00002069void CodeGenFunction::EmitOMPTeamsDistributeParallelForDirective(
2070 const OMPTeamsDistributeParallelForDirective &S) {
2071 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2072 CGM.getOpenMPRuntime().emitInlinedDirective(
2073 *this, OMPD_teams_distribute_parallel_for,
2074 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2075 OMPLoopScope PreInitScope(CGF, S);
2076 CGF.EmitStmt(
2077 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2078 });
2079}
2080
Kelvin Li83c451e2016-12-25 04:52:54 +00002081void CodeGenFunction::EmitOMPTargetTeamsDistributeDirective(
2082 const OMPTargetTeamsDistributeDirective &S) {
Alexey Bataev931e19b2017-10-02 16:32:39 +00002083 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Kelvin Li26fd21a2016-12-28 17:57:07 +00002084 CGM.getOpenMPRuntime().emitInlinedDirective(
2085 *this, OMPD_target_teams_distribute,
Kelvin Li83c451e2016-12-25 04:52:54 +00002086 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Kelvin Li26fd21a2016-12-28 17:57:07 +00002087 CGF.EmitStmt(
2088 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Kelvin Li83c451e2016-12-25 04:52:54 +00002089 });
2090}
2091
Kelvin Li80e8f562016-12-29 22:16:30 +00002092void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDirective(
2093 const OMPTargetTeamsDistributeParallelForDirective &S) {
Alexey Bataev931e19b2017-10-02 16:32:39 +00002094 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Kelvin Li80e8f562016-12-29 22:16:30 +00002095 CGM.getOpenMPRuntime().emitInlinedDirective(
2096 *this, OMPD_target_teams_distribute_parallel_for,
2097 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2098 CGF.EmitStmt(
2099 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2100 });
2101}
2102
Kelvin Li1851df52017-01-03 05:23:48 +00002103void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDirective(
2104 const OMPTargetTeamsDistributeParallelForSimdDirective &S) {
Alexey Bataev931e19b2017-10-02 16:32:39 +00002105 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Kelvin Li1851df52017-01-03 05:23:48 +00002106 CGM.getOpenMPRuntime().emitInlinedDirective(
2107 *this, OMPD_target_teams_distribute_parallel_for_simd,
2108 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2109 CGF.EmitStmt(
2110 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2111 });
2112}
2113
Kelvin Lida681182017-01-10 18:08:18 +00002114void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDirective(
2115 const OMPTargetTeamsDistributeSimdDirective &S) {
Alexey Bataev931e19b2017-10-02 16:32:39 +00002116 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Kelvin Lida681182017-01-10 18:08:18 +00002117 CGM.getOpenMPRuntime().emitInlinedDirective(
2118 *this, OMPD_target_teams_distribute_simd,
2119 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2120 CGF.EmitStmt(
2121 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2122 });
2123}
2124
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002125namespace {
2126 struct ScheduleKindModifiersTy {
2127 OpenMPScheduleClauseKind Kind;
2128 OpenMPScheduleClauseModifier M1;
2129 OpenMPScheduleClauseModifier M2;
2130 ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind,
2131 OpenMPScheduleClauseModifier M1,
2132 OpenMPScheduleClauseModifier M2)
2133 : Kind(Kind), M1(M1), M2(M2) {}
2134 };
2135} // namespace
2136
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002137bool CodeGenFunction::EmitOMPWorksharingLoop(
2138 const OMPLoopDirective &S, Expr *EUB,
2139 const CodeGenLoopBoundsTy &CodeGenLoopBounds,
2140 const CodeGenDispatchBoundsTy &CGDispatchBounds) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002141 // Emit the loop iteration variable.
2142 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
2143 auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
2144 EmitVarDecl(*IVDecl);
2145
2146 // Emit the iterations count variable.
2147 // If it is not a variable, Sema decided to calculate iterations count on each
2148 // iteration (e.g., it is foldable into a constant).
2149 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
2150 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
2151 // Emit calculation of the iterations count.
2152 EmitIgnoredExpr(S.getCalcLastIteration());
2153 }
2154
2155 auto &RT = CGM.getOpenMPRuntime();
2156
Alexey Bataev38e89532015-04-16 04:54:05 +00002157 bool HasLastprivateClause;
Alexander Musmanc6388682014-12-15 07:07:06 +00002158 // Check pre-condition.
2159 {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002160 OMPLoopScope PreInitScope(*this, S);
Alexander Musmanc6388682014-12-15 07:07:06 +00002161 // Skip the entire loop if we don't meet the precondition.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002162 // If the condition constant folds and can be elided, avoid emitting the
2163 // whole loop.
2164 bool CondConstant;
2165 llvm::BasicBlock *ContBlock = nullptr;
2166 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
2167 if (!CondConstant)
2168 return false;
2169 } else {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002170 auto *ThenBlock = createBasicBlock("omp.precond.then");
2171 ContBlock = createBasicBlock("omp.precond.end");
2172 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
Justin Bogner66242d62015-04-23 23:06:47 +00002173 getProfileCount(&S));
Alexey Bataev62dbb972015-04-22 11:59:37 +00002174 EmitBlock(ThenBlock);
Justin Bogner66242d62015-04-23 23:06:47 +00002175 incrementProfileCounter(&S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00002176 }
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00002177
Alexey Bataev8b427062016-05-25 12:36:08 +00002178 bool Ordered = false;
2179 if (auto *OrderedClause = S.getSingleClause<OMPOrderedClause>()) {
2180 if (OrderedClause->getNumForLoops())
2181 RT.emitDoacrossInit(*this, S);
2182 else
2183 Ordered = true;
2184 }
2185
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002186 llvm::DenseSet<const Expr *> EmittedFinals;
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00002187 emitAlignedClause(*this, S);
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00002188 bool HasLinears = EmitOMPLinearClauseInit(S);
Alexey Bataevef549a82016-03-09 09:49:09 +00002189 // Emit helper vars inits.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002190
2191 std::pair<LValue, LValue> Bounds = CodeGenLoopBounds(*this, S);
2192 LValue LB = Bounds.first;
2193 LValue UB = Bounds.second;
Alexey Bataevef549a82016-03-09 09:49:09 +00002194 LValue ST =
2195 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
2196 LValue IL =
2197 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
2198
Alexander Musmanc6388682014-12-15 07:07:06 +00002199 // Emit 'then' code.
2200 {
Alexander Musmanc6388682014-12-15 07:07:06 +00002201 OMPPrivateScope LoopScope(*this);
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00002202 if (EmitOMPFirstprivateClause(S, LoopScope) || HasLinears) {
Alexey Bataev69c62a92015-04-15 04:52:20 +00002203 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00002204 // initialization of firstprivate variables and post-update of
2205 // lastprivate variables.
Alexey Bataev25e5b442015-09-15 12:52:43 +00002206 CGM.getOpenMPRuntime().emitBarrierCall(
2207 *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
2208 /*ForceSimpleCall=*/true);
Alexey Bataev69c62a92015-04-15 04:52:20 +00002209 }
Alexey Bataev50a64582015-04-22 12:24:45 +00002210 EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev38e89532015-04-16 04:54:05 +00002211 HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev7ebe5fd2015-04-22 13:43:03 +00002212 EmitOMPReductionClauseInit(S, LoopScope);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002213 EmitOMPPrivateLoopCounters(S, LoopScope);
2214 EmitOMPLinearClause(S, LoopScope);
Alexander Musman7931b982015-03-16 07:14:41 +00002215 (void)LoopScope.Privatize();
Alexander Musmanc6388682014-12-15 07:07:06 +00002216
2217 // Detect the loop schedule kind and chunk.
Alexey Bataev3392d762016-02-16 11:18:12 +00002218 llvm::Value *Chunk = nullptr;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002219 OpenMPScheduleTy ScheduleKind;
Alexey Bataev3392d762016-02-16 11:18:12 +00002220 if (auto *C = S.getSingleClause<OMPScheduleClause>()) {
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002221 ScheduleKind.Schedule = C->getScheduleKind();
2222 ScheduleKind.M1 = C->getFirstScheduleModifier();
2223 ScheduleKind.M2 = C->getSecondScheduleModifier();
Alexey Bataev3392d762016-02-16 11:18:12 +00002224 if (const auto *Ch = C->getChunkSize()) {
2225 Chunk = EmitScalarExpr(Ch);
2226 Chunk = EmitScalarConversion(Chunk, Ch->getType(),
2227 S.getIterationVariable()->getType(),
2228 S.getLocStart());
2229 }
2230 }
Alexander Musmanc6388682014-12-15 07:07:06 +00002231 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
2232 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002233 // OpenMP 4.5, 2.7.1 Loop Construct, Description.
2234 // If the static schedule kind is specified or if the ordered clause is
2235 // specified, and if no monotonic modifier is specified, the effect will
2236 // be as if the monotonic modifier was specified.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002237 if (RT.isStaticNonchunked(ScheduleKind.Schedule,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002238 /* Chunked */ Chunk != nullptr) &&
2239 !Ordered) {
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002240 if (isOpenMPSimdDirective(S.getDirectiveKind()))
2241 EmitOMPSimdInit(S, /*IsMonotonic=*/true);
Alexander Musmanc6388682014-12-15 07:07:06 +00002242 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
2243 // When no chunk_size is specified, the iteration space is divided into
2244 // chunks that are approximately equal in size, and at most one chunk is
2245 // distributed to each thread. Note that the size of the chunks is
2246 // unspecified in this case.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00002247 CGOpenMPRuntime::StaticRTInput StaticInit(
2248 IVSize, IVSigned, Ordered, IL.getAddress(), LB.getAddress(),
2249 UB.getAddress(), ST.getAddress());
2250 RT.emitForStaticInit(*this, S.getLocStart(), S.getDirectiveKind(),
2251 ScheduleKind, StaticInit);
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002252 auto LoopExit =
2253 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
Alexander Musmanc6388682014-12-15 07:07:06 +00002254 // UB = min(UB, GlobalUB);
2255 EmitIgnoredExpr(S.getEnsureUpperBound());
2256 // IV = LB;
2257 EmitIgnoredExpr(S.getInit());
2258 // while (idx <= UB) { BODY; ++idx; }
Alexey Bataevae05c292015-06-16 11:59:36 +00002259 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
2260 S.getInc(),
Alexey Bataev0f34da12015-07-02 04:17:07 +00002261 [&S, LoopExit](CodeGenFunction &CGF) {
2262 CGF.EmitOMPLoopBody(S, LoopExit);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002263 CGF.EmitStopPoint(&S);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002264 },
2265 [](CodeGenFunction &) {});
Alexey Bataev0f34da12015-07-02 04:17:07 +00002266 EmitBlock(LoopExit.getBlock());
Alexander Musmanc6388682014-12-15 07:07:06 +00002267 // Tell the runtime we are done.
Alexey Bataev957d8562016-11-17 15:12:05 +00002268 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
Alexey Bataevf43f7142017-09-06 16:17:35 +00002269 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd(),
2270 S.getDirectiveKind());
Alexey Bataev957d8562016-11-17 15:12:05 +00002271 };
2272 OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002273 } else {
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002274 const bool IsMonotonic =
2275 Ordered || ScheduleKind.Schedule == OMPC_SCHEDULE_static ||
2276 ScheduleKind.Schedule == OMPC_SCHEDULE_unknown ||
2277 ScheduleKind.M1 == OMPC_SCHEDULE_MODIFIER_monotonic ||
2278 ScheduleKind.M2 == OMPC_SCHEDULE_MODIFIER_monotonic;
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002279 // Emit the outer loop, which requests its work chunk [LB..UB] from
2280 // runtime and runs the inner loop to process it.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002281 const OMPLoopArguments LoopArguments(LB.getAddress(), UB.getAddress(),
2282 ST.getAddress(), IL.getAddress(),
2283 Chunk, EUB);
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002284 EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002285 LoopArguments, CGDispatchBounds);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002286 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002287 if (isOpenMPSimdDirective(S.getDirectiveKind())) {
2288 EmitOMPSimdFinal(S,
2289 [&](CodeGenFunction &CGF) -> llvm::Value * {
2290 return CGF.Builder.CreateIsNotNull(
2291 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2292 });
2293 }
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002294 EmitOMPReductionClauseFinal(
2295 S, /*ReductionKind=*/isOpenMPSimdDirective(S.getDirectiveKind())
2296 ? /*Parallel and Simd*/ OMPD_parallel_for_simd
2297 : /*Parallel only*/ OMPD_parallel);
Alexey Bataev61205072016-03-02 04:57:40 +00002298 // Emit post-update of the reduction variables if IsLastIter != 0.
2299 emitPostUpdateForReductionClause(
2300 *this, S, [&](CodeGenFunction &CGF) -> llvm::Value * {
2301 return CGF.Builder.CreateIsNotNull(
2302 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2303 });
Alexey Bataev38e89532015-04-16 04:54:05 +00002304 // Emit final copy of the lastprivate variables if IsLastIter != 0.
2305 if (HasLastprivateClause)
2306 EmitOMPLastprivateClauseFinal(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002307 S, isOpenMPSimdDirective(S.getDirectiveKind()),
2308 Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart())));
Alexander Musmanc6388682014-12-15 07:07:06 +00002309 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002310 EmitOMPLinearClauseFinal(S, [&](CodeGenFunction &CGF) -> llvm::Value * {
Alexey Bataevef549a82016-03-09 09:49:09 +00002311 return CGF.Builder.CreateIsNotNull(
2312 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2313 });
Alexander Musmanc6388682014-12-15 07:07:06 +00002314 // We're now done with the loop, so jump to the continuation block.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002315 if (ContBlock) {
2316 EmitBranch(ContBlock);
2317 EmitBlock(ContBlock, true);
2318 }
Alexander Musmanc6388682014-12-15 07:07:06 +00002319 }
Alexey Bataev38e89532015-04-16 04:54:05 +00002320 return HasLastprivateClause;
Alexander Musmanc6388682014-12-15 07:07:06 +00002321}
2322
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002323/// The following two functions generate expressions for the loop lower
2324/// and upper bounds in case of static and dynamic (dispatch) schedule
2325/// of the associated 'for' or 'distribute' loop.
2326static std::pair<LValue, LValue>
2327emitForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S) {
2328 const OMPLoopDirective &LS = cast<OMPLoopDirective>(S);
2329 LValue LB =
2330 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable()));
2331 LValue UB =
2332 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable()));
2333 return {LB, UB};
2334}
2335
2336/// When dealing with dispatch schedules (e.g. dynamic, guided) we do not
2337/// consider the lower and upper bound expressions generated by the
2338/// worksharing loop support, but we use 0 and the iteration space size as
2339/// constants
2340static std::pair<llvm::Value *, llvm::Value *>
2341emitDispatchForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S,
2342 Address LB, Address UB) {
2343 const OMPLoopDirective &LS = cast<OMPLoopDirective>(S);
2344 const Expr *IVExpr = LS.getIterationVariable();
2345 const unsigned IVSize = CGF.getContext().getTypeSize(IVExpr->getType());
2346 llvm::Value *LBVal = CGF.Builder.getIntN(IVSize, 0);
2347 llvm::Value *UBVal = CGF.EmitScalarExpr(LS.getLastIteration());
2348 return {LBVal, UBVal};
2349}
2350
Alexander Musmanc6388682014-12-15 07:07:06 +00002351void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) {
Alexey Bataev38e89532015-04-16 04:54:05 +00002352 bool HasLastprivates = false;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002353 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF,
2354 PrePostActionTy &) {
Alexey Bataev957d8562016-11-17 15:12:05 +00002355 OMPCancelStackRAII CancelRegion(CGF, OMPD_for, S.hasCancel());
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002356 HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(),
2357 emitForLoopBounds,
2358 emitDispatchForLoopBounds);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002359 };
Alexey Bataev3392d762016-02-16 11:18:12 +00002360 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002361 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002362 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen,
2363 S.hasCancel());
2364 }
Alexander Musmanc6388682014-12-15 07:07:06 +00002365
2366 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002367 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) {
Alexey Bataevf2685682015-03-30 04:30:22 +00002368 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
2369 }
Alexey Bataevf29276e2014-06-18 04:14:57 +00002370}
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002371
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002372void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002373 bool HasLastprivates = false;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002374 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF,
2375 PrePostActionTy &) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002376 HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(),
2377 emitForLoopBounds,
2378 emitDispatchForLoopBounds);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002379 };
Alexey Bataev3392d762016-02-16 11:18:12 +00002380 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002381 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002382 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
2383 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002384
2385 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002386 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002387 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
2388 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00002389}
2390
Alexey Bataev2df54a02015-03-12 08:53:29 +00002391static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty,
2392 const Twine &Name,
2393 llvm::Value *Init = nullptr) {
John McCall7f416cc2015-09-08 08:05:57 +00002394 auto LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty);
Alexey Bataev2df54a02015-03-12 08:53:29 +00002395 if (Init)
Akira Hatanaka642f7992016-10-18 19:05:41 +00002396 CGF.EmitStoreThroughLValue(RValue::get(Init), LVal, /*isInit*/ true);
Alexey Bataev2df54a02015-03-12 08:53:29 +00002397 return LVal;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002398}
2399
Alexey Bataev3392d762016-02-16 11:18:12 +00002400void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) {
Alexey Bataev2df54a02015-03-12 08:53:29 +00002401 auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt();
2402 auto *CS = dyn_cast<CompoundStmt>(Stmt);
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002403 bool HasLastprivates = false;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002404 auto &&CodeGen = [&S, Stmt, CS, &HasLastprivates](CodeGenFunction &CGF,
2405 PrePostActionTy &) {
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002406 auto &C = CGF.CGM.getContext();
2407 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
2408 // Emit helper vars inits.
2409 LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.",
2410 CGF.Builder.getInt32(0));
2411 auto *GlobalUBVal = CS != nullptr ? CGF.Builder.getInt32(CS->size() - 1)
2412 : CGF.Builder.getInt32(0);
2413 LValue UB =
2414 createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal);
2415 LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.",
2416 CGF.Builder.getInt32(1));
2417 LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.",
2418 CGF.Builder.getInt32(0));
2419 // Loop counter.
2420 LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv.");
2421 OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
2422 CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV);
2423 OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
2424 CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB);
2425 // Generate condition for loop.
2426 BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue,
Adam Nemet484aa452017-03-27 19:17:25 +00002427 OK_Ordinary, S.getLocStart(), FPOptions());
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002428 // Increment for loop counter.
2429 UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary,
2430 S.getLocStart());
2431 auto BodyGen = [Stmt, CS, &S, &IV](CodeGenFunction &CGF) {
2432 // Iterate through all sections and emit a switch construct:
2433 // switch (IV) {
2434 // case 0:
2435 // <SectionStmt[0]>;
2436 // break;
2437 // ...
2438 // case <NumSection> - 1:
2439 // <SectionStmt[<NumSection> - 1]>;
2440 // break;
2441 // }
2442 // .omp.sections.exit:
2443 auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit");
2444 auto *SwitchStmt = CGF.Builder.CreateSwitch(
2445 CGF.EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB,
2446 CS == nullptr ? 1 : CS->size());
2447 if (CS) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002448 unsigned CaseNumber = 0;
Benjamin Kramer642f1732015-07-02 21:03:14 +00002449 for (auto *SubStmt : CS->children()) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002450 auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
2451 CGF.EmitBlock(CaseBB);
2452 SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB);
Benjamin Kramer642f1732015-07-02 21:03:14 +00002453 CGF.EmitStmt(SubStmt);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002454 CGF.EmitBranch(ExitBB);
Benjamin Kramer642f1732015-07-02 21:03:14 +00002455 ++CaseNumber;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002456 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002457 } else {
2458 auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
2459 CGF.EmitBlock(CaseBB);
2460 SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB);
2461 CGF.EmitStmt(Stmt);
2462 CGF.EmitBranch(ExitBB);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002463 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002464 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
Alexey Bataev2df54a02015-03-12 08:53:29 +00002465 };
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002466
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002467 CodeGenFunction::OMPPrivateScope LoopScope(CGF);
2468 if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) {
Alexey Bataev9efc03b2015-04-27 04:34:03 +00002469 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00002470 // initialization of firstprivate variables and post-update of lastprivate
2471 // variables.
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002472 CGF.CGM.getOpenMPRuntime().emitBarrierCall(
2473 CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
2474 /*ForceSimpleCall=*/true);
Alexey Bataev9efc03b2015-04-27 04:34:03 +00002475 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002476 CGF.EmitOMPPrivateClause(S, LoopScope);
2477 HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
2478 CGF.EmitOMPReductionClauseInit(S, LoopScope);
2479 (void)LoopScope.Privatize();
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002480
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002481 // Emit static non-chunked loop.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002482 OpenMPScheduleTy ScheduleKind;
2483 ScheduleKind.Schedule = OMPC_SCHEDULE_static;
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00002484 CGOpenMPRuntime::StaticRTInput StaticInit(
2485 /*IVSize=*/32, /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(),
2486 LB.getAddress(), UB.getAddress(), ST.getAddress());
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002487 CGF.CGM.getOpenMPRuntime().emitForStaticInit(
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00002488 CGF, S.getLocStart(), S.getDirectiveKind(), ScheduleKind, StaticInit);
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002489 // UB = min(UB, GlobalUB);
2490 auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart());
2491 auto *MinUBGlobalUB = CGF.Builder.CreateSelect(
2492 CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal);
2493 CGF.EmitStoreOfScalar(MinUBGlobalUB, UB);
2494 // IV = LB;
2495 CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV);
2496 // while (idx <= UB) { BODY; ++idx; }
2497 CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen,
2498 [](CodeGenFunction &) {});
2499 // Tell the runtime we are done.
Alexey Bataev957d8562016-11-17 15:12:05 +00002500 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
Alexey Bataevf43f7142017-09-06 16:17:35 +00002501 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd(),
2502 S.getDirectiveKind());
Alexey Bataev957d8562016-11-17 15:12:05 +00002503 };
2504 CGF.OMPCancelStack.emitExit(CGF, S.getDirectiveKind(), CodeGen);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002505 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel);
Alexey Bataev61205072016-03-02 04:57:40 +00002506 // Emit post-update of the reduction variables if IsLastIter != 0.
2507 emitPostUpdateForReductionClause(
2508 CGF, S, [&](CodeGenFunction &CGF) -> llvm::Value * {
2509 return CGF.Builder.CreateIsNotNull(
2510 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2511 });
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002512
2513 // Emit final copy of the lastprivate variables if IsLastIter != 0.
2514 if (HasLastprivates)
2515 CGF.EmitOMPLastprivateClauseFinal(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002516 S, /*NoFinals=*/false,
2517 CGF.Builder.CreateIsNotNull(
2518 CGF.EmitLoadOfScalar(IL, S.getLocStart())));
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002519 };
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002520
2521 bool HasCancel = false;
2522 if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S))
2523 HasCancel = OSD->hasCancel();
2524 else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S))
2525 HasCancel = OPSD->hasCancel();
Alexey Bataev957d8562016-11-17 15:12:05 +00002526 OMPCancelStackRAII CancelRegion(*this, S.getDirectiveKind(), HasCancel);
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002527 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen,
2528 HasCancel);
2529 // Emit barrier for lastprivates only if 'sections' directive has 'nowait'
2530 // clause. Otherwise the barrier will be generated by the codegen for the
2531 // directive.
2532 if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) {
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002533 // Emit implicit barrier to synchronize threads and avoid data races on
2534 // initialization of firstprivate variables.
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002535 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
2536 OMPD_unknown);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002537 }
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002538}
Alexey Bataev2df54a02015-03-12 08:53:29 +00002539
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002540void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
Alexey Bataev3392d762016-02-16 11:18:12 +00002541 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002542 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002543 EmitSections(S);
2544 }
Alexey Bataev2df54a02015-03-12 08:53:29 +00002545 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002546 if (!S.getSingleClause<OMPNowaitClause>()) {
Alexey Bataev3392d762016-02-16 11:18:12 +00002547 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
2548 OMPD_sections);
Alexey Bataevf2685682015-03-30 04:30:22 +00002549 }
Alexey Bataev2df54a02015-03-12 08:53:29 +00002550}
2551
2552void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002553 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002554 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002555 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002556 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev25e5b442015-09-15 12:52:43 +00002557 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen,
2558 S.hasCancel());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002559}
2560
Alexey Bataev6956e2e2015-02-05 06:35:41 +00002561void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00002562 llvm::SmallVector<const Expr *, 8> CopyprivateVars;
Alexey Bataev420d45b2015-04-14 05:11:24 +00002563 llvm::SmallVector<const Expr *, 8> DestExprs;
Alexey Bataeva63048e2015-03-23 06:18:07 +00002564 llvm::SmallVector<const Expr *, 8> SrcExprs;
Alexey Bataeva63048e2015-03-23 06:18:07 +00002565 llvm::SmallVector<const Expr *, 8> AssignmentOps;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002566 // Check if there are any 'copyprivate' clauses associated with this
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00002567 // 'single' construct.
Alexey Bataeva63048e2015-03-23 06:18:07 +00002568 // Build a list of copyprivate variables along with helper expressions
2569 // (<source>, <destination>, <destination>=<source> expressions)
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002570 for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00002571 CopyprivateVars.append(C->varlists().begin(), C->varlists().end());
Alexey Bataev420d45b2015-04-14 05:11:24 +00002572 DestExprs.append(C->destination_exprs().begin(),
2573 C->destination_exprs().end());
Alexey Bataeva63048e2015-03-23 06:18:07 +00002574 SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end());
Alexey Bataeva63048e2015-03-23 06:18:07 +00002575 AssignmentOps.append(C->assignment_ops().begin(),
2576 C->assignment_ops().end());
2577 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002578 // Emit code for 'single' region along with 'copyprivate' clauses
2579 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2580 Action.Enter(CGF);
2581 OMPPrivateScope SingleScope(CGF);
2582 (void)CGF.EmitOMPFirstprivateClause(S, SingleScope);
2583 CGF.EmitOMPPrivateClause(S, SingleScope);
2584 (void)SingleScope.Privatize();
2585 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2586 };
Alexey Bataev3392d762016-02-16 11:18:12 +00002587 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002588 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002589 CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(),
2590 CopyprivateVars, DestExprs,
2591 SrcExprs, AssignmentOps);
2592 }
2593 // Emit an implicit barrier at the end (to avoid data race on firstprivate
2594 // init or if no 'nowait' clause was specified and no 'copyprivate' clause).
Alexey Bataev417089f2016-02-17 13:19:37 +00002595 if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) {
Alexey Bataev5521d782015-04-24 04:21:15 +00002596 CGM.getOpenMPRuntime().emitBarrierCall(
2597 *this, S.getLocStart(),
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002598 S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single);
Alexey Bataevf2685682015-03-30 04:30:22 +00002599 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002600}
2601
Alexey Bataev8d690652014-12-04 07:23:53 +00002602void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002603 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2604 Action.Enter(CGF);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002605 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002606 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002607 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002608 CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart());
Alexander Musman80c22892014-07-17 08:54:58 +00002609}
2610
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002611void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002612 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2613 Action.Enter(CGF);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002614 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002615 };
Alexey Bataevfc57d162015-12-15 10:55:09 +00002616 Expr *Hint = nullptr;
2617 if (auto *HintClause = S.getSingleClause<OMPHintClause>())
2618 Hint = HintClause->getHint();
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002619 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataevfc57d162015-12-15 10:55:09 +00002620 CGM.getOpenMPRuntime().emitCriticalRegion(*this,
2621 S.getDirectiveName().getAsString(),
2622 CodeGen, S.getLocStart(), Hint);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002623}
2624
Alexey Bataev671605e2015-04-13 05:28:11 +00002625void CodeGenFunction::EmitOMPParallelForDirective(
2626 const OMPParallelForDirective &S) {
2627 // Emit directive as a combined directive that consists of two implicit
2628 // directives: 'parallel' with 'for' directive.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002629 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev957d8562016-11-17 15:12:05 +00002630 OMPCancelStackRAII CancelRegion(CGF, OMPD_parallel_for, S.hasCancel());
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002631 CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds,
2632 emitDispatchForLoopBounds);
Alexey Bataev671605e2015-04-13 05:28:11 +00002633 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002634 emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen,
2635 emitEmptyBoundParameters);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002636}
2637
Alexander Musmane4e893b2014-09-23 09:33:00 +00002638void CodeGenFunction::EmitOMPParallelForSimdDirective(
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00002639 const OMPParallelForSimdDirective &S) {
2640 // Emit directive as a combined directive that consists of two implicit
2641 // directives: 'parallel' with 'for' directive.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002642 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002643 CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds,
2644 emitDispatchForLoopBounds);
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00002645 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002646 emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen,
2647 emitEmptyBoundParameters);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002648}
2649
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002650void CodeGenFunction::EmitOMPParallelSectionsDirective(
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002651 const OMPParallelSectionsDirective &S) {
2652 // Emit directive as a combined directive that consists of two implicit
2653 // directives: 'parallel' with 'sections' directive.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002654 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2655 CGF.EmitSections(S);
2656 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002657 emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen,
2658 emitEmptyBoundParameters);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002659}
2660
Alexey Bataev7292c292016-04-25 12:22:29 +00002661void CodeGenFunction::EmitOMPTaskBasedDirective(const OMPExecutableDirective &S,
2662 const RegionCodeGenTy &BodyGen,
2663 const TaskGenTy &TaskGen,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002664 OMPTaskDataTy &Data) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002665 // Emit outlined function for task construct.
2666 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
Alexey Bataev62b63b12015-03-10 07:28:44 +00002667 auto *I = CS->getCapturedDecl()->param_begin();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002668 auto *PartId = std::next(I);
Alexey Bataev48591dd2016-04-20 04:01:36 +00002669 auto *TaskT = std::next(I, 4);
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002670 // Check if the task is final
2671 if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) {
2672 // If the condition constant folds and can be elided, try to avoid emitting
2673 // the condition and the dead arm of the if/else.
2674 auto *Cond = Clause->getCondition();
2675 bool CondConstant;
2676 if (ConstantFoldsToSimpleInteger(Cond, CondConstant))
2677 Data.Final.setInt(CondConstant);
2678 else
2679 Data.Final.setPointer(EvaluateExprAsBool(Cond));
2680 } else {
2681 // By default the task is not final.
2682 Data.Final.setInt(/*IntVal=*/false);
2683 }
Alexey Bataev1e1e2862016-05-10 12:21:02 +00002684 // Check if the task has 'priority' clause.
2685 if (const auto *Clause = S.getSingleClause<OMPPriorityClause>()) {
Alexey Bataev1e1e2862016-05-10 12:21:02 +00002686 auto *Prio = Clause->getPriority();
Alexey Bataev5140e742016-07-19 04:21:09 +00002687 Data.Priority.setInt(/*IntVal=*/true);
Alexey Bataevad537bb2016-05-30 09:06:50 +00002688 Data.Priority.setPointer(EmitScalarConversion(
2689 EmitScalarExpr(Prio), Prio->getType(),
2690 getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1),
2691 Prio->getExprLoc()));
Alexey Bataev1e1e2862016-05-10 12:21:02 +00002692 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00002693 // The first function argument for tasks is a thread id, the second one is a
2694 // part id (0 for tied tasks, >=0 for untied task).
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002695 llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
2696 // Get list of private variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002697 for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002698 auto IRef = C->varlist_begin();
2699 for (auto *IInit : C->private_copies()) {
2700 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2701 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
Alexey Bataev7292c292016-04-25 12:22:29 +00002702 Data.PrivateVars.push_back(*IRef);
2703 Data.PrivateCopies.push_back(IInit);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002704 }
2705 ++IRef;
2706 }
2707 }
2708 EmittedAsPrivate.clear();
2709 // Get list of firstprivate variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002710 for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002711 auto IRef = C->varlist_begin();
2712 auto IElemInitRef = C->inits().begin();
2713 for (auto *IInit : C->private_copies()) {
2714 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2715 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
Alexey Bataev7292c292016-04-25 12:22:29 +00002716 Data.FirstprivateVars.push_back(*IRef);
2717 Data.FirstprivateCopies.push_back(IInit);
2718 Data.FirstprivateInits.push_back(*IElemInitRef);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002719 }
Richard Trieucc3949d2016-02-18 22:34:54 +00002720 ++IRef;
2721 ++IElemInitRef;
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002722 }
2723 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00002724 // Get list of lastprivate variables (for taskloops).
2725 llvm::DenseMap<const VarDecl *, const DeclRefExpr *> LastprivateDstsOrigs;
2726 for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) {
2727 auto IRef = C->varlist_begin();
2728 auto ID = C->destination_exprs().begin();
2729 for (auto *IInit : C->private_copies()) {
2730 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2731 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
2732 Data.LastprivateVars.push_back(*IRef);
2733 Data.LastprivateCopies.push_back(IInit);
2734 }
2735 LastprivateDstsOrigs.insert(
2736 {cast<VarDecl>(cast<DeclRefExpr>(*ID)->getDecl()),
2737 cast<DeclRefExpr>(*IRef)});
2738 ++IRef;
2739 ++ID;
2740 }
2741 }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002742 SmallVector<const Expr *, 4> LHSs;
2743 SmallVector<const Expr *, 4> RHSs;
2744 for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) {
2745 auto IPriv = C->privates().begin();
2746 auto IRed = C->reduction_ops().begin();
2747 auto ILHS = C->lhs_exprs().begin();
2748 auto IRHS = C->rhs_exprs().begin();
2749 for (const auto *Ref : C->varlists()) {
2750 Data.ReductionVars.emplace_back(Ref);
2751 Data.ReductionCopies.emplace_back(*IPriv);
2752 Data.ReductionOps.emplace_back(*IRed);
2753 LHSs.emplace_back(*ILHS);
2754 RHSs.emplace_back(*IRHS);
2755 std::advance(IPriv, 1);
2756 std::advance(IRed, 1);
2757 std::advance(ILHS, 1);
2758 std::advance(IRHS, 1);
2759 }
2760 }
2761 Data.Reductions = CGM.getOpenMPRuntime().emitTaskReductionInit(
2762 *this, S.getLocStart(), LHSs, RHSs, Data);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00002763 // Build list of dependences.
Alexey Bataev7292c292016-04-25 12:22:29 +00002764 for (const auto *C : S.getClausesOfKind<OMPDependClause>())
2765 for (auto *IRef : C->varlists())
2766 Data.Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef));
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002767 auto &&CodeGen = [&Data, &S, CS, &BodyGen, &LastprivateDstsOrigs](
Alexey Bataevf93095a2016-05-05 08:46:22 +00002768 CodeGenFunction &CGF, PrePostActionTy &Action) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002769 // Set proper addresses for generated private copies.
Alexey Bataev7292c292016-04-25 12:22:29 +00002770 OMPPrivateScope Scope(CGF);
Alexey Bataevf93095a2016-05-05 08:46:22 +00002771 if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty() ||
2772 !Data.LastprivateVars.empty()) {
Alexey Bataev3c595a62017-08-14 15:01:03 +00002773 enum { PrivatesParam = 2, CopyFnParam = 3 };
Alexey Bataev48591dd2016-04-20 04:01:36 +00002774 auto *CopyFn = CGF.Builder.CreateLoad(
2775 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3)));
2776 auto *PrivatesPtr = CGF.Builder.CreateLoad(
2777 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2)));
2778 // Map privates.
2779 llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs;
2780 llvm::SmallVector<llvm::Value *, 16> CallArgs;
2781 CallArgs.push_back(PrivatesPtr);
Alexey Bataev7292c292016-04-25 12:22:29 +00002782 for (auto *E : Data.PrivateVars) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00002783 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2784 Address PrivatePtr = CGF.CreateMemTemp(
2785 CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr");
2786 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
2787 CallArgs.push_back(PrivatePtr.getPointer());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002788 }
Alexey Bataev7292c292016-04-25 12:22:29 +00002789 for (auto *E : Data.FirstprivateVars) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00002790 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2791 Address PrivatePtr =
2792 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()),
2793 ".firstpriv.ptr.addr");
2794 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
2795 CallArgs.push_back(PrivatePtr.getPointer());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002796 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00002797 for (auto *E : Data.LastprivateVars) {
2798 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2799 Address PrivatePtr =
2800 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()),
2801 ".lastpriv.ptr.addr");
2802 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
2803 CallArgs.push_back(PrivatePtr.getPointer());
2804 }
Alexey Bataev3c595a62017-08-14 15:01:03 +00002805 CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getLocStart(),
2806 CopyFn, CallArgs);
Alexey Bataevf93095a2016-05-05 08:46:22 +00002807 for (auto &&Pair : LastprivateDstsOrigs) {
2808 auto *OrigVD = cast<VarDecl>(Pair.second->getDecl());
2809 DeclRefExpr DRE(
2810 const_cast<VarDecl *>(OrigVD),
2811 /*RefersToEnclosingVariableOrCapture=*/CGF.CapturedStmtInfo->lookup(
2812 OrigVD) != nullptr,
2813 Pair.second->getType(), VK_LValue, Pair.second->getExprLoc());
2814 Scope.addPrivate(Pair.first, [&CGF, &DRE]() {
2815 return CGF.EmitLValue(&DRE).getAddress();
2816 });
2817 }
Alexey Bataev48591dd2016-04-20 04:01:36 +00002818 for (auto &&Pair : PrivatePtrs) {
2819 Address Replacement(CGF.Builder.CreateLoad(Pair.second),
2820 CGF.getContext().getDeclAlign(Pair.first));
2821 Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; });
2822 }
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002823 }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002824 if (Data.Reductions) {
2825 OMPLexicalScope LexScope(CGF, S, /*AsInlined=*/true);
2826 ReductionCodeGen RedCG(Data.ReductionVars, Data.ReductionCopies,
2827 Data.ReductionOps);
2828 llvm::Value *ReductionsPtr = CGF.Builder.CreateLoad(
2829 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(9)));
2830 for (unsigned Cnt = 0, E = Data.ReductionVars.size(); Cnt < E; ++Cnt) {
2831 RedCG.emitSharedLValue(CGF, Cnt);
2832 RedCG.emitAggregateType(CGF, Cnt);
2833 Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem(
2834 CGF, S.getLocStart(), ReductionsPtr, RedCG.getSharedLValue(Cnt));
2835 Replacement =
2836 Address(CGF.EmitScalarConversion(
2837 Replacement.getPointer(), CGF.getContext().VoidPtrTy,
2838 CGF.getContext().getPointerType(
2839 Data.ReductionCopies[Cnt]->getType()),
2840 SourceLocation()),
2841 Replacement.getAlignment());
2842 Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement);
2843 Scope.addPrivate(RedCG.getBaseDecl(Cnt),
2844 [Replacement]() { return Replacement; });
2845 // FIXME: This must removed once the runtime library is fixed.
2846 // Emit required threadprivate variables for
2847 // initilizer/combiner/finalizer.
2848 CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getLocStart(),
2849 RedCG, Cnt);
2850 }
2851 }
Alexey Bataev88202be2017-07-27 13:20:36 +00002852 // Privatize all private variables except for in_reduction items.
Alexey Bataev48591dd2016-04-20 04:01:36 +00002853 (void)Scope.Privatize();
Alexey Bataev88202be2017-07-27 13:20:36 +00002854 SmallVector<const Expr *, 4> InRedVars;
2855 SmallVector<const Expr *, 4> InRedPrivs;
2856 SmallVector<const Expr *, 4> InRedOps;
2857 SmallVector<const Expr *, 4> TaskgroupDescriptors;
2858 for (const auto *C : S.getClausesOfKind<OMPInReductionClause>()) {
2859 auto IPriv = C->privates().begin();
2860 auto IRed = C->reduction_ops().begin();
2861 auto ITD = C->taskgroup_descriptors().begin();
2862 for (const auto *Ref : C->varlists()) {
2863 InRedVars.emplace_back(Ref);
2864 InRedPrivs.emplace_back(*IPriv);
2865 InRedOps.emplace_back(*IRed);
2866 TaskgroupDescriptors.emplace_back(*ITD);
2867 std::advance(IPriv, 1);
2868 std::advance(IRed, 1);
2869 std::advance(ITD, 1);
2870 }
2871 }
2872 // Privatize in_reduction items here, because taskgroup descriptors must be
2873 // privatized earlier.
2874 OMPPrivateScope InRedScope(CGF);
2875 if (!InRedVars.empty()) {
2876 ReductionCodeGen RedCG(InRedVars, InRedPrivs, InRedOps);
2877 for (unsigned Cnt = 0, E = InRedVars.size(); Cnt < E; ++Cnt) {
2878 RedCG.emitSharedLValue(CGF, Cnt);
2879 RedCG.emitAggregateType(CGF, Cnt);
2880 // The taskgroup descriptor variable is always implicit firstprivate and
2881 // privatized already during procoessing of the firstprivates.
2882 llvm::Value *ReductionsPtr = CGF.EmitLoadOfScalar(
2883 CGF.EmitLValue(TaskgroupDescriptors[Cnt]), SourceLocation());
2884 Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem(
2885 CGF, S.getLocStart(), ReductionsPtr, RedCG.getSharedLValue(Cnt));
2886 Replacement = Address(
2887 CGF.EmitScalarConversion(
2888 Replacement.getPointer(), CGF.getContext().VoidPtrTy,
2889 CGF.getContext().getPointerType(InRedPrivs[Cnt]->getType()),
2890 SourceLocation()),
2891 Replacement.getAlignment());
2892 Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement);
2893 InRedScope.addPrivate(RedCG.getBaseDecl(Cnt),
2894 [Replacement]() { return Replacement; });
2895 // FIXME: This must removed once the runtime library is fixed.
2896 // Emit required threadprivate variables for
2897 // initilizer/combiner/finalizer.
2898 CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getLocStart(),
2899 RedCG, Cnt);
2900 }
2901 }
2902 (void)InRedScope.Privatize();
Alexey Bataev48591dd2016-04-20 04:01:36 +00002903
2904 Action.Enter(CGF);
Alexey Bataev7292c292016-04-25 12:22:29 +00002905 BodyGen(CGF);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002906 };
Alexey Bataev7292c292016-04-25 12:22:29 +00002907 auto *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction(
2908 S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied,
2909 Data.NumberOfParts);
2910 OMPLexicalScope Scope(*this, S);
2911 TaskGen(*this, OutlinedFn, Data);
2912}
2913
2914void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) {
2915 // Emit outlined function for task construct.
2916 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
2917 auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002918 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
Alexey Bataev1d677132015-04-22 13:57:31 +00002919 const Expr *IfCond = nullptr;
Alexey Bataev7371aa32015-09-03 08:45:56 +00002920 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
2921 if (C->getNameModifier() == OMPD_unknown ||
2922 C->getNameModifier() == OMPD_task) {
2923 IfCond = C->getCondition();
2924 break;
2925 }
Alexey Bataev1d677132015-04-22 13:57:31 +00002926 }
Alexey Bataev7292c292016-04-25 12:22:29 +00002927
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002928 OMPTaskDataTy Data;
2929 // Check if we should emit tied or untied task.
2930 Data.Tied = !S.getSingleClause<OMPUntiedClause>();
Alexey Bataev7292c292016-04-25 12:22:29 +00002931 auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) {
2932 CGF.EmitStmt(CS->getCapturedStmt());
2933 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002934 auto &&TaskGen = [&S, SharedsTy, CapturedStruct,
Alexey Bataev7292c292016-04-25 12:22:29 +00002935 IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002936 const OMPTaskDataTy &Data) {
2937 CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getLocStart(), S, OutlinedFn,
2938 SharedsTy, CapturedStruct, IfCond,
2939 Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00002940 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002941 EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002942}
2943
Alexey Bataev9f797f32015-02-05 05:57:51 +00002944void CodeGenFunction::EmitOMPTaskyieldDirective(
2945 const OMPTaskyieldDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002946 CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart());
Alexey Bataev68446b72014-07-18 07:47:19 +00002947}
2948
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00002949void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) {
Alexey Bataevf2685682015-03-30 04:30:22 +00002950 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier);
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002951}
2952
Alexey Bataev8b8e2022015-04-27 05:22:09 +00002953void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) {
2954 CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart());
Alexey Bataev2df347a2014-07-18 10:17:07 +00002955}
2956
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002957void CodeGenFunction::EmitOMPTaskgroupDirective(
2958 const OMPTaskgroupDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002959 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2960 Action.Enter(CGF);
Alexey Bataev3b1b8952017-07-25 15:53:26 +00002961 if (const Expr *E = S.getReductionRef()) {
2962 SmallVector<const Expr *, 4> LHSs;
2963 SmallVector<const Expr *, 4> RHSs;
2964 OMPTaskDataTy Data;
2965 for (const auto *C : S.getClausesOfKind<OMPTaskReductionClause>()) {
2966 auto IPriv = C->privates().begin();
2967 auto IRed = C->reduction_ops().begin();
2968 auto ILHS = C->lhs_exprs().begin();
2969 auto IRHS = C->rhs_exprs().begin();
2970 for (const auto *Ref : C->varlists()) {
2971 Data.ReductionVars.emplace_back(Ref);
2972 Data.ReductionCopies.emplace_back(*IPriv);
2973 Data.ReductionOps.emplace_back(*IRed);
2974 LHSs.emplace_back(*ILHS);
2975 RHSs.emplace_back(*IRHS);
2976 std::advance(IPriv, 1);
2977 std::advance(IRed, 1);
2978 std::advance(ILHS, 1);
2979 std::advance(IRHS, 1);
2980 }
2981 }
2982 llvm::Value *ReductionDesc =
2983 CGF.CGM.getOpenMPRuntime().emitTaskReductionInit(CGF, S.getLocStart(),
2984 LHSs, RHSs, Data);
2985 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2986 CGF.EmitVarDecl(*VD);
2987 CGF.EmitStoreOfScalar(ReductionDesc, CGF.GetAddrOfLocalVar(VD),
2988 /*Volatile=*/false, E->getType());
2989 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002990 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002991 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002992 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002993 CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getLocStart());
2994}
2995
Alexey Bataevcc37cc12014-11-20 04:34:54 +00002996void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002997 CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002998 if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002999 return llvm::makeArrayRef(FlushClause->varlist_begin(),
3000 FlushClause->varlist_end());
3001 }
3002 return llvm::None;
3003 }(), S.getLocStart());
Alexey Bataev6125da92014-07-21 11:26:11 +00003004}
3005
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003006void CodeGenFunction::EmitOMPDistributeLoop(const OMPLoopDirective &S,
3007 const CodeGenLoopTy &CodeGenLoop,
3008 Expr *IncExpr) {
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003009 // Emit the loop iteration variable.
3010 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
3011 auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
3012 EmitVarDecl(*IVDecl);
3013
3014 // Emit the iterations count variable.
3015 // If it is not a variable, Sema decided to calculate iterations count on each
3016 // iteration (e.g., it is foldable into a constant).
3017 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
3018 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
3019 // Emit calculation of the iterations count.
3020 EmitIgnoredExpr(S.getCalcLastIteration());
3021 }
3022
3023 auto &RT = CGM.getOpenMPRuntime();
3024
Carlo Bertolli962bb802017-01-03 18:24:42 +00003025 bool HasLastprivateClause = false;
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003026 // Check pre-condition.
3027 {
Alexey Bataev5a3af132016-03-29 08:58:54 +00003028 OMPLoopScope PreInitScope(*this, S);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003029 // Skip the entire loop if we don't meet the precondition.
3030 // If the condition constant folds and can be elided, avoid emitting the
3031 // whole loop.
3032 bool CondConstant;
3033 llvm::BasicBlock *ContBlock = nullptr;
3034 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
3035 if (!CondConstant)
3036 return;
3037 } else {
3038 auto *ThenBlock = createBasicBlock("omp.precond.then");
3039 ContBlock = createBasicBlock("omp.precond.end");
3040 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
3041 getProfileCount(&S));
3042 EmitBlock(ThenBlock);
3043 incrementProfileCounter(&S);
3044 }
3045
3046 // Emit 'then' code.
3047 {
3048 // Emit helper vars inits.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003049
3050 LValue LB = EmitOMPHelperVar(
3051 *this, cast<DeclRefExpr>(
3052 (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3053 ? S.getCombinedLowerBoundVariable()
3054 : S.getLowerBoundVariable())));
3055 LValue UB = EmitOMPHelperVar(
3056 *this, cast<DeclRefExpr>(
3057 (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3058 ? S.getCombinedUpperBoundVariable()
3059 : S.getUpperBoundVariable())));
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003060 LValue ST =
3061 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
3062 LValue IL =
3063 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
3064
3065 OMPPrivateScope LoopScope(*this);
Carlo Bertolli962bb802017-01-03 18:24:42 +00003066 if (EmitOMPFirstprivateClause(S, LoopScope)) {
3067 // Emit implicit barrier to synchronize threads and avoid data races on
3068 // initialization of firstprivate variables and post-update of
3069 // lastprivate variables.
3070 CGM.getOpenMPRuntime().emitBarrierCall(
3071 *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
3072 /*ForceSimpleCall=*/true);
3073 }
3074 EmitOMPPrivateClause(S, LoopScope);
3075 HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003076 EmitOMPPrivateLoopCounters(S, LoopScope);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003077 (void)LoopScope.Privatize();
3078
3079 // Detect the distribute schedule kind and chunk.
3080 llvm::Value *Chunk = nullptr;
3081 OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown;
3082 if (auto *C = S.getSingleClause<OMPDistScheduleClause>()) {
3083 ScheduleKind = C->getDistScheduleKind();
3084 if (const auto *Ch = C->getChunkSize()) {
3085 Chunk = EmitScalarExpr(Ch);
3086 Chunk = EmitScalarConversion(Chunk, Ch->getType(),
3087 S.getIterationVariable()->getType(),
3088 S.getLocStart());
3089 }
3090 }
3091 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
3092 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
3093
3094 // OpenMP [2.10.8, distribute Construct, Description]
3095 // If dist_schedule is specified, kind must be static. If specified,
3096 // iterations are divided into chunks of size chunk_size, chunks are
3097 // assigned to the teams of the league in a round-robin fashion in the
3098 // order of the team number. When no chunk_size is specified, the
3099 // iteration space is divided into chunks that are approximately equal
3100 // in size, and at most one chunk is distributed to each team of the
3101 // league. The size of the chunks is unspecified in this case.
3102 if (RT.isStaticNonchunked(ScheduleKind,
3103 /* Chunked */ Chunk != nullptr)) {
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003104 CGOpenMPRuntime::StaticRTInput StaticInit(
3105 IVSize, IVSigned, /* Ordered = */ false, IL.getAddress(),
3106 LB.getAddress(), UB.getAddress(), ST.getAddress());
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003107 RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003108 StaticInit);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003109 auto LoopExit =
3110 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
3111 // UB = min(UB, GlobalUB);
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003112 EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3113 ? S.getCombinedEnsureUpperBound()
3114 : S.getEnsureUpperBound());
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003115 // IV = LB;
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003116 EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3117 ? S.getCombinedInit()
3118 : S.getInit());
3119
3120 Expr *Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3121 ? S.getCombinedCond()
3122 : S.getCond();
3123
3124 // for distribute alone, codegen
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003125 // while (idx <= UB) { BODY; ++idx; }
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003126 // when combined with 'for' (e.g. as in 'distribute parallel for')
3127 // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; }
3128 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), Cond, IncExpr,
3129 [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) {
3130 CodeGenLoop(CGF, S, LoopExit);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003131 },
3132 [](CodeGenFunction &) {});
3133 EmitBlock(LoopExit.getBlock());
3134 // Tell the runtime we are done.
Alexey Bataevf43f7142017-09-06 16:17:35 +00003135 RT.emitForStaticFinish(*this, S.getLocStart(), S.getDirectiveKind());
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003136 } else {
3137 // Emit the outer loop, which requests its work chunk [LB..UB] from
3138 // runtime and runs the inner loop to process it.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003139 const OMPLoopArguments LoopArguments = {
3140 LB.getAddress(), UB.getAddress(), ST.getAddress(), IL.getAddress(),
3141 Chunk};
3142 EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope, LoopArguments,
3143 CodeGenLoop);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003144 }
Carlo Bertolli962bb802017-01-03 18:24:42 +00003145
3146 // Emit final copy of the lastprivate variables if IsLastIter != 0.
3147 if (HasLastprivateClause)
3148 EmitOMPLastprivateClauseFinal(
3149 S, /*NoFinals=*/false,
3150 Builder.CreateIsNotNull(
3151 EmitLoadOfScalar(IL, S.getLocStart())));
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003152 }
3153
3154 // We're now done with the loop, so jump to the continuation block.
3155 if (ContBlock) {
3156 EmitBranch(ContBlock);
3157 EmitBlock(ContBlock, true);
3158 }
3159 }
3160}
3161
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003162void CodeGenFunction::EmitOMPDistributeDirective(
3163 const OMPDistributeDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003164 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003165
3166 CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc());
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003167 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00003168 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003169 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen,
3170 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003171}
3172
Alexey Bataev5f600d62015-09-29 03:48:57 +00003173static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM,
3174 const CapturedStmt *S) {
3175 CodeGenFunction CGF(CGM, /*suppressNewContext=*/true);
3176 CodeGenFunction::CGCapturedStmtInfo CapStmtInfo;
3177 CGF.CapturedStmtInfo = &CapStmtInfo;
3178 auto *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S);
3179 Fn->addFnAttr(llvm::Attribute::NoInline);
3180 return Fn;
3181}
3182
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003183void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) {
Alexey Bataev8b427062016-05-25 12:36:08 +00003184 if (!S.getAssociatedStmt()) {
3185 for (const auto *DC : S.getClausesOfKind<OMPDependClause>())
3186 CGM.getOpenMPRuntime().emitDoacrossOrdered(*this, DC);
Alexey Bataev8ef31412015-12-18 07:58:25 +00003187 return;
Alexey Bataev8b427062016-05-25 12:36:08 +00003188 }
Alexey Bataev5f600d62015-09-29 03:48:57 +00003189 auto *C = S.getSingleClause<OMPSIMDClause>();
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003190 auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF,
3191 PrePostActionTy &Action) {
Alexey Bataev5f600d62015-09-29 03:48:57 +00003192 if (C) {
3193 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
3194 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
3195 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
3196 auto *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS);
Alexey Bataev3c595a62017-08-14 15:01:03 +00003197 CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getLocStart(),
3198 OutlinedFn, CapturedVars);
Alexey Bataev5f600d62015-09-29 03:48:57 +00003199 } else {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003200 Action.Enter(CGF);
Alexey Bataev5f600d62015-09-29 03:48:57 +00003201 CGF.EmitStmt(
3202 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3203 }
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003204 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00003205 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev5f600d62015-09-29 03:48:57 +00003206 CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart(), !C);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003207}
3208
Alexey Bataevb57056f2015-01-22 06:17:56 +00003209static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003210 QualType SrcType, QualType DestType,
3211 SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00003212 assert(CGF.hasScalarEvaluationKind(DestType) &&
3213 "DestType must have scalar evaluation kind.");
3214 assert(!Val.isAggregate() && "Must be a scalar or complex.");
3215 return Val.isScalar()
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003216 ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType,
3217 Loc)
Alexey Bataevb57056f2015-01-22 06:17:56 +00003218 : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003219 DestType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003220}
3221
3222static CodeGenFunction::ComplexPairTy
3223convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003224 QualType DestType, SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00003225 assert(CGF.getEvaluationKind(DestType) == TEK_Complex &&
3226 "DestType must have complex evaluation kind.");
3227 CodeGenFunction::ComplexPairTy ComplexVal;
3228 if (Val.isScalar()) {
3229 // Convert the input element to the element type of the complex.
3230 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003231 auto ScalarVal = CGF.EmitScalarConversion(Val.getScalarVal(), SrcType,
3232 DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003233 ComplexVal = CodeGenFunction::ComplexPairTy(
3234 ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType()));
3235 } else {
3236 assert(Val.isComplex() && "Must be a scalar or complex.");
3237 auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType();
3238 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
3239 ComplexVal.first = CGF.EmitScalarConversion(
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003240 Val.getComplexVal().first, SrcElementType, DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003241 ComplexVal.second = CGF.EmitScalarConversion(
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003242 Val.getComplexVal().second, SrcElementType, DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003243 }
3244 return ComplexVal;
3245}
3246
Alexey Bataev5e018f92015-04-23 06:35:10 +00003247static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst,
3248 LValue LVal, RValue RVal) {
3249 if (LVal.isGlobalReg()) {
3250 CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal);
3251 } else {
JF Bastien92f4ef12016-04-06 17:26:42 +00003252 CGF.EmitAtomicStore(RVal, LVal,
3253 IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
3254 : llvm::AtomicOrdering::Monotonic,
Alexey Bataev5e018f92015-04-23 06:35:10 +00003255 LVal.isVolatile(), /*IsInit=*/false);
3256 }
3257}
3258
Alexey Bataev8524d152016-01-21 12:35:58 +00003259void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal,
3260 QualType RValTy, SourceLocation Loc) {
3261 switch (getEvaluationKind(LVal.getType())) {
Alexey Bataev5e018f92015-04-23 06:35:10 +00003262 case TEK_Scalar:
Alexey Bataev8524d152016-01-21 12:35:58 +00003263 EmitStoreThroughLValue(RValue::get(convertToScalarValue(
3264 *this, RVal, RValTy, LVal.getType(), Loc)),
3265 LVal);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003266 break;
3267 case TEK_Complex:
Alexey Bataev8524d152016-01-21 12:35:58 +00003268 EmitStoreOfComplex(
3269 convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal,
Alexey Bataev5e018f92015-04-23 06:35:10 +00003270 /*isInit=*/false);
3271 break;
3272 case TEK_Aggregate:
3273 llvm_unreachable("Must be a scalar or complex.");
3274 }
3275}
3276
Alexey Bataevb57056f2015-01-22 06:17:56 +00003277static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst,
3278 const Expr *X, const Expr *V,
3279 SourceLocation Loc) {
3280 // v = x;
3281 assert(V->isLValue() && "V of 'omp atomic read' is not lvalue");
3282 assert(X->isLValue() && "X of 'omp atomic read' is not lvalue");
3283 LValue XLValue = CGF.EmitLValue(X);
3284 LValue VLValue = CGF.EmitLValue(V);
David Majnemera5b195a2015-02-14 01:35:12 +00003285 RValue Res = XLValue.isGlobalReg()
3286 ? CGF.EmitLoadOfLValue(XLValue, Loc)
JF Bastien92f4ef12016-04-06 17:26:42 +00003287 : CGF.EmitAtomicLoad(
3288 XLValue, Loc,
3289 IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
3290 : llvm::AtomicOrdering::Monotonic,
3291 XLValue.isVolatile());
Alexey Bataevb57056f2015-01-22 06:17:56 +00003292 // OpenMP, 2.12.6, atomic Construct
3293 // Any atomic construct with a seq_cst clause forces the atomically
3294 // performed operation to include an implicit flush operation without a
3295 // list.
3296 if (IsSeqCst)
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003297 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
Alexey Bataev8524d152016-01-21 12:35:58 +00003298 CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003299}
3300
Alexey Bataevb8329262015-02-27 06:33:30 +00003301static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst,
3302 const Expr *X, const Expr *E,
3303 SourceLocation Loc) {
3304 // x = expr;
3305 assert(X->isLValue() && "X of 'omp atomic write' is not lvalue");
Alexey Bataev5e018f92015-04-23 06:35:10 +00003306 emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E));
Alexey Bataevb8329262015-02-27 06:33:30 +00003307 // OpenMP, 2.12.6, atomic Construct
3308 // Any atomic construct with a seq_cst clause forces the atomically
3309 // performed operation to include an implicit flush operation without a
3310 // list.
3311 if (IsSeqCst)
3312 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
3313}
3314
Benjamin Kramer439ee9d2015-05-01 13:59:53 +00003315static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X,
3316 RValue Update,
3317 BinaryOperatorKind BO,
3318 llvm::AtomicOrdering AO,
3319 bool IsXLHSInRHSPart) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003320 auto &Context = CGF.CGM.getContext();
3321 // Allow atomicrmw only if 'x' and 'update' are integer values, lvalue for 'x'
Alexey Bataevb4505a72015-03-30 05:20:59 +00003322 // expression is simple and atomic is allowed for the given type for the
3323 // target platform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003324 if (BO == BO_Comma || !Update.isScalar() ||
Alexey Bataev9d541a72015-05-08 11:47:16 +00003325 !Update.getScalarVal()->getType()->isIntegerTy() ||
3326 !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) &&
3327 (Update.getScalarVal()->getType() !=
John McCall7f416cc2015-09-08 08:05:57 +00003328 X.getAddress().getElementType())) ||
3329 !X.getAddress().getElementType()->isIntegerTy() ||
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003330 !Context.getTargetInfo().hasBuiltinAtomic(
3331 Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment())))
Alexey Bataev5e018f92015-04-23 06:35:10 +00003332 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003333
3334 llvm::AtomicRMWInst::BinOp RMWOp;
3335 switch (BO) {
3336 case BO_Add:
3337 RMWOp = llvm::AtomicRMWInst::Add;
3338 break;
3339 case BO_Sub:
3340 if (!IsXLHSInRHSPart)
Alexey Bataev5e018f92015-04-23 06:35:10 +00003341 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003342 RMWOp = llvm::AtomicRMWInst::Sub;
3343 break;
3344 case BO_And:
3345 RMWOp = llvm::AtomicRMWInst::And;
3346 break;
3347 case BO_Or:
3348 RMWOp = llvm::AtomicRMWInst::Or;
3349 break;
3350 case BO_Xor:
3351 RMWOp = llvm::AtomicRMWInst::Xor;
3352 break;
3353 case BO_LT:
3354 RMWOp = X.getType()->hasSignedIntegerRepresentation()
3355 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min
3356 : llvm::AtomicRMWInst::Max)
3357 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin
3358 : llvm::AtomicRMWInst::UMax);
3359 break;
3360 case BO_GT:
3361 RMWOp = X.getType()->hasSignedIntegerRepresentation()
3362 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max
3363 : llvm::AtomicRMWInst::Min)
3364 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax
3365 : llvm::AtomicRMWInst::UMin);
3366 break;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003367 case BO_Assign:
3368 RMWOp = llvm::AtomicRMWInst::Xchg;
3369 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003370 case BO_Mul:
3371 case BO_Div:
3372 case BO_Rem:
3373 case BO_Shl:
3374 case BO_Shr:
3375 case BO_LAnd:
3376 case BO_LOr:
Alexey Bataev5e018f92015-04-23 06:35:10 +00003377 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003378 case BO_PtrMemD:
3379 case BO_PtrMemI:
3380 case BO_LE:
3381 case BO_GE:
3382 case BO_EQ:
3383 case BO_NE:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003384 case BO_AddAssign:
3385 case BO_SubAssign:
3386 case BO_AndAssign:
3387 case BO_OrAssign:
3388 case BO_XorAssign:
3389 case BO_MulAssign:
3390 case BO_DivAssign:
3391 case BO_RemAssign:
3392 case BO_ShlAssign:
3393 case BO_ShrAssign:
3394 case BO_Comma:
3395 llvm_unreachable("Unsupported atomic update operation");
3396 }
3397 auto *UpdateVal = Update.getScalarVal();
3398 if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) {
3399 UpdateVal = CGF.Builder.CreateIntCast(
John McCall7f416cc2015-09-08 08:05:57 +00003400 IC, X.getAddress().getElementType(),
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003401 X.getType()->hasSignedIntegerRepresentation());
3402 }
John McCall7f416cc2015-09-08 08:05:57 +00003403 auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003404 return std::make_pair(true, RValue::get(Res));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003405}
3406
Alexey Bataev5e018f92015-04-23 06:35:10 +00003407std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr(
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003408 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
3409 llvm::AtomicOrdering AO, SourceLocation Loc,
3410 const llvm::function_ref<RValue(RValue)> &CommonGen) {
3411 // Update expressions are allowed to have the following forms:
3412 // x binop= expr; -> xrval + expr;
3413 // x++, ++x -> xrval + 1;
3414 // x--, --x -> xrval - 1;
3415 // x = x binop expr; -> xrval binop expr
3416 // x = expr Op x; - > expr binop xrval;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003417 auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart);
3418 if (!Res.first) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003419 if (X.isGlobalReg()) {
3420 // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop
3421 // 'xrval'.
3422 EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X);
3423 } else {
3424 // Perform compare-and-swap procedure.
3425 EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified());
Alexey Bataevb4505a72015-03-30 05:20:59 +00003426 }
3427 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00003428 return Res;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003429}
3430
3431static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst,
3432 const Expr *X, const Expr *E,
3433 const Expr *UE, bool IsXLHSInRHSPart,
3434 SourceLocation Loc) {
3435 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
3436 "Update expr in 'atomic update' must be a binary operator.");
3437 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
3438 // Update expressions are allowed to have the following forms:
3439 // x binop= expr; -> xrval + expr;
3440 // x++, ++x -> xrval + 1;
3441 // x--, --x -> xrval - 1;
3442 // x = x binop expr; -> xrval binop expr
3443 // x = expr Op x; - > expr binop xrval;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003444 assert(X->isLValue() && "X of 'omp atomic update' is not lvalue");
Alexey Bataevb4505a72015-03-30 05:20:59 +00003445 LValue XLValue = CGF.EmitLValue(X);
3446 RValue ExprRValue = CGF.EmitAnyExpr(E);
JF Bastien92f4ef12016-04-06 17:26:42 +00003447 auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
3448 : llvm::AtomicOrdering::Monotonic;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003449 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
3450 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
3451 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
3452 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
3453 auto Gen =
3454 [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue {
3455 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
3456 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
3457 return CGF.EmitAnyExpr(UE);
3458 };
Alexey Bataev5e018f92015-04-23 06:35:10 +00003459 (void)CGF.EmitOMPAtomicSimpleUpdateExpr(
3460 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
3461 // OpenMP, 2.12.6, atomic Construct
3462 // Any atomic construct with a seq_cst clause forces the atomically
3463 // performed operation to include an implicit flush operation without a
3464 // list.
3465 if (IsSeqCst)
3466 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
3467}
3468
3469static RValue convertToType(CodeGenFunction &CGF, RValue Value,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003470 QualType SourceType, QualType ResType,
3471 SourceLocation Loc) {
Alexey Bataev5e018f92015-04-23 06:35:10 +00003472 switch (CGF.getEvaluationKind(ResType)) {
3473 case TEK_Scalar:
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003474 return RValue::get(
3475 convertToScalarValue(CGF, Value, SourceType, ResType, Loc));
Alexey Bataev5e018f92015-04-23 06:35:10 +00003476 case TEK_Complex: {
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003477 auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003478 return RValue::getComplex(Res.first, Res.second);
3479 }
3480 case TEK_Aggregate:
3481 break;
3482 }
3483 llvm_unreachable("Must be a scalar or complex.");
3484}
3485
3486static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst,
3487 bool IsPostfixUpdate, const Expr *V,
3488 const Expr *X, const Expr *E,
3489 const Expr *UE, bool IsXLHSInRHSPart,
3490 SourceLocation Loc) {
3491 assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue");
3492 assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue");
3493 RValue NewVVal;
3494 LValue VLValue = CGF.EmitLValue(V);
3495 LValue XLValue = CGF.EmitLValue(X);
3496 RValue ExprRValue = CGF.EmitAnyExpr(E);
JF Bastien92f4ef12016-04-06 17:26:42 +00003497 auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
3498 : llvm::AtomicOrdering::Monotonic;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003499 QualType NewVValType;
3500 if (UE) {
3501 // 'x' is updated with some additional value.
3502 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
3503 "Update expr in 'atomic capture' must be a binary operator.");
3504 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
3505 // Update expressions are allowed to have the following forms:
3506 // x binop= expr; -> xrval + expr;
3507 // x++, ++x -> xrval + 1;
3508 // x--, --x -> xrval - 1;
3509 // x = x binop expr; -> xrval binop expr
3510 // x = expr Op x; - > expr binop xrval;
3511 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
3512 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
3513 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
3514 NewVValType = XRValExpr->getType();
3515 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
3516 auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr,
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00003517 IsPostfixUpdate](RValue XRValue) -> RValue {
Alexey Bataev5e018f92015-04-23 06:35:10 +00003518 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
3519 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
3520 RValue Res = CGF.EmitAnyExpr(UE);
3521 NewVVal = IsPostfixUpdate ? XRValue : Res;
3522 return Res;
3523 };
3524 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
3525 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
3526 if (Res.first) {
3527 // 'atomicrmw' instruction was generated.
3528 if (IsPostfixUpdate) {
3529 // Use old value from 'atomicrmw'.
3530 NewVVal = Res.second;
3531 } else {
3532 // 'atomicrmw' does not provide new value, so evaluate it using old
3533 // value of 'x'.
3534 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
3535 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second);
3536 NewVVal = CGF.EmitAnyExpr(UE);
3537 }
3538 }
3539 } else {
3540 // 'x' is simply rewritten with some 'expr'.
3541 NewVValType = X->getType().getNonReferenceType();
3542 ExprRValue = convertToType(CGF, ExprRValue, E->getType(),
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003543 X->getType().getNonReferenceType(), Loc);
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00003544 auto &&Gen = [&NewVVal, ExprRValue](RValue XRValue) -> RValue {
Alexey Bataev5e018f92015-04-23 06:35:10 +00003545 NewVVal = XRValue;
3546 return ExprRValue;
3547 };
3548 // Try to perform atomicrmw xchg, otherwise simple exchange.
3549 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
3550 XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO,
3551 Loc, Gen);
3552 if (Res.first) {
3553 // 'atomicrmw' instruction was generated.
3554 NewVVal = IsPostfixUpdate ? Res.second : ExprRValue;
3555 }
3556 }
3557 // Emit post-update store to 'v' of old/new 'x' value.
Alexey Bataev8524d152016-01-21 12:35:58 +00003558 CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc);
Alexey Bataevb4505a72015-03-30 05:20:59 +00003559 // OpenMP, 2.12.6, atomic Construct
3560 // Any atomic construct with a seq_cst clause forces the atomically
3561 // performed operation to include an implicit flush operation without a
3562 // list.
3563 if (IsSeqCst)
3564 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
3565}
3566
Alexey Bataevb57056f2015-01-22 06:17:56 +00003567static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind,
Alexey Bataev5e018f92015-04-23 06:35:10 +00003568 bool IsSeqCst, bool IsPostfixUpdate,
3569 const Expr *X, const Expr *V, const Expr *E,
3570 const Expr *UE, bool IsXLHSInRHSPart,
3571 SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00003572 switch (Kind) {
3573 case OMPC_read:
3574 EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc);
3575 break;
3576 case OMPC_write:
Alexey Bataevb8329262015-02-27 06:33:30 +00003577 EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc);
3578 break;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003579 case OMPC_unknown:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003580 case OMPC_update:
Alexey Bataevb4505a72015-03-30 05:20:59 +00003581 EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc);
3582 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00003583 case OMPC_capture:
Alexey Bataev5e018f92015-04-23 06:35:10 +00003584 EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE,
3585 IsXLHSInRHSPart, Loc);
3586 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00003587 case OMPC_if:
3588 case OMPC_final:
3589 case OMPC_num_threads:
3590 case OMPC_private:
3591 case OMPC_firstprivate:
3592 case OMPC_lastprivate:
3593 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003594 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00003595 case OMPC_in_reduction:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003596 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00003597 case OMPC_simdlen:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003598 case OMPC_collapse:
3599 case OMPC_default:
3600 case OMPC_seq_cst:
3601 case OMPC_shared:
3602 case OMPC_linear:
3603 case OMPC_aligned:
3604 case OMPC_copyin:
3605 case OMPC_copyprivate:
3606 case OMPC_flush:
3607 case OMPC_proc_bind:
3608 case OMPC_schedule:
3609 case OMPC_ordered:
3610 case OMPC_nowait:
3611 case OMPC_untied:
3612 case OMPC_threadprivate:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00003613 case OMPC_depend:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003614 case OMPC_mergeable:
Michael Wonge710d542015-08-07 16:16:36 +00003615 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00003616 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003617 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00003618 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00003619 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00003620 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00003621 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00003622 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00003623 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00003624 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00003625 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00003626 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00003627 case OMPC_defaultmap:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003628 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00003629 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00003630 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00003631 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00003632 case OMPC_is_device_ptr:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003633 llvm_unreachable("Clause is not allowed in 'omp atomic'.");
3634 }
3635}
3636
3637void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003638 bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>();
Alexey Bataevb57056f2015-01-22 06:17:56 +00003639 OpenMPClauseKind Kind = OMPC_unknown;
3640 for (auto *C : S.clauses()) {
3641 // Find first clause (skip seq_cst clause, if it is first).
3642 if (C->getClauseKind() != OMPC_seq_cst) {
3643 Kind = C->getClauseKind();
3644 break;
3645 }
3646 }
Alexey Bataev10fec572015-03-11 04:48:56 +00003647
3648 const auto *CS =
3649 S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003650 if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) {
Alexey Bataev10fec572015-03-11 04:48:56 +00003651 enterFullExpression(EWC);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003652 }
3653 // Processing for statements under 'atomic capture'.
3654 if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) {
3655 for (const auto *C : Compound->body()) {
3656 if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) {
3657 enterFullExpression(EWC);
3658 }
3659 }
3660 }
Alexey Bataev10fec572015-03-11 04:48:56 +00003661
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003662 auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF,
3663 PrePostActionTy &) {
Alexey Bataev33c56402015-12-14 09:26:19 +00003664 CGF.EmitStopPoint(CS);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003665 EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(),
3666 S.getV(), S.getExpr(), S.getUpdateExpr(),
3667 S.isXLHSInRHSPart(), S.getLocStart());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00003668 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00003669 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003670 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen);
Alexey Bataev0162e452014-07-22 10:10:35 +00003671}
3672
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003673static void emitCommonOMPTargetDirective(CodeGenFunction &CGF,
3674 const OMPExecutableDirective &S,
3675 const RegionCodeGenTy &CodeGen) {
3676 assert(isOpenMPTargetExecutionDirective(S.getDirectiveKind()));
3677 CodeGenModule &CGM = CGF.CGM;
Samuel Antaobed3c462015-10-02 16:14:20 +00003678 const CapturedStmt &CS = *cast<CapturedStmt>(S.getAssociatedStmt());
3679
Samuel Antaoee8fb302016-01-06 13:42:12 +00003680 llvm::Function *Fn = nullptr;
3681 llvm::Constant *FnID = nullptr;
Samuel Antaobed3c462015-10-02 16:14:20 +00003682
Samuel Antaobed3c462015-10-02 16:14:20 +00003683 const Expr *IfCond = nullptr;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003684 // Check for the at most one if clause associated with the target region.
3685 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
3686 if (C->getNameModifier() == OMPD_unknown ||
3687 C->getNameModifier() == OMPD_target) {
3688 IfCond = C->getCondition();
3689 break;
3690 }
Samuel Antaobed3c462015-10-02 16:14:20 +00003691 }
3692
3693 // Check if we have any device clause associated with the directive.
3694 const Expr *Device = nullptr;
3695 if (auto *C = S.getSingleClause<OMPDeviceClause>()) {
3696 Device = C->getDevice();
3697 }
3698
Samuel Antaoee8fb302016-01-06 13:42:12 +00003699 // Check if we have an if clause whose conditional always evaluates to false
3700 // or if we do not have any targets specified. If so the target region is not
3701 // an offload entry point.
3702 bool IsOffloadEntry = true;
3703 if (IfCond) {
3704 bool Val;
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003705 if (CGF.ConstantFoldsToSimpleInteger(IfCond, Val) && !Val)
Samuel Antaoee8fb302016-01-06 13:42:12 +00003706 IsOffloadEntry = false;
3707 }
3708 if (CGM.getLangOpts().OMPTargetTriples.empty())
3709 IsOffloadEntry = false;
3710
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003711 assert(CGF.CurFuncDecl && "No parent declaration for target region!");
Samuel Antaoee8fb302016-01-06 13:42:12 +00003712 StringRef ParentName;
3713 // In case we have Ctors/Dtors we use the complete type variant to produce
3714 // the mangling of the device outlined kernel.
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003715 if (auto *D = dyn_cast<CXXConstructorDecl>(CGF.CurFuncDecl))
Samuel Antaoee8fb302016-01-06 13:42:12 +00003716 ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete));
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003717 else if (auto *D = dyn_cast<CXXDestructorDecl>(CGF.CurFuncDecl))
Samuel Antaoee8fb302016-01-06 13:42:12 +00003718 ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete));
3719 else
3720 ParentName =
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003721 CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CGF.CurFuncDecl)));
Samuel Antaoee8fb302016-01-06 13:42:12 +00003722
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003723 // Emit target region as a standalone region.
3724 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(S, ParentName, Fn, FnID,
3725 IsOffloadEntry, CodeGen);
3726 OMPLexicalScope Scope(CGF, S);
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003727 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
3728 CGF.GenerateOpenMPCapturedVars(CS, CapturedVars);
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003729 CGM.getOpenMPRuntime().emitTargetCall(CGF, S, Fn, FnID, IfCond, Device,
Samuel Antaobed3c462015-10-02 16:14:20 +00003730 CapturedVars);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003731}
3732
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003733static void emitTargetRegion(CodeGenFunction &CGF, const OMPTargetDirective &S,
3734 PrePostActionTy &Action) {
3735 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
3736 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
3737 CGF.EmitOMPPrivateClause(S, PrivateScope);
3738 (void)PrivateScope.Privatize();
3739
3740 Action.Enter(CGF);
3741 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3742}
3743
3744void CodeGenFunction::EmitOMPTargetDeviceFunction(CodeGenModule &CGM,
3745 StringRef ParentName,
3746 const OMPTargetDirective &S) {
3747 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3748 emitTargetRegion(CGF, S, Action);
3749 };
3750 llvm::Function *Fn;
3751 llvm::Constant *Addr;
3752 // Emit target region as a standalone region.
3753 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
3754 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
3755 assert(Fn && Addr && "Target device function emission failed.");
3756}
3757
3758void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) {
3759 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3760 emitTargetRegion(CGF, S, Action);
3761 };
3762 emitCommonOMPTargetDirective(*this, S, CodeGen);
3763}
3764
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003765static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF,
3766 const OMPExecutableDirective &S,
3767 OpenMPDirectiveKind InnermostKind,
3768 const RegionCodeGenTy &CodeGen) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003769 const CapturedStmt *CS = S.getCapturedStmt(OMPD_teams);
3770 auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitTeamsOutlinedFunction(
3771 S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
Samuel Antaob68e2db2016-03-03 16:20:23 +00003772
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003773 const OMPNumTeamsClause *NT = S.getSingleClause<OMPNumTeamsClause>();
3774 const OMPThreadLimitClause *TL = S.getSingleClause<OMPThreadLimitClause>();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003775 if (NT || TL) {
Carlo Bertollic6872252016-04-04 15:55:02 +00003776 Expr *NumTeams = (NT) ? NT->getNumTeams() : nullptr;
3777 Expr *ThreadLimit = (TL) ? TL->getThreadLimit() : nullptr;
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003778
Carlo Bertollic6872252016-04-04 15:55:02 +00003779 CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit,
3780 S.getLocStart());
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003781 }
3782
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003783 OMPTeamsScope Scope(CGF, S);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003784 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
3785 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003786 CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getLocStart(), OutlinedFn,
3787 CapturedVars);
3788}
3789
3790void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) {
Kelvin Li51336dd2016-12-15 17:55:32 +00003791 // Emit teams region as a standalone region.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003792 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003793 OMPPrivateScope PrivateScope(CGF);
Carlo Bertolli6ad7b5a2016-03-03 22:09:40 +00003794 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
3795 CGF.EmitOMPPrivateClause(S, PrivateScope);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003796 CGF.EmitOMPReductionClauseInit(S, PrivateScope);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003797 (void)PrivateScope.Privatize();
3798 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003799 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003800 };
3801 emitCommonOMPTeamsDirective(*this, S, OMPD_teams, CodeGen);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003802 emitPostUpdateForReductionClause(
3803 *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev13314bf2014-10-09 04:18:56 +00003804}
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003805
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003806static void emitTargetTeamsRegion(CodeGenFunction &CGF, PrePostActionTy &Action,
3807 const OMPTargetTeamsDirective &S) {
3808 auto *CS = S.getCapturedStmt(OMPD_teams);
3809 Action.Enter(CGF);
3810 auto &&CodeGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) {
3811 // TODO: Add support for clauses.
3812 CGF.EmitStmt(CS->getCapturedStmt());
3813 };
3814 emitCommonOMPTeamsDirective(CGF, S, OMPD_teams, CodeGen);
3815}
3816
3817void CodeGenFunction::EmitOMPTargetTeamsDeviceFunction(
3818 CodeGenModule &CGM, StringRef ParentName,
3819 const OMPTargetTeamsDirective &S) {
3820 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3821 emitTargetTeamsRegion(CGF, Action, S);
3822 };
3823 llvm::Function *Fn;
3824 llvm::Constant *Addr;
3825 // Emit target region as a standalone region.
3826 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
3827 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
3828 assert(Fn && Addr && "Target device function emission failed.");
3829}
3830
3831void CodeGenFunction::EmitOMPTargetTeamsDirective(
3832 const OMPTargetTeamsDirective &S) {
3833 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3834 emitTargetTeamsRegion(CGF, Action, S);
3835 };
3836 emitCommonOMPTargetDirective(*this, S, CodeGen);
3837}
3838
Carlo Bertolliba1487b2017-10-04 14:12:09 +00003839void CodeGenFunction::EmitOMPTeamsDistributeDirective(
3840 const OMPTeamsDistributeDirective &S) {
3841
3842 auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
3843 CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc());
3844 };
3845
3846 // Emit teams region as a standalone region.
3847 auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF,
3848 PrePostActionTy &) {
3849 OMPPrivateScope PrivateScope(CGF);
3850 CGF.EmitOMPReductionClauseInit(S, PrivateScope);
3851 (void)PrivateScope.Privatize();
3852 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute,
3853 CodeGenDistribute);
3854 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams);
3855 };
3856 emitCommonOMPTeamsDirective(*this, S, OMPD_teams, CodeGen);
3857 emitPostUpdateForReductionClause(*this, S,
3858 [](CodeGenFunction &) { return nullptr; });
3859}
3860
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003861void CodeGenFunction::EmitOMPCancellationPointDirective(
3862 const OMPCancellationPointDirective &S) {
Alexey Bataev0f34da12015-07-02 04:17:07 +00003863 CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getLocStart(),
3864 S.getCancelRegion());
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003865}
3866
Alexey Bataev80909872015-07-02 11:25:17 +00003867void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) {
Alexey Bataev87933c72015-09-18 08:07:34 +00003868 const Expr *IfCond = nullptr;
3869 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
3870 if (C->getNameModifier() == OMPD_unknown ||
3871 C->getNameModifier() == OMPD_cancel) {
3872 IfCond = C->getCondition();
3873 break;
3874 }
3875 }
3876 CGM.getOpenMPRuntime().emitCancelCall(*this, S.getLocStart(), IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00003877 S.getCancelRegion());
Alexey Bataev80909872015-07-02 11:25:17 +00003878}
3879
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003880CodeGenFunction::JumpDest
3881CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) {
Alexey Bataev957d8562016-11-17 15:12:05 +00003882 if (Kind == OMPD_parallel || Kind == OMPD_task ||
3883 Kind == OMPD_target_parallel)
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003884 return ReturnBlock;
Alexey Bataev25e5b442015-09-15 12:52:43 +00003885 assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections ||
Alexey Bataev957d8562016-11-17 15:12:05 +00003886 Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for ||
3887 Kind == OMPD_distribute_parallel_for ||
3888 Kind == OMPD_target_parallel_for);
3889 return OMPCancelStack.getExitBlock();
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003890}
Michael Wong65f367f2015-07-21 13:44:28 +00003891
Samuel Antaocc10b852016-07-28 14:23:26 +00003892void CodeGenFunction::EmitOMPUseDevicePtrClause(
3893 const OMPClause &NC, OMPPrivateScope &PrivateScope,
3894 const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap) {
3895 const auto &C = cast<OMPUseDevicePtrClause>(NC);
3896 auto OrigVarIt = C.varlist_begin();
3897 auto InitIt = C.inits().begin();
3898 for (auto PvtVarIt : C.private_copies()) {
3899 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*OrigVarIt)->getDecl());
3900 auto *InitVD = cast<VarDecl>(cast<DeclRefExpr>(*InitIt)->getDecl());
3901 auto *PvtVD = cast<VarDecl>(cast<DeclRefExpr>(PvtVarIt)->getDecl());
3902
3903 // In order to identify the right initializer we need to match the
3904 // declaration used by the mapping logic. In some cases we may get
3905 // OMPCapturedExprDecl that refers to the original declaration.
3906 const ValueDecl *MatchingVD = OrigVD;
3907 if (auto *OED = dyn_cast<OMPCapturedExprDecl>(MatchingVD)) {
3908 // OMPCapturedExprDecl are used to privative fields of the current
3909 // structure.
3910 auto *ME = cast<MemberExpr>(OED->getInit());
3911 assert(isa<CXXThisExpr>(ME->getBase()) &&
3912 "Base should be the current struct!");
3913 MatchingVD = ME->getMemberDecl();
3914 }
3915
3916 // If we don't have information about the current list item, move on to
3917 // the next one.
3918 auto InitAddrIt = CaptureDeviceAddrMap.find(MatchingVD);
3919 if (InitAddrIt == CaptureDeviceAddrMap.end())
3920 continue;
3921
3922 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
3923 // Initialize the temporary initialization variable with the address we
3924 // get from the runtime library. We have to cast the source address
3925 // because it is always a void *. References are materialized in the
3926 // privatization scope, so the initialization here disregards the fact
3927 // the original variable is a reference.
3928 QualType AddrQTy =
3929 getContext().getPointerType(OrigVD->getType().getNonReferenceType());
3930 llvm::Type *AddrTy = ConvertTypeForMem(AddrQTy);
3931 Address InitAddr = Builder.CreateBitCast(InitAddrIt->second, AddrTy);
3932 setAddrOfLocalVar(InitVD, InitAddr);
3933
3934 // Emit private declaration, it will be initialized by the value we
3935 // declaration we just added to the local declarations map.
3936 EmitDecl(*PvtVD);
3937
3938 // The initialization variables reached its purpose in the emission
3939 // ofthe previous declaration, so we don't need it anymore.
3940 LocalDeclMap.erase(InitVD);
3941
3942 // Return the address of the private variable.
3943 return GetAddrOfLocalVar(PvtVD);
3944 });
3945 assert(IsRegistered && "firstprivate var already registered as private");
3946 // Silence the warning about unused variable.
3947 (void)IsRegistered;
3948
3949 ++OrigVarIt;
3950 ++InitIt;
3951 }
3952}
3953
Michael Wong65f367f2015-07-21 13:44:28 +00003954// Generate the instructions for '#pragma omp target data' directive.
3955void CodeGenFunction::EmitOMPTargetDataDirective(
3956 const OMPTargetDataDirective &S) {
Samuel Antaocc10b852016-07-28 14:23:26 +00003957 CGOpenMPRuntime::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true);
3958
3959 // Create a pre/post action to signal the privatization of the device pointer.
3960 // This action can be replaced by the OpenMP runtime code generation to
3961 // deactivate privatization.
3962 bool PrivatizeDevicePointers = false;
3963 class DevicePointerPrivActionTy : public PrePostActionTy {
3964 bool &PrivatizeDevicePointers;
3965
3966 public:
3967 explicit DevicePointerPrivActionTy(bool &PrivatizeDevicePointers)
3968 : PrePostActionTy(), PrivatizeDevicePointers(PrivatizeDevicePointers) {}
3969 void Enter(CodeGenFunction &CGF) override {
3970 PrivatizeDevicePointers = true;
3971 }
Samuel Antaodf158d52016-04-27 22:58:19 +00003972 };
Samuel Antaocc10b852016-07-28 14:23:26 +00003973 DevicePointerPrivActionTy PrivAction(PrivatizeDevicePointers);
3974
3975 auto &&CodeGen = [&S, &Info, &PrivatizeDevicePointers](
3976 CodeGenFunction &CGF, PrePostActionTy &Action) {
3977 auto &&InnermostCodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
3978 CGF.EmitStmt(
3979 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3980 };
3981
3982 // Codegen that selects wheather to generate the privatization code or not.
3983 auto &&PrivCodeGen = [&S, &Info, &PrivatizeDevicePointers,
3984 &InnermostCodeGen](CodeGenFunction &CGF,
3985 PrePostActionTy &Action) {
3986 RegionCodeGenTy RCG(InnermostCodeGen);
3987 PrivatizeDevicePointers = false;
3988
3989 // Call the pre-action to change the status of PrivatizeDevicePointers if
3990 // needed.
3991 Action.Enter(CGF);
3992
3993 if (PrivatizeDevicePointers) {
3994 OMPPrivateScope PrivateScope(CGF);
3995 // Emit all instances of the use_device_ptr clause.
3996 for (const auto *C : S.getClausesOfKind<OMPUseDevicePtrClause>())
3997 CGF.EmitOMPUseDevicePtrClause(*C, PrivateScope,
3998 Info.CaptureDeviceAddrMap);
3999 (void)PrivateScope.Privatize();
4000 RCG(CGF);
4001 } else
4002 RCG(CGF);
4003 };
4004
4005 // Forward the provided action to the privatization codegen.
4006 RegionCodeGenTy PrivRCG(PrivCodeGen);
4007 PrivRCG.setAction(Action);
4008
4009 // Notwithstanding the body of the region is emitted as inlined directive,
4010 // we don't use an inline scope as changes in the references inside the
4011 // region are expected to be visible outside, so we do not privative them.
4012 OMPLexicalScope Scope(CGF, S);
4013 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_target_data,
4014 PrivRCG);
4015 };
4016
4017 RegionCodeGenTy RCG(CodeGen);
Samuel Antaodf158d52016-04-27 22:58:19 +00004018
4019 // If we don't have target devices, don't bother emitting the data mapping
4020 // code.
4021 if (CGM.getLangOpts().OMPTargetTriples.empty()) {
Samuel Antaocc10b852016-07-28 14:23:26 +00004022 RCG(*this);
Samuel Antaodf158d52016-04-27 22:58:19 +00004023 return;
4024 }
4025
4026 // Check if we have any if clause associated with the directive.
4027 const Expr *IfCond = nullptr;
4028 if (auto *C = S.getSingleClause<OMPIfClause>())
4029 IfCond = C->getCondition();
4030
4031 // Check if we have any device clause associated with the directive.
4032 const Expr *Device = nullptr;
4033 if (auto *C = S.getSingleClause<OMPDeviceClause>())
4034 Device = C->getDevice();
4035
Samuel Antaocc10b852016-07-28 14:23:26 +00004036 // Set the action to signal privatization of device pointers.
4037 RCG.setAction(PrivAction);
4038
4039 // Emit region code.
4040 CGM.getOpenMPRuntime().emitTargetDataCalls(*this, S, IfCond, Device, RCG,
4041 Info);
Michael Wong65f367f2015-07-21 13:44:28 +00004042}
Alexey Bataev49f6e782015-12-01 04:18:41 +00004043
Samuel Antaodf67fc42016-01-19 19:15:56 +00004044void CodeGenFunction::EmitOMPTargetEnterDataDirective(
4045 const OMPTargetEnterDataDirective &S) {
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00004046 // If we don't have target devices, don't bother emitting the data mapping
4047 // code.
4048 if (CGM.getLangOpts().OMPTargetTriples.empty())
4049 return;
4050
4051 // Check if we have any if clause associated with the directive.
4052 const Expr *IfCond = nullptr;
4053 if (auto *C = S.getSingleClause<OMPIfClause>())
4054 IfCond = C->getCondition();
4055
4056 // Check if we have any device clause associated with the directive.
4057 const Expr *Device = nullptr;
4058 if (auto *C = S.getSingleClause<OMPDeviceClause>())
4059 Device = C->getDevice();
4060
Samuel Antao8d2d7302016-05-26 18:30:22 +00004061 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
Samuel Antaodf67fc42016-01-19 19:15:56 +00004062}
4063
Samuel Antao72590762016-01-19 20:04:50 +00004064void CodeGenFunction::EmitOMPTargetExitDataDirective(
4065 const OMPTargetExitDataDirective &S) {
Samuel Antao8dd66282016-04-27 23:14:30 +00004066 // If we don't have target devices, don't bother emitting the data mapping
4067 // code.
4068 if (CGM.getLangOpts().OMPTargetTriples.empty())
4069 return;
4070
4071 // Check if we have any if clause associated with the directive.
4072 const Expr *IfCond = nullptr;
4073 if (auto *C = S.getSingleClause<OMPIfClause>())
4074 IfCond = C->getCondition();
4075
4076 // Check if we have any device clause associated with the directive.
4077 const Expr *Device = nullptr;
4078 if (auto *C = S.getSingleClause<OMPDeviceClause>())
4079 Device = C->getDevice();
4080
Samuel Antao8d2d7302016-05-26 18:30:22 +00004081 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
Samuel Antao72590762016-01-19 20:04:50 +00004082}
4083
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004084static void emitTargetParallelRegion(CodeGenFunction &CGF,
4085 const OMPTargetParallelDirective &S,
4086 PrePostActionTy &Action) {
4087 // Get the captured statement associated with the 'parallel' region.
4088 auto *CS = S.getCapturedStmt(OMPD_parallel);
4089 Action.Enter(CGF);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00004090 auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &) {
4091 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
4092 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
4093 CGF.EmitOMPPrivateClause(S, PrivateScope);
4094 CGF.EmitOMPReductionClauseInit(S, PrivateScope);
4095 (void)PrivateScope.Privatize();
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004096 // TODO: Add support for clauses.
4097 CGF.EmitStmt(CS->getCapturedStmt());
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00004098 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004099 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00004100 emitCommonOMPParallelDirective(CGF, S, OMPD_parallel, CodeGen,
4101 emitEmptyBoundParameters);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00004102 emitPostUpdateForReductionClause(
4103 CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004104}
4105
4106void CodeGenFunction::EmitOMPTargetParallelDeviceFunction(
4107 CodeGenModule &CGM, StringRef ParentName,
4108 const OMPTargetParallelDirective &S) {
4109 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
4110 emitTargetParallelRegion(CGF, S, Action);
4111 };
4112 llvm::Function *Fn;
4113 llvm::Constant *Addr;
4114 // Emit target region as a standalone region.
4115 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
4116 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
4117 assert(Fn && Addr && "Target device function emission failed.");
4118}
4119
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00004120void CodeGenFunction::EmitOMPTargetParallelDirective(
4121 const OMPTargetParallelDirective &S) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004122 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
4123 emitTargetParallelRegion(CGF, S, Action);
4124 };
4125 emitCommonOMPTargetDirective(*this, S, CodeGen);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00004126}
4127
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00004128void CodeGenFunction::EmitOMPTargetParallelForDirective(
4129 const OMPTargetParallelForDirective &S) {
Alexey Bataev2a0c4f52017-10-10 14:14:43 +00004130 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
4131 CGM.getOpenMPRuntime().emitInlinedDirective(
4132 *this, OMPD_target_parallel_for,
4133 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
4134 OMPLoopScope PreInitScope(CGF, S);
4135 CGF.EmitStmt(
4136 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
4137 });
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00004138}
4139
Alexey Bataev7292c292016-04-25 12:22:29 +00004140/// Emit a helper variable and return corresponding lvalue.
4141static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper,
4142 const ImplicitParamDecl *PVD,
4143 CodeGenFunction::OMPPrivateScope &Privates) {
4144 auto *VDecl = cast<VarDecl>(Helper->getDecl());
4145 Privates.addPrivate(
4146 VDecl, [&CGF, PVD]() -> Address { return CGF.GetAddrOfLocalVar(PVD); });
4147}
4148
4149void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) {
4150 assert(isOpenMPTaskLoopDirective(S.getDirectiveKind()));
4151 // Emit outlined function for task construct.
4152 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
4153 auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
4154 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
4155 const Expr *IfCond = nullptr;
4156 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
4157 if (C->getNameModifier() == OMPD_unknown ||
4158 C->getNameModifier() == OMPD_taskloop) {
4159 IfCond = C->getCondition();
4160 break;
4161 }
4162 }
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004163
4164 OMPTaskDataTy Data;
4165 // Check if taskloop must be emitted without taskgroup.
4166 Data.Nogroup = S.getSingleClause<OMPNogroupClause>();
Alexey Bataev7292c292016-04-25 12:22:29 +00004167 // TODO: Check if we should emit tied or untied task.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004168 Data.Tied = true;
4169 // Set scheduling for taskloop
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00004170 if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) {
4171 // grainsize clause
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004172 Data.Schedule.setInt(/*IntVal=*/false);
4173 Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize()));
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00004174 } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) {
4175 // num_tasks clause
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004176 Data.Schedule.setInt(/*IntVal=*/true);
4177 Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks()));
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00004178 }
Alexey Bataev7292c292016-04-25 12:22:29 +00004179
4180 auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) {
4181 // if (PreCond) {
4182 // for (IV in 0..LastIteration) BODY;
4183 // <Final counter/linear vars updates>;
4184 // }
4185 //
4186
4187 // Emit: if (PreCond) - begin.
4188 // If the condition constant folds and can be elided, avoid emitting the
4189 // whole loop.
4190 bool CondConstant;
4191 llvm::BasicBlock *ContBlock = nullptr;
4192 OMPLoopScope PreInitScope(CGF, S);
4193 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
4194 if (!CondConstant)
4195 return;
4196 } else {
4197 auto *ThenBlock = CGF.createBasicBlock("taskloop.if.then");
4198 ContBlock = CGF.createBasicBlock("taskloop.if.end");
4199 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
4200 CGF.getProfileCount(&S));
4201 CGF.EmitBlock(ThenBlock);
4202 CGF.incrementProfileCounter(&S);
4203 }
4204
Alexey Bataev1e73ef32016-04-28 12:14:51 +00004205 if (isOpenMPSimdDirective(S.getDirectiveKind()))
4206 CGF.EmitOMPSimdInit(S);
4207
Alexey Bataev7292c292016-04-25 12:22:29 +00004208 OMPPrivateScope LoopScope(CGF);
4209 // Emit helper vars inits.
4210 enum { LowerBound = 5, UpperBound, Stride, LastIter };
4211 auto *I = CS->getCapturedDecl()->param_begin();
4212 auto *LBP = std::next(I, LowerBound);
4213 auto *UBP = std::next(I, UpperBound);
4214 auto *STP = std::next(I, Stride);
4215 auto *LIP = std::next(I, LastIter);
4216 mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP,
4217 LoopScope);
4218 mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP,
4219 LoopScope);
4220 mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope);
4221 mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP,
4222 LoopScope);
4223 CGF.EmitOMPPrivateLoopCounters(S, LoopScope);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004224 bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev7292c292016-04-25 12:22:29 +00004225 (void)LoopScope.Privatize();
4226 // Emit the loop iteration variable.
4227 const Expr *IVExpr = S.getIterationVariable();
4228 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
4229 CGF.EmitVarDecl(*IVDecl);
4230 CGF.EmitIgnoredExpr(S.getInit());
4231
4232 // Emit the iterations count variable.
4233 // If it is not a variable, Sema decided to calculate iterations count on
4234 // each iteration (e.g., it is foldable into a constant).
4235 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
4236 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
4237 // Emit calculation of the iterations count.
4238 CGF.EmitIgnoredExpr(S.getCalcLastIteration());
4239 }
4240
4241 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
4242 S.getInc(),
4243 [&S](CodeGenFunction &CGF) {
4244 CGF.EmitOMPLoopBody(S, JumpDest());
4245 CGF.EmitStopPoint(&S);
4246 },
4247 [](CodeGenFunction &) {});
4248 // Emit: if (PreCond) - end.
4249 if (ContBlock) {
4250 CGF.EmitBranch(ContBlock);
4251 CGF.EmitBlock(ContBlock, true);
4252 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004253 // Emit final copy of the lastprivate variables if IsLastIter != 0.
4254 if (HasLastprivateClause) {
4255 CGF.EmitOMPLastprivateClauseFinal(
4256 S, isOpenMPSimdDirective(S.getDirectiveKind()),
4257 CGF.Builder.CreateIsNotNull(CGF.EmitLoadOfScalar(
4258 CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false,
4259 (*LIP)->getType(), S.getLocStart())));
4260 }
Alexey Bataev7292c292016-04-25 12:22:29 +00004261 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004262 auto &&TaskGen = [&S, SharedsTy, CapturedStruct,
4263 IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn,
4264 const OMPTaskDataTy &Data) {
Alexey Bataev7292c292016-04-25 12:22:29 +00004265 auto &&CodeGen = [&](CodeGenFunction &CGF, PrePostActionTy &) {
4266 OMPLoopScope PreInitScope(CGF, S);
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004267 CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getLocStart(), S,
4268 OutlinedFn, SharedsTy,
4269 CapturedStruct, IfCond, Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00004270 };
4271 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop,
4272 CodeGen);
4273 };
Alexey Bataev33446032017-07-12 18:09:32 +00004274 if (Data.Nogroup)
4275 EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data);
4276 else {
4277 CGM.getOpenMPRuntime().emitTaskgroupRegion(
4278 *this,
4279 [&S, &BodyGen, &TaskGen, &Data](CodeGenFunction &CGF,
4280 PrePostActionTy &Action) {
4281 Action.Enter(CGF);
4282 CGF.EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data);
4283 },
4284 S.getLocStart());
4285 }
Alexey Bataev7292c292016-04-25 12:22:29 +00004286}
4287
Alexey Bataev49f6e782015-12-01 04:18:41 +00004288void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) {
Alexey Bataev7292c292016-04-25 12:22:29 +00004289 EmitOMPTaskLoopBasedDirective(S);
Alexey Bataev49f6e782015-12-01 04:18:41 +00004290}
4291
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004292void CodeGenFunction::EmitOMPTaskLoopSimdDirective(
4293 const OMPTaskLoopSimdDirective &S) {
Alexey Bataev1e73ef32016-04-28 12:14:51 +00004294 EmitOMPTaskLoopBasedDirective(S);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004295}
Samuel Antao686c70c2016-05-26 17:30:50 +00004296
4297// Generate the instructions for '#pragma omp target update' directive.
4298void CodeGenFunction::EmitOMPTargetUpdateDirective(
4299 const OMPTargetUpdateDirective &S) {
Samuel Antao8d2d7302016-05-26 18:30:22 +00004300 // If we don't have target devices, don't bother emitting the data mapping
4301 // code.
4302 if (CGM.getLangOpts().OMPTargetTriples.empty())
4303 return;
4304
4305 // Check if we have any if clause associated with the directive.
4306 const Expr *IfCond = nullptr;
4307 if (auto *C = S.getSingleClause<OMPIfClause>())
4308 IfCond = C->getCondition();
4309
4310 // Check if we have any device clause associated with the directive.
4311 const Expr *Device = nullptr;
4312 if (auto *C = S.getSingleClause<OMPDeviceClause>())
4313 Device = C->getDevice();
4314
4315 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
Samuel Antao686c70c2016-05-26 17:30:50 +00004316}