blob: 34e075925fd5f965a4094feffec904a99b383a02 [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
14#include "CGOpenMPRuntime.h"
15#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000017#include "TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000018#include "clang/AST/Stmt.h"
19#include "clang/AST/StmtOpenMP.h"
20using namespace clang;
21using namespace CodeGen;
22
23//===----------------------------------------------------------------------===//
24// OpenMP Directive Emission
25//===----------------------------------------------------------------------===//
Alexey Bataev420d45b2015-04-14 05:11:24 +000026void CodeGenFunction::EmitOMPAggregateAssign(
27 llvm::Value *DestAddr, llvm::Value *SrcAddr, QualType OriginalType,
28 const llvm::function_ref<void(llvm::Value *, llvm::Value *)> &CopyGen) {
29 // Perform element-by-element initialization.
30 QualType ElementTy;
31 auto SrcBegin = SrcAddr;
32 auto DestBegin = DestAddr;
33 auto ArrayTy = OriginalType->getAsArrayTypeUnsafe();
34 auto NumElements = emitArrayLength(ArrayTy, ElementTy, DestBegin);
35 // Cast from pointer to array type to pointer to single element.
36 SrcBegin = Builder.CreatePointerBitCastOrAddrSpaceCast(SrcBegin,
37 DestBegin->getType());
38 auto DestEnd = Builder.CreateGEP(DestBegin, NumElements);
39 // The basic structure here is a while-do loop.
40 auto BodyBB = createBasicBlock("omp.arraycpy.body");
41 auto DoneBB = createBasicBlock("omp.arraycpy.done");
42 auto IsEmpty =
43 Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty");
44 Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000045
Alexey Bataev420d45b2015-04-14 05:11:24 +000046 // Enter the loop body, making that address the current address.
47 auto EntryBB = Builder.GetInsertBlock();
48 EmitBlock(BodyBB);
49 auto SrcElementCurrent =
50 Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast");
51 SrcElementCurrent->addIncoming(SrcBegin, EntryBB);
52 auto DestElementCurrent = Builder.CreatePHI(DestBegin->getType(), 2,
53 "omp.arraycpy.destElementPast");
54 DestElementCurrent->addIncoming(DestBegin, EntryBB);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000055
Alexey Bataev420d45b2015-04-14 05:11:24 +000056 // Emit copy.
57 CopyGen(DestElementCurrent, SrcElementCurrent);
58
59 // Shift the address forward by one element.
60 auto DestElementNext = Builder.CreateConstGEP1_32(
61 DestElementCurrent, /*Idx0=*/1, "omp.arraycpy.dest.element");
62 auto SrcElementNext = Builder.CreateConstGEP1_32(
63 SrcElementCurrent, /*Idx0=*/1, "omp.arraycpy.src.element");
64 // Check whether we've reached the end.
65 auto Done =
66 Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
67 Builder.CreateCondBr(Done, DoneBB, BodyBB);
68 DestElementCurrent->addIncoming(DestElementNext, Builder.GetInsertBlock());
69 SrcElementCurrent->addIncoming(SrcElementNext, Builder.GetInsertBlock());
70
71 // Done.
72 EmitBlock(DoneBB, /*IsFinished=*/true);
73}
74
75void CodeGenFunction::EmitOMPCopy(CodeGenFunction &CGF,
76 QualType OriginalType, llvm::Value *DestAddr,
77 llvm::Value *SrcAddr, const VarDecl *DestVD,
78 const VarDecl *SrcVD, const Expr *Copy) {
79 if (OriginalType->isArrayType()) {
80 auto *BO = dyn_cast<BinaryOperator>(Copy);
81 if (BO && BO->getOpcode() == BO_Assign) {
82 // Perform simple memcpy for simple copying.
83 CGF.EmitAggregateAssign(DestAddr, SrcAddr, OriginalType);
84 } else {
85 // For arrays with complex element types perform element by element
86 // copying.
87 CGF.EmitOMPAggregateAssign(
88 DestAddr, SrcAddr, OriginalType,
89 [&CGF, Copy, SrcVD, DestVD](llvm::Value *DestElement,
90 llvm::Value *SrcElement) {
91 // Working with the single array element, so have to remap
92 // destination and source variables to corresponding array
93 // elements.
94 CodeGenFunction::OMPPrivateScope Remap(CGF);
95 Remap.addPrivate(DestVD, [DestElement]() -> llvm::Value *{
96 return DestElement;
97 });
98 Remap.addPrivate(
99 SrcVD, [SrcElement]() -> llvm::Value *{ return SrcElement; });
100 (void)Remap.Privatize();
101 CGF.EmitIgnoredExpr(Copy);
102 });
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000103 }
Alexey Bataev420d45b2015-04-14 05:11:24 +0000104 } else {
105 // Remap pseudo source variable to private copy.
106 CodeGenFunction::OMPPrivateScope Remap(CGF);
107 Remap.addPrivate(SrcVD, [SrcAddr]() -> llvm::Value *{ return SrcAddr; });
108 Remap.addPrivate(DestVD, [DestAddr]() -> llvm::Value *{ return DestAddr; });
109 (void)Remap.Privatize();
110 // Emit copying of the whole variable.
111 CGF.EmitIgnoredExpr(Copy);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000112 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000113}
114
Alexey Bataev69c62a92015-04-15 04:52:20 +0000115bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
116 OMPPrivateScope &PrivateScope) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000117 llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate;
Alexey Bataevc925aa32015-04-27 08:00:32 +0000118 for (auto &&I = D.getClausesOfKind(OMPC_firstprivate); I; ++I) {
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000119 auto *C = cast<OMPFirstprivateClause>(*I);
120 auto IRef = C->varlist_begin();
121 auto InitsRef = C->inits().begin();
122 for (auto IInit : C->private_copies()) {
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000123 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev69c62a92015-04-15 04:52:20 +0000124 if (EmittedAsFirstprivate.count(OrigVD) == 0) {
125 EmittedAsFirstprivate.insert(OrigVD);
126 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
127 auto *VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl());
128 bool IsRegistered;
129 DeclRefExpr DRE(
130 const_cast<VarDecl *>(OrigVD),
131 /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup(
132 OrigVD) != nullptr,
133 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
134 auto *OriginalAddr = EmitLValue(&DRE).getAddress();
135 if (OrigVD->getType()->isArrayType()) {
136 // Emit VarDecl with copy init for arrays.
137 // Get the address of the original variable captured in current
138 // captured region.
139 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value *{
140 auto Emission = EmitAutoVarAlloca(*VD);
141 auto *Init = VD->getInit();
142 if (!isa<CXXConstructExpr>(Init) || isTrivialInitializer(Init)) {
143 // Perform simple memcpy.
144 EmitAggregateAssign(Emission.getAllocatedAddress(), OriginalAddr,
145 (*IRef)->getType());
146 } else {
147 EmitOMPAggregateAssign(
148 Emission.getAllocatedAddress(), OriginalAddr,
149 (*IRef)->getType(),
150 [this, VDInit, Init](llvm::Value *DestElement,
151 llvm::Value *SrcElement) {
152 // Clean up any temporaries needed by the initialization.
153 RunCleanupsScope InitScope(*this);
154 // Emit initialization for single element.
155 LocalDeclMap[VDInit] = SrcElement;
156 EmitAnyExprToMem(Init, DestElement,
157 Init->getType().getQualifiers(),
158 /*IsInitializer*/ false);
159 LocalDeclMap.erase(VDInit);
160 });
161 }
162 EmitAutoVarCleanups(Emission);
163 return Emission.getAllocatedAddress();
164 });
165 } else {
166 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value *{
167 // Emit private VarDecl with copy init.
168 // Remap temp VDInit variable to the address of the original
169 // variable
170 // (for proper handling of captured global variables).
171 LocalDeclMap[VDInit] = OriginalAddr;
172 EmitDecl(*VD);
173 LocalDeclMap.erase(VDInit);
174 return GetAddrOfLocalVar(VD);
175 });
176 }
177 assert(IsRegistered &&
178 "firstprivate var already registered as private");
179 // Silence the warning about unused variable.
180 (void)IsRegistered;
181 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000182 ++IRef, ++InitsRef;
183 }
184 }
Alexey Bataev69c62a92015-04-15 04:52:20 +0000185 return !EmittedAsFirstprivate.empty();
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000186}
187
Alexey Bataev03b340a2014-10-21 03:16:40 +0000188void CodeGenFunction::EmitOMPPrivateClause(
189 const OMPExecutableDirective &D,
190 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataev50a64582015-04-22 12:24:45 +0000191 llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
Alexey Bataevc925aa32015-04-27 08:00:32 +0000192 for (auto &&I = D.getClausesOfKind(OMPC_private); I; ++I) {
Alexey Bataev03b340a2014-10-21 03:16:40 +0000193 auto *C = cast<OMPPrivateClause>(*I);
194 auto IRef = C->varlist_begin();
195 for (auto IInit : C->private_copies()) {
196 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev50a64582015-04-22 12:24:45 +0000197 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
198 auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
199 bool IsRegistered =
200 PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value *{
201 // Emit private VarDecl with copy init.
202 EmitDecl(*VD);
203 return GetAddrOfLocalVar(VD);
204 });
205 assert(IsRegistered && "private var already registered as private");
206 // Silence the warning about unused variable.
207 (void)IsRegistered;
208 }
Alexey Bataev03b340a2014-10-21 03:16:40 +0000209 ++IRef;
210 }
211 }
212}
213
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000214bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) {
215 // threadprivate_var1 = master_threadprivate_var1;
216 // operator=(threadprivate_var2, master_threadprivate_var2);
217 // ...
218 // __kmpc_barrier(&loc, global_tid);
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000219 llvm::DenseSet<const VarDecl *> CopiedVars;
220 llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr;
Alexey Bataevc925aa32015-04-27 08:00:32 +0000221 for (auto &&I = D.getClausesOfKind(OMPC_copyin); I; ++I) {
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000222 auto *C = cast<OMPCopyinClause>(*I);
223 auto IRef = C->varlist_begin();
224 auto ISrcRef = C->source_exprs().begin();
225 auto IDestRef = C->destination_exprs().begin();
226 for (auto *AssignOp : C->assignment_ops()) {
227 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
228 if (CopiedVars.insert(VD->getCanonicalDecl()).second) {
229 // Get the address of the master variable.
230 auto *MasterAddr = VD->isStaticLocal()
231 ? CGM.getStaticLocalDeclAddress(VD)
232 : CGM.GetAddrOfGlobal(VD);
233 // Get the address of the threadprivate variable.
234 auto *PrivateAddr = EmitLValue(*IRef).getAddress();
235 if (CopiedVars.size() == 1) {
236 // At first check if current thread is a master thread. If it is, no
237 // need to copy data.
238 CopyBegin = createBasicBlock("copyin.not.master");
239 CopyEnd = createBasicBlock("copyin.not.master.end");
240 Builder.CreateCondBr(
241 Builder.CreateICmpNE(
242 Builder.CreatePtrToInt(MasterAddr, CGM.IntPtrTy),
243 Builder.CreatePtrToInt(PrivateAddr, CGM.IntPtrTy)),
244 CopyBegin, CopyEnd);
245 EmitBlock(CopyBegin);
246 }
247 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
248 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
249 EmitOMPCopy(*this, (*IRef)->getType(), PrivateAddr, MasterAddr, DestVD,
250 SrcVD, AssignOp);
251 }
252 ++IRef;
253 ++ISrcRef;
254 ++IDestRef;
255 }
256 }
257 if (CopyEnd) {
258 // Exit out of copying procedure for non-master thread.
259 EmitBlock(CopyEnd, /*IsFinished=*/true);
260 return true;
261 }
262 return false;
263}
264
Alexey Bataev38e89532015-04-16 04:54:05 +0000265bool CodeGenFunction::EmitOMPLastprivateClauseInit(
266 const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000267 bool HasAtLeastOneLastprivate = false;
268 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
Alexey Bataevc925aa32015-04-27 08:00:32 +0000269 for (auto &&I = D.getClausesOfKind(OMPC_lastprivate); I; ++I) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000270 auto *C = cast<OMPLastprivateClause>(*I);
271 auto IRef = C->varlist_begin();
272 auto IDestRef = C->destination_exprs().begin();
273 for (auto *IInit : C->private_copies()) {
274 // Keep the address of the original variable for future update at the end
275 // of the loop.
276 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
277 if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) {
278 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
279 PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() -> llvm::Value *{
280 DeclRefExpr DRE(
281 const_cast<VarDecl *>(OrigVD),
282 /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup(
283 OrigVD) != nullptr,
284 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
285 return EmitLValue(&DRE).getAddress();
286 });
287 // Check if the variable is also a firstprivate: in this case IInit is
288 // not generated. Initialization of this variable will happen in codegen
289 // for 'firstprivate' clause.
290 if (!IInit)
291 continue;
292 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
293 bool IsRegistered =
294 PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value *{
295 // Emit private VarDecl with copy init.
296 EmitDecl(*VD);
297 return GetAddrOfLocalVar(VD);
298 });
299 assert(IsRegistered && "lastprivate var already registered as private");
300 HasAtLeastOneLastprivate = HasAtLeastOneLastprivate || IsRegistered;
301 }
302 ++IRef, ++IDestRef;
303 }
304 }
305 return HasAtLeastOneLastprivate;
306}
307
308void CodeGenFunction::EmitOMPLastprivateClauseFinal(
309 const OMPExecutableDirective &D, llvm::Value *IsLastIterCond) {
310 // Emit following code:
311 // if (<IsLastIterCond>) {
312 // orig_var1 = private_orig_var1;
313 // ...
314 // orig_varn = private_orig_varn;
315 // }
316 auto *ThenBB = createBasicBlock(".omp.lastprivate.then");
317 auto *DoneBB = createBasicBlock(".omp.lastprivate.done");
318 Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB);
319 EmitBlock(ThenBB);
320 {
Alexey Bataev38e89532015-04-16 04:54:05 +0000321 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
Alexey Bataevc925aa32015-04-27 08:00:32 +0000322 for (auto &&I = D.getClausesOfKind(OMPC_lastprivate); I; ++I) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000323 auto *C = cast<OMPLastprivateClause>(*I);
324 auto IRef = C->varlist_begin();
325 auto ISrcRef = C->source_exprs().begin();
326 auto IDestRef = C->destination_exprs().begin();
327 for (auto *AssignOp : C->assignment_ops()) {
328 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
329 if (AlreadyEmittedVars.insert(PrivateVD->getCanonicalDecl()).second) {
330 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
331 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
332 // Get the address of the original variable.
333 auto *OriginalAddr = GetAddrOfLocalVar(DestVD);
334 // Get the address of the private variable.
335 auto *PrivateAddr = GetAddrOfLocalVar(PrivateVD);
336 EmitOMPCopy(*this, (*IRef)->getType(), OriginalAddr, PrivateAddr,
337 DestVD, SrcVD, AssignOp);
338 }
339 ++IRef;
340 ++ISrcRef;
341 ++IDestRef;
342 }
343 }
344 }
345 EmitBlock(DoneBB, /*IsFinished=*/true);
346}
347
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000348void CodeGenFunction::EmitOMPReductionClauseInit(
349 const OMPExecutableDirective &D,
350 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataevc925aa32015-04-27 08:00:32 +0000351 for (auto &&I = D.getClausesOfKind(OMPC_reduction); I; ++I) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000352 auto *C = cast<OMPReductionClause>(*I);
353 auto ILHS = C->lhs_exprs().begin();
354 auto IRHS = C->rhs_exprs().begin();
355 for (auto IRef : C->varlists()) {
356 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl());
357 auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
358 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
359 // Store the address of the original variable associated with the LHS
360 // implicit variable.
361 PrivateScope.addPrivate(LHSVD, [this, OrigVD, IRef]() -> llvm::Value *{
362 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
363 CapturedStmtInfo->lookup(OrigVD) != nullptr,
364 IRef->getType(), VK_LValue, IRef->getExprLoc());
365 return EmitLValue(&DRE).getAddress();
366 });
367 // Emit reduction copy.
368 bool IsRegistered =
369 PrivateScope.addPrivate(OrigVD, [this, PrivateVD]() -> llvm::Value *{
370 // Emit private VarDecl with reduction init.
371 EmitDecl(*PrivateVD);
372 return GetAddrOfLocalVar(PrivateVD);
373 });
374 assert(IsRegistered && "private var already registered as private");
375 // Silence the warning about unused variable.
376 (void)IsRegistered;
377 ++ILHS, ++IRHS;
378 }
379 }
380}
381
382void CodeGenFunction::EmitOMPReductionClauseFinal(
383 const OMPExecutableDirective &D) {
384 llvm::SmallVector<const Expr *, 8> LHSExprs;
385 llvm::SmallVector<const Expr *, 8> RHSExprs;
386 llvm::SmallVector<const Expr *, 8> ReductionOps;
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000387 bool HasAtLeastOneReduction = false;
Alexey Bataevc925aa32015-04-27 08:00:32 +0000388 for (auto &&I = D.getClausesOfKind(OMPC_reduction); I; ++I) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000389 HasAtLeastOneReduction = true;
390 auto *C = cast<OMPReductionClause>(*I);
391 LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end());
392 RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end());
393 ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end());
394 }
395 if (HasAtLeastOneReduction) {
396 // Emit nowait reduction if nowait clause is present or directive is a
397 // parallel directive (it always has implicit barrier).
398 CGM.getOpenMPRuntime().emitReduction(
399 *this, D.getLocEnd(), LHSExprs, RHSExprs, ReductionOps,
400 D.getSingleClause(OMPC_nowait) ||
401 isOpenMPParallelDirective(D.getDirectiveKind()));
402 }
403}
404
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000405static void emitCommonOMPParallelDirective(CodeGenFunction &CGF,
406 const OMPExecutableDirective &S,
407 const RegionCodeGenTy &CodeGen) {
Alexey Bataev18095712014-10-10 12:19:54 +0000408 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000409 auto CapturedStruct = CGF.GenerateCapturedStmtArgument(*CS);
410 auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction(
411 S, *CS->getCapturedDecl()->param_begin(), CodeGen);
Alexey Bataev1d677132015-04-22 13:57:31 +0000412 if (auto C = S.getSingleClause(OMPC_num_threads)) {
413 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
414 auto NumThreadsClause = cast<OMPNumThreadsClause>(C);
415 auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
416 /*IgnoreResultAssign*/ true);
417 CGF.CGM.getOpenMPRuntime().emitNumThreadsClause(
418 CGF, NumThreads, NumThreadsClause->getLocStart());
419 }
420 const Expr *IfCond = nullptr;
421 if (auto C = S.getSingleClause(OMPC_if)) {
422 IfCond = cast<OMPIfClause>(C)->getCondition();
423 }
424 CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn,
425 CapturedStruct, IfCond);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000426}
427
428void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
429 LexicalScope Scope(*this, S.getSourceRange());
430 // Emit parallel region as a standalone region.
431 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
432 OMPPrivateScope PrivateScope(CGF);
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000433 bool Copyins = CGF.EmitOMPCopyinClause(S);
434 bool Firstprivates = CGF.EmitOMPFirstprivateClause(S, PrivateScope);
435 if (Copyins || Firstprivates) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000436 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000437 // initialization of firstprivate variables or propagation master's thread
438 // values of threadprivate variables to local instances of that variables
439 // of all other implicit threads.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000440 CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(),
441 OMPD_unknown);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000442 }
443 CGF.EmitOMPPrivateClause(S, PrivateScope);
444 CGF.EmitOMPReductionClauseInit(S, PrivateScope);
445 (void)PrivateScope.Privatize();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000446 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000447 CGF.EmitOMPReductionClauseFinal(S);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000448 // Emit implicit barrier at the end of the 'parallel' directive.
449 CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(),
450 OMPD_unknown);
451 };
452 emitCommonOMPParallelDirective(*this, S, CodeGen);
Alexey Bataev9959db52014-05-06 10:08:46 +0000453}
Alexander Musman515ad8c2014-05-22 08:54:05 +0000454
Alexander Musmand196ef22014-10-07 08:57:09 +0000455void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &S,
Alexander Musmana5f070a2014-10-01 06:03:56 +0000456 bool SeparateIter) {
457 RunCleanupsScope BodyScope(*this);
458 // Update counters values on current iteration.
459 for (auto I : S.updates()) {
460 EmitIgnoredExpr(I);
461 }
Alexander Musman3276a272015-03-21 10:12:56 +0000462 // Update the linear variables.
Alexey Bataevc925aa32015-04-27 08:00:32 +0000463 for (auto &&I = S.getClausesOfKind(OMPC_linear); I; ++I) {
464 auto *C = cast<OMPLinearClause>(*I);
Alexander Musman3276a272015-03-21 10:12:56 +0000465 for (auto U : C->updates()) {
466 EmitIgnoredExpr(U);
467 }
468 }
469
Alexander Musmana5f070a2014-10-01 06:03:56 +0000470 // On a continue in the body, jump to the end.
Alexander Musmand196ef22014-10-07 08:57:09 +0000471 auto Continue = getJumpDestInCurrentScope("omp.body.continue");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000472 BreakContinueStack.push_back(BreakContinue(JumpDest(), Continue));
473 // Emit loop body.
474 EmitStmt(S.getBody());
475 // The end (updates/cleanups).
476 EmitBlock(Continue.getBlock());
477 BreakContinueStack.pop_back();
478 if (SeparateIter) {
479 // TODO: Update lastprivates if the SeparateIter flag is true.
480 // This will be implemented in a follow-up OMPLastprivateClause patch, but
481 // result should be still correct without it, as we do not make these
482 // variables private yet.
483 }
484}
485
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000486void CodeGenFunction::EmitOMPInnerLoop(
487 const Stmt &S, bool RequiresCleanup, const Expr *LoopCond,
488 const Expr *IncExpr,
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000489 const llvm::function_ref<void(CodeGenFunction &)> &BodyGen,
490 const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) {
Alexander Musmand196ef22014-10-07 08:57:09 +0000491 auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000492
493 // Start the loop with a block that tests the condition.
Alexander Musmand196ef22014-10-07 08:57:09 +0000494 auto CondBlock = createBasicBlock("omp.inner.for.cond");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000495 EmitBlock(CondBlock);
496 LoopStack.push(CondBlock);
497
498 // If there are any cleanups between here and the loop-exit scope,
499 // create a block to stage a loop exit along.
500 auto ExitBlock = LoopExit.getBlock();
Alexey Bataev2df54a02015-03-12 08:53:29 +0000501 if (RequiresCleanup)
Alexander Musmand196ef22014-10-07 08:57:09 +0000502 ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000503
Alexander Musmand196ef22014-10-07 08:57:09 +0000504 auto LoopBody = createBasicBlock("omp.inner.for.body");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000505
Alexey Bataev2df54a02015-03-12 08:53:29 +0000506 // Emit condition.
Justin Bogner66242d62015-04-23 23:06:47 +0000507 EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S));
Alexander Musmana5f070a2014-10-01 06:03:56 +0000508 if (ExitBlock != LoopExit.getBlock()) {
509 EmitBlock(ExitBlock);
510 EmitBranchThroughCleanup(LoopExit);
511 }
512
513 EmitBlock(LoopBody);
Justin Bogner66242d62015-04-23 23:06:47 +0000514 incrementProfileCounter(&S);
Alexander Musmana5f070a2014-10-01 06:03:56 +0000515
516 // Create a block for the increment.
Alexander Musmand196ef22014-10-07 08:57:09 +0000517 auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000518 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
519
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000520 BodyGen(*this);
Alexander Musmana5f070a2014-10-01 06:03:56 +0000521
522 // Emit "IV = IV + 1" and a back-edge to the condition block.
523 EmitBlock(Continue.getBlock());
Alexey Bataev2df54a02015-03-12 08:53:29 +0000524 EmitIgnoredExpr(IncExpr);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000525 PostIncGen(*this);
Alexander Musmana5f070a2014-10-01 06:03:56 +0000526 BreakContinueStack.pop_back();
527 EmitBranch(CondBlock);
528 LoopStack.pop();
529 // Emit the fall-through block.
530 EmitBlock(LoopExit.getBlock());
531}
532
533void CodeGenFunction::EmitOMPSimdFinal(const OMPLoopDirective &S) {
534 auto IC = S.counters().begin();
535 for (auto F : S.finals()) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000536 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl());
537 if (LocalDeclMap.lookup(OrigVD)) {
538 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
539 CapturedStmtInfo->lookup(OrigVD) != nullptr,
540 (*IC)->getType(), VK_LValue, (*IC)->getExprLoc());
541 auto *OrigAddr = EmitLValue(&DRE).getAddress();
542 OMPPrivateScope VarScope(*this);
543 VarScope.addPrivate(OrigVD,
544 [OrigAddr]() -> llvm::Value *{ return OrigAddr; });
545 (void)VarScope.Privatize();
Alexander Musmana5f070a2014-10-01 06:03:56 +0000546 EmitIgnoredExpr(F);
547 }
548 ++IC;
549 }
Alexander Musman3276a272015-03-21 10:12:56 +0000550 // Emit the final values of the linear variables.
Alexey Bataevc925aa32015-04-27 08:00:32 +0000551 for (auto &&I = S.getClausesOfKind(OMPC_linear); I; ++I) {
552 auto *C = cast<OMPLinearClause>(*I);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000553 auto IC = C->varlist_begin();
Alexander Musman3276a272015-03-21 10:12:56 +0000554 for (auto F : C->finals()) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000555 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl());
556 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
557 CapturedStmtInfo->lookup(OrigVD) != nullptr,
558 (*IC)->getType(), VK_LValue, (*IC)->getExprLoc());
559 auto *OrigAddr = EmitLValue(&DRE).getAddress();
560 OMPPrivateScope VarScope(*this);
561 VarScope.addPrivate(OrigVD,
562 [OrigAddr]() -> llvm::Value *{ return OrigAddr; });
563 (void)VarScope.Privatize();
Alexander Musman3276a272015-03-21 10:12:56 +0000564 EmitIgnoredExpr(F);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000565 ++IC;
Alexander Musman3276a272015-03-21 10:12:56 +0000566 }
567 }
Alexander Musmana5f070a2014-10-01 06:03:56 +0000568}
569
Alexander Musman09184fe2014-09-30 05:29:28 +0000570static void EmitOMPAlignedClause(CodeGenFunction &CGF, CodeGenModule &CGM,
571 const OMPAlignedClause &Clause) {
572 unsigned ClauseAlignment = 0;
573 if (auto AlignmentExpr = Clause.getAlignment()) {
574 auto AlignmentCI =
575 cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr));
576 ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue());
577 }
578 for (auto E : Clause.varlists()) {
579 unsigned Alignment = ClauseAlignment;
580 if (Alignment == 0) {
581 // OpenMP [2.8.1, Description]
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000582 // If no optional parameter is specified, implementation-defined default
Alexander Musman09184fe2014-09-30 05:29:28 +0000583 // alignments for SIMD instructions on the target platforms are assumed.
584 Alignment = CGM.getTargetCodeGenInfo().getOpenMPSimdDefaultAlignment(
585 E->getType());
586 }
587 assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) &&
588 "alignment is not power of 2");
589 if (Alignment != 0) {
590 llvm::Value *PtrValue = CGF.EmitScalarExpr(E);
591 CGF.EmitAlignmentAssumption(PtrValue, Alignment);
592 }
593 }
594}
595
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000596static void EmitPrivateLoopCounters(CodeGenFunction &CGF,
597 CodeGenFunction::OMPPrivateScope &LoopScope,
598 ArrayRef<Expr *> Counters) {
599 for (auto *E : Counters) {
600 auto VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
Alexey Bataev61114692015-04-28 13:20:05 +0000601 (void)LoopScope.addPrivate(VD, [&]() -> llvm::Value *{
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000602 // Emit var without initialization.
603 auto VarEmission = CGF.EmitAutoVarAlloca(*VD);
604 CGF.EmitAutoVarCleanups(VarEmission);
605 return VarEmission.getAllocatedAddress();
606 });
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000607 }
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000608}
609
Alexey Bataev62dbb972015-04-22 11:59:37 +0000610static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S,
611 const Expr *Cond, llvm::BasicBlock *TrueBlock,
612 llvm::BasicBlock *FalseBlock, uint64_t TrueCount) {
613 CodeGenFunction::OMPPrivateScope PreCondScope(CGF);
614 EmitPrivateLoopCounters(CGF, PreCondScope, S.counters());
615 const VarDecl *IVDecl =
616 cast<VarDecl>(cast<DeclRefExpr>(S.getIterationVariable())->getDecl());
617 bool IsRegistered = PreCondScope.addPrivate(IVDecl, [&]() -> llvm::Value *{
618 // Emit var without initialization.
619 auto VarEmission = CGF.EmitAutoVarAlloca(*IVDecl);
620 CGF.EmitAutoVarCleanups(VarEmission);
621 return VarEmission.getAllocatedAddress();
622 });
623 assert(IsRegistered && "counter already registered as private");
624 // Silence the warning about unused variable.
625 (void)IsRegistered;
626 (void)PreCondScope.Privatize();
627 // Initialize internal counter to 0 to calculate initial values of real
628 // counters.
629 LValue IV = CGF.EmitLValue(S.getIterationVariable());
630 CGF.EmitStoreOfScalar(
631 llvm::ConstantInt::getNullValue(
632 IV.getAddress()->getType()->getPointerElementType()),
633 CGF.EmitLValue(S.getIterationVariable()), /*isInit=*/true);
634 // Get initial values of real counters.
635 for (auto I : S.updates()) {
636 CGF.EmitIgnoredExpr(I);
637 }
638 // Check that loop is executed at least one time.
639 CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount);
640}
641
Alexander Musman3276a272015-03-21 10:12:56 +0000642static void
643EmitPrivateLinearVars(CodeGenFunction &CGF, const OMPExecutableDirective &D,
644 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataevc925aa32015-04-27 08:00:32 +0000645 for (auto &&I = D.getClausesOfKind(OMPC_linear); I; ++I) {
646 auto *C = cast<OMPLinearClause>(*I);
647 for (auto *E : C->varlists()) {
Alexander Musman3276a272015-03-21 10:12:56 +0000648 auto VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
649 bool IsRegistered = PrivateScope.addPrivate(VD, [&]()->llvm::Value * {
650 // Emit var without initialization.
651 auto VarEmission = CGF.EmitAutoVarAlloca(*VD);
652 CGF.EmitAutoVarCleanups(VarEmission);
653 return VarEmission.getAllocatedAddress();
654 });
655 assert(IsRegistered && "linear var already registered as private");
656 // Silence the warning about unused variable.
657 (void)IsRegistered;
658 }
659 }
660}
661
Alexander Musman515ad8c2014-05-22 08:54:05 +0000662void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000663 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
664 // Pragma 'simd' code depends on presence of 'lastprivate'.
665 // If present, we have to separate last iteration of the loop:
666 //
Alexey Bataev62dbb972015-04-22 11:59:37 +0000667 // if (PreCond) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000668 // for (IV in 0..LastIteration-1) BODY;
669 // BODY with updates of lastprivate vars;
670 // <Final counter/linear vars updates>;
671 // }
672 //
673 // otherwise (when there's no lastprivate):
674 //
Alexey Bataev62dbb972015-04-22 11:59:37 +0000675 // if (PreCond) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000676 // for (IV in 0..LastIteration) BODY;
677 // <Final counter/linear vars updates>;
Alexey Bataev62dbb972015-04-22 11:59:37 +0000678 // }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000679 //
Alexander Musmana5f070a2014-10-01 06:03:56 +0000680
Alexey Bataev62dbb972015-04-22 11:59:37 +0000681 // Emit: if (PreCond) - begin.
682 // If the condition constant folds and can be elided, avoid emitting the
683 // whole loop.
684 bool CondConstant;
685 llvm::BasicBlock *ContBlock = nullptr;
686 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
687 if (!CondConstant)
688 return;
689 } else {
Alexey Bataev62dbb972015-04-22 11:59:37 +0000690 auto *ThenBlock = CGF.createBasicBlock("simd.if.then");
691 ContBlock = CGF.createBasicBlock("simd.if.end");
Justin Bogner66242d62015-04-23 23:06:47 +0000692 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
693 CGF.getProfileCount(&S));
Alexey Bataev62dbb972015-04-22 11:59:37 +0000694 CGF.EmitBlock(ThenBlock);
Justin Bogner66242d62015-04-23 23:06:47 +0000695 CGF.incrementProfileCounter(&S);
Alexey Bataev62dbb972015-04-22 11:59:37 +0000696 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000697 // Walk clauses and process safelen/lastprivate.
698 bool SeparateIter = false;
699 CGF.LoopStack.setParallel();
700 CGF.LoopStack.setVectorizerEnable(true);
701 for (auto C : S.clauses()) {
702 switch (C->getClauseKind()) {
703 case OMPC_safelen: {
704 RValue Len = CGF.EmitAnyExpr(cast<OMPSafelenClause>(C)->getSafelen(),
705 AggValueSlot::ignored(), true);
706 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
707 CGF.LoopStack.setVectorizerWidth(Val->getZExtValue());
708 // In presence of finite 'safelen', it may be unsafe to mark all
709 // the memory instructions parallel, because loop-carried
710 // dependences of 'safelen' iterations are possible.
711 CGF.LoopStack.setParallel(false);
712 break;
Alexander Musman3276a272015-03-21 10:12:56 +0000713 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000714 case OMPC_aligned:
715 EmitOMPAlignedClause(CGF, CGF.CGM, cast<OMPAlignedClause>(*C));
716 break;
717 case OMPC_lastprivate:
718 SeparateIter = true;
719 break;
720 default:
721 // Not handled yet
722 ;
723 }
724 }
Alexander Musman3276a272015-03-21 10:12:56 +0000725
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000726 // Emit inits for the linear variables.
Alexey Bataevc925aa32015-04-27 08:00:32 +0000727 for (auto &&I = S.getClausesOfKind(OMPC_linear); I; ++I) {
728 auto *C = cast<OMPLinearClause>(*I);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000729 for (auto Init : C->inits()) {
730 auto *D = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl());
731 CGF.EmitVarDecl(*D);
732 }
Alexander Musmana5f070a2014-10-01 06:03:56 +0000733 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000734
735 // Emit the loop iteration variable.
736 const Expr *IVExpr = S.getIterationVariable();
737 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
738 CGF.EmitVarDecl(*IVDecl);
739 CGF.EmitIgnoredExpr(S.getInit());
740
741 // Emit the iterations count variable.
742 // If it is not a variable, Sema decided to calculate iterations count on
743 // each
744 // iteration (e.g., it is foldable into a constant).
745 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
746 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
747 // Emit calculation of the iterations count.
748 CGF.EmitIgnoredExpr(S.getCalcLastIteration());
Alexander Musmana5f070a2014-10-01 06:03:56 +0000749 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000750
751 // Emit the linear steps for the linear clauses.
752 // If a step is not constant, it is pre-calculated before the loop.
Alexey Bataevc925aa32015-04-27 08:00:32 +0000753 for (auto &&I = S.getClausesOfKind(OMPC_linear); I; ++I) {
754 auto *C = cast<OMPLinearClause>(*I);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000755 if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep()))
756 if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) {
757 CGF.EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl()));
758 // Emit calculation of the linear step.
759 CGF.EmitIgnoredExpr(CS);
760 }
761 }
762
Alexey Bataev62dbb972015-04-22 11:59:37 +0000763 {
764 OMPPrivateScope LoopScope(CGF);
765 EmitPrivateLoopCounters(CGF, LoopScope, S.counters());
766 EmitPrivateLinearVars(CGF, S, LoopScope);
767 CGF.EmitOMPPrivateClause(S, LoopScope);
768 (void)LoopScope.Privatize();
769 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(),
770 S.getCond(SeparateIter), S.getInc(),
771 [&S](CodeGenFunction &CGF) {
772 CGF.EmitOMPLoopBody(S);
773 CGF.EmitStopPoint(&S);
774 },
775 [](CodeGenFunction &) {});
776 if (SeparateIter) {
777 CGF.EmitOMPLoopBody(S, /*SeparateIter=*/true);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000778 }
Alexey Bataev62dbb972015-04-22 11:59:37 +0000779 }
780 CGF.EmitOMPSimdFinal(S);
781 // Emit: if (PreCond) - end.
782 if (ContBlock) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000783 CGF.EmitBranch(ContBlock);
784 CGF.EmitBlock(ContBlock, true);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000785 }
786 };
787 CGM.getOpenMPRuntime().emitInlinedDirective(*this, CodeGen);
Alexander Musman515ad8c2014-05-22 08:54:05 +0000788}
789
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000790void CodeGenFunction::EmitOMPForOuterLoop(OpenMPScheduleClauseKind ScheduleKind,
791 const OMPLoopDirective &S,
792 OMPPrivateScope &LoopScope,
793 llvm::Value *LB, llvm::Value *UB,
794 llvm::Value *ST, llvm::Value *IL,
795 llvm::Value *Chunk) {
796 auto &RT = CGM.getOpenMPRuntime();
Alexander Musman92bdaab2015-03-12 13:37:50 +0000797
798 // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime).
799 const bool Dynamic = RT.isDynamic(ScheduleKind);
800
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000801 assert(!RT.isStaticNonchunked(ScheduleKind, /* Chunked */ Chunk != nullptr) &&
802 "static non-chunked schedule does not need outer loop");
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000803
804 // Emit outer loop.
805 //
806 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
Alexander Musman92bdaab2015-03-12 13:37:50 +0000807 // When schedule(dynamic,chunk_size) is specified, the iterations are
808 // distributed to threads in the team in chunks as the threads request them.
809 // Each thread executes a chunk of iterations, then requests another chunk,
810 // until no chunks remain to be distributed. Each chunk contains chunk_size
811 // iterations, except for the last chunk to be distributed, which may have
812 // fewer iterations. When no chunk_size is specified, it defaults to 1.
813 //
814 // When schedule(guided,chunk_size) is specified, the iterations are assigned
815 // to threads in the team in chunks as the executing threads request them.
816 // Each thread executes a chunk of iterations, then requests another chunk,
817 // until no chunks remain to be assigned. For a chunk_size of 1, the size of
818 // each chunk is proportional to the number of unassigned iterations divided
819 // by the number of threads in the team, decreasing to 1. For a chunk_size
820 // with value k (greater than 1), the size of each chunk is determined in the
821 // same way, with the restriction that the chunks do not contain fewer than k
822 // iterations (except for the last chunk to be assigned, which may have fewer
823 // than k iterations).
824 //
825 // When schedule(auto) is specified, the decision regarding scheduling is
826 // delegated to the compiler and/or runtime system. The programmer gives the
827 // implementation the freedom to choose any possible mapping of iterations to
828 // threads in the team.
829 //
830 // When schedule(runtime) is specified, the decision regarding scheduling is
831 // deferred until run time, and the schedule and chunk size are taken from the
832 // run-sched-var ICV. If the ICV is set to auto, the schedule is
833 // implementation defined
834 //
835 // while(__kmpc_dispatch_next(&LB, &UB)) {
836 // idx = LB;
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000837 // while (idx <= UB) { BODY; ++idx;
838 // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only.
839 // } // inner loop
Alexander Musman92bdaab2015-03-12 13:37:50 +0000840 // }
841 //
842 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000843 // When schedule(static, chunk_size) is specified, iterations are divided into
844 // chunks of size chunk_size, and the chunks are assigned to the threads in
845 // the team in a round-robin fashion in the order of the thread number.
846 //
847 // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) {
848 // while (idx <= UB) { BODY; ++idx; } // inner loop
849 // LB = LB + ST;
850 // UB = UB + ST;
851 // }
852 //
Alexander Musman92bdaab2015-03-12 13:37:50 +0000853
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000854 const Expr *IVExpr = S.getIterationVariable();
855 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
856 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
857
Alexander Musman92bdaab2015-03-12 13:37:50 +0000858 RT.emitForInit(
859 *this, S.getLocStart(), ScheduleKind, IVSize, IVSigned, IL, LB,
860 (Dynamic ? EmitAnyExpr(S.getLastIteration()).getScalarVal() : UB), ST,
861 Chunk);
862
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000863 auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end");
864
865 // Start the loop with a block that tests the condition.
866 auto CondBlock = createBasicBlock("omp.dispatch.cond");
867 EmitBlock(CondBlock);
868 LoopStack.push(CondBlock);
869
870 llvm::Value *BoolCondVal = nullptr;
Alexander Musman92bdaab2015-03-12 13:37:50 +0000871 if (!Dynamic) {
872 // UB = min(UB, GlobalUB)
873 EmitIgnoredExpr(S.getEnsureUpperBound());
874 // IV = LB
875 EmitIgnoredExpr(S.getInit());
876 // IV < UB
877 BoolCondVal = EvaluateExprAsBool(S.getCond(false));
878 } else {
879 BoolCondVal = RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned,
880 IL, LB, UB, ST);
881 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000882
883 // If there are any cleanups between here and the loop-exit scope,
884 // create a block to stage a loop exit along.
885 auto ExitBlock = LoopExit.getBlock();
886 if (LoopScope.requiresCleanups())
887 ExitBlock = createBasicBlock("omp.dispatch.cleanup");
888
889 auto LoopBody = createBasicBlock("omp.dispatch.body");
890 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
891 if (ExitBlock != LoopExit.getBlock()) {
892 EmitBlock(ExitBlock);
893 EmitBranchThroughCleanup(LoopExit);
894 }
895 EmitBlock(LoopBody);
896
Alexander Musman92bdaab2015-03-12 13:37:50 +0000897 // Emit "IV = LB" (in case of static schedule, we have already calculated new
898 // LB for loop condition and emitted it above).
899 if (Dynamic)
900 EmitIgnoredExpr(S.getInit());
901
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000902 // Create a block for the increment.
903 auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc");
904 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
905
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000906 bool DynamicWithOrderedClause =
907 Dynamic && S.getSingleClause(OMPC_ordered) != nullptr;
908 SourceLocation Loc = S.getLocStart();
Alexey Bataev53223c92015-05-07 04:25:17 +0000909 // Generate !llvm.loop.parallel metadata for loads and stores for loops with
910 // dynamic/guided scheduling and without ordered clause.
911 LoopStack.setParallel((ScheduleKind == OMPC_SCHEDULE_dynamic ||
912 ScheduleKind == OMPC_SCHEDULE_guided) &&
913 !DynamicWithOrderedClause);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000914 EmitOMPInnerLoop(
915 S, LoopScope.requiresCleanups(), S.getCond(/*SeparateIter=*/false),
916 S.getInc(),
917 [&S](CodeGenFunction &CGF) {
918 CGF.EmitOMPLoopBody(S);
919 CGF.EmitStopPoint(&S);
920 },
921 [DynamicWithOrderedClause, IVSize, IVSigned, Loc](CodeGenFunction &CGF) {
922 if (DynamicWithOrderedClause) {
923 CGF.CGM.getOpenMPRuntime().emitForOrderedDynamicIterationEnd(
924 CGF, Loc, IVSize, IVSigned);
925 }
926 });
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000927
928 EmitBlock(Continue.getBlock());
929 BreakContinueStack.pop_back();
Alexander Musman92bdaab2015-03-12 13:37:50 +0000930 if (!Dynamic) {
931 // Emit "LB = LB + Stride", "UB = UB + Stride".
932 EmitIgnoredExpr(S.getNextLowerBound());
933 EmitIgnoredExpr(S.getNextUpperBound());
934 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000935
936 EmitBranch(CondBlock);
937 LoopStack.pop();
938 // Emit the fall-through block.
939 EmitBlock(LoopExit.getBlock());
940
941 // Tell the runtime we are done.
Alexander Musman92bdaab2015-03-12 13:37:50 +0000942 if (!Dynamic)
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000943 RT.emitForStaticFinish(*this, S.getLocEnd());
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000944}
945
Alexander Musmanc6388682014-12-15 07:07:06 +0000946/// \brief Emit a helper variable and return corresponding lvalue.
947static LValue EmitOMPHelperVar(CodeGenFunction &CGF,
948 const DeclRefExpr *Helper) {
949 auto VDecl = cast<VarDecl>(Helper->getDecl());
950 CGF.EmitVarDecl(*VDecl);
951 return CGF.EmitLValue(Helper);
952}
953
Alexey Bataev38e89532015-04-16 04:54:05 +0000954bool CodeGenFunction::EmitOMPWorksharingLoop(const OMPLoopDirective &S) {
Alexander Musmanc6388682014-12-15 07:07:06 +0000955 // Emit the loop iteration variable.
956 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
957 auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
958 EmitVarDecl(*IVDecl);
959
960 // Emit the iterations count variable.
961 // If it is not a variable, Sema decided to calculate iterations count on each
962 // iteration (e.g., it is foldable into a constant).
963 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
964 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
965 // Emit calculation of the iterations count.
966 EmitIgnoredExpr(S.getCalcLastIteration());
967 }
968
969 auto &RT = CGM.getOpenMPRuntime();
970
Alexey Bataev38e89532015-04-16 04:54:05 +0000971 bool HasLastprivateClause;
Alexander Musmanc6388682014-12-15 07:07:06 +0000972 // Check pre-condition.
973 {
974 // Skip the entire loop if we don't meet the precondition.
Alexey Bataev62dbb972015-04-22 11:59:37 +0000975 // If the condition constant folds and can be elided, avoid emitting the
976 // whole loop.
977 bool CondConstant;
978 llvm::BasicBlock *ContBlock = nullptr;
979 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
980 if (!CondConstant)
981 return false;
982 } else {
Alexey Bataev62dbb972015-04-22 11:59:37 +0000983 auto *ThenBlock = createBasicBlock("omp.precond.then");
984 ContBlock = createBasicBlock("omp.precond.end");
985 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
Justin Bogner66242d62015-04-23 23:06:47 +0000986 getProfileCount(&S));
Alexey Bataev62dbb972015-04-22 11:59:37 +0000987 EmitBlock(ThenBlock);
Justin Bogner66242d62015-04-23 23:06:47 +0000988 incrementProfileCounter(&S);
Alexey Bataev62dbb972015-04-22 11:59:37 +0000989 }
Alexander Musmanc6388682014-12-15 07:07:06 +0000990 // Emit 'then' code.
991 {
992 // Emit helper vars inits.
993 LValue LB =
994 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable()));
995 LValue UB =
996 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable()));
997 LValue ST =
998 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
999 LValue IL =
1000 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
1001
1002 OMPPrivateScope LoopScope(*this);
Alexey Bataev69c62a92015-04-15 04:52:20 +00001003 if (EmitOMPFirstprivateClause(S, LoopScope)) {
1004 // Emit implicit barrier to synchronize threads and avoid data races on
1005 // initialization of firstprivate variables.
1006 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
1007 OMPD_unknown);
1008 }
Alexey Bataev50a64582015-04-22 12:24:45 +00001009 EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev38e89532015-04-16 04:54:05 +00001010 HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev7ebe5fd2015-04-22 13:43:03 +00001011 EmitOMPReductionClauseInit(S, LoopScope);
Alexander Musmanc6388682014-12-15 07:07:06 +00001012 EmitPrivateLoopCounters(*this, LoopScope, S.counters());
Alexander Musman7931b982015-03-16 07:14:41 +00001013 (void)LoopScope.Privatize();
Alexander Musmanc6388682014-12-15 07:07:06 +00001014
1015 // Detect the loop schedule kind and chunk.
1016 auto ScheduleKind = OMPC_SCHEDULE_unknown;
1017 llvm::Value *Chunk = nullptr;
1018 if (auto C = cast_or_null<OMPScheduleClause>(
1019 S.getSingleClause(OMPC_schedule))) {
1020 ScheduleKind = C->getScheduleKind();
1021 if (auto Ch = C->getChunkSize()) {
1022 Chunk = EmitScalarExpr(Ch);
1023 Chunk = EmitScalarConversion(Chunk, Ch->getType(),
1024 S.getIterationVariable()->getType());
1025 }
1026 }
1027 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1028 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1029 if (RT.isStaticNonchunked(ScheduleKind,
1030 /* Chunked */ Chunk != nullptr)) {
1031 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1032 // When no chunk_size is specified, the iteration space is divided into
1033 // chunks that are approximately equal in size, and at most one chunk is
1034 // distributed to each thread. Note that the size of the chunks is
1035 // unspecified in this case.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001036 RT.emitForInit(*this, S.getLocStart(), ScheduleKind, IVSize, IVSigned,
1037 IL.getAddress(), LB.getAddress(), UB.getAddress(),
1038 ST.getAddress());
Alexander Musmanc6388682014-12-15 07:07:06 +00001039 // UB = min(UB, GlobalUB);
1040 EmitIgnoredExpr(S.getEnsureUpperBound());
1041 // IV = LB;
1042 EmitIgnoredExpr(S.getInit());
1043 // while (idx <= UB) { BODY; ++idx; }
Alexey Bataev2df54a02015-03-12 08:53:29 +00001044 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(),
1045 S.getCond(/*SeparateIter=*/false), S.getInc(),
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001046 [&S](CodeGenFunction &CGF) {
1047 CGF.EmitOMPLoopBody(S);
1048 CGF.EmitStopPoint(&S);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001049 },
1050 [](CodeGenFunction &) {});
Alexander Musmanc6388682014-12-15 07:07:06 +00001051 // Tell the runtime we are done.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001052 RT.emitForStaticFinish(*this, S.getLocStart());
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001053 } else {
1054 // Emit the outer loop, which requests its work chunk [LB..UB] from
1055 // runtime and runs the inner loop to process it.
1056 EmitOMPForOuterLoop(ScheduleKind, S, LoopScope, LB.getAddress(),
1057 UB.getAddress(), ST.getAddress(), IL.getAddress(),
1058 Chunk);
1059 }
Alexey Bataev7ebe5fd2015-04-22 13:43:03 +00001060 EmitOMPReductionClauseFinal(S);
Alexey Bataev38e89532015-04-16 04:54:05 +00001061 // Emit final copy of the lastprivate variables if IsLastIter != 0.
1062 if (HasLastprivateClause)
1063 EmitOMPLastprivateClauseFinal(
1064 S, Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart())));
Alexander Musmanc6388682014-12-15 07:07:06 +00001065 }
1066 // We're now done with the loop, so jump to the continuation block.
Alexey Bataev62dbb972015-04-22 11:59:37 +00001067 if (ContBlock) {
1068 EmitBranch(ContBlock);
1069 EmitBlock(ContBlock, true);
1070 }
Alexander Musmanc6388682014-12-15 07:07:06 +00001071 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001072 return HasLastprivateClause;
Alexander Musmanc6388682014-12-15 07:07:06 +00001073}
1074
1075void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001076 LexicalScope Scope(*this, S.getSourceRange());
Alexey Bataev38e89532015-04-16 04:54:05 +00001077 bool HasLastprivates = false;
1078 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF) {
1079 HasLastprivates = CGF.EmitOMPWorksharingLoop(S);
1080 };
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001081 CGM.getOpenMPRuntime().emitInlinedDirective(*this, CodeGen);
Alexander Musmanc6388682014-12-15 07:07:06 +00001082
1083 // Emit an implicit barrier at the end.
Alexey Bataev38e89532015-04-16 04:54:05 +00001084 if (!S.getSingleClause(OMPC_nowait) || HasLastprivates) {
Alexey Bataevf2685682015-03-30 04:30:22 +00001085 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
1086 }
Alexey Bataevf29276e2014-06-18 04:14:57 +00001087}
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001088
Alexander Musmanf82886e2014-09-18 05:12:34 +00001089void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &) {
1090 llvm_unreachable("CodeGen for 'omp for simd' is not supported yet.");
1091}
1092
Alexey Bataev2df54a02015-03-12 08:53:29 +00001093static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty,
1094 const Twine &Name,
1095 llvm::Value *Init = nullptr) {
1096 auto LVal = CGF.MakeNaturalAlignAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty);
1097 if (Init)
1098 CGF.EmitScalarInit(Init, LVal);
1099 return LVal;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001100}
1101
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001102static OpenMPDirectiveKind emitSections(CodeGenFunction &CGF,
1103 const OMPExecutableDirective &S) {
Alexey Bataev2df54a02015-03-12 08:53:29 +00001104 auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt();
1105 auto *CS = dyn_cast<CompoundStmt>(Stmt);
1106 if (CS && CS->size() > 1) {
Alexey Bataev9efc03b2015-04-27 04:34:03 +00001107 bool HasLastprivates = false;
1108 auto &&CodeGen = [&S, CS, &HasLastprivates](CodeGenFunction &CGF) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001109 auto &C = CGF.CGM.getContext();
1110 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1111 // Emit helper vars inits.
1112 LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.",
1113 CGF.Builder.getInt32(0));
1114 auto *GlobalUBVal = CGF.Builder.getInt32(CS->size() - 1);
1115 LValue UB =
1116 createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal);
1117 LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.",
1118 CGF.Builder.getInt32(1));
1119 LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.",
1120 CGF.Builder.getInt32(0));
1121 // Loop counter.
1122 LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv.");
1123 OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001124 CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001125 OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001126 CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001127 // Generate condition for loop.
1128 BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue,
1129 OK_Ordinary, S.getLocStart(),
1130 /*fpContractable=*/false);
1131 // Increment for loop counter.
1132 UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue,
1133 OK_Ordinary, S.getLocStart());
1134 auto BodyGen = [CS, &S, &IV](CodeGenFunction &CGF) {
1135 // Iterate through all sections and emit a switch construct:
1136 // switch (IV) {
1137 // case 0:
1138 // <SectionStmt[0]>;
1139 // break;
1140 // ...
1141 // case <NumSection> - 1:
1142 // <SectionStmt[<NumSection> - 1]>;
1143 // break;
1144 // }
1145 // .omp.sections.exit:
1146 auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit");
1147 auto *SwitchStmt = CGF.Builder.CreateSwitch(
1148 CGF.EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB,
1149 CS->size());
1150 unsigned CaseNumber = 0;
1151 for (auto C = CS->children(); C; ++C, ++CaseNumber) {
1152 auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
1153 CGF.EmitBlock(CaseBB);
1154 SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB);
1155 CGF.EmitStmt(*C);
1156 CGF.EmitBranch(ExitBB);
1157 }
1158 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1159 };
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001160
1161 CodeGenFunction::OMPPrivateScope LoopScope(CGF);
1162 if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) {
1163 // Emit implicit barrier to synchronize threads and avoid data races on
1164 // initialization of firstprivate variables.
1165 CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(),
1166 OMPD_unknown);
1167 }
Alexey Bataev73870832015-04-27 04:12:12 +00001168 CGF.EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev9efc03b2015-04-27 04:34:03 +00001169 HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataeva89adf22015-04-27 05:04:13 +00001170 CGF.EmitOMPReductionClauseInit(S, LoopScope);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001171 (void)LoopScope.Privatize();
1172
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001173 // Emit static non-chunked loop.
1174 CGF.CGM.getOpenMPRuntime().emitForInit(
1175 CGF, S.getLocStart(), OMPC_SCHEDULE_static, /*IVSize=*/32,
1176 /*IVSigned=*/true, IL.getAddress(), LB.getAddress(), UB.getAddress(),
1177 ST.getAddress());
1178 // UB = min(UB, GlobalUB);
1179 auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart());
1180 auto *MinUBGlobalUB = CGF.Builder.CreateSelect(
1181 CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal);
1182 CGF.EmitStoreOfScalar(MinUBGlobalUB, UB);
1183 // IV = LB;
1184 CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV);
1185 // while (idx <= UB) { BODY; ++idx; }
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001186 CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen,
1187 [](CodeGenFunction &) {});
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001188 // Tell the runtime we are done.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001189 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocStart());
Alexey Bataeva89adf22015-04-27 05:04:13 +00001190 CGF.EmitOMPReductionClauseFinal(S);
Alexey Bataev9efc03b2015-04-27 04:34:03 +00001191
1192 // Emit final copy of the lastprivate variables if IsLastIter != 0.
1193 if (HasLastprivates)
1194 CGF.EmitOMPLastprivateClauseFinal(
1195 S, CGF.Builder.CreateIsNotNull(
1196 CGF.EmitLoadOfScalar(IL, S.getLocStart())));
Alexey Bataev2df54a02015-03-12 08:53:29 +00001197 };
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001198
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001199 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, CodeGen);
Alexey Bataev9efc03b2015-04-27 04:34:03 +00001200 // Emit barrier for lastprivates only if 'sections' directive has 'nowait'
1201 // clause. Otherwise the barrier will be generated by the codegen for the
1202 // directive.
1203 if (HasLastprivates && S.getSingleClause(OMPC_nowait)) {
1204 // Emit implicit barrier to synchronize threads and avoid data races on
1205 // initialization of firstprivate variables.
1206 CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(),
1207 OMPD_unknown);
1208 }
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001209 return OMPD_sections;
Alexey Bataev2df54a02015-03-12 08:53:29 +00001210 }
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001211 // If only one section is found - no need to generate loop, emit as a single
1212 // region.
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001213 bool HasFirstprivates;
Alexey Bataeva89adf22015-04-27 05:04:13 +00001214 // No need to generate reductions for sections with single section region, we
1215 // can use original shared variables for all operations.
Alexey Bataevc925aa32015-04-27 08:00:32 +00001216 bool HasReductions = !S.getClausesOfKind(OMPC_reduction).empty();
Alexey Bataev9efc03b2015-04-27 04:34:03 +00001217 // No need to generate lastprivates for sections with single section region,
1218 // we can use original shared variable for all calculations with barrier at
1219 // the end of the sections.
Alexey Bataevc925aa32015-04-27 08:00:32 +00001220 bool HasLastprivates = !S.getClausesOfKind(OMPC_lastprivate).empty();
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001221 auto &&CodeGen = [Stmt, &S, &HasFirstprivates](CodeGenFunction &CGF) {
1222 CodeGenFunction::OMPPrivateScope SingleScope(CGF);
1223 HasFirstprivates = CGF.EmitOMPFirstprivateClause(S, SingleScope);
Alexey Bataev73870832015-04-27 04:12:12 +00001224 CGF.EmitOMPPrivateClause(S, SingleScope);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001225 (void)SingleScope.Privatize();
1226
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001227 CGF.EmitStmt(Stmt);
1228 CGF.EnsureInsertPoint();
1229 };
1230 CGF.CGM.getOpenMPRuntime().emitSingleRegion(CGF, CodeGen, S.getLocStart(),
1231 llvm::None, llvm::None,
1232 llvm::None, llvm::None);
Alexey Bataeva89adf22015-04-27 05:04:13 +00001233 // Emit barrier for firstprivates, lastprivates or reductions only if
1234 // 'sections' directive has 'nowait' clause. Otherwise the barrier will be
1235 // generated by the codegen for the directive.
1236 if ((HasFirstprivates || HasLastprivates || HasReductions) &&
1237 S.getSingleClause(OMPC_nowait)) {
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001238 // Emit implicit barrier to synchronize threads and avoid data races on
1239 // initialization of firstprivate variables.
1240 CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(),
1241 OMPD_unknown);
1242 }
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001243 return OMPD_single;
1244}
Alexey Bataev2df54a02015-03-12 08:53:29 +00001245
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001246void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
1247 LexicalScope Scope(*this, S.getSourceRange());
1248 OpenMPDirectiveKind EmittedAs = emitSections(*this, S);
Alexey Bataev2df54a02015-03-12 08:53:29 +00001249 // Emit an implicit barrier at the end.
Alexey Bataevf2685682015-03-30 04:30:22 +00001250 if (!S.getSingleClause(OMPC_nowait)) {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001251 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), EmittedAs);
Alexey Bataevf2685682015-03-30 04:30:22 +00001252 }
Alexey Bataev2df54a02015-03-12 08:53:29 +00001253}
1254
1255void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001256 LexicalScope Scope(*this, S.getSourceRange());
1257 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1258 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1259 CGF.EnsureInsertPoint();
1260 };
1261 CGM.getOpenMPRuntime().emitInlinedDirective(*this, CodeGen);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001262}
1263
Alexey Bataev6956e2e2015-02-05 06:35:41 +00001264void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00001265 llvm::SmallVector<const Expr *, 8> CopyprivateVars;
Alexey Bataev420d45b2015-04-14 05:11:24 +00001266 llvm::SmallVector<const Expr *, 8> DestExprs;
Alexey Bataeva63048e2015-03-23 06:18:07 +00001267 llvm::SmallVector<const Expr *, 8> SrcExprs;
Alexey Bataeva63048e2015-03-23 06:18:07 +00001268 llvm::SmallVector<const Expr *, 8> AssignmentOps;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001269 // Check if there are any 'copyprivate' clauses associated with this
1270 // 'single'
Alexey Bataeva63048e2015-03-23 06:18:07 +00001271 // construct.
Alexey Bataeva63048e2015-03-23 06:18:07 +00001272 // Build a list of copyprivate variables along with helper expressions
1273 // (<source>, <destination>, <destination>=<source> expressions)
Alexey Bataevc925aa32015-04-27 08:00:32 +00001274 for (auto &&I = S.getClausesOfKind(OMPC_copyprivate); I; ++I) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00001275 auto *C = cast<OMPCopyprivateClause>(*I);
1276 CopyprivateVars.append(C->varlists().begin(), C->varlists().end());
Alexey Bataev420d45b2015-04-14 05:11:24 +00001277 DestExprs.append(C->destination_exprs().begin(),
1278 C->destination_exprs().end());
Alexey Bataeva63048e2015-03-23 06:18:07 +00001279 SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end());
Alexey Bataeva63048e2015-03-23 06:18:07 +00001280 AssignmentOps.append(C->assignment_ops().begin(),
1281 C->assignment_ops().end());
1282 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001283 LexicalScope Scope(*this, S.getSourceRange());
Alexey Bataeva63048e2015-03-23 06:18:07 +00001284 // Emit code for 'single' region along with 'copyprivate' clauses
Alexey Bataev5521d782015-04-24 04:21:15 +00001285 bool HasFirstprivates;
1286 auto &&CodeGen = [&S, &HasFirstprivates](CodeGenFunction &CGF) {
1287 CodeGenFunction::OMPPrivateScope SingleScope(CGF);
1288 HasFirstprivates = CGF.EmitOMPFirstprivateClause(S, SingleScope);
Alexey Bataev59c654a2015-04-27 03:48:52 +00001289 CGF.EmitOMPPrivateClause(S, SingleScope);
Alexey Bataev5521d782015-04-24 04:21:15 +00001290 (void)SingleScope.Privatize();
1291
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001292 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1293 CGF.EnsureInsertPoint();
1294 };
1295 CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(),
Alexey Bataev420d45b2015-04-14 05:11:24 +00001296 CopyprivateVars, DestExprs, SrcExprs,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001297 AssignmentOps);
Alexey Bataev5521d782015-04-24 04:21:15 +00001298 // Emit an implicit barrier at the end (to avoid data race on firstprivate
1299 // init or if no 'nowait' clause was specified and no 'copyprivate' clause).
1300 if ((!S.getSingleClause(OMPC_nowait) || HasFirstprivates) &&
1301 CopyprivateVars.empty()) {
1302 CGM.getOpenMPRuntime().emitBarrierCall(
1303 *this, S.getLocStart(),
1304 S.getSingleClause(OMPC_nowait) ? OMPD_unknown : OMPD_single);
Alexey Bataevf2685682015-03-30 04:30:22 +00001305 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001306}
1307
Alexey Bataev8d690652014-12-04 07:23:53 +00001308void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001309 LexicalScope Scope(*this, S.getSourceRange());
1310 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1311 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1312 CGF.EnsureInsertPoint();
1313 };
1314 CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart());
Alexander Musman80c22892014-07-17 08:54:58 +00001315}
1316
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001317void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001318 LexicalScope Scope(*this, S.getSourceRange());
1319 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1320 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1321 CGF.EnsureInsertPoint();
1322 };
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001323 CGM.getOpenMPRuntime().emitCriticalRegion(
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001324 *this, S.getDirectiveName().getAsString(), CodeGen, S.getLocStart());
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001325}
1326
Alexey Bataev671605e2015-04-13 05:28:11 +00001327void CodeGenFunction::EmitOMPParallelForDirective(
1328 const OMPParallelForDirective &S) {
1329 // Emit directive as a combined directive that consists of two implicit
1330 // directives: 'parallel' with 'for' directive.
1331 LexicalScope Scope(*this, S.getSourceRange());
1332 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1333 CGF.EmitOMPWorksharingLoop(S);
1334 // Emit implicit barrier at the end of parallel region, but this barrier
1335 // is at the end of 'for' directive, so emit it as the implicit barrier for
1336 // this 'for' directive.
1337 CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(),
1338 OMPD_parallel);
1339 };
1340 emitCommonOMPParallelDirective(*this, S, CodeGen);
Alexey Bataev4acb8592014-07-07 13:01:15 +00001341}
1342
Alexander Musmane4e893b2014-09-23 09:33:00 +00001343void CodeGenFunction::EmitOMPParallelForSimdDirective(
1344 const OMPParallelForSimdDirective &) {
1345 llvm_unreachable("CodeGen for 'omp parallel for simd' is not supported yet.");
1346}
1347
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001348void CodeGenFunction::EmitOMPParallelSectionsDirective(
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001349 const OMPParallelSectionsDirective &S) {
1350 // Emit directive as a combined directive that consists of two implicit
1351 // directives: 'parallel' with 'sections' directive.
1352 LexicalScope Scope(*this, S.getSourceRange());
1353 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1354 (void)emitSections(CGF, S);
1355 // Emit implicit barrier at the end of parallel region.
1356 CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(),
1357 OMPD_parallel);
1358 };
1359 emitCommonOMPParallelDirective(*this, S, CodeGen);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001360}
1361
Alexey Bataev62b63b12015-03-10 07:28:44 +00001362void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) {
1363 // Emit outlined function for task construct.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001364 LexicalScope Scope(*this, S.getSourceRange());
Alexey Bataev62b63b12015-03-10 07:28:44 +00001365 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
1366 auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
1367 auto *I = CS->getCapturedDecl()->param_begin();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001368 auto *PartId = std::next(I);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001369 // The first function argument for tasks is a thread id, the second one is a
1370 // part id (0 for tied tasks, >=0 for untied task).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001371 auto &&CodeGen = [PartId, &S](CodeGenFunction &CGF) {
1372 if (*PartId) {
1373 // TODO: emit code for untied tasks.
1374 }
1375 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1376 };
Alexey Bataev62b63b12015-03-10 07:28:44 +00001377 auto OutlinedFn =
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001378 CGM.getOpenMPRuntime().emitTaskOutlinedFunction(S, *I, CodeGen);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001379 // Check if we should emit tied or untied task.
1380 bool Tied = !S.getSingleClause(OMPC_untied);
1381 // Check if the task is final
1382 llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
1383 if (auto *Clause = S.getSingleClause(OMPC_final)) {
1384 // If the condition constant folds and can be elided, try to avoid emitting
1385 // the condition and the dead arm of the if/else.
1386 auto *Cond = cast<OMPFinalClause>(Clause)->getCondition();
1387 bool CondConstant;
1388 if (ConstantFoldsToSimpleInteger(Cond, CondConstant))
1389 Final.setInt(CondConstant);
1390 else
1391 Final.setPointer(EvaluateExprAsBool(Cond));
1392 } else {
1393 // By default the task is not final.
1394 Final.setInt(/*IntVal=*/false);
1395 }
1396 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
Alexey Bataev1d677132015-04-22 13:57:31 +00001397 const Expr *IfCond = nullptr;
1398 if (auto C = S.getSingleClause(OMPC_if)) {
1399 IfCond = cast<OMPIfClause>(C)->getCondition();
1400 }
Alexey Bataev9e034042015-05-05 04:05:12 +00001401 llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
Alexey Bataev36c1eb92015-04-30 06:51:57 +00001402 // Get list of private variables.
1403 llvm::SmallVector<const Expr *, 8> Privates;
1404 llvm::SmallVector<const Expr *, 8> PrivateCopies;
Alexey Bataev36c1eb92015-04-30 06:51:57 +00001405 for (auto &&I = S.getClausesOfKind(OMPC_private); I; ++I) {
1406 auto *C = cast<OMPPrivateClause>(*I);
1407 auto IRef = C->varlist_begin();
1408 for (auto *IInit : C->private_copies()) {
1409 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
1410 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
1411 Privates.push_back(*IRef);
1412 PrivateCopies.push_back(IInit);
1413 }
1414 ++IRef;
1415 }
1416 }
Alexey Bataev9e034042015-05-05 04:05:12 +00001417 EmittedAsPrivate.clear();
1418 // Get list of firstprivate variables.
1419 llvm::SmallVector<const Expr *, 8> FirstprivateVars;
1420 llvm::SmallVector<const Expr *, 8> FirstprivateCopies;
1421 llvm::SmallVector<const Expr *, 8> FirstprivateInits;
1422 for (auto &&I = S.getClausesOfKind(OMPC_firstprivate); I; ++I) {
1423 auto *C = cast<OMPFirstprivateClause>(*I);
1424 auto IRef = C->varlist_begin();
1425 auto IElemInitRef = C->inits().begin();
1426 for (auto *IInit : C->private_copies()) {
1427 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
1428 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
1429 FirstprivateVars.push_back(*IRef);
1430 FirstprivateCopies.push_back(IInit);
1431 FirstprivateInits.push_back(*IElemInitRef);
1432 }
1433 ++IRef, ++IElemInitRef;
1434 }
1435 }
1436 CGM.getOpenMPRuntime().emitTaskCall(
1437 *this, S.getLocStart(), S, Tied, Final, OutlinedFn, SharedsTy,
1438 CapturedStruct, IfCond, Privates, PrivateCopies, FirstprivateVars,
1439 FirstprivateCopies, FirstprivateInits);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001440}
1441
Alexey Bataev9f797f32015-02-05 05:57:51 +00001442void CodeGenFunction::EmitOMPTaskyieldDirective(
1443 const OMPTaskyieldDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001444 CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart());
Alexey Bataev68446b72014-07-18 07:47:19 +00001445}
1446
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00001447void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) {
Alexey Bataevf2685682015-03-30 04:30:22 +00001448 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier);
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001449}
1450
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001451void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) {
1452 CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart());
Alexey Bataev2df347a2014-07-18 10:17:07 +00001453}
1454
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001455void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001456 CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> {
1457 if (auto C = S.getSingleClause(/*K*/ OMPC_flush)) {
1458 auto FlushClause = cast<OMPFlushClause>(C);
1459 return llvm::makeArrayRef(FlushClause->varlist_begin(),
1460 FlushClause->varlist_end());
1461 }
1462 return llvm::None;
1463 }(), S.getLocStart());
Alexey Bataev6125da92014-07-21 11:26:11 +00001464}
1465
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001466void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) {
1467 LexicalScope Scope(*this, S.getSourceRange());
1468 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1469 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1470 CGF.EnsureInsertPoint();
1471 };
1472 CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001473}
1474
Alexey Bataevb57056f2015-01-22 06:17:56 +00001475static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val,
1476 QualType SrcType, QualType DestType) {
1477 assert(CGF.hasScalarEvaluationKind(DestType) &&
1478 "DestType must have scalar evaluation kind.");
1479 assert(!Val.isAggregate() && "Must be a scalar or complex.");
1480 return Val.isScalar()
1481 ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType)
1482 : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType,
1483 DestType);
1484}
1485
1486static CodeGenFunction::ComplexPairTy
1487convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType,
1488 QualType DestType) {
1489 assert(CGF.getEvaluationKind(DestType) == TEK_Complex &&
1490 "DestType must have complex evaluation kind.");
1491 CodeGenFunction::ComplexPairTy ComplexVal;
1492 if (Val.isScalar()) {
1493 // Convert the input element to the element type of the complex.
1494 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
1495 auto ScalarVal =
1496 CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestElementType);
1497 ComplexVal = CodeGenFunction::ComplexPairTy(
1498 ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType()));
1499 } else {
1500 assert(Val.isComplex() && "Must be a scalar or complex.");
1501 auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType();
1502 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
1503 ComplexVal.first = CGF.EmitScalarConversion(
1504 Val.getComplexVal().first, SrcElementType, DestElementType);
1505 ComplexVal.second = CGF.EmitScalarConversion(
1506 Val.getComplexVal().second, SrcElementType, DestElementType);
1507 }
1508 return ComplexVal;
1509}
1510
Alexey Bataev5e018f92015-04-23 06:35:10 +00001511static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst,
1512 LValue LVal, RValue RVal) {
1513 if (LVal.isGlobalReg()) {
1514 CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal);
1515 } else {
1516 CGF.EmitAtomicStore(RVal, LVal, IsSeqCst ? llvm::SequentiallyConsistent
1517 : llvm::Monotonic,
1518 LVal.isVolatile(), /*IsInit=*/false);
1519 }
1520}
1521
1522static void emitSimpleStore(CodeGenFunction &CGF, LValue LVal, RValue RVal,
1523 QualType RValTy) {
1524 switch (CGF.getEvaluationKind(LVal.getType())) {
1525 case TEK_Scalar:
1526 CGF.EmitStoreThroughLValue(
1527 RValue::get(convertToScalarValue(CGF, RVal, RValTy, LVal.getType())),
1528 LVal);
1529 break;
1530 case TEK_Complex:
1531 CGF.EmitStoreOfComplex(
1532 convertToComplexValue(CGF, RVal, RValTy, LVal.getType()), LVal,
1533 /*isInit=*/false);
1534 break;
1535 case TEK_Aggregate:
1536 llvm_unreachable("Must be a scalar or complex.");
1537 }
1538}
1539
Alexey Bataevb57056f2015-01-22 06:17:56 +00001540static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst,
1541 const Expr *X, const Expr *V,
1542 SourceLocation Loc) {
1543 // v = x;
1544 assert(V->isLValue() && "V of 'omp atomic read' is not lvalue");
1545 assert(X->isLValue() && "X of 'omp atomic read' is not lvalue");
1546 LValue XLValue = CGF.EmitLValue(X);
1547 LValue VLValue = CGF.EmitLValue(V);
David Majnemera5b195a2015-02-14 01:35:12 +00001548 RValue Res = XLValue.isGlobalReg()
1549 ? CGF.EmitLoadOfLValue(XLValue, Loc)
1550 : CGF.EmitAtomicLoad(XLValue, Loc,
1551 IsSeqCst ? llvm::SequentiallyConsistent
Alexey Bataevb8329262015-02-27 06:33:30 +00001552 : llvm::Monotonic,
1553 XLValue.isVolatile());
Alexey Bataevb57056f2015-01-22 06:17:56 +00001554 // OpenMP, 2.12.6, atomic Construct
1555 // Any atomic construct with a seq_cst clause forces the atomically
1556 // performed operation to include an implicit flush operation without a
1557 // list.
1558 if (IsSeqCst)
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001559 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
Alexey Bataev5e018f92015-04-23 06:35:10 +00001560 emitSimpleStore(CGF,VLValue, Res, X->getType().getNonReferenceType());
Alexey Bataevb57056f2015-01-22 06:17:56 +00001561}
1562
Alexey Bataevb8329262015-02-27 06:33:30 +00001563static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst,
1564 const Expr *X, const Expr *E,
1565 SourceLocation Loc) {
1566 // x = expr;
1567 assert(X->isLValue() && "X of 'omp atomic write' is not lvalue");
Alexey Bataev5e018f92015-04-23 06:35:10 +00001568 emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E));
Alexey Bataevb8329262015-02-27 06:33:30 +00001569 // OpenMP, 2.12.6, atomic Construct
1570 // Any atomic construct with a seq_cst clause forces the atomically
1571 // performed operation to include an implicit flush operation without a
1572 // list.
1573 if (IsSeqCst)
1574 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
1575}
1576
Benjamin Kramer439ee9d2015-05-01 13:59:53 +00001577static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X,
1578 RValue Update,
1579 BinaryOperatorKind BO,
1580 llvm::AtomicOrdering AO,
1581 bool IsXLHSInRHSPart) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001582 auto &Context = CGF.CGM.getContext();
1583 // Allow atomicrmw only if 'x' and 'update' are integer values, lvalue for 'x'
Alexey Bataevb4505a72015-03-30 05:20:59 +00001584 // expression is simple and atomic is allowed for the given type for the
1585 // target platform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001586 if (BO == BO_Comma || !Update.isScalar() ||
1587 !Update.getScalarVal()->getType()->isIntegerTy() || !X.isSimple() ||
1588 (!isa<llvm::ConstantInt>(Update.getScalarVal()) &&
1589 (Update.getScalarVal()->getType() !=
1590 X.getAddress()->getType()->getPointerElementType())) ||
1591 !Context.getTargetInfo().hasBuiltinAtomic(
1592 Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment())))
Alexey Bataev5e018f92015-04-23 06:35:10 +00001593 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001594
1595 llvm::AtomicRMWInst::BinOp RMWOp;
1596 switch (BO) {
1597 case BO_Add:
1598 RMWOp = llvm::AtomicRMWInst::Add;
1599 break;
1600 case BO_Sub:
1601 if (!IsXLHSInRHSPart)
Alexey Bataev5e018f92015-04-23 06:35:10 +00001602 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001603 RMWOp = llvm::AtomicRMWInst::Sub;
1604 break;
1605 case BO_And:
1606 RMWOp = llvm::AtomicRMWInst::And;
1607 break;
1608 case BO_Or:
1609 RMWOp = llvm::AtomicRMWInst::Or;
1610 break;
1611 case BO_Xor:
1612 RMWOp = llvm::AtomicRMWInst::Xor;
1613 break;
1614 case BO_LT:
1615 RMWOp = X.getType()->hasSignedIntegerRepresentation()
1616 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min
1617 : llvm::AtomicRMWInst::Max)
1618 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin
1619 : llvm::AtomicRMWInst::UMax);
1620 break;
1621 case BO_GT:
1622 RMWOp = X.getType()->hasSignedIntegerRepresentation()
1623 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max
1624 : llvm::AtomicRMWInst::Min)
1625 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax
1626 : llvm::AtomicRMWInst::UMin);
1627 break;
Alexey Bataev5e018f92015-04-23 06:35:10 +00001628 case BO_Assign:
1629 RMWOp = llvm::AtomicRMWInst::Xchg;
1630 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001631 case BO_Mul:
1632 case BO_Div:
1633 case BO_Rem:
1634 case BO_Shl:
1635 case BO_Shr:
1636 case BO_LAnd:
1637 case BO_LOr:
Alexey Bataev5e018f92015-04-23 06:35:10 +00001638 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001639 case BO_PtrMemD:
1640 case BO_PtrMemI:
1641 case BO_LE:
1642 case BO_GE:
1643 case BO_EQ:
1644 case BO_NE:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001645 case BO_AddAssign:
1646 case BO_SubAssign:
1647 case BO_AndAssign:
1648 case BO_OrAssign:
1649 case BO_XorAssign:
1650 case BO_MulAssign:
1651 case BO_DivAssign:
1652 case BO_RemAssign:
1653 case BO_ShlAssign:
1654 case BO_ShrAssign:
1655 case BO_Comma:
1656 llvm_unreachable("Unsupported atomic update operation");
1657 }
1658 auto *UpdateVal = Update.getScalarVal();
1659 if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) {
1660 UpdateVal = CGF.Builder.CreateIntCast(
1661 IC, X.getAddress()->getType()->getPointerElementType(),
1662 X.getType()->hasSignedIntegerRepresentation());
1663 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00001664 auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getAddress(), UpdateVal, AO);
1665 return std::make_pair(true, RValue::get(Res));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001666}
1667
Alexey Bataev5e018f92015-04-23 06:35:10 +00001668std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr(
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001669 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
1670 llvm::AtomicOrdering AO, SourceLocation Loc,
1671 const llvm::function_ref<RValue(RValue)> &CommonGen) {
1672 // Update expressions are allowed to have the following forms:
1673 // x binop= expr; -> xrval + expr;
1674 // x++, ++x -> xrval + 1;
1675 // x--, --x -> xrval - 1;
1676 // x = x binop expr; -> xrval binop expr
1677 // x = expr Op x; - > expr binop xrval;
Alexey Bataev5e018f92015-04-23 06:35:10 +00001678 auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart);
1679 if (!Res.first) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001680 if (X.isGlobalReg()) {
1681 // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop
1682 // 'xrval'.
1683 EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X);
1684 } else {
1685 // Perform compare-and-swap procedure.
1686 EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified());
Alexey Bataevb4505a72015-03-30 05:20:59 +00001687 }
1688 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00001689 return Res;
Alexey Bataevb4505a72015-03-30 05:20:59 +00001690}
1691
1692static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst,
1693 const Expr *X, const Expr *E,
1694 const Expr *UE, bool IsXLHSInRHSPart,
1695 SourceLocation Loc) {
1696 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
1697 "Update expr in 'atomic update' must be a binary operator.");
1698 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
1699 // Update expressions are allowed to have the following forms:
1700 // x binop= expr; -> xrval + expr;
1701 // x++, ++x -> xrval + 1;
1702 // x--, --x -> xrval - 1;
1703 // x = x binop expr; -> xrval binop expr
1704 // x = expr Op x; - > expr binop xrval;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001705 assert(X->isLValue() && "X of 'omp atomic update' is not lvalue");
Alexey Bataevb4505a72015-03-30 05:20:59 +00001706 LValue XLValue = CGF.EmitLValue(X);
1707 RValue ExprRValue = CGF.EmitAnyExpr(E);
Alexey Bataevb4505a72015-03-30 05:20:59 +00001708 auto AO = IsSeqCst ? llvm::SequentiallyConsistent : llvm::Monotonic;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001709 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
1710 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
1711 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
1712 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
1713 auto Gen =
1714 [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue {
1715 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
1716 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
1717 return CGF.EmitAnyExpr(UE);
1718 };
Alexey Bataev5e018f92015-04-23 06:35:10 +00001719 (void)CGF.EmitOMPAtomicSimpleUpdateExpr(
1720 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
1721 // OpenMP, 2.12.6, atomic Construct
1722 // Any atomic construct with a seq_cst clause forces the atomically
1723 // performed operation to include an implicit flush operation without a
1724 // list.
1725 if (IsSeqCst)
1726 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
1727}
1728
1729static RValue convertToType(CodeGenFunction &CGF, RValue Value,
1730 QualType SourceType, QualType ResType) {
1731 switch (CGF.getEvaluationKind(ResType)) {
1732 case TEK_Scalar:
1733 return RValue::get(convertToScalarValue(CGF, Value, SourceType, ResType));
1734 case TEK_Complex: {
1735 auto Res = convertToComplexValue(CGF, Value, SourceType, ResType);
1736 return RValue::getComplex(Res.first, Res.second);
1737 }
1738 case TEK_Aggregate:
1739 break;
1740 }
1741 llvm_unreachable("Must be a scalar or complex.");
1742}
1743
1744static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst,
1745 bool IsPostfixUpdate, const Expr *V,
1746 const Expr *X, const Expr *E,
1747 const Expr *UE, bool IsXLHSInRHSPart,
1748 SourceLocation Loc) {
1749 assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue");
1750 assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue");
1751 RValue NewVVal;
1752 LValue VLValue = CGF.EmitLValue(V);
1753 LValue XLValue = CGF.EmitLValue(X);
1754 RValue ExprRValue = CGF.EmitAnyExpr(E);
1755 auto AO = IsSeqCst ? llvm::SequentiallyConsistent : llvm::Monotonic;
1756 QualType NewVValType;
1757 if (UE) {
1758 // 'x' is updated with some additional value.
1759 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
1760 "Update expr in 'atomic capture' must be a binary operator.");
1761 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
1762 // Update expressions are allowed to have the following forms:
1763 // x binop= expr; -> xrval + expr;
1764 // x++, ++x -> xrval + 1;
1765 // x--, --x -> xrval - 1;
1766 // x = x binop expr; -> xrval binop expr
1767 // x = expr Op x; - > expr binop xrval;
1768 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
1769 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
1770 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
1771 NewVValType = XRValExpr->getType();
1772 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
1773 auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr,
1774 IsSeqCst, IsPostfixUpdate](RValue XRValue) -> RValue {
1775 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
1776 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
1777 RValue Res = CGF.EmitAnyExpr(UE);
1778 NewVVal = IsPostfixUpdate ? XRValue : Res;
1779 return Res;
1780 };
1781 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
1782 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
1783 if (Res.first) {
1784 // 'atomicrmw' instruction was generated.
1785 if (IsPostfixUpdate) {
1786 // Use old value from 'atomicrmw'.
1787 NewVVal = Res.second;
1788 } else {
1789 // 'atomicrmw' does not provide new value, so evaluate it using old
1790 // value of 'x'.
1791 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
1792 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second);
1793 NewVVal = CGF.EmitAnyExpr(UE);
1794 }
1795 }
1796 } else {
1797 // 'x' is simply rewritten with some 'expr'.
1798 NewVValType = X->getType().getNonReferenceType();
1799 ExprRValue = convertToType(CGF, ExprRValue, E->getType(),
1800 X->getType().getNonReferenceType());
1801 auto &&Gen = [&CGF, &NewVVal, ExprRValue](RValue XRValue) -> RValue {
1802 NewVVal = XRValue;
1803 return ExprRValue;
1804 };
1805 // Try to perform atomicrmw xchg, otherwise simple exchange.
1806 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
1807 XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO,
1808 Loc, Gen);
1809 if (Res.first) {
1810 // 'atomicrmw' instruction was generated.
1811 NewVVal = IsPostfixUpdate ? Res.second : ExprRValue;
1812 }
1813 }
1814 // Emit post-update store to 'v' of old/new 'x' value.
1815 emitSimpleStore(CGF, VLValue, NewVVal, NewVValType);
Alexey Bataevb4505a72015-03-30 05:20:59 +00001816 // OpenMP, 2.12.6, atomic Construct
1817 // Any atomic construct with a seq_cst clause forces the atomically
1818 // performed operation to include an implicit flush operation without a
1819 // list.
1820 if (IsSeqCst)
1821 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
1822}
1823
Alexey Bataevb57056f2015-01-22 06:17:56 +00001824static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind,
Alexey Bataev5e018f92015-04-23 06:35:10 +00001825 bool IsSeqCst, bool IsPostfixUpdate,
1826 const Expr *X, const Expr *V, const Expr *E,
1827 const Expr *UE, bool IsXLHSInRHSPart,
1828 SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001829 switch (Kind) {
1830 case OMPC_read:
1831 EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc);
1832 break;
1833 case OMPC_write:
Alexey Bataevb8329262015-02-27 06:33:30 +00001834 EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc);
1835 break;
Alexey Bataevb4505a72015-03-30 05:20:59 +00001836 case OMPC_unknown:
Alexey Bataevb57056f2015-01-22 06:17:56 +00001837 case OMPC_update:
Alexey Bataevb4505a72015-03-30 05:20:59 +00001838 EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc);
1839 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00001840 case OMPC_capture:
Alexey Bataev5e018f92015-04-23 06:35:10 +00001841 EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE,
1842 IsXLHSInRHSPart, Loc);
1843 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00001844 case OMPC_if:
1845 case OMPC_final:
1846 case OMPC_num_threads:
1847 case OMPC_private:
1848 case OMPC_firstprivate:
1849 case OMPC_lastprivate:
1850 case OMPC_reduction:
1851 case OMPC_safelen:
1852 case OMPC_collapse:
1853 case OMPC_default:
1854 case OMPC_seq_cst:
1855 case OMPC_shared:
1856 case OMPC_linear:
1857 case OMPC_aligned:
1858 case OMPC_copyin:
1859 case OMPC_copyprivate:
1860 case OMPC_flush:
1861 case OMPC_proc_bind:
1862 case OMPC_schedule:
1863 case OMPC_ordered:
1864 case OMPC_nowait:
1865 case OMPC_untied:
1866 case OMPC_threadprivate:
1867 case OMPC_mergeable:
Alexey Bataevb57056f2015-01-22 06:17:56 +00001868 llvm_unreachable("Clause is not allowed in 'omp atomic'.");
1869 }
1870}
1871
1872void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) {
1873 bool IsSeqCst = S.getSingleClause(/*K=*/OMPC_seq_cst);
1874 OpenMPClauseKind Kind = OMPC_unknown;
1875 for (auto *C : S.clauses()) {
1876 // Find first clause (skip seq_cst clause, if it is first).
1877 if (C->getClauseKind() != OMPC_seq_cst) {
1878 Kind = C->getClauseKind();
1879 break;
1880 }
1881 }
Alexey Bataev10fec572015-03-11 04:48:56 +00001882
1883 const auto *CS =
1884 S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev5e018f92015-04-23 06:35:10 +00001885 if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) {
Alexey Bataev10fec572015-03-11 04:48:56 +00001886 enterFullExpression(EWC);
Alexey Bataev5e018f92015-04-23 06:35:10 +00001887 }
1888 // Processing for statements under 'atomic capture'.
1889 if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) {
1890 for (const auto *C : Compound->body()) {
1891 if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) {
1892 enterFullExpression(EWC);
1893 }
1894 }
1895 }
Alexey Bataev10fec572015-03-11 04:48:56 +00001896
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001897 LexicalScope Scope(*this, S.getSourceRange());
1898 auto &&CodeGen = [&S, Kind, IsSeqCst](CodeGenFunction &CGF) {
Alexey Bataev5e018f92015-04-23 06:35:10 +00001899 EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(),
1900 S.getV(), S.getExpr(), S.getUpdateExpr(),
1901 S.isXLHSInRHSPart(), S.getLocStart());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001902 };
1903 CGM.getOpenMPRuntime().emitInlinedDirective(*this, CodeGen);
Alexey Bataev0162e452014-07-22 10:10:35 +00001904}
1905
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001906void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &) {
1907 llvm_unreachable("CodeGen for 'omp target' is not supported yet.");
1908}
1909
Alexey Bataev13314bf2014-10-09 04:18:56 +00001910void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &) {
1911 llvm_unreachable("CodeGen for 'omp teams' is not supported yet.");
1912}