r235838 - [OPENMP] Simplified iteration over clauses, NFC.
Alexey Bataev
a.bataev at hotmail.com
Mon Apr 27 01:00:32 PDT 2015
Author: abataev
Date: Mon Apr 27 03:00:32 2015
New Revision: 235838
URL: http://llvm.org/viewvc/llvm-project?rev=235838&view=rev
Log:
[OPENMP] Simplified iteration over clauses, NFC.
Modified:
cfe/trunk/include/clang/AST/StmtOpenMP.h
cfe/trunk/lib/AST/Stmt.cpp
cfe/trunk/lib/CodeGen/CGStmtOpenMP.cpp
cfe/trunk/lib/Sema/SemaOpenMP.cpp
Modified: cfe/trunk/include/clang/AST/StmtOpenMP.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/StmtOpenMP.h?rev=235838&r1=235837&r2=235838&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/StmtOpenMP.h (original)
+++ cfe/trunk/include/clang/AST/StmtOpenMP.h Mon Apr 27 03:00:32 2015
@@ -108,7 +108,7 @@ public:
typedef const OMPClause *value_type;
filtered_clause_iterator() : Current(), End() {}
filtered_clause_iterator(ArrayRef<OMPClause *> Arr, FilterPredicate Pred)
- : Current(Arr.begin()), End(Arr.end()), Pred(Pred) {
+ : Current(Arr.begin()), End(Arr.end()), Pred(std::move(Pred)) {
SkipToNextClause();
}
value_type operator*() const { return *Current; }
@@ -126,29 +126,24 @@ public:
}
bool operator!() { return Current == End; }
- operator bool() { return Current != End; }
+ explicit operator bool() { return Current != End; }
bool empty() const { return Current == End; }
};
- /// \brief A filter to iterate over 'linear' clauses using a C++ range
- /// for loop.
- struct linear_filter : public filtered_clause_iterator<
- std::function<bool(const OMPClause *)> > {
- linear_filter(ArrayRef<OMPClause *> Arr)
- : filtered_clause_iterator(Arr, [](const OMPClause *C)->bool {
- return C->getClauseKind() == OMPC_linear;
- }) {}
- const OMPLinearClause *operator*() const {
- return cast<OMPLinearClause>(*Current);
- }
- const OMPLinearClause *operator->() const {
- return cast<OMPLinearClause>(*Current);
- }
- friend linear_filter begin(const linear_filter &range) { return range; }
- friend linear_filter end(const linear_filter &range) {
- return linear_filter(ArrayRef<OMPClause *>(range.End, range.End));
+ template <typename Fn>
+ filtered_clause_iterator<Fn> getFilteredClauses(Fn &&fn) const {
+ return filtered_clause_iterator<Fn>(clauses(), std::move(fn));
+ }
+ struct ClauseKindFilter {
+ OpenMPClauseKind Kind;
+ bool operator()(const OMPClause *clause) const {
+ return clause->getClauseKind() == Kind;
}
};
+ filtered_clause_iterator<ClauseKindFilter>
+ getClausesOfKind(OpenMPClauseKind Kind) const {
+ return getFilteredClauses(ClauseKindFilter{Kind});
+ }
/// \brief Gets a single clause of the specified kind \a K associated with the
/// current directive iff there is only one clause of this kind (and assertion
Modified: cfe/trunk/lib/AST/Stmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Stmt.cpp?rev=235838&r1=235837&r2=235838&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Stmt.cpp (original)
+++ cfe/trunk/lib/AST/Stmt.cpp Mon Apr 27 03:00:32 2015
@@ -1581,10 +1581,7 @@ OMPFlushClause *OMPFlushClause::CreateEm
const OMPClause *
OMPExecutableDirective::getSingleClause(OpenMPClauseKind K) const {
- auto ClauseFilter =
- [=](const OMPClause *C) -> bool { return C->getClauseKind() == K; };
- OMPExecutableDirective::filtered_clause_iterator<decltype(ClauseFilter)> I(
- clauses(), ClauseFilter);
+ auto &&I = getClausesOfKind(K);
if (I) {
auto *Clause = *I;
Modified: cfe/trunk/lib/CodeGen/CGStmtOpenMP.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGStmtOpenMP.cpp?rev=235838&r1=235837&r2=235838&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGStmtOpenMP.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGStmtOpenMP.cpp Mon Apr 27 03:00:32 2015
@@ -114,13 +114,8 @@ void CodeGenFunction::EmitOMPCopy(CodeGe
bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
OMPPrivateScope &PrivateScope) {
- auto FirstprivateFilter = [](const OMPClause *C) -> bool {
- return C->getClauseKind() == OMPC_firstprivate;
- };
llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate;
- for (OMPExecutableDirective::filtered_clause_iterator<decltype(
- FirstprivateFilter)> I(D.clauses(), FirstprivateFilter);
- I; ++I) {
+ for (auto &&I = D.getClausesOfKind(OMPC_firstprivate); I; ++I) {
auto *C = cast<OMPFirstprivateClause>(*I);
auto IRef = C->varlist_begin();
auto InitsRef = C->inits().begin();
@@ -193,13 +188,8 @@ bool CodeGenFunction::EmitOMPFirstprivat
void CodeGenFunction::EmitOMPPrivateClause(
const OMPExecutableDirective &D,
CodeGenFunction::OMPPrivateScope &PrivateScope) {
- auto PrivateFilter = [](const OMPClause *C) -> bool {
- return C->getClauseKind() == OMPC_private;
- };
llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
- for (OMPExecutableDirective::filtered_clause_iterator<decltype(PrivateFilter)>
- I(D.clauses(), PrivateFilter);
- I; ++I) {
+ for (auto &&I = D.getClausesOfKind(OMPC_private); I; ++I) {
auto *C = cast<OMPPrivateClause>(*I);
auto IRef = C->varlist_begin();
for (auto IInit : C->private_copies()) {
@@ -226,14 +216,9 @@ bool CodeGenFunction::EmitOMPCopyinClaus
// operator=(threadprivate_var2, master_threadprivate_var2);
// ...
// __kmpc_barrier(&loc, global_tid);
- auto CopyinFilter = [](const OMPClause *C) -> bool {
- return C->getClauseKind() == OMPC_copyin;
- };
llvm::DenseSet<const VarDecl *> CopiedVars;
llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr;
- for (OMPExecutableDirective::filtered_clause_iterator<decltype(CopyinFilter)>
- I(D.clauses(), CopyinFilter);
- I; ++I) {
+ for (auto &&I = D.getClausesOfKind(OMPC_copyin); I; ++I) {
auto *C = cast<OMPCopyinClause>(*I);
auto IRef = C->varlist_begin();
auto ISrcRef = C->source_exprs().begin();
@@ -279,14 +264,9 @@ bool CodeGenFunction::EmitOMPCopyinClaus
bool CodeGenFunction::EmitOMPLastprivateClauseInit(
const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) {
- auto LastprivateFilter = [](const OMPClause *C) -> bool {
- return C->getClauseKind() == OMPC_lastprivate;
- };
bool HasAtLeastOneLastprivate = false;
llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
- for (OMPExecutableDirective::filtered_clause_iterator<decltype(
- LastprivateFilter)> I(D.clauses(), LastprivateFilter);
- I; ++I) {
+ for (auto &&I = D.getClausesOfKind(OMPC_lastprivate); I; ++I) {
auto *C = cast<OMPLastprivateClause>(*I);
auto IRef = C->varlist_begin();
auto IDestRef = C->destination_exprs().begin();
@@ -338,13 +318,8 @@ void CodeGenFunction::EmitOMPLastprivate
Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB);
EmitBlock(ThenBB);
{
- auto LastprivateFilter = [](const OMPClause *C) -> bool {
- return C->getClauseKind() == OMPC_lastprivate;
- };
llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
- for (OMPExecutableDirective::filtered_clause_iterator<decltype(
- LastprivateFilter)> I(D.clauses(), LastprivateFilter);
- I; ++I) {
+ for (auto &&I = D.getClausesOfKind(OMPC_lastprivate); I; ++I) {
auto *C = cast<OMPLastprivateClause>(*I);
auto IRef = C->varlist_begin();
auto ISrcRef = C->source_exprs().begin();
@@ -373,12 +348,7 @@ void CodeGenFunction::EmitOMPLastprivate
void CodeGenFunction::EmitOMPReductionClauseInit(
const OMPExecutableDirective &D,
CodeGenFunction::OMPPrivateScope &PrivateScope) {
- auto ReductionFilter = [](const OMPClause *C) -> bool {
- return C->getClauseKind() == OMPC_reduction;
- };
- for (OMPExecutableDirective::filtered_clause_iterator<decltype(
- ReductionFilter)> I(D.clauses(), ReductionFilter);
- I; ++I) {
+ for (auto &&I = D.getClausesOfKind(OMPC_reduction); I; ++I) {
auto *C = cast<OMPReductionClause>(*I);
auto ILHS = C->lhs_exprs().begin();
auto IRHS = C->rhs_exprs().begin();
@@ -414,13 +384,8 @@ void CodeGenFunction::EmitOMPReductionCl
llvm::SmallVector<const Expr *, 8> LHSExprs;
llvm::SmallVector<const Expr *, 8> RHSExprs;
llvm::SmallVector<const Expr *, 8> ReductionOps;
- auto ReductionFilter = [](const OMPClause *C) -> bool {
- return C->getClauseKind() == OMPC_reduction;
- };
bool HasAtLeastOneReduction = false;
- for (OMPExecutableDirective::filtered_clause_iterator<decltype(
- ReductionFilter)> I(D.clauses(), ReductionFilter);
- I; ++I) {
+ for (auto &&I = D.getClausesOfKind(OMPC_reduction); I; ++I) {
HasAtLeastOneReduction = true;
auto *C = cast<OMPReductionClause>(*I);
LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end());
@@ -495,7 +460,8 @@ void CodeGenFunction::EmitOMPLoopBody(co
EmitIgnoredExpr(I);
}
// Update the linear variables.
- for (auto C : OMPExecutableDirective::linear_filter(S.clauses())) {
+ for (auto &&I = S.getClausesOfKind(OMPC_linear); I; ++I) {
+ auto *C = cast<OMPLinearClause>(*I);
for (auto U : C->updates()) {
EmitIgnoredExpr(U);
}
@@ -573,7 +539,8 @@ void CodeGenFunction::EmitOMPSimdFinal(c
++IC;
}
// Emit the final values of the linear variables.
- for (auto C : OMPExecutableDirective::linear_filter(S.clauses())) {
+ for (auto &&I = S.getClausesOfKind(OMPC_linear); I; ++I) {
+ auto *C = cast<OMPLinearClause>(*I);
for (auto F : C->finals()) {
EmitIgnoredExpr(F);
}
@@ -658,8 +625,9 @@ static void emitPreCond(CodeGenFunction
static void
EmitPrivateLinearVars(CodeGenFunction &CGF, const OMPExecutableDirective &D,
CodeGenFunction::OMPPrivateScope &PrivateScope) {
- for (auto Clause : OMPExecutableDirective::linear_filter(D.clauses())) {
- for (auto *E : Clause->varlists()) {
+ for (auto &&I = D.getClausesOfKind(OMPC_linear); I; ++I) {
+ auto *C = cast<OMPLinearClause>(*I);
+ for (auto *E : C->varlists()) {
auto VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
bool IsRegistered = PrivateScope.addPrivate(VD, [&]()->llvm::Value * {
// Emit var without initialization.
@@ -739,7 +707,8 @@ void CodeGenFunction::EmitOMPSimdDirecti
}
// Emit inits for the linear variables.
- for (auto C : OMPExecutableDirective::linear_filter(S.clauses())) {
+ for (auto &&I = S.getClausesOfKind(OMPC_linear); I; ++I) {
+ auto *C = cast<OMPLinearClause>(*I);
for (auto Init : C->inits()) {
auto *D = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl());
CGF.EmitVarDecl(*D);
@@ -764,7 +733,8 @@ void CodeGenFunction::EmitOMPSimdDirecti
// Emit the linear steps for the linear clauses.
// If a step is not constant, it is pre-calculated before the loop.
- for (auto C : OMPExecutableDirective::linear_filter(S.clauses())) {
+ for (auto &&I = S.getClausesOfKind(OMPC_linear); I; ++I) {
+ auto *C = cast<OMPLinearClause>(*I);
if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep()))
if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) {
CGF.EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl()));
@@ -1221,21 +1191,11 @@ static OpenMPDirectiveKind emitSections(
bool HasFirstprivates;
// No need to generate reductions for sections with single section region, we
// can use original shared variables for all operations.
- auto ReductionFilter = [](const OMPClause *C) -> bool {
- return C->getClauseKind() == OMPC_reduction;
- };
- OMPExecutableDirective::filtered_clause_iterator<decltype(ReductionFilter)> I(
- S.clauses(), ReductionFilter);
- bool HasReductions = I;
+ bool HasReductions = !S.getClausesOfKind(OMPC_reduction).empty();
// No need to generate lastprivates for sections with single section region,
// we can use original shared variable for all calculations with barrier at
// the end of the sections.
- auto LastprivateFilter = [](const OMPClause *C) -> bool {
- return C->getClauseKind() == OMPC_lastprivate;
- };
- OMPExecutableDirective::filtered_clause_iterator<decltype(LastprivateFilter)>
- I1(S.clauses(), LastprivateFilter);
- bool HasLastprivates = I1;
+ bool HasLastprivates = !S.getClausesOfKind(OMPC_lastprivate).empty();
auto &&CodeGen = [Stmt, &S, &HasFirstprivates](CodeGenFunction &CGF) {
CodeGenFunction::OMPPrivateScope SingleScope(CGF);
HasFirstprivates = CGF.EmitOMPFirstprivateClause(S, SingleScope);
@@ -1287,14 +1247,9 @@ void CodeGenFunction::EmitOMPSingleDirec
// Check if there are any 'copyprivate' clauses associated with this
// 'single'
// construct.
- auto CopyprivateFilter = [](const OMPClause *C) -> bool {
- return C->getClauseKind() == OMPC_copyprivate;
- };
// Build a list of copyprivate variables along with helper expressions
// (<source>, <destination>, <destination>=<source> expressions)
- typedef OMPExecutableDirective::filtered_clause_iterator<decltype(
- CopyprivateFilter)> CopyprivateIter;
- for (CopyprivateIter I(S.clauses(), CopyprivateFilter); I; ++I) {
+ for (auto &&I = S.getClausesOfKind(OMPC_copyprivate); I; ++I) {
auto *C = cast<OMPCopyprivateClause>(*I);
CopyprivateVars.append(C->varlists().begin(), C->varlists().end());
DestExprs.append(C->destination_exprs().begin(),
Modified: cfe/trunk/lib/Sema/SemaOpenMP.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaOpenMP.cpp?rev=235838&r1=235837&r2=235838&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaOpenMP.cpp (original)
+++ cfe/trunk/lib/Sema/SemaOpenMP.cpp Mon Apr 27 03:00:32 2015
@@ -2997,11 +2997,11 @@ CheckOpenMPLoop(OpenMPDirectiveKind DKin
}
static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
- auto CollapseFilter = [](const OMPClause *C) -> bool {
+ auto &&CollapseFilter = [](const OMPClause *C) -> bool {
return C->getClauseKind() == OMPC_collapse;
};
OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
- Clauses, CollapseFilter);
+ Clauses, std::move(CollapseFilter));
if (I)
return cast<OMPCollapseClause>(*I)->getNumForLoops();
return nullptr;
More information about the cfe-commits
mailing list