This patch commits OpenMP 4 target device clauses

This is committed on behalf of Kelvin Li
http://reviews.llvm.org/D11469?id=31227

llvm-svn: 244325
This commit is contained in:
Michael Wong 2015-08-07 16:16:36 +00:00
parent 827a40b944
commit e710d5459e
17 changed files with 217 additions and 2 deletions

View File

@ -2667,6 +2667,12 @@ bool RecursiveASTVisitor<Derived>::VisitOMPDependClause(OMPDependClause *C) {
return true;
}
template <typename Derived>
bool RecursiveASTVisitor<Derived>::VisitOMPDeviceClause(OMPDeviceClause *C) {
TRY_TO(TraverseStmt(C->getDevice()));
return true;
}
// FIXME: look at the following tricky-seeming exprs to see if we
// need to recurse on anything. These are ones that have methods
// returning decls or qualtypes or nestednamespecifier -- though I'm

View File

@ -2349,6 +2349,61 @@ public:
}
};
/// \brief This represents 'device' clause in the '#pragma omp ...'
/// directive.
///
/// \code
/// #pragma omp target device(a)
/// \endcode
/// In this example directive '#pragma omp target' has clause 'device'
/// with single expression 'a'.
///
class OMPDeviceClause : public OMPClause {
friend class OMPClauseReader;
/// \brief Location of '('.
SourceLocation LParenLoc;
/// \brief Device number.
Stmt *Device;
/// \brief Set the device number.
///
/// \param E Device number.
///
void setDevice(Expr *E) { Device = E; }
public:
/// \brief Build 'device' clause.
///
/// \param E Expression associated with this clause.
/// \param StartLoc Starting location of the clause.
/// \param LParenLoc Location of '('.
/// \param EndLoc Ending location of the clause.
///
OMPDeviceClause(Expr *E, SourceLocation StartLoc, SourceLocation LParenLoc,
SourceLocation EndLoc)
: OMPClause(OMPC_device, StartLoc, EndLoc), LParenLoc(LParenLoc),
Device(E) {}
/// \brief Build an empty clause.
///
OMPDeviceClause()
: OMPClause(OMPC_device, SourceLocation(), SourceLocation()),
LParenLoc(SourceLocation()), Device(0) {}
/// \brief Sets the location of '('.
void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
/// \brief Returns the location of '('.
SourceLocation getLParenLoc() const { return LParenLoc; }
/// \brief Return device number.
Expr *getDevice() { return cast<Expr>(Device); }
/// \brief Return device number.
Expr *getDevice() const { return cast<Expr>(Device); }
static bool classof(const OMPClause *T) {
return T->getClauseKind() == OMPC_device;
}
child_range children() { return child_range(&Device, &Device + 1); }
};
} // end namespace clang
#endif

View File

@ -2699,6 +2699,12 @@ bool RecursiveASTVisitor<Derived>::VisitOMPDependClause(OMPDependClause *C) {
return true;
}
template <typename Derived>
bool RecursiveASTVisitor<Derived>::VisitOMPDeviceClause(OMPDeviceClause *C) {
TRY_TO(TraverseStmt(C->getDevice()));
return true;
}
// FIXME: look at the following tricky-seeming exprs to see if we
// need to recurse on anything. These are ones that have methods
// returning decls or qualtypes or nestednamespecifier -- though I'm

View File

@ -133,6 +133,7 @@ OPENMP_CLAUSE(update, OMPUpdateClause)
OPENMP_CLAUSE(capture, OMPCaptureClause)
OPENMP_CLAUSE(seq_cst, OMPSeqCstClause)
OPENMP_CLAUSE(depend, OMPDependClause)
OPENMP_CLAUSE(device, OMPDeviceClause)
// Clauses allowed for OpenMP directive 'parallel'.
OPENMP_PARALLEL_CLAUSE(if)
@ -277,10 +278,12 @@ OPENMP_ATOMIC_CLAUSE(seq_cst)
// Clauses allowed for OpenMP directive 'target'.
// TODO More clauses for 'target' directive.
OPENMP_TARGET_CLAUSE(if)
OPENMP_TARGET_CLAUSE(device)
// Clauses allowed for OpenMP directive 'target data'.
// TODO More clauses for 'target data' directive.
OPENMP_TARGET_DATA_CLAUSE(if)
OPENMP_TARGET_DATA_CLAUSE(device)
// Clauses allowed for OpenMP directive 'teams'.
// TODO More clauses for 'teams' directive.

