Skip to content

Commit

Permalink
[OPENMP] 'if' clause support (no CodeGen support)
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@201297 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
alexey-bataev committed Feb 13, 2014
1 parent 6e1cfa4 commit 154210a
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 12 deletions.
6 changes: 6 additions & 0 deletions include/clang/AST/DataRecursiveASTVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions include/clang/AST/RecursiveASTVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
54 changes: 54 additions & 0 deletions include/clang/AST/StmtOpenMP.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,60 @@ class OMPVarList {
}
};

/// \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
Expand Down
2 changes: 2 additions & 0 deletions include/clang/Basic/DiagnosticParseKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -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 <
Expand Down
2 changes: 2 additions & 0 deletions include/clang/Basic/OpenMPKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -7125,6 +7125,16 @@ class Sema {
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,
Expand Down
12 changes: 10 additions & 2 deletions lib/AST/StmtPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions lib/AST/StmtProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ class OMPClauseProfiler : public ConstOMPClauseVisitor<OMPClauseProfiler> {
#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>
Expand Down
2 changes: 2 additions & 0 deletions lib/Basic/OpenMPKinds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
49 changes: 46 additions & 3 deletions lib/Parse/ParseOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down
48 changes: 47 additions & 1 deletion lib/Sema/SemaOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,18 @@ class TreeTransform {
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.
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions lib/Serialization/ASTReaderStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,9 @@ class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
OMPClause *OMPClauseReader::readClause() {
OMPClause *C;
switch (Record[Idx++]) {
case OMPC_if:
C = new (Context) OMPIfClause();
break;
case OMPC_default:
C = new (Context) OMPDefaultClause();
break;
Expand All @@ -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++]));
Expand Down
5 changes: 5 additions & 0 deletions lib/Serialization/ASTWriterStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 154210a

Please sign in to comment.