From: Michael Wong Date: Fri, 7 Aug 2015 16:16:36 +0000 (+0000) Subject: This patch commits OpenMP 4 target device clauses X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b6f1a49465f108ffe901b66ee5b9056ed3a041c4;p=clang This patch commits OpenMP 4 target device clauses This is committed on behalf of Kelvin Li http://reviews.llvm.org/D11469?id=31227 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@244325 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/DataRecursiveASTVisitor.h b/include/clang/AST/DataRecursiveASTVisitor.h index c1e98e757e..997d0400c7 100644 --- a/include/clang/AST/DataRecursiveASTVisitor.h +++ b/include/clang/AST/DataRecursiveASTVisitor.h @@ -2667,6 +2667,12 @@ bool RecursiveASTVisitor::VisitOMPDependClause(OMPDependClause *C) { return true; } +template +bool RecursiveASTVisitor::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 diff --git a/include/clang/AST/OpenMPClause.h b/include/clang/AST/OpenMPClause.h index 03104be183..dc08db48fb 100644 --- a/include/clang/AST/OpenMPClause.h +++ b/include/clang/AST/OpenMPClause.h @@ -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(Device); } + /// \brief Return device number. + Expr *getDevice() const { return cast(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 diff --git a/include/clang/AST/RecursiveASTVisitor.h b/include/clang/AST/RecursiveASTVisitor.h index e0a48e86b8..b8ae8331fd 100644 --- a/include/clang/AST/RecursiveASTVisitor.h +++ b/include/clang/AST/RecursiveASTVisitor.h @@ -2699,6 +2699,12 @@ bool RecursiveASTVisitor::VisitOMPDependClause(OMPDependClause *C) { return true; } +template +bool RecursiveASTVisitor::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 diff --git a/include/clang/Basic/OpenMPKinds.def b/include/clang/Basic/OpenMPKinds.def index 302a30d61a..e1d7c553ac 100644 --- a/include/clang/Basic/OpenMPKinds.def +++ b/include/clang/Basic/OpenMPKinds.def @@ -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. diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 9480716bd1..9ae9639565 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -8024,7 +8024,11 @@ public: SourceLocation ColonLoc, ArrayRef 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. diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp index 57187ca862..64f90ba951 100644 --- a/lib/AST/StmtPrinter.cpp +++ b/lib/AST/StmtPrinter.cpp @@ -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 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) { for (typename T::varlist_iterator I = Node->varlist_begin(), diff --git a/lib/AST/StmtProfile.cpp b/lib/AST/StmtProfile.cpp index 091682ad7f..fa1be8644b 100644 --- a/lib/AST/StmtProfile.cpp +++ b/lib/AST/StmtProfile.cpp @@ -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 diff --git a/lib/Basic/OpenMPKinds.cpp b/lib/Basic/OpenMPKinds.cpp index 7db0381665..b431598191 100644 --- a/lib/Basic/OpenMPKinds.cpp +++ b/lib/Basic/OpenMPKinds.cpp @@ -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"); diff --git a/lib/CodeGen/CGStmtOpenMP.cpp b/lib/CodeGen/CGStmtOpenMP.cpp index fa91575526..d69040d16b 100644 --- a/lib/CodeGen/CGStmtOpenMP.cpp +++ b/lib/CodeGen/CGStmtOpenMP.cpp @@ -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'."); } } diff --git a/lib/Parse/ParseOpenMP.cpp b/lib/Parse/ParseOpenMP.cpp index 1266e447fc..fb348ef55d 100644 --- a/lib/Parse/ParseOpenMP.cpp +++ b/lib/Parse/ParseOpenMP.cpp @@ -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. diff --git a/lib/Sema/SemaOpenMP.cpp b/lib/Sema/SemaOpenMP.cpp index 57ebb38a70..03e7d23718 100644 --- a/lib/Sema/SemaOpenMP.cpp +++ b/lib/Sema/SemaOpenMP.cpp @@ -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); +} diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h index 885dba778f..6b19bd2ad7 100644 --- a/lib/Sema/TreeTransform.h +++ b/lib/Sema/TreeTransform.h @@ -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::TransformOMPDependClause(OMPDependClause *C) { C->getLocStart(), C->getLParenLoc(), C->getLocEnd()); } +template +OMPClause * +TreeTransform::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 //===----------------------------------------------------------------------===// diff --git a/lib/Serialization/ASTReaderStmt.cpp b/lib/Serialization/ASTReaderStmt.cpp index f95e66fc07..1de92d3b75 100644 --- a/lib/Serialization/ASTReaderStmt.cpp +++ b/lib/Serialization/ASTReaderStmt.cpp @@ -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. //===----------------------------------------------------------------------===// diff --git a/lib/Serialization/ASTWriterStmt.cpp b/lib/Serialization/ASTWriterStmt.cpp index b9c14e548c..1284720a24 100644 --- a/lib/Serialization/ASTWriterStmt.cpp +++ b/lib/Serialization/ASTWriterStmt.cpp @@ -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. //===----------------------------------------------------------------------===// diff --git a/test/OpenMP/target_data_device_messages.cpp b/test/OpenMP/target_data_device_messages.cpp new file mode 100644 index 0000000000..6544a14269 --- /dev/null +++ b/test/OpenMP/target_data_device_messages.cpp @@ -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; +} diff --git a/test/OpenMP/target_device_messages.cpp b/test/OpenMP/target_device_messages.cpp new file mode 100644 index 0000000000..53ae8c5709 --- /dev/null +++ b/test/OpenMP/target_device_messages.cpp @@ -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; +} diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index b9a56833a8..1fb62d259c 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -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 void OMPClauseEnqueue::VisitOMPClauseList(T *Node) { for (const auto *I : Node->varlists()) {