View File

@ -8024,7 +8024,11 @@ public:
SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
SourceLocation StartLoc, SourceLocation LParenLoc,
SourceLocation EndLoc);
/// \brief Called on well-formed 'device' clause.
OMPClause *ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
SourceLocation LParenLoc,
SourceLocation EndLoc);
/// \brief The kind of conversion being performed.
enum CheckedConversionKind {
/// \brief An implicit conversion.

View File

@ -688,6 +688,12 @@ void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
OS << "seq_cst";
}
void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
OS << "device(";
Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
OS << ")";
}
template<typename T>
void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
for (typename T::varlist_iterator I = Node->varlist_begin(),

View File

@ -431,6 +431,9 @@ void OMPClauseProfiler::VisitOMPFlushClause(const OMPFlushClause *C) {
void OMPClauseProfiler::VisitOMPDependClause(const OMPDependClause *C) {
VisitOMPClauseList(C);
}
void OMPClauseProfiler::VisitOMPDeviceClause(const OMPDeviceClause *C) {
Profiler->VisitStmt(C->getDevice());
}
}
void

View File

@ -122,6 +122,7 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind,
case OMPC_update:
case OMPC_capture:
case OMPC_seq_cst:
case OMPC_device:
break;
}
llvm_unreachable("Invalid OpenMP simple clause kind");
@ -195,6 +196,7 @@ const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
case OMPC_update:
case OMPC_capture:
case OMPC_seq_cst:
case OMPC_device:
break;
}
llvm_unreachable("Invalid OpenMP simple clause kind");

View File

@ -2077,6 +2077,7 @@ static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind,
case OMPC_threadprivate:
case OMPC_depend:
case OMPC_mergeable:
case OMPC_device:
llvm_unreachable("Clause is not allowed in 'omp atomic'.");
}
}

View File

