[OPENMP] 'if' clause support (no CodeGen support)

llvm-svn: 201297
This commit is contained in:
Alexey Bataev 2014-02-13 05:29:23 +00:00
parent 0e3b5e0b20
commit aadd52e5cc
17 changed files with 275 additions and 12 deletions

View File

@ -2352,6 +2352,12 @@ bool DataRecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
return true;
}
template<typename Derived>
bool DataRecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
TraverseStmt(C->getCondition());
return true;
}
template<typename Derived>
bool DataRecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *C) {
return true;

View File

@ -2376,6 +2376,12 @@ bool RecursiveASTVisitor<Derived>::TraverseOMPClause(OMPClause *C) {
return true;
}
template<typename Derived>
bool RecursiveASTVisitor<Derived>::VisitOMPIfClause(OMPIfClause *C) {
TraverseStmt(C->getCondition());
return true;
}
template<typename Derived>
bool RecursiveASTVisitor<Derived>::VisitOMPDefaultClause(OMPDefaultClause *C) {
return true;

View File

@ -121,6 +121,60 @@ public:
}
};
/// \brief This represents 'if' clause in the '#pragma omp ...' directive.
///
/// \code
/// #pragma omp parallel if(a > 5)
/// \endcode
/// In this example directive '#pragma omp parallel' has simple 'if'
/// clause with condition 'a > 5'.
///
class OMPIfClause : public OMPClause {
friend class OMPClauseReader;
/// \brief Location of '('.
SourceLocation LParenLoc;
/// \brief Condition of the 'if' clause.
Stmt *Condition;
/// \brief Set condition.
///
void setCondition(Expr *Cond) { Condition = Cond; }
public:
/// \brief Build 'if' clause with condition \a Cond.
///
/// \param StartLoc Starting location of the clause.
/// \param LParenLoc Location of '('.
/// \param Cond Condition of the clause.
/// \param EndLoc Ending location of the clause.
///
OMPIfClause(Expr *Cond, SourceLocation StartLoc,
SourceLocation LParenLoc, SourceLocation EndLoc)
: OMPClause(OMPC_if, StartLoc, EndLoc), LParenLoc(LParenLoc),
Condition(Cond) { }
/// \brief Build an empty clause.
///
OMPIfClause()
: OMPClause(OMPC_if, SourceLocation(), SourceLocation()),
LParenLoc(SourceLocation()), Condition(0) { }
/// \brief Sets the location of '('.
void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
/// \brief Returns the location of '('.
SourceLocation getLParenLoc() const { return LParenLoc; }
/// \brief Returns condition.
Expr *getCondition() const { return cast_or_null<Expr>(Condition); }
static bool classof(const OMPClause *T) {
return T->getClauseKind() == OMPC_if;
}
StmtRange children() {
return StmtRange(&Condition, &Condition + 1);
}
};
/// \brief This represents 'default' clause in the '#pragma omp ...' directive.
///
/// \code

View File

@ -855,6 +855,8 @@ def err_omp_unexpected_directive : Error <
"unexpected OpenMP directive '#pragma omp %0'">;
def err_omp_expected_punc : Error <
"expected ',' or ')' in '%0' clause">;
def err_omp_expected_rparen : Error <
"expected ')' in '%0' clause">;
def err_omp_unexpected_clause : Error <
"unexpected OpenMP clause '%0' in directive '#pragma omp %1'">;
def err_omp_more_one_clause : Error <

View File

@ -31,12 +31,14 @@ OPENMP_DIRECTIVE(parallel)
OPENMP_DIRECTIVE(task)
// OpenMP clauses.
OPENMP_CLAUSE(if, OMPIfClause)
OPENMP_CLAUSE(default, OMPDefaultClause)
OPENMP_CLAUSE(private, OMPPrivateClause)
OPENMP_CLAUSE(firstprivate, OMPFirstprivateClause)
OPENMP_CLAUSE(shared, OMPSharedClause)
// Clauses allowed for OpenMP directives.
OPENMP_PARALLEL_CLAUSE(if)
OPENMP_PARALLEL_CLAUSE(default)
OPENMP_PARALLEL_CLAUSE(private)
OPENMP_PARALLEL_CLAUSE(firstprivate)

View File

@ -7125,6 +7125,16 @@ public:
SourceLocation StartLoc,
SourceLocation EndLoc);
OMPClause *ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind,
Expr *Expr,
SourceLocation StartLoc,
SourceLocation LParenLoc,
SourceLocation EndLoc);
/// \brief Called on well-formed 'if' clause.
OMPClause *ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
SourceLocation LParenLoc,
SourceLocation EndLoc);
OMPClause *ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
unsigned Argument,
SourceLocation ArgumentLoc,

