Skip to content

Commit ee14b03

Browse files
committed
Adding parsing support for scan directive.
1 parent 9991ea2 commit ee14b03

File tree

13 files changed

+177
-78
lines changed

13 files changed

+177
-78
lines changed

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,6 +2520,9 @@ static void genOMPDispatch(lower::AbstractConverter &converter,
25202520
case llvm::omp::Directive::OMPD_parallel:
25212521
genStandaloneParallel(converter, symTable, semaCtx, eval, loc, queue, item);
25222522
break;
2523+
case llvm::omp::Directive::OMPD_scan:
2524+
TODO(loc, "Unhandled directive " + llvm::omp::getOpenMPDirectiveName(dir));
2525+
break;
25232526
case llvm::omp::Directive::OMPD_section:
25242527
llvm_unreachable("genOMPDispatch: OMPD_section");
25252528
// Lowered in the enclosing genSectionsOp.

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ TYPE_PARSER(
559559
construct<OmpClause>(construct<OmpClause::DynamicAllocators>()) ||
560560
"ENTER" >> construct<OmpClause>(construct<OmpClause::Enter>(
561561
parenthesized(Parser<OmpObjectList>{}))) ||
562+
"EXCLUSIVE" >> construct<OmpClause>(construct<OmpClause::Exclusive>(
563+
parenthesized(Parser<OmpObjectList>{}))) ||
562564
"FILTER" >> construct<OmpClause>(construct<OmpClause::Filter>(
563565
parenthesized(scalarIntExpr))) ||
564566
"FINAL" >> construct<OmpClause>(construct<OmpClause::Final>(
@@ -578,6 +580,8 @@ TYPE_PARSER(
578580
"IF" >> construct<OmpClause>(construct<OmpClause::If>(
579581
parenthesized(Parser<OmpIfClause>{}))) ||
580582
"INBRANCH" >> construct<OmpClause>(construct<OmpClause::Inbranch>()) ||
583+
"INCLUSIVE" >> construct<OmpClause>(construct<OmpClause::Inclusive>(
584+
parenthesized(Parser<OmpObjectList>{}))) ||
581585
"IS_DEVICE_PTR" >> construct<OmpClause>(construct<OmpClause::IsDevicePtr>(
582586
parenthesized(Parser<OmpObjectList>{}))) ||
583587
"LASTPRIVATE" >> construct<OmpClause>(construct<OmpClause::Lastprivate>(
@@ -790,6 +794,7 @@ TYPE_PARSER(sourced(construct<OpenMPFlushConstruct>(verbatim("FLUSH"_tok),
790794
TYPE_PARSER(sourced(construct<OmpSimpleStandaloneDirective>(first(
791795
"BARRIER" >> pure(llvm::omp::Directive::OMPD_barrier),
792796
"ORDERED" >> pure(llvm::omp::Directive::OMPD_ordered),
797+
"SCAN" >> pure(llvm::omp::Directive::OMPD_scan),
793798
"TARGET ENTER DATA" >> pure(llvm::omp::Directive::OMPD_target_enter_data),
794799
"TARGET EXIT DATA" >> pure(llvm::omp::Directive::OMPD_target_exit_data),
795800
"TARGET UPDATE" >> pure(llvm::omp::Directive::OMPD_target_update),

flang/lib/Parser/unparse.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,6 +2393,9 @@ class UnparseVisitor {
23932393
case llvm::omp::Directive::OMPD_barrier:
23942394
Word("BARRIER ");
23952395
break;
2396+
case llvm::omp::Directive::OMPD_scan:
2397+
Word("SCAN ");
2398+
break;
23962399
case llvm::omp::Directive::OMPD_taskwait:
23972400
Word("TASKWAIT ");
23982401
break;

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

Lines changed: 62 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -747,62 +747,70 @@ void OmpStructureChecker::CheckSIMDNest(const parser::OpenMPConstruct &c) {
747747
// current context yet.
748748
// TODO: Check for declare simd regions.
749749
bool eligibleSIMD{false};
750-
common::visit(Fortran::common::visitors{
751-
// Allow `!$OMP ORDERED SIMD`
752-
[&](const parser::OpenMPBlockConstruct &c) {
753-
const auto &beginBlockDir{
754-
std::get<parser::OmpBeginBlockDirective>(c.t)};
755-
const auto &beginDir{
756-
std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
757-
if (beginDir.v == llvm::omp::Directive::OMPD_ordered) {
758-
const auto &clauses{
759-
std::get<parser::OmpClauseList>(beginBlockDir.t)};
760-
for (const auto &clause : clauses.v) {
761-
if (std::get_if<parser::OmpClause::Simd>(&clause.u)) {
762-
eligibleSIMD = true;
763-
break;
764-
}
765-
}
766-
}
767-
},
768-
[&](const parser::OpenMPSimpleStandaloneConstruct &c) {
769-
const auto &dir{
770-
std::get<parser::OmpSimpleStandaloneDirective>(c.t)};
771-
if (dir.v == llvm::omp::Directive::OMPD_ordered) {
772-
const auto &clauses{
773-
std::get<parser::OmpClauseList>(c.t)};
774-
for (const auto &clause : clauses.v) {
775-
if (std::get_if<parser::OmpClause::Simd>(&clause.u)) {
776-
eligibleSIMD = true;
777-
break;
778-
}
779-
}
780-
}
781-
},
782-
// Allowing SIMD construct
783-
[&](const parser::OpenMPLoopConstruct &c) {
784-
const auto &beginLoopDir{
785-
std::get<parser::OmpBeginLoopDirective>(c.t)};
786-
const auto &beginDir{
787-
std::get<parser::OmpLoopDirective>(beginLoopDir.t)};
788-
if ((beginDir.v == llvm::omp::Directive::OMPD_simd) ||
789-
(beginDir.v == llvm::omp::Directive::OMPD_do_simd)) {
790-
eligibleSIMD = true;
791-
}
792-
},
793-
[&](const parser::OpenMPAtomicConstruct &c) {
794-
// Allow `!$OMP ATOMIC`
795-
eligibleSIMD = true;
796-
},
797-
[&](const auto &c) {},
798-
},
750+
common::visit(
751+
Fortran::common::visitors{
752+
// Allow `!$OMP ORDERED SIMD`
753+
[&](const parser::OpenMPBlockConstruct &c) {
754+
const auto &beginBlockDir{
755+
std::get<parser::OmpBeginBlockDirective>(c.t)};
756+
const auto &beginDir{
757+
std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
758+
if (beginDir.v == llvm::omp::Directive::OMPD_ordered) {
759+
const auto &clauses{
760+
std::get<parser::OmpClauseList>(beginBlockDir.t)};
761+
for (const auto &clause : clauses.v) {
762+
if (std::get_if<parser::OmpClause::Simd>(&clause.u)) {
763+
eligibleSIMD = true;
764+
break;
765+
}
766+
}
767+
}
768+
},
769+
[&](const parser::OpenMPStandaloneConstruct &c) {
770+
if (const auto &simpleConstruct =
771+
std::get_if<parser::OpenMPSimpleStandaloneConstruct>(
772+
&c.u)) {
773+
const auto &dir{std::get<parser::OmpSimpleStandaloneDirective>(
774+
simpleConstruct->t)};
775+
if (dir.v == llvm::omp::Directive::OMPD_ordered) {
776+
const auto &clauses{
777+
std::get<parser::OmpClauseList>(simpleConstruct->t)};
778+
for (const auto &clause : clauses.v) {
779+
if (std::get_if<parser::OmpClause::Simd>(&clause.u)) {
780+
eligibleSIMD = true;
781+
break;
782+
}
783+
}
784+
} else if (dir.v == llvm::omp::Directive::OMPD_scan) {
785+
eligibleSIMD = true;
786+
}
787+
}
788+
},
789+
// Allowing SIMD construct
790+
[&](const parser::OpenMPLoopConstruct &c) {
791+
const auto &beginLoopDir{
792+
std::get<parser::OmpBeginLoopDirective>(c.t)};
793+
const auto &beginDir{
794+
std::get<parser::OmpLoopDirective>(beginLoopDir.t)};
795+
if ((beginDir.v == llvm::omp::Directive::OMPD_simd) ||
796+
(beginDir.v == llvm::omp::Directive::OMPD_parallel_do_simd) ||
797+
(beginDir.v == llvm::omp::Directive::OMPD_do_simd)) {
798+
eligibleSIMD = true;
799+
}
800+
},
801+
[&](const parser::OpenMPAtomicConstruct &c) {
802+
// Allow `!$OMP ATOMIC`
803+
eligibleSIMD = true;
804+
},
805+
[&](const auto &c) {},
806+
},
799807
c.u);
800808
if (!eligibleSIMD) {
801809
context_.Say(parser::FindSourceLocation(c),
802810
"The only OpenMP constructs that can be encountered during execution "
803811
"of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, "
804-
"the `SIMD` construct and the `ORDERED` construct with the `SIMD` "
805-
"clause."_err_en_US);
812+
"the `SIMD` construct, the `SCAN` construct and the `ORDERED` "
813+
"construct with the `SIMD` clause."_err_en_US);
806814
}
807815
}
808816

@@ -2985,6 +2993,9 @@ void OmpStructureChecker::CheckReductionModifier(
29852993
switch (dirCtx.directive) {
29862994
case llvm::omp::Directive::OMPD_do: // worksharing-loop
29872995
case llvm::omp::Directive::OMPD_do_simd: // worksharing-loop simd
2996+
case llvm::omp::Directive::
2997+
OMPD_parallel_do_simd: // worksharing-parallel-loop
2998+
// simd
29882999
case llvm::omp::Directive::OMPD_simd: // "simd"
29893000
break;
29903001
default:

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,9 +1623,11 @@ bool OmpAttributeVisitor::Pre(
16231623
const parser::OpenMPSimpleStandaloneConstruct &x) {
16241624
const auto &standaloneDir{
16251625
std::get<parser::OmpSimpleStandaloneDirective>(x.t)};
1626+
const auto &parentContext{GetContextIf()};
16261627
switch (standaloneDir.v) {
16271628
case llvm::omp::Directive::OMPD_barrier:
16281629
case llvm::omp::Directive::OMPD_ordered:
1630+
case llvm::omp::Directive::OMPD_scan:
16291631
case llvm::omp::Directive::OMPD_target_enter_data:
16301632
case llvm::omp::Directive::OMPD_target_exit_data:
16311633
case llvm::omp::Directive::OMPD_target_update:
@@ -1636,6 +1638,20 @@ bool OmpAttributeVisitor::Pre(
16361638
default:
16371639
break;
16381640
}
1641+
if (standaloneDir.v == llvm::omp::Directive::OMPD_scan) {
1642+
if ((std::get<parser::OmpClauseList>(x.t).v.size() != 1)) {
1643+
context_.Say(standaloneDir.source,
1644+
"Exactly one of `exclusive` or `inclusive` clause is expected"_err_en_US);
1645+
}
1646+
if (!parentContext ||
1647+
(llvm::omp::getDirectiveAssociation(parentContext->directive) !=
1648+
llvm::omp::Association::Loop)) {
1649+
context_.Say(standaloneDir.source,
1650+
"Orphaned `omp scan` directives are prohibited; perhaps you forgot "
1651+
"to enclose the directive in to a worksharing loop, a worksharing "
1652+
"loop simd or a simd directive."_err_en_US);
1653+
}
1654+
}
16391655
ClearDataSharingAttributeObjects();
16401656
return true;
16411657
}

flang/test/Parser/OpenMP/scan.f90

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
! RUN: %flang_fc1 -fdebug-unparse -fopenmp %s | FileCheck --ignore-case %s
2+
! RUN: %flang_fc1 -fdebug-dump-parse-tree -fopenmp %s | FileCheck --check-prefix="PARSE-TREE" %s
3+
4+
! Check for parsing scan directive
5+
subroutine test_scan_inclusive()
6+
implicit none
7+
integer, parameter :: n = 100
8+
integer a(n), b(n)
9+
integer x, k
10+
11+
! initialization
12+
x = 0
13+
do k = 1, n
14+
a(k) = k
15+
end do
16+
17+
! a(k) is included in the computation of producing results in b(k)
18+
!$omp parallel do simd reduction(inscan,+: x)
19+
do k = 1, n
20+
x = x + a(k)
21+
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPStandaloneConstruct -> OpenMPSimpleStandaloneConstruct
22+
!PARSE-TREE-NEXT: OmpSimpleStandaloneDirective -> llvm::omp::Directive = scan
23+
!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> Inclusive -> OmpObjectList -> OmpObject -> Designator -> DataRef -> Name = 'x'
24+
!CHECK: !$omp scan inclusive(x)
25+
!$omp scan inclusive(x)
26+
b(k) = x
27+
end do
28+
29+
! a(k) is not included in the computation of producing results in b(k)
30+
!$omp parallel do simd reduction(inscan,+: x)
31+
do k = 1, n
32+
b(k) = x
33+
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPStandaloneConstruct -> OpenMPSimpleStandaloneConstruct
34+
!PARSE-TREE-NEXT: OmpSimpleStandaloneDirective -> llvm::omp::Directive = scan
35+
!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> Exclusive -> OmpObjectList -> OmpObject -> Designator -> DataRef -> Name = 'x'
36+
!CHECK: !$omp scan exclusive(x)
37+
!$omp scan exclusive(x)
38+
x = x + a(k)
39+
end do
40+
end subroutine

flang/test/Semantics/OpenMP/do05.f90

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ program omp_do
3939
if( i == 5 ) then
4040
cycle
4141
end if
42-
!ERROR: The only OpenMP constructs that can be encountered during execution of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD` construct and the `ORDERED` construct with the `SIMD` clause.
42+
!ERROR: The only OpenMP constructs that can be encountered during execution of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD` construct, the `SCAN` construct and the `ORDERED` construct with the `SIMD` clause.
4343
!ERROR: A worksharing region may not be closely nested inside a worksharing, explicit task, taskloop, critical, ordered, atomic, or master region
4444
!$omp single
4545
do j=1,10
@@ -70,7 +70,7 @@ program omp_do
7070
if( i == 3 ) then
7171
cycle
7272
end if
73-
!ERROR: The only OpenMP constructs that can be encountered during execution of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD` construct and the `ORDERED` construct with the `SIMD` clause.
73+
!ERROR: The only OpenMP constructs that can be encountered during execution of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD` construct, the `SCAN` construct and the `ORDERED` construct with the `SIMD` clause.
7474
!ERROR: A worksharing region may not be closely nested inside a worksharing, explicit task, taskloop, critical, ordered, atomic, or master region
7575
!$omp single
7676
do j=1,10
@@ -93,7 +93,7 @@ program omp_do
9393

9494
!$omp target parallel do simd
9595
do i=1,10
96-
!ERROR: The only OpenMP constructs that can be encountered during execution of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD` construct and the `ORDERED` construct with the `SIMD` clause.
96+
!ERROR: The only OpenMP constructs that can be encountered during execution of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD` construct, the `SCAN` construct and the `ORDERED` construct with the `SIMD` clause.
9797
!ERROR: A worksharing region may not be closely nested inside a worksharing, explicit task, taskloop, critical, ordered, atomic, or master region
9898
!$omp single
9999
do j=1,10
@@ -116,7 +116,7 @@ program omp_do
116116

117117
!$omp target teams distribute parallel do simd
118118
do i=1,10
119-
!ERROR: The only OpenMP constructs that can be encountered during execution of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD` construct and the `ORDERED` construct with the `SIMD` clause.
119+
!ERROR: The only OpenMP constructs that can be encountered during execution of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD` construct, the `SCAN` construct and the `ORDERED` construct with the `SIMD` clause.
120120
!ERROR: A worksharing region may not be closely nested inside a worksharing, explicit task, taskloop, critical, ordered, atomic, or master region
121121
!$omp single
122122
do j=1,10

flang/test/Semantics/OpenMP/nested-barrier.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ program omp_nest_barrier
1717
!$omp do simd
1818
do i = 1, 10
1919
k = k + 1
20-
!ERROR: The only OpenMP constructs that can be encountered during execution of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD` construct and the `ORDERED` construct with the `SIMD` clause.
20+
!ERROR: The only OpenMP constructs that can be encountered during execution of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD` construct, the `SCAN` construct and the `ORDERED` construct with the `SIMD` clause.
2121
!ERROR: `BARRIER` region may not be closely nested inside of `WORKSHARING`, `LOOP`, `TASK`, `TASKLOOP`,`CRITICAL`, `ORDERED`, `ATOMIC` or `MASTER` region.
2222
!$omp barrier
2323
j = j -1
@@ -34,7 +34,7 @@ program omp_nest_barrier
3434
!$omp parallel do simd
3535
do i = 1, 10
3636
k = k + 1
37-
!ERROR: The only OpenMP constructs that can be encountered during execution of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD` construct and the `ORDERED` construct with the `SIMD` clause.
37+
!ERROR: The only OpenMP constructs that can be encountered during execution of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD` construct, the `SCAN` construct and the `ORDERED` construct with the `SIMD` clause.
3838
!ERROR: `BARRIER` region may not be closely nested inside of `WORKSHARING`, `LOOP`, `TASK`, `TASKLOOP`,`CRITICAL`, `ORDERED`, `ATOMIC` or `MASTER` region.
3939
!$omp barrier
4040
j = j -1

flang/test/Semantics/OpenMP/nested-master.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ program omp_nest_master
6464
do i = 1, 10
6565
k = k + 1
6666
!WARNING: OpenMP directive MASTER has been deprecated, please use MASKED instead.
67-
!ERROR: The only OpenMP constructs that can be encountered during execution of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD` construct and the `ORDERED` construct with the `SIMD` clause.
67+
!ERROR: The only OpenMP constructs that can be encountered during execution of a 'SIMD' region are the `ATOMIC` construct, the `LOOP` construct, the `SIMD` construct, the `SCAN` construct and the `ORDERED` construct with the `SIMD` clause.
6868
!ERROR: `MASTER` region may not be closely nested inside of `WORKSHARING`, `LOOP`, `TASK`, `TASKLOOP`, or `ATOMIC` region.
6969
!$omp master
7070
j = j -1

0 commit comments

Comments
 (0)