blob: f74e47a4ca5a137ac09dac1aeacdada7df55224e [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 Bataev36bf0112015-03-10 05:15:26 +000026namespace {
27/// \brief RAII for inlined OpenMP regions (like 'omp for', 'omp simd', 'omp
28/// critical' etc.). Helps to generate proper debug info and provides correct
29/// code generation for such constructs.
30class InlinedOpenMPRegionScopeRAII {
31 InlinedOpenMPRegionRAII Region;
32 CodeGenFunction::LexicalScope DirectiveScope;
33
34public:
35 InlinedOpenMPRegionScopeRAII(CodeGenFunction &CGF,
36 const OMPExecutableDirective &D)
37 : Region(CGF, D), DirectiveScope(CGF, D.getSourceRange()) {}
38};
39} // namespace
Alexey Bataev9959db52014-05-06 10:08:46 +000040
Alexey Bataevd74d0602014-10-13 06:02:40 +000041/// \brief Emits code for OpenMP 'if' clause using specified \a CodeGen
42/// function. Here is the logic:
43/// if (Cond) {
44/// CodeGen(true);
45/// } else {
46/// CodeGen(false);
47/// }
48static void EmitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond,
49 const std::function<void(bool)> &CodeGen) {
50 CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange());
51
52 // If the condition constant folds and can be elided, try to avoid emitting
53 // the condition and the dead arm of the if/else.
54 bool CondConstant;
55 if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) {
56 CodeGen(CondConstant);
57 return;
58 }
59
60 // Otherwise, the condition did not fold, or we couldn't elide it. Just
61 // emit the conditional branch.
62 auto ThenBlock = CGF.createBasicBlock(/*name*/ "omp_if.then");
63 auto ElseBlock = CGF.createBasicBlock(/*name*/ "omp_if.else");
64 auto ContBlock = CGF.createBasicBlock(/*name*/ "omp_if.end");
65 CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount*/ 0);
66
67 // Emit the 'then' code.
68 CGF.EmitBlock(ThenBlock);
69 CodeGen(/*ThenBlock*/ true);
70 CGF.EmitBranch(ContBlock);
71 // Emit the 'else' code if present.
72 {
73 // There is no need to emit line number for unconditional branch.
Adrian Prantl95b24e92015-02-03 20:00:54 +000074 auto NL = ApplyDebugLocation::CreateEmpty(CGF);
Alexey Bataevd74d0602014-10-13 06:02:40 +000075 CGF.EmitBlock(ElseBlock);
76 }
77 CodeGen(/*ThenBlock*/ false);
78 {
79 // There is no need to emit line number for unconditional branch.
Adrian Prantl95b24e92015-02-03 20:00:54 +000080 auto NL = ApplyDebugLocation::CreateEmpty(CGF);
Alexey Bataevd74d0602014-10-13 06:02:40 +000081 CGF.EmitBranch(ContBlock);
82 }
83 // Emit the continuation block for code after the if.
84 CGF.EmitBlock(ContBlock, /*IsFinished*/ true);
85}
86
Alexey Bataev4a5bb772014-10-08 14:01:46 +000087void CodeGenFunction::EmitOMPAggregateAssign(LValue OriginalAddr,
88 llvm::Value *PrivateAddr,
89 const Expr *AssignExpr,
90 QualType OriginalType,
91 const VarDecl *VDInit) {
92 EmitBlock(createBasicBlock(".omp.assign.begin."));
93 if (!isa<CXXConstructExpr>(AssignExpr) || isTrivialInitializer(AssignExpr)) {
94 // Perform simple memcpy.
95 EmitAggregateAssign(PrivateAddr, OriginalAddr.getAddress(),
96 AssignExpr->getType());
97 } else {
98 // Perform element-by-element initialization.
99 QualType ElementTy;
100 auto SrcBegin = OriginalAddr.getAddress();
101 auto DestBegin = PrivateAddr;
102 auto ArrayTy = OriginalType->getAsArrayTypeUnsafe();
103 auto SrcNumElements = emitArrayLength(ArrayTy, ElementTy, SrcBegin);
104 auto DestNumElements = emitArrayLength(ArrayTy, ElementTy, DestBegin);
105 auto SrcEnd = Builder.CreateGEP(SrcBegin, SrcNumElements);
106 auto DestEnd = Builder.CreateGEP(DestBegin, DestNumElements);
107 // The basic structure here is a do-while loop, because we don't
108 // need to check for the zero-element case.
109 auto BodyBB = createBasicBlock("omp.arraycpy.body");
110 auto DoneBB = createBasicBlock("omp.arraycpy.done");
111 auto IsEmpty =
112 Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty");
113 Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
114
115 // Enter the loop body, making that address the current address.
116 auto EntryBB = Builder.GetInsertBlock();
117 EmitBlock(BodyBB);
118 auto SrcElementPast = Builder.CreatePHI(SrcBegin->getType(), 2,
119 "omp.arraycpy.srcElementPast");
120 SrcElementPast->addIncoming(SrcEnd, EntryBB);
121 auto DestElementPast = Builder.CreatePHI(DestBegin->getType(), 2,
122 "omp.arraycpy.destElementPast");
123 DestElementPast->addIncoming(DestEnd, EntryBB);
124
125 // Shift the address back by one element.
126 auto NegativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
127 auto DestElement = Builder.CreateGEP(DestElementPast, NegativeOne,
128 "omp.arraycpy.dest.element");
129 auto SrcElement = Builder.CreateGEP(SrcElementPast, NegativeOne,
130 "omp.arraycpy.src.element");
131 {
132 // Create RunCleanScope to cleanup possible temps.
133 CodeGenFunction::RunCleanupsScope Init(*this);
134 // Emit initialization for single element.
135 LocalDeclMap[VDInit] = SrcElement;
136 EmitAnyExprToMem(AssignExpr, DestElement,
137 AssignExpr->getType().getQualifiers(),
138 /*IsInitializer*/ false);
139 LocalDeclMap.erase(VDInit);
140 }
141
142 // Check whether we've reached the end.
143 auto Done =
144 Builder.CreateICmpEQ(DestElement, DestBegin, "omp.arraycpy.done");
145 Builder.CreateCondBr(Done, DoneBB, BodyBB);
146 DestElementPast->addIncoming(DestElement, Builder.GetInsertBlock());
147 SrcElementPast->addIncoming(SrcElement, Builder.GetInsertBlock());
148
149 // Done.
150 EmitBlock(DoneBB, true);
151 }
152 EmitBlock(createBasicBlock(".omp.assign.end."));
153}
154
155void CodeGenFunction::EmitOMPFirstprivateClause(
156 const OMPExecutableDirective &D,
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000157 CodeGenFunction::OMPPrivateScope &PrivateScope) {
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000158 auto PrivateFilter = [](const OMPClause *C) -> bool {
159 return C->getClauseKind() == OMPC_firstprivate;
160 };
161 for (OMPExecutableDirective::filtered_clause_iterator<decltype(PrivateFilter)>
162 I(D.clauses(), PrivateFilter); I; ++I) {
163 auto *C = cast<OMPFirstprivateClause>(*I);
164 auto IRef = C->varlist_begin();
165 auto InitsRef = C->inits().begin();
166 for (auto IInit : C->private_copies()) {
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000167 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
168 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
169 bool IsRegistered;
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000170 if (*InitsRef != nullptr) {
171 // Emit VarDecl with copy init for arrays.
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000172 auto *FD = CapturedStmtInfo->lookup(OrigVD);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000173 LValue Base = MakeNaturalAlignAddrLValue(
174 CapturedStmtInfo->getContextValue(),
175 getContext().getTagDeclType(FD->getParent()));
176 auto OriginalAddr = EmitLValueForField(Base, FD);
177 auto VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl());
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000178 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value * {
179 auto Emission = EmitAutoVarAlloca(*VD);
180 // Emit initialization of aggregate firstprivate vars.
181 EmitOMPAggregateAssign(OriginalAddr, Emission.getAllocatedAddress(),
182 VD->getInit(), (*IRef)->getType(), VDInit);
183 EmitAutoVarCleanups(Emission);
184 return Emission.getAllocatedAddress();
185 });
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000186 } else
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000187 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value * {
188 // Emit private VarDecl with copy init.
189 EmitDecl(*VD);
190 return GetAddrOfLocalVar(VD);
191 });
Alexander Musman7931b982015-03-16 07:14:41 +0000192 assert(IsRegistered && "firstprivate var already registered as private");
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000193 // Silence the warning about unused variable.
194 (void)IsRegistered;
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000195 ++IRef, ++InitsRef;
196 }
197 }
198}
199
Alexey Bataev03b340a2014-10-21 03:16:40 +0000200void CodeGenFunction::EmitOMPPrivateClause(
201 const OMPExecutableDirective &D,
202 CodeGenFunction::OMPPrivateScope &PrivateScope) {
203 auto PrivateFilter = [](const OMPClause *C) -> bool {
204 return C->getClauseKind() == OMPC_private;
205 };
206 for (OMPExecutableDirective::filtered_clause_iterator<decltype(PrivateFilter)>
207 I(D.clauses(), PrivateFilter); I; ++I) {
208 auto *C = cast<OMPPrivateClause>(*I);
209 auto IRef = C->varlist_begin();
210 for (auto IInit : C->private_copies()) {
211 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
212 auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
213 bool IsRegistered =
214 PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value * {
215 // Emit private VarDecl with copy init.
216 EmitDecl(*VD);
217 return GetAddrOfLocalVar(VD);
218 });
Alexander Musman7931b982015-03-16 07:14:41 +0000219 assert(IsRegistered && "private var already registered as private");
Alexey Bataev03b340a2014-10-21 03:16:40 +0000220 // Silence the warning about unused variable.
221 (void)IsRegistered;
222 ++IRef;
223 }
224 }
225}
226
Alexey Bataevb2059782014-10-13 08:23:51 +0000227/// \brief Emits code for OpenMP parallel directive in the parallel region.
228static void EmitOMPParallelCall(CodeGenFunction &CGF,
229 const OMPParallelDirective &S,
230 llvm::Value *OutlinedFn,
231 llvm::Value *CapturedStruct) {
232 if (auto C = S.getSingleClause(/*K*/ OMPC_num_threads)) {
233 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
234 auto NumThreadsClause = cast<OMPNumThreadsClause>(C);
235 auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
236 /*IgnoreResultAssign*/ true);
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000237 CGF.CGM.getOpenMPRuntime().emitNumThreadsClause(
Alexey Bataevb2059782014-10-13 08:23:51 +0000238 CGF, NumThreads, NumThreadsClause->getLocStart());
239 }
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000240 CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn,
241 CapturedStruct);
Alexey Bataevb2059782014-10-13 08:23:51 +0000242}
243
Alexey Bataev9959db52014-05-06 10:08:46 +0000244void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
Alexey Bataev18095712014-10-10 12:19:54 +0000245 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
246 auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000247 auto OutlinedFn = CGM.getOpenMPRuntime().emitOutlinedFunction(
Alexey Bataev18095712014-10-10 12:19:54 +0000248 S, *CS->getCapturedDecl()->param_begin());
Alexey Bataevd74d0602014-10-13 06:02:40 +0000249 if (auto C = S.getSingleClause(/*K*/ OMPC_if)) {
250 auto Cond = cast<OMPIfClause>(C)->getCondition();
251 EmitOMPIfClause(*this, Cond, [&](bool ThenBlock) {
252 if (ThenBlock)
Alexey Bataevb2059782014-10-13 08:23:51 +0000253 EmitOMPParallelCall(*this, S, OutlinedFn, CapturedStruct);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000254 else
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000255 CGM.getOpenMPRuntime().emitSerialCall(*this, S.getLocStart(),
256 OutlinedFn, CapturedStruct);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000257 });
Alexey Bataevb2059782014-10-13 08:23:51 +0000258 } else
259 EmitOMPParallelCall(*this, S, OutlinedFn, CapturedStruct);
Alexey Bataev9959db52014-05-06 10:08:46 +0000260}
Alexander Musman515ad8c2014-05-22 08:54:05 +0000261
Alexander Musmand196ef22014-10-07 08:57:09 +0000262void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &S,
Alexander Musmana5f070a2014-10-01 06:03:56 +0000263 bool SeparateIter) {
264 RunCleanupsScope BodyScope(*this);
265 // Update counters values on current iteration.
266 for (auto I : S.updates()) {
267 EmitIgnoredExpr(I);
268 }
Alexander Musman3276a272015-03-21 10:12:56 +0000269 // Update the linear variables.
270 for (auto C : OMPExecutableDirective::linear_filter(S.clauses())) {
271 for (auto U : C->updates()) {
272 EmitIgnoredExpr(U);
273 }
274 }
275
Alexander Musmana5f070a2014-10-01 06:03:56 +0000276 // On a continue in the body, jump to the end.
Alexander Musmand196ef22014-10-07 08:57:09 +0000277 auto Continue = getJumpDestInCurrentScope("omp.body.continue");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000278 BreakContinueStack.push_back(BreakContinue(JumpDest(), Continue));
279 // Emit loop body.
280 EmitStmt(S.getBody());
281 // The end (updates/cleanups).
282 EmitBlock(Continue.getBlock());
283 BreakContinueStack.pop_back();
284 if (SeparateIter) {
285 // TODO: Update lastprivates if the SeparateIter flag is true.
286 // This will be implemented in a follow-up OMPLastprivateClause patch, but
287 // result should be still correct without it, as we do not make these
288 // variables private yet.
289 }
290}
291
Alexey Bataev2df54a02015-03-12 08:53:29 +0000292void CodeGenFunction::EmitOMPInnerLoop(const Stmt &S, bool RequiresCleanup,
293 const Expr *LoopCond,
294 const Expr *IncExpr,
295 const std::function<void()> &BodyGen) {
Alexander Musmand196ef22014-10-07 08:57:09 +0000296 auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000297 auto Cnt = getPGORegionCounter(&S);
298
299 // Start the loop with a block that tests the condition.
Alexander Musmand196ef22014-10-07 08:57:09 +0000300 auto CondBlock = createBasicBlock("omp.inner.for.cond");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000301 EmitBlock(CondBlock);
302 LoopStack.push(CondBlock);
303
304 // If there are any cleanups between here and the loop-exit scope,
305 // create a block to stage a loop exit along.
306 auto ExitBlock = LoopExit.getBlock();
Alexey Bataev2df54a02015-03-12 08:53:29 +0000307 if (RequiresCleanup)
Alexander Musmand196ef22014-10-07 08:57:09 +0000308 ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000309
Alexander Musmand196ef22014-10-07 08:57:09 +0000310 auto LoopBody = createBasicBlock("omp.inner.for.body");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000311
Alexey Bataev2df54a02015-03-12 08:53:29 +0000312 // Emit condition.
313 EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, Cnt.getCount());
Alexander Musmana5f070a2014-10-01 06:03:56 +0000314 if (ExitBlock != LoopExit.getBlock()) {
315 EmitBlock(ExitBlock);
316 EmitBranchThroughCleanup(LoopExit);
317 }
318
319 EmitBlock(LoopBody);
320 Cnt.beginRegion(Builder);
321
322 // Create a block for the increment.
Alexander Musmand196ef22014-10-07 08:57:09 +0000323 auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc");
Alexander Musmana5f070a2014-10-01 06:03:56 +0000324 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
325
Alexey Bataev2df54a02015-03-12 08:53:29 +0000326 BodyGen();
Alexander Musmana5f070a2014-10-01 06:03:56 +0000327
328 // Emit "IV = IV + 1" and a back-edge to the condition block.
329 EmitBlock(Continue.getBlock());
Alexey Bataev2df54a02015-03-12 08:53:29 +0000330 EmitIgnoredExpr(IncExpr);
Alexander Musmana5f070a2014-10-01 06:03:56 +0000331 BreakContinueStack.pop_back();
332 EmitBranch(CondBlock);
333 LoopStack.pop();
334 // Emit the fall-through block.
335 EmitBlock(LoopExit.getBlock());
336}
337
338void CodeGenFunction::EmitOMPSimdFinal(const OMPLoopDirective &S) {
339 auto IC = S.counters().begin();
340 for (auto F : S.finals()) {
341 if (LocalDeclMap.lookup(cast<DeclRefExpr>((*IC))->getDecl())) {
342 EmitIgnoredExpr(F);
343 }
344 ++IC;
345 }
Alexander Musman3276a272015-03-21 10:12:56 +0000346 // Emit the final values of the linear variables.
347 for (auto C : OMPExecutableDirective::linear_filter(S.clauses())) {
348 for (auto F : C->finals()) {
349 EmitIgnoredExpr(F);
350 }
351 }
Alexander Musmana5f070a2014-10-01 06:03:56 +0000352}
353
Alexander Musman09184fe2014-09-30 05:29:28 +0000354static void EmitOMPAlignedClause(CodeGenFunction &CGF, CodeGenModule &CGM,
355 const OMPAlignedClause &Clause) {
356 unsigned ClauseAlignment = 0;
357 if (auto AlignmentExpr = Clause.getAlignment()) {
358 auto AlignmentCI =
359 cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr));
360 ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue());
361 }
362 for (auto E : Clause.varlists()) {
363 unsigned Alignment = ClauseAlignment;
364 if (Alignment == 0) {
365 // OpenMP [2.8.1, Description]
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000366 // If no optional parameter is specified, implementation-defined default
Alexander Musman09184fe2014-09-30 05:29:28 +0000367 // alignments for SIMD instructions on the target platforms are assumed.
368 Alignment = CGM.getTargetCodeGenInfo().getOpenMPSimdDefaultAlignment(
369 E->getType());
370 }
371 assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) &&
372 "alignment is not power of 2");
373 if (Alignment != 0) {
374 llvm::Value *PtrValue = CGF.EmitScalarExpr(E);
375 CGF.EmitAlignmentAssumption(PtrValue, Alignment);
376 }
377 }
378}
379
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000380static void EmitPrivateLoopCounters(CodeGenFunction &CGF,
381 CodeGenFunction::OMPPrivateScope &LoopScope,
382 ArrayRef<Expr *> Counters) {
383 for (auto *E : Counters) {
384 auto VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
385 bool IsRegistered = LoopScope.addPrivate(VD, [&]() -> llvm::Value * {
386 // Emit var without initialization.
387 auto VarEmission = CGF.EmitAutoVarAlloca(*VD);
388 CGF.EmitAutoVarCleanups(VarEmission);
389 return VarEmission.getAllocatedAddress();
390 });
391 assert(IsRegistered && "counter already registered as private");
392 // Silence the warning about unused variable.
393 (void)IsRegistered;
394 }
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000395}
396
Alexander Musman3276a272015-03-21 10:12:56 +0000397static void
398EmitPrivateLinearVars(CodeGenFunction &CGF, const OMPExecutableDirective &D,
399 CodeGenFunction::OMPPrivateScope &PrivateScope) {
400 for (auto Clause : OMPExecutableDirective::linear_filter(D.clauses())) {
401 for (auto *E : Clause->varlists()) {
402 auto VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
403 bool IsRegistered = PrivateScope.addPrivate(VD, [&]()->llvm::Value * {
404 // Emit var without initialization.
405 auto VarEmission = CGF.EmitAutoVarAlloca(*VD);
406 CGF.EmitAutoVarCleanups(VarEmission);
407 return VarEmission.getAllocatedAddress();
408 });
409 assert(IsRegistered && "linear var already registered as private");
410 // Silence the warning about unused variable.
411 (void)IsRegistered;
412 }
413 }
414}
415
Alexander Musman515ad8c2014-05-22 08:54:05 +0000416void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) {
Alexander Musmana5f070a2014-10-01 06:03:56 +0000417 // Pragma 'simd' code depends on presence of 'lastprivate'.
418 // If present, we have to separate last iteration of the loop:
419 //
420 // if (LastIteration != 0) {
421 // for (IV in 0..LastIteration-1) BODY;
422 // BODY with updates of lastprivate vars;
423 // <Final counter/linear vars updates>;
424 // }
425 //
426 // otherwise (when there's no lastprivate):
427 //
428 // for (IV in 0..LastIteration) BODY;
429 // <Final counter/linear vars updates>;
430 //
431
432 // Walk clauses and process safelen/lastprivate.
433 bool SeparateIter = false;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000434 LoopStack.setParallel();
435 LoopStack.setVectorizerEnable(true);
436 for (auto C : S.clauses()) {
437 switch (C->getClauseKind()) {
438 case OMPC_safelen: {
439 RValue Len = EmitAnyExpr(cast<OMPSafelenClause>(C)->getSafelen(),
440 AggValueSlot::ignored(), true);
441 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
442 LoopStack.setVectorizerWidth(Val->getZExtValue());
443 // In presence of finite 'safelen', it may be unsafe to mark all
444 // the memory instructions parallel, because loop-carried
445 // dependences of 'safelen' iterations are possible.
446 LoopStack.setParallel(false);
447 break;
448 }
Alexander Musman09184fe2014-09-30 05:29:28 +0000449 case OMPC_aligned:
450 EmitOMPAlignedClause(*this, CGM, cast<OMPAlignedClause>(*C));
451 break;
Alexander Musmana5f070a2014-10-01 06:03:56 +0000452 case OMPC_lastprivate:
453 SeparateIter = true;
454 break;
Alexander Musman515ad8c2014-05-22 08:54:05 +0000455 default:
456 // Not handled yet
457 ;
458 }
459 }
Alexander Musmana5f070a2014-10-01 06:03:56 +0000460
Alexey Bataev36bf0112015-03-10 05:15:26 +0000461 InlinedOpenMPRegionScopeRAII Region(*this, S);
Alexander Musmana5f070a2014-10-01 06:03:56 +0000462
Alexander Musman3276a272015-03-21 10:12:56 +0000463 // Emit inits for the linear variables.
464 for (auto C : OMPExecutableDirective::linear_filter(S.clauses())) {
465 for (auto Init : C->inits()) {
466 auto *D = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl());
467 EmitVarDecl(*D);
468 }
469 }
470
Alexander Musmana5f070a2014-10-01 06:03:56 +0000471 // Emit the loop iteration variable.
472 const Expr *IVExpr = S.getIterationVariable();
473 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
474 EmitVarDecl(*IVDecl);
475 EmitIgnoredExpr(S.getInit());
476
477 // Emit the iterations count variable.
478 // If it is not a variable, Sema decided to calculate iterations count on each
479 // iteration (e.g., it is foldable into a constant).
480 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
481 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
482 // Emit calculation of the iterations count.
483 EmitIgnoredExpr(S.getCalcLastIteration());
484 }
485
Alexander Musman3276a272015-03-21 10:12:56 +0000486 // Emit the linear steps for the linear clauses.
487 // If a step is not constant, it is pre-calculated before the loop.
488 for (auto C : OMPExecutableDirective::linear_filter(S.clauses())) {
489 if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep()))
490 if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) {
491 EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl()));
492 // Emit calculation of the linear step.
493 EmitIgnoredExpr(CS);
494 }
495 }
496
Alexander Musmana5f070a2014-10-01 06:03:56 +0000497 if (SeparateIter) {
498 // Emit: if (LastIteration > 0) - begin.
499 RegionCounter Cnt = getPGORegionCounter(&S);
500 auto ThenBlock = createBasicBlock("simd.if.then");
501 auto ContBlock = createBasicBlock("simd.if.end");
502 EmitBranchOnBoolExpr(S.getPreCond(), ThenBlock, ContBlock, Cnt.getCount());
503 EmitBlock(ThenBlock);
504 Cnt.beginRegion(Builder);
505 // Emit 'then' code.
506 {
507 OMPPrivateScope LoopScope(*this);
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000508 EmitPrivateLoopCounters(*this, LoopScope, S.counters());
Alexander Musman3276a272015-03-21 10:12:56 +0000509 EmitPrivateLinearVars(*this, S, LoopScope);
Alexander Musman7931b982015-03-16 07:14:41 +0000510 EmitOMPPrivateClause(S, LoopScope);
511 (void)LoopScope.Privatize();
Alexey Bataev2df54a02015-03-12 08:53:29 +0000512 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(),
513 S.getCond(/*SeparateIter=*/true), S.getInc(),
514 [&S, this]() {
515 EmitOMPLoopBody(S);
516 EmitStopPoint(&S);
517 });
Alexander Musmand196ef22014-10-07 08:57:09 +0000518 EmitOMPLoopBody(S, /* SeparateIter */ true);
Alexander Musmana5f070a2014-10-01 06:03:56 +0000519 }
520 EmitOMPSimdFinal(S);
521 // Emit: if (LastIteration != 0) - end.
522 EmitBranch(ContBlock);
523 EmitBlock(ContBlock, true);
524 } else {
525 {
526 OMPPrivateScope LoopScope(*this);
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000527 EmitPrivateLoopCounters(*this, LoopScope, S.counters());
Alexander Musman3276a272015-03-21 10:12:56 +0000528 EmitPrivateLinearVars(*this, S, LoopScope);
Alexander Musman7931b982015-03-16 07:14:41 +0000529 EmitOMPPrivateClause(S, LoopScope);
530 (void)LoopScope.Privatize();
Alexey Bataev2df54a02015-03-12 08:53:29 +0000531 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(),
532 S.getCond(/*SeparateIter=*/false), S.getInc(),
533 [&S, this]() {
534 EmitOMPLoopBody(S);
535 EmitStopPoint(&S);
536 });
Alexander Musmana5f070a2014-10-01 06:03:56 +0000537 }
538 EmitOMPSimdFinal(S);
539 }
Alexander Musman515ad8c2014-05-22 08:54:05 +0000540}
541
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000542void CodeGenFunction::EmitOMPForOuterLoop(OpenMPScheduleClauseKind ScheduleKind,
543 const OMPLoopDirective &S,
544 OMPPrivateScope &LoopScope,
545 llvm::Value *LB, llvm::Value *UB,
546 llvm::Value *ST, llvm::Value *IL,
547 llvm::Value *Chunk) {
548 auto &RT = CGM.getOpenMPRuntime();
Alexander Musman92bdaab2015-03-12 13:37:50 +0000549
550 // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime).
551 const bool Dynamic = RT.isDynamic(ScheduleKind);
552
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000553 assert(!RT.isStaticNonchunked(ScheduleKind, /* Chunked */ Chunk != nullptr) &&
554 "static non-chunked schedule does not need outer loop");
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000555
556 // Emit outer loop.
557 //
558 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
Alexander Musman92bdaab2015-03-12 13:37:50 +0000559 // When schedule(dynamic,chunk_size) is specified, the iterations are
560 // distributed to threads in the team in chunks as the threads request them.
561 // Each thread executes a chunk of iterations, then requests another chunk,
562 // until no chunks remain to be distributed. Each chunk contains chunk_size
563 // iterations, except for the last chunk to be distributed, which may have
564 // fewer iterations. When no chunk_size is specified, it defaults to 1.
565 //
566 // When schedule(guided,chunk_size) is specified, the iterations are assigned
567 // to threads in the team in chunks as the executing threads request them.
568 // Each thread executes a chunk of iterations, then requests another chunk,
569 // until no chunks remain to be assigned. For a chunk_size of 1, the size of
570 // each chunk is proportional to the number of unassigned iterations divided
571 // by the number of threads in the team, decreasing to 1. For a chunk_size
572 // with value k (greater than 1), the size of each chunk is determined in the
573 // same way, with the restriction that the chunks do not contain fewer than k
574 // iterations (except for the last chunk to be assigned, which may have fewer
575 // than k iterations).
576 //
577 // When schedule(auto) is specified, the decision regarding scheduling is
578 // delegated to the compiler and/or runtime system. The programmer gives the
579 // implementation the freedom to choose any possible mapping of iterations to
580 // threads in the team.
581 //
582 // When schedule(runtime) is specified, the decision regarding scheduling is
583 // deferred until run time, and the schedule and chunk size are taken from the
584 // run-sched-var ICV. If the ICV is set to auto, the schedule is
585 // implementation defined
586 //
587 // while(__kmpc_dispatch_next(&LB, &UB)) {
588 // idx = LB;
589 // while (idx <= UB) { BODY; ++idx; } // inner loop
590 // }
591 //
592 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000593 // When schedule(static, chunk_size) is specified, iterations are divided into
594 // chunks of size chunk_size, and the chunks are assigned to the threads in
595 // the team in a round-robin fashion in the order of the thread number.
596 //
597 // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) {
598 // while (idx <= UB) { BODY; ++idx; } // inner loop
599 // LB = LB + ST;
600 // UB = UB + ST;
601 // }
602 //
Alexander Musman92bdaab2015-03-12 13:37:50 +0000603
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000604 const Expr *IVExpr = S.getIterationVariable();
605 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
606 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
607
Alexander Musman92bdaab2015-03-12 13:37:50 +0000608 RT.emitForInit(
609 *this, S.getLocStart(), ScheduleKind, IVSize, IVSigned, IL, LB,
610 (Dynamic ? EmitAnyExpr(S.getLastIteration()).getScalarVal() : UB), ST,
611 Chunk);
612
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000613 auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end");
614
615 // Start the loop with a block that tests the condition.
616 auto CondBlock = createBasicBlock("omp.dispatch.cond");
617 EmitBlock(CondBlock);
618 LoopStack.push(CondBlock);
619
620 llvm::Value *BoolCondVal = nullptr;
Alexander Musman92bdaab2015-03-12 13:37:50 +0000621 if (!Dynamic) {
622 // UB = min(UB, GlobalUB)
623 EmitIgnoredExpr(S.getEnsureUpperBound());
624 // IV = LB
625 EmitIgnoredExpr(S.getInit());
626 // IV < UB
627 BoolCondVal = EvaluateExprAsBool(S.getCond(false));
628 } else {
629 BoolCondVal = RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned,
630 IL, LB, UB, ST);
631 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000632
633 // If there are any cleanups between here and the loop-exit scope,
634 // create a block to stage a loop exit along.
635 auto ExitBlock = LoopExit.getBlock();
636 if (LoopScope.requiresCleanups())
637 ExitBlock = createBasicBlock("omp.dispatch.cleanup");
638
639 auto LoopBody = createBasicBlock("omp.dispatch.body");
640 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
641 if (ExitBlock != LoopExit.getBlock()) {
642 EmitBlock(ExitBlock);
643 EmitBranchThroughCleanup(LoopExit);
644 }
645 EmitBlock(LoopBody);
646
Alexander Musman92bdaab2015-03-12 13:37:50 +0000647 // Emit "IV = LB" (in case of static schedule, we have already calculated new
648 // LB for loop condition and emitted it above).
649 if (Dynamic)
650 EmitIgnoredExpr(S.getInit());
651
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000652 // Create a block for the increment.
653 auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc");
654 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
655
Alexey Bataev2df54a02015-03-12 08:53:29 +0000656 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(),
657 S.getCond(/*SeparateIter=*/false), S.getInc(), [&S, this]() {
658 EmitOMPLoopBody(S);
659 EmitStopPoint(&S);
660 });
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000661
662 EmitBlock(Continue.getBlock());
663 BreakContinueStack.pop_back();
Alexander Musman92bdaab2015-03-12 13:37:50 +0000664 if (!Dynamic) {
665 // Emit "LB = LB + Stride", "UB = UB + Stride".
666 EmitIgnoredExpr(S.getNextLowerBound());
667 EmitIgnoredExpr(S.getNextUpperBound());
668 }
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000669
670 EmitBranch(CondBlock);
671 LoopStack.pop();
672 // Emit the fall-through block.
673 EmitBlock(LoopExit.getBlock());
674
675 // Tell the runtime we are done.
Alexander Musman92bdaab2015-03-12 13:37:50 +0000676 // FIXME: Also call fini for ordered loops with dynamic scheduling.
677 if (!Dynamic)
678 RT.emitForFinish(*this, S.getLocStart(), ScheduleKind);
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000679}
680
Alexander Musmanc6388682014-12-15 07:07:06 +0000681/// \brief Emit a helper variable and return corresponding lvalue.
682static LValue EmitOMPHelperVar(CodeGenFunction &CGF,
683 const DeclRefExpr *Helper) {
684 auto VDecl = cast<VarDecl>(Helper->getDecl());
685 CGF.EmitVarDecl(*VDecl);
686 return CGF.EmitLValue(Helper);
687}
688
689void CodeGenFunction::EmitOMPWorksharingLoop(const OMPLoopDirective &S) {
690 // Emit the loop iteration variable.
691 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
692 auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
693 EmitVarDecl(*IVDecl);
694
695 // Emit the iterations count variable.
696 // If it is not a variable, Sema decided to calculate iterations count on each
697 // iteration (e.g., it is foldable into a constant).
698 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
699 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
700 // Emit calculation of the iterations count.
701 EmitIgnoredExpr(S.getCalcLastIteration());
702 }
703
704 auto &RT = CGM.getOpenMPRuntime();
705
706 // Check pre-condition.
707 {
708 // Skip the entire loop if we don't meet the precondition.
709 RegionCounter Cnt = getPGORegionCounter(&S);
710 auto ThenBlock = createBasicBlock("omp.precond.then");
711 auto ContBlock = createBasicBlock("omp.precond.end");
712 EmitBranchOnBoolExpr(S.getPreCond(), ThenBlock, ContBlock, Cnt.getCount());
713 EmitBlock(ThenBlock);
714 Cnt.beginRegion(Builder);
715 // Emit 'then' code.
716 {
717 // Emit helper vars inits.
718 LValue LB =
719 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable()));
720 LValue UB =
721 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable()));
722 LValue ST =
723 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
724 LValue IL =
725 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
726
727 OMPPrivateScope LoopScope(*this);
728 EmitPrivateLoopCounters(*this, LoopScope, S.counters());
Alexander Musman7931b982015-03-16 07:14:41 +0000729 (void)LoopScope.Privatize();
Alexander Musmanc6388682014-12-15 07:07:06 +0000730
731 // Detect the loop schedule kind and chunk.
732 auto ScheduleKind = OMPC_SCHEDULE_unknown;
733 llvm::Value *Chunk = nullptr;
734 if (auto C = cast_or_null<OMPScheduleClause>(
735 S.getSingleClause(OMPC_schedule))) {
736 ScheduleKind = C->getScheduleKind();
737 if (auto Ch = C->getChunkSize()) {
738 Chunk = EmitScalarExpr(Ch);
739 Chunk = EmitScalarConversion(Chunk, Ch->getType(),
740 S.getIterationVariable()->getType());
741 }
742 }
743 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
744 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
745 if (RT.isStaticNonchunked(ScheduleKind,
746 /* Chunked */ Chunk != nullptr)) {
747 // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
748 // When no chunk_size is specified, the iteration space is divided into
749 // chunks that are approximately equal in size, and at most one chunk is
750 // distributed to each thread. Note that the size of the chunks is
751 // unspecified in this case.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000752 RT.emitForInit(*this, S.getLocStart(), ScheduleKind, IVSize, IVSigned,
753 IL.getAddress(), LB.getAddress(), UB.getAddress(),
754 ST.getAddress());
Alexander Musmanc6388682014-12-15 07:07:06 +0000755 // UB = min(UB, GlobalUB);
756 EmitIgnoredExpr(S.getEnsureUpperBound());
757 // IV = LB;
758 EmitIgnoredExpr(S.getInit());
759 // while (idx <= UB) { BODY; ++idx; }
Alexey Bataev2df54a02015-03-12 08:53:29 +0000760 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(),
761 S.getCond(/*SeparateIter=*/false), S.getInc(),
762 [&S, this]() {
763 EmitOMPLoopBody(S);
764 EmitStopPoint(&S);
765 });
Alexander Musmanc6388682014-12-15 07:07:06 +0000766 // Tell the runtime we are done.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000767 RT.emitForFinish(*this, S.getLocStart(), ScheduleKind);
Alexander Musmandf7a8e22015-01-22 08:49:35 +0000768 } else {
769 // Emit the outer loop, which requests its work chunk [LB..UB] from
770 // runtime and runs the inner loop to process it.
771 EmitOMPForOuterLoop(ScheduleKind, S, LoopScope, LB.getAddress(),
772 UB.getAddress(), ST.getAddress(), IL.getAddress(),
773 Chunk);
774 }
Alexander Musmanc6388682014-12-15 07:07:06 +0000775 }
776 // We're now done with the loop, so jump to the continuation block.
777 EmitBranch(ContBlock);
778 EmitBlock(ContBlock, true);
779 }
780}
781
782void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) {
Alexey Bataev36bf0112015-03-10 05:15:26 +0000783 InlinedOpenMPRegionScopeRAII Region(*this, S);
Alexander Musmanc6388682014-12-15 07:07:06 +0000784
785 EmitOMPWorksharingLoop(S);
786
787 // Emit an implicit barrier at the end.
Alexey Bataevf2685682015-03-30 04:30:22 +0000788 if (!S.getSingleClause(OMPC_nowait)) {
789 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
790 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000791}
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000792
Alexander Musmanf82886e2014-09-18 05:12:34 +0000793void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &) {
794 llvm_unreachable("CodeGen for 'omp for simd' is not supported yet.");
795}
796
Alexey Bataev2df54a02015-03-12 08:53:29 +0000797static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty,
798 const Twine &Name,
799 llvm::Value *Init = nullptr) {
800 auto LVal = CGF.MakeNaturalAlignAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty);
801 if (Init)
802 CGF.EmitScalarInit(Init, LVal);
803 return LVal;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000804}
805
Alexey Bataev2df54a02015-03-12 08:53:29 +0000806void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
807 InlinedOpenMPRegionScopeRAII Region(*this, S);
808
809 auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt();
810 auto *CS = dyn_cast<CompoundStmt>(Stmt);
811 if (CS && CS->size() > 1) {
812 auto &C = CGM.getContext();
813 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
814 // Emit helper vars inits.
815 LValue LB = createSectionLVal(*this, KmpInt32Ty, ".omp.sections.lb.",
816 Builder.getInt32(0));
817 auto *GlobalUBVal = Builder.getInt32(CS->size() - 1);
818 LValue UB =
819 createSectionLVal(*this, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal);
820 LValue ST = createSectionLVal(*this, KmpInt32Ty, ".omp.sections.st.",
821 Builder.getInt32(1));
822 LValue IL = createSectionLVal(*this, KmpInt32Ty, ".omp.sections.il.",
823 Builder.getInt32(0));
824 // Loop counter.
825 LValue IV = createSectionLVal(*this, KmpInt32Ty, ".omp.sections.iv.");
826 OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
827 OpaqueValueMapping OpaqueIV(*this, &IVRefExpr, IV);
828 OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
829 OpaqueValueMapping OpaqueUB(*this, &UBRefExpr, UB);
830 // Generate condition for loop.
831 BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue,
832 OK_Ordinary, S.getLocStart(), /*fpContractable=*/false);
833 // Increment for loop counter.
834 UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary,
835 S.getLocStart());
836 auto BodyGen = [this, CS, &S, &IV]() {
837 // Iterate through all sections and emit a switch construct:
838 // switch (IV) {
839 // case 0:
840 // <SectionStmt[0]>;
841 // break;
842 // ...
843 // case <NumSection> - 1:
844 // <SectionStmt[<NumSection> - 1]>;
845 // break;
846 // }
847 // .omp.sections.exit:
848 auto *ExitBB = createBasicBlock(".omp.sections.exit");
849 auto *SwitchStmt = Builder.CreateSwitch(
850 EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB,
851 CS->size());
852 unsigned CaseNumber = 0;
853 for (auto C = CS->children(); C; ++C, ++CaseNumber) {
854 auto CaseBB = createBasicBlock(".omp.sections.case");
855 EmitBlock(CaseBB);
856 SwitchStmt->addCase(Builder.getInt32(CaseNumber), CaseBB);
857 EmitStmt(*C);
858 EmitBranch(ExitBB);
859 }
860 EmitBlock(ExitBB, /*IsFinished=*/true);
861 };
862 // Emit static non-chunked loop.
863 CGM.getOpenMPRuntime().emitForInit(
864 *this, S.getLocStart(), OMPC_SCHEDULE_static, /*IVSize=*/32,
865 /*IVSigned=*/true, IL.getAddress(), LB.getAddress(), UB.getAddress(),
866 ST.getAddress());
867 // UB = min(UB, GlobalUB);
868 auto *UBVal = EmitLoadOfScalar(UB, S.getLocStart());
869 auto *MinUBGlobalUB = Builder.CreateSelect(
870 Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal);
871 EmitStoreOfScalar(MinUBGlobalUB, UB);
872 // IV = LB;
873 EmitStoreOfScalar(EmitLoadOfScalar(LB, S.getLocStart()), IV);
874 // while (idx <= UB) { BODY; ++idx; }
875 EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen);
876 // Tell the runtime we are done.
877 CGM.getOpenMPRuntime().emitForFinish(*this, S.getLocStart(),
878 OMPC_SCHEDULE_static);
879 } else {
880 // If only one section is found - no need to generate loop, emit as a single
881 // region.
882 CGM.getOpenMPRuntime().emitSingleRegion(*this, [&]() -> void {
883 InlinedOpenMPRegionScopeRAII Region(*this, S);
884 EmitStmt(Stmt);
885 EnsureInsertPoint();
Alexey Bataeva63048e2015-03-23 06:18:07 +0000886 }, S.getLocStart(), llvm::None, llvm::None, llvm::None, llvm::None);
Alexey Bataev2df54a02015-03-12 08:53:29 +0000887 }
888
889 // Emit an implicit barrier at the end.
Alexey Bataevf2685682015-03-30 04:30:22 +0000890 if (!S.getSingleClause(OMPC_nowait)) {
891 CGM.getOpenMPRuntime().emitBarrierCall(
892 *this, S.getLocStart(),
893 (CS && CS->size() > 1) ? OMPD_sections : OMPD_single);
894 }
Alexey Bataev2df54a02015-03-12 08:53:29 +0000895}
896
897void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) {
898 InlinedOpenMPRegionScopeRAII Region(*this, S);
899 EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
900 EnsureInsertPoint();
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000901}
902
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000903void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) {
Alexey Bataeva63048e2015-03-23 06:18:07 +0000904 llvm::SmallVector<const Expr *, 8> CopyprivateVars;
905 llvm::SmallVector<const Expr *, 8> SrcExprs;
906 llvm::SmallVector<const Expr *, 8> DstExprs;
907 llvm::SmallVector<const Expr *, 8> AssignmentOps;
908 // Check if there are any 'copyprivate' clauses associated with this 'single'
909 // construct.
910 auto CopyprivateFilter = [](const OMPClause *C) -> bool {
911 return C->getClauseKind() == OMPC_copyprivate;
912 };
913 // Build a list of copyprivate variables along with helper expressions
914 // (<source>, <destination>, <destination>=<source> expressions)
915 typedef OMPExecutableDirective::filtered_clause_iterator<decltype(
916 CopyprivateFilter)> CopyprivateIter;
917 for (CopyprivateIter I(S.clauses(), CopyprivateFilter); I; ++I) {
918 auto *C = cast<OMPCopyprivateClause>(*I);
919 CopyprivateVars.append(C->varlists().begin(), C->varlists().end());
920 SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end());
921 DstExprs.append(C->destination_exprs().begin(),
922 C->destination_exprs().end());
923 AssignmentOps.append(C->assignment_ops().begin(),
924 C->assignment_ops().end());
925 }
926 // Emit code for 'single' region along with 'copyprivate' clauses
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000927 CGM.getOpenMPRuntime().emitSingleRegion(*this, [&]() -> void {
Alexey Bataev36bf0112015-03-10 05:15:26 +0000928 InlinedOpenMPRegionScopeRAII Region(*this, S);
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000929 EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
930 EnsureInsertPoint();
Alexey Bataeva63048e2015-03-23 06:18:07 +0000931 }, S.getLocStart(), CopyprivateVars, SrcExprs, DstExprs, AssignmentOps);
932 // Emit an implicit barrier at the end.
Alexey Bataevf2685682015-03-30 04:30:22 +0000933 if (!S.getSingleClause(OMPC_nowait)) {
934 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_single);
935 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000936}
937
Alexey Bataev8d690652014-12-04 07:23:53 +0000938void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000939 CGM.getOpenMPRuntime().emitMasterRegion(*this, [&]() -> void {
Alexey Bataev36bf0112015-03-10 05:15:26 +0000940 InlinedOpenMPRegionScopeRAII Region(*this, S);
Alexey Bataev8d690652014-12-04 07:23:53 +0000941 EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
942 EnsureInsertPoint();
943 }, S.getLocStart());
Alexander Musman80c22892014-07-17 08:54:58 +0000944}
945
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000946void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000947 CGM.getOpenMPRuntime().emitCriticalRegion(
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000948 *this, S.getDirectiveName().getAsString(), [&]() -> void {
Alexey Bataev36bf0112015-03-10 05:15:26 +0000949 InlinedOpenMPRegionScopeRAII Region(*this, S);
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000950 EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
951 EnsureInsertPoint();
952 }, S.getLocStart());
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000953}
954
Alexey Bataev4acb8592014-07-07 13:01:15 +0000955void
956CodeGenFunction::EmitOMPParallelForDirective(const OMPParallelForDirective &) {
957 llvm_unreachable("CodeGen for 'omp parallel for' is not supported yet.");
958}
959
Alexander Musmane4e893b2014-09-23 09:33:00 +0000960void CodeGenFunction::EmitOMPParallelForSimdDirective(
961 const OMPParallelForSimdDirective &) {
962 llvm_unreachable("CodeGen for 'omp parallel for simd' is not supported yet.");
963}
964
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000965void CodeGenFunction::EmitOMPParallelSectionsDirective(
966 const OMPParallelSectionsDirective &) {
967 llvm_unreachable("CodeGen for 'omp parallel sections' is not supported yet.");
968}
969
Alexey Bataev62b63b12015-03-10 07:28:44 +0000970void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) {
971 // Emit outlined function for task construct.
972 auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
973 auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
974 auto *I = CS->getCapturedDecl()->param_begin();
975 // The first function argument for tasks is a thread id, the second one is a
976 // part id (0 for tied tasks, >=0 for untied task).
977 auto OutlinedFn =
978 CGM.getOpenMPRuntime().emitTaskOutlinedFunction(S, *I, *std::next(I));
979 // Check if we should emit tied or untied task.
980 bool Tied = !S.getSingleClause(OMPC_untied);
981 // Check if the task is final
982 llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
983 if (auto *Clause = S.getSingleClause(OMPC_final)) {
984 // If the condition constant folds and can be elided, try to avoid emitting
985 // the condition and the dead arm of the if/else.
986 auto *Cond = cast<OMPFinalClause>(Clause)->getCondition();
987 bool CondConstant;
988 if (ConstantFoldsToSimpleInteger(Cond, CondConstant))
989 Final.setInt(CondConstant);
990 else
991 Final.setPointer(EvaluateExprAsBool(Cond));
992 } else {
993 // By default the task is not final.
994 Final.setInt(/*IntVal=*/false);
995 }
996 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
997 CGM.getOpenMPRuntime().emitTaskCall(*this, S.getLocStart(), Tied, Final,
998 OutlinedFn, SharedsTy, CapturedStruct);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000999}
1000
Alexey Bataev9f797f32015-02-05 05:57:51 +00001001void CodeGenFunction::EmitOMPTaskyieldDirective(
1002 const OMPTaskyieldDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001003 CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart());
Alexey Bataev68446b72014-07-18 07:47:19 +00001004}
1005
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00001006void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) {
Alexey Bataevf2685682015-03-30 04:30:22 +00001007 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier);
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001008}
1009
Alexey Bataev2df347a2014-07-18 10:17:07 +00001010void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &) {
1011 llvm_unreachable("CodeGen for 'omp taskwait' is not supported yet.");
1012}
1013
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001014void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001015 CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> {
1016 if (auto C = S.getSingleClause(/*K*/ OMPC_flush)) {
1017 auto FlushClause = cast<OMPFlushClause>(C);
1018 return llvm::makeArrayRef(FlushClause->varlist_begin(),
1019 FlushClause->varlist_end());
1020 }
1021 return llvm::None;
1022 }(), S.getLocStart());
Alexey Bataev6125da92014-07-21 11:26:11 +00001023}
1024
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001025void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &) {
1026 llvm_unreachable("CodeGen for 'omp ordered' is not supported yet.");
1027}
1028
Alexey Bataevb57056f2015-01-22 06:17:56 +00001029static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val,
1030 QualType SrcType, QualType DestType) {
1031 assert(CGF.hasScalarEvaluationKind(DestType) &&
1032 "DestType must have scalar evaluation kind.");
1033 assert(!Val.isAggregate() && "Must be a scalar or complex.");
1034 return Val.isScalar()
1035 ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType)
1036 : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType,
1037 DestType);
1038}
1039
1040static CodeGenFunction::ComplexPairTy
1041convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType,
1042 QualType DestType) {
1043 assert(CGF.getEvaluationKind(DestType) == TEK_Complex &&
1044 "DestType must have complex evaluation kind.");
1045 CodeGenFunction::ComplexPairTy ComplexVal;
1046 if (Val.isScalar()) {
1047 // Convert the input element to the element type of the complex.
1048 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
1049 auto ScalarVal =
1050 CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestElementType);
1051 ComplexVal = CodeGenFunction::ComplexPairTy(
1052 ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType()));
1053 } else {
1054 assert(Val.isComplex() && "Must be a scalar or complex.");
1055 auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType();
1056 auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
1057 ComplexVal.first = CGF.EmitScalarConversion(
1058 Val.getComplexVal().first, SrcElementType, DestElementType);
1059 ComplexVal.second = CGF.EmitScalarConversion(
1060 Val.getComplexVal().second, SrcElementType, DestElementType);
1061 }
1062 return ComplexVal;
1063}
1064
1065static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst,
1066 const Expr *X, const Expr *V,
1067 SourceLocation Loc) {
1068 // v = x;
1069 assert(V->isLValue() && "V of 'omp atomic read' is not lvalue");
1070 assert(X->isLValue() && "X of 'omp atomic read' is not lvalue");
1071 LValue XLValue = CGF.EmitLValue(X);
1072 LValue VLValue = CGF.EmitLValue(V);
David Majnemera5b195a2015-02-14 01:35:12 +00001073 RValue Res = XLValue.isGlobalReg()
1074 ? CGF.EmitLoadOfLValue(XLValue, Loc)
1075 : CGF.EmitAtomicLoad(XLValue, Loc,
1076 IsSeqCst ? llvm::SequentiallyConsistent
Alexey Bataevb8329262015-02-27 06:33:30 +00001077 : llvm::Monotonic,
1078 XLValue.isVolatile());
Alexey Bataevb57056f2015-01-22 06:17:56 +00001079 // OpenMP, 2.12.6, atomic Construct
1080 // Any atomic construct with a seq_cst clause forces the atomically
1081 // performed operation to include an implicit flush operation without a
1082 // list.
1083 if (IsSeqCst)
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001084 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
Alexey Bataevb57056f2015-01-22 06:17:56 +00001085 switch (CGF.getEvaluationKind(V->getType())) {
1086 case TEK_Scalar:
1087 CGF.EmitStoreOfScalar(
1088 convertToScalarValue(CGF, Res, X->getType(), V->getType()), VLValue);
1089 break;
1090 case TEK_Complex:
1091 CGF.EmitStoreOfComplex(
1092 convertToComplexValue(CGF, Res, X->getType(), V->getType()), VLValue,
1093 /*isInit=*/false);
1094 break;
1095 case TEK_Aggregate:
1096 llvm_unreachable("Must be a scalar or complex.");
1097 }
1098}
1099
Alexey Bataevb8329262015-02-27 06:33:30 +00001100static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst,
1101 const Expr *X, const Expr *E,
1102 SourceLocation Loc) {
1103 // x = expr;
1104 assert(X->isLValue() && "X of 'omp atomic write' is not lvalue");
1105 LValue XLValue = CGF.EmitLValue(X);
1106 RValue ExprRValue = CGF.EmitAnyExpr(E);
1107 if (XLValue.isGlobalReg())
1108 CGF.EmitStoreThroughGlobalRegLValue(ExprRValue, XLValue);
1109 else
1110 CGF.EmitAtomicStore(ExprRValue, XLValue,
1111 IsSeqCst ? llvm::SequentiallyConsistent
1112 : llvm::Monotonic,
1113 XLValue.isVolatile(), /*IsInit=*/false);
1114 // OpenMP, 2.12.6, atomic Construct
1115 // Any atomic construct with a seq_cst clause forces the atomically
1116 // performed operation to include an implicit flush operation without a
1117 // list.
1118 if (IsSeqCst)
1119 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
1120}
1121
Alexey Bataevb57056f2015-01-22 06:17:56 +00001122static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind,
1123 bool IsSeqCst, const Expr *X, const Expr *V,
Alexey Bataevb8329262015-02-27 06:33:30 +00001124 const Expr *E, SourceLocation Loc) {
Alexey Bataevb57056f2015-01-22 06:17:56 +00001125 switch (Kind) {
1126 case OMPC_read:
1127 EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc);
1128 break;
1129 case OMPC_write:
Alexey Bataevb8329262015-02-27 06:33:30 +00001130 EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc);
1131 break;
Alexey Bataevb57056f2015-01-22 06:17:56 +00001132 case OMPC_update:
1133 case OMPC_capture:
1134 llvm_unreachable("CodeGen for 'omp atomic clause' is not supported yet.");
1135 case OMPC_if:
1136 case OMPC_final:
1137 case OMPC_num_threads:
1138 case OMPC_private:
1139 case OMPC_firstprivate:
1140 case OMPC_lastprivate:
1141 case OMPC_reduction:
1142 case OMPC_safelen:
1143 case OMPC_collapse:
1144 case OMPC_default:
1145 case OMPC_seq_cst:
1146 case OMPC_shared:
1147 case OMPC_linear:
1148 case OMPC_aligned:
1149 case OMPC_copyin:
1150 case OMPC_copyprivate:
1151 case OMPC_flush:
1152 case OMPC_proc_bind:
1153 case OMPC_schedule:
1154 case OMPC_ordered:
1155 case OMPC_nowait:
1156 case OMPC_untied:
1157 case OMPC_threadprivate:
1158 case OMPC_mergeable:
1159 case OMPC_unknown:
1160 llvm_unreachable("Clause is not allowed in 'omp atomic'.");
1161 }
1162}
1163
1164void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) {
1165 bool IsSeqCst = S.getSingleClause(/*K=*/OMPC_seq_cst);
1166 OpenMPClauseKind Kind = OMPC_unknown;
1167 for (auto *C : S.clauses()) {
1168 // Find first clause (skip seq_cst clause, if it is first).
1169 if (C->getClauseKind() != OMPC_seq_cst) {
1170 Kind = C->getClauseKind();
1171 break;
1172 }
1173 }
Alexey Bataev10fec572015-03-11 04:48:56 +00001174
1175 const auto *CS =
1176 S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true);
1177 if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS))
1178 enterFullExpression(EWC);
Alexey Bataev36bf0112015-03-10 05:15:26 +00001179 InlinedOpenMPRegionScopeRAII Region(*this, S);
Alexey Bataev10fec572015-03-11 04:48:56 +00001180
Alexey Bataevb57056f2015-01-22 06:17:56 +00001181 EmitOMPAtomicExpr(*this, Kind, IsSeqCst, S.getX(), S.getV(), S.getExpr(),
1182 S.getLocStart());
Alexey Bataev0162e452014-07-22 10:10:35 +00001183}
1184
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001185void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &) {
1186 llvm_unreachable("CodeGen for 'omp target' is not supported yet.");
1187}
1188
Alexey Bataev13314bf2014-10-09 04:18:56 +00001189void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &) {
1190 llvm_unreachable("CodeGen for 'omp teams' is not supported yet.");
1191}
1192