View File

@ -593,16 +593,24 @@ void StmtPrinter::VisitSEHFinallyStmt(SEHFinallyStmt *Node) {
namespace {
class OMPClausePrinter : public OMPClauseVisitor<OMPClausePrinter> {
raw_ostream &OS;
const PrintingPolicy &Policy;
/// \brief Process clauses with list of variables.
template <typename T>
void VisitOMPClauseList(T *Node, char StartSym);
public:
OMPClausePrinter(raw_ostream &OS) : OS(OS) { }
OMPClausePrinter(raw_ostream &OS, const PrintingPolicy &Policy)
: OS(OS), Policy(Policy) { }
#define OPENMP_CLAUSE(Name, Class) \
void Visit##Class(Class *S);
#include "clang/Basic/OpenMPKinds.def"
};
void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
OS << "if(";
Node->getCondition()->printPretty(OS, 0, Policy, 0);
OS << ")";
}
void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
OS << "default("
<< getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
@ -651,7 +659,7 @@ void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
Indent() << "#pragma omp parallel ";
OMPClausePrinter Printer(OS);
OMPClausePrinter Printer(OS, Policy);
ArrayRef<OMPClause *> Clauses = Node->clauses();
for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
I != E; ++I)

View File

@ -265,6 +265,11 @@ public:
#include "clang/Basic/OpenMPKinds.def"
};
void OMPClauseProfiler::VisitOMPIfClause(const OMPIfClause *C) {
if (C->getCondition())
Profiler->VisitStmt(C->getCondition());
}
void OMPClauseProfiler::VisitOMPDefaultClause(const OMPDefaultClause *C) { }
template<typename T>

View File

@ -77,6 +77,7 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind,
.Default(OMPC_DEFAULT_unknown);
case OMPC_unknown:
case OMPC_threadprivate:
case OMPC_if:
case OMPC_private:
case OMPC_firstprivate:
case OMPC_shared:
@ -100,6 +101,7 @@ const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
llvm_unreachable("Invalid OpenMP 'default' clause type");
case OMPC_unknown:
case OMPC_threadprivate:
case OMPC_if:
case OMPC_private:
case OMPC_firstprivate:
case OMPC_shared:

View File

@ -269,10 +269,20 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
}
switch (CKind) {
case OMPC_if:
// OpenMP [2.5, Restrictions]
// At most one if clause can appear on the directive.
if (!FirstClause) {
Diag(Tok, diag::err_omp_more_one_clause)
<< getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind);
}
Clause = ParseOpenMPSingleExprClause(CKind);
break;
case OMPC_default:
// OpenMP [2.9.3.1, Restrictions]
// Only a single default clause may be specified on a parallel or task
// directive.
// OpenMP [2.14.3.1, Restrictions]
// Only a single default clause may be specified on a parallel, task or
// teams directive.
if (!FirstClause) {
Diag(Tok, diag::err_omp_more_one_clause)
<< getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind);
@ -300,6 +310,39 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
return ErrorFound ? 0 : Clause;
}
/// \brief Parsing of OpenMP clauses with single expressions like 'if',
/// 'collapse', 'safelen', 'num_threads', 'simdlen', 'num_teams' or
/// 'thread_limit'.
///
/// if-clause:
/// 'if' '(' expression ')'
///
OMPClause *Parser::ParseOpenMPSingleExprClause(OpenMPClauseKind Kind) {
SourceLocation Loc = ConsumeToken();
BalancedDelimiterTracker T(*this, tok::l_paren, tok::annot_pragma_openmp_end);
if (T.expectAndConsume(diag::err_expected_lparen_after,
getOpenMPClauseName(Kind)))
return 0;
ExprResult LHS(ParseCastExpression(false, false, NotTypeCast));
ExprResult Val(ParseRHSOfBinaryExpression(LHS, prec::Conditional));
if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::comma) &&
Tok.isNot(tok::annot_pragma_openmp_end))
ConsumeAnyToken();
// Parse ')'.
T.consumeClose();
if (Val.isInvalid())
return 0;
return Actions.ActOnOpenMPSingleExprClause(Kind, Val.take(), Loc,
T.getOpenLocation(),
T.getCloseLocation());
}
/// \brief Parsing of simple OpenMP clauses like 'default'.
///
/// default-clause:

View File