@ -396,7 +396,7 @@ bool Parser::ParseOpenMPSimpleVarList(OpenMPDirectiveKind Kind,
/// lastprivate-clause | reduction-clause | proc_bind-clause |
/// schedule-clause | copyin-clause | copyprivate-clause | untied-clause |
/// mergeable-clause | flush-clause | read-clause | write-clause |
/// update-clause | capture-clause | seq_cst-clause
/// update-clause | capture-clause | seq_cst-clause | device-clause
///
OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
OpenMPClauseKind CKind, bool FirstClause) {
@ -416,12 +416,15 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
case OMPC_safelen:
case OMPC_collapse:
case OMPC_ordered:
case OMPC_device:
// OpenMP [2.5, Restrictions]
// At most one if clause can appear on the directive.
// At most one num_threads clause can appear on the directive.
// OpenMP [2.8.1, simd construct, Restrictions]
// Only one safelen clause can appear on a simd directive.
// Only one collapse clause can appear on a simd directive.
// OpenMP [2.9.1, target data construct, Restrictions]
// At most one device clause can appear on the directive.
// OpenMP [2.11.1, task Construct, Restrictions]
// At most one if clause can appear on the directive.
// At most one final clause can appear on the directive.

View File

@ -4511,6 +4511,9 @@ OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
case OMPC_ordered:
Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
break;
case OMPC_device:
Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
break;
case OMPC_default:
case OMPC_proc_bind:
case OMPC_schedule:
@ -4774,6 +4777,7 @@ OMPClause *Sema::ActOnOpenMPSimpleClause(
case OMPC_capture:
case OMPC_seq_cst:
case OMPC_depend:
case OMPC_device:
case OMPC_unknown:
llvm_unreachable("Clause is not allowed.");
}
@ -4895,6 +4899,7 @@ OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
case OMPC_capture:
case OMPC_seq_cst:
case OMPC_depend:
case OMPC_device:
case OMPC_unknown:
llvm_unreachable("Clause is not allowed.");
}
@ -5018,6 +5023,7 @@ OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
case OMPC_threadprivate:
case OMPC_flush:
case OMPC_depend:
case OMPC_device:
case OMPC_unknown:
llvm_unreachable("Clause is not allowed.");
}
@ -5128,6 +5134,7 @@ OMPClause *Sema::ActOnOpenMPVarListClause(
case OMPC_update:
case OMPC_capture:
case OMPC_seq_cst:
case OMPC_device:
case OMPC_unknown:
llvm_unreachable("Clause is not allowed.");
}
@ -6722,3 +6729,28 @@ Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
DepLoc, ColonLoc, Vars);
}
OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
SourceLocation LParenLoc,
SourceLocation EndLoc) {
Expr *ValExpr = Device;
if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
!ValExpr->isInstantiationDependent()) {
SourceLocation Loc = ValExpr->getExprLoc();
ExprResult Value = PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
if (Value.isInvalid())
return nullptr;
// OpenMP [2.9.1, Restrictions]
// The device expression must evaluate to a non-negative integer value.
llvm::APSInt Result;
if (Value.get()->isIntegerConstantExpr(Result, Context) &&
Result.isSigned() && !Result.isStrictlyPositive()) {
Diag(Loc, diag::err_omp_negative_expression_in_clause)
<< "device" << ValExpr->getSourceRange();
return nullptr;
}
}
return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
}

View File

@ -1596,6 +1596,17 @@ public:
StartLoc, LParenLoc, EndLoc);
}
/// \brief Build a new OpenMP 'device' clause.
///
/// By default, performs semantic analysis to build the new statement.
/// Subclasses may override this routine to provide different behavior.
OMPClause *RebuildOMPDeviceClause(Expr *Device, SourceLocation StartLoc,
SourceLocation LParenLoc,
SourceLocation EndLoc) {
return getSema().ActOnOpenMPDeviceClause(Device, StartLoc, LParenLoc,
EndLoc);
}
/// \brief Rebuild the operand to an Objective-C \@synchronized statement.
///
/// By default, performs semantic analysis to build the new statement.
@ -7494,6 +7505,16 @@ TreeTransform<Derived>::TransformOMPDependClause(OMPDependClause *C) {
C->getLocStart(), C->getLParenLoc(), C->getLocEnd());
}
template <typename Derived>
OMPClause *
TreeTransform<Derived>::TransformOMPDeviceClause(OMPDeviceClause *C) {
ExprResult E = getDerived().TransformExpr(C->getDevice());
if (E.isInvalid())
return nullptr;
return getDerived().RebuildOMPDeviceClause(
E.get(), C->getLocStart(), C->getLParenLoc(), C->getLocEnd());
}
//===----------------------------------------------------------------------===//
// Expression transformation
//===----------------------------------------------------------------------===//

View File

@ -1788,6 +1788,9 @@ OMPClause *OMPClauseReader::readClause() {
case OMPC_depend:
C = OMPDependClause::CreateEmpty(Context, Record[Idx++]);
break;
case OMPC_device:
C = new (Context) OMPDeviceClause();
break;
}
Visit(C);
C->setLocStart(Reader->ReadSourceLocation(Record, Idx));
@ -2068,6 +2071,11 @@ void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
C->setVarRefs(Vars);
}
void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
C->setDevice(Reader->Reader.ReadSubExpr());
C->setLParenLoc(Reader->ReadSourceLocation(Record, Idx));
}
//===----------------------------------------------------------------------===//
// OpenMP Directives.
//===----------------------------------------------------------------------===//

