blob: 4dcdaa29379444426ff9d9324b9426274a22bf1c [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 Bataev1189bd02016-01-26 12:20:39 +0000141llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) {
142 auto &C = getContext();
143 llvm::Value *Size = nullptr;
144 auto SizeInChars = C.getTypeSizeInChars(Ty);
145 if (SizeInChars.isZero()) {
146 // getTypeSizeInChars() returns 0 for a VLA.
147 while (auto *VAT = C.getAsVariableArrayType(Ty)) {
148 llvm::Value *ArraySize;
149 std::tie(ArraySize, Ty) = getVLASize(VAT);
150 Size = Size ? Builder.CreateNUWMul(Size, ArraySize) : ArraySize;
151 }
152 SizeInChars = C.getTypeSizeInChars(Ty);
153 if (SizeInChars.isZero())
154 return llvm::ConstantInt::get(SizeTy, /*V=*/0);
155 Size = Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars));
156 } else
157 Size = CGM.getSize(SizeInChars);
158 return Size;
159}
160
Alexey Bataev2377fe92015-09-10 08:12:02 +0000161void CodeGenFunction::GenerateOpenMPCapturedVars(
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000162 const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) {
Alexey Bataev2377fe92015-09-10 08:12:02 +0000163 const RecordDecl *RD = S.getCapturedRecordDecl();
164 auto CurField = RD->field_begin();
165 auto CurCap = S.captures().begin();
166 for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(),
167 E = S.capture_init_end();
168 I != E; ++I, ++CurField, ++CurCap) {
169 if (CurField->hasCapturedVLAType()) {
170 auto VAT = CurField->getCapturedVLAType();
Samuel Antaobed3c462015-10-02 16:14:20 +0000171 auto *Val = VLASizeMap[VAT->getSizeExpr()];
Samuel Antaobed3c462015-10-02 16:14:20 +0000172 CapturedVars.push_back(Val);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000173 } else if (CurCap->capturesThis())
174 CapturedVars.push_back(CXXThisValue);
Samuel Antao6d004262016-06-16 18:39:34 +0000175 else if (CurCap->capturesVariableByCopy()) {
176 llvm::Value *CV =
177 EmitLoadOfLValue(EmitLValue(*I), SourceLocation()).getScalarVal();
178
179 // If the field is not a pointer, we need to save the actual value
180 // and load it as a void pointer.
181 if (!CurField->getType()->isAnyPointerType()) {
182 auto &Ctx = getContext();
183 auto DstAddr = CreateMemTemp(
184 Ctx.getUIntPtrType(),
185 Twine(CurCap->getCapturedVar()->getName()) + ".casted");
186 LValue DstLV = MakeAddrLValue(DstAddr, Ctx.getUIntPtrType());
187
188 auto *SrcAddrVal = EmitScalarConversion(
189 DstAddr.getPointer(), Ctx.getPointerType(Ctx.getUIntPtrType()),
190 Ctx.getPointerType(CurField->getType()), SourceLocation());
191 LValue SrcLV =
192 MakeNaturalAlignAddrLValue(SrcAddrVal, CurField->getType());
193
194 // Store the value using the source type pointer.
195 EmitStoreThroughLValue(RValue::get(CV), SrcLV);
196
197 // Load the value using the destination type pointer.
198 CV = EmitLoadOfLValue(DstLV, SourceLocation()).getScalarVal();
199 }
200 CapturedVars.push_back(CV);
201 } else {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000202 assert(CurCap->capturesVariable() && "Expected capture by reference.");
Alexey Bataev2377fe92015-09-10 08:12:02 +0000203 CapturedVars.push_back(EmitLValue(*I).getAddress().getPointer());
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000204 }
Alexey Bataev2377fe92015-09-10 08:12:02 +0000205 }
206}
207
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000208static Address castValueFromUintptr(CodeGenFunction &CGF, QualType DstType,
209 StringRef Name, LValue AddrLV,
210 bool isReferenceType = false) {
211 ASTContext &Ctx = CGF.getContext();
212
213 auto *CastedPtr = CGF.EmitScalarConversion(
214 AddrLV.getAddress().getPointer(), Ctx.getUIntPtrType(),
215 Ctx.getPointerType(DstType), SourceLocation());
216 auto TmpAddr =
217 CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType))
218 .getAddress();
219
220 // If we are dealing with references we need to return the address of the
221 // reference instead of the reference of the value.
222 if (isReferenceType) {
223 QualType RefType = Ctx.getLValueReferenceType(DstType);
224 auto *RefVal = TmpAddr.getPointer();
225 TmpAddr = CGF.CreateMemTemp(RefType, Twine(Name) + ".ref");
226 auto TmpLVal = CGF.MakeAddrLValue(TmpAddr, RefType);
Akira Hatanaka642f7992016-10-18 19:05:41 +0000227 CGF.EmitStoreThroughLValue(RValue::get(RefVal), TmpLVal, /*isInit*/ true);
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000228 }
229
230 return TmpAddr;
231}
232
Alexey Bataevf7ce1662017-04-10 19:16:45 +0000233static QualType getCanonicalParamType(ASTContext &C, QualType T) {
234 if (T->isLValueReferenceType()) {
235 return C.getLValueReferenceType(
236 getCanonicalParamType(C, T.getNonReferenceType()),
237 /*SpelledAsLValue=*/false);
238 }
239 if (T->isPointerType())
240 return C.getPointerType(getCanonicalParamType(C, T->getPointeeType()));
241 return C.getCanonicalParamType(T);
242}
243
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000244namespace {
245 /// Contains required data for proper outlined function codegen.
246 struct FunctionOptions {
247 /// Captured statement for which the function is generated.
248 const CapturedStmt *S = nullptr;
249 /// true if cast to/from UIntPtr is required for variables captured by
250 /// value.
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000251 const bool UIntPtrCastRequired = true;
Alexey Bataeve754b182017-08-09 19:38:53 +0000252 /// true if only casted arguments must be registered as local args or VLA
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000253 /// sizes.
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000254 const bool RegisterCastedArgsOnly = false;
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000255 /// Name of the generated function.
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000256 const StringRef FunctionName;
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000257 explicit FunctionOptions(const CapturedStmt *S, bool UIntPtrCastRequired,
258 bool RegisterCastedArgsOnly,
Alexey Bataev4aa19052017-08-08 16:45:36 +0000259 StringRef FunctionName)
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000260 : S(S), UIntPtrCastRequired(UIntPtrCastRequired),
261 RegisterCastedArgsOnly(UIntPtrCastRequired && RegisterCastedArgsOnly),
Alexey Bataev4aa19052017-08-08 16:45:36 +0000262 FunctionName(FunctionName) {}
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000263 };
264}
265
Alexey Bataeve754b182017-08-09 19:38:53 +0000266static llvm::Function *emitOutlinedFunctionPrologue(
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000267 CodeGenFunction &CGF, FunctionArgList &Args,
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000268 llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>>
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000269 &LocalAddrs,
270 llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>>
271 &VLASizes,
272 llvm::Value *&CXXThisValue, const FunctionOptions &FO) {
273 const CapturedDecl *CD = FO.S->getCapturedDecl();
274 const RecordDecl *RD = FO.S->getCapturedRecordDecl();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000275 assert(CD->hasBody() && "missing CapturedDecl body");
276
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000277 CXXThisValue = nullptr;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000278 // Build the argument list.
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000279 CodeGenModule &CGM = CGF.CGM;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000280 ASTContext &Ctx = CGM.getContext();
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000281 FunctionArgList TargetArgs;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000282 Args.append(CD->param_begin(),
283 std::next(CD->param_begin(), CD->getContextParamPosition()));
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000284 TargetArgs.append(
285 CD->param_begin(),
286 std::next(CD->param_begin(), CD->getContextParamPosition()));
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000287 auto I = FO.S->captures().begin();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000288 for (auto *FD : RD->fields()) {
289 QualType ArgType = FD->getType();
290 IdentifierInfo *II = nullptr;
291 VarDecl *CapVar = nullptr;
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000292
293 // If this is a capture by copy and the type is not a pointer, the outlined
294 // function argument type should be uintptr and the value properly casted to
295 // uintptr. This is necessary given that the runtime library is only able to
296 // deal with pointers. We can pass in the same way the VLA type sizes to the
297 // outlined function.
Samuel Antao6d004262016-06-16 18:39:34 +0000298 if ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) ||
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000299 I->capturesVariableArrayType()) {
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000300 if (FO.UIntPtrCastRequired)
301 ArgType = Ctx.getUIntPtrType();
302 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000303
304 if (I->capturesVariable() || I->capturesVariableByCopy()) {
Alexey Bataev2377fe92015-09-10 08:12:02 +0000305 CapVar = I->getCapturedVar();
306 II = CapVar->getIdentifier();
307 } else if (I->capturesThis())
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000308 II = &Ctx.Idents.get("this");
Alexey Bataev2377fe92015-09-10 08:12:02 +0000309 else {
310 assert(I->capturesVariableArrayType());
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000311 II = &Ctx.Idents.get("vla");
Alexey Bataev2377fe92015-09-10 08:12:02 +0000312 }
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000313 if (ArgType->isVariablyModifiedType())
314 ArgType = getCanonicalParamType(Ctx, ArgType.getNonReferenceType());
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000315 auto *Arg =
316 ImplicitParamDecl::Create(Ctx, /*DC=*/nullptr, FD->getLocation(), II,
317 ArgType, ImplicitParamDecl::Other);
318 Args.emplace_back(Arg);
319 // Do not cast arguments if we emit function with non-original types.
320 TargetArgs.emplace_back(
321 FO.UIntPtrCastRequired
322 ? Arg
323 : CGM.getOpenMPRuntime().translateParameter(FD, Arg));
Alexey Bataev2377fe92015-09-10 08:12:02 +0000324 ++I;
325 }
326 Args.append(
327 std::next(CD->param_begin(), CD->getContextParamPosition() + 1),
328 CD->param_end());
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000329 TargetArgs.append(
330 std::next(CD->param_begin(), CD->getContextParamPosition() + 1),
331 CD->param_end());
Alexey Bataev2377fe92015-09-10 08:12:02 +0000332
333 // Create the function declaration.
334 FunctionType::ExtInfo ExtInfo;
335 const CGFunctionInfo &FuncInfo =
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000336 CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, TargetArgs);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000337 llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo);
338
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000339 llvm::Function *F =
340 llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage,
341 FO.FunctionName, &CGM.getModule());
Alexey Bataev2377fe92015-09-10 08:12:02 +0000342 CGM.SetInternalFunctionAttributes(CD, F, FuncInfo);
343 if (CD->isNothrow())
Alexey Bataev2c7eee52017-08-04 19:10:54 +0000344 F->setDoesNotThrow();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000345
346 // Generate the function.
Alexey Bataev6e01dc12017-08-14 16:03:47 +0000347 CGF.StartFunction(CD, Ctx.VoidTy, F, FuncInfo, TargetArgs,
348 FO.S->getLocStart(), CD->getBody()->getLocStart());
Alexey Bataev2377fe92015-09-10 08:12:02 +0000349 unsigned Cnt = CD->getContextParamPosition();
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000350 I = FO.S->captures().begin();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000351 for (auto *FD : RD->fields()) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000352 // Do not map arguments if we emit function with non-original types.
353 Address LocalAddr(Address::invalid());
354 if (!FO.UIntPtrCastRequired && Args[Cnt] != TargetArgs[Cnt]) {
355 LocalAddr = CGM.getOpenMPRuntime().getParameterAddress(CGF, Args[Cnt],
356 TargetArgs[Cnt]);
357 } else {
358 LocalAddr = CGF.GetAddrOfLocalVar(Args[Cnt]);
359 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000360 // If we are capturing a pointer by copy we don't need to do anything, just
361 // use the value that we get from the arguments.
362 if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) {
Samuel Antao403ffd42016-07-27 22:49:49 +0000363 const VarDecl *CurVD = I->getCapturedVar();
Samuel Antao403ffd42016-07-27 22:49:49 +0000364 // If the variable is a reference we need to materialize it here.
365 if (CurVD->getType()->isReferenceType()) {
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000366 Address RefAddr = CGF.CreateMemTemp(
367 CurVD->getType(), CGM.getPointerAlign(), ".materialized_ref");
368 CGF.EmitStoreOfScalar(LocalAddr.getPointer(), RefAddr,
369 /*Volatile=*/false, CurVD->getType());
Samuel Antao403ffd42016-07-27 22:49:49 +0000370 LocalAddr = RefAddr;
371 }
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000372 if (!FO.RegisterCastedArgsOnly)
373 LocalAddrs.insert({Args[Cnt], {CurVD, LocalAddr}});
Richard Trieucc3949d2016-02-18 22:34:54 +0000374 ++Cnt;
375 ++I;
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000376 continue;
377 }
378
Krzysztof Parzyszek8f248232017-05-18 17:07:11 +0000379 LValueBaseInfo BaseInfo(AlignmentSource::Decl, false);
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000380 LValue ArgLVal =
381 CGF.MakeAddrLValue(LocalAddr, Args[Cnt]->getType(), BaseInfo);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000382 if (FD->hasCapturedVLAType()) {
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000383 if (FO.UIntPtrCastRequired) {
384 ArgLVal = CGF.MakeAddrLValue(castValueFromUintptr(CGF, FD->getType(),
385 Args[Cnt]->getName(),
386 ArgLVal),
387 FD->getType(), BaseInfo);
388 }
Alexey Bataev2377fe92015-09-10 08:12:02 +0000389 auto *ExprArg =
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000390 CGF.EmitLoadOfLValue(ArgLVal, SourceLocation()).getScalarVal();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000391 auto VAT = FD->getCapturedVLAType();
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000392 VLASizes.insert({Args[Cnt], {VAT->getSizeExpr(), ExprArg}});
Alexey Bataev2377fe92015-09-10 08:12:02 +0000393 } else if (I->capturesVariable()) {
394 auto *Var = I->getCapturedVar();
395 QualType VarTy = Var->getType();
396 Address ArgAddr = ArgLVal.getAddress();
397 if (!VarTy->isReferenceType()) {
Alexey Bataev2f5ed342016-10-13 09:52:46 +0000398 if (ArgLVal.getType()->isLValueReferenceType()) {
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000399 ArgAddr = CGF.EmitLoadOfReference(
Alexey Bataev2f5ed342016-10-13 09:52:46 +0000400 ArgAddr, ArgLVal.getType()->castAs<ReferenceType>());
Alexey Bataevac5eabb2016-11-07 11:16:04 +0000401 } else if (!VarTy->isVariablyModifiedType() || !VarTy->isPointerType()) {
Alexey Bataev2f5ed342016-10-13 09:52:46 +0000402 assert(ArgLVal.getType()->isPointerType());
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000403 ArgAddr = CGF.EmitLoadOfPointer(
Alexey Bataev2f5ed342016-10-13 09:52:46 +0000404 ArgAddr, ArgLVal.getType()->castAs<PointerType>());
405 }
Alexey Bataev2377fe92015-09-10 08:12:02 +0000406 }
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000407 if (!FO.RegisterCastedArgsOnly) {
408 LocalAddrs.insert(
409 {Args[Cnt],
410 {Var, Address(ArgAddr.getPointer(), Ctx.getDeclAlign(Var))}});
411 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000412 } else if (I->capturesVariableByCopy()) {
413 assert(!FD->getType()->isAnyPointerType() &&
414 "Not expecting a captured pointer.");
415 auto *Var = I->getCapturedVar();
416 QualType VarTy = Var->getType();
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000417 LocalAddrs.insert(
418 {Args[Cnt],
419 {Var,
420 FO.UIntPtrCastRequired
421 ? castValueFromUintptr(CGF, FD->getType(), Args[Cnt]->getName(),
422 ArgLVal, VarTy->isReferenceType())
423 : ArgLVal.getAddress()}});
Alexey Bataev2377fe92015-09-10 08:12:02 +0000424 } else {
425 // If 'this' is captured, load it into CXXThisValue.
426 assert(I->capturesThis());
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000427 CXXThisValue = CGF.EmitLoadOfLValue(ArgLVal, Args[Cnt]->getLocation())
428 .getScalarVal();
429 LocalAddrs.insert({Args[Cnt], {nullptr, ArgLVal.getAddress()}});
Alexey Bataev2377fe92015-09-10 08:12:02 +0000430 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000431 ++Cnt;
432 ++I;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000433 }
434
Alexey Bataeve754b182017-08-09 19:38:53 +0000435 return F;
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000436}
437
438llvm::Function *
439CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S) {
440 assert(
441 CapturedStmtInfo &&
442 "CapturedStmtInfo should be set when generating the captured function");
443 const CapturedDecl *CD = S.getCapturedDecl();
444 // Build the argument list.
445 bool NeedWrapperFunction =
446 getDebugInfo() &&
447 CGM.getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo;
448 FunctionArgList Args;
Alexey Bataev3b8d5582017-08-08 18:04:06 +0000449 llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> LocalAddrs;
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000450 llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> VLASizes;
Alexey Bataeve754b182017-08-09 19:38:53 +0000451 SmallString<256> Buffer;
452 llvm::raw_svector_ostream Out(Buffer);
453 Out << CapturedStmtInfo->getHelperName();
454 if (NeedWrapperFunction)
455 Out << "_debug__";
Alexey Bataev4aa19052017-08-08 16:45:36 +0000456 FunctionOptions FO(&S, !NeedWrapperFunction, /*RegisterCastedArgsOnly=*/false,
Alexey Bataeve754b182017-08-09 19:38:53 +0000457 Out.str());
458 llvm::Function *F = emitOutlinedFunctionPrologue(*this, Args, LocalAddrs,
459 VLASizes, CXXThisValue, FO);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000460 for (const auto &LocalAddrPair : LocalAddrs) {
461 if (LocalAddrPair.second.first) {
462 setAddrOfLocalVar(LocalAddrPair.second.first,
463 LocalAddrPair.second.second);
464 }
465 }
466 for (const auto &VLASizePair : VLASizes)
467 VLASizeMap[VLASizePair.second.first] = VLASizePair.second.second;
Serge Pavlov3a561452015-12-06 14:32:39 +0000468 PGO.assignRegionCounters(GlobalDecl(CD), F);
Alexey Bataev2377fe92015-09-10 08:12:02 +0000469 CapturedStmtInfo->EmitBody(*this, CD->getBody());
470 FinishFunction(CD->getBodyRBrace());
Alexey Bataeve754b182017-08-09 19:38:53 +0000471 if (!NeedWrapperFunction)
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000472 return F;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000473
Alexey Bataevefd884d2017-08-04 21:26:25 +0000474 FunctionOptions WrapperFO(&S, /*UIntPtrCastRequired=*/true,
Alexey Bataeve754b182017-08-09 19:38:53 +0000475 /*RegisterCastedArgsOnly=*/true,
476 CapturedStmtInfo->getHelperName());
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000477 CodeGenFunction WrapperCGF(CGM, /*suppressNewContext=*/true);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000478 Args.clear();
479 LocalAddrs.clear();
480 VLASizes.clear();
481 llvm::Function *WrapperF =
482 emitOutlinedFunctionPrologue(WrapperCGF, Args, LocalAddrs, VLASizes,
Alexey Bataeve754b182017-08-09 19:38:53 +0000483 WrapperCGF.CXXThisValue, WrapperFO);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000484 LValueBaseInfo BaseInfo(AlignmentSource::Decl, false);
485 llvm::SmallVector<llvm::Value *, 4> CallArgs;
486 for (const auto *Arg : Args) {
487 llvm::Value *CallArg;
488 auto I = LocalAddrs.find(Arg);
489 if (I != LocalAddrs.end()) {
490 LValue LV =
491 WrapperCGF.MakeAddrLValue(I->second.second, Arg->getType(), BaseInfo);
492 CallArg = WrapperCGF.EmitLoadOfScalar(LV, SourceLocation());
493 } else {
494 auto EI = VLASizes.find(Arg);
495 if (EI != VLASizes.end())
496 CallArg = EI->second.second;
497 else {
498 LValue LV = WrapperCGF.MakeAddrLValue(WrapperCGF.GetAddrOfLocalVar(Arg),
499 Arg->getType(), BaseInfo);
500 CallArg = WrapperCGF.EmitLoadOfScalar(LV, SourceLocation());
501 }
502 }
503 CallArgs.emplace_back(CallArg);
504 }
Alexey Bataev3c595a62017-08-14 15:01:03 +0000505 CGM.getOpenMPRuntime().emitOutlinedFunctionCall(WrapperCGF, S.getLocStart(),
506 F, CallArgs);
Alexey Bataev1fdfdf72017-06-29 16:43:05 +0000507 WrapperCGF.FinishFunction();
508 return WrapperF;
Alexey Bataev2377fe92015-09-10 08:12:02 +0000509}
510
Alexey Bataev9959db52014-05-06 10:08:46 +0000511//===----------------------------------------------------------------------===//
512// OpenMP Directive Emission
513//===----------------------------------------------------------------------===//
Alexey Bataev420d45b2015-04-14 05:11:24 +0000514void CodeGenFunction::EmitOMPAggregateAssign(
John McCall7f416cc2015-09-08 08:05:57 +0000515 Address DestAddr, Address SrcAddr, QualType OriginalType,
516 const llvm::function_ref<void(Address, Address)> &CopyGen) {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000517 // Perform element-by-element initialization.
518 QualType ElementTy;
John McCall7f416cc2015-09-08 08:05:57 +0000519
520 // Drill down to the base element type on both arrays.
Alexey Bataev420d45b2015-04-14 05:11:24 +0000521 auto ArrayTy = OriginalType->getAsArrayTypeUnsafe();
John McCall7f416cc2015-09-08 08:05:57 +0000522 auto NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr);
523 SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
524
525 auto SrcBegin = SrcAddr.getPointer();
526 auto DestBegin = DestAddr.getPointer();
Alexey Bataev420d45b2015-04-14 05:11:24 +0000527 // Cast from pointer to array type to pointer to single element.
Alexey Bataev420d45b2015-04-14 05:11:24 +0000528 auto DestEnd = Builder.CreateGEP(DestBegin, NumElements);
529 // The basic structure here is a while-do loop.
530 auto BodyBB = createBasicBlock("omp.arraycpy.body");
531 auto DoneBB = createBasicBlock("omp.arraycpy.done");
532 auto IsEmpty =
533 Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty");
534 Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000535
Alexey Bataev420d45b2015-04-14 05:11:24 +0000536 // Enter the loop body, making that address the current address.
537 auto EntryBB = Builder.GetInsertBlock();
538 EmitBlock(BodyBB);
John McCall7f416cc2015-09-08 08:05:57 +0000539
540 CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy);
541
542 llvm::PHINode *SrcElementPHI =
543 Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast");
544 SrcElementPHI->addIncoming(SrcBegin, EntryBB);
545 Address SrcElementCurrent =
546 Address(SrcElementPHI,
547 SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize));
548
549 llvm::PHINode *DestElementPHI =
550 Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast");
551 DestElementPHI->addIncoming(DestBegin, EntryBB);
552 Address DestElementCurrent =
553 Address(DestElementPHI,
554 DestAddr.getAlignment().alignmentOfArrayElement(ElementSize));
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000555
Alexey Bataev420d45b2015-04-14 05:11:24 +0000556 // Emit copy.
557 CopyGen(DestElementCurrent, SrcElementCurrent);
558
559 // Shift the address forward by one element.
560 auto DestElementNext = Builder.CreateConstGEP1_32(
John McCall7f416cc2015-09-08 08:05:57 +0000561 DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
Alexey Bataev420d45b2015-04-14 05:11:24 +0000562 auto SrcElementNext = Builder.CreateConstGEP1_32(
John McCall7f416cc2015-09-08 08:05:57 +0000563 SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element");
Alexey Bataev420d45b2015-04-14 05:11:24 +0000564 // Check whether we've reached the end.
565 auto Done =
566 Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
567 Builder.CreateCondBr(Done, DoneBB, BodyBB);
John McCall7f416cc2015-09-08 08:05:57 +0000568 DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock());
569 SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock());
Alexey Bataev420d45b2015-04-14 05:11:24 +0000570
571 // Done.
572 EmitBlock(DoneBB, /*IsFinished=*/true);
573}
574
John McCall7f416cc2015-09-08 08:05:57 +0000575void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr,
576 Address SrcAddr, const VarDecl *DestVD,
Alexey Bataev420d45b2015-04-14 05:11:24 +0000577 const VarDecl *SrcVD, const Expr *Copy) {
578 if (OriginalType->isArrayType()) {
579 auto *BO = dyn_cast<BinaryOperator>(Copy);
580 if (BO && BO->getOpcode() == BO_Assign) {
581 // Perform simple memcpy for simple copying.
John McCall7f416cc2015-09-08 08:05:57 +0000582 EmitAggregateAssign(DestAddr, SrcAddr, OriginalType);
Alexey Bataev420d45b2015-04-14 05:11:24 +0000583 } else {
584 // For arrays with complex element types perform element by element
585 // copying.
John McCall7f416cc2015-09-08 08:05:57 +0000586 EmitOMPAggregateAssign(
Alexey Bataev420d45b2015-04-14 05:11:24 +0000587 DestAddr, SrcAddr, OriginalType,
John McCall7f416cc2015-09-08 08:05:57 +0000588 [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000589 // Working with the single array element, so have to remap
590 // destination and source variables to corresponding array
591 // elements.
John McCall7f416cc2015-09-08 08:05:57 +0000592 CodeGenFunction::OMPPrivateScope Remap(*this);
593 Remap.addPrivate(DestVD, [DestElement]() -> Address {
Alexey Bataev420d45b2015-04-14 05:11:24 +0000594 return DestElement;
595 });
596 Remap.addPrivate(
John McCall7f416cc2015-09-08 08:05:57 +0000597 SrcVD, [SrcElement]() -> Address { return SrcElement; });
Alexey Bataev420d45b2015-04-14 05:11:24 +0000598 (void)Remap.Privatize();
John McCall7f416cc2015-09-08 08:05:57 +0000599 EmitIgnoredExpr(Copy);
Alexey Bataev420d45b2015-04-14 05:11:24 +0000600 });
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000601 }
Alexey Bataev420d45b2015-04-14 05:11:24 +0000602 } else {
603 // Remap pseudo source variable to private copy.
John McCall7f416cc2015-09-08 08:05:57 +0000604 CodeGenFunction::OMPPrivateScope Remap(*this);
605 Remap.addPrivate(SrcVD, [SrcAddr]() -> Address { return SrcAddr; });
606 Remap.addPrivate(DestVD, [DestAddr]() -> Address { return DestAddr; });
Alexey Bataev420d45b2015-04-14 05:11:24 +0000607 (void)Remap.Privatize();
608 // Emit copying of the whole variable.
John McCall7f416cc2015-09-08 08:05:57 +0000609 EmitIgnoredExpr(Copy);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000610 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000611}
612
Alexey Bataev69c62a92015-04-15 04:52:20 +0000613bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
614 OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000615 if (!HaveInsertPoint())
616 return false;
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000617 bool FirstprivateIsLastprivate = false;
618 llvm::DenseSet<const VarDecl *> Lastprivates;
619 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
620 for (const auto *D : C->varlists())
621 Lastprivates.insert(
622 cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl());
623 }
Alexey Bataev69c62a92015-04-15 04:52:20 +0000624 llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate;
Alexey Bataev9afe5752016-05-24 07:40:12 +0000625 CGCapturedStmtInfo CapturesInfo(cast<CapturedStmt>(*D.getAssociatedStmt()));
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000626 for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) {
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000627 auto IRef = C->varlist_begin();
628 auto InitsRef = C->inits().begin();
629 for (auto IInit : C->private_copies()) {
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000630 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000631 bool ThisFirstprivateIsLastprivate =
632 Lastprivates.count(OrigVD->getCanonicalDecl()) > 0;
Alexey Bataev9afe5752016-05-24 07:40:12 +0000633 auto *CapFD = CapturesInfo.lookup(OrigVD);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000634 auto *FD = CapturedStmtInfo->lookup(OrigVD);
Alexey Bataev9afe5752016-05-24 07:40:12 +0000635 if (!ThisFirstprivateIsLastprivate && FD && (FD == CapFD) &&
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000636 !FD->getType()->isReferenceType()) {
637 EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl());
638 ++IRef;
639 ++InitsRef;
640 continue;
641 }
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000642 FirstprivateIsLastprivate =
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000643 FirstprivateIsLastprivate || ThisFirstprivateIsLastprivate;
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000644 if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000645 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
646 auto *VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl());
647 bool IsRegistered;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000648 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
649 /*RefersToEnclosingVariableOrCapture=*/FD != nullptr,
650 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
John McCall7f416cc2015-09-08 08:05:57 +0000651 Address OriginalAddr = EmitLValue(&DRE).getAddress();
Alexey Bataevfeddd642016-04-22 09:05:03 +0000652 QualType Type = VD->getType();
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000653 if (Type->isArrayType()) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000654 // Emit VarDecl with copy init for arrays.
655 // Get the address of the original variable captured in current
656 // captured region.
John McCall7f416cc2015-09-08 08:05:57 +0000657 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000658 auto Emission = EmitAutoVarAlloca(*VD);
659 auto *Init = VD->getInit();
660 if (!isa<CXXConstructExpr>(Init) || isTrivialInitializer(Init)) {
661 // Perform simple memcpy.
662 EmitAggregateAssign(Emission.getAllocatedAddress(), OriginalAddr,
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000663 Type);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000664 } else {
665 EmitOMPAggregateAssign(
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000666 Emission.getAllocatedAddress(), OriginalAddr, Type,
John McCall7f416cc2015-09-08 08:05:57 +0000667 [this, VDInit, Init](Address DestElement,
668 Address SrcElement) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000669 // Clean up any temporaries needed by the initialization.
670 RunCleanupsScope InitScope(*this);
671 // Emit initialization for single element.
John McCall7f416cc2015-09-08 08:05:57 +0000672 setAddrOfLocalVar(VDInit, SrcElement);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000673 EmitAnyExprToMem(Init, DestElement,
674 Init->getType().getQualifiers(),
675 /*IsInitializer*/ false);
676 LocalDeclMap.erase(VDInit);
677 });
678 }
679 EmitAutoVarCleanups(Emission);
680 return Emission.getAllocatedAddress();
681 });
682 } else {
John McCall7f416cc2015-09-08 08:05:57 +0000683 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000684 // Emit private VarDecl with copy init.
685 // Remap temp VDInit variable to the address of the original
686 // variable
687 // (for proper handling of captured global variables).
John McCall7f416cc2015-09-08 08:05:57 +0000688 setAddrOfLocalVar(VDInit, OriginalAddr);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000689 EmitDecl(*VD);
690 LocalDeclMap.erase(VDInit);
691 return GetAddrOfLocalVar(VD);
692 });
693 }
694 assert(IsRegistered &&
695 "firstprivate var already registered as private");
696 // Silence the warning about unused variable.
697 (void)IsRegistered;
698 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000699 ++IRef;
700 ++InitsRef;
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000701 }
702 }
Alexey Bataevcd8b6a22016-02-15 08:07:17 +0000703 return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000704}
705
Alexey Bataev03b340a2014-10-21 03:16:40 +0000706void CodeGenFunction::EmitOMPPrivateClause(
707 const OMPExecutableDirective &D,
708 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000709 if (!HaveInsertPoint())
710 return;
Alexey Bataev50a64582015-04-22 12:24:45 +0000711 llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000712 for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) {
Alexey Bataev03b340a2014-10-21 03:16:40 +0000713 auto IRef = C->varlist_begin();
714 for (auto IInit : C->private_copies()) {
715 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev50a64582015-04-22 12:24:45 +0000716 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
717 auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
718 bool IsRegistered =
John McCall7f416cc2015-09-08 08:05:57 +0000719 PrivateScope.addPrivate(OrigVD, [&]() -> Address {
Alexey Bataev50a64582015-04-22 12:24:45 +0000720 // Emit private VarDecl with copy init.
721 EmitDecl(*VD);
722 return GetAddrOfLocalVar(VD);
723 });
724 assert(IsRegistered && "private var already registered as private");
725 // Silence the warning about unused variable.
726 (void)IsRegistered;
727 }
Alexey Bataev03b340a2014-10-21 03:16:40 +0000728 ++IRef;
729 }
730 }
731}
732
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000733bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000734 if (!HaveInsertPoint())
735 return false;
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000736 // threadprivate_var1 = master_threadprivate_var1;
737 // operator=(threadprivate_var2, master_threadprivate_var2);
738 // ...
739 // __kmpc_barrier(&loc, global_tid);
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000740 llvm::DenseSet<const VarDecl *> CopiedVars;
741 llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000742 for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) {
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000743 auto IRef = C->varlist_begin();
744 auto ISrcRef = C->source_exprs().begin();
745 auto IDestRef = C->destination_exprs().begin();
746 for (auto *AssignOp : C->assignment_ops()) {
747 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000748 QualType Type = VD->getType();
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000749 if (CopiedVars.insert(VD->getCanonicalDecl()).second) {
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000750 // Get the address of the master variable. If we are emitting code with
751 // TLS support, the address is passed from the master as field in the
752 // captured declaration.
John McCall7f416cc2015-09-08 08:05:57 +0000753 Address MasterAddr = Address::invalid();
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000754 if (getLangOpts().OpenMPUseTLS &&
755 getContext().getTargetInfo().isTLSSupported()) {
756 assert(CapturedStmtInfo->lookup(VD) &&
757 "Copyin threadprivates should have been captured!");
758 DeclRefExpr DRE(const_cast<VarDecl *>(VD), true, (*IRef)->getType(),
759 VK_LValue, (*IRef)->getExprLoc());
760 MasterAddr = EmitLValue(&DRE).getAddress();
Alexey Bataev2377fe92015-09-10 08:12:02 +0000761 LocalDeclMap.erase(VD);
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000762 } else {
John McCall7f416cc2015-09-08 08:05:57 +0000763 MasterAddr =
764 Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD)
765 : CGM.GetAddrOfGlobal(VD),
766 getContext().getDeclAlign(VD));
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000767 }
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000768 // Get the address of the threadprivate variable.
John McCall7f416cc2015-09-08 08:05:57 +0000769 Address PrivateAddr = EmitLValue(*IRef).getAddress();
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000770 if (CopiedVars.size() == 1) {
771 // At first check if current thread is a master thread. If it is, no
772 // need to copy data.
773 CopyBegin = createBasicBlock("copyin.not.master");
774 CopyEnd = createBasicBlock("copyin.not.master.end");
775 Builder.CreateCondBr(
776 Builder.CreateICmpNE(
John McCall7f416cc2015-09-08 08:05:57 +0000777 Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy),
778 Builder.CreatePtrToInt(PrivateAddr.getPointer(), CGM.IntPtrTy)),
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000779 CopyBegin, CopyEnd);
780 EmitBlock(CopyBegin);
781 }
782 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
783 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +0000784 EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp);
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000785 }
786 ++IRef;
787 ++ISrcRef;
788 ++IDestRef;
789 }
790 }
791 if (CopyEnd) {
792 // Exit out of copying procedure for non-master thread.
793 EmitBlock(CopyEnd, /*IsFinished=*/true);
794 return true;
795 }
796 return false;
797}
798
Alexey Bataev38e89532015-04-16 04:54:05 +0000799bool CodeGenFunction::EmitOMPLastprivateClauseInit(
800 const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000801 if (!HaveInsertPoint())
802 return false;
Alexey Bataev38e89532015-04-16 04:54:05 +0000803 bool HasAtLeastOneLastprivate = false;
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000804 llvm::DenseSet<const VarDecl *> SIMDLCVs;
805 if (isOpenMPSimdDirective(D.getDirectiveKind())) {
806 auto *LoopDirective = cast<OMPLoopDirective>(&D);
807 for (auto *C : LoopDirective->counters()) {
808 SIMDLCVs.insert(
809 cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl());
810 }
811 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000812 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000813 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
Alexey Bataevd130fd12015-05-13 10:23:02 +0000814 HasAtLeastOneLastprivate = true;
Alexey Bataevf93095a2016-05-05 08:46:22 +0000815 if (isOpenMPTaskLoopDirective(D.getDirectiveKind()))
816 break;
Alexey Bataev38e89532015-04-16 04:54:05 +0000817 auto IRef = C->varlist_begin();
818 auto IDestRef = C->destination_exprs().begin();
819 for (auto *IInit : C->private_copies()) {
820 // Keep the address of the original variable for future update at the end
821 // of the loop.
822 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataevf93095a2016-05-05 08:46:22 +0000823 // Taskloops do not require additional initialization, it is done in
824 // runtime support library.
Alexey Bataev38e89532015-04-16 04:54:05 +0000825 if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) {
826 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
John McCall7f416cc2015-09-08 08:05:57 +0000827 PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() -> Address {
Alexey Bataev38e89532015-04-16 04:54:05 +0000828 DeclRefExpr DRE(
829 const_cast<VarDecl *>(OrigVD),
830 /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup(
831 OrigVD) != nullptr,
832 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
833 return EmitLValue(&DRE).getAddress();
834 });
835 // Check if the variable is also a firstprivate: in this case IInit is
836 // not generated. Initialization of this variable will happen in codegen
837 // for 'firstprivate' clause.
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000838 if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) {
Alexey Bataevd130fd12015-05-13 10:23:02 +0000839 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
Alexey Bataevf93095a2016-05-05 08:46:22 +0000840 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
841 // Emit private VarDecl with copy init.
842 EmitDecl(*VD);
843 return GetAddrOfLocalVar(VD);
844 });
Alexey Bataevd130fd12015-05-13 10:23:02 +0000845 assert(IsRegistered &&
846 "lastprivate var already registered as private");
847 (void)IsRegistered;
848 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000849 }
Richard Trieucc3949d2016-02-18 22:34:54 +0000850 ++IRef;
851 ++IDestRef;
Alexey Bataev38e89532015-04-16 04:54:05 +0000852 }
853 }
854 return HasAtLeastOneLastprivate;
855}
856
857void CodeGenFunction::EmitOMPLastprivateClauseFinal(
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000858 const OMPExecutableDirective &D, bool NoFinals,
859 llvm::Value *IsLastIterCond) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000860 if (!HaveInsertPoint())
861 return;
Alexey Bataev38e89532015-04-16 04:54:05 +0000862 // Emit following code:
863 // if (<IsLastIterCond>) {
864 // orig_var1 = private_orig_var1;
865 // ...
866 // orig_varn = private_orig_varn;
867 // }
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000868 llvm::BasicBlock *ThenBB = nullptr;
869 llvm::BasicBlock *DoneBB = nullptr;
870 if (IsLastIterCond) {
871 ThenBB = createBasicBlock(".omp.lastprivate.then");
872 DoneBB = createBasicBlock(".omp.lastprivate.done");
873 Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB);
874 EmitBlock(ThenBB);
875 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000876 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
877 llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates;
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000878 if (auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) {
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000879 auto IC = LoopDirective->counters().begin();
880 for (auto F : LoopDirective->finals()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000881 auto *D =
882 cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl();
883 if (NoFinals)
884 AlreadyEmittedVars.insert(D);
885 else
886 LoopCountersAndUpdates[D] = F;
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000887 ++IC;
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000888 }
889 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000890 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
891 auto IRef = C->varlist_begin();
892 auto ISrcRef = C->source_exprs().begin();
893 auto IDestRef = C->destination_exprs().begin();
894 for (auto *AssignOp : C->assignment_ops()) {
895 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
896 QualType Type = PrivateVD->getType();
897 auto *CanonicalVD = PrivateVD->getCanonicalDecl();
898 if (AlreadyEmittedVars.insert(CanonicalVD).second) {
899 // If lastprivate variable is a loop control variable for loop-based
900 // directive, update its value before copyin back to original
901 // variable.
Alexey Bataev5dff95c2016-04-22 03:56:56 +0000902 if (auto *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD))
903 EmitIgnoredExpr(FinalExpr);
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000904 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
905 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
906 // Get the address of the original variable.
907 Address OriginalAddr = GetAddrOfLocalVar(DestVD);
908 // Get the address of the private variable.
909 Address PrivateAddr = GetAddrOfLocalVar(PrivateVD);
910 if (auto RefTy = PrivateVD->getType()->getAs<ReferenceType>())
911 PrivateAddr =
John McCall7f416cc2015-09-08 08:05:57 +0000912 Address(Builder.CreateLoad(PrivateAddr),
913 getNaturalTypeAlignment(RefTy->getPointeeType()));
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000914 EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp);
Alexey Bataev38e89532015-04-16 04:54:05 +0000915 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000916 ++IRef;
917 ++ISrcRef;
918 ++IDestRef;
Alexey Bataev38e89532015-04-16 04:54:05 +0000919 }
Alexey Bataev005248a2016-02-25 05:25:57 +0000920 if (auto *PostUpdate = C->getPostUpdateExpr())
921 EmitIgnoredExpr(PostUpdate);
Alexey Bataev38e89532015-04-16 04:54:05 +0000922 }
Alexey Bataev8ffcc942016-02-18 13:48:15 +0000923 if (IsLastIterCond)
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000924 EmitBlock(DoneBB, /*IsFinished=*/true);
Alexey Bataev38e89532015-04-16 04:54:05 +0000925}
926
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000927void CodeGenFunction::EmitOMPReductionClauseInit(
928 const OMPExecutableDirective &D,
929 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataev8ef31412015-12-18 07:58:25 +0000930 if (!HaveInsertPoint())
931 return;
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000932 SmallVector<const Expr *, 4> Shareds;
933 SmallVector<const Expr *, 4> Privates;
934 SmallVector<const Expr *, 4> ReductionOps;
935 SmallVector<const Expr *, 4> LHSs;
936 SmallVector<const Expr *, 4> RHSs;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +0000937 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
Alexey Bataevf24e7b12015-10-08 09:10:53 +0000938 auto IPriv = C->privates().begin();
Alexey Bataeva839ddd2016-03-17 10:19:46 +0000939 auto IRed = C->reduction_ops().begin();
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000940 auto ILHS = C->lhs_exprs().begin();
941 auto IRHS = C->rhs_exprs().begin();
942 for (const auto *Ref : C->varlists()) {
943 Shareds.emplace_back(Ref);
944 Privates.emplace_back(*IPriv);
945 ReductionOps.emplace_back(*IRed);
946 LHSs.emplace_back(*ILHS);
947 RHSs.emplace_back(*IRHS);
948 std::advance(IPriv, 1);
949 std::advance(IRed, 1);
950 std::advance(ILHS, 1);
951 std::advance(IRHS, 1);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000952 }
953 }
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000954 ReductionCodeGen RedCG(Shareds, Privates, ReductionOps);
955 unsigned Count = 0;
956 auto ILHS = LHSs.begin();
957 auto IRHS = RHSs.begin();
958 auto IPriv = Privates.begin();
959 for (const auto *IRef : Shareds) {
960 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl());
961 // Emit private VarDecl with reduction init.
962 RedCG.emitSharedLValue(*this, Count);
963 RedCG.emitAggregateType(*this, Count);
964 auto Emission = EmitAutoVarAlloca(*PrivateVD);
965 RedCG.emitInitialization(*this, Count, Emission.getAllocatedAddress(),
966 RedCG.getSharedLValue(Count),
967 [&Emission](CodeGenFunction &CGF) {
968 CGF.EmitAutoVarInit(Emission);
969 return true;
970 });
971 EmitAutoVarCleanups(Emission);
972 Address BaseAddr = RedCG.adjustPrivateAddress(
973 *this, Count, Emission.getAllocatedAddress());
974 bool IsRegistered = PrivateScope.addPrivate(
975 RedCG.getBaseDecl(Count), [BaseAddr]() -> Address { return BaseAddr; });
976 assert(IsRegistered && "private var already registered as private");
977 // Silence the warning about unused variable.
978 (void)IsRegistered;
979
980 auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
981 auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
Eric Christopher7aba9782017-07-14 01:42:57 +0000982 if (isa<OMPArraySectionExpr>(IRef)) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000983 // Store the address of the original variable associated with the LHS
984 // implicit variable.
985 PrivateScope.addPrivate(LHSVD, [&RedCG, Count]() -> Address {
986 return RedCG.getSharedLValue(Count).getAddress();
987 });
988 PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address {
989 return GetAddrOfLocalVar(PrivateVD);
990 });
Eric Christopher7aba9782017-07-14 01:42:57 +0000991 } else if (isa<ArraySubscriptExpr>(IRef)) {
Alexey Bataev5c40bec2017-07-13 13:36:14 +0000992 // Store the address of the original variable associated with the LHS
993 // implicit variable.
994 PrivateScope.addPrivate(LHSVD, [&RedCG, Count]() -> Address {
995 return RedCG.getSharedLValue(Count).getAddress();
996 });
997 PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address {
998 return Builder.CreateElementBitCast(GetAddrOfLocalVar(PrivateVD),
999 ConvertTypeForMem(RHSVD->getType()),
1000 "rhs.begin");
1001 });
1002 } else {
1003 QualType Type = PrivateVD->getType();
1004 bool IsArray = getContext().getAsArrayType(Type) != nullptr;
1005 Address OriginalAddr = RedCG.getSharedLValue(Count).getAddress();
1006 // Store the address of the original variable associated with the LHS
1007 // implicit variable.
1008 if (IsArray) {
1009 OriginalAddr = Builder.CreateElementBitCast(
1010 OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin");
1011 }
1012 PrivateScope.addPrivate(
1013 LHSVD, [OriginalAddr]() -> Address { return OriginalAddr; });
1014 PrivateScope.addPrivate(
1015 RHSVD, [this, PrivateVD, RHSVD, IsArray]() -> Address {
1016 return IsArray
1017 ? Builder.CreateElementBitCast(
1018 GetAddrOfLocalVar(PrivateVD),
1019 ConvertTypeForMem(RHSVD->getType()), "rhs.begin")
1020 : GetAddrOfLocalVar(PrivateVD);
1021 });
1022 }
1023 ++ILHS;
1024 ++IRHS;
1025 ++IPriv;
1026 ++Count;
1027 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001028}
1029
1030void CodeGenFunction::EmitOMPReductionClauseFinal(
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001031 const OMPExecutableDirective &D, const OpenMPDirectiveKind ReductionKind) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001032 if (!HaveInsertPoint())
1033 return;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001034 llvm::SmallVector<const Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001035 llvm::SmallVector<const Expr *, 8> LHSExprs;
1036 llvm::SmallVector<const Expr *, 8> RHSExprs;
1037 llvm::SmallVector<const Expr *, 8> ReductionOps;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001038 bool HasAtLeastOneReduction = false;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001039 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001040 HasAtLeastOneReduction = true;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001041 Privates.append(C->privates().begin(), C->privates().end());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001042 LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end());
1043 RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end());
1044 ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end());
1045 }
1046 if (HasAtLeastOneReduction) {
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001047 bool WithNowait = D.getSingleClause<OMPNowaitClause>() ||
1048 isOpenMPParallelDirective(D.getDirectiveKind()) ||
1049 D.getDirectiveKind() == OMPD_simd;
1050 bool SimpleReduction = D.getDirectiveKind() == OMPD_simd;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001051 // Emit nowait reduction if nowait clause is present or directive is a
1052 // parallel directive (it always has implicit barrier).
1053 CGM.getOpenMPRuntime().emitReduction(
Alexey Bataevf24e7b12015-10-08 09:10:53 +00001054 *this, D.getLocEnd(), Privates, LHSExprs, RHSExprs, ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001055 {WithNowait, SimpleReduction, ReductionKind});
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001056 }
1057}
1058
Alexey Bataev61205072016-03-02 04:57:40 +00001059static void emitPostUpdateForReductionClause(
1060 CodeGenFunction &CGF, const OMPExecutableDirective &D,
1061 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
1062 if (!CGF.HaveInsertPoint())
1063 return;
1064 llvm::BasicBlock *DoneBB = nullptr;
1065 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
1066 if (auto *PostUpdate = C->getPostUpdateExpr()) {
1067 if (!DoneBB) {
1068 if (auto *Cond = CondGen(CGF)) {
1069 // If the first post-update expression is found, emit conditional
1070 // block if it was requested.
1071 auto *ThenBB = CGF.createBasicBlock(".omp.reduction.pu");
1072 DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done");
1073 CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1074 CGF.EmitBlock(ThenBB);
1075 }
1076 }
1077 CGF.EmitIgnoredExpr(PostUpdate);
1078 }
1079 }
1080 if (DoneBB)
1081 CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
1082}
1083
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001084namespace {
1085/// Codegen lambda for appending distribute lower and upper bounds to outlined
1086/// parallel function. This is necessary for combined constructs such as
1087/// 'distribute parallel for'
1088typedef llvm::function_ref<void(CodeGenFunction &,
1089 const OMPExecutableDirective &,
1090 llvm::SmallVectorImpl<llvm::Value *> &)>
1091 CodeGenBoundParametersTy;
1092} // anonymous namespace
1093
1094static void emitCommonOMPParallelDirective(
1095 CodeGenFunction &CGF, const OMPExecutableDirective &S,
1096 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1097 const CodeGenBoundParametersTy &CodeGenBoundParameters) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00001098 const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel);
1099 auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction(
1100 S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001101 if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) {
Alexey Bataev1d677132015-04-22 13:57:31 +00001102 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
Alexey Bataev1d677132015-04-22 13:57:31 +00001103 auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
1104 /*IgnoreResultAssign*/ true);
1105 CGF.CGM.getOpenMPRuntime().emitNumThreadsClause(
1106 CGF, NumThreads, NumThreadsClause->getLocStart());
1107 }
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001108 if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001109 CodeGenFunction::RunCleanupsScope ProcBindScope(CGF);
Alexey Bataev7f210c62015-06-18 13:40:03 +00001110 CGF.CGM.getOpenMPRuntime().emitProcBindClause(
1111 CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getLocStart());
1112 }
Alexey Bataev1d677132015-04-22 13:57:31 +00001113 const Expr *IfCond = nullptr;
Alexey Bataev7371aa32015-09-03 08:45:56 +00001114 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
1115 if (C->getNameModifier() == OMPD_unknown ||
1116 C->getNameModifier() == OMPD_parallel) {
1117 IfCond = C->getCondition();
1118 break;
1119 }
Alexey Bataev1d677132015-04-22 13:57:31 +00001120 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001121
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00001122 OMPParallelScope Scope(CGF, S);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001123 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001124 // Combining 'distribute' with 'for' requires sharing each 'distribute' chunk
1125 // lower and upper bounds with the pragma 'for' chunking mechanism.
1126 // The following lambda takes care of appending the lower and upper bound
1127 // parameters when necessary
1128 CodeGenBoundParameters(CGF, S, CapturedVars);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001129 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
Alexey Bataev1d677132015-04-22 13:57:31 +00001130 CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn,
Alexey Bataev2377fe92015-09-10 08:12:02 +00001131 CapturedVars, IfCond);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001132}
1133
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001134static void emitEmptyBoundParameters(CodeGenFunction &,
1135 const OMPExecutableDirective &,
1136 llvm::SmallVectorImpl<llvm::Value *> &) {}
1137
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001138void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001139 // Emit parallel region as a standalone region.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001140 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001141 OMPPrivateScope PrivateScope(CGF);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001142 bool Copyins = CGF.EmitOMPCopyinClause(S);
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00001143 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
1144 if (Copyins) {
Alexey Bataev69c62a92015-04-15 04:52:20 +00001145 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00001146 // propagation master's thread values of threadprivate variables to local
1147 // instances of that variables of all other implicit threads.
Alexey Bataev25e5b442015-09-15 12:52:43 +00001148 CGF.CGM.getOpenMPRuntime().emitBarrierCall(
1149 CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
1150 /*ForceSimpleCall=*/true);
Alexey Bataev69c62a92015-04-15 04:52:20 +00001151 }
1152 CGF.EmitOMPPrivateClause(S, PrivateScope);
1153 CGF.EmitOMPReductionClauseInit(S, PrivateScope);
1154 (void)PrivateScope.Privatize();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001155 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001156 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001157 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001158 emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen,
1159 emitEmptyBoundParameters);
Alexey Bataev61205072016-03-02 04:57:40 +00001160 emitPostUpdateForReductionClause(
1161 *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev9959db52014-05-06 10:08:46 +00001162}
Alexander Musman515ad8c2014-05-22 08:54:05 +00001163
Alexey Bataev0f34da12015-07-02 04:17:07 +00001164void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D,
1165 JumpDest LoopExit) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00001166 RunCleanupsScope BodyScope(*this);
1167 // Update counters values on current iteration.
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001168 for (auto I : D.updates()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00001169 EmitIgnoredExpr(I);
1170 }
Alexander Musman3276a272015-03-21 10:12:56 +00001171 // Update the linear variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001172 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001173 for (auto *U : C->updates())
Alexander Musman3276a272015-03-21 10:12:56 +00001174 EmitIgnoredExpr(U);
Alexander Musman3276a272015-03-21 10:12:56 +00001175 }
1176
Alexander Musmana5f070a2014-10-01 06:03:56 +00001177 // On a continue in the body, jump to the end.
Alexander Musmand196ef22014-10-07 08:57:09 +00001178 auto Continue = getJumpDestInCurrentScope("omp.body.continue");
Alexey Bataev0f34da12015-07-02 04:17:07 +00001179 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
Alexander Musmana5f070a2014-10-01 06:03:56 +00001180 // Emit loop body.
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001181 EmitStmt(D.getBody());
Alexander Musmana5f070a2014-10-01 06:03:56 +00001182 // The end (updates/cleanups).
1183 EmitBlock(Continue.getBlock());
1184 BreakContinueStack.pop_back();
Alexander Musmana5f070a2014-10-01 06:03:56 +00001185}
1186
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001187void CodeGenFunction::EmitOMPInnerLoop(
1188 const Stmt &S, bool RequiresCleanup, const Expr *LoopCond,
1189 const Expr *IncExpr,
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001190 const llvm::function_ref<void(CodeGenFunction &)> &BodyGen,
1191 const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) {
Alexander Musmand196ef22014-10-07 08:57:09 +00001192 auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001193
1194 // Start the loop with a block that tests the condition.
Alexander Musmand196ef22014-10-07 08:57:09 +00001195 auto CondBlock = createBasicBlock("omp.inner.for.cond");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001196 EmitBlock(CondBlock);
Amara Emerson652795d2016-11-10 14:44:30 +00001197 const SourceRange &R = S.getSourceRange();
1198 LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()),
1199 SourceLocToDebugLoc(R.getEnd()));
Alexander Musmana5f070a2014-10-01 06:03:56 +00001200
1201 // If there are any cleanups between here and the loop-exit scope,
1202 // create a block to stage a loop exit along.
1203 auto ExitBlock = LoopExit.getBlock();
Alexey Bataev2df54a02015-03-12 08:53:29 +00001204 if (RequiresCleanup)
Alexander Musmand196ef22014-10-07 08:57:09 +00001205 ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001206
Alexander Musmand196ef22014-10-07 08:57:09 +00001207 auto LoopBody = createBasicBlock("omp.inner.for.body");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001208
Alexey Bataev2df54a02015-03-12 08:53:29 +00001209 // Emit condition.
Justin Bogner66242d62015-04-23 23:06:47 +00001210 EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S));
Alexander Musmana5f070a2014-10-01 06:03:56 +00001211 if (ExitBlock != LoopExit.getBlock()) {
1212 EmitBlock(ExitBlock);
1213 EmitBranchThroughCleanup(LoopExit);
1214 }
1215
1216 EmitBlock(LoopBody);
Justin Bogner66242d62015-04-23 23:06:47 +00001217 incrementProfileCounter(&S);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001218
1219 // Create a block for the increment.
Alexander Musmand196ef22014-10-07 08:57:09 +00001220 auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc");
Alexander Musmana5f070a2014-10-01 06:03:56 +00001221 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1222
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001223 BodyGen(*this);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001224
1225 // Emit "IV = IV + 1" and a back-edge to the condition block.
1226 EmitBlock(Continue.getBlock());
Alexey Bataev2df54a02015-03-12 08:53:29 +00001227 EmitIgnoredExpr(IncExpr);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001228 PostIncGen(*this);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001229 BreakContinueStack.pop_back();
1230 EmitBranch(CondBlock);
1231 LoopStack.pop();
1232 // Emit the fall-through block.
1233 EmitBlock(LoopExit.getBlock());
1234}
1235
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00001236bool CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001237 if (!HaveInsertPoint())
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00001238 return false;
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001239 // Emit inits for the linear variables.
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00001240 bool HasLinears = false;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001241 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001242 for (auto *Init : C->inits()) {
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00001243 HasLinears = true;
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001244 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl());
Alexey Bataevef549a82016-03-09 09:49:09 +00001245 if (auto *Ref = dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) {
1246 AutoVarEmission Emission = EmitAutoVarAlloca(*VD);
1247 auto *OrigVD = cast<VarDecl>(Ref->getDecl());
1248 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
1249 CapturedStmtInfo->lookup(OrigVD) != nullptr,
1250 VD->getInit()->getType(), VK_LValue,
1251 VD->getInit()->getExprLoc());
1252 EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(),
1253 VD->getType()),
1254 /*capturedByInit=*/false);
1255 EmitAutoVarCleanups(Emission);
1256 } else
1257 EmitVarDecl(*VD);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001258 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001259 // Emit the linear steps for the linear clauses.
1260 // If a step is not constant, it is pre-calculated before the loop.
1261 if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep()))
1262 if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) {
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001263 EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl()));
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001264 // Emit calculation of the linear step.
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001265 EmitIgnoredExpr(CS);
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001266 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001267 }
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00001268 return HasLinears;
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001269}
1270
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001271void CodeGenFunction::EmitOMPLinearClauseFinal(
1272 const OMPLoopDirective &D,
Alexey Bataevef549a82016-03-09 09:49:09 +00001273 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001274 if (!HaveInsertPoint())
Alexey Bataev8ef31412015-12-18 07:58:25 +00001275 return;
Alexey Bataevef549a82016-03-09 09:49:09 +00001276 llvm::BasicBlock *DoneBB = nullptr;
Alexander Musman3276a272015-03-21 10:12:56 +00001277 // Emit the final values of the linear variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001278 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataev39f915b82015-05-08 10:41:21 +00001279 auto IC = C->varlist_begin();
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001280 for (auto *F : C->finals()) {
Alexey Bataevef549a82016-03-09 09:49:09 +00001281 if (!DoneBB) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001282 if (auto *Cond = CondGen(*this)) {
Alexey Bataevef549a82016-03-09 09:49:09 +00001283 // If the first post-update expression is found, emit conditional
1284 // block if it was requested.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001285 auto *ThenBB = createBasicBlock(".omp.linear.pu");
1286 DoneBB = createBasicBlock(".omp.linear.pu.done");
1287 Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1288 EmitBlock(ThenBB);
Alexey Bataevef549a82016-03-09 09:49:09 +00001289 }
1290 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00001291 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl());
1292 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001293 CapturedStmtInfo->lookup(OrigVD) != nullptr,
Alexey Bataev39f915b82015-05-08 10:41:21 +00001294 (*IC)->getType(), VK_LValue, (*IC)->getExprLoc());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001295 Address OrigAddr = EmitLValue(&DRE).getAddress();
1296 CodeGenFunction::OMPPrivateScope VarScope(*this);
1297 VarScope.addPrivate(OrigVD, [OrigAddr]() -> Address { return OrigAddr; });
Alexey Bataev39f915b82015-05-08 10:41:21 +00001298 (void)VarScope.Privatize();
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001299 EmitIgnoredExpr(F);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001300 ++IC;
Alexander Musman3276a272015-03-21 10:12:56 +00001301 }
Alexey Bataev78849fb2016-03-09 09:49:00 +00001302 if (auto *PostUpdate = C->getPostUpdateExpr())
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001303 EmitIgnoredExpr(PostUpdate);
Alexander Musman3276a272015-03-21 10:12:56 +00001304 }
Alexey Bataevef549a82016-03-09 09:49:09 +00001305 if (DoneBB)
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001306 EmitBlock(DoneBB, /*IsFinished=*/true);
Alexander Musmana5f070a2014-10-01 06:03:56 +00001307}
1308
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001309static void emitAlignedClause(CodeGenFunction &CGF,
1310 const OMPExecutableDirective &D) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001311 if (!CGF.HaveInsertPoint())
1312 return;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001313 for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001314 unsigned ClauseAlignment = 0;
1315 if (auto AlignmentExpr = Clause->getAlignment()) {
1316 auto AlignmentCI =
1317 cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr));
1318 ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue());
Alexander Musman09184fe2014-09-30 05:29:28 +00001319 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001320 for (auto E : Clause->varlists()) {
1321 unsigned Alignment = ClauseAlignment;
1322 if (Alignment == 0) {
1323 // OpenMP [2.8.1, Description]
1324 // If no optional parameter is specified, implementation-defined default
1325 // alignments for SIMD instructions on the target platforms are assumed.
1326 Alignment =
Alexey Bataev00396512015-07-02 03:40:19 +00001327 CGF.getContext()
1328 .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign(
1329 E->getType()->getPointeeType()))
1330 .getQuantity();
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001331 }
1332 assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) &&
1333 "alignment is not power of 2");
1334 if (Alignment != 0) {
1335 llvm::Value *PtrValue = CGF.EmitScalarExpr(E);
1336 CGF.EmitAlignmentAssumption(PtrValue, Alignment);
1337 }
Alexander Musman09184fe2014-09-30 05:29:28 +00001338 }
1339 }
1340}
1341
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001342void CodeGenFunction::EmitOMPPrivateLoopCounters(
1343 const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) {
1344 if (!HaveInsertPoint())
Alexey Bataev8ef31412015-12-18 07:58:25 +00001345 return;
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001346 auto I = S.private_counters().begin();
1347 for (auto *E : S.counters()) {
Alexey Bataeva8899172015-08-06 12:30:57 +00001348 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1349 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001350 (void)LoopScope.addPrivate(VD, [&]() -> Address {
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001351 // Emit var without initialization.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001352 if (!LocalDeclMap.count(PrivateVD)) {
1353 auto VarEmission = EmitAutoVarAlloca(*PrivateVD);
1354 EmitAutoVarCleanups(VarEmission);
1355 }
1356 DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD),
1357 /*RefersToEnclosingVariableOrCapture=*/false,
1358 (*I)->getType(), VK_LValue, (*I)->getExprLoc());
1359 return EmitLValue(&DRE).getAddress();
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001360 });
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001361 if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) ||
1362 VD->hasGlobalStorage()) {
1363 (void)LoopScope.addPrivate(PrivateVD, [&]() -> Address {
1364 DeclRefExpr DRE(const_cast<VarDecl *>(VD),
1365 LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD),
1366 E->getType(), VK_LValue, E->getExprLoc());
1367 return EmitLValue(&DRE).getAddress();
1368 });
1369 }
Alexey Bataeva8899172015-08-06 12:30:57 +00001370 ++I;
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001371 }
Alexey Bataev435ad7b2014-10-10 09:48:26 +00001372}
1373
Alexey Bataev62dbb972015-04-22 11:59:37 +00001374static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S,
1375 const Expr *Cond, llvm::BasicBlock *TrueBlock,
1376 llvm::BasicBlock *FalseBlock, uint64_t TrueCount) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001377 if (!CGF.HaveInsertPoint())
1378 return;
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001379 {
1380 CodeGenFunction::OMPPrivateScope PreCondScope(CGF);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001381 CGF.EmitOMPPrivateLoopCounters(S, PreCondScope);
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001382 (void)PreCondScope.Privatize();
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001383 // Get initial values of real counters.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00001384 for (auto I : S.inits()) {
Alexey Bataev6e8248f2015-06-11 10:53:56 +00001385 CGF.EmitIgnoredExpr(I);
1386 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00001387 }
1388 // Check that loop is executed at least one time.
1389 CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount);
1390}
1391
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001392void CodeGenFunction::EmitOMPLinearClause(
1393 const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) {
1394 if (!HaveInsertPoint())
Alexey Bataev8ef31412015-12-18 07:58:25 +00001395 return;
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001396 llvm::DenseSet<const VarDecl *> SIMDLCVs;
1397 if (isOpenMPSimdDirective(D.getDirectiveKind())) {
1398 auto *LoopDirective = cast<OMPLoopDirective>(&D);
1399 for (auto *C : LoopDirective->counters()) {
1400 SIMDLCVs.insert(
1401 cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl());
1402 }
1403 }
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001404 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001405 auto CurPrivate = C->privates().begin();
Alexey Bataevc925aa32015-04-27 08:00:32 +00001406 for (auto *E : C->varlists()) {
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001407 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1408 auto *PrivateVD =
1409 cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001410 if (!SIMDLCVs.count(VD->getCanonicalDecl())) {
1411 bool IsRegistered = PrivateScope.addPrivate(VD, [&]() -> Address {
1412 // Emit private VarDecl with copy init.
1413 EmitVarDecl(*PrivateVD);
1414 return GetAddrOfLocalVar(PrivateVD);
1415 });
1416 assert(IsRegistered && "linear var already registered as private");
1417 // Silence the warning about unused variable.
1418 (void)IsRegistered;
1419 } else
1420 EmitVarDecl(*PrivateVD);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00001421 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00001422 }
1423 }
1424}
1425
Alexey Bataev45bfad52015-08-21 12:19:04 +00001426static void emitSimdlenSafelenClause(CodeGenFunction &CGF,
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001427 const OMPExecutableDirective &D,
1428 bool IsMonotonic) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001429 if (!CGF.HaveInsertPoint())
1430 return;
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001431 if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) {
Alexey Bataev45bfad52015-08-21 12:19:04 +00001432 RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(),
1433 /*ignoreResult=*/true);
1434 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
1435 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
1436 // In presence of finite 'safelen', it may be unsafe to mark all
1437 // the memory instructions parallel, because loop-carried
1438 // dependences of 'safelen' iterations are possible.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001439 if (!IsMonotonic)
1440 CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>());
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00001441 } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001442 RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(),
1443 /*ignoreResult=*/true);
1444 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
Tyler Nowickida46d0e2015-07-14 23:03:09 +00001445 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001446 // In presence of finite 'safelen', it may be unsafe to mark all
1447 // the memory instructions parallel, because loop-carried
1448 // dependences of 'safelen' iterations are possible.
1449 CGF.LoopStack.setParallel(false);
1450 }
1451}
1452
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001453void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D,
1454 bool IsMonotonic) {
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001455 // Walk clauses and process safelen/lastprivate.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001456 LoopStack.setParallel(!IsMonotonic);
Tyler Nowickida46d0e2015-07-14 23:03:09 +00001457 LoopStack.setVectorizeEnable(true);
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001458 emitSimdlenSafelenClause(*this, D, IsMonotonic);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001459}
1460
Alexey Bataevef549a82016-03-09 09:49:09 +00001461void CodeGenFunction::EmitOMPSimdFinal(
1462 const OMPLoopDirective &D,
1463 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
Alexey Bataev8ef31412015-12-18 07:58:25 +00001464 if (!HaveInsertPoint())
1465 return;
Alexey Bataevef549a82016-03-09 09:49:09 +00001466 llvm::BasicBlock *DoneBB = nullptr;
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001467 auto IC = D.counters().begin();
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001468 auto IPC = D.private_counters().begin();
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001469 for (auto F : D.finals()) {
1470 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl());
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001471 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl());
1472 auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD);
1473 if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) ||
1474 OrigVD->hasGlobalStorage() || CED) {
Alexey Bataevef549a82016-03-09 09:49:09 +00001475 if (!DoneBB) {
1476 if (auto *Cond = CondGen(*this)) {
1477 // If the first post-update expression is found, emit conditional
1478 // block if it was requested.
1479 auto *ThenBB = createBasicBlock(".omp.final.then");
1480 DoneBB = createBasicBlock(".omp.final.done");
1481 Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1482 EmitBlock(ThenBB);
1483 }
1484 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001485 Address OrigAddr = Address::invalid();
1486 if (CED)
1487 OrigAddr = EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress();
1488 else {
1489 DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD),
1490 /*RefersToEnclosingVariableOrCapture=*/false,
1491 (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc());
1492 OrigAddr = EmitLValue(&DRE).getAddress();
1493 }
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001494 OMPPrivateScope VarScope(*this);
1495 VarScope.addPrivate(OrigVD,
John McCall7f416cc2015-09-08 08:05:57 +00001496 [OrigAddr]() -> Address { return OrigAddr; });
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001497 (void)VarScope.Privatize();
1498 EmitIgnoredExpr(F);
1499 }
1500 ++IC;
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001501 ++IPC;
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001502 }
Alexey Bataevef549a82016-03-09 09:49:09 +00001503 if (DoneBB)
1504 EmitBlock(DoneBB, /*IsFinished=*/true);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001505}
1506
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001507static void emitOMPLoopBodyWithStopPoint(CodeGenFunction &CGF,
1508 const OMPLoopDirective &S,
1509 CodeGenFunction::JumpDest LoopExit) {
1510 CGF.EmitOMPLoopBody(S, LoopExit);
1511 CGF.EmitStopPoint(&S);
Hans Wennborged129ae2017-04-27 17:02:25 +00001512}
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001513
Alexander Musman515ad8c2014-05-22 08:54:05 +00001514void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001515 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00001516 OMPLoopScope PreInitScope(CGF, S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001517 // if (PreCond) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001518 // for (IV in 0..LastIteration) BODY;
1519 // <Final counter/linear vars updates>;
Alexey Bataev62dbb972015-04-22 11:59:37 +00001520 // }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001521 //
Alexander Musmana5f070a2014-10-01 06:03:56 +00001522
Alexey Bataev62dbb972015-04-22 11:59:37 +00001523 // Emit: if (PreCond) - begin.
1524 // If the condition constant folds and can be elided, avoid emitting the
1525 // whole loop.
1526 bool CondConstant;
1527 llvm::BasicBlock *ContBlock = nullptr;
1528 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
1529 if (!CondConstant)
1530 return;
1531 } else {
Alexey Bataev62dbb972015-04-22 11:59:37 +00001532 auto *ThenBlock = CGF.createBasicBlock("simd.if.then");
1533 ContBlock = CGF.createBasicBlock("simd.if.end");
Justin Bogner66242d62015-04-23 23:06:47 +00001534 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
1535 CGF.getProfileCount(&S));
Alexey Bataev62dbb972015-04-22 11:59:37 +00001536 CGF.EmitBlock(ThenBlock);
Justin Bogner66242d62015-04-23 23:06:47 +00001537 CGF.incrementProfileCounter(&S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001538 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001539
1540 // Emit the loop iteration variable.
1541 const Expr *IVExpr = S.getIterationVariable();
1542 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
1543 CGF.EmitVarDecl(*IVDecl);
1544 CGF.EmitIgnoredExpr(S.getInit());
1545
1546 // Emit the iterations count variable.
1547 // If it is not a variable, Sema decided to calculate iterations count on
Alexey Bataev7a228ff2015-05-21 07:59:51 +00001548 // each iteration (e.g., it is foldable into a constant).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001549 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
1550 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
1551 // Emit calculation of the iterations count.
1552 CGF.EmitIgnoredExpr(S.getCalcLastIteration());
Alexander Musmana5f070a2014-10-01 06:03:56 +00001553 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001554
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001555 CGF.EmitOMPSimdInit(S);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001556
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001557 emitAlignedClause(CGF, S);
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00001558 (void)CGF.EmitOMPLinearClauseInit(S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001559 {
1560 OMPPrivateScope LoopScope(CGF);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001561 CGF.EmitOMPPrivateLoopCounters(S, LoopScope);
1562 CGF.EmitOMPLinearClause(S, LoopScope);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001563 CGF.EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev89e7e8e2015-06-17 06:21:39 +00001564 CGF.EmitOMPReductionClauseInit(S, LoopScope);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001565 bool HasLastprivateClause =
1566 CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001567 (void)LoopScope.Privatize();
Alexey Bataev0f34da12015-07-02 04:17:07 +00001568 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
1569 S.getInc(),
Alexey Bataev62dbb972015-04-22 11:59:37 +00001570 [&S](CodeGenFunction &CGF) {
Alexey Bataev0f34da12015-07-02 04:17:07 +00001571 CGF.EmitOMPLoopBody(S, JumpDest());
Alexey Bataev62dbb972015-04-22 11:59:37 +00001572 CGF.EmitStopPoint(&S);
1573 },
1574 [](CodeGenFunction &) {});
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001575 CGF.EmitOMPSimdFinal(
1576 S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataevfc087ec2015-06-16 13:14:42 +00001577 // Emit final copy of the lastprivate variables at the end of loops.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001578 if (HasLastprivateClause)
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001579 CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001580 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_simd);
Alexey Bataev61205072016-03-02 04:57:40 +00001581 emitPostUpdateForReductionClause(
1582 CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev62dbb972015-04-22 11:59:37 +00001583 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001584 CGF.EmitOMPLinearClauseFinal(
Alexey Bataevef549a82016-03-09 09:49:09 +00001585 S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev62dbb972015-04-22 11:59:37 +00001586 // Emit: if (PreCond) - end.
1587 if (ContBlock) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001588 CGF.EmitBranch(ContBlock);
1589 CGF.EmitBlock(ContBlock, true);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001590 }
1591 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00001592 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001593 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
Alexander Musman515ad8c2014-05-22 08:54:05 +00001594}
1595
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001596void CodeGenFunction::EmitOMPOuterLoop(
1597 bool DynamicOrOrdered, bool IsMonotonic, const OMPLoopDirective &S,
1598 CodeGenFunction::OMPPrivateScope &LoopScope,
1599 const CodeGenFunction::OMPLoopArguments &LoopArgs,
1600 const CodeGenFunction::CodeGenLoopTy &CodeGenLoop,
1601 const CodeGenFunction::CodeGenOrderedTy &CodeGenOrdered) {
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001602 auto &RT = CGM.getOpenMPRuntime();
Alexander Musman92bdaab2015-03-12 13:37:50 +00001603
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001604 const Expr *IVExpr = S.getIterationVariable();
1605 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1606 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1607
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001608 auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end");
1609
1610 // Start the loop with a block that tests the condition.
1611 auto CondBlock = createBasicBlock("omp.dispatch.cond");
1612 EmitBlock(CondBlock);
Amara Emerson652795d2016-11-10 14:44:30 +00001613 const SourceRange &R = S.getSourceRange();
1614 LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()),
1615 SourceLocToDebugLoc(R.getEnd()));
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001616
1617 llvm::Value *BoolCondVal = nullptr;
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001618 if (!DynamicOrOrdered) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001619 // UB = min(UB, GlobalUB) or
1620 // UB = min(UB, PrevUB) for combined loop sharing constructs (e.g.
1621 // 'distribute parallel for')
1622 EmitIgnoredExpr(LoopArgs.EUB);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001623 // IV = LB
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001624 EmitIgnoredExpr(LoopArgs.Init);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001625 // IV < UB
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001626 BoolCondVal = EvaluateExprAsBool(LoopArgs.Cond);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001627 } else {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001628 BoolCondVal =
1629 RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned, LoopArgs.IL,
1630 LoopArgs.LB, LoopArgs.UB, LoopArgs.ST);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001631 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001632
1633 // If there are any cleanups between here and the loop-exit scope,
1634 // create a block to stage a loop exit along.
1635 auto ExitBlock = LoopExit.getBlock();
1636 if (LoopScope.requiresCleanups())
1637 ExitBlock = createBasicBlock("omp.dispatch.cleanup");
1638
1639 auto LoopBody = createBasicBlock("omp.dispatch.body");
1640 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
1641 if (ExitBlock != LoopExit.getBlock()) {
1642 EmitBlock(ExitBlock);
1643 EmitBranchThroughCleanup(LoopExit);
1644 }
1645 EmitBlock(LoopBody);
1646
Alexander Musman92bdaab2015-03-12 13:37:50 +00001647 // Emit "IV = LB" (in case of static schedule, we have already calculated new
1648 // LB for loop condition and emitted it above).
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001649 if (DynamicOrOrdered)
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001650 EmitIgnoredExpr(LoopArgs.Init);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001651
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001652 // Create a block for the increment.
1653 auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc");
1654 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1655
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001656 // Generate !llvm.loop.parallel metadata for loads and stores for loops
1657 // with dynamic/guided scheduling and without ordered clause.
Alexey Bataeva6f2a142015-12-31 06:52:34 +00001658 if (!isOpenMPSimdDirective(S.getDirectiveKind()))
1659 LoopStack.setParallel(!IsMonotonic);
1660 else
1661 EmitOMPSimdInit(S, IsMonotonic);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001662
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001663 SourceLocation Loc = S.getLocStart();
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001664
1665 // when 'distribute' is not combined with a 'for':
1666 // while (idx <= UB) { BODY; ++idx; }
1667 // when 'distribute' is combined with a 'for'
1668 // (e.g. 'distribute parallel for')
1669 // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; }
1670 EmitOMPInnerLoop(
1671 S, LoopScope.requiresCleanups(), LoopArgs.Cond, LoopArgs.IncExpr,
1672 [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) {
1673 CodeGenLoop(CGF, S, LoopExit);
1674 },
1675 [IVSize, IVSigned, Loc, &CodeGenOrdered](CodeGenFunction &CGF) {
1676 CodeGenOrdered(CGF, Loc, IVSize, IVSigned);
1677 });
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001678
1679 EmitBlock(Continue.getBlock());
1680 BreakContinueStack.pop_back();
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001681 if (!DynamicOrOrdered) {
Alexander Musman92bdaab2015-03-12 13:37:50 +00001682 // Emit "LB = LB + Stride", "UB = UB + Stride".
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001683 EmitIgnoredExpr(LoopArgs.NextLB);
1684 EmitIgnoredExpr(LoopArgs.NextUB);
Alexander Musman92bdaab2015-03-12 13:37:50 +00001685 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001686
1687 EmitBranch(CondBlock);
1688 LoopStack.pop();
1689 // Emit the fall-through block.
1690 EmitBlock(LoopExit.getBlock());
1691
1692 // Tell the runtime we are done.
Alexey Bataev957d8562016-11-17 15:12:05 +00001693 auto &&CodeGen = [DynamicOrOrdered, &S](CodeGenFunction &CGF) {
1694 if (!DynamicOrOrdered)
Alexey Bataevf43f7142017-09-06 16:17:35 +00001695 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd(),
1696 S.getDirectiveKind());
Alexey Bataev957d8562016-11-17 15:12:05 +00001697 };
1698 OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001699}
1700
1701void CodeGenFunction::EmitOMPForOuterLoop(
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001702 const OpenMPScheduleTy &ScheduleKind, bool IsMonotonic,
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001703 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001704 const OMPLoopArguments &LoopArgs,
1705 const CodeGenDispatchBoundsTy &CGDispatchBounds) {
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001706 auto &RT = CGM.getOpenMPRuntime();
1707
1708 // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime).
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001709 const bool DynamicOrOrdered =
1710 Ordered || RT.isDynamic(ScheduleKind.Schedule);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001711
1712 assert((Ordered ||
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001713 !RT.isStaticNonchunked(ScheduleKind.Schedule,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001714 LoopArgs.Chunk != nullptr)) &&
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001715 "static non-chunked schedule does not need outer loop");
1716
1717 // Emit outer loop.
1718 //
1719 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1720 // When schedule(dynamic,chunk_size) is specified, the iterations are
1721 // distributed to threads in the team in chunks as the threads request them.
1722 // Each thread executes a chunk of iterations, then requests another chunk,
1723 // until no chunks remain to be distributed. Each chunk contains chunk_size
1724 // iterations, except for the last chunk to be distributed, which may have
1725 // fewer iterations. When no chunk_size is specified, it defaults to 1.
1726 //
1727 // When schedule(guided,chunk_size) is specified, the iterations are assigned
1728 // to threads in the team in chunks as the executing threads request them.
1729 // Each thread executes a chunk of iterations, then requests another chunk,
1730 // until no chunks remain to be assigned. For a chunk_size of 1, the size of
1731 // each chunk is proportional to the number of unassigned iterations divided
1732 // by the number of threads in the team, decreasing to 1. For a chunk_size
1733 // with value k (greater than 1), the size of each chunk is determined in the
1734 // same way, with the restriction that the chunks do not contain fewer than k
1735 // iterations (except for the last chunk to be assigned, which may have fewer
1736 // than k iterations).
1737 //
1738 // When schedule(auto) is specified, the decision regarding scheduling is
1739 // delegated to the compiler and/or runtime system. The programmer gives the
1740 // implementation the freedom to choose any possible mapping of iterations to
1741 // threads in the team.
1742 //
1743 // When schedule(runtime) is specified, the decision regarding scheduling is
1744 // deferred until run time, and the schedule and chunk size are taken from the
1745 // run-sched-var ICV. If the ICV is set to auto, the schedule is
1746 // implementation defined
1747 //
1748 // while(__kmpc_dispatch_next(&LB, &UB)) {
1749 // idx = LB;
1750 // while (idx <= UB) { BODY; ++idx;
1751 // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only.
1752 // } // inner loop
1753 // }
1754 //
1755 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1756 // When schedule(static, chunk_size) is specified, iterations are divided into
1757 // chunks of size chunk_size, and the chunks are assigned to the threads in
1758 // the team in a round-robin fashion in the order of the thread number.
1759 //
1760 // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) {
1761 // while (idx <= UB) { BODY; ++idx; } // inner loop
1762 // LB = LB + ST;
1763 // UB = UB + ST;
1764 // }
1765 //
1766
1767 const Expr *IVExpr = S.getIterationVariable();
1768 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1769 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1770
1771 if (DynamicOrOrdered) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001772 auto DispatchBounds = CGDispatchBounds(*this, S, LoopArgs.LB, LoopArgs.UB);
1773 llvm::Value *LBVal = DispatchBounds.first;
1774 llvm::Value *UBVal = DispatchBounds.second;
1775 CGOpenMPRuntime::DispatchRTInput DipatchRTInputValues = {LBVal, UBVal,
1776 LoopArgs.Chunk};
Alexey Bataev9ebd7422016-05-10 09:57:36 +00001777 RT.emitForDispatchInit(*this, S.getLocStart(), ScheduleKind, IVSize,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001778 IVSigned, Ordered, DipatchRTInputValues);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001779 } else {
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001780 CGOpenMPRuntime::StaticRTInput StaticInit(
1781 IVSize, IVSigned, Ordered, LoopArgs.IL, LoopArgs.LB, LoopArgs.UB,
1782 LoopArgs.ST, LoopArgs.Chunk);
1783 RT.emitForStaticInit(*this, S.getLocStart(), S.getDirectiveKind(),
1784 ScheduleKind, StaticInit);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001785 }
1786
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001787 auto &&CodeGenOrdered = [Ordered](CodeGenFunction &CGF, SourceLocation Loc,
1788 const unsigned IVSize,
1789 const bool IVSigned) {
1790 if (Ordered) {
1791 CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(CGF, Loc, IVSize,
1792 IVSigned);
1793 }
1794 };
1795
1796 OMPLoopArguments OuterLoopArgs(LoopArgs.LB, LoopArgs.UB, LoopArgs.ST,
1797 LoopArgs.IL, LoopArgs.Chunk, LoopArgs.EUB);
1798 OuterLoopArgs.IncExpr = S.getInc();
1799 OuterLoopArgs.Init = S.getInit();
1800 OuterLoopArgs.Cond = S.getCond();
1801 OuterLoopArgs.NextLB = S.getNextLowerBound();
1802 OuterLoopArgs.NextUB = S.getNextUpperBound();
1803 EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, OuterLoopArgs,
1804 emitOMPLoopBodyWithStopPoint, CodeGenOrdered);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001805}
1806
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001807static void emitEmptyOrdered(CodeGenFunction &, SourceLocation Loc,
1808 const unsigned IVSize, const bool IVSigned) {}
1809
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001810void CodeGenFunction::EmitOMPDistributeOuterLoop(
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001811 OpenMPDistScheduleClauseKind ScheduleKind, const OMPLoopDirective &S,
1812 OMPPrivateScope &LoopScope, const OMPLoopArguments &LoopArgs,
1813 const CodeGenLoopTy &CodeGenLoopContent) {
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001814
1815 auto &RT = CGM.getOpenMPRuntime();
1816
1817 // Emit outer loop.
1818 // Same behavior as a OMPForOuterLoop, except that schedule cannot be
1819 // dynamic
1820 //
1821
1822 const Expr *IVExpr = S.getIterationVariable();
1823 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1824 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1825
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00001826 CGOpenMPRuntime::StaticRTInput StaticInit(
1827 IVSize, IVSigned, /* Ordered = */ false, LoopArgs.IL, LoopArgs.LB,
1828 LoopArgs.UB, LoopArgs.ST, LoopArgs.Chunk);
1829 RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind, StaticInit);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00001830
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001831 // for combined 'distribute' and 'for' the increment expression of distribute
1832 // is store in DistInc. For 'distribute' alone, it is in Inc.
1833 Expr *IncExpr;
1834 if (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()))
1835 IncExpr = S.getDistInc();
1836 else
1837 IncExpr = S.getInc();
1838
1839 // this routine is shared by 'omp distribute parallel for' and
1840 // 'omp distribute': select the right EUB expression depending on the
1841 // directive
1842 OMPLoopArguments OuterLoopArgs;
1843 OuterLoopArgs.LB = LoopArgs.LB;
1844 OuterLoopArgs.UB = LoopArgs.UB;
1845 OuterLoopArgs.ST = LoopArgs.ST;
1846 OuterLoopArgs.IL = LoopArgs.IL;
1847 OuterLoopArgs.Chunk = LoopArgs.Chunk;
1848 OuterLoopArgs.EUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
1849 ? S.getCombinedEnsureUpperBound()
1850 : S.getEnsureUpperBound();
1851 OuterLoopArgs.IncExpr = IncExpr;
1852 OuterLoopArgs.Init = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
1853 ? S.getCombinedInit()
1854 : S.getInit();
1855 OuterLoopArgs.Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
1856 ? S.getCombinedCond()
1857 : S.getCond();
1858 OuterLoopArgs.NextLB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
1859 ? S.getCombinedNextLowerBound()
1860 : S.getNextLowerBound();
1861 OuterLoopArgs.NextUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
1862 ? S.getCombinedNextUpperBound()
1863 : S.getNextUpperBound();
1864
1865 EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false, S,
1866 LoopScope, OuterLoopArgs, CodeGenLoopContent,
1867 emitEmptyOrdered);
1868}
1869
1870/// Emit a helper variable and return corresponding lvalue.
1871static LValue EmitOMPHelperVar(CodeGenFunction &CGF,
1872 const DeclRefExpr *Helper) {
1873 auto VDecl = cast<VarDecl>(Helper->getDecl());
1874 CGF.EmitVarDecl(*VDecl);
1875 return CGF.EmitLValue(Helper);
1876}
1877
1878static std::pair<LValue, LValue>
1879emitDistributeParallelForInnerBounds(CodeGenFunction &CGF,
1880 const OMPExecutableDirective &S) {
1881 const OMPLoopDirective &LS = cast<OMPLoopDirective>(S);
1882 LValue LB =
1883 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable()));
1884 LValue UB =
1885 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable()));
1886
1887 // When composing 'distribute' with 'for' (e.g. as in 'distribute
1888 // parallel for') we need to use the 'distribute'
1889 // chunk lower and upper bounds rather than the whole loop iteration
1890 // space. These are parameters to the outlined function for 'parallel'
1891 // and we copy the bounds of the previous schedule into the
1892 // the current ones.
1893 LValue PrevLB = CGF.EmitLValue(LS.getPrevLowerBoundVariable());
1894 LValue PrevUB = CGF.EmitLValue(LS.getPrevUpperBoundVariable());
1895 llvm::Value *PrevLBVal = CGF.EmitLoadOfScalar(PrevLB, SourceLocation());
1896 PrevLBVal = CGF.EmitScalarConversion(
1897 PrevLBVal, LS.getPrevLowerBoundVariable()->getType(),
1898 LS.getIterationVariable()->getType(), SourceLocation());
1899 llvm::Value *PrevUBVal = CGF.EmitLoadOfScalar(PrevUB, SourceLocation());
1900 PrevUBVal = CGF.EmitScalarConversion(
1901 PrevUBVal, LS.getPrevUpperBoundVariable()->getType(),
1902 LS.getIterationVariable()->getType(), SourceLocation());
1903
1904 CGF.EmitStoreOfScalar(PrevLBVal, LB);
1905 CGF.EmitStoreOfScalar(PrevUBVal, UB);
1906
1907 return {LB, UB};
1908}
1909
1910/// if the 'for' loop has a dispatch schedule (e.g. dynamic, guided) then
1911/// we need to use the LB and UB expressions generated by the worksharing
1912/// code generation support, whereas in non combined situations we would
1913/// just emit 0 and the LastIteration expression
1914/// This function is necessary due to the difference of the LB and UB
1915/// types for the RT emission routines for 'for_static_init' and
1916/// 'for_dispatch_init'
1917static std::pair<llvm::Value *, llvm::Value *>
1918emitDistributeParallelForDispatchBounds(CodeGenFunction &CGF,
1919 const OMPExecutableDirective &S,
1920 Address LB, Address UB) {
1921 const OMPLoopDirective &LS = cast<OMPLoopDirective>(S);
1922 const Expr *IVExpr = LS.getIterationVariable();
1923 // when implementing a dynamic schedule for a 'for' combined with a
1924 // 'distribute' (e.g. 'distribute parallel for'), the 'for' loop
1925 // is not normalized as each team only executes its own assigned
1926 // distribute chunk
1927 QualType IteratorTy = IVExpr->getType();
1928 llvm::Value *LBVal = CGF.EmitLoadOfScalar(LB, /*Volatile=*/false, IteratorTy,
1929 SourceLocation());
1930 llvm::Value *UBVal = CGF.EmitLoadOfScalar(UB, /*Volatile=*/false, IteratorTy,
1931 SourceLocation());
1932 return {LBVal, UBVal};
Hans Wennborged129ae2017-04-27 17:02:25 +00001933}
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001934
1935static void emitDistributeParallelForDistributeInnerBoundParams(
1936 CodeGenFunction &CGF, const OMPExecutableDirective &S,
1937 llvm::SmallVectorImpl<llvm::Value *> &CapturedVars) {
1938 const auto &Dir = cast<OMPLoopDirective>(S);
1939 LValue LB =
1940 CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedLowerBoundVariable()));
1941 auto LBCast = CGF.Builder.CreateIntCast(
1942 CGF.Builder.CreateLoad(LB.getAddress()), CGF.SizeTy, /*isSigned=*/false);
1943 CapturedVars.push_back(LBCast);
1944 LValue UB =
1945 CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedUpperBoundVariable()));
1946
1947 auto UBCast = CGF.Builder.CreateIntCast(
1948 CGF.Builder.CreateLoad(UB.getAddress()), CGF.SizeTy, /*isSigned=*/false);
1949 CapturedVars.push_back(UBCast);
Hans Wennborged129ae2017-04-27 17:02:25 +00001950}
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001951
1952static void
1953emitInnerParallelForWhenCombined(CodeGenFunction &CGF,
1954 const OMPLoopDirective &S,
1955 CodeGenFunction::JumpDest LoopExit) {
1956 auto &&CGInlinedWorksharingLoop = [&S](CodeGenFunction &CGF,
1957 PrePostActionTy &) {
1958 CGF.EmitOMPWorksharingLoop(S, S.getPrevEnsureUpperBound(),
1959 emitDistributeParallelForInnerBounds,
1960 emitDistributeParallelForDispatchBounds);
1961 };
1962
1963 emitCommonOMPParallelDirective(
1964 CGF, S, OMPD_for, CGInlinedWorksharingLoop,
1965 emitDistributeParallelForDistributeInnerBoundParams);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001966}
1967
Carlo Bertolli9925f152016-06-27 14:55:37 +00001968void CodeGenFunction::EmitOMPDistributeParallelForDirective(
1969 const OMPDistributeParallelForDirective &S) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001970 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
1971 CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined,
1972 S.getDistInc());
1973 };
Carlo Bertolli9925f152016-06-27 14:55:37 +00001974 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00001975 OMPCancelStackRAII CancelRegion(*this, OMPD_distribute_parallel_for,
1976 /*HasCancel=*/false);
1977 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen,
1978 /*HasCancel=*/false);
Carlo Bertolli9925f152016-06-27 14:55:37 +00001979}
1980
Kelvin Li4a39add2016-07-05 05:00:15 +00001981void CodeGenFunction::EmitOMPDistributeParallelForSimdDirective(
1982 const OMPDistributeParallelForSimdDirective &S) {
1983 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
1984 CGM.getOpenMPRuntime().emitInlinedDirective(
1985 *this, OMPD_distribute_parallel_for_simd,
1986 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
1987 OMPLoopScope PreInitScope(CGF, S);
1988 CGF.EmitStmt(
1989 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1990 });
1991}
Kelvin Li787f3fc2016-07-06 04:45:38 +00001992
1993void CodeGenFunction::EmitOMPDistributeSimdDirective(
1994 const OMPDistributeSimdDirective &S) {
1995 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
1996 CGM.getOpenMPRuntime().emitInlinedDirective(
1997 *this, OMPD_distribute_simd,
1998 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
1999 OMPLoopScope PreInitScope(CGF, S);
2000 CGF.EmitStmt(
2001 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2002 });
2003}
2004
Kelvin Lia579b912016-07-14 02:54:56 +00002005void CodeGenFunction::EmitOMPTargetParallelForSimdDirective(
2006 const OMPTargetParallelForSimdDirective &S) {
2007 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2008 CGM.getOpenMPRuntime().emitInlinedDirective(
2009 *this, OMPD_target_parallel_for_simd,
2010 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2011 OMPLoopScope PreInitScope(CGF, S);
2012 CGF.EmitStmt(
2013 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2014 });
2015}
2016
Kelvin Li986330c2016-07-20 22:57:10 +00002017void CodeGenFunction::EmitOMPTargetSimdDirective(
2018 const OMPTargetSimdDirective &S) {
2019 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2020 CGM.getOpenMPRuntime().emitInlinedDirective(
2021 *this, OMPD_target_simd, [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2022 OMPLoopScope PreInitScope(CGF, S);
2023 CGF.EmitStmt(
2024 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2025 });
2026}
2027
Kelvin Li02532872016-08-05 14:37:37 +00002028void CodeGenFunction::EmitOMPTeamsDistributeDirective(
2029 const OMPTeamsDistributeDirective &S) {
2030 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2031 CGM.getOpenMPRuntime().emitInlinedDirective(
2032 *this, OMPD_teams_distribute,
2033 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2034 OMPLoopScope PreInitScope(CGF, S);
2035 CGF.EmitStmt(
2036 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2037 });
2038}
2039
Kelvin Li4e325f72016-10-25 12:50:55 +00002040void CodeGenFunction::EmitOMPTeamsDistributeSimdDirective(
2041 const OMPTeamsDistributeSimdDirective &S) {
2042 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2043 CGM.getOpenMPRuntime().emitInlinedDirective(
2044 *this, OMPD_teams_distribute_simd,
2045 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2046 OMPLoopScope PreInitScope(CGF, S);
2047 CGF.EmitStmt(
2048 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2049 });
2050}
2051
Kelvin Li579e41c2016-11-30 23:51:03 +00002052void CodeGenFunction::EmitOMPTeamsDistributeParallelForSimdDirective(
2053 const OMPTeamsDistributeParallelForSimdDirective &S) {
2054 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2055 CGM.getOpenMPRuntime().emitInlinedDirective(
2056 *this, OMPD_teams_distribute_parallel_for_simd,
2057 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2058 OMPLoopScope PreInitScope(CGF, S);
2059 CGF.EmitStmt(
2060 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2061 });
2062}
Kelvin Li4e325f72016-10-25 12:50:55 +00002063
Kelvin Li7ade93f2016-12-09 03:24:30 +00002064void CodeGenFunction::EmitOMPTeamsDistributeParallelForDirective(
2065 const OMPTeamsDistributeParallelForDirective &S) {
2066 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2067 CGM.getOpenMPRuntime().emitInlinedDirective(
2068 *this, OMPD_teams_distribute_parallel_for,
2069 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2070 OMPLoopScope PreInitScope(CGF, S);
2071 CGF.EmitStmt(
2072 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2073 });
2074}
2075
Kelvin Li83c451e2016-12-25 04:52:54 +00002076void CodeGenFunction::EmitOMPTargetTeamsDistributeDirective(
2077 const OMPTargetTeamsDistributeDirective &S) {
Kelvin Li26fd21a2016-12-28 17:57:07 +00002078 CGM.getOpenMPRuntime().emitInlinedDirective(
2079 *this, OMPD_target_teams_distribute,
Kelvin Li83c451e2016-12-25 04:52:54 +00002080 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Kelvin Li26fd21a2016-12-28 17:57:07 +00002081 CGF.EmitStmt(
2082 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Kelvin Li83c451e2016-12-25 04:52:54 +00002083 });
2084}
2085
Kelvin Li80e8f562016-12-29 22:16:30 +00002086void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDirective(
2087 const OMPTargetTeamsDistributeParallelForDirective &S) {
2088 CGM.getOpenMPRuntime().emitInlinedDirective(
2089 *this, OMPD_target_teams_distribute_parallel_for,
2090 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2091 CGF.EmitStmt(
2092 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2093 });
2094}
2095
Kelvin Li1851df52017-01-03 05:23:48 +00002096void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDirective(
2097 const OMPTargetTeamsDistributeParallelForSimdDirective &S) {
2098 CGM.getOpenMPRuntime().emitInlinedDirective(
2099 *this, OMPD_target_teams_distribute_parallel_for_simd,
2100 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2101 CGF.EmitStmt(
2102 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2103 });
2104}
2105
Kelvin Lida681182017-01-10 18:08:18 +00002106void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDirective(
2107 const OMPTargetTeamsDistributeSimdDirective &S) {
2108 CGM.getOpenMPRuntime().emitInlinedDirective(
2109 *this, OMPD_target_teams_distribute_simd,
2110 [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2111 CGF.EmitStmt(
2112 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2113 });
2114}
2115
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002116namespace {
2117 struct ScheduleKindModifiersTy {
2118 OpenMPScheduleClauseKind Kind;
2119 OpenMPScheduleClauseModifier M1;
2120 OpenMPScheduleClauseModifier M2;
2121 ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind,
2122 OpenMPScheduleClauseModifier M1,
2123 OpenMPScheduleClauseModifier M2)
2124 : Kind(Kind), M1(M1), M2(M2) {}
2125 };
2126} // namespace
2127
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002128bool CodeGenFunction::EmitOMPWorksharingLoop(
2129 const OMPLoopDirective &S, Expr *EUB,
2130 const CodeGenLoopBoundsTy &CodeGenLoopBounds,
2131 const CodeGenDispatchBoundsTy &CGDispatchBounds) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002132 // Emit the loop iteration variable.
2133 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
2134 auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
2135 EmitVarDecl(*IVDecl);
2136
2137 // Emit the iterations count variable.
2138 // If it is not a variable, Sema decided to calculate iterations count on each
2139 // iteration (e.g., it is foldable into a constant).
2140 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
2141 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
2142 // Emit calculation of the iterations count.
2143 EmitIgnoredExpr(S.getCalcLastIteration());
2144 }
2145
2146 auto &RT = CGM.getOpenMPRuntime();
2147
Alexey Bataev38e89532015-04-16 04:54:05 +00002148 bool HasLastprivateClause;
Alexander Musmanc6388682014-12-15 07:07:06 +00002149 // Check pre-condition.
2150 {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002151 OMPLoopScope PreInitScope(*this, S);
Alexander Musmanc6388682014-12-15 07:07:06 +00002152 // Skip the entire loop if we don't meet the precondition.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002153 // If the condition constant folds and can be elided, avoid emitting the
2154 // whole loop.
2155 bool CondConstant;
2156 llvm::BasicBlock *ContBlock = nullptr;
2157 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
2158 if (!CondConstant)
2159 return false;
2160 } else {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002161 auto *ThenBlock = createBasicBlock("omp.precond.then");
2162 ContBlock = createBasicBlock("omp.precond.end");
2163 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
Justin Bogner66242d62015-04-23 23:06:47 +00002164 getProfileCount(&S));
Alexey Bataev62dbb972015-04-22 11:59:37 +00002165 EmitBlock(ThenBlock);
Justin Bogner66242d62015-04-23 23:06:47 +00002166 incrementProfileCounter(&S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00002167 }
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00002168
Alexey Bataev8b427062016-05-25 12:36:08 +00002169 bool Ordered = false;
2170 if (auto *OrderedClause = S.getSingleClause<OMPOrderedClause>()) {
2171 if (OrderedClause->getNumForLoops())
2172 RT.emitDoacrossInit(*this, S);
2173 else
2174 Ordered = true;
2175 }
2176
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002177 llvm::DenseSet<const Expr *> EmittedFinals;
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00002178 emitAlignedClause(*this, S);
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00002179 bool HasLinears = EmitOMPLinearClauseInit(S);
Alexey Bataevef549a82016-03-09 09:49:09 +00002180 // Emit helper vars inits.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002181
2182 std::pair<LValue, LValue> Bounds = CodeGenLoopBounds(*this, S);
2183 LValue LB = Bounds.first;
2184 LValue UB = Bounds.second;
Alexey Bataevef549a82016-03-09 09:49:09 +00002185 LValue ST =
2186 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
2187 LValue IL =
2188 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
2189
Alexander Musmanc6388682014-12-15 07:07:06 +00002190 // Emit 'then' code.
2191 {
Alexander Musmanc6388682014-12-15 07:07:06 +00002192 OMPPrivateScope LoopScope(*this);
Alexey Bataev8c3edfe2017-08-16 15:58:46 +00002193 if (EmitOMPFirstprivateClause(S, LoopScope) || HasLinears) {
Alexey Bataev69c62a92015-04-15 04:52:20 +00002194 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00002195 // initialization of firstprivate variables and post-update of
2196 // lastprivate variables.
Alexey Bataev25e5b442015-09-15 12:52:43 +00002197 CGM.getOpenMPRuntime().emitBarrierCall(
2198 *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
2199 /*ForceSimpleCall=*/true);
Alexey Bataev69c62a92015-04-15 04:52:20 +00002200 }
Alexey Bataev50a64582015-04-22 12:24:45 +00002201 EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev38e89532015-04-16 04:54:05 +00002202 HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev7ebe5fd2015-04-22 13:43:03 +00002203 EmitOMPReductionClauseInit(S, LoopScope);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002204 EmitOMPPrivateLoopCounters(S, LoopScope);
2205 EmitOMPLinearClause(S, LoopScope);
Alexander Musman7931b982015-03-16 07:14:41 +00002206 (void)LoopScope.Privatize();
Alexander Musmanc6388682014-12-15 07:07:06 +00002207
2208 // Detect the loop schedule kind and chunk.
Alexey Bataev3392d762016-02-16 11:18:12 +00002209 llvm::Value *Chunk = nullptr;
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002210 OpenMPScheduleTy ScheduleKind;
Alexey Bataev3392d762016-02-16 11:18:12 +00002211 if (auto *C = S.getSingleClause<OMPScheduleClause>()) {
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002212 ScheduleKind.Schedule = C->getScheduleKind();
2213 ScheduleKind.M1 = C->getFirstScheduleModifier();
2214 ScheduleKind.M2 = C->getSecondScheduleModifier();
Alexey Bataev3392d762016-02-16 11:18:12 +00002215 if (const auto *Ch = C->getChunkSize()) {
2216 Chunk = EmitScalarExpr(Ch);
2217 Chunk = EmitScalarConversion(Chunk, Ch->getType(),
2218 S.getIterationVariable()->getType(),
2219 S.getLocStart());
2220 }
2221 }
Alexander Musmanc6388682014-12-15 07:07:06 +00002222 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
2223 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002224 // OpenMP 4.5, 2.7.1 Loop Construct, Description.
2225 // If the static schedule kind is specified or if the ordered clause is
2226 // specified, and if no monotonic modifier is specified, the effect will
2227 // be as if the monotonic modifier was specified.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002228 if (RT.isStaticNonchunked(ScheduleKind.Schedule,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00002229 /* Chunked */ Chunk != nullptr) &&
2230 !Ordered) {
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002231 if (isOpenMPSimdDirective(S.getDirectiveKind()))
2232 EmitOMPSimdInit(S, /*IsMonotonic=*/true);
Alexander Musmanc6388682014-12-15 07:07:06 +00002233 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
2234 // When no chunk_size is specified, the iteration space is divided into
2235 // chunks that are approximately equal in size, and at most one chunk is
2236 // distributed to each thread. Note that the size of the chunks is
2237 // unspecified in this case.
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00002238 CGOpenMPRuntime::StaticRTInput StaticInit(
2239 IVSize, IVSigned, Ordered, IL.getAddress(), LB.getAddress(),
2240 UB.getAddress(), ST.getAddress());
2241 RT.emitForStaticInit(*this, S.getLocStart(), S.getDirectiveKind(),
2242 ScheduleKind, StaticInit);
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002243 auto LoopExit =
2244 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
Alexander Musmanc6388682014-12-15 07:07:06 +00002245 // UB = min(UB, GlobalUB);
2246 EmitIgnoredExpr(S.getEnsureUpperBound());
2247 // IV = LB;
2248 EmitIgnoredExpr(S.getInit());
2249 // while (idx <= UB) { BODY; ++idx; }
Alexey Bataevae05c292015-06-16 11:59:36 +00002250 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
2251 S.getInc(),
Alexey Bataev0f34da12015-07-02 04:17:07 +00002252 [&S, LoopExit](CodeGenFunction &CGF) {
2253 CGF.EmitOMPLoopBody(S, LoopExit);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002254 CGF.EmitStopPoint(&S);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00002255 },
2256 [](CodeGenFunction &) {});
Alexey Bataev0f34da12015-07-02 04:17:07 +00002257 EmitBlock(LoopExit.getBlock());
Alexander Musmanc6388682014-12-15 07:07:06 +00002258 // Tell the runtime we are done.
Alexey Bataev957d8562016-11-17 15:12:05 +00002259 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
Alexey Bataevf43f7142017-09-06 16:17:35 +00002260 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd(),
2261 S.getDirectiveKind());
Alexey Bataev957d8562016-11-17 15:12:05 +00002262 };
2263 OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002264 } else {
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002265 const bool IsMonotonic =
2266 Ordered || ScheduleKind.Schedule == OMPC_SCHEDULE_static ||
2267 ScheduleKind.Schedule == OMPC_SCHEDULE_unknown ||
2268 ScheduleKind.M1 == OMPC_SCHEDULE_MODIFIER_monotonic ||
2269 ScheduleKind.M2 == OMPC_SCHEDULE_MODIFIER_monotonic;
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002270 // Emit the outer loop, which requests its work chunk [LB..UB] from
2271 // runtime and runs the inner loop to process it.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002272 const OMPLoopArguments LoopArguments(LB.getAddress(), UB.getAddress(),
2273 ST.getAddress(), IL.getAddress(),
2274 Chunk, EUB);
Alexey Bataeva6f2a142015-12-31 06:52:34 +00002275 EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered,
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002276 LoopArguments, CGDispatchBounds);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00002277 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002278 if (isOpenMPSimdDirective(S.getDirectiveKind())) {
2279 EmitOMPSimdFinal(S,
2280 [&](CodeGenFunction &CGF) -> llvm::Value * {
2281 return CGF.Builder.CreateIsNotNull(
2282 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2283 });
2284 }
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002285 EmitOMPReductionClauseFinal(
2286 S, /*ReductionKind=*/isOpenMPSimdDirective(S.getDirectiveKind())
2287 ? /*Parallel and Simd*/ OMPD_parallel_for_simd
2288 : /*Parallel only*/ OMPD_parallel);
Alexey Bataev61205072016-03-02 04:57:40 +00002289 // Emit post-update of the reduction variables if IsLastIter != 0.
2290 emitPostUpdateForReductionClause(
2291 *this, S, [&](CodeGenFunction &CGF) -> llvm::Value * {
2292 return CGF.Builder.CreateIsNotNull(
2293 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2294 });
Alexey Bataev38e89532015-04-16 04:54:05 +00002295 // Emit final copy of the lastprivate variables if IsLastIter != 0.
2296 if (HasLastprivateClause)
2297 EmitOMPLastprivateClauseFinal(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002298 S, isOpenMPSimdDirective(S.getDirectiveKind()),
2299 Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart())));
Alexander Musmanc6388682014-12-15 07:07:06 +00002300 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002301 EmitOMPLinearClauseFinal(S, [&](CodeGenFunction &CGF) -> llvm::Value * {
Alexey Bataevef549a82016-03-09 09:49:09 +00002302 return CGF.Builder.CreateIsNotNull(
2303 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2304 });
Alexander Musmanc6388682014-12-15 07:07:06 +00002305 // We're now done with the loop, so jump to the continuation block.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002306 if (ContBlock) {
2307 EmitBranch(ContBlock);
2308 EmitBlock(ContBlock, true);
2309 }
Alexander Musmanc6388682014-12-15 07:07:06 +00002310 }
Alexey Bataev38e89532015-04-16 04:54:05 +00002311 return HasLastprivateClause;
Alexander Musmanc6388682014-12-15 07:07:06 +00002312}
2313
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002314/// The following two functions generate expressions for the loop lower
2315/// and upper bounds in case of static and dynamic (dispatch) schedule
2316/// of the associated 'for' or 'distribute' loop.
2317static std::pair<LValue, LValue>
2318emitForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S) {
2319 const OMPLoopDirective &LS = cast<OMPLoopDirective>(S);
2320 LValue LB =
2321 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable()));
2322 LValue UB =
2323 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable()));
2324 return {LB, UB};
2325}
2326
2327/// When dealing with dispatch schedules (e.g. dynamic, guided) we do not
2328/// consider the lower and upper bound expressions generated by the
2329/// worksharing loop support, but we use 0 and the iteration space size as
2330/// constants
2331static std::pair<llvm::Value *, llvm::Value *>
2332emitDispatchForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S,
2333 Address LB, Address UB) {
2334 const OMPLoopDirective &LS = cast<OMPLoopDirective>(S);
2335 const Expr *IVExpr = LS.getIterationVariable();
2336 const unsigned IVSize = CGF.getContext().getTypeSize(IVExpr->getType());
2337 llvm::Value *LBVal = CGF.Builder.getIntN(IVSize, 0);
2338 llvm::Value *UBVal = CGF.EmitScalarExpr(LS.getLastIteration());
2339 return {LBVal, UBVal};
2340}
2341
Alexander Musmanc6388682014-12-15 07:07:06 +00002342void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) {
Alexey Bataev38e89532015-04-16 04:54:05 +00002343 bool HasLastprivates = false;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002344 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF,
2345 PrePostActionTy &) {
Alexey Bataev957d8562016-11-17 15:12:05 +00002346 OMPCancelStackRAII CancelRegion(CGF, OMPD_for, S.hasCancel());
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002347 HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(),
2348 emitForLoopBounds,
2349 emitDispatchForLoopBounds);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002350 };
Alexey Bataev3392d762016-02-16 11:18:12 +00002351 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002352 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002353 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen,
2354 S.hasCancel());
2355 }
Alexander Musmanc6388682014-12-15 07:07:06 +00002356
2357 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002358 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) {
Alexey Bataevf2685682015-03-30 04:30:22 +00002359 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
2360 }
Alexey Bataevf29276e2014-06-18 04:14:57 +00002361}
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002362
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002363void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002364 bool HasLastprivates = false;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002365 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF,
2366 PrePostActionTy &) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002367 HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(),
2368 emitForLoopBounds,
2369 emitDispatchForLoopBounds);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002370 };
Alexey Bataev3392d762016-02-16 11:18:12 +00002371 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002372 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002373 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
2374 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002375
2376 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002377 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00002378 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
2379 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00002380}
2381
Alexey Bataev2df54a02015-03-12 08:53:29 +00002382static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty,
2383 const Twine &Name,
2384 llvm::Value *Init = nullptr) {
John McCall7f416cc2015-09-08 08:05:57 +00002385 auto LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty);
Alexey Bataev2df54a02015-03-12 08:53:29 +00002386 if (Init)
Akira Hatanaka642f7992016-10-18 19:05:41 +00002387 CGF.EmitStoreThroughLValue(RValue::get(Init), LVal, /*isInit*/ true);
Alexey Bataev2df54a02015-03-12 08:53:29 +00002388 return LVal;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002389}
2390
Alexey Bataev3392d762016-02-16 11:18:12 +00002391void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) {
Alexey Bataev2df54a02015-03-12 08:53:29 +00002392 auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt();
2393 auto *CS = dyn_cast<CompoundStmt>(Stmt);
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002394 bool HasLastprivates = false;
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002395 auto &&CodeGen = [&S, Stmt, CS, &HasLastprivates](CodeGenFunction &CGF,
2396 PrePostActionTy &) {
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002397 auto &C = CGF.CGM.getContext();
2398 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
2399 // Emit helper vars inits.
2400 LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.",
2401 CGF.Builder.getInt32(0));
2402 auto *GlobalUBVal = CS != nullptr ? CGF.Builder.getInt32(CS->size() - 1)
2403 : CGF.Builder.getInt32(0);
2404 LValue UB =
2405 createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal);
2406 LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.",
2407 CGF.Builder.getInt32(1));
2408 LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.",
2409 CGF.Builder.getInt32(0));
2410 // Loop counter.
2411 LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv.");
2412 OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
2413 CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV);
2414 OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
2415 CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB);
2416 // Generate condition for loop.
2417 BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue,
Adam Nemet484aa452017-03-27 19:17:25 +00002418 OK_Ordinary, S.getLocStart(), FPOptions());
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002419 // Increment for loop counter.
2420 UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary,
2421 S.getLocStart());
2422 auto BodyGen = [Stmt, CS, &S, &IV](CodeGenFunction &CGF) {
2423 // Iterate through all sections and emit a switch construct:
2424 // switch (IV) {
2425 // case 0:
2426 // <SectionStmt[0]>;
2427 // break;
2428 // ...
2429 // case <NumSection> - 1:
2430 // <SectionStmt[<NumSection> - 1]>;
2431 // break;
2432 // }
2433 // .omp.sections.exit:
2434 auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit");
2435 auto *SwitchStmt = CGF.Builder.CreateSwitch(
2436 CGF.EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB,
2437 CS == nullptr ? 1 : CS->size());
2438 if (CS) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002439 unsigned CaseNumber = 0;
Benjamin Kramer642f1732015-07-02 21:03:14 +00002440 for (auto *SubStmt : CS->children()) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002441 auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
2442 CGF.EmitBlock(CaseBB);
2443 SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB);
Benjamin Kramer642f1732015-07-02 21:03:14 +00002444 CGF.EmitStmt(SubStmt);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002445 CGF.EmitBranch(ExitBB);
Benjamin Kramer642f1732015-07-02 21:03:14 +00002446 ++CaseNumber;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002447 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002448 } else {
2449 auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
2450 CGF.EmitBlock(CaseBB);
2451 SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB);
2452 CGF.EmitStmt(Stmt);
2453 CGF.EmitBranch(ExitBB);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002454 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002455 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
Alexey Bataev2df54a02015-03-12 08:53:29 +00002456 };
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002457
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002458 CodeGenFunction::OMPPrivateScope LoopScope(CGF);
2459 if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) {
Alexey Bataev9efc03b2015-04-27 04:34:03 +00002460 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00002461 // initialization of firstprivate variables and post-update of lastprivate
2462 // variables.
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002463 CGF.CGM.getOpenMPRuntime().emitBarrierCall(
2464 CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
2465 /*ForceSimpleCall=*/true);
Alexey Bataev9efc03b2015-04-27 04:34:03 +00002466 }
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002467 CGF.EmitOMPPrivateClause(S, LoopScope);
2468 HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
2469 CGF.EmitOMPReductionClauseInit(S, LoopScope);
2470 (void)LoopScope.Privatize();
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002471
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002472 // Emit static non-chunked loop.
Alexey Bataev9ebd7422016-05-10 09:57:36 +00002473 OpenMPScheduleTy ScheduleKind;
2474 ScheduleKind.Schedule = OMPC_SCHEDULE_static;
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00002475 CGOpenMPRuntime::StaticRTInput StaticInit(
2476 /*IVSize=*/32, /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(),
2477 LB.getAddress(), UB.getAddress(), ST.getAddress());
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002478 CGF.CGM.getOpenMPRuntime().emitForStaticInit(
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00002479 CGF, S.getLocStart(), S.getDirectiveKind(), ScheduleKind, StaticInit);
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002480 // UB = min(UB, GlobalUB);
2481 auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart());
2482 auto *MinUBGlobalUB = CGF.Builder.CreateSelect(
2483 CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal);
2484 CGF.EmitStoreOfScalar(MinUBGlobalUB, UB);
2485 // IV = LB;
2486 CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV);
2487 // while (idx <= UB) { BODY; ++idx; }
2488 CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen,
2489 [](CodeGenFunction &) {});
2490 // Tell the runtime we are done.
Alexey Bataev957d8562016-11-17 15:12:05 +00002491 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
Alexey Bataevf43f7142017-09-06 16:17:35 +00002492 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd(),
2493 S.getDirectiveKind());
Alexey Bataev957d8562016-11-17 15:12:05 +00002494 };
2495 CGF.OMPCancelStack.emitExit(CGF, S.getDirectiveKind(), CodeGen);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002496 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel);
Alexey Bataev61205072016-03-02 04:57:40 +00002497 // Emit post-update of the reduction variables if IsLastIter != 0.
2498 emitPostUpdateForReductionClause(
2499 CGF, S, [&](CodeGenFunction &CGF) -> llvm::Value * {
2500 return CGF.Builder.CreateIsNotNull(
2501 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2502 });
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002503
2504 // Emit final copy of the lastprivate variables if IsLastIter != 0.
2505 if (HasLastprivates)
2506 CGF.EmitOMPLastprivateClauseFinal(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00002507 S, /*NoFinals=*/false,
2508 CGF.Builder.CreateIsNotNull(
2509 CGF.EmitLoadOfScalar(IL, S.getLocStart())));
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002510 };
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002511
2512 bool HasCancel = false;
2513 if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S))
2514 HasCancel = OSD->hasCancel();
2515 else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S))
2516 HasCancel = OPSD->hasCancel();
Alexey Bataev957d8562016-11-17 15:12:05 +00002517 OMPCancelStackRAII CancelRegion(*this, S.getDirectiveKind(), HasCancel);
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002518 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen,
2519 HasCancel);
2520 // Emit barrier for lastprivates only if 'sections' directive has 'nowait'
2521 // clause. Otherwise the barrier will be generated by the codegen for the
2522 // directive.
2523 if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) {
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002524 // Emit implicit barrier to synchronize threads and avoid data races on
2525 // initialization of firstprivate variables.
Alexey Bataev3015bcc2016-01-22 08:56:50 +00002526 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
2527 OMPD_unknown);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00002528 }
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002529}
Alexey Bataev2df54a02015-03-12 08:53:29 +00002530
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002531void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
Alexey Bataev3392d762016-02-16 11:18:12 +00002532 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002533 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002534 EmitSections(S);
2535 }
Alexey Bataev2df54a02015-03-12 08:53:29 +00002536 // Emit an implicit barrier at the end.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002537 if (!S.getSingleClause<OMPNowaitClause>()) {
Alexey Bataev3392d762016-02-16 11:18:12 +00002538 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
2539 OMPD_sections);
Alexey Bataevf2685682015-03-30 04:30:22 +00002540 }
Alexey Bataev2df54a02015-03-12 08:53:29 +00002541}
2542
2543void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002544 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002545 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002546 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002547 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev25e5b442015-09-15 12:52:43 +00002548 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen,
2549 S.hasCancel());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002550}
2551
Alexey Bataev6956e2e2015-02-05 06:35:41 +00002552void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00002553 llvm::SmallVector<const Expr *, 8> CopyprivateVars;
Alexey Bataev420d45b2015-04-14 05:11:24 +00002554 llvm::SmallVector<const Expr *, 8> DestExprs;
Alexey Bataeva63048e2015-03-23 06:18:07 +00002555 llvm::SmallVector<const Expr *, 8> SrcExprs;
Alexey Bataeva63048e2015-03-23 06:18:07 +00002556 llvm::SmallVector<const Expr *, 8> AssignmentOps;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002557 // Check if there are any 'copyprivate' clauses associated with this
Alexey Bataevcd8b6a22016-02-15 08:07:17 +00002558 // 'single' construct.
Alexey Bataeva63048e2015-03-23 06:18:07 +00002559 // Build a list of copyprivate variables along with helper expressions
2560 // (<source>, <destination>, <destination>=<source> expressions)
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002561 for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00002562 CopyprivateVars.append(C->varlists().begin(), C->varlists().end());
Alexey Bataev420d45b2015-04-14 05:11:24 +00002563 DestExprs.append(C->destination_exprs().begin(),
2564 C->destination_exprs().end());
Alexey Bataeva63048e2015-03-23 06:18:07 +00002565 SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end());
Alexey Bataeva63048e2015-03-23 06:18:07 +00002566 AssignmentOps.append(C->assignment_ops().begin(),
2567 C->assignment_ops().end());
2568 }
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002569 // Emit code for 'single' region along with 'copyprivate' clauses
2570 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2571 Action.Enter(CGF);
2572 OMPPrivateScope SingleScope(CGF);
2573 (void)CGF.EmitOMPFirstprivateClause(S, SingleScope);
2574 CGF.EmitOMPPrivateClause(S, SingleScope);
2575 (void)SingleScope.Privatize();
2576 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2577 };
Alexey Bataev3392d762016-02-16 11:18:12 +00002578 {
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002579 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00002580 CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(),
2581 CopyprivateVars, DestExprs,
2582 SrcExprs, AssignmentOps);
2583 }
2584 // Emit an implicit barrier at the end (to avoid data race on firstprivate
2585 // init or if no 'nowait' clause was specified and no 'copyprivate' clause).
Alexey Bataev417089f2016-02-17 13:19:37 +00002586 if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) {
Alexey Bataev5521d782015-04-24 04:21:15 +00002587 CGM.getOpenMPRuntime().emitBarrierCall(
2588 *this, S.getLocStart(),
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002589 S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single);
Alexey Bataevf2685682015-03-30 04:30:22 +00002590 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002591}
2592
Alexey Bataev8d690652014-12-04 07:23:53 +00002593void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002594 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2595 Action.Enter(CGF);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002596 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002597 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002598 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002599 CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart());
Alexander Musman80c22892014-07-17 08:54:58 +00002600}
2601
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00002602void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &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 Bataevfc57d162015-12-15 10:55:09 +00002607 Expr *Hint = nullptr;
2608 if (auto *HintClause = S.getSingleClause<OMPHintClause>())
2609 Hint = HintClause->getHint();
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002610 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataevfc57d162015-12-15 10:55:09 +00002611 CGM.getOpenMPRuntime().emitCriticalRegion(*this,
2612 S.getDirectiveName().getAsString(),
2613 CodeGen, S.getLocStart(), Hint);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002614}
2615
Alexey Bataev671605e2015-04-13 05:28:11 +00002616void CodeGenFunction::EmitOMPParallelForDirective(
2617 const OMPParallelForDirective &S) {
2618 // Emit directive as a combined directive that consists of two implicit
2619 // directives: 'parallel' with 'for' directive.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002620 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Alexey Bataev957d8562016-11-17 15:12:05 +00002621 OMPCancelStackRAII CancelRegion(CGF, OMPD_parallel_for, S.hasCancel());
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002622 CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds,
2623 emitDispatchForLoopBounds);
Alexey Bataev671605e2015-04-13 05:28:11 +00002624 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002625 emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen,
2626 emitEmptyBoundParameters);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002627}
2628
Alexander Musmane4e893b2014-09-23 09:33:00 +00002629void CodeGenFunction::EmitOMPParallelForSimdDirective(
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00002630 const OMPParallelForSimdDirective &S) {
2631 // Emit directive as a combined directive that consists of two implicit
2632 // directives: 'parallel' with 'for' directive.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002633 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002634 CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds,
2635 emitDispatchForLoopBounds);
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00002636 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002637 emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen,
2638 emitEmptyBoundParameters);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002639}
2640
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002641void CodeGenFunction::EmitOMPParallelSectionsDirective(
Alexey Bataev68adb7d2015-04-14 03:29:22 +00002642 const OMPParallelSectionsDirective &S) {
2643 // Emit directive as a combined directive that consists of two implicit
2644 // directives: 'parallel' with 'sections' directive.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002645 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2646 CGF.EmitSections(S);
2647 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002648 emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen,
2649 emitEmptyBoundParameters);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002650}
2651
Alexey Bataev7292c292016-04-25 12:22:29 +00002652void CodeGenFunction::EmitOMPTaskBasedDirective(const OMPExecutableDirective &S,
2653 const RegionCodeGenTy &BodyGen,
2654 const TaskGenTy &TaskGen,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002655 OMPTaskDataTy &Data) {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002656 // Emit outlined function for task construct.
2657 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
Alexey Bataev62b63b12015-03-10 07:28:44 +00002658 auto *I = CS->getCapturedDecl()->param_begin();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002659 auto *PartId = std::next(I);
Alexey Bataev48591dd2016-04-20 04:01:36 +00002660 auto *TaskT = std::next(I, 4);
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002661 // Check if the task is final
2662 if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) {
2663 // If the condition constant folds and can be elided, try to avoid emitting
2664 // the condition and the dead arm of the if/else.
2665 auto *Cond = Clause->getCondition();
2666 bool CondConstant;
2667 if (ConstantFoldsToSimpleInteger(Cond, CondConstant))
2668 Data.Final.setInt(CondConstant);
2669 else
2670 Data.Final.setPointer(EvaluateExprAsBool(Cond));
2671 } else {
2672 // By default the task is not final.
2673 Data.Final.setInt(/*IntVal=*/false);
2674 }
Alexey Bataev1e1e2862016-05-10 12:21:02 +00002675 // Check if the task has 'priority' clause.
2676 if (const auto *Clause = S.getSingleClause<OMPPriorityClause>()) {
Alexey Bataev1e1e2862016-05-10 12:21:02 +00002677 auto *Prio = Clause->getPriority();
Alexey Bataev5140e742016-07-19 04:21:09 +00002678 Data.Priority.setInt(/*IntVal=*/true);
Alexey Bataevad537bb2016-05-30 09:06:50 +00002679 Data.Priority.setPointer(EmitScalarConversion(
2680 EmitScalarExpr(Prio), Prio->getType(),
2681 getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1),
2682 Prio->getExprLoc()));
Alexey Bataev1e1e2862016-05-10 12:21:02 +00002683 }
Alexey Bataev62b63b12015-03-10 07:28:44 +00002684 // The first function argument for tasks is a thread id, the second one is a
2685 // part id (0 for tied tasks, >=0 for untied task).
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002686 llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
2687 // Get list of private variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002688 for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002689 auto IRef = C->varlist_begin();
2690 for (auto *IInit : C->private_copies()) {
2691 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2692 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
Alexey Bataev7292c292016-04-25 12:22:29 +00002693 Data.PrivateVars.push_back(*IRef);
2694 Data.PrivateCopies.push_back(IInit);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002695 }
2696 ++IRef;
2697 }
2698 }
2699 EmittedAsPrivate.clear();
2700 // Get list of firstprivate variables.
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002701 for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002702 auto IRef = C->varlist_begin();
2703 auto IElemInitRef = C->inits().begin();
2704 for (auto *IInit : C->private_copies()) {
2705 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2706 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
Alexey Bataev7292c292016-04-25 12:22:29 +00002707 Data.FirstprivateVars.push_back(*IRef);
2708 Data.FirstprivateCopies.push_back(IInit);
2709 Data.FirstprivateInits.push_back(*IElemInitRef);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002710 }
Richard Trieucc3949d2016-02-18 22:34:54 +00002711 ++IRef;
2712 ++IElemInitRef;
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002713 }
2714 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00002715 // Get list of lastprivate variables (for taskloops).
2716 llvm::DenseMap<const VarDecl *, const DeclRefExpr *> LastprivateDstsOrigs;
2717 for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) {
2718 auto IRef = C->varlist_begin();
2719 auto ID = C->destination_exprs().begin();
2720 for (auto *IInit : C->private_copies()) {
2721 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2722 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
2723 Data.LastprivateVars.push_back(*IRef);
2724 Data.LastprivateCopies.push_back(IInit);
2725 }
2726 LastprivateDstsOrigs.insert(
2727 {cast<VarDecl>(cast<DeclRefExpr>(*ID)->getDecl()),
2728 cast<DeclRefExpr>(*IRef)});
2729 ++IRef;
2730 ++ID;
2731 }
2732 }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002733 SmallVector<const Expr *, 4> LHSs;
2734 SmallVector<const Expr *, 4> RHSs;
2735 for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) {
2736 auto IPriv = C->privates().begin();
2737 auto IRed = C->reduction_ops().begin();
2738 auto ILHS = C->lhs_exprs().begin();
2739 auto IRHS = C->rhs_exprs().begin();
2740 for (const auto *Ref : C->varlists()) {
2741 Data.ReductionVars.emplace_back(Ref);
2742 Data.ReductionCopies.emplace_back(*IPriv);
2743 Data.ReductionOps.emplace_back(*IRed);
2744 LHSs.emplace_back(*ILHS);
2745 RHSs.emplace_back(*IRHS);
2746 std::advance(IPriv, 1);
2747 std::advance(IRed, 1);
2748 std::advance(ILHS, 1);
2749 std::advance(IRHS, 1);
2750 }
2751 }
2752 Data.Reductions = CGM.getOpenMPRuntime().emitTaskReductionInit(
2753 *this, S.getLocStart(), LHSs, RHSs, Data);
Alexey Bataev1d2353d2015-06-24 11:01:36 +00002754 // Build list of dependences.
Alexey Bataev7292c292016-04-25 12:22:29 +00002755 for (const auto *C : S.getClausesOfKind<OMPDependClause>())
2756 for (auto *IRef : C->varlists())
2757 Data.Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef));
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002758 auto &&CodeGen = [&Data, &S, CS, &BodyGen, &LastprivateDstsOrigs](
Alexey Bataevf93095a2016-05-05 08:46:22 +00002759 CodeGenFunction &CGF, PrePostActionTy &Action) {
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002760 // Set proper addresses for generated private copies.
Alexey Bataev7292c292016-04-25 12:22:29 +00002761 OMPPrivateScope Scope(CGF);
Alexey Bataevf93095a2016-05-05 08:46:22 +00002762 if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty() ||
2763 !Data.LastprivateVars.empty()) {
Alexey Bataev3c595a62017-08-14 15:01:03 +00002764 enum { PrivatesParam = 2, CopyFnParam = 3 };
Alexey Bataev48591dd2016-04-20 04:01:36 +00002765 auto *CopyFn = CGF.Builder.CreateLoad(
2766 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3)));
2767 auto *PrivatesPtr = CGF.Builder.CreateLoad(
2768 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2)));
2769 // Map privates.
2770 llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs;
2771 llvm::SmallVector<llvm::Value *, 16> CallArgs;
2772 CallArgs.push_back(PrivatesPtr);
Alexey Bataev7292c292016-04-25 12:22:29 +00002773 for (auto *E : Data.PrivateVars) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00002774 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2775 Address PrivatePtr = CGF.CreateMemTemp(
2776 CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr");
2777 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
2778 CallArgs.push_back(PrivatePtr.getPointer());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002779 }
Alexey Bataev7292c292016-04-25 12:22:29 +00002780 for (auto *E : Data.FirstprivateVars) {
Alexey Bataev48591dd2016-04-20 04:01:36 +00002781 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2782 Address PrivatePtr =
2783 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()),
2784 ".firstpriv.ptr.addr");
2785 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
2786 CallArgs.push_back(PrivatePtr.getPointer());
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002787 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00002788 for (auto *E : Data.LastprivateVars) {
2789 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2790 Address PrivatePtr =
2791 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()),
2792 ".lastpriv.ptr.addr");
2793 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
2794 CallArgs.push_back(PrivatePtr.getPointer());
2795 }
Alexey Bataev3c595a62017-08-14 15:01:03 +00002796 CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getLocStart(),
2797 CopyFn, CallArgs);
Alexey Bataevf93095a2016-05-05 08:46:22 +00002798 for (auto &&Pair : LastprivateDstsOrigs) {
2799 auto *OrigVD = cast<VarDecl>(Pair.second->getDecl());
2800 DeclRefExpr DRE(
2801 const_cast<VarDecl *>(OrigVD),
2802 /*RefersToEnclosingVariableOrCapture=*/CGF.CapturedStmtInfo->lookup(
2803 OrigVD) != nullptr,
2804 Pair.second->getType(), VK_LValue, Pair.second->getExprLoc());
2805 Scope.addPrivate(Pair.first, [&CGF, &DRE]() {
2806 return CGF.EmitLValue(&DRE).getAddress();
2807 });
2808 }
Alexey Bataev48591dd2016-04-20 04:01:36 +00002809 for (auto &&Pair : PrivatePtrs) {
2810 Address Replacement(CGF.Builder.CreateLoad(Pair.second),
2811 CGF.getContext().getDeclAlign(Pair.first));
2812 Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; });
2813 }
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002814 }
Alexey Bataevbe5a8b42017-07-17 13:30:36 +00002815 if (Data.Reductions) {
2816 OMPLexicalScope LexScope(CGF, S, /*AsInlined=*/true);
2817 ReductionCodeGen RedCG(Data.ReductionVars, Data.ReductionCopies,
2818 Data.ReductionOps);
2819 llvm::Value *ReductionsPtr = CGF.Builder.CreateLoad(
2820 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(9)));
2821 for (unsigned Cnt = 0, E = Data.ReductionVars.size(); Cnt < E; ++Cnt) {
2822 RedCG.emitSharedLValue(CGF, Cnt);
2823 RedCG.emitAggregateType(CGF, Cnt);
2824 Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem(
2825 CGF, S.getLocStart(), ReductionsPtr, RedCG.getSharedLValue(Cnt));
2826 Replacement =
2827 Address(CGF.EmitScalarConversion(
2828 Replacement.getPointer(), CGF.getContext().VoidPtrTy,
2829 CGF.getContext().getPointerType(
2830 Data.ReductionCopies[Cnt]->getType()),
2831 SourceLocation()),
2832 Replacement.getAlignment());
2833 Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement);
2834 Scope.addPrivate(RedCG.getBaseDecl(Cnt),
2835 [Replacement]() { return Replacement; });
2836 // FIXME: This must removed once the runtime library is fixed.
2837 // Emit required threadprivate variables for
2838 // initilizer/combiner/finalizer.
2839 CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getLocStart(),
2840 RedCG, Cnt);
2841 }
2842 }
Alexey Bataev88202be2017-07-27 13:20:36 +00002843 // Privatize all private variables except for in_reduction items.
Alexey Bataev48591dd2016-04-20 04:01:36 +00002844 (void)Scope.Privatize();
Alexey Bataev88202be2017-07-27 13:20:36 +00002845 SmallVector<const Expr *, 4> InRedVars;
2846 SmallVector<const Expr *, 4> InRedPrivs;
2847 SmallVector<const Expr *, 4> InRedOps;
2848 SmallVector<const Expr *, 4> TaskgroupDescriptors;
2849 for (const auto *C : S.getClausesOfKind<OMPInReductionClause>()) {
2850 auto IPriv = C->privates().begin();
2851 auto IRed = C->reduction_ops().begin();
2852 auto ITD = C->taskgroup_descriptors().begin();
2853 for (const auto *Ref : C->varlists()) {
2854 InRedVars.emplace_back(Ref);
2855 InRedPrivs.emplace_back(*IPriv);
2856 InRedOps.emplace_back(*IRed);
2857 TaskgroupDescriptors.emplace_back(*ITD);
2858 std::advance(IPriv, 1);
2859 std::advance(IRed, 1);
2860 std::advance(ITD, 1);
2861 }
2862 }
2863 // Privatize in_reduction items here, because taskgroup descriptors must be
2864 // privatized earlier.
2865 OMPPrivateScope InRedScope(CGF);
2866 if (!InRedVars.empty()) {
2867 ReductionCodeGen RedCG(InRedVars, InRedPrivs, InRedOps);
2868 for (unsigned Cnt = 0, E = InRedVars.size(); Cnt < E; ++Cnt) {
2869 RedCG.emitSharedLValue(CGF, Cnt);
2870 RedCG.emitAggregateType(CGF, Cnt);
2871 // The taskgroup descriptor variable is always implicit firstprivate and
2872 // privatized already during procoessing of the firstprivates.
2873 llvm::Value *ReductionsPtr = CGF.EmitLoadOfScalar(
2874 CGF.EmitLValue(TaskgroupDescriptors[Cnt]), SourceLocation());
2875 Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem(
2876 CGF, S.getLocStart(), ReductionsPtr, RedCG.getSharedLValue(Cnt));
2877 Replacement = Address(
2878 CGF.EmitScalarConversion(
2879 Replacement.getPointer(), CGF.getContext().VoidPtrTy,
2880 CGF.getContext().getPointerType(InRedPrivs[Cnt]->getType()),
2881 SourceLocation()),
2882 Replacement.getAlignment());
2883 Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement);
2884 InRedScope.addPrivate(RedCG.getBaseDecl(Cnt),
2885 [Replacement]() { return Replacement; });
2886 // FIXME: This must removed once the runtime library is fixed.
2887 // Emit required threadprivate variables for
2888 // initilizer/combiner/finalizer.
2889 CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getLocStart(),
2890 RedCG, Cnt);
2891 }
2892 }
2893 (void)InRedScope.Privatize();
Alexey Bataev48591dd2016-04-20 04:01:36 +00002894
2895 Action.Enter(CGF);
Alexey Bataev7292c292016-04-25 12:22:29 +00002896 BodyGen(CGF);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002897 };
Alexey Bataev7292c292016-04-25 12:22:29 +00002898 auto *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction(
2899 S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied,
2900 Data.NumberOfParts);
2901 OMPLexicalScope Scope(*this, S);
2902 TaskGen(*this, OutlinedFn, Data);
2903}
2904
2905void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) {
2906 // Emit outlined function for task construct.
2907 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
2908 auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002909 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
Alexey Bataev1d677132015-04-22 13:57:31 +00002910 const Expr *IfCond = nullptr;
Alexey Bataev7371aa32015-09-03 08:45:56 +00002911 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
2912 if (C->getNameModifier() == OMPD_unknown ||
2913 C->getNameModifier() == OMPD_task) {
2914 IfCond = C->getCondition();
2915 break;
2916 }
Alexey Bataev1d677132015-04-22 13:57:31 +00002917 }
Alexey Bataev7292c292016-04-25 12:22:29 +00002918
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002919 OMPTaskDataTy Data;
2920 // Check if we should emit tied or untied task.
2921 Data.Tied = !S.getSingleClause<OMPUntiedClause>();
Alexey Bataev7292c292016-04-25 12:22:29 +00002922 auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) {
2923 CGF.EmitStmt(CS->getCapturedStmt());
2924 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002925 auto &&TaskGen = [&S, SharedsTy, CapturedStruct,
Alexey Bataev7292c292016-04-25 12:22:29 +00002926 IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn,
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002927 const OMPTaskDataTy &Data) {
2928 CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getLocStart(), S, OutlinedFn,
2929 SharedsTy, CapturedStruct, IfCond,
2930 Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00002931 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00002932 EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002933}
2934
Alexey Bataev9f797f32015-02-05 05:57:51 +00002935void CodeGenFunction::EmitOMPTaskyieldDirective(
2936 const OMPTaskyieldDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002937 CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart());
Alexey Bataev68446b72014-07-18 07:47:19 +00002938}
2939
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00002940void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) {
Alexey Bataevf2685682015-03-30 04:30:22 +00002941 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier);
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002942}
2943
Alexey Bataev8b8e2022015-04-27 05:22:09 +00002944void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) {
2945 CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart());
Alexey Bataev2df347a2014-07-18 10:17:07 +00002946}
2947
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002948void CodeGenFunction::EmitOMPTaskgroupDirective(
2949 const OMPTaskgroupDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00002950 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2951 Action.Enter(CGF);
Alexey Bataev3b1b8952017-07-25 15:53:26 +00002952 if (const Expr *E = S.getReductionRef()) {
2953 SmallVector<const Expr *, 4> LHSs;
2954 SmallVector<const Expr *, 4> RHSs;
2955 OMPTaskDataTy Data;
2956 for (const auto *C : S.getClausesOfKind<OMPTaskReductionClause>()) {
2957 auto IPriv = C->privates().begin();
2958 auto IRed = C->reduction_ops().begin();
2959 auto ILHS = C->lhs_exprs().begin();
2960 auto IRHS = C->rhs_exprs().begin();
2961 for (const auto *Ref : C->varlists()) {
2962 Data.ReductionVars.emplace_back(Ref);
2963 Data.ReductionCopies.emplace_back(*IPriv);
2964 Data.ReductionOps.emplace_back(*IRed);
2965 LHSs.emplace_back(*ILHS);
2966 RHSs.emplace_back(*IRHS);
2967 std::advance(IPriv, 1);
2968 std::advance(IRed, 1);
2969 std::advance(ILHS, 1);
2970 std::advance(IRHS, 1);
2971 }
2972 }
2973 llvm::Value *ReductionDesc =
2974 CGF.CGM.getOpenMPRuntime().emitTaskReductionInit(CGF, S.getLocStart(),
2975 LHSs, RHSs, Data);
2976 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2977 CGF.EmitVarDecl(*VD);
2978 CGF.EmitStoreOfScalar(ReductionDesc, CGF.GetAddrOfLocalVar(VD),
2979 /*Volatile=*/false, E->getType());
2980 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002981 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002982 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00002983 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002984 CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getLocStart());
2985}
2986
Alexey Bataevcc37cc12014-11-20 04:34:54 +00002987void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002988 CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00002989 if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00002990 return llvm::makeArrayRef(FlushClause->varlist_begin(),
2991 FlushClause->varlist_end());
2992 }
2993 return llvm::None;
2994 }(), S.getLocStart());
Alexey Bataev6125da92014-07-21 11:26:11 +00002995}
2996
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00002997void CodeGenFunction::EmitOMPDistributeLoop(const OMPLoopDirective &S,
2998 const CodeGenLoopTy &CodeGenLoop,
2999 Expr *IncExpr) {
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003000 // Emit the loop iteration variable.
3001 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
3002 auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
3003 EmitVarDecl(*IVDecl);
3004
3005 // Emit the iterations count variable.
3006 // If it is not a variable, Sema decided to calculate iterations count on each
3007 // iteration (e.g., it is foldable into a constant).
3008 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
3009 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
3010 // Emit calculation of the iterations count.
3011 EmitIgnoredExpr(S.getCalcLastIteration());
3012 }
3013
3014 auto &RT = CGM.getOpenMPRuntime();
3015
Carlo Bertolli962bb802017-01-03 18:24:42 +00003016 bool HasLastprivateClause = false;
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003017 // Check pre-condition.
3018 {
Alexey Bataev5a3af132016-03-29 08:58:54 +00003019 OMPLoopScope PreInitScope(*this, S);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003020 // Skip the entire loop if we don't meet the precondition.
3021 // If the condition constant folds and can be elided, avoid emitting the
3022 // whole loop.
3023 bool CondConstant;
3024 llvm::BasicBlock *ContBlock = nullptr;
3025 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
3026 if (!CondConstant)
3027 return;
3028 } else {
3029 auto *ThenBlock = createBasicBlock("omp.precond.then");
3030 ContBlock = createBasicBlock("omp.precond.end");
3031 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
3032 getProfileCount(&S));
3033 EmitBlock(ThenBlock);
3034 incrementProfileCounter(&S);
3035 }
3036
3037 // Emit 'then' code.
3038 {
3039 // Emit helper vars inits.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003040
3041 LValue LB = EmitOMPHelperVar(
3042 *this, cast<DeclRefExpr>(
3043 (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3044 ? S.getCombinedLowerBoundVariable()
3045 : S.getLowerBoundVariable())));
3046 LValue UB = EmitOMPHelperVar(
3047 *this, cast<DeclRefExpr>(
3048 (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3049 ? S.getCombinedUpperBoundVariable()
3050 : S.getUpperBoundVariable())));
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003051 LValue ST =
3052 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
3053 LValue IL =
3054 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
3055
3056 OMPPrivateScope LoopScope(*this);
Carlo Bertolli962bb802017-01-03 18:24:42 +00003057 if (EmitOMPFirstprivateClause(S, LoopScope)) {
3058 // Emit implicit barrier to synchronize threads and avoid data races on
3059 // initialization of firstprivate variables and post-update of
3060 // lastprivate variables.
3061 CGM.getOpenMPRuntime().emitBarrierCall(
3062 *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
3063 /*ForceSimpleCall=*/true);
3064 }
3065 EmitOMPPrivateClause(S, LoopScope);
3066 HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003067 EmitOMPPrivateLoopCounters(S, LoopScope);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003068 (void)LoopScope.Privatize();
3069
3070 // Detect the distribute schedule kind and chunk.
3071 llvm::Value *Chunk = nullptr;
3072 OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown;
3073 if (auto *C = S.getSingleClause<OMPDistScheduleClause>()) {
3074 ScheduleKind = C->getDistScheduleKind();
3075 if (const auto *Ch = C->getChunkSize()) {
3076 Chunk = EmitScalarExpr(Ch);
3077 Chunk = EmitScalarConversion(Chunk, Ch->getType(),
3078 S.getIterationVariable()->getType(),
3079 S.getLocStart());
3080 }
3081 }
3082 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
3083 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
3084
3085 // OpenMP [2.10.8, distribute Construct, Description]
3086 // If dist_schedule is specified, kind must be static. If specified,
3087 // iterations are divided into chunks of size chunk_size, chunks are
3088 // assigned to the teams of the league in a round-robin fashion in the
3089 // order of the team number. When no chunk_size is specified, the
3090 // iteration space is divided into chunks that are approximately equal
3091 // in size, and at most one chunk is distributed to each team of the
3092 // league. The size of the chunks is unspecified in this case.
3093 if (RT.isStaticNonchunked(ScheduleKind,
3094 /* Chunked */ Chunk != nullptr)) {
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003095 CGOpenMPRuntime::StaticRTInput StaticInit(
3096 IVSize, IVSigned, /* Ordered = */ false, IL.getAddress(),
3097 LB.getAddress(), UB.getAddress(), ST.getAddress());
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003098 RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind,
Alexey Bataev0f87dbe2017-08-14 17:56:13 +00003099 StaticInit);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003100 auto LoopExit =
3101 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
3102 // UB = min(UB, GlobalUB);
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003103 EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3104 ? S.getCombinedEnsureUpperBound()
3105 : S.getEnsureUpperBound());
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003106 // IV = LB;
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003107 EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3108 ? S.getCombinedInit()
3109 : S.getInit());
3110
3111 Expr *Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
3112 ? S.getCombinedCond()
3113 : S.getCond();
3114
3115 // for distribute alone, codegen
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003116 // while (idx <= UB) { BODY; ++idx; }
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003117 // when combined with 'for' (e.g. as in 'distribute parallel for')
3118 // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; }
3119 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), Cond, IncExpr,
3120 [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) {
3121 CodeGenLoop(CGF, S, LoopExit);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003122 },
3123 [](CodeGenFunction &) {});
3124 EmitBlock(LoopExit.getBlock());
3125 // Tell the runtime we are done.
Alexey Bataevf43f7142017-09-06 16:17:35 +00003126 RT.emitForStaticFinish(*this, S.getLocStart(), S.getDirectiveKind());
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003127 } else {
3128 // Emit the outer loop, which requests its work chunk [LB..UB] from
3129 // runtime and runs the inner loop to process it.
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003130 const OMPLoopArguments LoopArguments = {
3131 LB.getAddress(), UB.getAddress(), ST.getAddress(), IL.getAddress(),
3132 Chunk};
3133 EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope, LoopArguments,
3134 CodeGenLoop);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003135 }
Carlo Bertolli962bb802017-01-03 18:24:42 +00003136
3137 // Emit final copy of the lastprivate variables if IsLastIter != 0.
3138 if (HasLastprivateClause)
3139 EmitOMPLastprivateClauseFinal(
3140 S, /*NoFinals=*/false,
3141 Builder.CreateIsNotNull(
3142 EmitLoadOfScalar(IL, S.getLocStart())));
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003143 }
3144
3145 // We're now done with the loop, so jump to the continuation block.
3146 if (ContBlock) {
3147 EmitBranch(ContBlock);
3148 EmitBlock(ContBlock, true);
3149 }
3150 }
3151}
3152
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003153void CodeGenFunction::EmitOMPDistributeDirective(
3154 const OMPDistributeDirective &S) {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003155 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00003156
3157 CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc());
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003158 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00003159 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Carlo Bertollifc35ad22016-03-07 16:04:49 +00003160 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen,
3161 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003162}
3163
Alexey Bataev5f600d62015-09-29 03:48:57 +00003164static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM,
3165 const CapturedStmt *S) {
3166 CodeGenFunction CGF(CGM, /*suppressNewContext=*/true);
3167 CodeGenFunction::CGCapturedStmtInfo CapStmtInfo;
3168 CGF.CapturedStmtInfo = &CapStmtInfo;
3169 auto *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S);
3170 Fn->addFnAttr(llvm::Attribute::NoInline);
3171 return Fn;
3172}
3173
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003174void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) {
Alexey Bataev8b427062016-05-25 12:36:08 +00003175 if (!S.getAssociatedStmt()) {
3176 for (const auto *DC : S.getClausesOfKind<OMPDependClause>())
3177 CGM.getOpenMPRuntime().emitDoacrossOrdered(*this, DC);
Alexey Bataev8ef31412015-12-18 07:58:25 +00003178 return;
Alexey Bataev8b427062016-05-25 12:36:08 +00003179 }
Alexey Bataev5f600d62015-09-29 03:48:57 +00003180 auto *C = S.getSingleClause<OMPSIMDClause>();
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003181 auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF,
3182 PrePostActionTy &Action) {
Alexey Bataev5f600d62015-09-29 03:48:57 +00003183 if (C) {
3184 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
3185 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
3186 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
3187 auto *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS);
Alexey Bataev3c595a62017-08-14 15:01:03 +00003188 CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getLocStart(),
3189 OutlinedFn, CapturedVars);
Alexey Bataev5f600d62015-09-29 03:48:57 +00003190 } else {
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003191 Action.Enter(CGF);
Alexey Bataev5f600d62015-09-29 03:48:57 +00003192 CGF.EmitStmt(
3193 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3194 }
Alexey Bataev98eb6e32015-04-22 11:15:40 +00003195 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00003196 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev5f600d62015-09-29 03:48:57 +00003197 CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart(), !C);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003198}
3199
Alexey Bataevb57056f2015-01-22 06:17:56 +00003200static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003201 QualType SrcType, QualType DestType,
3202 SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00003203 assert(CGF.hasScalarEvaluationKind(DestType) &&
3204 "DestType must have scalar evaluation kind.");
3205 assert(!Val.isAggregate() && "Must be a scalar or complex.");
3206 return Val.isScalar()
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003207 ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType,
3208 Loc)
Alexey Bataevb57056f2015-01-22 06:17:56 +00003209 : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003210 DestType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003211}
3212
3213static CodeGenFunction::ComplexPairTy
3214convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003215 QualType DestType, SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00003216 assert(CGF.getEvaluationKind(DestType) == TEK_Complex &&
3217 "DestType must have complex evaluation kind.");
3218 CodeGenFunction::ComplexPairTy ComplexVal;
3219 if (Val.isScalar()) {
3220 // Convert the input element to the element type of the complex.
3221 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003222 auto ScalarVal = CGF.EmitScalarConversion(Val.getScalarVal(), SrcType,
3223 DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003224 ComplexVal = CodeGenFunction::ComplexPairTy(
3225 ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType()));
3226 } else {
3227 assert(Val.isComplex() && "Must be a scalar or complex.");
3228 auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType();
3229 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
3230 ComplexVal.first = CGF.EmitScalarConversion(
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003231 Val.getComplexVal().first, SrcElementType, DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003232 ComplexVal.second = CGF.EmitScalarConversion(
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003233 Val.getComplexVal().second, SrcElementType, DestElementType, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003234 }
3235 return ComplexVal;
3236}
3237
Alexey Bataev5e018f92015-04-23 06:35:10 +00003238static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst,
3239 LValue LVal, RValue RVal) {
3240 if (LVal.isGlobalReg()) {
3241 CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal);
3242 } else {
JF Bastien92f4ef12016-04-06 17:26:42 +00003243 CGF.EmitAtomicStore(RVal, LVal,
3244 IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
3245 : llvm::AtomicOrdering::Monotonic,
Alexey Bataev5e018f92015-04-23 06:35:10 +00003246 LVal.isVolatile(), /*IsInit=*/false);
3247 }
3248}
3249
Alexey Bataev8524d152016-01-21 12:35:58 +00003250void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal,
3251 QualType RValTy, SourceLocation Loc) {
3252 switch (getEvaluationKind(LVal.getType())) {
Alexey Bataev5e018f92015-04-23 06:35:10 +00003253 case TEK_Scalar:
Alexey Bataev8524d152016-01-21 12:35:58 +00003254 EmitStoreThroughLValue(RValue::get(convertToScalarValue(
3255 *this, RVal, RValTy, LVal.getType(), Loc)),
3256 LVal);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003257 break;
3258 case TEK_Complex:
Alexey Bataev8524d152016-01-21 12:35:58 +00003259 EmitStoreOfComplex(
3260 convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal,
Alexey Bataev5e018f92015-04-23 06:35:10 +00003261 /*isInit=*/false);
3262 break;
3263 case TEK_Aggregate:
3264 llvm_unreachable("Must be a scalar or complex.");
3265 }
3266}
3267
Alexey Bataevb57056f2015-01-22 06:17:56 +00003268static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst,
3269 const Expr *X, const Expr *V,
3270 SourceLocation Loc) {
3271 // v = x;
3272 assert(V->isLValue() && "V of 'omp atomic read' is not lvalue");
3273 assert(X->isLValue() && "X of 'omp atomic read' is not lvalue");
3274 LValue XLValue = CGF.EmitLValue(X);
3275 LValue VLValue = CGF.EmitLValue(V);
David Majnemera5b195a2015-02-14 01:35:12 +00003276 RValue Res = XLValue.isGlobalReg()
3277 ? CGF.EmitLoadOfLValue(XLValue, Loc)
JF Bastien92f4ef12016-04-06 17:26:42 +00003278 : CGF.EmitAtomicLoad(
3279 XLValue, Loc,
3280 IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
3281 : llvm::AtomicOrdering::Monotonic,
3282 XLValue.isVolatile());
Alexey Bataevb57056f2015-01-22 06:17:56 +00003283 // OpenMP, 2.12.6, atomic Construct
3284 // Any atomic construct with a seq_cst clause forces the atomically
3285 // performed operation to include an implicit flush operation without a
3286 // list.
3287 if (IsSeqCst)
Alexey Bataev3eff5f42015-02-25 08:32:46 +00003288 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
Alexey Bataev8524d152016-01-21 12:35:58 +00003289 CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00003290}
3291
Alexey Bataevb8329262015-02-27 06:33:30 +00003292static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst,
3293 const Expr *X, const Expr *E,
3294 SourceLocation Loc) {
3295 // x = expr;
3296 assert(X->isLValue() && "X of 'omp atomic write' is not lvalue");
Alexey Bataev5e018f92015-04-23 06:35:10 +00003297 emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E));
Alexey Bataevb8329262015-02-27 06:33:30 +00003298 // OpenMP, 2.12.6, atomic Construct
3299 // Any atomic construct with a seq_cst clause forces the atomically
3300 // performed operation to include an implicit flush operation without a
3301 // list.
3302 if (IsSeqCst)
3303 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
3304}
3305
Benjamin Kramer439ee9d2015-05-01 13:59:53 +00003306static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X,
3307 RValue Update,
3308 BinaryOperatorKind BO,
3309 llvm::AtomicOrdering AO,
3310 bool IsXLHSInRHSPart) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003311 auto &Context = CGF.CGM.getContext();
3312 // Allow atomicrmw only if 'x' and 'update' are integer values, lvalue for 'x'
Alexey Bataevb4505a72015-03-30 05:20:59 +00003313 // expression is simple and atomic is allowed for the given type for the
3314 // target platform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003315 if (BO == BO_Comma || !Update.isScalar() ||
Alexey Bataev9d541a72015-05-08 11:47:16 +00003316 !Update.getScalarVal()->getType()->isIntegerTy() ||
3317 !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) &&
3318 (Update.getScalarVal()->getType() !=
John McCall7f416cc2015-09-08 08:05:57 +00003319 X.getAddress().getElementType())) ||
3320 !X.getAddress().getElementType()->isIntegerTy() ||
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003321 !Context.getTargetInfo().hasBuiltinAtomic(
3322 Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment())))
Alexey Bataev5e018f92015-04-23 06:35:10 +00003323 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003324
3325 llvm::AtomicRMWInst::BinOp RMWOp;
3326 switch (BO) {
3327 case BO_Add:
3328 RMWOp = llvm::AtomicRMWInst::Add;
3329 break;
3330 case BO_Sub:
3331 if (!IsXLHSInRHSPart)
Alexey Bataev5e018f92015-04-23 06:35:10 +00003332 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003333 RMWOp = llvm::AtomicRMWInst::Sub;
3334 break;
3335 case BO_And:
3336 RMWOp = llvm::AtomicRMWInst::And;
3337 break;
3338 case BO_Or:
3339 RMWOp = llvm::AtomicRMWInst::Or;
3340 break;
3341 case BO_Xor:
3342 RMWOp = llvm::AtomicRMWInst::Xor;
3343 break;
3344 case BO_LT:
3345 RMWOp = X.getType()->hasSignedIntegerRepresentation()
3346 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min
3347 : llvm::AtomicRMWInst::Max)
3348 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin
3349 : llvm::AtomicRMWInst::UMax);
3350 break;
3351 case BO_GT:
3352 RMWOp = X.getType()->hasSignedIntegerRepresentation()
3353 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max
3354 : llvm::AtomicRMWInst::Min)
3355 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax
3356 : llvm::AtomicRMWInst::UMin);
3357 break;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003358 case BO_Assign:
3359 RMWOp = llvm::AtomicRMWInst::Xchg;
3360 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003361 case BO_Mul:
3362 case BO_Div:
3363 case BO_Rem:
3364 case BO_Shl:
3365 case BO_Shr:
3366 case BO_LAnd:
3367 case BO_LOr:
Alexey Bataev5e018f92015-04-23 06:35:10 +00003368 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003369 case BO_PtrMemD:
3370 case BO_PtrMemI:
3371 case BO_LE:
3372 case BO_GE:
3373 case BO_EQ:
3374 case BO_NE:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003375 case BO_AddAssign:
3376 case BO_SubAssign:
3377 case BO_AndAssign:
3378 case BO_OrAssign:
3379 case BO_XorAssign:
3380 case BO_MulAssign:
3381 case BO_DivAssign:
3382 case BO_RemAssign:
3383 case BO_ShlAssign:
3384 case BO_ShrAssign:
3385 case BO_Comma:
3386 llvm_unreachable("Unsupported atomic update operation");
3387 }
3388 auto *UpdateVal = Update.getScalarVal();
3389 if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) {
3390 UpdateVal = CGF.Builder.CreateIntCast(
John McCall7f416cc2015-09-08 08:05:57 +00003391 IC, X.getAddress().getElementType(),
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003392 X.getType()->hasSignedIntegerRepresentation());
3393 }
John McCall7f416cc2015-09-08 08:05:57 +00003394 auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003395 return std::make_pair(true, RValue::get(Res));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003396}
3397
Alexey Bataev5e018f92015-04-23 06:35:10 +00003398std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr(
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003399 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
3400 llvm::AtomicOrdering AO, SourceLocation Loc,
3401 const llvm::function_ref<RValue(RValue)> &CommonGen) {
3402 // Update expressions are allowed to have the following forms:
3403 // x binop= expr; -> xrval + expr;
3404 // x++, ++x -> xrval + 1;
3405 // x--, --x -> xrval - 1;
3406 // x = x binop expr; -> xrval binop expr
3407 // x = expr Op x; - > expr binop xrval;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003408 auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart);
3409 if (!Res.first) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003410 if (X.isGlobalReg()) {
3411 // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop
3412 // 'xrval'.
3413 EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X);
3414 } else {
3415 // Perform compare-and-swap procedure.
3416 EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified());
Alexey Bataevb4505a72015-03-30 05:20:59 +00003417 }
3418 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00003419 return Res;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003420}
3421
3422static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst,
3423 const Expr *X, const Expr *E,
3424 const Expr *UE, bool IsXLHSInRHSPart,
3425 SourceLocation Loc) {
3426 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
3427 "Update expr in 'atomic update' must be a binary operator.");
3428 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
3429 // Update expressions are allowed to have the following forms:
3430 // x binop= expr; -> xrval + expr;
3431 // x++, ++x -> xrval + 1;
3432 // x--, --x -> xrval - 1;
3433 // x = x binop expr; -> xrval binop expr
3434 // x = expr Op x; - > expr binop xrval;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003435 assert(X->isLValue() && "X of 'omp atomic update' is not lvalue");
Alexey Bataevb4505a72015-03-30 05:20:59 +00003436 LValue XLValue = CGF.EmitLValue(X);
3437 RValue ExprRValue = CGF.EmitAnyExpr(E);
JF Bastien92f4ef12016-04-06 17:26:42 +00003438 auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
3439 : llvm::AtomicOrdering::Monotonic;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00003440 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
3441 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
3442 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
3443 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
3444 auto Gen =
3445 [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue {
3446 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
3447 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
3448 return CGF.EmitAnyExpr(UE);
3449 };
Alexey Bataev5e018f92015-04-23 06:35:10 +00003450 (void)CGF.EmitOMPAtomicSimpleUpdateExpr(
3451 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
3452 // OpenMP, 2.12.6, atomic Construct
3453 // Any atomic construct with a seq_cst clause forces the atomically
3454 // performed operation to include an implicit flush operation without a
3455 // list.
3456 if (IsSeqCst)
3457 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
3458}
3459
3460static RValue convertToType(CodeGenFunction &CGF, RValue Value,
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003461 QualType SourceType, QualType ResType,
3462 SourceLocation Loc) {
Alexey Bataev5e018f92015-04-23 06:35:10 +00003463 switch (CGF.getEvaluationKind(ResType)) {
3464 case TEK_Scalar:
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003465 return RValue::get(
3466 convertToScalarValue(CGF, Value, SourceType, ResType, Loc));
Alexey Bataev5e018f92015-04-23 06:35:10 +00003467 case TEK_Complex: {
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003468 auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003469 return RValue::getComplex(Res.first, Res.second);
3470 }
3471 case TEK_Aggregate:
3472 break;
3473 }
3474 llvm_unreachable("Must be a scalar or complex.");
3475}
3476
3477static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst,
3478 bool IsPostfixUpdate, const Expr *V,
3479 const Expr *X, const Expr *E,
3480 const Expr *UE, bool IsXLHSInRHSPart,
3481 SourceLocation Loc) {
3482 assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue");
3483 assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue");
3484 RValue NewVVal;
3485 LValue VLValue = CGF.EmitLValue(V);
3486 LValue XLValue = CGF.EmitLValue(X);
3487 RValue ExprRValue = CGF.EmitAnyExpr(E);
JF Bastien92f4ef12016-04-06 17:26:42 +00003488 auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
3489 : llvm::AtomicOrdering::Monotonic;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003490 QualType NewVValType;
3491 if (UE) {
3492 // 'x' is updated with some additional value.
3493 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
3494 "Update expr in 'atomic capture' must be a binary operator.");
3495 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
3496 // Update expressions are allowed to have the following forms:
3497 // x binop= expr; -> xrval + expr;
3498 // x++, ++x -> xrval + 1;
3499 // x--, --x -> xrval - 1;
3500 // x = x binop expr; -> xrval binop expr
3501 // x = expr Op x; - > expr binop xrval;
3502 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
3503 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
3504 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
3505 NewVValType = XRValExpr->getType();
3506 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
3507 auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr,
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00003508 IsPostfixUpdate](RValue XRValue) -> RValue {
Alexey Bataev5e018f92015-04-23 06:35:10 +00003509 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
3510 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
3511 RValue Res = CGF.EmitAnyExpr(UE);
3512 NewVVal = IsPostfixUpdate ? XRValue : Res;
3513 return Res;
3514 };
3515 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
3516 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
3517 if (Res.first) {
3518 // 'atomicrmw' instruction was generated.
3519 if (IsPostfixUpdate) {
3520 // Use old value from 'atomicrmw'.
3521 NewVVal = Res.second;
3522 } else {
3523 // 'atomicrmw' does not provide new value, so evaluate it using old
3524 // value of 'x'.
3525 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
3526 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second);
3527 NewVVal = CGF.EmitAnyExpr(UE);
3528 }
3529 }
3530 } else {
3531 // 'x' is simply rewritten with some 'expr'.
3532 NewVValType = X->getType().getNonReferenceType();
3533 ExprRValue = convertToType(CGF, ExprRValue, E->getType(),
Filipe Cabecinhas7af183d2015-08-11 04:19:28 +00003534 X->getType().getNonReferenceType(), Loc);
Malcolm Parsonsc6e45832017-01-13 18:55:32 +00003535 auto &&Gen = [&NewVVal, ExprRValue](RValue XRValue) -> RValue {
Alexey Bataev5e018f92015-04-23 06:35:10 +00003536 NewVVal = XRValue;
3537 return ExprRValue;
3538 };
3539 // Try to perform atomicrmw xchg, otherwise simple exchange.
3540 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
3541 XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO,
3542 Loc, Gen);
3543 if (Res.first) {
3544 // 'atomicrmw' instruction was generated.
3545 NewVVal = IsPostfixUpdate ? Res.second : ExprRValue;
3546 }
3547 }
3548 // Emit post-update store to 'v' of old/new 'x' value.
Alexey Bataev8524d152016-01-21 12:35:58 +00003549 CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc);
Alexey Bataevb4505a72015-03-30 05:20:59 +00003550 // OpenMP, 2.12.6, atomic Construct
3551 // Any atomic construct with a seq_cst clause forces the atomically
3552 // performed operation to include an implicit flush operation without a
3553 // list.
3554 if (IsSeqCst)
3555 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
3556}
3557
Alexey Bataevb57056f2015-01-22 06:17:56 +00003558static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind,
Alexey Bataev5e018f92015-04-23 06:35:10 +00003559 bool IsSeqCst, bool IsPostfixUpdate,
3560 const Expr *X, const Expr *V, const Expr *E,
3561 const Expr *UE, bool IsXLHSInRHSPart,
3562 SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00003563 switch (Kind) {
3564 case OMPC_read:
3565 EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc);
3566 break;
3567 case OMPC_write:
Alexey Bataevb8329262015-02-27 06:33:30 +00003568 EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc);
3569 break;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003570 case OMPC_unknown:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003571 case OMPC_update:
Alexey Bataevb4505a72015-03-30 05:20:59 +00003572 EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc);
3573 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00003574 case OMPC_capture:
Alexey Bataev5e018f92015-04-23 06:35:10 +00003575 EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE,
3576 IsXLHSInRHSPart, Loc);
3577 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00003578 case OMPC_if:
3579 case OMPC_final:
3580 case OMPC_num_threads:
3581 case OMPC_private:
3582 case OMPC_firstprivate:
3583 case OMPC_lastprivate:
3584 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003585 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00003586 case OMPC_in_reduction:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003587 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00003588 case OMPC_simdlen:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003589 case OMPC_collapse:
3590 case OMPC_default:
3591 case OMPC_seq_cst:
3592 case OMPC_shared:
3593 case OMPC_linear:
3594 case OMPC_aligned:
3595 case OMPC_copyin:
3596 case OMPC_copyprivate:
3597 case OMPC_flush:
3598 case OMPC_proc_bind:
3599 case OMPC_schedule:
3600 case OMPC_ordered:
3601 case OMPC_nowait:
3602 case OMPC_untied:
3603 case OMPC_threadprivate:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00003604 case OMPC_depend:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003605 case OMPC_mergeable:
Michael Wonge710d542015-08-07 16:16:36 +00003606 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00003607 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003608 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00003609 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00003610 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00003611 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00003612 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00003613 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00003614 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00003615 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00003616 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00003617 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00003618 case OMPC_defaultmap:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003619 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00003620 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00003621 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00003622 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00003623 case OMPC_is_device_ptr:
Alexey Bataevb57056f2015-01-22 06:17:56 +00003624 llvm_unreachable("Clause is not allowed in 'omp atomic'.");
3625 }
3626}
3627
3628void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003629 bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>();
Alexey Bataevb57056f2015-01-22 06:17:56 +00003630 OpenMPClauseKind Kind = OMPC_unknown;
3631 for (auto *C : S.clauses()) {
3632 // Find first clause (skip seq_cst clause, if it is first).
3633 if (C->getClauseKind() != OMPC_seq_cst) {
3634 Kind = C->getClauseKind();
3635 break;
3636 }
3637 }
Alexey Bataev10fec572015-03-11 04:48:56 +00003638
3639 const auto *CS =
3640 S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003641 if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) {
Alexey Bataev10fec572015-03-11 04:48:56 +00003642 enterFullExpression(EWC);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003643 }
3644 // Processing for statements under 'atomic capture'.
3645 if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) {
3646 for (const auto *C : Compound->body()) {
3647 if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) {
3648 enterFullExpression(EWC);
3649 }
3650 }
3651 }
Alexey Bataev10fec572015-03-11 04:48:56 +00003652
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003653 auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF,
3654 PrePostActionTy &) {
Alexey Bataev33c56402015-12-14 09:26:19 +00003655 CGF.EmitStopPoint(CS);
Alexey Bataev5e018f92015-04-23 06:35:10 +00003656 EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(),
3657 S.getV(), S.getExpr(), S.getUpdateExpr(),
3658 S.isXLHSInRHSPart(), S.getLocStart());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00003659 };
Alexey Bataev4ba78a42016-04-27 07:56:03 +00003660 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003661 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen);
Alexey Bataev0162e452014-07-22 10:10:35 +00003662}
3663
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003664static void emitCommonOMPTargetDirective(CodeGenFunction &CGF,
3665 const OMPExecutableDirective &S,
3666 const RegionCodeGenTy &CodeGen) {
3667 assert(isOpenMPTargetExecutionDirective(S.getDirectiveKind()));
3668 CodeGenModule &CGM = CGF.CGM;
Samuel Antaobed3c462015-10-02 16:14:20 +00003669 const CapturedStmt &CS = *cast<CapturedStmt>(S.getAssociatedStmt());
3670
Samuel Antaoee8fb302016-01-06 13:42:12 +00003671 llvm::Function *Fn = nullptr;
3672 llvm::Constant *FnID = nullptr;
Samuel Antaobed3c462015-10-02 16:14:20 +00003673
Samuel Antaobed3c462015-10-02 16:14:20 +00003674 const Expr *IfCond = nullptr;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003675 // Check for the at most one if clause associated with the target region.
3676 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
3677 if (C->getNameModifier() == OMPD_unknown ||
3678 C->getNameModifier() == OMPD_target) {
3679 IfCond = C->getCondition();
3680 break;
3681 }
Samuel Antaobed3c462015-10-02 16:14:20 +00003682 }
3683
3684 // Check if we have any device clause associated with the directive.
3685 const Expr *Device = nullptr;
3686 if (auto *C = S.getSingleClause<OMPDeviceClause>()) {
3687 Device = C->getDevice();
3688 }
3689
Samuel Antaoee8fb302016-01-06 13:42:12 +00003690 // Check if we have an if clause whose conditional always evaluates to false
3691 // or if we do not have any targets specified. If so the target region is not
3692 // an offload entry point.
3693 bool IsOffloadEntry = true;
3694 if (IfCond) {
3695 bool Val;
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003696 if (CGF.ConstantFoldsToSimpleInteger(IfCond, Val) && !Val)
Samuel Antaoee8fb302016-01-06 13:42:12 +00003697 IsOffloadEntry = false;
3698 }
3699 if (CGM.getLangOpts().OMPTargetTriples.empty())
3700 IsOffloadEntry = false;
3701
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003702 assert(CGF.CurFuncDecl && "No parent declaration for target region!");
Samuel Antaoee8fb302016-01-06 13:42:12 +00003703 StringRef ParentName;
3704 // In case we have Ctors/Dtors we use the complete type variant to produce
3705 // the mangling of the device outlined kernel.
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003706 if (auto *D = dyn_cast<CXXConstructorDecl>(CGF.CurFuncDecl))
Samuel Antaoee8fb302016-01-06 13:42:12 +00003707 ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete));
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003708 else if (auto *D = dyn_cast<CXXDestructorDecl>(CGF.CurFuncDecl))
Samuel Antaoee8fb302016-01-06 13:42:12 +00003709 ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete));
3710 else
3711 ParentName =
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003712 CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CGF.CurFuncDecl)));
Samuel Antaoee8fb302016-01-06 13:42:12 +00003713
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003714 // Emit target region as a standalone region.
3715 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(S, ParentName, Fn, FnID,
3716 IsOffloadEntry, CodeGen);
3717 OMPLexicalScope Scope(CGF, S);
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003718 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
3719 CGF.GenerateOpenMPCapturedVars(CS, CapturedVars);
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003720 CGM.getOpenMPRuntime().emitTargetCall(CGF, S, Fn, FnID, IfCond, Device,
Samuel Antaobed3c462015-10-02 16:14:20 +00003721 CapturedVars);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003722}
3723
Arpith Chacko Jacob43a8b7b2017-01-16 15:26:02 +00003724static void emitTargetRegion(CodeGenFunction &CGF, const OMPTargetDirective &S,
3725 PrePostActionTy &Action) {
3726 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
3727 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
3728 CGF.EmitOMPPrivateClause(S, PrivateScope);
3729 (void)PrivateScope.Privatize();
3730
3731 Action.Enter(CGF);
3732 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3733}
3734
3735void CodeGenFunction::EmitOMPTargetDeviceFunction(CodeGenModule &CGM,
3736 StringRef ParentName,
3737 const OMPTargetDirective &S) {
3738 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3739 emitTargetRegion(CGF, S, Action);
3740 };
3741 llvm::Function *Fn;
3742 llvm::Constant *Addr;
3743 // Emit target region as a standalone region.
3744 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
3745 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
3746 assert(Fn && Addr && "Target device function emission failed.");
3747}
3748
3749void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) {
3750 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3751 emitTargetRegion(CGF, S, Action);
3752 };
3753 emitCommonOMPTargetDirective(*this, S, CodeGen);
3754}
3755
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003756static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF,
3757 const OMPExecutableDirective &S,
3758 OpenMPDirectiveKind InnermostKind,
3759 const RegionCodeGenTy &CodeGen) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003760 const CapturedStmt *CS = S.getCapturedStmt(OMPD_teams);
3761 auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitTeamsOutlinedFunction(
3762 S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
Samuel Antaob68e2db2016-03-03 16:20:23 +00003763
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003764 const OMPNumTeamsClause *NT = S.getSingleClause<OMPNumTeamsClause>();
3765 const OMPThreadLimitClause *TL = S.getSingleClause<OMPThreadLimitClause>();
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003766 if (NT || TL) {
Carlo Bertollic6872252016-04-04 15:55:02 +00003767 Expr *NumTeams = (NT) ? NT->getNumTeams() : nullptr;
3768 Expr *ThreadLimit = (TL) ? TL->getThreadLimit() : nullptr;
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003769
Carlo Bertollic6872252016-04-04 15:55:02 +00003770 CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit,
3771 S.getLocStart());
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003772 }
3773
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003774 OMPTeamsScope Scope(CGF, S);
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003775 llvm::SmallVector<llvm::Value *, 16> CapturedVars;
3776 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003777 CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getLocStart(), OutlinedFn,
3778 CapturedVars);
3779}
3780
3781void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) {
Kelvin Li51336dd2016-12-15 17:55:32 +00003782 // Emit teams region as a standalone region.
Alexey Bataev14fa1c62016-03-29 05:34:15 +00003783 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003784 OMPPrivateScope PrivateScope(CGF);
Carlo Bertolli6ad7b5a2016-03-03 22:09:40 +00003785 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
3786 CGF.EmitOMPPrivateClause(S, PrivateScope);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003787 CGF.EmitOMPReductionClauseInit(S, PrivateScope);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003788 (void)PrivateScope.Privatize();
3789 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003790 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams);
Carlo Bertolli430d8ec2016-03-03 20:34:23 +00003791 };
3792 emitCommonOMPTeamsDirective(*this, S, OMPD_teams, CodeGen);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003793 emitPostUpdateForReductionClause(
3794 *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Alexey Bataev13314bf2014-10-09 04:18:56 +00003795}
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003796
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00003797static void emitTargetTeamsRegion(CodeGenFunction &CGF, PrePostActionTy &Action,
3798 const OMPTargetTeamsDirective &S) {
3799 auto *CS = S.getCapturedStmt(OMPD_teams);
3800 Action.Enter(CGF);
3801 auto &&CodeGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) {
3802 // TODO: Add support for clauses.
3803 CGF.EmitStmt(CS->getCapturedStmt());
3804 };
3805 emitCommonOMPTeamsDirective(CGF, S, OMPD_teams, CodeGen);
3806}
3807
3808void CodeGenFunction::EmitOMPTargetTeamsDeviceFunction(
3809 CodeGenModule &CGM, StringRef ParentName,
3810 const OMPTargetTeamsDirective &S) {
3811 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3812 emitTargetTeamsRegion(CGF, Action, S);
3813 };
3814 llvm::Function *Fn;
3815 llvm::Constant *Addr;
3816 // Emit target region as a standalone region.
3817 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
3818 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
3819 assert(Fn && Addr && "Target device function emission failed.");
3820}
3821
3822void CodeGenFunction::EmitOMPTargetTeamsDirective(
3823 const OMPTargetTeamsDirective &S) {
3824 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3825 emitTargetTeamsRegion(CGF, Action, S);
3826 };
3827 emitCommonOMPTargetDirective(*this, S, CodeGen);
3828}
3829
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003830void CodeGenFunction::EmitOMPCancellationPointDirective(
3831 const OMPCancellationPointDirective &S) {
Alexey Bataev0f34da12015-07-02 04:17:07 +00003832 CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getLocStart(),
3833 S.getCancelRegion());
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003834}
3835
Alexey Bataev80909872015-07-02 11:25:17 +00003836void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) {
Alexey Bataev87933c72015-09-18 08:07:34 +00003837 const Expr *IfCond = nullptr;
3838 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
3839 if (C->getNameModifier() == OMPD_unknown ||
3840 C->getNameModifier() == OMPD_cancel) {
3841 IfCond = C->getCondition();
3842 break;
3843 }
3844 }
3845 CGM.getOpenMPRuntime().emitCancelCall(*this, S.getLocStart(), IfCond,
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00003846 S.getCancelRegion());
Alexey Bataev80909872015-07-02 11:25:17 +00003847}
3848
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003849CodeGenFunction::JumpDest
3850CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) {
Alexey Bataev957d8562016-11-17 15:12:05 +00003851 if (Kind == OMPD_parallel || Kind == OMPD_task ||
3852 Kind == OMPD_target_parallel)
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003853 return ReturnBlock;
Alexey Bataev25e5b442015-09-15 12:52:43 +00003854 assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections ||
Alexey Bataev957d8562016-11-17 15:12:05 +00003855 Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for ||
3856 Kind == OMPD_distribute_parallel_for ||
3857 Kind == OMPD_target_parallel_for);
3858 return OMPCancelStack.getExitBlock();
Alexey Bataev81c7ea02015-07-03 09:56:58 +00003859}
Michael Wong65f367f2015-07-21 13:44:28 +00003860
Samuel Antaocc10b852016-07-28 14:23:26 +00003861void CodeGenFunction::EmitOMPUseDevicePtrClause(
3862 const OMPClause &NC, OMPPrivateScope &PrivateScope,
3863 const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap) {
3864 const auto &C = cast<OMPUseDevicePtrClause>(NC);
3865 auto OrigVarIt = C.varlist_begin();
3866 auto InitIt = C.inits().begin();
3867 for (auto PvtVarIt : C.private_copies()) {
3868 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*OrigVarIt)->getDecl());
3869 auto *InitVD = cast<VarDecl>(cast<DeclRefExpr>(*InitIt)->getDecl());
3870 auto *PvtVD = cast<VarDecl>(cast<DeclRefExpr>(PvtVarIt)->getDecl());
3871
3872 // In order to identify the right initializer we need to match the
3873 // declaration used by the mapping logic. In some cases we may get
3874 // OMPCapturedExprDecl that refers to the original declaration.
3875 const ValueDecl *MatchingVD = OrigVD;
3876 if (auto *OED = dyn_cast<OMPCapturedExprDecl>(MatchingVD)) {
3877 // OMPCapturedExprDecl are used to privative fields of the current
3878 // structure.
3879 auto *ME = cast<MemberExpr>(OED->getInit());
3880 assert(isa<CXXThisExpr>(ME->getBase()) &&
3881 "Base should be the current struct!");
3882 MatchingVD = ME->getMemberDecl();
3883 }
3884
3885 // If we don't have information about the current list item, move on to
3886 // the next one.
3887 auto InitAddrIt = CaptureDeviceAddrMap.find(MatchingVD);
3888 if (InitAddrIt == CaptureDeviceAddrMap.end())
3889 continue;
3890
3891 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
3892 // Initialize the temporary initialization variable with the address we
3893 // get from the runtime library. We have to cast the source address
3894 // because it is always a void *. References are materialized in the
3895 // privatization scope, so the initialization here disregards the fact
3896 // the original variable is a reference.
3897 QualType AddrQTy =
3898 getContext().getPointerType(OrigVD->getType().getNonReferenceType());
3899 llvm::Type *AddrTy = ConvertTypeForMem(AddrQTy);
3900 Address InitAddr = Builder.CreateBitCast(InitAddrIt->second, AddrTy);
3901 setAddrOfLocalVar(InitVD, InitAddr);
3902
3903 // Emit private declaration, it will be initialized by the value we
3904 // declaration we just added to the local declarations map.
3905 EmitDecl(*PvtVD);
3906
3907 // The initialization variables reached its purpose in the emission
3908 // ofthe previous declaration, so we don't need it anymore.
3909 LocalDeclMap.erase(InitVD);
3910
3911 // Return the address of the private variable.
3912 return GetAddrOfLocalVar(PvtVD);
3913 });
3914 assert(IsRegistered && "firstprivate var already registered as private");
3915 // Silence the warning about unused variable.
3916 (void)IsRegistered;
3917
3918 ++OrigVarIt;
3919 ++InitIt;
3920 }
3921}
3922
Michael Wong65f367f2015-07-21 13:44:28 +00003923// Generate the instructions for '#pragma omp target data' directive.
3924void CodeGenFunction::EmitOMPTargetDataDirective(
3925 const OMPTargetDataDirective &S) {
Samuel Antaocc10b852016-07-28 14:23:26 +00003926 CGOpenMPRuntime::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true);
3927
3928 // Create a pre/post action to signal the privatization of the device pointer.
3929 // This action can be replaced by the OpenMP runtime code generation to
3930 // deactivate privatization.
3931 bool PrivatizeDevicePointers = false;
3932 class DevicePointerPrivActionTy : public PrePostActionTy {
3933 bool &PrivatizeDevicePointers;
3934
3935 public:
3936 explicit DevicePointerPrivActionTy(bool &PrivatizeDevicePointers)
3937 : PrePostActionTy(), PrivatizeDevicePointers(PrivatizeDevicePointers) {}
3938 void Enter(CodeGenFunction &CGF) override {
3939 PrivatizeDevicePointers = true;
3940 }
Samuel Antaodf158d52016-04-27 22:58:19 +00003941 };
Samuel Antaocc10b852016-07-28 14:23:26 +00003942 DevicePointerPrivActionTy PrivAction(PrivatizeDevicePointers);
3943
3944 auto &&CodeGen = [&S, &Info, &PrivatizeDevicePointers](
3945 CodeGenFunction &CGF, PrePostActionTy &Action) {
3946 auto &&InnermostCodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
3947 CGF.EmitStmt(
3948 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3949 };
3950
3951 // Codegen that selects wheather to generate the privatization code or not.
3952 auto &&PrivCodeGen = [&S, &Info, &PrivatizeDevicePointers,
3953 &InnermostCodeGen](CodeGenFunction &CGF,
3954 PrePostActionTy &Action) {
3955 RegionCodeGenTy RCG(InnermostCodeGen);
3956 PrivatizeDevicePointers = false;
3957
3958 // Call the pre-action to change the status of PrivatizeDevicePointers if
3959 // needed.
3960 Action.Enter(CGF);
3961
3962 if (PrivatizeDevicePointers) {
3963 OMPPrivateScope PrivateScope(CGF);
3964 // Emit all instances of the use_device_ptr clause.
3965 for (const auto *C : S.getClausesOfKind<OMPUseDevicePtrClause>())
3966 CGF.EmitOMPUseDevicePtrClause(*C, PrivateScope,
3967 Info.CaptureDeviceAddrMap);
3968 (void)PrivateScope.Privatize();
3969 RCG(CGF);
3970 } else
3971 RCG(CGF);
3972 };
3973
3974 // Forward the provided action to the privatization codegen.
3975 RegionCodeGenTy PrivRCG(PrivCodeGen);
3976 PrivRCG.setAction(Action);
3977
3978 // Notwithstanding the body of the region is emitted as inlined directive,
3979 // we don't use an inline scope as changes in the references inside the
3980 // region are expected to be visible outside, so we do not privative them.
3981 OMPLexicalScope Scope(CGF, S);
3982 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_target_data,
3983 PrivRCG);
3984 };
3985
3986 RegionCodeGenTy RCG(CodeGen);
Samuel Antaodf158d52016-04-27 22:58:19 +00003987
3988 // If we don't have target devices, don't bother emitting the data mapping
3989 // code.
3990 if (CGM.getLangOpts().OMPTargetTriples.empty()) {
Samuel Antaocc10b852016-07-28 14:23:26 +00003991 RCG(*this);
Samuel Antaodf158d52016-04-27 22:58:19 +00003992 return;
3993 }
3994
3995 // Check if we have any if clause associated with the directive.
3996 const Expr *IfCond = nullptr;
3997 if (auto *C = S.getSingleClause<OMPIfClause>())
3998 IfCond = C->getCondition();
3999
4000 // Check if we have any device clause associated with the directive.
4001 const Expr *Device = nullptr;
4002 if (auto *C = S.getSingleClause<OMPDeviceClause>())
4003 Device = C->getDevice();
4004
Samuel Antaocc10b852016-07-28 14:23:26 +00004005 // Set the action to signal privatization of device pointers.
4006 RCG.setAction(PrivAction);
4007
4008 // Emit region code.
4009 CGM.getOpenMPRuntime().emitTargetDataCalls(*this, S, IfCond, Device, RCG,
4010 Info);
Michael Wong65f367f2015-07-21 13:44:28 +00004011}
Alexey Bataev49f6e782015-12-01 04:18:41 +00004012
Samuel Antaodf67fc42016-01-19 19:15:56 +00004013void CodeGenFunction::EmitOMPTargetEnterDataDirective(
4014 const OMPTargetEnterDataDirective &S) {
Samuel Antaobd0ae2e2016-04-27 23:07:29 +00004015 // If we don't have target devices, don't bother emitting the data mapping
4016 // code.
4017 if (CGM.getLangOpts().OMPTargetTriples.empty())
4018 return;
4019
4020 // Check if we have any if clause associated with the directive.
4021 const Expr *IfCond = nullptr;
4022 if (auto *C = S.getSingleClause<OMPIfClause>())
4023 IfCond = C->getCondition();
4024
4025 // Check if we have any device clause associated with the directive.
4026 const Expr *Device = nullptr;
4027 if (auto *C = S.getSingleClause<OMPDeviceClause>())
4028 Device = C->getDevice();
4029
Samuel Antao8d2d7302016-05-26 18:30:22 +00004030 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
Samuel Antaodf67fc42016-01-19 19:15:56 +00004031}
4032
Samuel Antao72590762016-01-19 20:04:50 +00004033void CodeGenFunction::EmitOMPTargetExitDataDirective(
4034 const OMPTargetExitDataDirective &S) {
Samuel Antao8dd66282016-04-27 23:14:30 +00004035 // If we don't have target devices, don't bother emitting the data mapping
4036 // code.
4037 if (CGM.getLangOpts().OMPTargetTriples.empty())
4038 return;
4039
4040 // Check if we have any if clause associated with the directive.
4041 const Expr *IfCond = nullptr;
4042 if (auto *C = S.getSingleClause<OMPIfClause>())
4043 IfCond = C->getCondition();
4044
4045 // Check if we have any device clause associated with the directive.
4046 const Expr *Device = nullptr;
4047 if (auto *C = S.getSingleClause<OMPDeviceClause>())
4048 Device = C->getDevice();
4049
Samuel Antao8d2d7302016-05-26 18:30:22 +00004050 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
Samuel Antao72590762016-01-19 20:04:50 +00004051}
4052
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004053static void emitTargetParallelRegion(CodeGenFunction &CGF,
4054 const OMPTargetParallelDirective &S,
4055 PrePostActionTy &Action) {
4056 // Get the captured statement associated with the 'parallel' region.
4057 auto *CS = S.getCapturedStmt(OMPD_parallel);
4058 Action.Enter(CGF);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00004059 auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &) {
4060 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
4061 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
4062 CGF.EmitOMPPrivateClause(S, PrivateScope);
4063 CGF.EmitOMPReductionClauseInit(S, PrivateScope);
4064 (void)PrivateScope.Privatize();
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004065 // TODO: Add support for clauses.
4066 CGF.EmitStmt(CS->getCapturedStmt());
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00004067 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004068 };
Carlo Bertollib0ff0a62017-04-25 17:52:12 +00004069 emitCommonOMPParallelDirective(CGF, S, OMPD_parallel, CodeGen,
4070 emitEmptyBoundParameters);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00004071 emitPostUpdateForReductionClause(
4072 CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004073}
4074
4075void CodeGenFunction::EmitOMPTargetParallelDeviceFunction(
4076 CodeGenModule &CGM, StringRef ParentName,
4077 const OMPTargetParallelDirective &S) {
4078 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
4079 emitTargetParallelRegion(CGF, S, Action);
4080 };
4081 llvm::Function *Fn;
4082 llvm::Constant *Addr;
4083 // Emit target region as a standalone region.
4084 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
4085 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
4086 assert(Fn && Addr && "Target device function emission failed.");
4087}
4088
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00004089void CodeGenFunction::EmitOMPTargetParallelDirective(
4090 const OMPTargetParallelDirective &S) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00004091 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
4092 emitTargetParallelRegion(CGF, S, Action);
4093 };
4094 emitCommonOMPTargetDirective(*this, S, CodeGen);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00004095}
4096
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00004097void CodeGenFunction::EmitOMPTargetParallelForDirective(
4098 const OMPTargetParallelForDirective &S) {
4099 // TODO: codegen for target parallel for.
4100}
4101
Alexey Bataev7292c292016-04-25 12:22:29 +00004102/// Emit a helper variable and return corresponding lvalue.
4103static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper,
4104 const ImplicitParamDecl *PVD,
4105 CodeGenFunction::OMPPrivateScope &Privates) {
4106 auto *VDecl = cast<VarDecl>(Helper->getDecl());
4107 Privates.addPrivate(
4108 VDecl, [&CGF, PVD]() -> Address { return CGF.GetAddrOfLocalVar(PVD); });
4109}
4110
4111void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) {
4112 assert(isOpenMPTaskLoopDirective(S.getDirectiveKind()));
4113 // Emit outlined function for task construct.
4114 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
4115 auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
4116 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
4117 const Expr *IfCond = nullptr;
4118 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
4119 if (C->getNameModifier() == OMPD_unknown ||
4120 C->getNameModifier() == OMPD_taskloop) {
4121 IfCond = C->getCondition();
4122 break;
4123 }
4124 }
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004125
4126 OMPTaskDataTy Data;
4127 // Check if taskloop must be emitted without taskgroup.
4128 Data.Nogroup = S.getSingleClause<OMPNogroupClause>();
Alexey Bataev7292c292016-04-25 12:22:29 +00004129 // TODO: Check if we should emit tied or untied task.
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004130 Data.Tied = true;
4131 // Set scheduling for taskloop
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00004132 if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) {
4133 // grainsize clause
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004134 Data.Schedule.setInt(/*IntVal=*/false);
4135 Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize()));
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00004136 } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) {
4137 // num_tasks clause
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004138 Data.Schedule.setInt(/*IntVal=*/true);
4139 Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks()));
Alexey Bataev2b19a6f2016-04-28 09:15:06 +00004140 }
Alexey Bataev7292c292016-04-25 12:22:29 +00004141
4142 auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) {
4143 // if (PreCond) {
4144 // for (IV in 0..LastIteration) BODY;
4145 // <Final counter/linear vars updates>;
4146 // }
4147 //
4148
4149 // Emit: if (PreCond) - begin.
4150 // If the condition constant folds and can be elided, avoid emitting the
4151 // whole loop.
4152 bool CondConstant;
4153 llvm::BasicBlock *ContBlock = nullptr;
4154 OMPLoopScope PreInitScope(CGF, S);
4155 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
4156 if (!CondConstant)
4157 return;
4158 } else {
4159 auto *ThenBlock = CGF.createBasicBlock("taskloop.if.then");
4160 ContBlock = CGF.createBasicBlock("taskloop.if.end");
4161 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
4162 CGF.getProfileCount(&S));
4163 CGF.EmitBlock(ThenBlock);
4164 CGF.incrementProfileCounter(&S);
4165 }
4166
Alexey Bataev1e73ef32016-04-28 12:14:51 +00004167 if (isOpenMPSimdDirective(S.getDirectiveKind()))
4168 CGF.EmitOMPSimdInit(S);
4169
Alexey Bataev7292c292016-04-25 12:22:29 +00004170 OMPPrivateScope LoopScope(CGF);
4171 // Emit helper vars inits.
4172 enum { LowerBound = 5, UpperBound, Stride, LastIter };
4173 auto *I = CS->getCapturedDecl()->param_begin();
4174 auto *LBP = std::next(I, LowerBound);
4175 auto *UBP = std::next(I, UpperBound);
4176 auto *STP = std::next(I, Stride);
4177 auto *LIP = std::next(I, LastIter);
4178 mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP,
4179 LoopScope);
4180 mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP,
4181 LoopScope);
4182 mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope);
4183 mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP,
4184 LoopScope);
4185 CGF.EmitOMPPrivateLoopCounters(S, LoopScope);
Alexey Bataevf93095a2016-05-05 08:46:22 +00004186 bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev7292c292016-04-25 12:22:29 +00004187 (void)LoopScope.Privatize();
4188 // Emit the loop iteration variable.
4189 const Expr *IVExpr = S.getIterationVariable();
4190 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
4191 CGF.EmitVarDecl(*IVDecl);
4192 CGF.EmitIgnoredExpr(S.getInit());
4193
4194 // Emit the iterations count variable.
4195 // If it is not a variable, Sema decided to calculate iterations count on
4196 // each iteration (e.g., it is foldable into a constant).
4197 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
4198 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
4199 // Emit calculation of the iterations count.
4200 CGF.EmitIgnoredExpr(S.getCalcLastIteration());
4201 }
4202
4203 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
4204 S.getInc(),
4205 [&S](CodeGenFunction &CGF) {
4206 CGF.EmitOMPLoopBody(S, JumpDest());
4207 CGF.EmitStopPoint(&S);
4208 },
4209 [](CodeGenFunction &) {});
4210 // Emit: if (PreCond) - end.
4211 if (ContBlock) {
4212 CGF.EmitBranch(ContBlock);
4213 CGF.EmitBlock(ContBlock, true);
4214 }
Alexey Bataevf93095a2016-05-05 08:46:22 +00004215 // Emit final copy of the lastprivate variables if IsLastIter != 0.
4216 if (HasLastprivateClause) {
4217 CGF.EmitOMPLastprivateClauseFinal(
4218 S, isOpenMPSimdDirective(S.getDirectiveKind()),
4219 CGF.Builder.CreateIsNotNull(CGF.EmitLoadOfScalar(
4220 CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false,
4221 (*LIP)->getType(), S.getLocStart())));
4222 }
Alexey Bataev7292c292016-04-25 12:22:29 +00004223 };
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004224 auto &&TaskGen = [&S, SharedsTy, CapturedStruct,
4225 IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn,
4226 const OMPTaskDataTy &Data) {
Alexey Bataev7292c292016-04-25 12:22:29 +00004227 auto &&CodeGen = [&](CodeGenFunction &CGF, PrePostActionTy &) {
4228 OMPLoopScope PreInitScope(CGF, S);
Alexey Bataev24b5bae2016-04-28 09:23:51 +00004229 CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getLocStart(), S,
4230 OutlinedFn, SharedsTy,
4231 CapturedStruct, IfCond, Data);
Alexey Bataev7292c292016-04-25 12:22:29 +00004232 };
4233 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop,
4234 CodeGen);
4235 };
Alexey Bataev33446032017-07-12 18:09:32 +00004236 if (Data.Nogroup)
4237 EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data);
4238 else {
4239 CGM.getOpenMPRuntime().emitTaskgroupRegion(
4240 *this,
4241 [&S, &BodyGen, &TaskGen, &Data](CodeGenFunction &CGF,
4242 PrePostActionTy &Action) {
4243 Action.Enter(CGF);
4244 CGF.EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data);
4245 },
4246 S.getLocStart());
4247 }
Alexey Bataev7292c292016-04-25 12:22:29 +00004248}
4249
Alexey Bataev49f6e782015-12-01 04:18:41 +00004250void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) {
Alexey Bataev7292c292016-04-25 12:22:29 +00004251 EmitOMPTaskLoopBasedDirective(S);
Alexey Bataev49f6e782015-12-01 04:18:41 +00004252}
4253
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004254void CodeGenFunction::EmitOMPTaskLoopSimdDirective(
4255 const OMPTaskLoopSimdDirective &S) {
Alexey Bataev1e73ef32016-04-28 12:14:51 +00004256 EmitOMPTaskLoopBasedDirective(S);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004257}
Samuel Antao686c70c2016-05-26 17:30:50 +00004258
4259// Generate the instructions for '#pragma omp target update' directive.
4260void CodeGenFunction::EmitOMPTargetUpdateDirective(
4261 const OMPTargetUpdateDirective &S) {
Samuel Antao8d2d7302016-05-26 18:30:22 +00004262 // If we don't have target devices, don't bother emitting the data mapping
4263 // code.
4264 if (CGM.getLangOpts().OMPTargetTriples.empty())
4265 return;
4266
4267 // Check if we have any if clause associated with the directive.
4268 const Expr *IfCond = nullptr;
4269 if (auto *C = S.getSingleClause<OMPIfClause>())
4270 IfCond = C->getCondition();
4271
4272 // Check if we have any device clause associated with the directive.
4273 const Expr *Device = nullptr;
4274 if (auto *C = S.getSingleClause<OMPDeviceClause>())
4275 Device = C->getDevice();
4276
4277 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
Samuel Antao686c70c2016-05-26 17:30:50 +00004278}