Skip to content

Commit 390985c

Browse files
[Flang][OpenMP]Add parsing support for DISPATCH construct
This allows the Flang parser to accept the !$OMP DISPATCH and related clauses. Lowering is currently not implemented. Tests for unparse and parse-tree dump is provided, and one for checking that the lowering ends in a "not yet implemented"
1 parent 80ecbaa commit 390985c

File tree

11 files changed

+140
-2
lines changed

11 files changed

+140
-2
lines changed

flang/include/flang/Parser/dump-parse-tree.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,9 @@ class ParseTreeDumper {
662662
NODE(parser, OmpAtomicDefaultMemOrderClause)
663663
NODE_ENUM(common, OmpAtomicDefaultMemOrderType)
664664
NODE(parser, OpenMPDepobjConstruct)
665+
NODE(parser, OpenMPDispatchConstruct)
666+
NODE(parser, OmpDispatchDirective)
667+
NODE(parser, OmpEndDispatchDirective)
665668
NODE(parser, OpenMPErrorConstruct)
666669
NODE(parser, OpenMPFlushConstruct)
667670
NODE(parser, OpenMPLoopConstruct)

flang/include/flang/Parser/parse-tree.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4506,6 +4506,31 @@ struct OpenMPDepobjConstruct {
45064506
std::tuple<Verbatim, OmpObject, OmpClause> t;
45074507
};
45084508

4509+
// Ref: [5.2: 200-201]
4510+
//
4511+
// dispatch-construct -> DISPATCH dispatch-clause
4512+
// dispatch-clause -> depend-clause |
4513+
// device-clause |
4514+
// is_device_ptr-clause |
4515+
// nocontext-clause |
4516+
// novariants-clause |
4517+
// nowait-clause
4518+
struct OmpDispatchDirective {
4519+
TUPLE_CLASS_BOILERPLATE(OmpDispatchDirective);
4520+
CharBlock source;
4521+
std::tuple<Verbatim, OmpClauseList> t;
4522+
};
4523+
4524+
EMPTY_CLASS(OmpEndDispatchDirective);
4525+
4526+
struct OpenMPDispatchConstruct {
4527+
TUPLE_CLASS_BOILERPLATE(OpenMPDispatchConstruct);
4528+
CharBlock source;
4529+
std::tuple<OmpDispatchDirective, Block,
4530+
std::optional<OmpEndDispatchDirective>>
4531+
t;
4532+
};
4533+
45094534
// Ref: OpenMP [5.2:216-218]
45104535
// ERROR AT(compilation|execution) SEVERITY(fatal|warning) MESSAGE("msg-str)
45114536
struct OpenMPErrorConstruct {
@@ -4586,8 +4611,8 @@ struct OpenMPConstruct {
45864611
UNION_CLASS_BOILERPLATE(OpenMPConstruct);
45874612
std::variant<OpenMPStandaloneConstruct, OpenMPSectionsConstruct,
45884613
OpenMPSectionConstruct, OpenMPLoopConstruct, OpenMPBlockConstruct,
4589-
OpenMPAtomicConstruct, OpenMPDeclarativeAllocate, OpenMPErrorConstruct,
4590-
OpenMPExecutableAllocate, OpenMPAllocatorsConstruct,
4614+
OpenMPAtomicConstruct, OpenMPDeclarativeAllocate, OpenMPDispatchConstruct,
4615+
OpenMPErrorConstruct, OpenMPExecutableAllocate, OpenMPAllocatorsConstruct,
45914616
OpenMPCriticalConstruct>
45924617
u;
45934618
};

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2904,6 +2904,13 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
29042904
queue.begin(), name);
29052905
}
29062906

