2014-05-06 18:08:46 +08:00
|
|
|
//===--- CGStmtOpenMP.cpp - Emit LLVM Code from Statements ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit OpenMP nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CGOpenMPRuntime.h"
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
|
|
|
#include "clang/AST/Stmt.h"
|
|
|
|
#include "clang/AST/StmtOpenMP.h"
|
2014-09-30 13:29:28 +08:00
|
|
|
#include "TargetInfo.h"
|
2014-05-06 18:08:46 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// OpenMP Directive Emission
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[OPENMP] Codegen for 'if' clause in 'parallel' directive.
Adds codegen for 'if' clause. Currently only for 'if' clause used with the 'parallel' directive.
If condition evaluates to true, the code executes parallel version of the code by calling __kmpc_fork_call(loc, 1, microtask, captured_struct/*context*/), where loc - debug location, 1 - number of additional parameters after "microtask" argument, microtask - is outlined finction for the code associated with the 'parallel' directive, captured_struct - list of variables captured in this outlined function.
If condition evaluates to false, the code executes serial version of the code by executing the following code:
global_thread_id.addr = alloca i32
store i32 global_thread_id, global_thread_id.addr
zero.addr = alloca i32
store i32 0, zero.addr
kmpc_serialized_parallel(loc, global_thread_id);
microtask(global_thread_id.addr, zero.addr, captured_struct/*context*/);
kmpc_end_serialized_parallel(loc, global_thread_id);
Where loc - debug location, global_thread_id - global thread id, returned by __kmpc_global_thread_num() call or passed as a first parameter in microtask() call, global_thread_id.addr - address of the variable, where stored global_thread_id value, zero.addr - implicit bound thread id (should be set to 0 for serial call), microtask() and captured_struct are the same as in parallel call.
Also this patch checks if the condition is constant and if it is constant it evaluates its value and then generates either parallel version of the code (if the condition evaluates to true), or the serial version of the code (if the condition evaluates to false).
Differential Revision: http://reviews.llvm.org/D4716
llvm-svn: 219597
2014-10-13 14:02:40 +08:00
|
|
|
/// \brief Emits code for OpenMP 'if' clause using specified \a CodeGen
|
|
|
|
/// function. Here is the logic:
|
|
|
|
/// if (Cond) {
|
|
|
|
/// CodeGen(true);
|
|
|
|
/// } else {
|
|
|
|
/// CodeGen(false);
|
|
|
|
/// }
|
|
|
|
static void EmitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond,
|
|
|
|
const std::function<void(bool)> &CodeGen) {
|
|
|
|
CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange());
|
|
|
|
|
|
|
|
// If the condition constant folds and can be elided, try to avoid emitting
|
|
|
|
// the condition and the dead arm of the if/else.
|
|
|
|
bool CondConstant;
|
|
|
|
if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) {
|
|
|
|
CodeGen(CondConstant);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, the condition did not fold, or we couldn't elide it. Just
|
|
|
|
// emit the conditional branch.
|
|
|
|
auto ThenBlock = CGF.createBasicBlock(/*name*/ "omp_if.then");
|
|
|
|
auto ElseBlock = CGF.createBasicBlock(/*name*/ "omp_if.else");
|
|
|
|
auto ContBlock = CGF.createBasicBlock(/*name*/ "omp_if.end");
|
|
|
|
CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount*/ 0);
|
|
|
|
|
|
|
|
// Emit the 'then' code.
|
|
|
|
CGF.EmitBlock(ThenBlock);
|
|
|
|
CodeGen(/*ThenBlock*/ true);
|
|
|
|
CGF.EmitBranch(ContBlock);
|
|
|
|
// Emit the 'else' code if present.
|
|
|
|
{
|
|
|
|
// There is no need to emit line number for unconditional branch.
|
|
|
|
SuppressDebugLocation SDL(CGF.Builder);
|
|
|
|
CGF.EmitBlock(ElseBlock);
|
|
|
|
}
|
|
|
|
CodeGen(/*ThenBlock*/ false);
|
|
|
|
{
|
|
|
|
// There is no need to emit line number for unconditional branch.
|
|
|
|
SuppressDebugLocation SDL(CGF.Builder);
|
|
|
|
CGF.EmitBranch(ContBlock);
|
|
|
|
}
|
|
|
|
// Emit the continuation block for code after the if.
|
|
|
|
CGF.EmitBlock(ContBlock, /*IsFinished*/ true);
|
|
|
|
}
|
|
|
|
|
2014-10-08 22:01:46 +08:00
|
|
|
void CodeGenFunction::EmitOMPAggregateAssign(LValue OriginalAddr,
|
|
|
|
llvm::Value *PrivateAddr,
|
|
|
|
const Expr *AssignExpr,
|
|
|
|
QualType OriginalType,
|
|
|
|
const VarDecl *VDInit) {
|
|
|
|
EmitBlock(createBasicBlock(".omp.assign.begin."));
|
|
|
|
if (!isa<CXXConstructExpr>(AssignExpr) || isTrivialInitializer(AssignExpr)) {
|
|
|
|
// Perform simple memcpy.
|
|
|
|
EmitAggregateAssign(PrivateAddr, OriginalAddr.getAddress(),
|
|
|
|
AssignExpr->getType());
|
|
|
|
} else {
|
|
|
|
// Perform element-by-element initialization.
|
|
|
|
QualType ElementTy;
|
|
|
|
auto SrcBegin = OriginalAddr.getAddress();
|
|
|
|
auto DestBegin = PrivateAddr;
|
|
|
|
auto ArrayTy = OriginalType->getAsArrayTypeUnsafe();
|
|
|
|
auto SrcNumElements = emitArrayLength(ArrayTy, ElementTy, SrcBegin);
|
|
|
|
auto DestNumElements = emitArrayLength(ArrayTy, ElementTy, DestBegin);
|
|
|
|
auto SrcEnd = Builder.CreateGEP(SrcBegin, SrcNumElements);
|
|
|
|
auto DestEnd = Builder.CreateGEP(DestBegin, DestNumElements);
|
|
|
|
// The basic structure here is a do-while loop, because we don't
|
|
|
|
// need to check for the zero-element case.
|
|
|
|
auto BodyBB = createBasicBlock("omp.arraycpy.body");
|
|
|
|
auto DoneBB = createBasicBlock("omp.arraycpy.done");
|
|
|
|
auto IsEmpty =
|
|
|
|
Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty");
|
|
|
|
Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
|
|
|
|
|
|
|
|
// Enter the loop body, making that address the current address.
|
|
|
|
auto EntryBB = Builder.GetInsertBlock();
|
|
|
|
EmitBlock(BodyBB);
|
|
|
|
auto SrcElementPast = Builder.CreatePHI(SrcBegin->getType(), 2,
|
|
|
|
"omp.arraycpy.srcElementPast");
|
|
|
|
SrcElementPast->addIncoming(SrcEnd, EntryBB);
|
|
|
|
auto DestElementPast = Builder.CreatePHI(DestBegin->getType(), 2,
|
|
|
|
"omp.arraycpy.destElementPast");
|
|
|
|
DestElementPast->addIncoming(DestEnd, EntryBB);
|
|
|
|
|
|
|
|
// Shift the address back by one element.
|
|
|
|
auto NegativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
|
|
|
|
auto DestElement = Builder.CreateGEP(DestElementPast, NegativeOne,
|
|
|
|
"omp.arraycpy.dest.element");
|
|
|
|
auto SrcElement = Builder.CreateGEP(SrcElementPast, NegativeOne,
|
|
|
|
"omp.arraycpy.src.element");
|
|
|
|
{
|
|
|
|
// Create RunCleanScope to cleanup possible temps.
|
|
|
|
CodeGenFunction::RunCleanupsScope Init(*this);
|
|
|
|
// Emit initialization for single element.
|
|
|
|
LocalDeclMap[VDInit] = SrcElement;
|
|
|
|
EmitAnyExprToMem(AssignExpr, DestElement,
|
|
|
|
AssignExpr->getType().getQualifiers(),
|
|
|
|
/*IsInitializer*/ false);
|
|
|
|
LocalDeclMap.erase(VDInit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether we've reached the end.
|
|
|
|
auto Done =
|
|
|
|
Builder.CreateICmpEQ(DestElement, DestBegin, "omp.arraycpy.done");
|
|
|
|
Builder.CreateCondBr(Done, DoneBB, BodyBB);
|
|
|
|
DestElementPast->addIncoming(DestElement, Builder.GetInsertBlock());
|
|
|
|
SrcElementPast->addIncoming(SrcElement, Builder.GetInsertBlock());
|
|
|
|
|
|
|
|
// Done.
|
|
|
|
EmitBlock(DoneBB, true);
|
|
|
|
}
|
|
|
|
EmitBlock(createBasicBlock(".omp.assign.end."));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitOMPFirstprivateClause(
|
|
|
|
const OMPExecutableDirective &D,
|
2014-10-10 17:48:26 +08:00
|
|
|
CodeGenFunction::OMPPrivateScope &PrivateScope) {
|
2014-10-08 22:01:46 +08:00
|
|
|
auto PrivateFilter = [](const OMPClause *C) -> bool {
|
|
|
|
return C->getClauseKind() == OMPC_firstprivate;
|
|
|
|
};
|
|
|
|
for (OMPExecutableDirective::filtered_clause_iterator<decltype(PrivateFilter)>
|
|
|
|
I(D.clauses(), PrivateFilter); I; ++I) {
|
|
|
|
auto *C = cast<OMPFirstprivateClause>(*I);
|
|
|
|
auto IRef = C->varlist_begin();
|
|
|
|
auto InitsRef = C->inits().begin();
|
|
|
|
for (auto IInit : C->private_copies()) {
|
2014-10-10 17:48:26 +08:00
|
|
|
auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
|
|
|
|
auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
|
|
|
|
bool IsRegistered;
|
2014-10-08 22:01:46 +08:00
|
|
|
if (*InitsRef != nullptr) {
|
|
|
|
// Emit VarDecl with copy init for arrays.
|
2014-10-10 17:48:26 +08:00
|
|
|
auto *FD = CapturedStmtInfo->lookup(OrigVD);
|
2014-10-08 22:01:46 +08:00
|
|
|
LValue Base = MakeNaturalAlignAddrLValue(
|
|
|
|
CapturedStmtInfo->getContextValue(),
|
|
|
|
getContext().getTagDeclType(FD->getParent()));
|
|
|
|
auto OriginalAddr = EmitLValueForField(Base, FD);
|
|
|
|
auto VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl());
|
2014-10-10 17:48:26 +08:00
|
|
|
IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value * {
|
|
|
|
auto Emission = EmitAutoVarAlloca(*VD);
|
|
|
|
// Emit initialization of aggregate firstprivate vars.
|
|
|
|
EmitOMPAggregateAssign(OriginalAddr, Emission.getAllocatedAddress(),
|
|
|
|
VD->getInit(), (*IRef)->getType(), VDInit);
|
|
|
|
EmitAutoVarCleanups(Emission);
|
|
|
|
return Emission.getAllocatedAddress();
|
|
|
|
});
|
2014-10-08 22:01:46 +08:00
|
|
|
} else
|
2014-10-10 17:48:26 +08:00
|
|
|
IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value * {
|
|
|
|
// Emit private VarDecl with copy init.
|
|
|
|
EmitDecl(*VD);
|
|
|
|
return GetAddrOfLocalVar(VD);
|
|
|
|
});
|
|
|
|
assert(IsRegistered && "counter already registered as private");
|
|
|
|
// Silence the warning about unused variable.
|
|
|
|
(void)IsRegistered;
|
2014-10-08 22:01:46 +08:00
|
|
|
++IRef, ++InitsRef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 11:16:40 +08:00
|
|
|
void CodeGenFunction::EmitOMPPrivateClause(
|
|
|
|
const OMPExecutableDirective &D,
|
|
|
|
CodeGenFunction::OMPPrivateScope &PrivateScope) {
|
|
|
|
auto PrivateFilter = [](const OMPClause *C) -> bool {
|
|
|
|
return C->getClauseKind() == OMPC_private;
|
|
|
|
};
|
|
|
|
for (OMPExecutableDirective::filtered_clause_iterator<decltype(PrivateFilter)>
|
|
|
|
I(D.clauses(), PrivateFilter); I; ++I) {
|
|
|
|
auto *C = cast<OMPPrivateClause>(*I);
|
|
|
|
auto IRef = C->varlist_begin();
|
|
|
|
for (auto IInit : C->private_copies()) {
|
|
|
|
auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
|
|
|
|
auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
|
|
|
|
bool IsRegistered =
|
|
|
|
PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value * {
|
|
|
|
// Emit private VarDecl with copy init.
|
|
|
|
EmitDecl(*VD);
|
|
|
|
return GetAddrOfLocalVar(VD);
|
|
|
|
});
|
|
|
|
assert(IsRegistered && "counter already registered as private");
|
|
|
|
// Silence the warning about unused variable.
|
|
|
|
(void)IsRegistered;
|
|
|
|
++IRef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-13 16:23:51 +08:00
|
|
|
/// \brief Emits code for OpenMP parallel directive in the parallel region.
|
|
|
|
static void EmitOMPParallelCall(CodeGenFunction &CGF,
|
|
|
|
const OMPParallelDirective &S,
|
|
|
|
llvm::Value *OutlinedFn,
|
|
|
|
llvm::Value *CapturedStruct) {
|
|
|
|
if (auto C = S.getSingleClause(/*K*/ OMPC_num_threads)) {
|
|
|
|
CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
|
|
|
|
auto NumThreadsClause = cast<OMPNumThreadsClause>(C);
|
|
|
|
auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
|
|
|
|
/*IgnoreResultAssign*/ true);
|
|
|
|
CGF.CGM.getOpenMPRuntime().EmitOMPNumThreadsClause(
|
|
|
|
CGF, NumThreads, NumThreadsClause->getLocStart());
|
|
|
|
}
|
|
|
|
CGF.CGM.getOpenMPRuntime().EmitOMPParallelCall(CGF, S.getLocStart(),
|
|
|
|
OutlinedFn, CapturedStruct);
|
|
|
|
}
|
|
|
|
|
2014-05-06 18:08:46 +08:00
|
|
|
void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
|
2014-10-10 20:19:54 +08:00
|
|
|
auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
|
|
|
|
auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
|
|
|
|
auto OutlinedFn = CGM.getOpenMPRuntime().EmitOpenMPOutlinedFunction(
|
|
|
|
S, *CS->getCapturedDecl()->param_begin());
|
[OPENMP] Codegen for 'if' clause in 'parallel' directive.
Adds codegen for 'if' clause. Currently only for 'if' clause used with the 'parallel' directive.
If condition evaluates to true, the code executes parallel version of the code by calling __kmpc_fork_call(loc, 1, microtask, captured_struct/*context*/), where loc - debug location, 1 - number of additional parameters after "microtask" argument, microtask - is outlined finction for the code associated with the 'parallel' directive, captured_struct - list of variables captured in this outlined function.
If condition evaluates to false, the code executes serial version of the code by executing the following code:
global_thread_id.addr = alloca i32
store i32 global_thread_id, global_thread_id.addr
zero.addr = alloca i32
store i32 0, zero.addr
kmpc_serialized_parallel(loc, global_thread_id);
microtask(global_thread_id.addr, zero.addr, captured_struct/*context*/);
kmpc_end_serialized_parallel(loc, global_thread_id);
Where loc - debug location, global_thread_id - global thread id, returned by __kmpc_global_thread_num() call or passed as a first parameter in microtask() call, global_thread_id.addr - address of the variable, where stored global_thread_id value, zero.addr - implicit bound thread id (should be set to 0 for serial call), microtask() and captured_struct are the same as in parallel call.
Also this patch checks if the condition is constant and if it is constant it evaluates its value and then generates either parallel version of the code (if the condition evaluates to true), or the serial version of the code (if the condition evaluates to false).
Differential Revision: http://reviews.llvm.org/D4716
llvm-svn: 219597
2014-10-13 14:02:40 +08:00
|
|
|
if (auto C = S.getSingleClause(/*K*/ OMPC_if)) {
|
|
|
|
auto Cond = cast<OMPIfClause>(C)->getCondition();
|
|
|
|
EmitOMPIfClause(*this, Cond, [&](bool ThenBlock) {
|
|
|
|
if (ThenBlock)
|
2014-10-13 16:23:51 +08:00
|
|
|
EmitOMPParallelCall(*this, S, OutlinedFn, CapturedStruct);
|
[OPENMP] Codegen for 'if' clause in 'parallel' directive.
Adds codegen for 'if' clause. Currently only for 'if' clause used with the 'parallel' directive.
If condition evaluates to true, the code executes parallel version of the code by calling __kmpc_fork_call(loc, 1, microtask, captured_struct/*context*/), where loc - debug location, 1 - number of additional parameters after "microtask" argument, microtask - is outlined finction for the code associated with the 'parallel' directive, captured_struct - list of variables captured in this outlined function.
If condition evaluates to false, the code executes serial version of the code by executing the following code:
global_thread_id.addr = alloca i32
store i32 global_thread_id, global_thread_id.addr
zero.addr = alloca i32
store i32 0, zero.addr
kmpc_serialized_parallel(loc, global_thread_id);
microtask(global_thread_id.addr, zero.addr, captured_struct/*context*/);
kmpc_end_serialized_parallel(loc, global_thread_id);
Where loc - debug location, global_thread_id - global thread id, returned by __kmpc_global_thread_num() call or passed as a first parameter in microtask() call, global_thread_id.addr - address of the variable, where stored global_thread_id value, zero.addr - implicit bound thread id (should be set to 0 for serial call), microtask() and captured_struct are the same as in parallel call.
Also this patch checks if the condition is constant and if it is constant it evaluates its value and then generates either parallel version of the code (if the condition evaluates to true), or the serial version of the code (if the condition evaluates to false).
Differential Revision: http://reviews.llvm.org/D4716
llvm-svn: 219597
2014-10-13 14:02:40 +08:00
|
|
|
else
|
|
|
|
CGM.getOpenMPRuntime().EmitOMPSerialCall(*this, S.getLocStart(),
|
|
|
|
OutlinedFn, CapturedStruct);
|
|
|
|
});
|
2014-10-13 16:23:51 +08:00
|
|
|
} else
|
|
|
|
EmitOMPParallelCall(*this, S, OutlinedFn, CapturedStruct);
|
2014-05-06 18:08:46 +08:00
|
|
|
}
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2014-10-07 16:57:09 +08:00
|
|
|
void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &S,
|
2014-10-01 14:03:56 +08:00
|
|
|
bool SeparateIter) {
|
|
|
|
RunCleanupsScope BodyScope(*this);
|
|
|
|
// Update counters values on current iteration.
|
|
|
|
for (auto I : S.updates()) {
|
|
|
|
EmitIgnoredExpr(I);
|
|
|
|
}
|
|
|
|
// On a continue in the body, jump to the end.
|
2014-10-07 16:57:09 +08:00
|
|
|
auto Continue = getJumpDestInCurrentScope("omp.body.continue");
|
2014-10-01 14:03:56 +08:00
|
|
|
BreakContinueStack.push_back(BreakContinue(JumpDest(), Continue));
|
|
|
|
// Emit loop body.
|
|
|
|
EmitStmt(S.getBody());
|
|
|
|
// The end (updates/cleanups).
|
|
|
|
EmitBlock(Continue.getBlock());
|
|
|
|
BreakContinueStack.pop_back();
|
|
|
|
if (SeparateIter) {
|
|
|
|
// TODO: Update lastprivates if the SeparateIter flag is true.
|
|
|
|
// This will be implemented in a follow-up OMPLastprivateClause patch, but
|
|
|
|
// result should be still correct without it, as we do not make these
|
|
|
|
// variables private yet.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-07 16:57:09 +08:00
|
|
|
void CodeGenFunction::EmitOMPInnerLoop(const OMPLoopDirective &S,
|
|
|
|
OMPPrivateScope &LoopScope,
|
|
|
|
bool SeparateIter) {
|
|
|
|
auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end");
|
2014-10-01 14:03:56 +08:00
|
|
|
auto Cnt = getPGORegionCounter(&S);
|
|
|
|
|
|
|
|
// Start the loop with a block that tests the condition.
|
2014-10-07 16:57:09 +08:00
|
|
|
auto CondBlock = createBasicBlock("omp.inner.for.cond");
|
2014-10-01 14:03:56 +08:00
|
|
|
EmitBlock(CondBlock);
|
|
|
|
LoopStack.push(CondBlock);
|
|
|
|
|
|
|
|
// If there are any cleanups between here and the loop-exit scope,
|
|
|
|
// create a block to stage a loop exit along.
|
|
|
|
auto ExitBlock = LoopExit.getBlock();
|
|
|
|
if (LoopScope.requiresCleanups())
|
2014-10-07 16:57:09 +08:00
|
|
|
ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup");
|
2014-10-01 14:03:56 +08:00
|
|
|
|
2014-10-07 16:57:09 +08:00
|
|
|
auto LoopBody = createBasicBlock("omp.inner.for.body");
|
2014-10-01 14:03:56 +08:00
|
|
|
|
|
|
|
// Emit condition: "IV < LastIteration + 1 [ - 1]"
|
|
|
|
// ("- 1" when lastprivate clause is present - separate one iteration).
|
|
|
|
llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond(SeparateIter));
|
|
|
|
Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock,
|
|
|
|
PGO.createLoopWeights(S.getCond(SeparateIter), Cnt));
|
|
|
|
|
|
|
|
if (ExitBlock != LoopExit.getBlock()) {
|
|
|
|
EmitBlock(ExitBlock);
|
|
|
|
EmitBranchThroughCleanup(LoopExit);
|
|
|
|
}
|
|
|
|
|
|
|
|
EmitBlock(LoopBody);
|
|
|
|
Cnt.beginRegion(Builder);
|
|
|
|
|
|
|
|
// Create a block for the increment.
|
2014-10-07 16:57:09 +08:00
|
|
|
auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc");
|
2014-10-01 14:03:56 +08:00
|
|
|
BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
|
|
|
|
|
2014-10-07 16:57:09 +08:00
|
|
|
EmitOMPLoopBody(S);
|
2014-10-01 14:03:56 +08:00
|
|
|
EmitStopPoint(&S);
|
|
|
|
|
|
|
|
// Emit "IV = IV + 1" and a back-edge to the condition block.
|
|
|
|
EmitBlock(Continue.getBlock());
|
|
|
|
EmitIgnoredExpr(S.getInc());
|
|
|
|
BreakContinueStack.pop_back();
|
|
|
|
EmitBranch(CondBlock);
|
|
|
|
LoopStack.pop();
|
|
|
|
// Emit the fall-through block.
|
|
|
|
EmitBlock(LoopExit.getBlock());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitOMPSimdFinal(const OMPLoopDirective &S) {
|
|
|
|
auto IC = S.counters().begin();
|
|
|
|
for (auto F : S.finals()) {
|
|
|
|
if (LocalDeclMap.lookup(cast<DeclRefExpr>((*IC))->getDecl())) {
|
|
|
|
EmitIgnoredExpr(F);
|
|
|
|
}
|
|
|
|
++IC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 13:29:28 +08:00
|
|
|
static void EmitOMPAlignedClause(CodeGenFunction &CGF, CodeGenModule &CGM,
|
|
|
|
const OMPAlignedClause &Clause) {
|
|
|
|
unsigned ClauseAlignment = 0;
|
|
|
|
if (auto AlignmentExpr = Clause.getAlignment()) {
|
|
|
|
auto AlignmentCI =
|
|
|
|
cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr));
|
|
|
|
ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue());
|
|
|
|
}
|
|
|
|
for (auto E : Clause.varlists()) {
|
|
|
|
unsigned Alignment = ClauseAlignment;
|
|
|
|
if (Alignment == 0) {
|
|
|
|
// OpenMP [2.8.1, Description]
|
2014-10-10 17:48:26 +08:00
|
|
|
// If no optional parameter is specified, implementation-defined default
|
2014-09-30 13:29:28 +08:00
|
|
|
// alignments for SIMD instructions on the target platforms are assumed.
|
|
|
|
Alignment = CGM.getTargetCodeGenInfo().getOpenMPSimdDefaultAlignment(
|
|
|
|
E->getType());
|
|
|
|
}
|
|
|
|
assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) &&
|
|
|
|
"alignment is not power of 2");
|
|
|
|
if (Alignment != 0) {
|
|
|
|
llvm::Value *PtrValue = CGF.EmitScalarExpr(E);
|
|
|
|
CGF.EmitAlignmentAssumption(PtrValue, Alignment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 17:48:26 +08:00
|
|
|
static void EmitPrivateLoopCounters(CodeGenFunction &CGF,
|
|
|
|
CodeGenFunction::OMPPrivateScope &LoopScope,
|
|
|
|
ArrayRef<Expr *> Counters) {
|
|
|
|
for (auto *E : Counters) {
|
|
|
|
auto VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
|
|
|
|
bool IsRegistered = LoopScope.addPrivate(VD, [&]() -> llvm::Value * {
|
|
|
|
// Emit var without initialization.
|
|
|
|
auto VarEmission = CGF.EmitAutoVarAlloca(*VD);
|
|
|
|
CGF.EmitAutoVarCleanups(VarEmission);
|
|
|
|
return VarEmission.getAllocatedAddress();
|
|
|
|
});
|
|
|
|
assert(IsRegistered && "counter already registered as private");
|
|
|
|
// Silence the warning about unused variable.
|
|
|
|
(void)IsRegistered;
|
|
|
|
}
|
|
|
|
(void)LoopScope.Privatize();
|
|
|
|
}
|
|
|
|
|
2014-05-22 16:54:05 +08:00
|
|
|
void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) {
|
2014-10-01 14:03:56 +08:00
|
|
|
// Pragma 'simd' code depends on presence of 'lastprivate'.
|
|
|
|
// If present, we have to separate last iteration of the loop:
|
|
|
|
//
|
|
|
|
// if (LastIteration != 0) {
|
|
|
|
// for (IV in 0..LastIteration-1) BODY;
|
|
|
|
// BODY with updates of lastprivate vars;
|
|
|
|
// <Final counter/linear vars updates>;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// otherwise (when there's no lastprivate):
|
|
|
|
//
|
|
|
|
// for (IV in 0..LastIteration) BODY;
|
|
|
|
// <Final counter/linear vars updates>;
|
|
|
|
//
|
|
|
|
|
|
|
|
// Walk clauses and process safelen/lastprivate.
|
|
|
|
bool SeparateIter = false;
|
2014-05-22 16:54:05 +08:00
|
|
|
LoopStack.setParallel();
|
|
|
|
LoopStack.setVectorizerEnable(true);
|
|
|
|
for (auto C : S.clauses()) {
|
|
|
|
switch (C->getClauseKind()) {
|
|
|
|
case OMPC_safelen: {
|
|
|
|
RValue Len = EmitAnyExpr(cast<OMPSafelenClause>(C)->getSafelen(),
|
|
|
|
AggValueSlot::ignored(), true);
|
|
|
|
llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
|
|
|
|
LoopStack.setVectorizerWidth(Val->getZExtValue());
|
|
|
|
// In presence of finite 'safelen', it may be unsafe to mark all
|
|
|
|
// the memory instructions parallel, because loop-carried
|
|
|
|
// dependences of 'safelen' iterations are possible.
|
|
|
|
LoopStack.setParallel(false);
|
|
|
|
break;
|
|
|
|
}
|
2014-09-30 13:29:28 +08:00
|
|
|
case OMPC_aligned:
|
|
|
|
EmitOMPAlignedClause(*this, CGM, cast<OMPAlignedClause>(*C));
|
|
|
|
break;
|
2014-10-01 14:03:56 +08:00
|
|
|
case OMPC_lastprivate:
|
|
|
|
SeparateIter = true;
|
|
|
|
break;
|
2014-05-22 16:54:05 +08:00
|
|
|
default:
|
|
|
|
// Not handled yet
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
2014-10-01 14:03:56 +08:00
|
|
|
|
|
|
|
RunCleanupsScope DirectiveScope(*this);
|
|
|
|
|
|
|
|
CGDebugInfo *DI = getDebugInfo();
|
|
|
|
if (DI)
|
|
|
|
DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin());
|
|
|
|
|
|
|
|
// Emit the loop iteration variable.
|
|
|
|
const Expr *IVExpr = S.getIterationVariable();
|
|
|
|
const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
|
|
|
|
EmitVarDecl(*IVDecl);
|
|
|
|
EmitIgnoredExpr(S.getInit());
|
|
|
|
|
|
|
|
// Emit the iterations count variable.
|
|
|
|
// If it is not a variable, Sema decided to calculate iterations count on each
|
|
|
|
// iteration (e.g., it is foldable into a constant).
|
|
|
|
if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
|
|
|
|
EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
|
|
|
|
// Emit calculation of the iterations count.
|
|
|
|
EmitIgnoredExpr(S.getCalcLastIteration());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SeparateIter) {
|
|
|
|
// Emit: if (LastIteration > 0) - begin.
|
|
|
|
RegionCounter Cnt = getPGORegionCounter(&S);
|
|
|
|
auto ThenBlock = createBasicBlock("simd.if.then");
|
|
|
|
auto ContBlock = createBasicBlock("simd.if.end");
|
|
|
|
EmitBranchOnBoolExpr(S.getPreCond(), ThenBlock, ContBlock, Cnt.getCount());
|
|
|
|
EmitBlock(ThenBlock);
|
|
|
|
Cnt.beginRegion(Builder);
|
|
|
|
// Emit 'then' code.
|
|
|
|
{
|
|
|
|
OMPPrivateScope LoopScope(*this);
|
2014-10-10 17:48:26 +08:00
|
|
|
EmitPrivateLoopCounters(*this, LoopScope, S.counters());
|
2014-10-07 16:57:09 +08:00
|
|
|
EmitOMPInnerLoop(S, LoopScope, /* SeparateIter */ true);
|
|
|
|
EmitOMPLoopBody(S, /* SeparateIter */ true);
|
2014-10-01 14:03:56 +08:00
|
|
|
}
|
|
|
|
EmitOMPSimdFinal(S);
|
|
|
|
// Emit: if (LastIteration != 0) - end.
|
|
|
|
EmitBranch(ContBlock);
|
|
|
|
EmitBlock(ContBlock, true);
|
|
|
|
} else {
|
|
|
|
{
|
|
|
|
OMPPrivateScope LoopScope(*this);
|
2014-10-10 17:48:26 +08:00
|
|
|
EmitPrivateLoopCounters(*this, LoopScope, S.counters());
|
2014-10-07 16:57:09 +08:00
|
|
|
EmitOMPInnerLoop(S, LoopScope);
|
2014-10-01 14:03:56 +08:00
|
|
|
}
|
|
|
|
EmitOMPSimdFinal(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DI)
|
|
|
|
DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd());
|
2014-05-22 16:54:05 +08:00
|
|
|
}
|
|
|
|
|
2014-06-18 12:14:57 +08:00
|
|
|
void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &) {
|
2014-06-25 19:44:49 +08:00
|
|
|
llvm_unreachable("CodeGen for 'omp for' is not supported yet.");
|
2014-06-18 12:14:57 +08:00
|
|
|
}
|
2014-06-25 19:44:49 +08:00
|
|
|
|
2014-09-18 13:12:34 +08:00
|
|
|
void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp for simd' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-06-25 19:44:49 +08:00
|
|
|
void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp sections' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-06-26 16:21:58 +08:00
|
|
|
void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp section' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-06-26 20:05:45 +08:00
|
|
|
void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp single' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-12-04 15:23:53 +08:00
|
|
|
void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) {
|
|
|
|
CGM.getOpenMPRuntime().EmitOMPMasterRegion(
|
|
|
|
*this, [&]() -> void {
|
|
|
|
RunCleanupsScope Scope(*this);
|
|
|
|
EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
|
|
|
|
EnsureInsertPoint();
|
|
|
|
}, S.getLocStart());
|
2014-07-17 16:54:58 +08:00
|
|
|
}
|
|
|
|
|
2014-09-22 18:01:53 +08:00
|
|
|
void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) {
|
2014-12-01 19:32:38 +08:00
|
|
|
CGM.getOpenMPRuntime().EmitOMPCriticalRegion(
|
|
|
|
*this, S.getDirectiveName().getAsString(), [&]() -> void {
|
2014-09-22 18:01:53 +08:00
|
|
|
RunCleanupsScope Scope(*this);
|
|
|
|
EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
|
|
|
|
EnsureInsertPoint();
|
2014-12-01 19:32:38 +08:00
|
|
|
}, S.getLocStart());
|
2014-07-21 17:42:05 +08:00
|
|
|
}
|
|
|
|
|
2014-07-07 21:01:15 +08:00
|
|
|
void
|
|
|
|
CodeGenFunction::EmitOMPParallelForDirective(const OMPParallelForDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp parallel for' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-09-23 17:33:00 +08:00
|
|
|
void CodeGenFunction::EmitOMPParallelForSimdDirective(
|
|
|
|
const OMPParallelForSimdDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp parallel for simd' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-08 16:12:03 +08:00
|
|
|
void CodeGenFunction::EmitOMPParallelSectionsDirective(
|
|
|
|
const OMPParallelSectionsDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp parallel sections' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-11 19:25:16 +08:00
|
|
|
void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp task' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-18 15:47:19 +08:00
|
|
|
void CodeGenFunction::EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp taskyield' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-18 17:11:51 +08:00
|
|
|
void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp barrier' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-18 18:17:07 +08:00
|
|
|
void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp taskwait' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-11-20 12:34:54 +08:00
|
|
|
void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) {
|
|
|
|
CGM.getOpenMPRuntime().EmitOMPFlush(
|
|
|
|
*this, [&]() -> ArrayRef<const Expr *> {
|
|
|
|
if (auto C = S.getSingleClause(/*K*/ OMPC_flush)) {
|
|
|
|
auto FlushClause = cast<OMPFlushClause>(C);
|
|
|
|
return llvm::makeArrayRef(FlushClause->varlist_begin(),
|
|
|
|
FlushClause->varlist_end());
|
|
|
|
}
|
|
|
|
return llvm::None;
|
|
|
|
}(),
|
|
|
|
S.getLocStart());
|
2014-07-21 19:26:11 +08:00
|
|
|
}
|
|
|
|
|
2014-07-22 14:45:04 +08:00
|
|
|
void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp ordered' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-22 18:10:35 +08:00
|
|
|
void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp atomic' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-09-19 16:19:49 +08:00
|
|
|
void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp target' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-10-09 12:18:56 +08:00
|
|
|
void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp teams' is not supported yet.");
|
|
|
|
}
|
|
|
|
|