From: Alexey Bataev Date: Wed, 1 Jul 2015 06:57:41 +0000 (+0000) Subject: [OPENMP 4.0] Initial support for 'omp cancellation point' construct. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a4233e101446c16b138cc488c9b55d57b823446b;p=clang [OPENMP 4.0] Initial support for 'omp cancellation point' construct. Add parsing and sema analysis for 'omp cancellation point' directive. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@241145 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index a224180207..5a2e088274 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -2225,12 +2225,15 @@ enum CXCursorKind { */ CXCursor_OMPTeamsDirective = 253, - /** \brief OpenMP taskwait directive. + /** \brief OpenMP taskgroup directive. */ CXCursor_OMPTaskgroupDirective = 254, + /** \brief OpenMP cancellation point directive. + */ + CXCursor_OMPCancellationPointDirective = 255, - CXCursor_LastStmt = CXCursor_OMPTaskgroupDirective, + CXCursor_LastStmt = CXCursor_OMPCancellationPointDirective, /** * \brief Cursor that represents the translation unit itself. diff --git a/include/clang/AST/DataRecursiveASTVisitor.h b/include/clang/AST/DataRecursiveASTVisitor.h index a78fe44ff2..7a4ebf062f 100644 --- a/include/clang/AST/DataRecursiveASTVisitor.h +++ b/include/clang/AST/DataRecursiveASTVisitor.h @@ -2358,6 +2358,9 @@ DEF_TRAVERSE_STMT(OMPTaskwaitDirective, DEF_TRAVERSE_STMT(OMPTaskgroupDirective, { TRY_TO(TraverseOMPExecutableDirective(S)); }) +DEF_TRAVERSE_STMT(OMPCancellationPointDirective, + { TRY_TO(TraverseOMPExecutableDirective(S)); }) + DEF_TRAVERSE_STMT(OMPFlushDirective, { TRY_TO(TraverseOMPExecutableDirective(S)); }) diff --git a/include/clang/AST/RecursiveASTVisitor.h b/include/clang/AST/RecursiveASTVisitor.h index b01d17bb32..1fe5b55342 100644 --- a/include/clang/AST/RecursiveASTVisitor.h +++ b/include/clang/AST/RecursiveASTVisitor.h @@ -2391,6 +2391,9 @@ DEF_TRAVERSE_STMT(OMPTaskwaitDirective, DEF_TRAVERSE_STMT(OMPTaskgroupDirective, { TRY_TO(TraverseOMPExecutableDirective(S)); }) +DEF_TRAVERSE_STMT(OMPCancellationPointDirective, + { TRY_TO(TraverseOMPExecutableDirective(S)); }) + DEF_TRAVERSE_STMT(OMPFlushDirective, { TRY_TO(TraverseOMPExecutableDirective(S)); }) diff --git a/include/clang/AST/StmtOpenMP.h b/include/clang/AST/StmtOpenMP.h index 63f295ddfe..8c8985dabf 100644 --- a/include/clang/AST/StmtOpenMP.h +++ b/include/clang/AST/StmtOpenMP.h @@ -1856,6 +1856,64 @@ public: } }; +/// \brief This represents '#pragma omp cancellation point' directive. +/// +/// \code +/// #pragma omp cancellation point for +/// \endcode +/// +/// In this example a cancellation point is created for innermost 'for' region. +class OMPCancellationPointDirective : public OMPExecutableDirective { + friend class ASTStmtReader; + OpenMPDirectiveKind CancelRegion; + /// \brief Build directive with the given start and end location. + /// + /// \param StartLoc Starting location of the directive kind. + /// \param EndLoc Ending location of the directive. + /// + OMPCancellationPointDirective(SourceLocation StartLoc, SourceLocation EndLoc) + : OMPExecutableDirective(this, OMPCancellationPointDirectiveClass, + OMPD_cancellation_point, StartLoc, EndLoc, 0, 0), + CancelRegion(OMPD_unknown) {} + + /// \brief Build an empty directive. + /// + explicit OMPCancellationPointDirective() + : OMPExecutableDirective(this, OMPCancellationPointDirectiveClass, + OMPD_cancellation_point, SourceLocation(), + SourceLocation(), 0, 0), + CancelRegion(OMPD_unknown) {} + + /// \brief Set cancel region for current cancellation point. + /// \param CR Cancellation region. + void setCancelRegion(OpenMPDirectiveKind CR) { CancelRegion = CR; } + +public: + /// \brief Creates directive. + /// + /// \param C AST context. + /// \param StartLoc Starting location of the directive kind. + /// \param EndLoc Ending Location of the directive. + /// + static OMPCancellationPointDirective * + Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc, + OpenMPDirectiveKind CancelRegion); + + /// \brief Creates an empty directive. + /// + /// \param C AST context. + /// + static OMPCancellationPointDirective *CreateEmpty(const ASTContext &C, + EmptyShell); + + /// \brief Get cancellation region for the current cancellation point. + OpenMPDirectiveKind getCancelRegion() const { return CancelRegion; } + + static bool classof(const Stmt *T) { + return T->getStmtClass() == OMPCancellationPointDirectiveClass; + } +}; + } // end namespace clang #endif diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 8c96f65548..dc197fcce5 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -7611,6 +7611,12 @@ def err_omp_single_copyprivate_with_nowait : Error< "the 'copyprivate' clause must not be used with the 'nowait' clause">; def note_omp_nowait_clause_here : Note< "'nowait' clause is here">; +def err_omp_wrong_cancel_region : Error< + "one of 'for', 'parallel', 'sections' or 'taskgroup' is expected">; +def err_omp_parent_cancel_region_nowait : Error< + "parent region for 'omp %select{cancellation point/cancel}0' construct cannot be nowait">; +def err_omp_parent_cancel_region_ordered : Error< + "parent region for 'omp %select{cancellation point/cancel}0' construct cannot be ordered">; } // end of OpenMP category let CategoryName = "Related Result Type Issue" in { diff --git a/include/clang/Basic/OpenMPKinds.def b/include/clang/Basic/OpenMPKinds.def index c63c05b01c..892c992b09 100644 --- a/include/clang/Basic/OpenMPKinds.def +++ b/include/clang/Basic/OpenMPKinds.def @@ -97,6 +97,7 @@ OPENMP_DIRECTIVE_EXT(parallel_for, "parallel for") OPENMP_DIRECTIVE_EXT(parallel_for_simd, "parallel for simd") OPENMP_DIRECTIVE_EXT(parallel_sections, "parallel sections") OPENMP_DIRECTIVE_EXT(for_simd, "for simd") +OPENMP_DIRECTIVE_EXT(cancellation_point, "cancellation point") // OpenMP clauses. OPENMP_CLAUSE(if, OMPIfClause) diff --git a/include/clang/Basic/StmtNodes.td b/include/clang/Basic/StmtNodes.td index 675e91d866..79f5463d79 100644 --- a/include/clang/Basic/StmtNodes.td +++ b/include/clang/Basic/StmtNodes.td @@ -205,3 +205,4 @@ def OMPOrderedDirective : DStmt; def OMPAtomicDirective : DStmt; def OMPTargetDirective : DStmt; def OMPTeamsDirective : DStmt; +def OMPCancellationPointDirective : DStmt; diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index cad4bcb2ee..07a47484cd 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -7664,12 +7664,10 @@ public: /// /// \returns Statement for finished OpenMP region. StmtResult ActOnOpenMPRegionEnd(StmtResult S, ArrayRef Clauses); - StmtResult ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind, - const DeclarationNameInfo &DirName, - ArrayRef Clauses, - Stmt *AStmt, - SourceLocation StartLoc, - SourceLocation EndLoc); + StmtResult ActOnOpenMPExecutableDirective( + OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, + OpenMPDirectiveKind CancelRegion, ArrayRef Clauses, + Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc); /// \brief Called on well-formed '\#pragma omp parallel' after parsing /// of the associated statement. StmtResult ActOnOpenMPParallelDirective(ArrayRef Clauses, @@ -7775,6 +7773,11 @@ public: StmtResult ActOnOpenMPTeamsDirective(ArrayRef Clauses, Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc); + /// \brief Called on well-formed '\#pragma omp cancellation point'. + StmtResult + ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, + SourceLocation EndLoc, + OpenMPDirectiveKind CancelRegion); OMPClause *ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, diff --git a/include/clang/Serialization/ASTBitCodes.h b/include/clang/Serialization/ASTBitCodes.h index 83185a870a..781c80ef64 100644 --- a/include/clang/Serialization/ASTBitCodes.h +++ b/include/clang/Serialization/ASTBitCodes.h @@ -1397,6 +1397,7 @@ namespace clang { STMT_OMP_TARGET_DIRECTIVE, STMT_OMP_TEAMS_DIRECTIVE, STMT_OMP_TASKGROUP_DIRECTIVE, + STMT_OMP_CANCELLATION_POINT_DIRECTIVE, // ARC EXPR_OBJC_BRIDGED_CAST, // ObjCBridgedCastExpr diff --git a/lib/AST/Stmt.cpp b/lib/AST/Stmt.cpp index 8694cf3e22..911ec5e30e 100644 --- a/lib/AST/Stmt.cpp +++ b/lib/AST/Stmt.cpp @@ -2086,6 +2086,26 @@ OMPTaskgroupDirective *OMPTaskgroupDirective::CreateEmpty(const ASTContext &C, return new (Mem) OMPTaskgroupDirective(); } +OMPCancellationPointDirective *OMPCancellationPointDirective::Create( + const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc, + OpenMPDirectiveKind CancelRegion) { + unsigned Size = llvm::RoundUpToAlignment( + sizeof(OMPCancellationPointDirective), llvm::alignOf()); + void *Mem = C.Allocate(Size); + OMPCancellationPointDirective *Dir = + new (Mem) OMPCancellationPointDirective(StartLoc, EndLoc); + Dir->setCancelRegion(CancelRegion); + return Dir; +} + +OMPCancellationPointDirective * +OMPCancellationPointDirective::CreateEmpty(const ASTContext &C, EmptyShell) { + unsigned Size = llvm::RoundUpToAlignment( + sizeof(OMPCancellationPointDirective), llvm::alignOf()); + void *Mem = C.Allocate(Size); + return new (Mem) OMPCancellationPointDirective(); +} + OMPFlushDirective *OMPFlushDirective::Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc, diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp index a82d104d7a..3e9e9df3fa 100644 --- a/lib/AST/StmtPrinter.cpp +++ b/lib/AST/StmtPrinter.cpp @@ -951,6 +951,12 @@ void StmtPrinter::VisitOMPTeamsDirective(OMPTeamsDirective *Node) { PrintOMPExecutableDirective(Node); } +void StmtPrinter::VisitOMPCancellationPointDirective( + OMPCancellationPointDirective *Node) { + Indent() << "#pragma omp cancellation point " + << getOpenMPDirectiveName(Node->getCancelRegion()); + PrintOMPExecutableDirective(Node); +} //===----------------------------------------------------------------------===// // Expr printing methods. //===----------------------------------------------------------------------===// diff --git a/lib/AST/StmtProfile.cpp b/lib/AST/StmtProfile.cpp index 8a48ee267f..7d1d48be79 100644 --- a/lib/AST/StmtProfile.cpp +++ b/lib/AST/StmtProfile.cpp @@ -537,6 +537,11 @@ void StmtProfiler::VisitOMPTeamsDirective(const OMPTeamsDirective *S) { VisitOMPExecutableDirective(S); } +void StmtProfiler::VisitOMPCancellationPointDirective( + const OMPCancellationPointDirective *S) { + VisitOMPExecutableDirective(S); +} + void StmtProfiler::VisitExpr(const Expr *S) { VisitStmt(S); } diff --git a/lib/Basic/OpenMPKinds.cpp b/lib/Basic/OpenMPKinds.cpp index e4694b46cd..0e9d0c0d5f 100644 --- a/lib/Basic/OpenMPKinds.cpp +++ b/lib/Basic/OpenMPKinds.cpp @@ -347,6 +347,7 @@ bool clang::isAllowedClauseForDirective(OpenMPDirectiveKind DKind, case OMPD_barrier: case OMPD_taskwait: case OMPD_taskgroup: + case OMPD_cancellation_point: case OMPD_ordered: break; } diff --git a/lib/CodeGen/CGStmt.cpp b/lib/CodeGen/CGStmt.cpp index f10e819ec4..81b115c34b 100644 --- a/lib/CodeGen/CGStmt.cpp +++ b/lib/CodeGen/CGStmt.cpp @@ -240,6 +240,9 @@ void CodeGenFunction::EmitStmt(const Stmt *S) { case Stmt::OMPTeamsDirectiveClass: EmitOMPTeamsDirective(cast(*S)); break; + case Stmt::OMPCancellationPointDirectiveClass: + EmitOMPCancellationPointDirective(cast(*S)); + break; } } diff --git a/lib/CodeGen/CGStmtOpenMP.cpp b/lib/CodeGen/CGStmtOpenMP.cpp index 10817ec002..8b4902fac0 100644 --- a/lib/CodeGen/CGStmtOpenMP.cpp +++ b/lib/CodeGen/CGStmtOpenMP.cpp @@ -2097,3 +2097,10 @@ void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &) { void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &) { llvm_unreachable("CodeGen for 'omp teams' is not supported yet."); } + +void CodeGenFunction::EmitOMPCancellationPointDirective( + const OMPCancellationPointDirective &S) { + llvm_unreachable( + "CodeGen for 'omp cancellation point' is not supported yet."); +} + diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index d98c7ae631..e396d6adb6 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -2202,6 +2202,8 @@ public: void EmitOMPAtomicDirective(const OMPAtomicDirective &S); void EmitOMPTargetDirective(const OMPTargetDirective &S); void EmitOMPTeamsDirective(const OMPTeamsDirective &S); + void + EmitOMPCancellationPointDirective(const OMPCancellationPointDirective &S); /// \brief Emit inner loop of the worksharing/simd construct. /// diff --git a/lib/Parse/ParseOpenMP.cpp b/lib/Parse/ParseOpenMP.cpp index 38efed8926..b1d8f2fd31 100644 --- a/lib/Parse/ParseOpenMP.cpp +++ b/lib/Parse/ParseOpenMP.cpp @@ -30,24 +30,39 @@ static OpenMPDirectiveKind ParseOpenMPDirectiveKind(Parser &P) { // E.g.: OMPD_for OMPD_simd ===> OMPD_for_simd // TODO: add other combined directives in topological order. const OpenMPDirectiveKind F[][3] = { - { OMPD_for, OMPD_simd, OMPD_for_simd }, - { OMPD_parallel, OMPD_for, OMPD_parallel_for }, - { OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd }, - { OMPD_parallel, OMPD_sections, OMPD_parallel_sections } - }; + {OMPD_unknown /*cancellation*/, OMPD_unknown /*point*/, + OMPD_cancellation_point}, + {OMPD_for, OMPD_simd, OMPD_for_simd}, + {OMPD_parallel, OMPD_for, OMPD_parallel_for}, + {OMPD_parallel_for, OMPD_simd, OMPD_parallel_for_simd}, + {OMPD_parallel, OMPD_sections, OMPD_parallel_sections}}; auto Tok = P.getCurToken(); auto DKind = Tok.isAnnotation() ? OMPD_unknown : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok)); + bool TokenMatched = false; for (unsigned i = 0; i < llvm::array_lengthof(F); ++i) { - if (DKind == F[i][0]) { + if (!Tok.isAnnotation() && DKind == OMPD_unknown) { + TokenMatched = + (i == 0) && + !P.getPreprocessor().getSpelling(Tok).compare("cancellation"); + } else { + TokenMatched = DKind == F[i][0] && DKind != OMPD_unknown; + } + if (TokenMatched) { Tok = P.getPreprocessor().LookAhead(0); auto SDKind = Tok.isAnnotation() ? OMPD_unknown : getOpenMPDirectiveKind(P.getPreprocessor().getSpelling(Tok)); - if (SDKind == F[i][1]) { + if (!Tok.isAnnotation() && DKind == OMPD_unknown) { + TokenMatched = + (i == 0) && !P.getPreprocessor().getSpelling(Tok).compare("point"); + } else { + TokenMatched = SDKind == F[i][1] && SDKind != OMPD_unknown; + } + if (TokenMatched) { P.ConsumeToken(); DKind = F[i][2]; } @@ -110,6 +125,7 @@ Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirective() { case OMPD_atomic: case OMPD_target: case OMPD_teams: + case OMPD_cancellation_point: Diag(Tok, diag::err_omp_unexpected_directive) << getOpenMPDirectiveName(DKind); break; @@ -145,6 +161,7 @@ Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) { Scope::FnScope | Scope::DeclScope | Scope::OpenMPDirectiveScope; SourceLocation Loc = ConsumeToken(), EndLoc; auto DKind = ParseOpenMPDirectiveKind(*this); + OpenMPDirectiveKind CancelRegion = OMPD_unknown; // Name of critical directive. DeclarationNameInfo DirName; StmtResult Directive = StmtError(); @@ -178,6 +195,7 @@ Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) { case OMPD_taskyield: case OMPD_barrier: case OMPD_taskwait: + case OMPD_cancellation_point: if (!StandAloneAllowed) { Diag(Tok, diag::err_omp_immediate_directive) << getOpenMPDirectiveName(DKind); @@ -217,6 +235,10 @@ Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) { } T.consumeClose(); } + } else if (DKind == OMPD_cancellation_point) { + CancelRegion = ParseOpenMPDirectiveKind(*this); + if (Tok.isNot(tok::annot_pragma_openmp_end)) + ConsumeToken(); } if (isOpenMPLoopDirective(DKind)) @@ -267,7 +289,8 @@ Parser::ParseOpenMPDeclarativeOrExecutableDirective(bool StandAloneAllowed) { } if (CreateDirective) Directive = Actions.ActOnOpenMPExecutableDirective( - DKind, DirName, Clauses, AssociatedStmt.get(), Loc, EndLoc); + DKind, DirName, CancelRegion, Clauses, AssociatedStmt.get(), Loc, + EndLoc); // Exit scope. Actions.EndOpenMPDSABlock(Directive.get()); diff --git a/lib/Sema/SemaOpenMP.cpp b/lib/Sema/SemaOpenMP.cpp index 7c1746f90e..8d6d01e6eb 100644 --- a/lib/Sema/SemaOpenMP.cpp +++ b/lib/Sema/SemaOpenMP.cpp @@ -95,19 +95,20 @@ private: Scope *CurScope; SourceLocation ConstructLoc; bool OrderedRegion; + bool NowaitRegion; unsigned CollapseNumber; SourceLocation InnerTeamsRegionLoc; SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, Scope *CurScope, SourceLocation Loc) : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified), Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope), - ConstructLoc(Loc), OrderedRegion(false), CollapseNumber(1), - InnerTeamsRegionLoc() {} + ConstructLoc(Loc), OrderedRegion(false), NowaitRegion(false), + CollapseNumber(1), InnerTeamsRegionLoc() {} SharingMapTy() : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified), Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr), - ConstructLoc(), OrderedRegion(false), CollapseNumber(1), - InnerTeamsRegionLoc() {} + ConstructLoc(), OrderedRegion(false), NowaitRegion(false), + CollapseNumber(1), InnerTeamsRegionLoc() {} }; typedef SmallVector StackTy; @@ -232,6 +233,17 @@ public: return Stack[Stack.size() - 2].OrderedRegion; return false; } + /// \brief Marks current region as nowait (it has a 'nowait' clause). + void setNowaitRegion(bool IsNowait = true) { + Stack.back().NowaitRegion = IsNowait; + } + /// \brief Returns true, if parent region is nowait (has associated + /// 'nowait' clause), false - otherwise. + bool isParentNowaitRegion() const { + if (Stack.size() > 2) + return Stack[Stack.size() - 2].NowaitRegion; + return false; + } /// \brief Set collapse value for the region. void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; } @@ -1312,6 +1324,7 @@ void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) { case OMPD_taskyield: case OMPD_barrier: case OMPD_taskwait: + case OMPD_cancellation_point: case OMPD_flush: llvm_unreachable("OpenMP Directive is not allowed"); case OMPD_unknown: @@ -1352,6 +1365,7 @@ StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S, static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, OpenMPDirectiveKind CurrentRegion, const DeclarationNameInfo &CurrentName, + OpenMPDirectiveKind CancelRegion, SourceLocation StartLoc) { // Allowed nesting of constructs // +------------------+-----------------+------------------------------------+ @@ -1379,6 +1393,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | parallel | atomic | * | // | parallel | target | * | // | parallel | teams | + | + // | parallel | cancellation | | + // | | point | ! | // +------------------+-----------------+------------------------------------+ // | for | parallel | * | // | for | for | + | @@ -1402,6 +1418,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | for | atomic | * | // | for | target | * | // | for | teams | + | + // | for | cancellation | | + // | | point | ! | // +------------------+-----------------+------------------------------------+ // | master | parallel | * | // | master | for | + | @@ -1425,6 +1443,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | master | atomic | * | // | master | target | * | // | master | teams | + | + // | master | cancellation | | + // | | point | | // +------------------+-----------------+------------------------------------+ // | critical | parallel | * | // | critical | for | + | @@ -1447,6 +1467,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | critical | atomic | * | // | critical | target | * | // | critical | teams | + | + // | critical | cancellation | | + // | | point | | // +------------------+-----------------+------------------------------------+ // | simd | parallel | | // | simd | for | | @@ -1470,6 +1492,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | simd | atomic | | // | simd | target | | // | simd | teams | | + // | simd | cancellation | | + // | | point | | // +------------------+-----------------+------------------------------------+ // | for simd | parallel | | // | for simd | for | | @@ -1493,6 +1517,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | for simd | atomic | | // | for simd | target | | // | for simd | teams | | + // | for simd | cancellation | | + // | | point | | // +------------------+-----------------+------------------------------------+ // | parallel for simd| parallel | | // | parallel for simd| for | | @@ -1516,6 +1542,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | parallel for simd| atomic | | // | parallel for simd| target | | // | parallel for simd| teams | | + // | parallel for simd| cancellation | | + // | | point | | // +------------------+-----------------+------------------------------------+ // | sections | parallel | * | // | sections | for | + | @@ -1539,6 +1567,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | sections | atomic | * | // | sections | target | * | // | sections | teams | + | + // | sections | cancellation | | + // | | point | ! | // +------------------+-----------------+------------------------------------+ // | section | parallel | * | // | section | for | + | @@ -1562,6 +1592,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | section | atomic | * | // | section | target | * | // | section | teams | + | + // | section | cancellation | | + // | | point | ! | // +------------------+-----------------+------------------------------------+ // | single | parallel | * | // | single | for | + | @@ -1585,6 +1617,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | single | atomic | * | // | single | target | * | // | single | teams | + | + // | single | cancellation | | + // | | point | | // +------------------+-----------------+------------------------------------+ // | parallel for | parallel | * | // | parallel for | for | + | @@ -1608,6 +1642,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | parallel for | atomic | * | // | parallel for | target | * | // | parallel for | teams | + | + // | parallel for | cancellation | | + // | | point | ! | // +------------------+-----------------+------------------------------------+ // | parallel sections| parallel | * | // | parallel sections| for | + | @@ -1631,6 +1667,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | parallel sections| atomic | * | // | parallel sections| target | * | // | parallel sections| teams | + | + // | parallel sections| cancellation | | + // | | point | ! | // +------------------+-----------------+------------------------------------+ // | task | parallel | * | // | task | for | + | @@ -1654,6 +1692,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | task | atomic | * | // | task | target | * | // | task | teams | + | + // | task | cancellation | | + // | | point | ! | // +------------------+-----------------+------------------------------------+ // | ordered | parallel | * | // | ordered | for | + | @@ -1677,6 +1717,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | ordered | atomic | * | // | ordered | target | * | // | ordered | teams | + | + // | ordered | cancellation | | + // | | point | | // +------------------+-----------------+------------------------------------+ // | atomic | parallel | | // | atomic | for | | @@ -1700,6 +1742,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | atomic | atomic | | // | atomic | target | | // | atomic | teams | | + // | atomic | cancellation | | + // | | point | | // +------------------+-----------------+------------------------------------+ // | target | parallel | * | // | target | for | * | @@ -1723,6 +1767,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | target | atomic | * | // | target | target | * | // | target | teams | * | + // | target | cancellation | | + // | | point | | // +------------------+-----------------+------------------------------------+ // | teams | parallel | * | // | teams | for | + | @@ -1746,6 +1792,8 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // | teams | atomic | + | // | teams | target | + | // | teams | teams | + | + // | teams | cancellation | | + // | | point | | // +------------------+-----------------+------------------------------------+ if (Stack->getCurScope()) { auto ParentRegion = Stack->getParentDirective(); @@ -1787,7 +1835,20 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, // called from OpenMP regions with the required preconditions). if (ParentRegion == OMPD_unknown) return false; - if (CurrentRegion == OMPD_master) { + if (CurrentRegion == OMPD_cancellation_point) { + // OpenMP [2.16, Nesting of Regions] + // A cancellation point construct for which construct-type-clause is + // taskgroup must be nested inside a task construct. A cancellation + // point construct for which construct-type-clause is not taskgroup must + // be closely nested inside an OpenMP construct that matches the type + // specified in construct-type-clause. + NestingProhibited = + !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) || + (CancelRegion == OMPD_for && ParentRegion == OMPD_for) || + (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) || + (CancelRegion == OMPD_sections && + (ParentRegion == OMPD_section || ParentRegion == OMPD_sections))); + } else if (CurrentRegion == OMPD_master) { // OpenMP [2.16, Nesting of Regions] // A master region may not be closely nested inside a worksharing, // atomic, or explicit task region. @@ -1877,14 +1938,13 @@ static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, return false; } -StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind, - const DeclarationNameInfo &DirName, - ArrayRef Clauses, - Stmt *AStmt, - SourceLocation StartLoc, - SourceLocation EndLoc) { +StmtResult Sema::ActOnOpenMPExecutableDirective( + OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName, + OpenMPDirectiveKind CancelRegion, ArrayRef Clauses, + Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { StmtResult Res = StmtError(); - if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc)) + if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion, + StartLoc)) return StmtError(); llvm::SmallVector ClausesWithImplicit; @@ -2018,6 +2078,13 @@ StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind, Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); break; + case OMPD_cancellation_point: + assert(ClausesWithImplicit.empty() && + "No clauses are allowed for 'omp cancellation point' directive"); + assert(AStmt == nullptr && "No associated statement allowed for 'omp " + "cancellation point' directive"); + Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion); + break; case OMPD_threadprivate: llvm_unreachable("OpenMP Directive is not allowed"); case OMPD_unknown: @@ -4204,6 +4271,28 @@ StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef Clauses, return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); } +StmtResult +Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc, + SourceLocation EndLoc, + OpenMPDirectiveKind CancelRegion) { + if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for && + CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) { + Diag(StartLoc, diag::err_omp_wrong_cancel_region) + << getOpenMPDirectiveName(CancelRegion); + return StmtError(); + } + if (DSAStack->isParentNowaitRegion()) { + Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0; + return StmtError(); + } + if (DSAStack->isParentOrderedRegion()) { + Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0; + return StmtError(); + } + return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc, + CancelRegion); +} + OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, SourceLocation StartLoc, SourceLocation LParenLoc, @@ -4722,6 +4811,7 @@ OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, SourceLocation EndLoc) { + DSAStack->setNowaitRegion(); return new (Context) OMPNowaitClause(StartLoc, EndLoc); } diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h index 5d9c35f08d..41ab5a2e9b 100644 --- a/lib/Sema/TreeTransform.h +++ b/lib/Sema/TreeTransform.h @@ -1321,11 +1321,12 @@ public: /// Subclasses may override this routine to provide different behavior. StmtResult RebuildOMPExecutableDirective(OpenMPDirectiveKind Kind, DeclarationNameInfo DirName, + OpenMPDirectiveKind CancelRegion, ArrayRef Clauses, Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) { - return getSema().ActOnOpenMPExecutableDirective(Kind, DirName, Clauses, - AStmt, StartLoc, EndLoc); + return getSema().ActOnOpenMPExecutableDirective( + Kind, DirName, CancelRegion, Clauses, AStmt, StartLoc, EndLoc); } /// \brief Build a new OpenMP 'if' clause. @@ -6715,10 +6716,14 @@ StmtResult TreeTransform::TransformOMPExecutableDirective( DirName = cast(D)->getDirectiveName(); DirName = getDerived().TransformDeclarationNameInfo(DirName); } + OpenMPDirectiveKind CancelRegion = OMPD_unknown; + if (D->getDirectiveKind() == OMPD_cancellation_point) { + CancelRegion = cast(D)->getCancelRegion(); + } return getDerived().RebuildOMPExecutableDirective( - D->getDirectiveKind(), DirName, TClauses, AssociatedStmt.get(), - D->getLocStart(), D->getLocEnd()); + D->getDirectiveKind(), DirName, CancelRegion, TClauses, + AssociatedStmt.get(), D->getLocStart(), D->getLocEnd()); } template @@ -6962,6 +6967,17 @@ TreeTransform::TransformOMPTeamsDirective(OMPTeamsDirective *D) { return Res; } +template +StmtResult TreeTransform::TransformOMPCancellationPointDirective( + OMPCancellationPointDirective *D) { + DeclarationNameInfo DirName; + getDerived().getSema().StartOpenMPDSABlock(OMPD_cancellation_point, DirName, + nullptr, D->getLocStart()); + StmtResult Res = getDerived().TransformOMPExecutableDirective(D); + getDerived().getSema().EndOpenMPDSABlock(Res.get()); + return Res; +} + //===----------------------------------------------------------------------===// // OpenMP clause transformation //===----------------------------------------------------------------------===// diff --git a/lib/Serialization/ASTReaderStmt.cpp b/lib/Serialization/ASTReaderStmt.cpp index f64d856a62..d7975c434f 100644 --- a/lib/Serialization/ASTReaderStmt.cpp +++ b/lib/Serialization/ASTReaderStmt.cpp @@ -2249,6 +2249,13 @@ void ASTStmtReader::VisitOMPTeamsDirective(OMPTeamsDirective *D) { VisitOMPExecutableDirective(D); } +void ASTStmtReader::VisitOMPCancellationPointDirective( + OMPCancellationPointDirective *D) { + VisitStmt(D); + VisitOMPExecutableDirective(D); + D->setCancelRegion(static_cast(Record[Idx++])); +} + //===----------------------------------------------------------------------===// // ASTReader Implementation //===----------------------------------------------------------------------===// @@ -2852,6 +2859,10 @@ Stmt *ASTReader::ReadStmtFromStream(ModuleFile &F) { Context, Record[ASTStmtReader::NumStmtFields], Empty); break; + case STMT_OMP_CANCELLATION_POINT_DIRECTIVE: + S = OMPCancellationPointDirective::CreateEmpty(Context, Empty); + break; + case EXPR_CXX_OPERATOR_CALL: S = new (Context) CXXOperatorCallExpr(Context, Empty); break; diff --git a/lib/Serialization/ASTWriterStmt.cpp b/lib/Serialization/ASTWriterStmt.cpp index 0f35f0ce8a..846acd04d4 100644 --- a/lib/Serialization/ASTWriterStmt.cpp +++ b/lib/Serialization/ASTWriterStmt.cpp @@ -2107,6 +2107,14 @@ void ASTStmtWriter::VisitOMPTeamsDirective(OMPTeamsDirective *D) { Code = serialization::STMT_OMP_TEAMS_DIRECTIVE; } +void ASTStmtWriter::VisitOMPCancellationPointDirective( + OMPCancellationPointDirective *D) { + VisitStmt(D); + VisitOMPExecutableDirective(D); + Record.push_back(D->getCancelRegion()); + Code = serialization::STMT_OMP_CANCELLATION_POINT_DIRECTIVE; +} + //===----------------------------------------------------------------------===// // ASTWriter Implementation //===----------------------------------------------------------------------===// diff --git a/lib/StaticAnalyzer/Core/ExprEngine.cpp b/lib/StaticAnalyzer/Core/ExprEngine.cpp index ef515fb593..e8eba7c593 100644 --- a/lib/StaticAnalyzer/Core/ExprEngine.cpp +++ b/lib/StaticAnalyzer/Core/ExprEngine.cpp @@ -822,6 +822,7 @@ void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred, case Stmt::OMPAtomicDirectiveClass: case Stmt::OMPTargetDirectiveClass: case Stmt::OMPTeamsDirectiveClass: + case Stmt::OMPCancellationPointDirectiveClass: llvm_unreachable("Stmt should not be in analyzer evaluation loop"); case Stmt::ObjCSubscriptRefExprClass: diff --git a/test/OpenMP/cancellation_point_ast_print.cpp b/test/OpenMP/cancellation_point_ast_print.cpp new file mode 100644 index 0000000000..c8b133cbac --- /dev/null +++ b/test/OpenMP/cancellation_point_ast_print.cpp @@ -0,0 +1,47 @@ +// RUN: %clang_cc1 -verify -fopenmp -ast-print %s | FileCheck %s +// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -emit-pch -o %t %s +// RUN: %clang_cc1 -fopenmp -std=c++11 -include-pch %t -fsyntax-only -verify %s -ast-print | FileCheck %s +// expected-no-diagnostics + +#ifndef HEADER +#define HEADER + +int main (int argc, char **argv) { +// CHECK: int main(int argc, char **argv) { +#pragma omp parallel +{ +#pragma omp cancellation point parallel +} +// CHECK: #pragma omp parallel +// CHECK-NEXT: { +// CHECK-NEXT: #pragma omp cancellation point parallel +// CHECK-NEXT: } +#pragma omp sections +{ +#pragma omp cancellation point sections +} +// CHECK-NEXT: #pragma omp sections +// CHECK: { +// CHECK: #pragma omp cancellation point sections +// CHECK: } +#pragma omp for +for (int i = 0; i < argc; ++i) { +#pragma omp cancellation point for +} +// CHECK: #pragma omp for +// CHECK-NEXT: for (int i = 0; i < argc; ++i) { +// CHECK-NEXT: #pragma omp cancellation point for +// CHECK-NEXT: } +#pragma omp task +{ +#pragma omp cancellation point taskgroup +} +// CHECK: #pragma omp task +// CHECK: { +// CHECK: #pragma omp cancellation point taskgroup +// CHECK: } +// CHECK: return argc; + return argc; +} + +#endif diff --git a/test/OpenMP/cancellation_point_messages.cpp b/test/OpenMP/cancellation_point_messages.cpp new file mode 100644 index 0000000000..d25cb6113d --- /dev/null +++ b/test/OpenMP/cancellation_point_messages.cpp @@ -0,0 +1,83 @@ +// RUN: %clang_cc1 -verify -fopenmp -ferror-limit 100 %s + +int main(int argc, char **argv) { +#pragma omp cancellation // expected-error {{expected an OpenMP directive}} +#pragma omp cancellation point // expected-error {{one of 'for', 'parallel', 'sections' or 'taskgroup' is expected}} + ; +#pragma omp cancellation point parallel untied // expected-error {{unexpected OpenMP clause 'untied' in directive '#pragma omp cancellation point'}} +#pragma omp cancellation point unknown // expected-error {{one of 'for', 'parallel', 'sections' or 'taskgroup' is expected}} +#pragma omp cancellation point sections( // expected-warning {{extra tokens at the end of '#pragma omp cancellation point' are ignored}} +#pragma omp cancellation point for, ) // expected-warning {{extra tokens at the end of '#pragma omp cancellation point' are ignored}} +#pragma omp cancellation point taskgroup() // expected-warning {{extra tokens at the end of '#pragma omp cancellation point' are ignored}} +#pragma omp cancellation point parallel, if // expected-warning {{extra tokens at the end of '#pragma omp cancellation point' are ignored}} + if (argc) +#pragma omp cancellation point for // expected-error {{'#pragma omp cancellation point' cannot be an immediate substatement}} + if (argc) { +#pragma omp taskgroup +#pragma omp task +#pragma omp parallel + { +#pragma omp cancellation point taskgroup // expected-error {{region cannot be closely nested inside 'parallel' region}} + } + } +#pragma omp parallel +#pragma omp taskgroup + { +#pragma omp cancellation point taskgroup // expected-error {{region cannot be closely nested inside 'taskgroup' region}} + } +#pragma omp parallel + { +#pragma omp cancellation point for // expected-error {{region cannot be closely nested inside 'parallel' region}} + } +#pragma omp task + { +#pragma omp cancellation point sections // expected-error {{region cannot be closely nested inside 'task' region}} + } +#pragma omp sections + { +#pragma omp cancellation point parallel // expected-error {{region cannot be closely nested inside 'sections' region}} + } + while (argc) +#pragma omp cancellation point for // expected-error {{'#pragma omp cancellation point' cannot be an immediate substatement}} + while (argc) { +#pragma omp cancellation point sections + } + do +#pragma omp cancellation point parallel // expected-error {{'#pragma omp cancellation point' cannot be an immediate substatement}} + while (argc) + ; + do { +#pragma omp cancellation point taskgroup + } while (argc); + switch (argc) +#pragma omp cancellation point parallel // expected-error {{'#pragma omp cancellation point' cannot be an immediate substatement}} + switch (argc) + case 1: +#pragma omp cancellation point sections // expected-error {{'#pragma omp cancellation point' cannot be an immediate substatement}} + switch (argc) + case 1: { +#pragma omp cancellation point for + } + switch (argc) { +#pragma omp cancellation point taskgroup + case 1: +#pragma omp cancellation point parallel // expected-error {{'#pragma omp cancellation point' cannot be an immediate substatement}} + break; + default: { +#pragma omp cancellation point sections + } break; + } + for (;;) +#pragma omp cancellation point for // expected-error {{'#pragma omp cancellation point' cannot be an immediate substatement}} + for (;;) { +#pragma omp cancellation point taskgroup + } +label: +#pragma omp cancellation point parallel // expected-error {{'#pragma omp cancellation point' cannot be an immediate substatement}} +label1 : { +#pragma omp cancellation point sections +} + + return 0; +} + diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index 6a17f57648..2bb5334852 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -1880,6 +1880,8 @@ public: void VisitOMPBarrierDirective(const OMPBarrierDirective *D); void VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *D); void VisitOMPTaskgroupDirective(const OMPTaskgroupDirective *D); + void + VisitOMPCancellationPointDirective(const OMPCancellationPointDirective *D); void VisitOMPFlushDirective(const OMPFlushDirective *D); void VisitOMPOrderedDirective(const OMPOrderedDirective *D); void VisitOMPAtomicDirective(const OMPAtomicDirective *D); @@ -2507,6 +2509,11 @@ void EnqueueVisitor::VisitOMPTeamsDirective(const OMPTeamsDirective *D) { VisitOMPExecutableDirective(D); } +void EnqueueVisitor::VisitOMPCancellationPointDirective( + const OMPCancellationPointDirective *D) { + VisitOMPExecutableDirective(D); +} + void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) { EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S); } @@ -4283,6 +4290,8 @@ CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { return cxstring::createRef("OMPTargetDirective"); case CXCursor_OMPTeamsDirective: return cxstring::createRef("OMPTeamsDirective"); + case CXCursor_OMPCancellationPointDirective: + return cxstring::createRef("OMPCancellationPointDirective"); case CXCursor_OverloadCandidate: return cxstring::createRef("OverloadCandidate"); } diff --git a/tools/libclang/CXCursor.cpp b/tools/libclang/CXCursor.cpp index b8bb28ed85..0c66e43c85 100644 --- a/tools/libclang/CXCursor.cpp +++ b/tools/libclang/CXCursor.cpp @@ -588,6 +588,9 @@ CXCursor cxcursor::MakeCXCursor(const Stmt *S, const Decl *Parent, case Stmt::OMPTeamsDirectiveClass: K = CXCursor_OMPTeamsDirective; break; + case Stmt::OMPCancellationPointDirectiveClass: + K = CXCursor_OMPCancellationPointDirective; + break; } CXCursor C = { K, 0, { Parent, S, TU } };