2907+
static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
2908+
semantics::SemanticsContext &semaCtx,
2909+
lower::pft::Evaluation &eval,
2910+
const parser::OpenMPDispatchConstruct &) {
2911+
TODO(converter.getCurrentLocation(), "OpenMPDispatchConstruct");
2912+
}
2913+
29072914
static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
29082915
semantics::SemanticsContext &semaCtx,
29092916
lower::pft::Evaluation &eval,

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,11 +659,15 @@ TYPE_PARSER(
659659
"MERGEABLE" >> construct<OmpClause>(construct<OmpClause::Mergeable>()) ||
660660
"MESSAGE" >> construct<OmpClause>(construct<OmpClause::Message>(
661661
parenthesized(Parser<OmpMessageClause>{}))) ||
662+
"NOCONTEXT" >> construct<OmpClause>(construct<OmpClause::Nocontext>(
663+
parenthesized(scalarLogicalExpr))) ||
662664
"NOGROUP" >> construct<OmpClause>(construct<OmpClause::Nogroup>()) ||
663665
"NONTEMPORAL" >> construct<OmpClause>(construct<OmpClause::Nontemporal>(
664666
parenthesized(nonemptyList(name)))) ||
665667
"NOTINBRANCH" >>
666668
construct<OmpClause>(construct<OmpClause::Notinbranch>()) ||
669+
"NOVARIANTS" >> construct<OmpClause>(construct<OmpClause::Novariants>(
670+
parenthesized(scalarLogicalExpr))) ||
667671
"NOWAIT" >> construct<OmpClause>(construct<OmpClause::Nowait>()) ||
668672
"NUM_TASKS" >> construct<OmpClause>(construct<OmpClause::NumTasks>(
669673
parenthesized(Parser<OmpNumTasksClause>{}))) ||
@@ -1027,6 +1031,16 @@ TYPE_PARSER(sourced(construct<OmpCriticalDirective>(verbatim("CRITICAL"_tok),
10271031
TYPE_PARSER(construct<OpenMPCriticalConstruct>(
10281032
Parser<OmpCriticalDirective>{}, block, Parser<OmpEndCriticalDirective>{}))
10291033

1034+
TYPE_PARSER(sourced(construct<OmpDispatchDirective>(
1035+
verbatim("DISPATCH"_tok), Parser<OmpClauseList>{})))
1036+
1037+
TYPE_PARSER(
1038+
construct<OmpEndDispatchDirective>(startOmpLine >> "END DISPATCH"_tok))
1039+
1040+
TYPE_PARSER(sourced(construct<OpenMPDispatchConstruct>(
1041+
Parser<OmpDispatchDirective>{} / endOmpLine, block,
1042+
maybe(Parser<OmpEndDispatchDirective>{} / endOmpLine))))
1043+
10301044
TYPE_PARSER(sourced(construct<OpenMPErrorConstruct>(
10311045
verbatim("ERROR"_tok), Parser<OmpClauseList>{})))
10321046

@@ -1127,6 +1141,7 @@ TYPE_CONTEXT_PARSER("OpenMP construct"_en_US,
11271141
// OpenMPStandaloneConstruct to resolve !$OMP ORDERED
11281142
construct<OpenMPConstruct>(Parser<OpenMPStandaloneConstruct>{}),
11291143
construct<OpenMPConstruct>(Parser<OpenMPAtomicConstruct>{}),
1144+
construct<OpenMPConstruct>(Parser<OpenMPDispatchConstruct>{}),
11301145
construct<OpenMPConstruct>(Parser<OpenMPErrorConstruct>{}),
11311146
construct<OpenMPConstruct>(Parser<OpenMPExecutableAllocate>{}),
11321147
construct<OpenMPConstruct>(Parser<OpenMPAllocatorsConstruct>{}),

flang/lib/Parser/unparse.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,6 +2710,15 @@ class UnparseVisitor {
27102710
Walk(x.v);
27112711
return false;
27122712
}
2713+
void Unparse(const OmpDispatchDirective &x) {
2714+
Word("!$OMP DISPATCH");
2715+
Walk(x.t);
2716+
Put("\n");
2717+
}
2718+
void Unparse(const OmpEndDispatchDirective &) {
2719+
Word("!$OMP END DISPATCH");
2720+
Put("\n");
2721+
}
27132722
void Unparse(const OpenMPErrorConstruct &x) {
27142723
Word("!$OMP ERROR ");
27152724
Walk(x.t);

flang/lib/Semantics/check-omp-structure.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,14 @@ void OmpStructureChecker::Leave(const parser::OpenMPDeclareTargetConstruct &x) {
16881688
dirContext_.pop_back();
16891689
}
16901690

1691+
void OmpStructureChecker::Enter(const parser::OpenMPDispatchConstruct &x) {
1692+
PushContextAndClauseSets(x.source, llvm::omp::Directive::OMPD_dispatch);
1693+
}
1694+
1695+
void OmpStructureChecker::Leave(const parser::OpenMPDispatchConstruct &x) {
1696+
dirContext_.pop_back();
1697+
}
1698+
16911699
void OmpStructureChecker::Enter(const parser::OpenMPErrorConstruct &x) {
16921700
const auto &dir{std::get<parser::Verbatim>(x.t)};
16931701
PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_error);

flang/lib/Semantics/check-omp-structure.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ class OmpStructureChecker
102102
void Enter(const parser::OmpDeclareTargetWithList &);
103103
void Enter(const parser::OmpDeclareTargetWithClause &);
104104
void Leave(const parser::OmpDeclareTargetWithClause &);
105+
void Enter(const parser::OpenMPDispatchConstruct &);
106+
void Leave(const parser::OpenMPDispatchConstruct &);
105107
void Enter(const parser::OpenMPErrorConstruct &);
106108
void Leave(const parser::OpenMPErrorConstruct &);
107109
void Enter(const parser::OpenMPExecutableAllocate &);

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
441441
bool Pre(const parser::OpenMPDeclarativeAllocate &);
442442
void Post(const parser::OpenMPDeclarativeAllocate &) { PopContext(); }
443443

444+
bool Pre(const parser::OpenMPDispatchConstruct &);
445+
void Post(const parser::OpenMPDispatchConstruct &) { PopContext(); }
446+
444447
bool Pre(const parser::OpenMPExecutableAllocate &);
445448
void Post(const parser::OpenMPExecutableAllocate &);
446449

@@ -1994,6 +1997,11 @@ bool OmpAttributeVisitor::Pre(const parser::OpenMPDeclarativeAllocate &x) {
19941997
return false;
19951998
}
19961999

2000+
bool OmpAttributeVisitor::Pre(const parser::OpenMPDispatchConstruct &x) {
2001+
PushContext(x.source, llvm::omp::Directive::OMPD_dispatch);
2002+
return true;
2003+
}
2004+
19972005
bool OmpAttributeVisitor::Pre(const parser::OpenMPExecutableAllocate &x) {
19982006
PushContext(x.source, llvm::omp::Directive::OMPD_allocate);
19992007
const auto &list{std::get<std::optional<parser::OmpObjectList>>(x.t)};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
! RUN: %not_todo_cmd %flang_fc1 -emit-fir -fopenmp -fopenmp-version=51 -o - %s 2>&1 | FileCheck %s
2+
3+
! CHECK: not yet implemented: OpenMPDispatchConstruct
4+
program p
5+
integer r
6+
r = 1
7+
!$omp dispatch nowait
8+
print *,r
9+
end program p

flang/test/Parser/OpenMP/dispatch.f90

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
! RUN: %flang_fc1 %openmp_flags -fdebug-dump-parse-tree %s | FileCheck %s
2+
! RUN: %flang_fc1 %openmp_flags -fdebug-unparse %s | FileCheck %s --check-prefix="UNPARSE"
3+
4+
integer function func(a, b, c)
5+
integer :: a, b, c
6+
func = a + b + c
7+
end function func
8+
9+
subroutine sub(x)
10+
use iso_c_binding
11+
integer :: func
12+
integer :: r
13+
type(c_ptr) :: x
14+
integer :: a = 14, b = 7, c = 21
15+
!UNPARSE: !$OMP DISPATCH DEVICE(3_4) NOWAIT NOCONTEXT(.false._4) NOVARIANTS(.true._4)
16+
!CHECK: | | ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPDispatchConstruct
17+
!CHECK-NEXT: | | | OmpDispatchDirective
18+
!CHECK: | | | | OmpClauseList -> OmpClause -> Device -> OmpDeviceClause
19+
!CHECK-NEXT: | | | | | Scalar -> Integer -> Expr = '3_4'
20+
!CHECK-NEXT: | | | | | | LiteralConstant -> IntLiteralConstant = '3'
21+
!CHECK-NEXT: | | | | OmpClause -> Nowait
22+
!CHECK-NEXT: | | | | OmpClause -> Nocontext -> Scalar -> Logical -> Expr = '.false._4'
23+
!CHECK-NEXT: | | | | | LiteralConstant -> LogicalLiteralConstant
24+
!CHECK-NEXT: | | | | | | bool = 'false'
25+
!CHECK-NEXT: | | | | OmpClause -> Novariants -> Scalar -> Logical -> Expr = '.true._4'
26+
!CHECK-NEXT: | | | | | EQ
27+
!CHECK-NEXT: | | | | | | Expr = '1_4'
28+
!CHECK-NEXT: | | | | | | | LiteralConstant -> IntLiteralConstant = '1'
29+
!CHECK-NEXT: | | | | | | Expr = '1_4'
30+
!CHECK-NEXT: | | | | | | | LiteralConstant -> IntLiteralConstant = '1'
31+
!CHECK-NEXT: | | | Block
32+
33+
!$omp dispatch device(3) nowait nocontext(.false.) novariants(1.eq.1)
34+
r = func(a, b, c)
35+
!UNPARSE: !$OMP END DISPATCH
36+
!CHECK: | | | OmpEndDispatchDirective
37+
!$omp end dispatch
38+
39+
!! Test the "no end dispatch" option.
40+
!UNPARSE: !$OMP DISPATCH DEVICE(3_4) IS_DEVICE_PTR(x)
41+
!CHECK: | | ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPDispatchConstruct
42+
!CHECK-NEXT: | | | OmpDispatchDirective
43+
!CHECK: | | | | OmpClause -> IsDevicePtr -> OmpObjectList -> OmpObject -> Designator -> DataRef -> Name = 'x'
44+
!$omp dispatch device(3) is_device_ptr(x)
45+
r = func(a+1, b+2, c+3)
46+
!CHECK-NOT: | | | OmpEndDispatchDirective
47+
48+
end subroutine sub
49+
50+
51+

llvm/include/llvm/Frontend/OpenMP/OMP.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ def OMPC_Novariants : Clause<"novariants"> {
320320
}
321321
def OMPC_NoWait : Clause<"nowait"> {
322322
let clangClass = "OMPNowaitClause";
323+
let clangClass = "OmpNowaitClause";
323324
}
324325
def OMP_NUMTASKS_Strict : ClauseVal<"strict", 1, 1> {}
325326
def OMP_NUMTASKS_Unknown : ClauseVal<"unknown", 2, 0> { let isDefault = 1; }

0 commit comments

Comments
 (0)