diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h --- a/clang/include/clang-c/Index.h +++ b/clang/include/clang-c/Index.h @@ -2596,7 +2596,11 @@ */ CXCursor_OMPMetaDirective = 294, - CXCursor_LastStmt = CXCursor_OMPMetaDirective, + /** OpenMP loop directive. + */ + CXCursor_OMPGenericLoopDirective = 295, + + CXCursor_LastStmt = CXCursor_OMPGenericLoopDirective, /** * Cursor that represents the translation unit itself. diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -3023,6 +3023,9 @@ DEF_TRAVERSE_STMT(OMPMaskedDirective, { TRY_TO(TraverseOMPExecutableDirective(S)); }) +DEF_TRAVERSE_STMT(OMPGenericLoopDirective, + { TRY_TO(TraverseOMPExecutableDirective(S)); }) + // OpenMP clauses. template bool RecursiveASTVisitor::TraverseOMPClause(OMPClause *C) { diff --git a/clang/include/clang/AST/StmtOpenMP.h b/clang/include/clang/AST/StmtOpenMP.h --- a/clang/include/clang/AST/StmtOpenMP.h +++ b/clang/include/clang/AST/StmtOpenMP.h @@ -1144,7 +1144,7 @@ if (isOpenMPLoopBoundSharingDirective(Kind)) return CombinedDistributeEnd; if (isOpenMPWorksharingDirective(Kind) || isOpenMPTaskLoopDirective(Kind) || - isOpenMPDistributeDirective(Kind)) + isOpenMPGenericLoopDirective(Kind) || isOpenMPDistributeDirective(Kind)) return WorksharingEnd; return DefaultEnd; } @@ -1176,6 +1176,7 @@ } void setIsLastIterVariable(Expr *IL) { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1183,6 +1184,7 @@ } void setLowerBoundVariable(Expr *LB) { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1190,6 +1192,7 @@ } void setUpperBoundVariable(Expr *UB) { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1197,6 +1200,7 @@ } void setStrideVariable(Expr *ST) { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1204,6 +1208,7 @@ } void setEnsureUpperBound(Expr *EUB) { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1211,6 +1216,7 @@ } void setNextLowerBound(Expr *NLB) { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1218,6 +1224,7 @@ } void setNextUpperBound(Expr *NUB) { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1225,6 +1232,7 @@ } void setNumIterations(Expr *NI) { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1327,6 +1335,7 @@ Stmt *getPreInits() { return Data->getChildren()[PreInitsOffset]; } Expr *getIsLastIterVariable() const { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1334,6 +1343,7 @@ } Expr *getLowerBoundVariable() const { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1341,6 +1351,7 @@ } Expr *getUpperBoundVariable() const { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1348,6 +1359,7 @@ } Expr *getStrideVariable() const { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1355,6 +1367,7 @@ } Expr *getEnsureUpperBound() const { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1362,6 +1375,7 @@ } Expr *getNextLowerBound() const { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1369,6 +1383,7 @@ } Expr *getNextUpperBound() const { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1376,6 +1391,7 @@ } Expr *getNumIterations() const { assert((isOpenMPWorksharingDirective(getDirectiveKind()) || + isOpenMPGenericLoopDirective(getDirectiveKind()) || isOpenMPTaskLoopDirective(getDirectiveKind()) || isOpenMPDistributeDirective(getDirectiveKind())) && "expected worksharing loop directive"); @@ -1509,6 +1525,7 @@ T->getStmtClass() == OMPTaskLoopSimdDirectiveClass || T->getStmtClass() == OMPMasterTaskLoopDirectiveClass || T->getStmtClass() == OMPMasterTaskLoopSimdDirectiveClass || + T->getStmtClass() == OMPGenericLoopDirectiveClass || T->getStmtClass() == OMPParallelMasterTaskLoopDirectiveClass || T->getStmtClass() == OMPParallelMasterTaskLoopSimdDirectiveClass || T->getStmtClass() == OMPDistributeDirectiveClass || @@ -5461,6 +5478,69 @@ } }; +/// This represents '#pragma omp loop' directive. +/// +/// \code +/// #pragma omp loop private(a,b) binding(parallel) order(concurrent) +/// \endcode +/// In this example directive '#pragma omp loop' has +/// clauses 'private' with the variables 'a' and 'b', 'binding' with +/// modifier 'parallel' and 'order(concurrent). +/// +class OMPGenericLoopDirective final : public OMPLoopDirective { + friend class ASTStmtReader; + friend class OMPExecutableDirective; + /// Build directive with the given start and end location. + /// + /// \param StartLoc Starting location of the directive kind. + /// \param EndLoc Ending location of the directive. + /// \param CollapsedNum Number of collapsed nested loops. + /// + OMPGenericLoopDirective(SourceLocation StartLoc, SourceLocation EndLoc, + unsigned CollapsedNum) + : OMPLoopDirective(OMPGenericLoopDirectiveClass, llvm::omp::OMPD_loop, + StartLoc, EndLoc, CollapsedNum) {} + + /// Build an empty directive. + /// + /// \param CollapsedNum Number of collapsed nested loops. + /// + explicit OMPGenericLoopDirective(unsigned CollapsedNum) + : OMPLoopDirective(OMPGenericLoopDirectiveClass, llvm::omp::OMPD_loop, + SourceLocation(), SourceLocation(), CollapsedNum) {} + +public: + /// Creates directive with a list of \p Clauses. + /// + /// \param C AST context. + /// \param StartLoc Starting location of the directive kind. + /// \param EndLoc Ending Location of the directive. + /// \param CollapsedNum Number of collapsed loops. + /// \param Clauses List of clauses. + /// \param AssociatedStmt Statement, associated with the directive. + /// \param Exprs Helper expressions for CodeGen. + /// + static OMPGenericLoopDirective * + Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc, + unsigned CollapsedNum, ArrayRef Clauses, + Stmt *AssociatedStmt, const HelperExprs &Exprs); + + /// Creates an empty directive with a place for \a NumClauses clauses. + /// + /// \param C AST context. + /// \param NumClauses Number of clauses. + /// \param CollapsedNum Number of collapsed nested loops. + /// + static OMPGenericLoopDirective *CreateEmpty(const ASTContext &C, + unsigned NumClauses, + unsigned CollapsedNum, + EmptyShell); + + static bool classof(const Stmt *T) { + return T->getStmtClass() == OMPGenericLoopDirectiveClass; + } +}; + } // end namespace clang #endif diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -10803,6 +10803,9 @@ : Note<"jump bypasses OpenMP structured block">; def note_omp_exits_structured_block : Note<"jump exits scope of OpenMP structured block">; +def err_omp_lastprivate_loop_var_non_loop_iteration : Error< + "only loop iteration variables are allowed in 'lastprivate' clause in " + "'omp loop' directives">; def err_omp_interop_variable_expected : Error< "expected%select{| non-const}0 variable of type 'omp_interop_t'">; def err_omp_interop_variable_wrong_type : Error< diff --git a/clang/include/clang/Basic/OpenMPKinds.h b/clang/include/clang/Basic/OpenMPKinds.h --- a/clang/include/clang/Basic/OpenMPKinds.h +++ b/clang/include/clang/Basic/OpenMPKinds.h @@ -253,6 +253,13 @@ /// otherwise - false. bool isOpenMPNestingDistributeDirective(OpenMPDirectiveKind DKind); +/// Checks if the specified directive constitutes a 'loop' directive in the +/// outermost nest. For example, 'omp teams loop' or 'omp loop'. +/// \param DKind Specified directive. +/// \return true - the directive has loop on the outermost nest. +/// otherwise - false. +bool isOpenMPGenericLoopDirective(OpenMPDirectiveKind DKind); + /// Checks if the specified clause is one of private clauses like /// 'private', 'firstprivate', 'reduction' etc.. /// \param Kind Clause kind. diff --git a/clang/include/clang/Basic/StmtNodes.td b/clang/include/clang/Basic/StmtNodes.td --- a/clang/include/clang/Basic/StmtNodes.td +++ b/clang/include/clang/Basic/StmtNodes.td @@ -282,3 +282,4 @@ def OMPInteropDirective : StmtNode; def OMPDispatchDirective : StmtNode; def OMPMaskedDirective : StmtNode; +def OMPGenericLoopDirective : StmtNode; diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -10940,6 +10940,12 @@ Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc); + /// Called on well-formed '\#pragma omp loop' after parsing of the + /// associated statement. + StmtResult ActOnOpenMPGenericLoopDirective( + ArrayRef Clauses, Stmt *AStmt, SourceLocation StartLoc, + SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA); + /// Checks correctness of linear modifiers. bool CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind, SourceLocation LinLoc); diff --git a/clang/include/clang/Serialization/ASTBitCodes.h b/clang/include/clang/Serialization/ASTBitCodes.h --- a/clang/include/clang/Serialization/ASTBitCodes.h +++ b/clang/include/clang/Serialization/ASTBitCodes.h @@ -1957,6 +1957,7 @@ STMT_OMP_INTEROP_DIRECTIVE, STMT_OMP_DISPATCH_DIRECTIVE, STMT_OMP_MASKED_DIRECTIVE, + STMT_OMP_GENERIC_LOOP_DIRECTIVE, EXPR_OMP_ARRAY_SECTION, EXPR_OMP_ARRAY_SHAPING, EXPR_OMP_ITERATOR, diff --git a/clang/lib/AST/StmtOpenMP.cpp b/clang/lib/AST/StmtOpenMP.cpp --- a/clang/lib/AST/StmtOpenMP.cpp +++ b/clang/lib/AST/StmtOpenMP.cpp @@ -2086,3 +2086,45 @@ return createEmptyDirective(C, NumClauses, /*HasAssociatedStmt=*/true); } + +OMPGenericLoopDirective *OMPGenericLoopDirective::Create( + const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc, + unsigned CollapsedNum, ArrayRef Clauses, Stmt *AssociatedStmt, + const HelperExprs &Exprs) { + auto *Dir = createDirective( + C, Clauses, AssociatedStmt, numLoopChildren(CollapsedNum, OMPD_loop), + StartLoc, EndLoc, CollapsedNum); + Dir->setIterationVariable(Exprs.IterationVarRef); + Dir->setLastIteration(Exprs.LastIteration); + Dir->setCalcLastIteration(Exprs.CalcLastIteration); + Dir->setPreCond(Exprs.PreCond); + Dir->setCond(Exprs.Cond); + Dir->setInit(Exprs.Init); + Dir->setInc(Exprs.Inc); + Dir->setIsLastIterVariable(Exprs.IL); + Dir->setLowerBoundVariable(Exprs.LB); + Dir->setUpperBoundVariable(Exprs.UB); + Dir->setStrideVariable(Exprs.ST); + Dir->setEnsureUpperBound(Exprs.EUB); + Dir->setNextLowerBound(Exprs.NLB); + Dir->setNextUpperBound(Exprs.NUB); + Dir->setNumIterations(Exprs.NumIterations); + Dir->setCounters(Exprs.Counters); + Dir->setPrivateCounters(Exprs.PrivateCounters); + Dir->setInits(Exprs.Inits); + Dir->setUpdates(Exprs.Updates); + Dir->setFinals(Exprs.Finals); + Dir->setDependentCounters(Exprs.DependentCounters); + Dir->setDependentInits(Exprs.DependentInits); + Dir->setFinalsConditions(Exprs.FinalsConditions); + Dir->setPreInits(Exprs.PreInits); + return Dir; +} + +OMPGenericLoopDirective * +OMPGenericLoopDirective::CreateEmpty(const ASTContext &C, unsigned NumClauses, + unsigned CollapsedNum, EmptyShell) { + return createEmptyDirective( + C, NumClauses, /*HasAssociatedStmt=*/true, + numLoopChildren(CollapsedNum, OMPD_loop), CollapsedNum); +} diff --git a/clang/lib/AST/StmtPrinter.cpp b/clang/lib/AST/StmtPrinter.cpp --- a/clang/lib/AST/StmtPrinter.cpp +++ b/clang/lib/AST/StmtPrinter.cpp @@ -1000,6 +1000,11 @@ PrintOMPExecutableDirective(Node); } +void StmtPrinter::VisitOMPGenericLoopDirective(OMPGenericLoopDirective *Node) { + Indent() << "#pragma omp loop"; + PrintOMPExecutableDirective(Node); +} + //===----------------------------------------------------------------------===// // Expr printing methods. //===----------------------------------------------------------------------===// diff --git a/clang/lib/AST/StmtProfile.cpp b/clang/lib/AST/StmtProfile.cpp --- a/clang/lib/AST/StmtProfile.cpp +++ b/clang/lib/AST/StmtProfile.cpp @@ -1190,6 +1190,11 @@ VisitOMPExecutableDirective(S); } +void StmtProfiler::VisitOMPGenericLoopDirective( + const OMPGenericLoopDirective *S) { + VisitOMPLoopDirective(S); +} + void StmtProfiler::VisitExpr(const Expr *S) { VisitStmt(S); } diff --git a/clang/lib/Basic/OpenMPKinds.cpp b/clang/lib/Basic/OpenMPKinds.cpp --- a/clang/lib/Basic/OpenMPKinds.cpp +++ b/clang/lib/Basic/OpenMPKinds.cpp @@ -474,7 +474,7 @@ DKind == OMPD_target_teams_distribute_parallel_for || DKind == OMPD_target_teams_distribute_parallel_for_simd || DKind == OMPD_target_teams_distribute_simd || DKind == OMPD_tile || - DKind == OMPD_unroll; + DKind == OMPD_unroll || DKind == OMPD_loop; } bool clang::isOpenMPWorksharingDirective(OpenMPDirectiveKind DKind) { @@ -577,6 +577,10 @@ Kind == OMPD_target_teams_distribute_simd; } +bool clang::isOpenMPGenericLoopDirective(OpenMPDirectiveKind Kind) { + return Kind == OMPD_loop; +} + bool clang::isOpenMPPrivate(OpenMPClauseKind Kind) { return Kind == OMPC_private || Kind == OMPC_firstprivate || Kind == OMPC_lastprivate || Kind == OMPC_linear || @@ -675,6 +679,10 @@ CaptureRegions.push_back(OMPD_teams); CaptureRegions.push_back(OMPD_parallel); break; + case OMPD_loop: + // TODO: 'loop' may require different capture regions depending on the bind + // clause or the parent directive when there is no bind clause. Use + // OMPD_unknown for now. case OMPD_simd: case OMPD_for: case OMPD_for_simd: diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp --- a/clang/lib/CodeGen/CGStmt.cpp +++ b/clang/lib/CodeGen/CGStmt.cpp @@ -393,6 +393,9 @@ case Stmt::OMPMaskedDirectiveClass: EmitOMPMaskedDirective(cast(*S)); break; + case Stmt::OMPGenericLoopDirectiveClass: + EmitOMPGenericLoopDirective(cast(*S)); + break; } } diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp --- a/clang/lib/CodeGen/CGStmtOpenMP.cpp +++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp @@ -7239,6 +7239,16 @@ CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); } +void CodeGenFunction::EmitOMPGenericLoopDirective( + const OMPGenericLoopDirective &S) { + // Unimplemented, just inline the underlying statement for now. + auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { + CGF.EmitStmt(cast(S.getAssociatedStmt())->getCapturedStmt()); + }; + OMPLexicalScope Scope(*this, S, OMPD_unknown); + CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_loop, CodeGen); +} + void CodeGenFunction::EmitSimpleOMPExecutableDirective( const OMPExecutableDirective &D) { if (const auto *SD = dyn_cast(&D)) { diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -3529,6 +3529,7 @@ const OMPTargetTeamsDistributeParallelForSimdDirective &S); void EmitOMPTargetTeamsDistributeSimdDirective( const OMPTargetTeamsDistributeSimdDirective &S); + void EmitOMPGenericLoopDirective(const OMPGenericLoopDirective &S); /// Emit device code for the target directive. static void EmitOMPTargetDeviceFunction(CodeGenModule &CGM, diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp --- a/clang/lib/Parse/ParseOpenMP.cpp +++ b/clang/lib/Parse/ParseOpenMP.cpp @@ -2375,6 +2375,7 @@ case OMPD_dispatch: case OMPD_masked: case OMPD_metadirective: + case OMPD_loop: Diag(Tok, diag::err_omp_unexpected_directive) << 1 << getOpenMPDirectiveName(DKind); break; @@ -2724,6 +2725,7 @@ case OMPD_target_data: case OMPD_target_parallel: case OMPD_target_parallel_for: + case OMPD_loop: case OMPD_taskloop: case OMPD_taskloop_simd: case OMPD_master_taskloop: diff --git a/clang/lib/Sema/SemaExceptionSpec.cpp b/clang/lib/Sema/SemaExceptionSpec.cpp --- a/clang/lib/Sema/SemaExceptionSpec.cpp +++ b/clang/lib/Sema/SemaExceptionSpec.cpp @@ -1497,6 +1497,7 @@ case Stmt::OMPDispatchDirectiveClass: case Stmt::OMPMaskedDirectiveClass: case Stmt::OMPMetaDirectiveClass: + case Stmt::OMPGenericLoopDirectiveClass: case Stmt::ReturnStmtClass: case Stmt::SEHExceptStmtClass: case Stmt::SEHFinallyStmtClass: diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp --- a/clang/lib/Sema/SemaOpenMP.cpp +++ b/clang/lib/Sema/SemaOpenMP.cpp @@ -4013,6 +4013,9 @@ case OMPD_tile: case OMPD_unroll: break; + case OMPD_loop: + // TODO: 'loop' may require additional parameters depending on the binding. + // Treat similar to OMPD_simd/OMPD_for for now. case OMPD_simd: case OMPD_for: case OMPD_for_simd: @@ -4788,6 +4791,7 @@ // A masked region may not be closely nested inside a worksharing, loop, // atomic, task, or taskloop region. NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || + isOpenMPGenericLoopDirective(ParentRegion) || isOpenMPTaskingDirective(ParentRegion); } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) { // OpenMP [2.16, Nesting of Regions] @@ -4821,6 +4825,7 @@ // task, taskloop, critical, ordered, atomic, or masked region. NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || + isOpenMPGenericLoopDirective(ParentRegion) || isOpenMPTaskingDirective(ParentRegion) || ParentRegion == OMPD_master || ParentRegion == OMPD_masked || ParentRegion == OMPD_parallel_master || @@ -4834,6 +4839,7 @@ // critical, ordered, atomic, or masked region. NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) || + isOpenMPGenericLoopDirective(ParentRegion) || isOpenMPTaskingDirective(ParentRegion) || ParentRegion == OMPD_master || ParentRegion == OMPD_masked || ParentRegion == OMPD_parallel_master || @@ -4879,12 +4885,16 @@ !isOpenMPTargetExecutionDirective(CurrentRegion) && !isOpenMPTargetDataManagementDirective(CurrentRegion) && (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) { - // OpenMP [2.16, Nesting of Regions] - // distribute, parallel, parallel sections, parallel workshare, and the - // parallel loop and parallel loop SIMD constructs are the only OpenMP - // constructs that can be closely nested in the teams region. + // OpenMP [5.1, 2.22, Nesting of Regions] + // distribute, distribute simd, distribute parallel worksharing-loop, + // distribute parallel worksharing-loop SIMD, loop, parallel regions, + // including any parallel regions arising from combined constructs, + // omp_get_num_teams() regions, and omp_get_team_num() regions are the + // only OpenMP regions that may be strictly nested inside the teams + // region. NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) && - !isOpenMPDistributeDirective(CurrentRegion); + !isOpenMPDistributeDirective(CurrentRegion) && + CurrentRegion != OMPD_loop; Recommend = ShouldBeInParallelRegion; } if (!NestingProhibited && @@ -6231,6 +6241,10 @@ Res = ActOnOpenMPDispatchDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); break; + case OMPD_loop: + Res = ActOnOpenMPGenericLoopDirective(ClausesWithImplicit, AStmt, StartLoc, + EndLoc, VarsWithInheritedDSA); + break; case OMPD_declare_target: case OMPD_end_declare_target: case OMPD_threadprivate: @@ -8817,6 +8831,7 @@ ResultIterSpaces[CurrentNestedLoopCount].NumIterations = ISC.buildNumIterations(DSA.getCurScope(), ResultIterSpaces, (isOpenMPWorksharingDirective(DKind) || + isOpenMPGenericLoopDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) || isOpenMPLoopTransformationDirective(DKind)), @@ -9300,6 +9315,7 @@ ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB; if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) || + isOpenMPGenericLoopDirective(DKind) || isOpenMPLoopTransformationDirective(DKind)) { // Lower bound variable, initialized with zero. VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb"); @@ -9399,6 +9415,7 @@ VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv"); IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc); Expr *RHS = (isOpenMPWorksharingDirective(DKind) || + isOpenMPGenericLoopDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) || isOpenMPLoopTransformationDirective(DKind)) @@ -9410,6 +9427,7 @@ if (isOpenMPLoopBoundSharingDirective(DKind)) { Expr *CombRHS = (isOpenMPWorksharingDirective(DKind) || + isOpenMPGenericLoopDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)) ? CombLB.get() @@ -9441,6 +9459,7 @@ } ExprResult Cond = (isOpenMPWorksharingDirective(DKind) || + isOpenMPGenericLoopDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) || isOpenMPLoopTransformationDirective(DKind)) ? SemaRef.BuildBinOp(CurScope, CondLoc, @@ -9490,6 +9509,7 @@ // base variables for the update ExprResult NextLB, NextUB, CombNextLB, CombNextUB; if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) || + isOpenMPGenericLoopDirective(DKind) || isOpenMPDistributeDirective(DKind) || isOpenMPLoopTransformationDirective(DKind)) { // LB + ST @@ -10044,6 +10064,57 @@ TargetCallLoc); } +StmtResult Sema::ActOnOpenMPGenericLoopDirective( + ArrayRef Clauses, Stmt *AStmt, SourceLocation StartLoc, + SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) { + if (!AStmt) + return StmtError(); + + // OpenMP 5.1 [2.11.7, loop construct] + // A list item may not appear in a lastprivate clause unless it is the + // loop iteration variable of a loop that is associated with the construct. + for (OMPClause *C : Clauses) { + if (auto *LPC = dyn_cast(C)) { + for (Expr *RefExpr : LPC->varlists()) { + SourceLocation ELoc; + SourceRange ERange; + Expr *SimpleRefExpr = RefExpr; + auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange); + if (ValueDecl *D = Res.first) { + auto &&Info = DSAStack->isLoopControlVariable(D); + if (!Info.first) { + Diag(ELoc, diag::err_omp_lastprivate_loop_var_non_loop_iteration); + return StmtError(); + } + } + } + } + } + + auto *CS = cast(AStmt); + // 1.2.2 OpenMP Language Terminology + // Structured block - An executable statement with a single entry at the + // top and a single exit at the bottom. + // The point of exit cannot be a branch out of the structured block. + // longjmp() and throw() must not violate the entry/exit criteria. + CS->getCapturedDecl()->setNothrow(); + + OMPLoopDirective::HelperExprs B; + // In presence of clause 'collapse', it will define the nested loops number. + unsigned NestedLoopCount = checkOpenMPLoop( + OMPD_loop, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses), + AStmt, *this, *DSAStack, VarsWithImplicitDSA, B); + if (NestedLoopCount == 0) + return StmtError(); + + assert((CurContext->isDependentContext() || B.builtAll()) && + "omp loop exprs were not built"); + + setFunctionHasBranchProtectedScope(); + return OMPGenericLoopDirective::Create(Context, StartLoc, EndLoc, + NestedLoopCount, Clauses, AStmt, B); +} + StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef Clauses, Stmt *AStmt, SourceLocation StartLoc, @@ -13529,6 +13600,7 @@ case OMPD_end_declare_variant: case OMPD_declare_target: case OMPD_end_declare_target: + case OMPD_loop: case OMPD_teams: case OMPD_tile: case OMPD_unroll: @@ -13608,6 +13680,7 @@ case OMPD_end_declare_variant: case OMPD_declare_target: case OMPD_end_declare_target: + case OMPD_loop: case OMPD_teams: case OMPD_simd: case OMPD_tile: @@ -13692,6 +13765,7 @@ case OMPD_end_declare_variant: case OMPD_declare_target: case OMPD_end_declare_target: + case OMPD_loop: case OMPD_simd: case OMPD_tile: case OMPD_unroll: @@ -13773,6 +13847,7 @@ case OMPD_end_declare_variant: case OMPD_declare_target: case OMPD_end_declare_target: + case OMPD_loop: case OMPD_simd: case OMPD_tile: case OMPD_unroll: @@ -13855,6 +13930,7 @@ case OMPD_end_declare_variant: case OMPD_declare_target: case OMPD_end_declare_target: + case OMPD_loop: case OMPD_simd: case OMPD_tile: case OMPD_unroll: @@ -13936,6 +14012,7 @@ case OMPD_end_declare_variant: case OMPD_declare_target: case OMPD_end_declare_target: + case OMPD_loop: case OMPD_simd: case OMPD_tile: case OMPD_unroll: @@ -14018,6 +14095,7 @@ case OMPD_end_declare_variant: case OMPD_declare_target: case OMPD_end_declare_target: + case OMPD_loop: case OMPD_simd: case OMPD_tile: case OMPD_unroll: @@ -14101,6 +14179,7 @@ case OMPD_end_declare_variant: case OMPD_declare_target: case OMPD_end_declare_target: + case OMPD_loop: case OMPD_simd: case OMPD_tile: case OMPD_unroll: diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -9172,6 +9172,17 @@ return Res; } +template +StmtResult TreeTransform::TransformOMPGenericLoopDirective( + OMPGenericLoopDirective *D) { + DeclarationNameInfo DirName; + getDerived().getSema().StartOpenMPDSABlock(OMPD_loop, DirName, nullptr, + D->getBeginLoc()); + StmtResult Res = getDerived().TransformOMPExecutableDirective(D); + getDerived().getSema().EndOpenMPDSABlock(Res.get()); + return Res; +} + //===----------------------------------------------------------------------===// // OpenMP clause transformation //===----------------------------------------------------------------------===// diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp --- a/clang/lib/Serialization/ASTReaderStmt.cpp +++ b/clang/lib/Serialization/ASTReaderStmt.cpp @@ -2632,6 +2632,10 @@ VisitOMPExecutableDirective(D); } +void ASTStmtReader::VisitOMPGenericLoopDirective(OMPGenericLoopDirective *D) { + VisitOMPLoopDirective(D); +} + //===----------------------------------------------------------------------===// // ASTReader Implementation //===----------------------------------------------------------------------===// @@ -3578,6 +3582,14 @@ Context, Record[ASTStmtReader::NumStmtFields], Empty); break; + case STMT_OMP_GENERIC_LOOP_DIRECTIVE: { + unsigned CollapsedNum = Record[ASTStmtReader::NumStmtFields]; + unsigned NumClauses = Record[ASTStmtReader::NumStmtFields + 1]; + S = OMPGenericLoopDirective::CreateEmpty(Context, NumClauses, + CollapsedNum, Empty); + break; + } + case EXPR_CXX_OPERATOR_CALL: S = CXXOperatorCallExpr::CreateEmpty( Context, /*NumArgs=*/Record[ASTStmtReader::NumExprFields], diff --git a/clang/lib/Serialization/ASTWriterStmt.cpp b/clang/lib/Serialization/ASTWriterStmt.cpp --- a/clang/lib/Serialization/ASTWriterStmt.cpp +++ b/clang/lib/Serialization/ASTWriterStmt.cpp @@ -2589,6 +2589,11 @@ Code = serialization::STMT_OMP_MASKED_DIRECTIVE; } +void ASTStmtWriter::VisitOMPGenericLoopDirective(OMPGenericLoopDirective *D) { + VisitOMPLoopDirective(D); + Code = serialization::STMT_OMP_GENERIC_LOOP_DIRECTIVE; +} + //===----------------------------------------------------------------------===// // ASTWriter Implementation //===----------------------------------------------------------------------===// diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp --- a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp +++ b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp @@ -1297,6 +1297,7 @@ case Stmt::OMPInteropDirectiveClass: case Stmt::OMPDispatchDirectiveClass: case Stmt::OMPMaskedDirectiveClass: + case Stmt::OMPGenericLoopDirectiveClass: case Stmt::CapturedStmtClass: case Stmt::OMPUnrollDirectiveClass: case Stmt::OMPMetaDirectiveClass: { diff --git a/clang/test/OpenMP/generic_loop_ast_print.cpp b/clang/test/OpenMP/generic_loop_ast_print.cpp new file mode 100644 --- /dev/null +++ b/clang/test/OpenMP/generic_loop_ast_print.cpp @@ -0,0 +1,141 @@ +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fopenmp -fopenmp-version=51 \ +// RUN: -fsyntax-only -verify %s + +// expected-no-diagnostics + +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fopenmp -fopenmp-version=51 \ +// RUN: -ast-print %s | FileCheck %s --check-prefix=PRINT + +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fopenmp -fopenmp-version=51 \ +// RUN: -ast-dump %s | FileCheck %s --check-prefix=DUMP + +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fopenmp -fopenmp-version=51 \ +// RUN: -emit-pch -o %t %s + +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fopenmp -fopenmp-version=51 \ +// RUN: -include-pch %t -ast-dump-all %s | FileCheck %s --check-prefix=DUMP + +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fopenmp -fopenmp-version=51 \ +// RUN: -include-pch %t -ast-print %s | FileCheck %s --check-prefix=PRINT + +#ifndef HEADER +#define HEADER + +//PRINT: template void templ_foo(T t) { +//PRINT: T j, z; +//PRINT: #pragma omp loop collapse(C) reduction(+: z) lastprivate(j) +//PRINT: for (T i = 0; i < t; ++i) +//PRINT: for (j = 0; j < t; ++j) +//PRINT: z += i + j; +//PRINT: } +//DUMP: FunctionTemplateDecl{{.*}}templ_foo +//DUMP: TemplateTypeParmDecl{{.*}}T +//DUMP: NonTypeTemplateParmDecl{{.*}}C +//DUMP: OMPGenericLoopDirective +//DUMP: OMPCollapseClause +//DUMP: DeclRefExpr{{.*}}'C' 'int' +//DUMP: OMPReductionClause +//DUMP: DeclRefExpr{{.*}}'z' 'T' +//DUMP: OMPLastprivateClause +//DUMP: DeclRefExpr{{.*}}'j' 'T' +//DUMP: ForStmt +//DUMP: ForStmt + +//PRINT: template<> void templ_foo(int t) { +//PRINT: int j, z; +//PRINT: #pragma omp loop collapse(2) reduction(+: z) lastprivate(j) +//PRINT: for (int i = 0; i < t; ++i) +//PRINT: for (j = 0; j < t; ++j) +//PRINT: z += i + j; +//PRINT: } +//DUMP: FunctionDecl{{.*}}templ_foo 'void (int)' +//DUMP: TemplateArgument type 'int' +//DUMP: TemplateArgument integral 2 +//DUMP: ParmVarDecl{{.*}}'int':'int' +//DUMP: OMPGenericLoopDirective +//DUMP: OMPCollapseClause +//DUMP: ConstantExpr{{.*}}'int' +//DUMP: value: Int 2 +//DUMP: OMPReductionClause +//DUMP: DeclRefExpr{{.*}}'z' 'int':'int' +//DUMP: OMPLastprivateClause +//DUMP: DeclRefExpr{{.*}}'j' 'int':'int' +//DUMP: ForStmt +template +void templ_foo(T t) { + + T j,z; + #pragma omp loop collapse(C) reduction(+:z) lastprivate(j) + for (T i = 0; i(8); +} + +#endif // HEADER diff --git a/clang/test/OpenMP/generic_loop_messages.cpp b/clang/test/OpenMP/generic_loop_messages.cpp new file mode 100644 --- /dev/null +++ b/clang/test/OpenMP/generic_loop_messages.cpp @@ -0,0 +1,133 @@ +// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=51 -Wuninitialized %s + +void foo() +{ + int i,j,k; + int z; + + // expected-error@+2 {{statement after '#pragma omp loop' must be a for loop}} + #pragma omp loop + i = 0; + + // OpenMP 5.1 [2.22 Nesting of regions] + // + // A barrier region may not be closely nested inside a worksharing, loop, + // task, taskloop, critical, ordered, atomic, or masked region. + + // expected-error@+3 {{region cannot be closely nested inside 'loop' region}} + #pragma omp loop + for (i=0; i<1000; ++i) { + #pragma omp barrier + } + + // A masked region may not be closely nested inside a worksharing, loop, + // atomic, task, or taskloop region. + + // expected-error@+3 {{region cannot be closely nested inside 'loop' region}} + #pragma omp loop + for (i=0; i<1000; ++i) { + #pragma omp masked filter(2) + { } + } + + // An ordered region that corresponds to an ordered construct without any + // clause or with the threads or depend clause may not be closely nested + // inside a critical, ordered, loop, atomic, task, or taskloop region. + + // expected-error@+3 {{region cannot be closely nested inside 'loop' region; perhaps you forget to enclose 'omp ordered' directive into a for or a parallel for region with 'ordered' clause?}} + #pragma omp loop + for (i=0; i<1000; ++i) { + #pragma omp ordered + { } + } + + // expected-error@+3 {{region cannot be closely nested inside 'loop' region; perhaps you forget to enclose 'omp ordered' directive into a for or a parallel for region with 'ordered' clause?}} + #pragma omp loop + for (i=0; i<1000; ++i) { + #pragma omp ordered threads + { } + } + + // expected-error@+3 {{region cannot be closely nested inside 'loop' region; perhaps you forget to enclose 'omp ordered' directive into a for or a parallel for region with 'ordered' clause?}} + #pragma omp loop + for (i=0; i<1000; ++i) { + #pragma omp ordered depend(source) + } + + // bind clause (not yet implemented) + + // collapse clause + + // expected-error@+4 {{expected 2 for loops after '#pragma omp loop', but found only 1}} + // expected-note@+1 {{as specified in 'collapse' clause}} + #pragma omp loop collapse(2) + for (i=0; i<1000; ++i) + z = i+11; + + // expected-error@+1 {{directive '#pragma omp loop' cannot contain more than one 'collapse' clause}} + #pragma omp loop collapse(2) collapse(2) + for (i=0; i<1000; ++i) + for (j=0; j<1000; ++j) + z = i+j+11; + + // order clause + + // expected-error@+1 {{expected 'concurrent' in OpenMP clause 'order'}} + #pragma omp loop order(foo) + for (i=0; i<1000; ++i) + z = i+11; + + // private clause + + // expected-error@+1 {{use of undeclared identifier 'undef_var'}} + #pragma omp loop private(undef_var) + for (i=0; i<1000; ++i) + z = i+11; + + // lastprivate + + // A list item may not appear in a lastprivate clause unless it is the loop + // iteration variable of a loop that is associated with the construct. + + // expected-error@+1 {{only loop iteration variables are allowed in 'lastprivate' clause in 'omp loop' directives}} + #pragma omp loop lastprivate(z) + for (i=0; i<1000; ++i) { + z = i+11; + } + + // expected-error@+1 {{only loop iteration variables are allowed in 'lastprivate' clause in 'omp loop' directives}} + #pragma omp loop lastprivate(k) collapse(2) + for (i=0; i<1000; ++i) + for (j=0; j<1000; ++j) + for (k=0; k<1000; ++k) + z = i+j+k+11; + + // reduction + + // expected-error@+1 {{use of undeclared identifier 'undef_var'}} + #pragma omp loop reduction(+:undef_var) + for (i=0; i<1000; ++i) + z = i+11; +} + +template +void templ_test(T t) { + T i,z; + + // expected-error@+4 {{expected 2 for loops after '#pragma omp loop', but found only 1}} + // expected-note@+1 {{as specified in 'collapse' clause}} + #pragma omp loop collapse(C) + for (i=0; i<1000; ++i) + z = i+11; + + // expected-error@+1 {{only loop iteration variables are allowed in 'lastprivate' clause in 'omp loop' directives}} + #pragma omp loop lastprivate(z) + for (i=0; i<1000; ++i) { + z = i+11; + } +} + +void bar() +{ + templ_test(16); // expected-note {{in instantiation of function template specialization 'templ_test' requested here}} +} diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -5710,6 +5710,8 @@ return cxstring::createRef("OMPDispatchDirective"); case CXCursor_OMPMaskedDirective: return cxstring::createRef("OMPMaskedDirective"); + case CXCursor_OMPGenericLoopDirective: + return cxstring::createRef("OMPGenericLoopDirective"); case CXCursor_OverloadCandidate: return cxstring::createRef("OverloadCandidate"); case CXCursor_TypeAliasTemplateDecl: diff --git a/clang/tools/libclang/CXCursor.cpp b/clang/tools/libclang/CXCursor.cpp --- a/clang/tools/libclang/CXCursor.cpp +++ b/clang/tools/libclang/CXCursor.cpp @@ -823,6 +823,9 @@ case Stmt::OMPMaskedDirectiveClass: K = CXCursor_OMPMaskedDirective; break; + case Stmt::OMPGenericLoopDirectiveClass: + K = CXCursor_OMPGenericLoopDirective; + break; case Stmt::BuiltinBitCastExprClass: K = CXCursor_BuiltinBitCastExpr; } diff --git a/llvm/include/llvm/Frontend/OpenMP/OMP.td b/llvm/include/llvm/Frontend/OpenMP/OMP.td --- a/llvm/include/llvm/Frontend/OpenMP/OMP.td +++ b/llvm/include/llvm/Frontend/OpenMP/OMP.td @@ -1732,6 +1732,17 @@ VersionedClause ]; } +def OMP_loop : Directive<"loop"> { + let allowedClauses = [ + VersionedClause, + VersionedClause, + VersionedClause, + ]; + let allowedOnceClauses = [ + VersionedClause, + VersionedClause, + ]; +} def OMP_Metadirective : Directive<"metadirective"> { let allowedClauses = [VersionedClause]; let allowedOnceClauses = [VersionedClause];