@ -721,6 +721,48 @@ StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
Clauses, AStmt));
}
OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind,
Expr *Expr,
SourceLocation StartLoc,
SourceLocation LParenLoc,
SourceLocation EndLoc) {
OMPClause *Res = 0;
switch (Kind) {
case OMPC_if:
Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
break;
case OMPC_default:
case OMPC_private:
case OMPC_firstprivate:
case OMPC_shared:
case OMPC_threadprivate:
case OMPC_unknown:
case NUM_OPENMP_CLAUSES:
llvm_unreachable("Clause is not allowed.");
}
return Res;
}
OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition,
SourceLocation StartLoc,
SourceLocation LParenLoc,
SourceLocation EndLoc) {
Expr *ValExpr = Condition;
if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
!Condition->isInstantiationDependent() &&
!Condition->containsUnexpandedParameterPack()) {
ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Condition->getExprLoc(),
Condition);
if (Val.isInvalid())
return 0;
ValExpr = Val.take();
}
return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
}
OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
unsigned Argument,
SourceLocation ArgumentLoc,
@ -734,6 +776,7 @@ OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
ArgumentLoc, StartLoc, LParenLoc, EndLoc);
break;
case OMPC_if:
case OMPC_private:
case OMPC_firstprivate:
case OMPC_shared:
@ -780,7 +823,9 @@ OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
case OMPC_DEFAULT_shared:
DSAStack->setDefaultDSAShared();
break;
default:
case OMPC_DEFAULT_unknown:
case NUM_OPENMP_DEFAULT_KINDS:
llvm_unreachable("Clause kind is not allowed.");
break;
}
return new (Context) OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc,
@ -803,6 +848,7 @@ OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
case OMPC_shared:
Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
break;
case OMPC_if:
case OMPC_default:
case OMPC_threadprivate:
case OMPC_unknown:

View File

@ -1298,6 +1298,18 @@ public:
StartLoc, EndLoc);
}
/// \brief Build a new OpenMP 'if' clause.
///
/// By default, performs semantic analysis to build the new statement.
/// Subclasses may override this routine to provide different behavior.
OMPClause *RebuildOMPIfClause(Expr *Condition,
SourceLocation StartLoc,
SourceLocation LParenLoc,
SourceLocation EndLoc) {
return getSema().ActOnOpenMPIfClause(Condition, StartLoc,
LParenLoc, EndLoc);
}
/// \brief Build a new OpenMP 'default' clause.
///
/// By default, performs semantic analysis to build the new statement.
@ -6277,6 +6289,13 @@ TreeTransform<Derived>::TransformOMPParallelDirective(OMPParallelDirective *D) {
return Res;
}
template<typename Derived>
OMPClause *
TreeTransform<Derived>::TransformOMPIfClause(OMPIfClause *C) {
return getDerived().RebuildOMPIfClause(C->getCondition(), C->getLocStart(),
C->getLParenLoc(), C->getLocEnd());
}
template<typename Derived>
OMPClause *
TreeTransform<Derived>::TransformOMPDefaultClause(OMPDefaultClause *C) {

View File

@ -1673,6 +1673,9 @@ public:
OMPClause *OMPClauseReader::readClause() {
OMPClause *C;
switch (Record[Idx++]) {
case OMPC_if:
C = new (Context) OMPIfClause();
break;
case OMPC_default:
C = new (Context) OMPDefaultClause();
break;
@ -1693,6 +1696,11 @@ OMPClause *OMPClauseReader::readClause() {
return C;
}
void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
C->setCondition(Reader->Reader.ReadSubExpr());
C->setLParenLoc(Reader->ReadSourceLocation(Record, Idx));
}
void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
C->setDefaultKind(
static_cast<OpenMPDefaultClauseKind>(Record[Idx++]));

View File

@ -1679,6 +1679,11 @@ void OMPClauseWriter::writeClause(OMPClause *C) {
Writer->Writer.AddSourceLocation(C->getLocEnd(), Record);
}
void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
Writer->Writer.AddStmt(C->getCondition());
Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
}
void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
Record.push_back(C->getDefaultKind());
Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);

View File

