diff --git a/flang/include/flang/Parser/dump-parse-tree.h b/flang/include/flang/Parser/dump-parse-tree.h --- a/flang/include/flang/Parser/dump-parse-tree.h +++ b/flang/include/flang/Parser/dump-parse-tree.h @@ -545,6 +545,7 @@ NODE(parser, OpenMPCancellationPointConstruct) NODE(parser, OpenMPConstruct) NODE(parser, OpenMPCriticalConstruct) + NODE(parser, OpenMPDeclarativeAllocate) NODE(parser, OpenMPDeclarativeConstruct) NODE(parser, OpenMPDeclareReductionConstruct) NODE(parser, OpenMPDeclareSimdConstruct) @@ -552,6 +553,7 @@ NODE(parser, OmpMemoryOrderClause) NODE(parser, OpenMPFlushConstruct) NODE(parser, OpenMPLoopConstruct) + NODE(parser, OpenMPExecutableAllocate) NODE(parser, OpenMPSimpleStandaloneConstruct) NODE(parser, OpenMPStandaloneConstruct) NODE(parser, OpenMPSectionsConstruct) diff --git a/flang/include/flang/Parser/parse-tree.h b/flang/include/flang/Parser/parse-tree.h --- a/flang/include/flang/Parser/parse-tree.h +++ b/flang/include/flang/Parser/parse-tree.h @@ -3583,11 +3583,19 @@ std::tuple t; }; +// 2.11.3 allocate -> ALLOCATE (variable-name-list) [clause] +struct OpenMPDeclarativeAllocate { + TUPLE_CLASS_BOILERPLATE(OpenMPDeclarativeAllocate); + CharBlock source; + std::tuple t; +}; + struct OpenMPDeclarativeConstruct { UNION_CLASS_BOILERPLATE(OpenMPDeclarativeConstruct); CharBlock source; - std::variant + std::variant u; }; @@ -3607,6 +3615,19 @@ std::tuple t; }; +// 2.11.3 allocate -> ALLOCATE [(variable-name-list)] [clause] +// [ALLOCATE (variable-name-list) [clause] [...]] +// allocate-statement +// clause -> allocator-clause +struct OpenMPExecutableAllocate { + TUPLE_CLASS_BOILERPLATE(OpenMPExecutableAllocate); + CharBlock source; + std::tuple, OmpClauseList, + std::optional>, + Statement> + t; +}; + // 2.17.7 atomic -> ATOMIC [clause[,]] atomic-clause [[,]clause] | // ATOMIC [clause] // clause -> memory-order-clause | HINT(hint-expression) @@ -3777,6 +3798,7 @@ UNION_CLASS_BOILERPLATE(OpenMPConstruct); std::variant u; }; diff --git a/flang/lib/Lower/OpenMP.cpp b/flang/lib/Lower/OpenMP.cpp --- a/flang/lib/Lower/OpenMP.cpp +++ b/flang/lib/Lower/OpenMP.cpp @@ -256,6 +256,10 @@ [&](const Fortran::parser::OpenMPLoopConstruct &loopConstruct) { TODO(""); }, + [&](const Fortran::parser::OpenMPDeclarativeAllocate + &execAllocConstruct) { TODO(""); }, + [&](const Fortran::parser::OpenMPExecutableAllocate + &execAllocConstruct) { TODO(""); }, [&](const Fortran::parser::OpenMPBlockConstruct &blockConstruct) { genOMP(converter, eval, blockConstruct); }, diff --git a/flang/lib/Parser/openmp-parsers.cpp b/flang/lib/Parser/openmp-parsers.cpp --- a/flang/lib/Parser/openmp-parsers.cpp +++ b/flang/lib/Parser/openmp-parsers.cpp @@ -159,6 +159,8 @@ construct(parenthesized(Parser{})) || "ALLOCATE" >> construct(parenthesized(Parser{})) || + "ALLOCATOR" >> construct(construct( + parenthesized(scalarIntExpr))) || "COLLAPSE" >> construct(construct( parenthesized(scalarIntConstantExpr))) || "COPYIN" >> construct(construct( @@ -454,6 +456,13 @@ TYPE_PARSER(construct( Parser{}, block, Parser{})) +// 2.11.3 Executable Allocate directive +TYPE_PARSER( + sourced(construct(verbatim("ALLOCATE"_tok), + maybe(parenthesized(Parser{})), Parser{}, + maybe(nonemptyList(Parser{})) / endOmpLine, + statement(allocateStmt)))) + // 2.8.2 Declare Simd construct TYPE_PARSER( sourced(construct(verbatim("DECLARE SIMD"_tok), @@ -463,6 +472,12 @@ TYPE_PARSER(sourced(construct( verbatim("THREADPRIVATE"_tok), parenthesized(Parser{})))) +// 2.11.3 Declarative Allocate directive +TYPE_PARSER( + sourced(construct(verbatim("ALLOCATE"_tok), + parenthesized(Parser{}), Parser{})) / + lookAhead(endOmpLine / !statement(allocateStmt))) + // Declarative constructs TYPE_PARSER(startOmpLine >> sourced(construct( @@ -471,6 +486,8 @@ Parser{}) || construct( Parser{}) || + construct( + Parser{}) || construct(Parser{})) / endOmpLine) @@ -511,6 +528,8 @@ // OpenMPStandaloneConstruct to resolve !$OMP ORDERED construct(Parser{}), construct(Parser{}), + construct(Parser{}), + construct(Parser{}), construct(Parser{}))) // END OMP Block directives diff --git a/flang/lib/Parser/type-parsers.h b/flang/lib/Parser/type-parsers.h --- a/flang/lib/Parser/type-parsers.h +++ b/flang/lib/Parser/type-parsers.h @@ -85,6 +85,7 @@ constexpr Parser substring; // R908 constexpr Parser dataRef; // R911, R914, R917 constexpr Parser structureComponent; // R913 +constexpr Parser allocateStmt; // R927 constexpr Parser statVariable; // R929 constexpr Parser statOrErrmsg; // R942 & R1165 constexpr Parser definedOpName; // R1003, R1023, R1414, & R1415 diff --git a/flang/lib/Parser/unparse.cpp b/flang/lib/Parser/unparse.cpp --- a/flang/lib/Parser/unparse.cpp +++ b/flang/lib/Parser/unparse.cpp @@ -2287,6 +2287,25 @@ Walk(std::get>(x.t), "!$OMP END ATOMIC\n"); EndOpenMP(); } + void Unparse(const OpenMPExecutableAllocate &x) { + BeginOpenMP(); + Word("!$OMP ALLOCATE"); + Walk(" (", std::get>(x.t), ")"); + Walk(std::get(x.t)); + Put("\n"); + EndOpenMP(); + Walk(std::get>(x.t)); + } + void Unparse(const OpenMPDeclarativeAllocate &x) { + BeginOpenMP(); + Word("!$OMP ALLOCATE"); + Put(" ("); + Walk(std::get(x.t)); + Put(")"); + Walk(std::get(x.t)); + Put("\n"); + EndOpenMP(); + } void Unparse(const OmpCriticalDirective &x) { BeginOpenMP(); Word("!$OMP CRITICAL"); @@ -2339,6 +2358,15 @@ BeginOpenMP(); Word("!$OMP "); return std::visit(common::visitors{ + [&](const OpenMPDeclarativeAllocate &z) { + Word("ALLOCATE ("); + Walk(std::get(z.t)); + Put(")"); + Walk(std::get(z.t)); + Put("\n"); + EndOpenMP(); + return false; + }, [&](const OpenMPDeclareReductionConstruct &) { Word("DECLARE REDUCTION "); return true; diff --git a/flang/lib/Semantics/check-omp-structure.h b/flang/lib/Semantics/check-omp-structure.h --- a/flang/lib/Semantics/check-omp-structure.h +++ b/flang/lib/Semantics/check-omp-structure.h @@ -104,8 +104,12 @@ void Enter(const parser::OpenMPDeclareSimdConstruct &); void Leave(const parser::OpenMPDeclareSimdConstruct &); + void Enter(const parser::OpenMPDeclarativeAllocate &); + void Leave(const parser::OpenMPDeclarativeAllocate &); void Enter(const parser::OpenMPDeclareTargetConstruct &); void Leave(const parser::OpenMPDeclareTargetConstruct &); + void Enter(const parser::OpenMPExecutableAllocate &); + void Leave(const parser::OpenMPExecutableAllocate &); void Enter(const parser::OpenMPSimpleStandaloneConstruct &); void Leave(const parser::OpenMPSimpleStandaloneConstruct &); @@ -121,6 +125,7 @@ void Leave(const parser::OmpClauseList &); void Enter(const parser::OmpClause &); void Enter(const parser::OmpNowait &); + void Enter(const parser::OmpClause::Allocator &); void Enter(const parser::OmpClause::Inbranch &); void Enter(const parser::OmpClause::Mergeable &); void Enter(const parser::OmpClause::Nogroup &); diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp --- a/flang/lib/Semantics/check-omp-structure.cpp +++ b/flang/lib/Semantics/check-omp-structure.cpp @@ -179,6 +179,15 @@ dirContext_.pop_back(); } +void OmpStructureChecker::Enter(const parser::OpenMPDeclarativeAllocate &x) { + const auto &dir{std::get(x.t)}; + PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_allocate); +} + +void OmpStructureChecker::Leave(const parser::OpenMPDeclarativeAllocate &) { + dirContext_.pop_back(); +} + void OmpStructureChecker::Enter(const parser::OpenMPDeclareTargetConstruct &x) { const auto &dir{std::get(x.t)}; PushContext(dir.source, llvm::omp::Directive::OMPD_declare_target); @@ -192,6 +201,15 @@ dirContext_.pop_back(); } +void OmpStructureChecker::Enter(const parser::OpenMPExecutableAllocate &x) { + const auto &dir{std::get(x.t)}; + PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_allocate); +} + +void OmpStructureChecker::Leave(const parser::OpenMPExecutableAllocate &) { + dirContext_.pop_back(); +} + void OmpStructureChecker::Enter( const parser::OpenMPSimpleStandaloneConstruct &x) { const auto &dir{std::get(x.t)}; @@ -382,6 +400,7 @@ CHECK_SIMPLE_CLAUSE(Release, OMPC_release) CHECK_SIMPLE_CLAUSE(Relaxed, OMPC_relaxed) +CHECK_REQ_SCALAR_INT_CLAUSE(Allocator, OMPC_allocator) CHECK_REQ_SCALAR_INT_CLAUSE(Grainsize, OMPC_grainsize) CHECK_REQ_SCALAR_INT_CLAUSE(NumTasks, OMPC_num_tasks) CHECK_REQ_SCALAR_INT_CLAUSE(NumTeams, OMPC_num_teams) diff --git a/flang/test/Parser/omp-allocate-unparse.f90 b/flang/test/Parser/omp-allocate-unparse.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Parser/omp-allocate-unparse.f90 @@ -0,0 +1,44 @@ +! RUN: %f18 -fdebug-no-semantics -funparse -fopenmp %s | FileCheck %s +! Check Unparsing of OpenMP Allocate directive + +program allocate_unparse +use omp_lib + +real, dimension (:,:), allocatable :: darray +integer :: a, b, m, n, t, x, y, z + +! 2.11.3 declarative allocate + +!$omp allocate(x, y) +!$omp allocate(x, y) allocator(omp_default_mem_alloc) + +! 2.11.3 executable allocate + +!$omp allocate(a, b) + allocate ( darray(a, b) ) +!$omp allocate allocator(omp_default_mem_alloc) + allocate ( darray(a, b) ) +!$omp allocate(a, b) allocator(omp_default_mem_alloc) + allocate ( darray(a, b) ) + +!$omp allocate(t) allocator(omp_const_mem_alloc) +!$omp allocate(z) allocator(omp_default_mem_alloc) +!$omp allocate(m) allocator(omp_default_mem_alloc) +!$omp allocate(n) + allocate ( darray(z, t) ) + +end program allocate_unparse + +!CHECK:!$OMP ALLOCATE (x,y) +!CHECK:!$OMP ALLOCATE (x,y) ALLOCATOR(omp_default_mem_alloc) +!CHECK:!$OMP ALLOCATE (a,b) +!CHECK:ALLOCATE(darray(a,b)) +!CHECK:!$OMP ALLOCATE ALLOCATOR(omp_default_mem_alloc) +!CHECK:ALLOCATE(darray(a,b)) +!CHECK:!$OMP ALLOCATE (a,b) ALLOCATOR(omp_default_mem_alloc) +!CHECK:ALLOCATE(darray(a,b)) +!CHECK:!$OMP ALLOCATE (t) ALLOCATOR(omp_const_mem_alloc) +!CHECK:!$OMP ALLOCATE (z) ALLOCATOR(omp_default_mem_alloc) +!CHECK:!$OMP ALLOCATE (m) ALLOCATOR(omp_default_mem_alloc) +!CHECK:!$OMP ALLOCATE (n) +!CHECK:ALLOCATE(darray(z,t)) diff --git a/flang/test/Semantics/omp-allocate-directive.f90 b/flang/test/Semantics/omp-allocate-directive.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/omp-allocate-directive.f90 @@ -0,0 +1,25 @@ +! RUN: %S/test_errors.sh %s %t %f18 -fopenmp +! Check OpenMP Allocate directive +use omp_lib + +! 2.11.3 declarative allocate +! 2.11.3 executable allocate + +real, dimension (:,:), allocatable :: darray +integer :: a, b, x, y, m, n, t, z +!$omp allocate(x, y) +!$omp allocate(x, y) allocator(omp_default_mem_alloc) + +!$omp allocate(a, b) + allocate ( darray(a, b) ) + +!$omp allocate(a, b) allocator(omp_default_mem_alloc) + allocate ( darray(a, b) ) + +!$omp allocate(t) allocator(omp_const_mem_alloc) +!$omp allocate(z) allocator(omp_default_mem_alloc) +!$omp allocate(m) allocator(omp_default_mem_alloc) +!$omp allocate(n) + allocate ( darray(z, t) ) + +end 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 @@ -34,6 +34,7 @@ def OMPC_Allocator : Clause<"allocator"> { let clangClass = "OMPAllocatorClause"; + let flangClassValue = "ScalarIntExpr"; } def OMPC_If : Clause<"if"> { let clangClass = "OMPIfClause";