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 @@ -544,6 +544,7 @@ NODE(parser, OpenMPCancellationPointConstruct) NODE(parser, OpenMPConstruct) NODE(parser, OpenMPCriticalConstruct) + NODE(parser, OpenMPDeclarativeAllocate) NODE(parser, OpenMPDeclarativeConstruct) NODE(parser, OpenMPDeclareReductionConstruct) NODE(parser, OpenMPDeclareSimdConstruct) @@ -551,6 +552,8 @@ NODE(parser, OmpFlushMemoryClause) NODE(parser, OpenMPFlushConstruct) NODE(parser, OpenMPLoopConstruct) + NODE(parser, OpenMPExecutableAllocate) + NODE(parser, OpenMPExecutableAllocateList) 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,26 @@ std::tuple t; }; +// 2.11.3 allocate -> [ALLOCATE (variable-name-list) [clause]] +// allocate-statement +// clause -> allocator-clause +struct OpenMPExecutableAllocateList { + TUPLE_CLASS_BOILERPLATE(OpenMPExecutableAllocateList); + CharBlock source; + std::tuple t; +}; + +// 2.11.3 allocate -> 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 +3805,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::OpenMPExecutableAllocate + &execAllocConstruct) { TODO(""); }, + [&](const Fortran::parser::OpenMPExecutableAllocateList + &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( @@ -449,6 +451,19 @@ TYPE_PARSER(construct( Parser{}, block, Parser{})) +// 2.11.3 Executable Allocate List +TYPE_PARSER( + sourced(construct(verbatim("ALLOCATE"_tok), + parenthesized(Parser{}), Parser{})) /*/ + lookAhead(endOmpLine / !statement(allocateStmt))*/) + +// 2.11.3 Executable Allocate directive +TYPE_PARSER( + sourced(construct(verbatim("ALLOCATE"_tok), + maybe(parenthesized(Parser{})), Parser{}, + maybe(Parser{}) / endOmpLine, + statement(allocateStmt)))) + // 2.8.2 Declare Simd construct TYPE_PARSER( sourced(construct(verbatim("DECLARE SIMD"_tok), @@ -458,6 +473,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( @@ -466,6 +487,8 @@ Parser{}) || construct( Parser{}) || + construct( + Parser{}) || construct(Parser{})) / endOmpLine) @@ -506,6 +529,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 @@ -91,6 +91,7 @@ constexpr Parser expr; // R1022 constexpr Parser specificationExpr; // R1028 constexpr Parser assignmentStmt; // R1032 +constexpr Parser allocateStmt; constexpr Parser pointerAssignmentStmt; // R1033 constexpr Parser whereStmt; // R1041, R1045, R1046 constexpr Parser whereConstruct; // R1042 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,24 @@ 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 OpenMPExecutableAllocateList &x) { + BeginOpenMP(); + Word("!$OMP ALLOCATE"); + Put(" ("); + Walk(std::get(x.t)); + Put(")"); + Walk(std::get(x.t)); + Put("\n"); + } void Unparse(const OmpCriticalDirective &x) { BeginOpenMP(); Word("!$OMP CRITICAL"); @@ -2339,6 +2357,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,6 +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::OpenMPExecutableAllocate &); + void Leave(const parser::OpenMPExecutableAllocate &); + void Enter(const parser::OpenMPExecutableAllocateList &); + void Leave(const parser::OpenMPExecutableAllocateList &); void Enter(const parser::OpenMPDeclareTargetConstruct &); void Leave(const parser::OpenMPDeclareTargetConstruct &); @@ -138,6 +144,7 @@ void Enter(const parser::OmpClause::NumTasks &); void Enter(const parser::OmpClause::NumTeams &); void Enter(const parser::OmpClause::NumThreads &); + void Enter(const parser::OmpClause::Allocator &); void Enter(const parser::OmpClause::Ordered &); void Enter(const parser::OmpClause::Priority &); void Enter(const parser::OmpClause::Private &); 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 @@ -178,6 +178,33 @@ 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::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::OpenMPExecutableAllocateList &x) { + const auto &dir{std::get(x.t)}; + PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_allocate); +} + +void OmpStructureChecker::Leave(const parser::OpenMPExecutableAllocateList &) { + 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); @@ -392,6 +419,7 @@ CHECK_SIMPLE_CLAUSE(Untied, OMPC_untied) CHECK_SIMPLE_CLAUSE(UseDevicePtr, OMPC_use_device_ptr) +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";