@ -15,7 +15,7 @@ T tmain(T argc, T *argv) {
static T a;
#pragma omp parallel
a=2;
#pragma omp parallel default(none), private(argc,b) firstprivate(argv) shared (d)
#pragma omp parallel default(none), private(argc,b) firstprivate(argv) shared (d) if (argc > 0)
foo();
return 0;
}
@ -24,21 +24,21 @@ T tmain(T argc, T *argv) {
// CHECK-NEXT: static int a;
// CHECK-NEXT: #pragma omp parallel
// CHECK-NEXT: a = 2;
// CHECK-NEXT: #pragma omp parallel default(none) private(argc,b) firstprivate(argv) shared(d)
// CHECK-NEXT: #pragma omp parallel default(none) private(argc,b) firstprivate(argv) shared(d) if(argc > 0)
// CHECK-NEXT: foo()
// CHECK: template <typename T = float> float tmain(float argc, float *argv) {
// CHECK-NEXT: float b = argc, c, d, e, f, g;
// CHECK-NEXT: static float a;
// CHECK-NEXT: #pragma omp parallel
// CHECK-NEXT: a = 2;
// CHECK-NEXT: #pragma omp parallel default(none) private(argc,b) firstprivate(argv) shared(d)
// CHECK-NEXT: #pragma omp parallel default(none) private(argc,b) firstprivate(argv) shared(d) if(argc > 0)
// CHECK-NEXT: foo()
// CHECK: template <typename T> T tmain(T argc, T *argv) {
// CHECK-NEXT: T b = argc, c, d, e, f, g;
// CHECK-NEXT: static T a;
// CHECK-NEXT: #pragma omp parallel
// CHECK-NEXT: a = 2;
// CHECK-NEXT: #pragma omp parallel default(none) private(argc,b) firstprivate(argv) shared(d)
// CHECK-NEXT: #pragma omp parallel default(none) private(argc,b) firstprivate(argv) shared(d) if(argc > 0)
// CHECK-NEXT: foo()
int main (int argc, char **argv) {
@ -50,8 +50,8 @@ int main (int argc, char **argv) {
// CHECK-NEXT: #pragma omp parallel
a=2;
// CHECK-NEXT: a = 2;
#pragma omp parallel default(none), private(argc,b) firstprivate(argv)
// CHECK-NEXT: #pragma omp parallel default(none) private(argc,b) firstprivate(argv)
#pragma omp parallel default(none), private(argc,b) firstprivate(argv) if (argc > 0)
// CHECK-NEXT: #pragma omp parallel default(none) private(argc,b) firstprivate(argv) if(argc > 0)
foo();
// CHECK-NEXT: foo();
return tmain(b, &b) + tmain(x, &x);

View File

@ -0,0 +1,43 @@
// RUN: %clang_cc1 -verify -fopenmp -ferror-limit 100 %s
void foo() {
}
bool foobool(int argc) {
return argc;
}
struct S1; // expected-note {{declared here}}
template <class T, class S> // expected-note {{declared here}}
int tmain(T argc, S **argv) {
#pragma omp parallel if // expected-error {{expected '(' after 'if'}}
#pragma omp parallel if ( // expected-error {{expected expression}} expected-error {{expected ')'}} expected-note {{to match this '('}}
#pragma omp parallel if () // expected-error {{expected expression}}
#pragma omp parallel if (argc // expected-error {{expected ')'}} expected-note {{to match this '('}}
#pragma omp parallel if (argc)) // expected-warning {{extra tokens at the end of '#pragma omp parallel' are ignored}}
#pragma omp parallel if (argc > 0 ? argv[1] : argv[2])
#pragma omp parallel if (foobool(argc)), if (true) // expected-error {{directive '#pragma omp parallel' cannot contain more than one 'if' clause}}
#pragma omp parallel if (S) // expected-error {{'S' does not refer to a value}}
#pragma omp parallel if (argv[1]=2) // expected-error {{expected ')'}} expected-note {{to match this '('}}
#pragma omp parallel if(argc)
foo();
return 0;
}
int main(int argc, char **argv) {
#pragma omp parallel if // expected-error {{expected '(' after 'if'}}
#pragma omp parallel if ( // expected-error {{expected expression}} expected-error {{expected ')'}} expected-note {{to match this '('}}
#pragma omp parallel if () // expected-error {{expected expression}}
#pragma omp parallel if (argc // expected-error {{expected ')'}} expected-note {{to match this '('}}
#pragma omp parallel if (argc)) // expected-warning {{extra tokens at the end of '#pragma omp parallel' are ignored}}
#pragma omp parallel if (argc > 0 ? argv[1] : argv[2])
#pragma omp parallel if (foobool(argc)), if (true) // expected-error {{directive '#pragma omp parallel' cannot contain more than one 'if' clause}}
#pragma omp parallel if (S1) // expected-error {{'S1' does not refer to a value}}
#pragma omp parallel if (argv[1]=2) // expected-error {{expected ')'}} expected-note {{to match this '('}}
#pragma omp parallel if(if(tmain(argc, argv) // expected-error {{expected expression}} expected-error {{expected ')'}} expected-note {{to match this '('}}
foo();
return tmain(argc, argv);
}

View File

@ -1939,6 +1939,10 @@ public:
#include "clang/Basic/OpenMPKinds.def"
};
void OMPClauseEnqueue::VisitOMPIfClause(const OMPIfClause *C) {
Visitor->AddStmt(C->getCondition());
}
void OMPClauseEnqueue::VisitOMPDefaultClause(const OMPDefaultClause *C) { }
template<typename T>