blob: 04b4731e8533bdb8d0ee0274d76fbe8adba9d290 [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();
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000135 QualType Type = OrigVD->getType();
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000136 if (Type->isArrayType()) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000137 // Emit VarDecl with copy init for arrays.
138 // Get the address of the original variable captured in current
139 // captured region.
140 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value *{
141 auto Emission = EmitAutoVarAlloca(*VD);
142 auto *Init = VD->getInit();
143 if (!isa<CXXConstructExpr>(Init) || isTrivialInitializer(Init)) {
144 // Perform simple memcpy.
145 EmitAggregateAssign(Emission.getAllocatedAddress(), OriginalAddr,
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000146 Type);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000147 } else {
148 EmitOMPAggregateAssign(
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000149 Emission.getAllocatedAddress(), OriginalAddr, Type,
Alexey Bataev69c62a92015-04-15 04:52:20 +0000150 [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());
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000228 QualType Type = VD->getType();
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000229 if (CopiedVars.insert(VD->getCanonicalDecl()).second) {
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000230
231 // Get the address of the master variable. If we are emitting code with
232 // TLS support, the address is passed from the master as field in the
233 // captured declaration.
234 llvm::Value *MasterAddr;
235 if (getLangOpts().OpenMPUseTLS &&
236 getContext().getTargetInfo().isTLSSupported()) {
237 assert(CapturedStmtInfo->lookup(VD) &&
238 "Copyin threadprivates should have been captured!");
239 DeclRefExpr DRE(const_cast<VarDecl *>(VD), true, (*IRef)->getType(),
240 VK_LValue, (*IRef)->getExprLoc());
241 MasterAddr = EmitLValue(&DRE).getAddress();
242 } else {
243 MasterAddr = VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD)
244 : CGM.GetAddrOfGlobal(VD);
245 }
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000246 // Get the address of the threadprivate variable.
247 auto *PrivateAddr = EmitLValue(*IRef).getAddress();
248 if (CopiedVars.size() == 1) {
249 // At first check if current thread is a master thread. If it is, no
250 // need to copy data.
251 CopyBegin = createBasicBlock("copyin.not.master");
252 CopyEnd = createBasicBlock("copyin.not.master.end");
253 Builder.CreateCondBr(
254 Builder.CreateICmpNE(
255 Builder.CreatePtrToInt(MasterAddr, CGM.IntPtrTy),
256 Builder.CreatePtrToInt(PrivateAddr, CGM.IntPtrTy)),
257 CopyBegin, CopyEnd);
258 EmitBlock(CopyBegin);
259 }
260 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
261 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000262 EmitOMPCopy(*this, Type, PrivateAddr, MasterAddr, DestVD, SrcVD,
263 AssignOp);
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000264 }
265 ++IRef;
266 ++ISrcRef;
267 ++IDestRef;
268 }
269 }
270 if (CopyEnd) {
271 // Exit out of copying procedure for non-master thread.
272 EmitBlock(CopyEnd, /*IsFinished=*/true);
273 return true;
274 }
275 return false;
276}
277
Alexey Bataev38e89532015-04-16 04:54:05 +0000278bool CodeGenFunction::EmitOMPLastprivateClauseInit(
279 const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000280 bool HasAtLeastOneLastprivate = false;
281 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
Alexey Bataevc925aa32015-04-27 08:00:32 +0000282 for (auto &&I = D.getClausesOfKind(OMPC_lastprivate); I; ++I) {
Alexey Bataevd130fd12015-05-13 10:23:02 +0000283 HasAtLeastOneLastprivate = true;
Alexey Bataev38e89532015-04-16 04:54:05 +0000284 auto *C = cast<OMPLastprivateClause>(*I);
285 auto IRef = C->varlist_begin();
286 auto IDestRef = C->destination_exprs().begin();
287 for (auto *IInit : C->private_copies()) {
288 // Keep the address of the original variable for future update at the end
289 // of the loop.
290 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
291 if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) {
292 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
293 PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() -> llvm::Value *{
294 DeclRefExpr DRE(
295 const_cast<VarDecl *>(OrigVD),
296 /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup(
297 OrigVD) != nullptr,
298 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
299 return EmitLValue(&DRE).getAddress();
300 });
301 // Check if the variable is also a firstprivate: in this case IInit is
302 // not generated. Initialization of this variable will happen in codegen
303 // for 'firstprivate' clause.
Alexey Bataevd130fd12015-05-13 10:23:02 +0000304 if (IInit) {
305 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
306 bool IsRegistered =
307 PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value *{
308 // Emit private VarDecl with copy init.
309 EmitDecl(*VD);
310 return GetAddrOfLocalVar(VD);
311 });
312 assert(IsRegistered &&
313 "lastprivate var already registered as private");
314 (void)IsRegistered;
315 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000316 }
317 ++IRef, ++IDestRef;
318 }
319 }
320 return HasAtLeastOneLastprivate;
321}
322
323void CodeGenFunction::EmitOMPLastprivateClauseFinal(
324 const OMPExecutableDirective &D, llvm::Value *IsLastIterCond) {
325 // Emit following code:
326 // if (<IsLastIterCond>) {
327 // orig_var1 = private_orig_var1;
328 // ...
329 // orig_varn = private_orig_varn;
330 // }
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000331 llvm::BasicBlock *ThenBB = nullptr;
332 llvm::BasicBlock *DoneBB = nullptr;
333 if (IsLastIterCond) {
334 ThenBB = createBasicBlock(".omp.lastprivate.then");
335 DoneBB = createBasicBlock(".omp.lastprivate.done");
336 Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB);
337 EmitBlock(ThenBB);
338 }
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000339 llvm::DenseMap<const Decl *, const Expr *> LoopCountersAndUpdates;
340 const Expr *LastIterVal = nullptr;
341 const Expr *IVExpr = nullptr;
342 const Expr *IncExpr = nullptr;
343 if (auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) {
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000344 if (isOpenMPWorksharingDirective(D.getDirectiveKind())) {
345 LastIterVal = cast<VarDecl>(cast<DeclRefExpr>(
346 LoopDirective->getUpperBoundVariable())
347 ->getDecl())
348 ->getAnyInitializer();
349 IVExpr = LoopDirective->getIterationVariable();
350 IncExpr = LoopDirective->getInc();
351 auto IUpdate = LoopDirective->updates().begin();
352 for (auto *E : LoopDirective->counters()) {
353 auto *D = cast<DeclRefExpr>(E)->getDecl()->getCanonicalDecl();
354 LoopCountersAndUpdates[D] = *IUpdate;
355 ++IUpdate;
356 }
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000357 }
358 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000359 {
Alexey Bataev38e89532015-04-16 04:54:05 +0000360 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000361 bool FirstLCV = true;
Alexey Bataevc925aa32015-04-27 08:00:32 +0000362 for (auto &&I = D.getClausesOfKind(OMPC_lastprivate); I; ++I) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000363 auto *C = cast<OMPLastprivateClause>(*I);
364 auto IRef = C->varlist_begin();
365 auto ISrcRef = C->source_exprs().begin();
366 auto IDestRef = C->destination_exprs().begin();
367 for (auto *AssignOp : C->assignment_ops()) {
368 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000369 QualType Type = PrivateVD->getType();
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000370 auto *CanonicalVD = PrivateVD->getCanonicalDecl();
371 if (AlreadyEmittedVars.insert(CanonicalVD).second) {
372 // If lastprivate variable is a loop control variable for loop-based
373 // directive, update its value before copyin back to original
374 // variable.
375 if (auto *UpExpr = LoopCountersAndUpdates.lookup(CanonicalVD)) {
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000376 if (FirstLCV && LastIterVal) {
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000377 EmitAnyExprToMem(LastIterVal, EmitLValue(IVExpr).getAddress(),
378 IVExpr->getType().getQualifiers(),
379 /*IsInitializer=*/false);
380 EmitIgnoredExpr(IncExpr);
381 FirstLCV = false;
382 }
383 EmitIgnoredExpr(UpExpr);
384 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000385 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
386 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
387 // Get the address of the original variable.
388 auto *OriginalAddr = GetAddrOfLocalVar(DestVD);
389 // Get the address of the private variable.
390 auto *PrivateAddr = GetAddrOfLocalVar(PrivateVD);
Alexey Bataev1d9c15c2015-05-19 12:31:28 +0000391 EmitOMPCopy(*this, Type, OriginalAddr, PrivateAddr, DestVD, SrcVD,
392 AssignOp);
Alexey Bataev38e89532015-04-16 04:54:05 +0000393 }
394 ++IRef;
395 ++ISrcRef;
396 ++IDestRef;
397 }
398 }
399 }
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000400 if (IsLastIterCond) {
401 EmitBlock(DoneBB, /*IsFinished=*/true);
402 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000403}
404
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000405void CodeGenFunction::EmitOMPReductionClauseInit(
406 const OMPExecutableDirective &D,
407 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataevc925aa32015-04-27 08:00:32 +0000408 for (auto &&I = D.getClausesOfKind(OMPC_reduction); I; ++I) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000409 auto *C = cast<OMPReductionClause>(*I);
410 auto ILHS = C->lhs_exprs().begin();
411 auto IRHS = C->rhs_exprs().begin();
412 for (auto IRef : C->varlists()) {
413 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl());
414 auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
415 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
416 // Store the address of the original variable associated with the LHS
417 // implicit variable.
418 PrivateScope.addPrivate(LHSVD, [this, OrigVD, IRef]() -> llvm::Value *{
419 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
420 CapturedStmtInfo->lookup(OrigVD) != nullptr,
421 IRef->getType(), VK_LValue, IRef->getExprLoc());
422 return EmitLValue(&DRE).getAddress();
423 });
424 // Emit reduction copy.
425 bool IsRegistered =
426 PrivateScope.addPrivate(OrigVD, [this, PrivateVD]() -> llvm::Value *{
427 // Emit private VarDecl with reduction init.
428 EmitDecl(*PrivateVD);
429 return GetAddrOfLocalVar(PrivateVD);
430 });
431 assert(IsRegistered && "private var already registered as private");
432 // Silence the warning about unused variable.
433 (void)IsRegistered;
434 ++ILHS, ++IRHS;
435 }
436 }
437}
438
439void CodeGenFunction::EmitOMPReductionClauseFinal(
440 const OMPExecutableDirective &D) {
441 llvm::SmallVector<const Expr *, 8> LHSExprs;
442 llvm::SmallVector<const Expr *, 8> RHSExprs;
443 llvm::SmallVector<const Expr *, 8> ReductionOps;
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000444 bool HasAtLeastOneReduction = false;
Alexey Bataevc925aa32015-04-27 08:00:32 +0000445 for (auto &&I = D.getClausesOfKind(OMPC_reduction); I; ++I) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000446 HasAtLeastOneReduction = true;
447 auto *C = cast<OMPReductionClause>(*I);
448 LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end());
449 RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end());
450 ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end());
451 }
452 if (HasAtLeastOneReduction) {
453 // Emit nowait reduction if nowait clause is present or directive is a
454 // parallel directive (it always has implicit barrier).
455 CGM.getOpenMPRuntime().emitReduction(
456 *this, D.getLocEnd(), LHSExprs, RHSExprs, ReductionOps,
457 D.getSingleClause(OMPC_nowait) ||
Alexey Bataev89e7e8e2015-06-17 06:21:39 +0000458 isOpenMPParallelDirective(D.getDirectiveKind()) ||
459 D.getDirectiveKind() == OMPD_simd,
460 D.getDirectiveKind() == OMPD_simd);
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000461 }
462}
463
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000464static void emitCommonOMPParallelDirective(CodeGenFunction &CGF,
465 const OMPExecutableDirective &S,
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000466 OpenMPDirectiveKind InnermostKind,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000467 const RegionCodeGenTy &CodeGen) {
Alexey Bataev18095712014-10-10 12:19:54 +0000468 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000469 auto CapturedStruct = CGF.GenerateCapturedStmtArgument(*CS);
470 auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction(
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000471 S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
Alexey Bataev1d677132015-04-22 13:57:31 +0000472 if (auto C = S.getSingleClause(OMPC_num_threads)) {
473 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
474 auto NumThreadsClause = cast<OMPNumThreadsClause>(C);
475 auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
476 /*IgnoreResultAssign*/ true);
477 CGF.CGM.getOpenMPRuntime().emitNumThreadsClause(
478 CGF, NumThreads, NumThreadsClause->getLocStart());
479 }
Alexey Bataev7f210c62015-06-18 13:40:03 +0000480 if (auto *C = S.getSingleClause(OMPC_proc_bind)) {
481 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
482 auto *ProcBindClause = cast<OMPProcBindClause>(C);
483 CGF.CGM.getOpenMPRuntime().emitProcBindClause(
484 CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getLocStart());
485 }
Alexey Bataev1d677132015-04-22 13:57:31 +0000486 const Expr *IfCond = nullptr;
487 if (auto C = S.getSingleClause(OMPC_if)) {
488 IfCond = cast<OMPIfClause>(C)->getCondition();
489 }
490 CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn,
491 CapturedStruct, IfCond);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000492}
493
494void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
495 LexicalScope Scope(*this, S.getSourceRange());
496 // Emit parallel region as a standalone region.
497 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
498 OMPPrivateScope PrivateScope(CGF);
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000499 bool Copyins = CGF.EmitOMPCopyinClause(S);
500 bool Firstprivates = CGF.EmitOMPFirstprivateClause(S, PrivateScope);
501 if (Copyins || Firstprivates) {
Alexey Bataev69c62a92015-04-15 04:52:20 +0000502 // Emit implicit barrier to synchronize threads and avoid data races on
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000503 // initialization of firstprivate variables or propagation master's thread
504 // values of threadprivate variables to local instances of that variables
505 // of all other implicit threads.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000506 CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(),
507 OMPD_unknown);
Alexey Bataev69c62a92015-04-15 04:52:20 +0000508 }
509 CGF.EmitOMPPrivateClause(S, PrivateScope);
510 CGF.EmitOMPReductionClauseInit(S, PrivateScope);
511 (void)PrivateScope.Privatize();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000512 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
Alexey Bataev794ba0d2015-04-10 10:43:45 +0000513 CGF.EmitOMPReductionClauseFinal(S);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000514 // Emit implicit barrier at the end of the 'parallel' directive.
515 CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(),
516 OMPD_unknown);
517 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000518 emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen);
Alexey Bataev9959db52014-05-06 10:08:46 +0000519}
Alexander Musman515ad8c2014-05-22 08:54:05 +0000520
Alexey Bataev0f34da12015-07-02 04:17:07 +0000521void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D,
522 JumpDest LoopExit) {
Alexander Musmana5f070a2014-10-01 06:03:56 +0000523 RunCleanupsScope BodyScope(*this);
524 // Update counters values on current iteration.
Alexey Bataev58e5bdb2015-06-18 04:45:29 +0000525 for (auto I : D.updates()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +0000526 EmitIgnoredExpr(I);
527 }
Alexander Musman3276a272015-03-21 10:12:56 +0000528 // Update the linear variables.
Alexey Bataev58e5bdb2015-06-18 04:45:29 +0000529 for (auto &&I = D.getClausesOfKind(OMPC_linear); I; ++I) {
Alexey Bataevc925aa32015-04-27 08:00:32 +0000530 auto *C = cast<OMPLinearClause>(*I);
Alexander Musman3276a272015-03-21 10:12:56 +0000531 for (auto U : C->updates()) {
532 EmitIgnoredExpr(U);
533 }
534 }
535
Alexander Musmana5f070a2014-10-01 06:03:56 +0000536 // On a continue in the body, jump to the end.
Alexander Musmand196ef22014-10-07 08:57:09 +0000537 auto Continue = getJumpDestInCurrentScope("omp.body.continue");
Alexey Bataev0f34da12015-07-02 04:17:07 +0000538 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
Alexander Musmana5f070a2014-10-01 06:03:56 +0000539 // Emit loop body.
Alexey Bataev58e5bdb2015-06-18 04:45:29 +0000540 EmitStmt(D.getBody());
Alexander Musmana5f070a2014-10-01 06:03:56 +0000541 // The end (updates/cleanups).
542 EmitBlock(Continue.getBlock());
543 BreakContinueStack.pop_back();
Alexander Musmana5f070a2014-10-01 06:03:56 +0000544 // TODO: Update lastprivates if the SeparateIter flag is true.
545 // This will be implemented in a follow-up OMPLastprivateClause patch, but
546 // result should be still correct without it, as we do not make these
547 // variables private yet.
Alexander Musmana5f070a2014-10-01 06:03:56 +0000548}
549
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000550void CodeGenFunction::EmitOMPInnerLoop(
551 const Stmt &S, bool RequiresCleanup, const Expr *LoopCond,
552 const Expr *IncExpr,
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000553 const llvm::function_ref<void(CodeGenFunction &)> &BodyGen,
554 const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) {
Alexander Musmand196ef22014-10-07 08:57:09 +0000555 auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000556
557 // Start the loop with a block that tests the condition.
Alexander Musmand196ef22014-10-07 08:57:09 +0000558 auto CondBlock = createBasicBlock("omp.inner.for.cond");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000559 EmitBlock(CondBlock);
560 LoopStack.push(CondBlock);
561
562 // If there are any cleanups between here and the loop-exit scope,
563 // create a block to stage a loop exit along.
564 auto ExitBlock = LoopExit.getBlock();
Alexey Bataev2df54a02015-03-12 08:53:29 +0000565 if (RequiresCleanup)
Alexander Musmand196ef22014-10-07 08:57:09 +0000566 ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000567
Alexander Musmand196ef22014-10-07 08:57:09 +0000568 auto LoopBody = createBasicBlock("omp.inner.for.body");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000569
Alexey Bataev2df54a02015-03-12 08:53:29 +0000570 // Emit condition.
Justin Bogner66242d62015-04-23 23:06:47 +0000571 EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S));
Alexander Musmana5f070a2014-10-01 06:03:56 +0000572 if (ExitBlock != LoopExit.getBlock()) {
573 EmitBlock(ExitBlock);
574 EmitBranchThroughCleanup(LoopExit);
575 }
576
577 EmitBlock(LoopBody);
Justin Bogner66242d62015-04-23 23:06:47 +0000578 incrementProfileCounter(&S);
Alexander Musmana5f070a2014-10-01 06:03:56 +0000579
580 // Create a block for the increment.
Alexander Musmand196ef22014-10-07 08:57:09 +0000581 auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000582 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
583
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000584 BodyGen(*this);
Alexander Musmana5f070a2014-10-01 06:03:56 +0000585
586 // Emit "IV = IV + 1" and a back-edge to the condition block.
587 EmitBlock(Continue.getBlock());
Alexey Bataev2df54a02015-03-12 08:53:29 +0000588 EmitIgnoredExpr(IncExpr);
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000589 PostIncGen(*this);
Alexander Musmana5f070a2014-10-01 06:03:56 +0000590 BreakContinueStack.pop_back();
591 EmitBranch(CondBlock);
592 LoopStack.pop();
593 // Emit the fall-through block.
594 EmitBlock(LoopExit.getBlock());
595}
596
Alexey Bataev3b5b5c42015-06-18 10:10:12 +0000597void CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) {
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000598 // Emit inits for the linear variables.
599 for (auto &&I = D.getClausesOfKind(OMPC_linear); I; ++I) {
600 auto *C = cast<OMPLinearClause>(*I);
601 for (auto Init : C->inits()) {
602 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl());
Alexey Bataev3b5b5c42015-06-18 10:10:12 +0000603 auto *OrigVD = cast<VarDecl>(
604 cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())->getDecl());
605 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
606 CapturedStmtInfo->lookup(OrigVD) != nullptr,
607 VD->getInit()->getType(), VK_LValue,
608 VD->getInit()->getExprLoc());
609 AutoVarEmission Emission = EmitAutoVarAlloca(*VD);
610 EmitExprAsInit(&DRE, VD,
611 MakeAddrLValue(Emission.getAllocatedAddress(),
612 VD->getType(), Emission.Alignment),
613 /*capturedByInit=*/false);
614 EmitAutoVarCleanups(Emission);
Alexander Musmana5f070a2014-10-01 06:03:56 +0000615 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000616 // Emit the linear steps for the linear clauses.
617 // If a step is not constant, it is pre-calculated before the loop.
618 if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep()))
619 if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) {
Alexey Bataev3b5b5c42015-06-18 10:10:12 +0000620 EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl()));
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000621 // Emit calculation of the linear step.
Alexey Bataev3b5b5c42015-06-18 10:10:12 +0000622 EmitIgnoredExpr(CS);
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000623 }
Alexander Musmana5f070a2014-10-01 06:03:56 +0000624 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000625}
626
627static void emitLinearClauseFinal(CodeGenFunction &CGF,
628 const OMPLoopDirective &D) {
Alexander Musman3276a272015-03-21 10:12:56 +0000629 // Emit the final values of the linear variables.
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000630 for (auto &&I = D.getClausesOfKind(OMPC_linear); I; ++I) {
Alexey Bataevc925aa32015-04-27 08:00:32 +0000631 auto *C = cast<OMPLinearClause>(*I);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000632 auto IC = C->varlist_begin();
Alexander Musman3276a272015-03-21 10:12:56 +0000633 for (auto F : C->finals()) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000634 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl());
635 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000636 CGF.CapturedStmtInfo->lookup(OrigVD) != nullptr,
Alexey Bataev39f915b82015-05-08 10:41:21 +0000637 (*IC)->getType(), VK_LValue, (*IC)->getExprLoc());
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000638 auto *OrigAddr = CGF.EmitLValue(&DRE).getAddress();
639 CodeGenFunction::OMPPrivateScope VarScope(CGF);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000640 VarScope.addPrivate(OrigVD,
641 [OrigAddr]() -> llvm::Value *{ return OrigAddr; });
642 (void)VarScope.Privatize();
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000643 CGF.EmitIgnoredExpr(F);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000644 ++IC;
Alexander Musman3276a272015-03-21 10:12:56 +0000645 }
646 }
Alexander Musmana5f070a2014-10-01 06:03:56 +0000647}
648
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000649static void emitAlignedClause(CodeGenFunction &CGF,
650 const OMPExecutableDirective &D) {
651 for (auto &&I = D.getClausesOfKind(OMPC_aligned); I; ++I) {
652 auto *Clause = cast<OMPAlignedClause>(*I);
653 unsigned ClauseAlignment = 0;
654 if (auto AlignmentExpr = Clause->getAlignment()) {
655 auto AlignmentCI =
656 cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr));
657 ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue());
Alexander Musman09184fe2014-09-30 05:29:28 +0000658 }
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000659 for (auto E : Clause->varlists()) {
660 unsigned Alignment = ClauseAlignment;
661 if (Alignment == 0) {
662 // OpenMP [2.8.1, Description]
663 // If no optional parameter is specified, implementation-defined default
664 // alignments for SIMD instructions on the target platforms are assumed.
665 Alignment =
Alexey Bataev00396512015-07-02 03:40:19 +0000666 CGF.getContext()
667 .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign(
668 E->getType()->getPointeeType()))
669 .getQuantity();
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000670 }
671 assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) &&
672 "alignment is not power of 2");
673 if (Alignment != 0) {
674 llvm::Value *PtrValue = CGF.EmitScalarExpr(E);
675 CGF.EmitAlignmentAssumption(PtrValue, Alignment);
676 }
Alexander Musman09184fe2014-09-30 05:29:28 +0000677 }
678 }
679}
680
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000681static void emitPrivateLoopCounters(CodeGenFunction &CGF,
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000682 CodeGenFunction::OMPPrivateScope &LoopScope,
683 ArrayRef<Expr *> Counters) {
684 for (auto *E : Counters) {
685 auto VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
Alexey Bataev61114692015-04-28 13:20:05 +0000686 (void)LoopScope.addPrivate(VD, [&]() -> llvm::Value *{
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000687 // Emit var without initialization.
688 auto VarEmission = CGF.EmitAutoVarAlloca(*VD);
689 CGF.EmitAutoVarCleanups(VarEmission);
690 return VarEmission.getAllocatedAddress();
691 });
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000692 }
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000693}
694
Alexey Bataev62dbb972015-04-22 11:59:37 +0000695static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S,
696 const Expr *Cond, llvm::BasicBlock *TrueBlock,
697 llvm::BasicBlock *FalseBlock, uint64_t TrueCount) {
Alexey Bataev6e8248f2015-06-11 10:53:56 +0000698 {
699 CodeGenFunction::OMPPrivateScope PreCondScope(CGF);
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000700 emitPrivateLoopCounters(CGF, PreCondScope, S.counters());
Alexey Bataev6e8248f2015-06-11 10:53:56 +0000701 const VarDecl *IVDecl =
702 cast<VarDecl>(cast<DeclRefExpr>(S.getIterationVariable())->getDecl());
703 bool IsRegistered = PreCondScope.addPrivate(IVDecl, [&]() -> llvm::Value *{
704 // Emit var without initialization.
705 auto VarEmission = CGF.EmitAutoVarAlloca(*IVDecl);
706 CGF.EmitAutoVarCleanups(VarEmission);
707 return VarEmission.getAllocatedAddress();
708 });
709 assert(IsRegistered && "counter already registered as private");
710 // Silence the warning about unused variable.
711 (void)IsRegistered;
712 (void)PreCondScope.Privatize();
713 // Initialize internal counter to 0 to calculate initial values of real
714 // counters.
715 LValue IV = CGF.EmitLValue(S.getIterationVariable());
716 CGF.EmitStoreOfScalar(
717 llvm::ConstantInt::getNullValue(
718 IV.getAddress()->getType()->getPointerElementType()),
719 CGF.EmitLValue(S.getIterationVariable()), /*isInit=*/true);
720 // Get initial values of real counters.
721 for (auto I : S.updates()) {
722 CGF.EmitIgnoredExpr(I);
723 }
Alexey Bataev62dbb972015-04-22 11:59:37 +0000724 }
725 // Check that loop is executed at least one time.
726 CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount);
727}
728
Alexander Musman3276a272015-03-21 10:12:56 +0000729static void
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000730emitPrivateLinearVars(CodeGenFunction &CGF, const OMPExecutableDirective &D,
Alexander Musman3276a272015-03-21 10:12:56 +0000731 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataevc925aa32015-04-27 08:00:32 +0000732 for (auto &&I = D.getClausesOfKind(OMPC_linear); I; ++I) {
733 auto *C = cast<OMPLinearClause>(*I);
734 for (auto *E : C->varlists()) {
Alexander Musman3276a272015-03-21 10:12:56 +0000735 auto VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
736 bool IsRegistered = PrivateScope.addPrivate(VD, [&]()->llvm::Value * {
737 // Emit var without initialization.
738 auto VarEmission = CGF.EmitAutoVarAlloca(*VD);
739 CGF.EmitAutoVarCleanups(VarEmission);
740 return VarEmission.getAllocatedAddress();
741 });
742 assert(IsRegistered && "linear var already registered as private");
743 // Silence the warning about unused variable.
744 (void)IsRegistered;
745 }
746 }
747}
748
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000749static void emitSafelenClause(CodeGenFunction &CGF,
750 const OMPExecutableDirective &D) {
751 if (auto *C =
752 cast_or_null<OMPSafelenClause>(D.getSingleClause(OMPC_safelen))) {
753 RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(),
754 /*ignoreResult=*/true);
755 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000756 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000757 // In presence of finite 'safelen', it may be unsafe to mark all
758 // the memory instructions parallel, because loop-carried
759 // dependences of 'safelen' iterations are possible.
760 CGF.LoopStack.setParallel(false);
761 }
762}
763
Alexey Bataev58e5bdb2015-06-18 04:45:29 +0000764void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D) {
765 // Walk clauses and process safelen/lastprivate.
766 LoopStack.setParallel();
Tyler Nowickida46d0e2015-07-14 23:03:09 +0000767 LoopStack.setVectorizeEnable(true);
Alexey Bataev58e5bdb2015-06-18 04:45:29 +0000768 emitSafelenClause(*this, D);
769}
770
771void CodeGenFunction::EmitOMPSimdFinal(const OMPLoopDirective &D) {
772 auto IC = D.counters().begin();
773 for (auto F : D.finals()) {
774 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl());
Alexey Bataev3b5b5c42015-06-18 10:10:12 +0000775 if (LocalDeclMap.lookup(OrigVD) || CapturedStmtInfo->lookup(OrigVD)) {
Alexey Bataev58e5bdb2015-06-18 04:45:29 +0000776 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
777 CapturedStmtInfo->lookup(OrigVD) != nullptr,
778 (*IC)->getType(), VK_LValue, (*IC)->getExprLoc());
779 auto *OrigAddr = EmitLValue(&DRE).getAddress();
780 OMPPrivateScope VarScope(*this);
781 VarScope.addPrivate(OrigVD,
782 [OrigAddr]() -> llvm::Value *{ return OrigAddr; });
783 (void)VarScope.Privatize();
784 EmitIgnoredExpr(F);
785 }
786 ++IC;
787 }
788 emitLinearClauseFinal(*this, D);
789}
790
Alexander Musman515ad8c2014-05-22 08:54:05 +0000791void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000792 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
Alexey Bataev62dbb972015-04-22 11:59:37 +0000793 // if (PreCond) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000794 // for (IV in 0..LastIteration) BODY;
795 // <Final counter/linear vars updates>;
Alexey Bataev62dbb972015-04-22 11:59:37 +0000796 // }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000797 //
Alexander Musmana5f070a2014-10-01 06:03:56 +0000798
Alexey Bataev62dbb972015-04-22 11:59:37 +0000799 // Emit: if (PreCond) - begin.
800 // If the condition constant folds and can be elided, avoid emitting the
801 // whole loop.
802 bool CondConstant;
803 llvm::BasicBlock *ContBlock = nullptr;
804 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
805 if (!CondConstant)
806 return;
807 } else {
Alexey Bataev62dbb972015-04-22 11:59:37 +0000808 auto *ThenBlock = CGF.createBasicBlock("simd.if.then");
809 ContBlock = CGF.createBasicBlock("simd.if.end");
Justin Bogner66242d62015-04-23 23:06:47 +0000810 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
811 CGF.getProfileCount(&S));
Alexey Bataev62dbb972015-04-22 11:59:37 +0000812 CGF.EmitBlock(ThenBlock);
Justin Bogner66242d62015-04-23 23:06:47 +0000813 CGF.incrementProfileCounter(&S);
Alexey Bataev62dbb972015-04-22 11:59:37 +0000814 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000815
816 // Emit the loop iteration variable.
817 const Expr *IVExpr = S.getIterationVariable();
818 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
819 CGF.EmitVarDecl(*IVDecl);
820 CGF.EmitIgnoredExpr(S.getInit());
821
822 // Emit the iterations count variable.
823 // If it is not a variable, Sema decided to calculate iterations count on
Alexey Bataev7a228ff2015-05-21 07:59:51 +0000824 // each iteration (e.g., it is foldable into a constant).
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000825 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
826 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
827 // Emit calculation of the iterations count.
828 CGF.EmitIgnoredExpr(S.getCalcLastIteration());
Alexander Musmana5f070a2014-10-01 06:03:56 +0000829 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000830
Alexey Bataev58e5bdb2015-06-18 04:45:29 +0000831 CGF.EmitOMPSimdInit(S);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000832
Alexey Bataev58e5bdb2015-06-18 04:45:29 +0000833 emitAlignedClause(CGF, S);
Alexey Bataev3b5b5c42015-06-18 10:10:12 +0000834 CGF.EmitOMPLinearClauseInit(S);
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000835 bool HasLastprivateClause;
Alexey Bataev62dbb972015-04-22 11:59:37 +0000836 {
837 OMPPrivateScope LoopScope(CGF);
Alexey Bataevcbdcbb72015-06-17 07:45:51 +0000838 emitPrivateLoopCounters(CGF, LoopScope, S.counters());
839 emitPrivateLinearVars(CGF, S, LoopScope);
Alexey Bataev62dbb972015-04-22 11:59:37 +0000840 CGF.EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev89e7e8e2015-06-17 06:21:39 +0000841 CGF.EmitOMPReductionClauseInit(S, LoopScope);
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000842 HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev62dbb972015-04-22 11:59:37 +0000843 (void)LoopScope.Privatize();
Alexey Bataev0f34da12015-07-02 04:17:07 +0000844 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
845 S.getInc(),
Alexey Bataev62dbb972015-04-22 11:59:37 +0000846 [&S](CodeGenFunction &CGF) {
Alexey Bataev0f34da12015-07-02 04:17:07 +0000847 CGF.EmitOMPLoopBody(S, JumpDest());
Alexey Bataev62dbb972015-04-22 11:59:37 +0000848 CGF.EmitStopPoint(&S);
849 },
850 [](CodeGenFunction &) {});
Alexey Bataevfc087ec2015-06-16 13:14:42 +0000851 // Emit final copy of the lastprivate variables at the end of loops.
852 if (HasLastprivateClause) {
853 CGF.EmitOMPLastprivateClauseFinal(S);
854 }
Alexey Bataev89e7e8e2015-06-17 06:21:39 +0000855 CGF.EmitOMPReductionClauseFinal(S);
Alexey Bataev62dbb972015-04-22 11:59:37 +0000856 }
Alexey Bataev58e5bdb2015-06-18 04:45:29 +0000857 CGF.EmitOMPSimdFinal(S);
Alexey Bataev62dbb972015-04-22 11:59:37 +0000858 // Emit: if (PreCond) - end.
859 if (ContBlock) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000860 CGF.EmitBranch(ContBlock);
861 CGF.EmitBlock(ContBlock, true);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +0000862 }
863 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +0000864 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
Alexander Musman515ad8c2014-05-22 08:54:05 +0000865}
866
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000867void CodeGenFunction::EmitOMPForOuterLoop(OpenMPScheduleClauseKind ScheduleKind,
868 const OMPLoopDirective &S,
869 OMPPrivateScope &LoopScope,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000870 bool Ordered, llvm::Value *LB,
871 llvm::Value *UB, llvm::Value *ST,
872 llvm::Value *IL, llvm::Value *Chunk) {
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000873 auto &RT = CGM.getOpenMPRuntime();
Alexander Musman92bdaab2015-03-12 13:37:50 +0000874
875 // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime).
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000876 const bool DynamicOrOrdered = Ordered || RT.isDynamic(ScheduleKind);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000877
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000878 assert((Ordered ||
879 !RT.isStaticNonchunked(ScheduleKind, /*Chunked=*/Chunk != nullptr)) &&
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000880 "static non-chunked schedule does not need outer loop");
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000881
882 // Emit outer loop.
883 //
884 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
Alexander Musman92bdaab2015-03-12 13:37:50 +0000885 // When schedule(dynamic,chunk_size) is specified, the iterations are
886 // distributed to threads in the team in chunks as the threads request them.
887 // Each thread executes a chunk of iterations, then requests another chunk,
888 // until no chunks remain to be distributed. Each chunk contains chunk_size
889 // iterations, except for the last chunk to be distributed, which may have
890 // fewer iterations. When no chunk_size is specified, it defaults to 1.
891 //
892 // When schedule(guided,chunk_size) is specified, the iterations are assigned
893 // to threads in the team in chunks as the executing threads request them.
894 // Each thread executes a chunk of iterations, then requests another chunk,
895 // until no chunks remain to be assigned. For a chunk_size of 1, the size of
896 // each chunk is proportional to the number of unassigned iterations divided
897 // by the number of threads in the team, decreasing to 1. For a chunk_size
898 // with value k (greater than 1), the size of each chunk is determined in the
899 // same way, with the restriction that the chunks do not contain fewer than k
900 // iterations (except for the last chunk to be assigned, which may have fewer
901 // than k iterations).
902 //
903 // When schedule(auto) is specified, the decision regarding scheduling is
904 // delegated to the compiler and/or runtime system. The programmer gives the
905 // implementation the freedom to choose any possible mapping of iterations to
906 // threads in the team.
907 //
908 // When schedule(runtime) is specified, the decision regarding scheduling is
909 // deferred until run time, and the schedule and chunk size are taken from the
910 // run-sched-var ICV. If the ICV is set to auto, the schedule is
911 // implementation defined
912 //
913 // while(__kmpc_dispatch_next(&LB, &UB)) {
914 // idx = LB;
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000915 // while (idx <= UB) { BODY; ++idx;
916 // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only.
917 // } // inner loop
Alexander Musman92bdaab2015-03-12 13:37:50 +0000918 // }
919 //
920 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000921 // When schedule(static, chunk_size) is specified, iterations are divided into
922 // chunks of size chunk_size, and the chunks are assigned to the threads in
923 // the team in a round-robin fashion in the order of the thread number.
924 //
925 // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) {
926 // while (idx <= UB) { BODY; ++idx; } // inner loop
927 // LB = LB + ST;
928 // UB = UB + ST;
929 // }
930 //
Alexander Musman92bdaab2015-03-12 13:37:50 +0000931
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000932 const Expr *IVExpr = S.getIterationVariable();
933 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
934 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
935
Alexander Musman92bdaab2015-03-12 13:37:50 +0000936 RT.emitForInit(
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000937 *this, S.getLocStart(), ScheduleKind, IVSize, IVSigned, Ordered, IL, LB,
938 (DynamicOrOrdered ? EmitAnyExpr(S.getLastIteration()).getScalarVal()
939 : UB),
940 ST, Chunk);
Alexander Musman92bdaab2015-03-12 13:37:50 +0000941
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000942 auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end");
943
944 // Start the loop with a block that tests the condition.
945 auto CondBlock = createBasicBlock("omp.dispatch.cond");
946 EmitBlock(CondBlock);
947 LoopStack.push(CondBlock);
948
949 llvm::Value *BoolCondVal = nullptr;
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000950 if (!DynamicOrOrdered) {
Alexander Musman92bdaab2015-03-12 13:37:50 +0000951 // UB = min(UB, GlobalUB)
952 EmitIgnoredExpr(S.getEnsureUpperBound());
953 // IV = LB
954 EmitIgnoredExpr(S.getInit());
955 // IV < UB
Alexey Bataevae05c292015-06-16 11:59:36 +0000956 BoolCondVal = EvaluateExprAsBool(S.getCond());
Alexander Musman92bdaab2015-03-12 13:37:50 +0000957 } else {
958 BoolCondVal = RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned,
959 IL, LB, UB, ST);
960 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000961
962 // If there are any cleanups between here and the loop-exit scope,
963 // create a block to stage a loop exit along.
964 auto ExitBlock = LoopExit.getBlock();
965 if (LoopScope.requiresCleanups())
966 ExitBlock = createBasicBlock("omp.dispatch.cleanup");
967
968 auto LoopBody = createBasicBlock("omp.dispatch.body");
969 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
970 if (ExitBlock != LoopExit.getBlock()) {
971 EmitBlock(ExitBlock);
972 EmitBranchThroughCleanup(LoopExit);
973 }
974 EmitBlock(LoopBody);
975
Alexander Musman92bdaab2015-03-12 13:37:50 +0000976 // Emit "IV = LB" (in case of static schedule, we have already calculated new
977 // LB for loop condition and emitted it above).
Alexey Bataevd7589ffe2015-05-20 13:12:48 +0000978 if (DynamicOrOrdered)
Alexander Musman92bdaab2015-03-12 13:37:50 +0000979 EmitIgnoredExpr(S.getInit());
980
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000981 // Create a block for the increment.
982 auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc");
983 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
984
Alexey Bataev58e5bdb2015-06-18 04:45:29 +0000985 // Generate !llvm.loop.parallel metadata for loads and stores for loops
986 // with dynamic/guided scheduling and without ordered clause.
987 if (!isOpenMPSimdDirective(S.getDirectiveKind())) {
988 LoopStack.setParallel((ScheduleKind == OMPC_SCHEDULE_dynamic ||
989 ScheduleKind == OMPC_SCHEDULE_guided) &&
990 !Ordered);
991 } else {
992 EmitOMPSimdInit(S);
993 }
994
Alexey Bataev98eb6e32015-04-22 11:15:40 +0000995 SourceLocation Loc = S.getLocStart();
Alexey Bataev0f34da12015-07-02 04:17:07 +0000996 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(),
997 [&S, LoopExit](CodeGenFunction &CGF) {
998 CGF.EmitOMPLoopBody(S, LoopExit);
999 CGF.EmitStopPoint(&S);
1000 },
1001 [Ordered, IVSize, IVSigned, Loc](CodeGenFunction &CGF) {
1002 if (Ordered) {
1003 CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(
1004 CGF, Loc, IVSize, IVSigned);
1005 }
1006 });
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001007
1008 EmitBlock(Continue.getBlock());
1009 BreakContinueStack.pop_back();
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001010 if (!DynamicOrOrdered) {
Alexander Musman92bdaab2015-03-12 13:37:50 +00001011 // Emit "LB = LB + Stride", "UB = UB + Stride".
1012 EmitIgnoredExpr(S.getNextLowerBound());
1013 EmitIgnoredExpr(S.getNextUpperBound());
1014 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001015
1016 EmitBranch(CondBlock);
1017 LoopStack.pop();
1018 // Emit the fall-through block.
1019 EmitBlock(LoopExit.getBlock());
1020
1021 // Tell the runtime we are done.
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001022 if (!DynamicOrOrdered)
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001023 RT.emitForStaticFinish(*this, S.getLocEnd());
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001024}
1025
Alexander Musmanc6388682014-12-15 07:07:06 +00001026/// \brief Emit a helper variable and return corresponding lvalue.
1027static LValue EmitOMPHelperVar(CodeGenFunction &CGF,
1028 const DeclRefExpr *Helper) {
1029 auto VDecl = cast<VarDecl>(Helper->getDecl());
1030 CGF.EmitVarDecl(*VDecl);
1031 return CGF.EmitLValue(Helper);
1032}
1033
Alexey Bataev040d5402015-05-12 08:35:28 +00001034static std::pair<llvm::Value * /*Chunk*/, OpenMPScheduleClauseKind>
1035emitScheduleClause(CodeGenFunction &CGF, const OMPLoopDirective &S,
1036 bool OuterRegion) {
1037 // Detect the loop schedule kind and chunk.
1038 auto ScheduleKind = OMPC_SCHEDULE_unknown;
1039 llvm::Value *Chunk = nullptr;
1040 if (auto *C =
1041 cast_or_null<OMPScheduleClause>(S.getSingleClause(OMPC_schedule))) {
1042 ScheduleKind = C->getScheduleKind();
1043 if (const auto *Ch = C->getChunkSize()) {
1044 if (auto *ImpRef = cast_or_null<DeclRefExpr>(C->getHelperChunkSize())) {
1045 if (OuterRegion) {
1046 const VarDecl *ImpVar = cast<VarDecl>(ImpRef->getDecl());
1047 CGF.EmitVarDecl(*ImpVar);
1048 CGF.EmitStoreThroughLValue(
1049 CGF.EmitAnyExpr(Ch),
1050 CGF.MakeNaturalAlignAddrLValue(CGF.GetAddrOfLocalVar(ImpVar),
1051 ImpVar->getType()));
1052 } else {
1053 Ch = ImpRef;
1054 }
1055 }
1056 if (!C->getHelperChunkSize() || !OuterRegion) {
1057 Chunk = CGF.EmitScalarExpr(Ch);
1058 Chunk = CGF.EmitScalarConversion(Chunk, Ch->getType(),
1059 S.getIterationVariable()->getType());
1060 }
1061 }
1062 }
1063 return std::make_pair(Chunk, ScheduleKind);
1064}
1065
Alexey Bataev38e89532015-04-16 04:54:05 +00001066bool CodeGenFunction::EmitOMPWorksharingLoop(const OMPLoopDirective &S) {
Alexander Musmanc6388682014-12-15 07:07:06 +00001067 // Emit the loop iteration variable.
1068 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
1069 auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
1070 EmitVarDecl(*IVDecl);
1071
1072 // Emit the iterations count variable.
1073 // If it is not a variable, Sema decided to calculate iterations count on each
1074 // iteration (e.g., it is foldable into a constant).
1075 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
1076 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
1077 // Emit calculation of the iterations count.
1078 EmitIgnoredExpr(S.getCalcLastIteration());
1079 }
1080
1081 auto &RT = CGM.getOpenMPRuntime();
1082
Alexey Bataev38e89532015-04-16 04:54:05 +00001083 bool HasLastprivateClause;
Alexander Musmanc6388682014-12-15 07:07:06 +00001084 // Check pre-condition.
1085 {
1086 // Skip the entire loop if we don't meet the precondition.
Alexey Bataev62dbb972015-04-22 11:59:37 +00001087 // If the condition constant folds and can be elided, avoid emitting the
1088 // whole loop.
1089 bool CondConstant;
1090 llvm::BasicBlock *ContBlock = nullptr;
1091 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
1092 if (!CondConstant)
1093 return false;
1094 } else {
Alexey Bataev62dbb972015-04-22 11:59:37 +00001095 auto *ThenBlock = createBasicBlock("omp.precond.then");
1096 ContBlock = createBasicBlock("omp.precond.end");
1097 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
Justin Bogner66242d62015-04-23 23:06:47 +00001098 getProfileCount(&S));
Alexey Bataev62dbb972015-04-22 11:59:37 +00001099 EmitBlock(ThenBlock);
Justin Bogner66242d62015-04-23 23:06:47 +00001100 incrementProfileCounter(&S);
Alexey Bataev62dbb972015-04-22 11:59:37 +00001101 }
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001102
1103 emitAlignedClause(*this, S);
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001104 EmitOMPLinearClauseInit(S);
Alexander Musmanc6388682014-12-15 07:07:06 +00001105 // Emit 'then' code.
1106 {
1107 // Emit helper vars inits.
1108 LValue LB =
1109 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable()));
1110 LValue UB =
1111 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable()));
1112 LValue ST =
1113 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
1114 LValue IL =
1115 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
1116
1117 OMPPrivateScope LoopScope(*this);
Alexey Bataev69c62a92015-04-15 04:52:20 +00001118 if (EmitOMPFirstprivateClause(S, LoopScope)) {
1119 // Emit implicit barrier to synchronize threads and avoid data races on
1120 // initialization of firstprivate variables.
1121 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
1122 OMPD_unknown);
1123 }
Alexey Bataev50a64582015-04-22 12:24:45 +00001124 EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev38e89532015-04-16 04:54:05 +00001125 HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataev7ebe5fd2015-04-22 13:43:03 +00001126 EmitOMPReductionClauseInit(S, LoopScope);
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001127 emitPrivateLoopCounters(*this, LoopScope, S.counters());
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001128 emitPrivateLinearVars(*this, S, LoopScope);
Alexander Musman7931b982015-03-16 07:14:41 +00001129 (void)LoopScope.Privatize();
Alexander Musmanc6388682014-12-15 07:07:06 +00001130
1131 // Detect the loop schedule kind and chunk.
Alexey Bataev040d5402015-05-12 08:35:28 +00001132 llvm::Value *Chunk;
1133 OpenMPScheduleClauseKind ScheduleKind;
1134 auto ScheduleInfo =
1135 emitScheduleClause(*this, S, /*OuterRegion=*/false);
1136 Chunk = ScheduleInfo.first;
1137 ScheduleKind = ScheduleInfo.second;
Alexander Musmanc6388682014-12-15 07:07:06 +00001138 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1139 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001140 const bool Ordered = S.getSingleClause(OMPC_ordered) != nullptr;
Alexander Musmanc6388682014-12-15 07:07:06 +00001141 if (RT.isStaticNonchunked(ScheduleKind,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001142 /* Chunked */ Chunk != nullptr) &&
1143 !Ordered) {
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001144 if (isOpenMPSimdDirective(S.getDirectiveKind())) {
1145 EmitOMPSimdInit(S);
1146 }
Alexander Musmanc6388682014-12-15 07:07:06 +00001147 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1148 // When no chunk_size is specified, the iteration space is divided into
1149 // chunks that are approximately equal in size, and at most one chunk is
1150 // distributed to each thread. Note that the size of the chunks is
1151 // unspecified in this case.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001152 RT.emitForInit(*this, S.getLocStart(), ScheduleKind, IVSize, IVSigned,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001153 Ordered, IL.getAddress(), LB.getAddress(),
1154 UB.getAddress(), ST.getAddress());
Alexey Bataev0f34da12015-07-02 04:17:07 +00001155 auto LoopExit = getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
Alexander Musmanc6388682014-12-15 07:07:06 +00001156 // UB = min(UB, GlobalUB);
1157 EmitIgnoredExpr(S.getEnsureUpperBound());
1158 // IV = LB;
1159 EmitIgnoredExpr(S.getInit());
1160 // while (idx <= UB) { BODY; ++idx; }
Alexey Bataevae05c292015-06-16 11:59:36 +00001161 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
1162 S.getInc(),
Alexey Bataev0f34da12015-07-02 04:17:07 +00001163 [&S, LoopExit](CodeGenFunction &CGF) {
1164 CGF.EmitOMPLoopBody(S, LoopExit);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001165 CGF.EmitStopPoint(&S);
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001166 },
1167 [](CodeGenFunction &) {});
Alexey Bataev0f34da12015-07-02 04:17:07 +00001168 EmitBlock(LoopExit.getBlock());
Alexander Musmanc6388682014-12-15 07:07:06 +00001169 // Tell the runtime we are done.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001170 RT.emitForStaticFinish(*this, S.getLocStart());
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001171 } else {
1172 // Emit the outer loop, which requests its work chunk [LB..UB] from
1173 // runtime and runs the inner loop to process it.
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001174 EmitOMPForOuterLoop(ScheduleKind, S, LoopScope, Ordered,
1175 LB.getAddress(), UB.getAddress(), ST.getAddress(),
1176 IL.getAddress(), Chunk);
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001177 }
Alexey Bataev7ebe5fd2015-04-22 13:43:03 +00001178 EmitOMPReductionClauseFinal(S);
Alexey Bataev38e89532015-04-16 04:54:05 +00001179 // Emit final copy of the lastprivate variables if IsLastIter != 0.
1180 if (HasLastprivateClause)
1181 EmitOMPLastprivateClauseFinal(
1182 S, Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart())));
Alexander Musmanc6388682014-12-15 07:07:06 +00001183 }
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00001184 if (isOpenMPSimdDirective(S.getDirectiveKind())) {
1185 EmitOMPSimdFinal(S);
1186 }
Alexander Musmanc6388682014-12-15 07:07:06 +00001187 // We're now done with the loop, so jump to the continuation block.
Alexey Bataev62dbb972015-04-22 11:59:37 +00001188 if (ContBlock) {
1189 EmitBranch(ContBlock);
1190 EmitBlock(ContBlock, true);
1191 }
Alexander Musmanc6388682014-12-15 07:07:06 +00001192 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001193 return HasLastprivateClause;
Alexander Musmanc6388682014-12-15 07:07:06 +00001194}
1195
1196void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001197 LexicalScope Scope(*this, S.getSourceRange());
Alexey Bataev38e89532015-04-16 04:54:05 +00001198 bool HasLastprivates = false;
1199 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF) {
1200 HasLastprivates = CGF.EmitOMPWorksharingLoop(S);
1201 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001202 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen);
Alexander Musmanc6388682014-12-15 07:07:06 +00001203
1204 // Emit an implicit barrier at the end.
Alexey Bataev38e89532015-04-16 04:54:05 +00001205 if (!S.getSingleClause(OMPC_nowait) || HasLastprivates) {
Alexey Bataevf2685682015-03-30 04:30:22 +00001206 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
1207 }
Alexey Bataevf29276e2014-06-18 04:14:57 +00001208}
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001209
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001210void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) {
1211 LexicalScope Scope(*this, S.getSourceRange());
1212 bool HasLastprivates = false;
1213 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF) {
1214 HasLastprivates = CGF.EmitOMPWorksharingLoop(S);
1215 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001216 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
Alexey Bataevcbdcbb72015-06-17 07:45:51 +00001217
1218 // Emit an implicit barrier at the end.
1219 if (!S.getSingleClause(OMPC_nowait) || HasLastprivates) {
1220 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
1221 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001222}
1223
Alexey Bataev2df54a02015-03-12 08:53:29 +00001224static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty,
1225 const Twine &Name,
1226 llvm::Value *Init = nullptr) {
1227 auto LVal = CGF.MakeNaturalAlignAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty);
1228 if (Init)
1229 CGF.EmitScalarInit(Init, LVal);
1230 return LVal;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001231}
1232
Alexey Bataev0f34da12015-07-02 04:17:07 +00001233OpenMPDirectiveKind
1234CodeGenFunction::EmitSections(const OMPExecutableDirective &S) {
Alexey Bataev2df54a02015-03-12 08:53:29 +00001235 auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt();
1236 auto *CS = dyn_cast<CompoundStmt>(Stmt);
1237 if (CS && CS->size() > 1) {
Alexey Bataev9efc03b2015-04-27 04:34:03 +00001238 bool HasLastprivates = false;
1239 auto &&CodeGen = [&S, CS, &HasLastprivates](CodeGenFunction &CGF) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001240 auto &C = CGF.CGM.getContext();
1241 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1242 // Emit helper vars inits.
1243 LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.",
1244 CGF.Builder.getInt32(0));
1245 auto *GlobalUBVal = CGF.Builder.getInt32(CS->size() - 1);
1246 LValue UB =
1247 createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal);
1248 LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.",
1249 CGF.Builder.getInt32(1));
1250 LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.",
1251 CGF.Builder.getInt32(0));
1252 // Loop counter.
1253 LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv.");
1254 OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001255 CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001256 OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001257 CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001258 // Generate condition for loop.
1259 BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue,
1260 OK_Ordinary, S.getLocStart(),
1261 /*fpContractable=*/false);
1262 // Increment for loop counter.
1263 UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue,
1264 OK_Ordinary, S.getLocStart());
1265 auto BodyGen = [CS, &S, &IV](CodeGenFunction &CGF) {
1266 // Iterate through all sections and emit a switch construct:
1267 // switch (IV) {
1268 // case 0:
1269 // <SectionStmt[0]>;
1270 // break;
1271 // ...
1272 // case <NumSection> - 1:
1273 // <SectionStmt[<NumSection> - 1]>;
1274 // break;
1275 // }
1276 // .omp.sections.exit:
1277 auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit");
1278 auto *SwitchStmt = CGF.Builder.CreateSwitch(
1279 CGF.EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB,
1280 CS->size());
1281 unsigned CaseNumber = 0;
Benjamin Kramer642f1732015-07-02 21:03:14 +00001282 for (auto *SubStmt : CS->children()) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001283 auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
1284 CGF.EmitBlock(CaseBB);
1285 SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB);
Benjamin Kramer642f1732015-07-02 21:03:14 +00001286 CGF.EmitStmt(SubStmt);
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001287 CGF.EmitBranch(ExitBB);
Benjamin Kramer642f1732015-07-02 21:03:14 +00001288 ++CaseNumber;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001289 }
1290 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1291 };
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001292
1293 CodeGenFunction::OMPPrivateScope LoopScope(CGF);
1294 if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) {
1295 // Emit implicit barrier to synchronize threads and avoid data races on
1296 // initialization of firstprivate variables.
1297 CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(),
1298 OMPD_unknown);
1299 }
Alexey Bataev73870832015-04-27 04:12:12 +00001300 CGF.EmitOMPPrivateClause(S, LoopScope);
Alexey Bataev9efc03b2015-04-27 04:34:03 +00001301 HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
Alexey Bataeva89adf22015-04-27 05:04:13 +00001302 CGF.EmitOMPReductionClauseInit(S, LoopScope);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001303 (void)LoopScope.Privatize();
1304
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001305 // Emit static non-chunked loop.
1306 CGF.CGM.getOpenMPRuntime().emitForInit(
1307 CGF, S.getLocStart(), OMPC_SCHEDULE_static, /*IVSize=*/32,
Alexey Bataevd7589ffe2015-05-20 13:12:48 +00001308 /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(),
1309 LB.getAddress(), UB.getAddress(), ST.getAddress());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001310 // UB = min(UB, GlobalUB);
1311 auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart());
1312 auto *MinUBGlobalUB = CGF.Builder.CreateSelect(
1313 CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal);
1314 CGF.EmitStoreOfScalar(MinUBGlobalUB, UB);
1315 // IV = LB;
1316 CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV);
1317 // while (idx <= UB) { BODY; ++idx; }
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001318 CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen,
1319 [](CodeGenFunction &) {});
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001320 // Tell the runtime we are done.
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001321 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocStart());
Alexey Bataeva89adf22015-04-27 05:04:13 +00001322 CGF.EmitOMPReductionClauseFinal(S);
Alexey Bataev9efc03b2015-04-27 04:34:03 +00001323
1324 // Emit final copy of the lastprivate variables if IsLastIter != 0.
1325 if (HasLastprivates)
1326 CGF.EmitOMPLastprivateClauseFinal(
1327 S, CGF.Builder.CreateIsNotNull(
1328 CGF.EmitLoadOfScalar(IL, S.getLocStart())));
Alexey Bataev2df54a02015-03-12 08:53:29 +00001329 };
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001330
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001331 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen);
Alexey Bataev9efc03b2015-04-27 04:34:03 +00001332 // Emit barrier for lastprivates only if 'sections' directive has 'nowait'
1333 // clause. Otherwise the barrier will be generated by the codegen for the
1334 // directive.
1335 if (HasLastprivates && S.getSingleClause(OMPC_nowait)) {
1336 // Emit implicit barrier to synchronize threads and avoid data races on
1337 // initialization of firstprivate variables.
Alexey Bataev0f34da12015-07-02 04:17:07 +00001338 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
1339 OMPD_unknown);
Alexey Bataev9efc03b2015-04-27 04:34:03 +00001340 }
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001341 return OMPD_sections;
Alexey Bataev2df54a02015-03-12 08:53:29 +00001342 }
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001343 // If only one section is found - no need to generate loop, emit as a single
1344 // region.
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001345 bool HasFirstprivates;
Alexey Bataeva89adf22015-04-27 05:04:13 +00001346 // No need to generate reductions for sections with single section region, we
1347 // can use original shared variables for all operations.
Alexey Bataevc925aa32015-04-27 08:00:32 +00001348 bool HasReductions = !S.getClausesOfKind(OMPC_reduction).empty();
Alexey Bataev9efc03b2015-04-27 04:34:03 +00001349 // No need to generate lastprivates for sections with single section region,
1350 // we can use original shared variable for all calculations with barrier at
1351 // the end of the sections.
Alexey Bataevc925aa32015-04-27 08:00:32 +00001352 bool HasLastprivates = !S.getClausesOfKind(OMPC_lastprivate).empty();
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001353 auto &&CodeGen = [Stmt, &S, &HasFirstprivates](CodeGenFunction &CGF) {
1354 CodeGenFunction::OMPPrivateScope SingleScope(CGF);
1355 HasFirstprivates = CGF.EmitOMPFirstprivateClause(S, SingleScope);
Alexey Bataev73870832015-04-27 04:12:12 +00001356 CGF.EmitOMPPrivateClause(S, SingleScope);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001357 (void)SingleScope.Privatize();
1358
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001359 CGF.EmitStmt(Stmt);
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001360 };
Alexey Bataev0f34da12015-07-02 04:17:07 +00001361 CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(),
1362 llvm::None, llvm::None, llvm::None,
1363 llvm::None);
Alexey Bataeva89adf22015-04-27 05:04:13 +00001364 // Emit barrier for firstprivates, lastprivates or reductions only if
1365 // 'sections' directive has 'nowait' clause. Otherwise the barrier will be
1366 // generated by the codegen for the directive.
1367 if ((HasFirstprivates || HasLastprivates || HasReductions) &&
1368 S.getSingleClause(OMPC_nowait)) {
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001369 // Emit implicit barrier to synchronize threads and avoid data races on
1370 // initialization of firstprivate variables.
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001371 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_unknown);
Alexey Bataev2cb9b952015-04-24 03:37:03 +00001372 }
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001373 return OMPD_single;
1374}
Alexey Bataev2df54a02015-03-12 08:53:29 +00001375
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001376void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
1377 LexicalScope Scope(*this, S.getSourceRange());
Alexey Bataev0f34da12015-07-02 04:17:07 +00001378 OpenMPDirectiveKind EmittedAs = EmitSections(S);
Alexey Bataev2df54a02015-03-12 08:53:29 +00001379 // Emit an implicit barrier at the end.
Alexey Bataevf2685682015-03-30 04:30:22 +00001380 if (!S.getSingleClause(OMPC_nowait)) {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001381 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), EmittedAs);
Alexey Bataevf2685682015-03-30 04:30:22 +00001382 }
Alexey Bataev2df54a02015-03-12 08:53:29 +00001383}
1384
1385void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001386 LexicalScope Scope(*this, S.getSourceRange());
1387 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1388 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1389 CGF.EnsureInsertPoint();
1390 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001391 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001392}
1393
Alexey Bataev6956e2e2015-02-05 06:35:41 +00001394void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00001395 llvm::SmallVector<const Expr *, 8> CopyprivateVars;
Alexey Bataev420d45b2015-04-14 05:11:24 +00001396 llvm::SmallVector<const Expr *, 8> DestExprs;
Alexey Bataeva63048e2015-03-23 06:18:07 +00001397 llvm::SmallVector<const Expr *, 8> SrcExprs;
Alexey Bataeva63048e2015-03-23 06:18:07 +00001398 llvm::SmallVector<const Expr *, 8> AssignmentOps;
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001399 // Check if there are any 'copyprivate' clauses associated with this
1400 // 'single'
Alexey Bataeva63048e2015-03-23 06:18:07 +00001401 // construct.
Alexey Bataeva63048e2015-03-23 06:18:07 +00001402 // Build a list of copyprivate variables along with helper expressions
1403 // (<source>, <destination>, <destination>=<source> expressions)
Alexey Bataevc925aa32015-04-27 08:00:32 +00001404 for (auto &&I = S.getClausesOfKind(OMPC_copyprivate); I; ++I) {
Alexey Bataeva63048e2015-03-23 06:18:07 +00001405 auto *C = cast<OMPCopyprivateClause>(*I);
1406 CopyprivateVars.append(C->varlists().begin(), C->varlists().end());
Alexey Bataev420d45b2015-04-14 05:11:24 +00001407 DestExprs.append(C->destination_exprs().begin(),
1408 C->destination_exprs().end());
Alexey Bataeva63048e2015-03-23 06:18:07 +00001409 SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end());
Alexey Bataeva63048e2015-03-23 06:18:07 +00001410 AssignmentOps.append(C->assignment_ops().begin(),
1411 C->assignment_ops().end());
1412 }
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001413 LexicalScope Scope(*this, S.getSourceRange());
Alexey Bataeva63048e2015-03-23 06:18:07 +00001414 // Emit code for 'single' region along with 'copyprivate' clauses
Alexey Bataev5521d782015-04-24 04:21:15 +00001415 bool HasFirstprivates;
1416 auto &&CodeGen = [&S, &HasFirstprivates](CodeGenFunction &CGF) {
1417 CodeGenFunction::OMPPrivateScope SingleScope(CGF);
1418 HasFirstprivates = CGF.EmitOMPFirstprivateClause(S, SingleScope);
Alexey Bataev59c654a2015-04-27 03:48:52 +00001419 CGF.EmitOMPPrivateClause(S, SingleScope);
Alexey Bataev5521d782015-04-24 04:21:15 +00001420 (void)SingleScope.Privatize();
1421
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001422 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1423 CGF.EnsureInsertPoint();
1424 };
1425 CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(),
Alexey Bataev420d45b2015-04-14 05:11:24 +00001426 CopyprivateVars, DestExprs, SrcExprs,
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001427 AssignmentOps);
Alexey Bataev5521d782015-04-24 04:21:15 +00001428 // Emit an implicit barrier at the end (to avoid data race on firstprivate
1429 // init or if no 'nowait' clause was specified and no 'copyprivate' clause).
1430 if ((!S.getSingleClause(OMPC_nowait) || HasFirstprivates) &&
1431 CopyprivateVars.empty()) {
1432 CGM.getOpenMPRuntime().emitBarrierCall(
1433 *this, S.getLocStart(),
1434 S.getSingleClause(OMPC_nowait) ? OMPD_unknown : OMPD_single);
Alexey Bataevf2685682015-03-30 04:30:22 +00001435 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001436}
1437
Alexey Bataev8d690652014-12-04 07:23:53 +00001438void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001439 LexicalScope Scope(*this, S.getSourceRange());
1440 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1441 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1442 CGF.EnsureInsertPoint();
1443 };
1444 CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart());
Alexander Musman80c22892014-07-17 08:54:58 +00001445}
1446
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +00001447void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) {
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001448 LexicalScope Scope(*this, S.getSourceRange());
1449 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1450 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1451 CGF.EnsureInsertPoint();
1452 };
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001453 CGM.getOpenMPRuntime().emitCriticalRegion(
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001454 *this, S.getDirectiveName().getAsString(), CodeGen, S.getLocStart());
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001455}
1456
Alexey Bataev671605e2015-04-13 05:28:11 +00001457void CodeGenFunction::EmitOMPParallelForDirective(
1458 const OMPParallelForDirective &S) {
1459 // Emit directive as a combined directive that consists of two implicit
1460 // directives: 'parallel' with 'for' directive.
1461 LexicalScope Scope(*this, S.getSourceRange());
Alexey Bataev040d5402015-05-12 08:35:28 +00001462 (void)emitScheduleClause(*this, S, /*OuterRegion=*/true);
Alexey Bataev671605e2015-04-13 05:28:11 +00001463 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1464 CGF.EmitOMPWorksharingLoop(S);
1465 // Emit implicit barrier at the end of parallel region, but this barrier
1466 // is at the end of 'for' directive, so emit it as the implicit barrier for
1467 // this 'for' directive.
1468 CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(),
1469 OMPD_parallel);
1470 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001471 emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen);
Alexey Bataev4acb8592014-07-07 13:01:15 +00001472}
1473
Alexander Musmane4e893b2014-09-23 09:33:00 +00001474void CodeGenFunction::EmitOMPParallelForSimdDirective(
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00001475 const OMPParallelForSimdDirective &S) {
1476 // Emit directive as a combined directive that consists of two implicit
1477 // directives: 'parallel' with 'for' directive.
1478 LexicalScope Scope(*this, S.getSourceRange());
1479 (void)emitScheduleClause(*this, S, /*OuterRegion=*/true);
1480 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1481 CGF.EmitOMPWorksharingLoop(S);
1482 // Emit implicit barrier at the end of parallel region, but this barrier
1483 // is at the end of 'for' directive, so emit it as the implicit barrier for
1484 // this 'for' directive.
1485 CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(),
1486 OMPD_parallel);
1487 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001488 emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen);
Alexander Musmane4e893b2014-09-23 09:33:00 +00001489}
1490
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001491void CodeGenFunction::EmitOMPParallelSectionsDirective(
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001492 const OMPParallelSectionsDirective &S) {
1493 // Emit directive as a combined directive that consists of two implicit
1494 // directives: 'parallel' with 'sections' directive.
1495 LexicalScope Scope(*this, S.getSourceRange());
1496 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
Alexey Bataev0f34da12015-07-02 04:17:07 +00001497 (void)CGF.EmitSections(S);
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001498 // Emit implicit barrier at the end of parallel region.
1499 CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(),
1500 OMPD_parallel);
1501 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001502 emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001503}
1504
Alexey Bataev62b63b12015-03-10 07:28:44 +00001505void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) {
1506 // Emit outlined function for task construct.
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001507 LexicalScope Scope(*this, S.getSourceRange());
Alexey Bataev62b63b12015-03-10 07:28:44 +00001508 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
1509 auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
1510 auto *I = CS->getCapturedDecl()->param_begin();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001511 auto *PartId = std::next(I);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001512 // The first function argument for tasks is a thread id, the second one is a
1513 // part id (0 for tied tasks, >=0 for untied task).
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001514 llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
1515 // Get list of private variables.
1516 llvm::SmallVector<const Expr *, 8> PrivateVars;
1517 llvm::SmallVector<const Expr *, 8> PrivateCopies;
1518 for (auto &&I = S.getClausesOfKind(OMPC_private); I; ++I) {
1519 auto *C = cast<OMPPrivateClause>(*I);
1520 auto IRef = C->varlist_begin();
1521 for (auto *IInit : C->private_copies()) {
1522 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
1523 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
1524 PrivateVars.push_back(*IRef);
1525 PrivateCopies.push_back(IInit);
1526 }
1527 ++IRef;
1528 }
1529 }
1530 EmittedAsPrivate.clear();
1531 // Get list of firstprivate variables.
1532 llvm::SmallVector<const Expr *, 8> FirstprivateVars;
1533 llvm::SmallVector<const Expr *, 8> FirstprivateCopies;
1534 llvm::SmallVector<const Expr *, 8> FirstprivateInits;
1535 for (auto &&I = S.getClausesOfKind(OMPC_firstprivate); I; ++I) {
1536 auto *C = cast<OMPFirstprivateClause>(*I);
1537 auto IRef = C->varlist_begin();
1538 auto IElemInitRef = C->inits().begin();
1539 for (auto *IInit : C->private_copies()) {
1540 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
1541 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
1542 FirstprivateVars.push_back(*IRef);
1543 FirstprivateCopies.push_back(IInit);
1544 FirstprivateInits.push_back(*IElemInitRef);
1545 }
1546 ++IRef, ++IElemInitRef;
1547 }
1548 }
Alexey Bataev1d2353d2015-06-24 11:01:36 +00001549 // Build list of dependences.
1550 llvm::SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 8>
1551 Dependences;
1552 for (auto &&I = S.getClausesOfKind(OMPC_depend); I; ++I) {
1553 auto *C = cast<OMPDependClause>(*I);
1554 for (auto *IRef : C->varlists()) {
1555 Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef));
1556 }
1557 }
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001558 auto &&CodeGen = [PartId, &S, &PrivateVars, &FirstprivateVars](
1559 CodeGenFunction &CGF) {
1560 // Set proper addresses for generated private copies.
1561 auto *CS = cast<CapturedStmt>(S.getAssociatedStmt());
1562 OMPPrivateScope Scope(CGF);
1563 if (!PrivateVars.empty() || !FirstprivateVars.empty()) {
1564 auto *CopyFn = CGF.Builder.CreateAlignedLoad(
1565 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3)),
1566 CGF.PointerAlignInBytes);
1567 auto *PrivatesPtr = CGF.Builder.CreateAlignedLoad(
1568 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2)),
1569 CGF.PointerAlignInBytes);
1570 // Map privates.
1571 llvm::SmallVector<std::pair<const VarDecl *, llvm::Value *>, 16>
1572 PrivatePtrs;
1573 llvm::SmallVector<llvm::Value *, 16> CallArgs;
1574 CallArgs.push_back(PrivatesPtr);
1575 for (auto *E : PrivateVars) {
1576 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1577 auto *PrivatePtr =
1578 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()));
1579 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
1580 CallArgs.push_back(PrivatePtr);
1581 }
1582 for (auto *E : FirstprivateVars) {
1583 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1584 auto *PrivatePtr =
1585 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()));
1586 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
1587 CallArgs.push_back(PrivatePtr);
1588 }
1589 CGF.EmitRuntimeCall(CopyFn, CallArgs);
1590 for (auto &&Pair : PrivatePtrs) {
1591 auto *Replacement =
1592 CGF.Builder.CreateAlignedLoad(Pair.second, CGF.PointerAlignInBytes);
1593 Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; });
1594 }
1595 }
1596 (void)Scope.Privatize();
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001597 if (*PartId) {
1598 // TODO: emit code for untied tasks.
1599 }
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001600 CGF.EmitStmt(CS->getCapturedStmt());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00001601 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00001602 auto OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction(
1603 S, *I, OMPD_task, CodeGen);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001604 // Check if we should emit tied or untied task.
1605 bool Tied = !S.getSingleClause(OMPC_untied);
1606 // Check if the task is final
1607 llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
1608 if (auto *Clause = S.getSingleClause(OMPC_final)) {
1609 // If the condition constant folds and can be elided, try to avoid emitting
1610 // the condition and the dead arm of the if/else.
1611 auto *Cond = cast<OMPFinalClause>(Clause)->getCondition();
1612 bool CondConstant;
1613 if (ConstantFoldsToSimpleInteger(Cond, CondConstant))
1614 Final.setInt(CondConstant);
1615 else
1616 Final.setPointer(EvaluateExprAsBool(Cond));
1617 } else {
1618 // By default the task is not final.
1619 Final.setInt(/*IntVal=*/false);
1620 }
1621 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
Alexey Bataev1d677132015-04-22 13:57:31 +00001622 const Expr *IfCond = nullptr;
1623 if (auto C = S.getSingleClause(OMPC_if)) {
1624 IfCond = cast<OMPIfClause>(C)->getCondition();
1625 }
Alexey Bataev9e034042015-05-05 04:05:12 +00001626 CGM.getOpenMPRuntime().emitTaskCall(
1627 *this, S.getLocStart(), S, Tied, Final, OutlinedFn, SharedsTy,
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001628 CapturedStruct, IfCond, PrivateVars, PrivateCopies, FirstprivateVars,
Alexey Bataev1d2353d2015-06-24 11:01:36 +00001629 FirstprivateCopies, FirstprivateInits, Dependences);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001630}
1631
Alexey Bataev9f797f32015-02-05 05:57:51 +00001632void CodeGenFunction::EmitOMPTaskyieldDirective(
1633 const OMPTaskyieldDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001634 CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart());
Alexey Bataev68446b72014-07-18 07:47:19 +00001635}
1636
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00001637void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) {
Alexey Bataevf2685682015-03-30 04:30:22 +00001638 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier);
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001639}
1640
Alexey Bataev8b8e2022015-04-27 05:22:09 +00001641void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) {
1642 CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart());
Alexey Bataev2df347a2014-07-18 10:17:07 +00001643}
1644
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001645void CodeGenFunction::EmitOMPTaskgroupDirective(
1646 const OMPTaskgroupDirective &S) {
1647 LexicalScope Scope(*this, S.getSourceRange());
1648 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1649 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1650 CGF.EnsureInsertPoint();
1651 };
1652 CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getLocStart());
1653}
1654
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001655void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001656 CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> {
1657 if (auto C = S.getSingleClause(/*K*/ OMPC_flush)) {
1658 auto FlushClause = cast<OMPFlushClause>(C);
1659 return llvm::makeArrayRef(FlushClause->varlist_begin(),
1660 FlushClause->varlist_end());
1661 }
1662 return llvm::None;
1663 }(), S.getLocStart());
Alexey Bataev6125da92014-07-21 11:26:11 +00001664}
1665
Alexey Bataev98eb6e32015-04-22 11:15:40 +00001666void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) {
1667 LexicalScope Scope(*this, S.getSourceRange());
1668 auto &&CodeGen = [&S](CodeGenFunction &CGF) {
1669 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1670 CGF.EnsureInsertPoint();
1671 };
1672 CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001673}
1674
Alexey Bataevb57056f2015-01-22 06:17:56 +00001675static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val,
1676 QualType SrcType, QualType DestType) {
1677 assert(CGF.hasScalarEvaluationKind(DestType) &&
1678 "DestType must have scalar evaluation kind.");
1679 assert(!Val.isAggregate() && "Must be a scalar or complex.");
1680 return Val.isScalar()
1681 ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType)
1682 : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType,
1683 DestType);
1684}
1685
1686static CodeGenFunction::ComplexPairTy
1687convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType,
1688 QualType DestType) {
1689 assert(CGF.getEvaluationKind(DestType) == TEK_Complex &&
1690 "DestType must have complex evaluation kind.");
1691 CodeGenFunction::ComplexPairTy ComplexVal;
1692 if (Val.isScalar()) {
1693 // Convert the input element to the element type of the complex.
1694 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
1695 auto ScalarVal =
1696 CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestElementType);
1697 ComplexVal = CodeGenFunction::ComplexPairTy(
1698 ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType()));
1699 } else {
1700 assert(Val.isComplex() && "Must be a scalar or complex.");
1701 auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType();
1702 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
1703 ComplexVal.first = CGF.EmitScalarConversion(
1704 Val.getComplexVal().first, SrcElementType, DestElementType);
1705 ComplexVal.second = CGF.EmitScalarConversion(
1706 Val.getComplexVal().second, SrcElementType, DestElementType);
1707 }
1708 return ComplexVal;
1709}
1710
Alexey Bataev5e018f92015-04-23 06:35:10 +00001711static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst,
1712 LValue LVal, RValue RVal) {
1713 if (LVal.isGlobalReg()) {
1714 CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal);
1715 } else {
1716 CGF.EmitAtomicStore(RVal, LVal, IsSeqCst ? llvm::SequentiallyConsistent
1717 : llvm::Monotonic,
1718 LVal.isVolatile(), /*IsInit=*/false);
1719 }
1720}
1721
1722static void emitSimpleStore(CodeGenFunction &CGF, LValue LVal, RValue RVal,
1723 QualType RValTy) {
1724 switch (CGF.getEvaluationKind(LVal.getType())) {
1725 case TEK_Scalar:
1726 CGF.EmitStoreThroughLValue(
1727 RValue::get(convertToScalarValue(CGF, RVal, RValTy, LVal.getType())),
1728 LVal);
1729 break;
1730 case TEK_Complex:
1731 CGF.EmitStoreOfComplex(
1732 convertToComplexValue(CGF, RVal, RValTy, LVal.getType()), LVal,
1733 /*isInit=*/false);
1734 break;
1735 case TEK_Aggregate:
1736 llvm_unreachable("Must be a scalar or complex.");
1737 }
1738}
1739
Alexey Bataevb57056f2015-01-22 06:17:56 +00001740static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst,
1741 const Expr *X, const Expr *V,
1742 SourceLocation Loc) {
1743 // v = x;
1744 assert(V->isLValue() && "V of 'omp atomic read' is not lvalue");
1745 assert(X->isLValue() && "X of 'omp atomic read' is not lvalue");
1746 LValue XLValue = CGF.EmitLValue(X);
1747 LValue VLValue = CGF.EmitLValue(V);
David Majnemera5b195a2015-02-14 01:35:12 +00001748 RValue Res = XLValue.isGlobalReg()
1749 ? CGF.EmitLoadOfLValue(XLValue, Loc)
1750 : CGF.EmitAtomicLoad(XLValue, Loc,
1751 IsSeqCst ? llvm::SequentiallyConsistent
Alexey Bataevb8329262015-02-27 06:33:30 +00001752 : llvm::Monotonic,
1753 XLValue.isVolatile());
Alexey Bataevb57056f2015-01-22 06:17:56 +00001754 // OpenMP, 2.12.6, atomic Construct
1755 // Any atomic construct with a seq_cst clause forces the atomically
1756 // performed operation to include an implicit flush operation without a
1757 // list.
1758 if (IsSeqCst)
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001759 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
Alexey Bataev5e018f92015-04-23 06:35:10 +00001760 emitSimpleStore(CGF,VLValue, Res, X->getType().getNonReferenceType());
Alexey Bataevb57056f2015-01-22 06:17:56 +00001761}
1762
Alexey Bataevb8329262015-02-27 06:33:30 +00001763static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst,
1764 const Expr *X, const Expr *E,
1765 SourceLocation Loc) {
1766 // x = expr;
1767 assert(X->isLValue() && "X of 'omp atomic write' is not lvalue");
Alexey Bataev5e018f92015-04-23 06:35:10 +00001768 emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E));
Alexey Bataevb8329262015-02-27 06:33:30 +00001769 // OpenMP, 2.12.6, atomic Construct
1770 // Any atomic construct with a seq_cst clause forces the atomically
1771 // performed operation to include an implicit flush operation without a
1772 // list.
1773 if (IsSeqCst)
1774 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
1775}
1776
Benjamin Kramer439ee9d2015-05-01 13:59:53 +00001777static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X,
1778 RValue Update,
1779 BinaryOperatorKind BO,
1780 llvm::AtomicOrdering AO,
1781 bool IsXLHSInRHSPart) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001782 auto &Context = CGF.CGM.getContext();
1783 // Allow atomicrmw only if 'x' and 'update' are integer values, lvalue for 'x'
Alexey Bataevb4505a72015-03-30 05:20:59 +00001784 // expression is simple and atomic is allowed for the given type for the
1785 // target platform.
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001786 if (BO == BO_Comma || !Update.isScalar() ||
Alexey Bataev9d541a72015-05-08 11:47:16 +00001787 !Update.getScalarVal()->getType()->isIntegerTy() ||
1788 !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) &&
1789 (Update.getScalarVal()->getType() !=
1790 X.getAddress()->getType()->getPointerElementType())) ||
1791 !X.getAddress()->getType()->getPointerElementType()->isIntegerTy() ||
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001792 !Context.getTargetInfo().hasBuiltinAtomic(
1793 Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment())))
Alexey Bataev5e018f92015-04-23 06:35:10 +00001794 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001795
1796 llvm::AtomicRMWInst::BinOp RMWOp;
1797 switch (BO) {
1798 case BO_Add:
1799 RMWOp = llvm::AtomicRMWInst::Add;
1800 break;
1801 case BO_Sub:
1802 if (!IsXLHSInRHSPart)
Alexey Bataev5e018f92015-04-23 06:35:10 +00001803 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001804 RMWOp = llvm::AtomicRMWInst::Sub;
1805 break;
1806 case BO_And:
1807 RMWOp = llvm::AtomicRMWInst::And;
1808 break;
1809 case BO_Or:
1810 RMWOp = llvm::AtomicRMWInst::Or;
1811 break;
1812 case BO_Xor:
1813 RMWOp = llvm::AtomicRMWInst::Xor;
1814 break;
1815 case BO_LT:
1816 RMWOp = X.getType()->hasSignedIntegerRepresentation()
1817 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min
1818 : llvm::AtomicRMWInst::Max)
1819 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin
1820 : llvm::AtomicRMWInst::UMax);
1821 break;
1822 case BO_GT:
1823 RMWOp = X.getType()->hasSignedIntegerRepresentation()
1824 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max
1825 : llvm::AtomicRMWInst::Min)
1826 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax
1827 : llvm::AtomicRMWInst::UMin);
1828 break;
Alexey Bataev5e018f92015-04-23 06:35:10 +00001829 case BO_Assign:
1830 RMWOp = llvm::AtomicRMWInst::Xchg;
1831 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001832 case BO_Mul:
1833 case BO_Div:
1834 case BO_Rem:
1835 case BO_Shl:
1836 case BO_Shr:
1837 case BO_LAnd:
1838 case BO_LOr:
Alexey Bataev5e018f92015-04-23 06:35:10 +00001839 return std::make_pair(false, RValue::get(nullptr));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001840 case BO_PtrMemD:
1841 case BO_PtrMemI:
1842 case BO_LE:
1843 case BO_GE:
1844 case BO_EQ:
1845 case BO_NE:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001846 case BO_AddAssign:
1847 case BO_SubAssign:
1848 case BO_AndAssign:
1849 case BO_OrAssign:
1850 case BO_XorAssign:
1851 case BO_MulAssign:
1852 case BO_DivAssign:
1853 case BO_RemAssign:
1854 case BO_ShlAssign:
1855 case BO_ShrAssign:
1856 case BO_Comma:
1857 llvm_unreachable("Unsupported atomic update operation");
1858 }
1859 auto *UpdateVal = Update.getScalarVal();
1860 if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) {
1861 UpdateVal = CGF.Builder.CreateIntCast(
1862 IC, X.getAddress()->getType()->getPointerElementType(),
1863 X.getType()->hasSignedIntegerRepresentation());
1864 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00001865 auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getAddress(), UpdateVal, AO);
1866 return std::make_pair(true, RValue::get(Res));
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001867}
1868
Alexey Bataev5e018f92015-04-23 06:35:10 +00001869std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr(
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001870 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
1871 llvm::AtomicOrdering AO, SourceLocation Loc,
1872 const llvm::function_ref<RValue(RValue)> &CommonGen) {
1873 // Update expressions are allowed to have the following forms:
1874 // x binop= expr; -> xrval + expr;
1875 // x++, ++x -> xrval + 1;
1876 // x--, --x -> xrval - 1;
1877 // x = x binop expr; -> xrval binop expr
1878 // x = expr Op x; - > expr binop xrval;
Alexey Bataev5e018f92015-04-23 06:35:10 +00001879 auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart);
1880 if (!Res.first) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001881 if (X.isGlobalReg()) {
1882 // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop
1883 // 'xrval'.
1884 EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X);
1885 } else {
1886 // Perform compare-and-swap procedure.
1887 EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified());
Alexey Bataevb4505a72015-03-30 05:20:59 +00001888 }
1889 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00001890 return Res;
Alexey Bataevb4505a72015-03-30 05:20:59 +00001891}
1892
1893static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst,
1894 const Expr *X, const Expr *E,
1895 const Expr *UE, bool IsXLHSInRHSPart,
1896 SourceLocation Loc) {
1897 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
1898 "Update expr in 'atomic update' must be a binary operator.");
1899 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
1900 // Update expressions are allowed to have the following forms:
1901 // x binop= expr; -> xrval + expr;
1902 // x++, ++x -> xrval + 1;
1903 // x--, --x -> xrval - 1;
1904 // x = x binop expr; -> xrval binop expr
1905 // x = expr Op x; - > expr binop xrval;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001906 assert(X->isLValue() && "X of 'omp atomic update' is not lvalue");
Alexey Bataevb4505a72015-03-30 05:20:59 +00001907 LValue XLValue = CGF.EmitLValue(X);
1908 RValue ExprRValue = CGF.EmitAnyExpr(E);
Alexey Bataevb4505a72015-03-30 05:20:59 +00001909 auto AO = IsSeqCst ? llvm::SequentiallyConsistent : llvm::Monotonic;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00001910 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
1911 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
1912 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
1913 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
1914 auto Gen =
1915 [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue {
1916 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
1917 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
1918 return CGF.EmitAnyExpr(UE);
1919 };
Alexey Bataev5e018f92015-04-23 06:35:10 +00001920 (void)CGF.EmitOMPAtomicSimpleUpdateExpr(
1921 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
1922 // OpenMP, 2.12.6, atomic Construct
1923 // Any atomic construct with a seq_cst clause forces the atomically
1924 // performed operation to include an implicit flush operation without a
1925 // list.
1926 if (IsSeqCst)
1927 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
1928}
1929
1930static RValue convertToType(CodeGenFunction &CGF, RValue Value,
1931 QualType SourceType, QualType ResType) {
1932 switch (CGF.getEvaluationKind(ResType)) {
1933 case TEK_Scalar:
1934 return RValue::get(convertToScalarValue(CGF, Value, SourceType, ResType));
1935 case TEK_Complex: {
1936 auto Res = convertToComplexValue(CGF, Value, SourceType, ResType);
1937 return RValue::getComplex(Res.first, Res.second);
1938 }
1939 case TEK_Aggregate:
1940 break;
1941 }
1942 llvm_unreachable("Must be a scalar or complex.");
1943}
1944
1945static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst,
1946 bool IsPostfixUpdate, const Expr *V,
1947 const Expr *X, const Expr *E,
1948 const Expr *UE, bool IsXLHSInRHSPart,
1949 SourceLocation Loc) {
1950 assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue");
1951 assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue");
1952 RValue NewVVal;
1953 LValue VLValue = CGF.EmitLValue(V);
1954 LValue XLValue = CGF.EmitLValue(X);
1955 RValue ExprRValue = CGF.EmitAnyExpr(E);
1956 auto AO = IsSeqCst ? llvm::SequentiallyConsistent : llvm::Monotonic;
1957 QualType NewVValType;
1958 if (UE) {
1959 // 'x' is updated with some additional value.
1960 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
1961 "Update expr in 'atomic capture' must be a binary operator.");
1962 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
1963 // Update expressions are allowed to have the following forms:
1964 // x binop= expr; -> xrval + expr;
1965 // x++, ++x -> xrval + 1;
1966 // x--, --x -> xrval - 1;
1967 // x = x binop expr; -> xrval binop expr
1968 // x = expr Op x; - > expr binop xrval;
1969 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
1970 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
1971 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
1972 NewVValType = XRValExpr->getType();
1973 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
1974 auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr,
1975 IsSeqCst, IsPostfixUpdate](RValue XRValue) -> RValue {
1976 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
1977 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
1978 RValue Res = CGF.EmitAnyExpr(UE);
1979 NewVVal = IsPostfixUpdate ? XRValue : Res;
1980 return Res;
1981 };
1982 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
1983 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
1984 if (Res.first) {
1985 // 'atomicrmw' instruction was generated.
1986 if (IsPostfixUpdate) {
1987 // Use old value from 'atomicrmw'.
1988 NewVVal = Res.second;
1989 } else {
1990 // 'atomicrmw' does not provide new value, so evaluate it using old
1991 // value of 'x'.
1992 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
1993 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second);
1994 NewVVal = CGF.EmitAnyExpr(UE);
1995 }
1996 }
1997 } else {
1998 // 'x' is simply rewritten with some 'expr'.
1999 NewVValType = X->getType().getNonReferenceType();
2000 ExprRValue = convertToType(CGF, ExprRValue, E->getType(),
2001 X->getType().getNonReferenceType());
2002 auto &&Gen = [&CGF, &NewVVal, ExprRValue](RValue XRValue) -> RValue {
2003 NewVVal = XRValue;
2004 return ExprRValue;
2005 };
2006 // Try to perform atomicrmw xchg, otherwise simple exchange.
2007 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
2008 XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO,
2009 Loc, Gen);
2010 if (Res.first) {
2011 // 'atomicrmw' instruction was generated.
2012 NewVVal = IsPostfixUpdate ? Res.second : ExprRValue;
2013 }
2014 }
2015 // Emit post-update store to 'v' of old/new 'x' value.
2016 emitSimpleStore(CGF, VLValue, NewVVal, NewVValType);
Alexey Bataevb4505a72015-03-30 05:20:59 +00002017 // OpenMP, 2.12.6, atomic Construct
2018 // Any atomic construct with a seq_cst clause forces the atomically
2019 // performed operation to include an implicit flush operation without a
2020 // list.
2021 if (IsSeqCst)
2022 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
2023}
2024
Alexey Bataevb57056f2015-01-22 06:17:56 +00002025static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind,
Alexey Bataev5e018f92015-04-23 06:35:10 +00002026 bool IsSeqCst, bool IsPostfixUpdate,
2027 const Expr *X, const Expr *V, const Expr *E,
2028 const Expr *UE, bool IsXLHSInRHSPart,
2029 SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00002030 switch (Kind) {
2031 case OMPC_read:
2032 EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc);
2033 break;
2034 case OMPC_write:
Alexey Bataevb8329262015-02-27 06:33:30 +00002035 EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc);
2036 break;
Alexey Bataevb4505a72015-03-30 05:20:59 +00002037 case OMPC_unknown:
Alexey Bataevb57056f2015-01-22 06:17:56 +00002038 case OMPC_update:
Alexey Bataevb4505a72015-03-30 05:20:59 +00002039 EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc);
2040 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00002041 case OMPC_capture:
Alexey Bataev5e018f92015-04-23 06:35:10 +00002042 EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE,
2043 IsXLHSInRHSPart, Loc);
2044 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00002045 case OMPC_if:
2046 case OMPC_final:
2047 case OMPC_num_threads:
2048 case OMPC_private:
2049 case OMPC_firstprivate:
2050 case OMPC_lastprivate:
2051 case OMPC_reduction:
2052 case OMPC_safelen:
2053 case OMPC_collapse:
2054 case OMPC_default:
2055 case OMPC_seq_cst:
2056 case OMPC_shared:
2057 case OMPC_linear:
2058 case OMPC_aligned:
2059 case OMPC_copyin:
2060 case OMPC_copyprivate:
2061 case OMPC_flush:
2062 case OMPC_proc_bind:
2063 case OMPC_schedule:
2064 case OMPC_ordered:
2065 case OMPC_nowait:
2066 case OMPC_untied:
2067 case OMPC_threadprivate:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00002068 case OMPC_depend:
Alexey Bataevb57056f2015-01-22 06:17:56 +00002069 case OMPC_mergeable:
Alexey Bataevb57056f2015-01-22 06:17:56 +00002070 llvm_unreachable("Clause is not allowed in 'omp atomic'.");
2071 }
2072}
2073
2074void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) {
2075 bool IsSeqCst = S.getSingleClause(/*K=*/OMPC_seq_cst);
2076 OpenMPClauseKind Kind = OMPC_unknown;
2077 for (auto *C : S.clauses()) {
2078 // Find first clause (skip seq_cst clause, if it is first).
2079 if (C->getClauseKind() != OMPC_seq_cst) {
2080 Kind = C->getClauseKind();
2081 break;
2082 }
2083 }
Alexey Bataev10fec572015-03-11 04:48:56 +00002084
2085 const auto *CS =
2086 S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev5e018f92015-04-23 06:35:10 +00002087 if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) {
Alexey Bataev10fec572015-03-11 04:48:56 +00002088 enterFullExpression(EWC);
Alexey Bataev5e018f92015-04-23 06:35:10 +00002089 }
2090 // Processing for statements under 'atomic capture'.
2091 if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) {
2092 for (const auto *C : Compound->body()) {
2093 if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) {
2094 enterFullExpression(EWC);
2095 }
2096 }
2097 }
Alexey Bataev10fec572015-03-11 04:48:56 +00002098
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002099 LexicalScope Scope(*this, S.getSourceRange());
2100 auto &&CodeGen = [&S, Kind, IsSeqCst](CodeGenFunction &CGF) {
Alexey Bataev5e018f92015-04-23 06:35:10 +00002101 EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(),
2102 S.getV(), S.getExpr(), S.getUpdateExpr(),
2103 S.isXLHSInRHSPart(), S.getLocStart());
Alexey Bataev6f1ffc02015-04-10 04:50:10 +00002104 };
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002105 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen);
Alexey Bataev0162e452014-07-22 10:10:35 +00002106}
2107
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002108void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &) {
2109 llvm_unreachable("CodeGen for 'omp target' is not supported yet.");
2110}
2111
Alexey Bataev13314bf2014-10-09 04:18:56 +00002112void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &) {
2113 llvm_unreachable("CodeGen for 'omp teams' is not supported yet.");
2114}
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002115
2116void CodeGenFunction::EmitOMPCancellationPointDirective(
2117 const OMPCancellationPointDirective &S) {
Alexey Bataev0f34da12015-07-02 04:17:07 +00002118 CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getLocStart(),
2119 S.getCancelRegion());
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002120}
2121
Alexey Bataev80909872015-07-02 11:25:17 +00002122void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) {
Alexey Bataev7d5d33e2015-07-06 05:50:32 +00002123 CGM.getOpenMPRuntime().emitCancelCall(*this, S.getLocStart(),
2124 S.getCancelRegion());
Alexey Bataev80909872015-07-02 11:25:17 +00002125}
2126
Alexey Bataev81c7ea02015-07-03 09:56:58 +00002127CodeGenFunction::JumpDest
2128CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) {
2129 if (Kind == OMPD_parallel || Kind == OMPD_task)
2130 return ReturnBlock;
2131 else if (Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections)
2132 return BreakContinueStack.empty() ? JumpDest()
2133 : BreakContinueStack.back().BreakBlock;
2134 return JumpDest();
2135}
Michael Wong65f367f2015-07-21 13:44:28 +00002136
2137// Generate the instructions for '#pragma omp target data' directive.
2138void CodeGenFunction::EmitOMPTargetDataDirective(
2139 const OMPTargetDataDirective &S) {
2140
2141 // emit the code inside the construct for now
2142 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
2143 EmitStmt(CS->getCapturedStmt());
2144}