View File

@ -1919,6 +1919,11 @@ void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
Writer->Writer.AddStmt(VE);
}
void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
Writer->Writer.AddStmt(C->getDevice());
Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
}
//===----------------------------------------------------------------------===//
// OpenMP Directives.
//===----------------------------------------------------------------------===//

View File

@ -0,0 +1,28 @@
// RUN: %clang_cc1 -triple x86_64-apple-macos10.7.0 -verify -fopenmp -ferror-limit 100 -o - %s
void foo() {
}
bool foobool(int argc) {
return argc;
}
struct S1; // expected-note {{declared here}}
int main(int argc, char **argv) {
#pragma omp target data device // expected-error {{expected '(' after 'device'}}
#pragma omp target data device ( // expected-error {{expected expression}} expected-error {{expected ')'}} expected-note {{to match this '('}}
#pragma omp target data device () // expected-error {{expected expression}}
#pragma omp target data device (argc // expected-error {{expected ')'}} expected-note {{to match this '('}}
#pragma omp target data device (argc)) // expected-warning {{extra tokens at the end of '#pragma omp target data' are ignored}}
#pragma omp target data device (argc > 0 ? argv[1] : argv[2]) // expected-error {{expression must have integral or unscoped enumeration type, not 'char *'}}
#pragma omp target data device (argc + argc)
#pragma omp target data device (argc), device (argc+1) // expected-error {{directive '#pragma omp target data' cannot contain more than one 'device' clause}}
#pragma omp target data device (S1) // expected-error {{'S1' does not refer to a value}}
#pragma omp target data device (-2) // expected-error {{argument to 'device' clause must be a positive integer value}}
#pragma omp target device (-10u)
#pragma omp target device (3.14) // expected-error {{expression must have integral or unscoped enumeration type, not 'double'}}
foo();
return 0;
}

View File

@ -0,0 +1,28 @@
// RUN: %clang_cc1 -triple x86_64-apple-macos10.7.0 -verify -fopenmp -ferror-limit 100 -o - %s
void foo() {
}
bool foobool(int argc) {
return argc;
}
struct S1; // expected-note {{declared here}}
int main(int argc, char **argv) {
#pragma omp target device // expected-error {{expected '(' after 'device'}}
#pragma omp target device ( // expected-error {{expected expression}} expected-error {{expected ')'}} expected-note {{to match this '('}}
#pragma omp target device () // expected-error {{expected expression}}
#pragma omp target device (argc // expected-error {{expected ')'}} expected-note {{to match this '('}}
#pragma omp target device (argc)) // expected-warning {{extra tokens at the end of '#pragma omp target' are ignored}}
#pragma omp target device (argc > 0 ? argv[1] : argv[2]) // expected-error {{expression must have integral or unscoped enumeration type, not 'char *'}}
#pragma omp target device (argc + argc)
#pragma omp target device (argc), device (argc+1) // expected-error {{directive '#pragma omp target' cannot contain more than one 'device' clause}}
#pragma omp target device (S1) // expected-error {{'S1' does not refer to a value}}
#pragma omp target device (-2) // expected-error {{argument to 'device' clause must be a positive integer value}}
#pragma omp target device (-10u)
#pragma omp target device (3.14) // expected-error {{expression must have integral or unscoped enumeration type, not 'double'}}
foo();
return 0;
}

View File

@ -2052,6 +2052,10 @@ void OMPClauseEnqueue::VisitOMPCaptureClause(const OMPCaptureClause *) {}
void OMPClauseEnqueue::VisitOMPSeqCstClause(const OMPSeqCstClause *) {}
void OMPClauseEnqueue::VisitOMPDeviceClause(const OMPDeviceClause *C) {
Visitor->AddStmt(C->getDevice());
}
template<typename T>
void OMPClauseEnqueue::VisitOMPClauseList(T *Node) {
for (const auto *I : Node->varlists()) {