Skip to content

Commit ae1623b

Browse files
committed
[flang][Parser] Add a node for individual sections in sections construct
This patch adds parser nodes for each indivudual section in sections construct. This should help with the translation to FIR. `!$omp section` was not recognized as a construct and hence needed special handling. `OpenMPSectionsConstruct` contains a list of `OpenMPConstruct`. Each such `OpenMPConstruct` wraps an `OpenMPSectionConstruct` (section, not sections). An `OpenMPSectionConstruct` is a wrapper around a `Block`. Reviewed By: kiranchandramohan, peixin Differential Revision: https://reviews.llvm.org/D121680
1 parent 831ab35 commit ae1623b

File tree

10 files changed

+278
-10
lines changed

10 files changed

+278
-10
lines changed

flang/examples/FlangOmpReport/FlangOmpReportVisitor.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ SourcePosition OpenMPCounterVisitor::getLocation(const OpenMPConstruct &c) {
8585
},
8686
c.u);
8787
},
88+
[&](const OpenMPSectionConstruct &c) -> SourcePosition {
89+
const CharBlock &source{c.source};
90+
return (parsing->allCooked().GetSourcePositionRange(source))->first;
91+
},
8892
},
8993
c.u);
9094
}
@@ -134,6 +138,9 @@ std::string OpenMPCounterVisitor::getName(const OpenMPConstruct &c) {
134138
},
135139
c.u);
136140
},
141+
[&](const OpenMPSectionConstruct &c) -> std::string {
142+
return "section";
143+
},
137144
// OpenMPSectionsConstruct, OpenMPLoopConstruct,
138145
// OpenMPBlockConstruct, OpenMPCriticalConstruct Get the source from
139146
// the directive field of the begin directive or from the verbatim

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ class ParseTreeDumper {
562562
NODE(parser, OpenMPExecutableAllocate)
563563
NODE(parser, OpenMPSimpleStandaloneConstruct)
564564
NODE(parser, OpenMPStandaloneConstruct)
565+
NODE(parser, OpenMPSectionConstruct)
565566
NODE(parser, OpenMPSectionsConstruct)
566567
NODE(parser, OpenMPThreadprivate)
567568
NODE(parser, OpenStmt)

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3515,7 +3515,15 @@ struct OmpEndSectionsDirective {
35153515
// [!$omp section
35163516
// structured-block]
35173517
// ...
3518-
WRAPPER_CLASS(OmpSectionBlocks, std::list<Block>);
3518+
struct OpenMPSectionConstruct {
3519+
WRAPPER_CLASS_BOILERPLATE(OpenMPSectionConstruct, Block);
3520+
CharBlock source;
3521+
};
3522+
3523+
// `OmpSectionBlocks` is a list of section constructs. The parser guarentees
3524+
// that the `OpenMPConstruct` here always encapsulates an
3525+
// `OpenMPSectionConstruct` and not any other OpenMP construct.
3526+
WRAPPER_CLASS(OmpSectionBlocks, std::list<OpenMPConstruct>);
35193527

35203528
struct OpenMPSectionsConstruct {
35213529
TUPLE_CLASS_BOILERPLATE(OpenMPSectionsConstruct);
@@ -3817,9 +3825,9 @@ struct OpenMPLoopConstruct {
38173825
struct OpenMPConstruct {
38183826
UNION_CLASS_BOILERPLATE(OpenMPConstruct);
38193827
std::variant<OpenMPStandaloneConstruct, OpenMPSectionsConstruct,
3820-
OpenMPLoopConstruct, OpenMPBlockConstruct, OpenMPAtomicConstruct,
3821-
OpenMPDeclarativeAllocate, OpenMPExecutableAllocate,
3822-
OpenMPCriticalConstruct>
3828+
OpenMPSectionConstruct, OpenMPLoopConstruct, OpenMPBlockConstruct,
3829+
OpenMPAtomicConstruct, OpenMPDeclarativeAllocate,
3830+
OpenMPExecutableAllocate, OpenMPCriticalConstruct>
38233831
u;
38243832
};
38253833

flang/lib/Lower/OpenMP.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ void Fortran::lower::genOpenMPConstruct(
217217
&sectionsConstruct) {
218218
TODO(converter.getCurrentLocation(), "OpenMPSectionsConstruct");
219219
},
220+
[&](const Fortran::parser::OpenMPSectionConstruct &sectionConstruct) {
221+
TODO(converter.getCurrentLocation(), "OpenMPSectionConstruct");
222+
},
220223
[&](const Fortran::parser::OpenMPLoopConstruct &loopConstruct) {
221224
TODO(converter.getCurrentLocation(), "OpenMPLoopConstruct");
222225
},

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,15 @@ TYPE_PARSER(
529529
Parser<OmpClauseList>{})))
530530

531531
// OMP SECTION-BLOCK
532+
533+
TYPE_PARSER(construct<OpenMPSectionConstruct>(block))
534+
532535
TYPE_PARSER(maybe(startOmpLine >> "SECTION"_tok / endOmpLine) >>
533-
construct<OmpSectionBlocks>(
534-
nonemptySeparated(block, startOmpLine >> "SECTION"_tok / endOmpLine)))
536+
construct<OmpSectionBlocks>(nonemptySeparated(
537+
construct<OpenMPConstruct>(sourced(Parser<OpenMPSectionConstruct>{})),
538+
startOmpLine >> "SECTION"_tok / endOmpLine)))
535539

536-
// OMP SECTIONS (2.7.2), PARALLEL SECTIONS (2.11.2)
540+
// OMP SECTIONS (OpenMP 5.0 - 2.8.1), PARALLEL SECTIONS (OpenMP 5.0 - 2.13.3)
537541
TYPE_PARSER(construct<OpenMPSectionsConstruct>(
538542
Parser<OmpBeginSectionsDirective>{} / endOmpLine,
539543
Parser<OmpSectionBlocks>{}, Parser<OmpEndSectionsDirective>{} / endOmpLine))

flang/lib/Parser/unparse.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2405,7 +2405,9 @@ class UnparseVisitor {
24052405
Word("!$OMP SECTION");
24062406
Put("\n");
24072407
EndOpenMP();
2408-
Walk(y, ""); // y is Block
2408+
// y.u is an OpenMPSectionConstruct
2409+
// (y.u).v is Block
2410+
Walk(std::get<OpenMPSectionConstruct>(y.u).v, "");
24092411
}
24102412
}
24112413
void Unparse(const OpenMPSectionsConstruct &x) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,9 @@ void OmpStructureChecker::Enter(const parser::OpenMPSectionsConstruct &x) {
801801

802802
PushContextAndClauseSets(beginDir.source, beginDir.v);
803803
const auto &sectionBlocks{std::get<parser::OmpSectionBlocks>(x.t)};
804-
for (const auto &block : sectionBlocks.v) {
805-
CheckNoBranching(block, beginDir.v, beginDir.source);
804+
for (const parser::OpenMPConstruct &block : sectionBlocks.v) {
805+
CheckNoBranching(std::get<parser::OpenMPSectionConstruct>(block.u).v,
806+
beginDir.v, beginDir.source);
806807
}
807808
HasInvalidWorksharingNesting(
808809
beginDir.source, llvm::omp::nestedWorkshareErrSet);

flang/test/Examples/omp-sections.f90

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
! REQUIRES: plugins, examples, shell
2+
3+
! RUN: %flang_fc1 -load %llvmshlibdir/flangOmpReport.so -plugin flang-omp-report -fopenmp %s -o - | FileCheck %s
4+
5+
subroutine omp_sections()
6+
integer :: x
7+
!$omp sections private(x)
8+
!$omp section
9+
call f1()
10+
!$omp section
11+
call f2()
12+
!$omp end sections nowait
13+
end subroutine omp_sections
14+
15+
!CHECK: - file: {{.*}}
16+
!CHECK: line: 9
17+
!CHECK: construct: section
18+
!CHECK: clauses: []
19+
!CHECK: - file: {{.*}}
20+
!CHECK: line: 11
21+
!CHECK: construct: section
22+
!CHECK: clauses: []
23+
!CHECK: - file: {{.*}}
24+
!CHECK: line: 7
25+
!CHECK: construct: sections
26+
!CHECK: clauses:
27+
!CHECK: - clause: nowait
28+
!CHECK: details: ''
29+
!CHECK: - clause: private
30+
!CHECK: details: x
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
! RUN: %flang_fc1 -fdebug-pre-fir-tree -fopenmp %s | FileCheck %s
2+
3+
subroutine openmp_sections(x, y)
4+
5+
integer, intent(inout)::x, y
6+
7+
!==============================================================================
8+
! empty construct
9+
!==============================================================================
10+
!$omp sections
11+
!$omp end sections
12+
13+
!CHECK: OpenMPConstruct
14+
!CHECK: End OpenMPConstruct
15+
16+
!==============================================================================
17+
! single section, without `!$omp section`
18+
!==============================================================================
19+
!$omp sections
20+
call F1()
21+
!$omp end sections
22+
23+
!CHECK: OpenMPConstruct
24+
!CHECK: OpenMPConstruct
25+
!CHECK: CallStmt
26+
!CHECK: End OpenMPConstruct
27+
!CHECK: End OpenMPConstruct
28+
29+
!==============================================================================
30+
! single section with `!$omp section`
31+
!==============================================================================
32+
!$omp sections
33+
!$omp section
34+
call F1
35+
!$omp end sections
36+
37+
!CHECK: OpenMPConstruct
38+
!CHECK: OpenMPConstruct
39+
!CHECK: CallStmt
40+
!CHECK: End OpenMPConstruct
41+
!CHECK: End OpenMPConstruct
42+
43+
!==============================================================================
44+
! multiple sections
45+
!==============================================================================
46+
!$omp sections
47+
!$omp section
48+
call F1
49+
!$omp section
50+
call F2
51+
!$omp section
52+
call F3
53+
!$omp end sections
54+
55+
!CHECK: OpenMPConstruct
56+
!CHECK: OpenMPConstruct
57+
!CHECK: CallStmt
58+
!CHECK: End OpenMPConstruct
59+
!CHECK: OpenMPConstruct
60+
!CHECK: CallStmt
61+
!CHECK: End OpenMPConstruct
62+
!CHECK: OpenMPConstruct
63+
!CHECK: CallStmt
64+
!CHECK: End OpenMPConstruct
65+
!CHECK: End OpenMPConstruct
66+
67+
!==============================================================================
68+
! multiple sections with clauses
69+
!==============================================================================
70+
!$omp sections PRIVATE(x) FIRSTPRIVATE(y)
71+
!$omp section
72+
call F1
73+
!$omp section
74+
call F2
75+
!$omp section
76+
call F3
77+
!$omp end sections NOWAIT
78+
79+
!CHECK: OpenMPConstruct
80+
!CHECK: OpenMPConstruct
81+
!CHECK: CallStmt
82+
!CHECK: End OpenMPConstruct
83+
!CHECK: OpenMPConstruct
84+
!CHECK: CallStmt
85+
!CHECK: End OpenMPConstruct
86+
!CHECK: OpenMPConstruct
87+
!CHECK: CallStmt
88+
!CHECK: End OpenMPConstruct
89+
!CHECK: End OpenMPConstruct
90+
91+
end subroutine openmp_sections

flang/test/Parser/omp-sections.f90

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
subroutine openmp_sections(x, y)
5+
6+
integer, intent(inout)::x, y
7+
8+
!==============================================================================
9+
! empty construct
10+
!==============================================================================
11+
!CHECK: !$omp sections
12+
!$omp sections
13+
!CHECK: !$omp section
14+
!CHECK: !$omp end sections
15+
!$omp end sections
16+
17+
!PARSE-TREE: OpenMPConstruct -> OpenMPSectionsConstruct
18+
!PARSE-TREE: OmpBeginSectionsDirective
19+
!PARSE-TREE-NOT: ExecutionPartConstruct
20+
!PARSE-TREE: OmpEndSectionsDirective
21+
22+
!==============================================================================
23+
! single section, without `!$omp section`
24+
!==============================================================================
25+
!CHECK: !$omp sections
26+
!$omp sections
27+
!CHECK: !$omp section
28+
!CHECK: CALL
29+
call F1()
30+
!CHECK: !$omp end sections
31+
!$omp end sections
32+
33+
!PARSE-TREE: OpenMPConstruct -> OpenMPSectionsConstruct
34+
!PARSE-TREE: OmpBeginSectionsDirective
35+
!PARSE-TREE: OpenMPConstruct -> OpenMPSectionConstruct -> Block
36+
!PARSE-TREE: CallStmt
37+
!PARSE-TREE-NOT: ExecutionPartConstruct
38+
!PARSE-TREE: OmpEndSectionsDirective
39+
40+
!==============================================================================
41+
! single section with `!$omp section`
42+
!==============================================================================
43+
!CHECK: !$omp sections
44+
!$omp sections
45+
!CHECK: !$omp section
46+
!$omp section
47+
!CHECK: CALL F1
48+
call F1
49+
!CHECK: !$omp end sections
50+
!$omp end sections
51+
52+
!PARSE-TREE: OpenMPConstruct -> OpenMPSectionsConstruct
53+
!PARSE-TREE: OmpBeginSectionsDirective
54+
!PARSE-TREE: OpenMPConstruct -> OpenMPSectionConstruct -> Block
55+
!PARSE-TREE: CallStmt
56+
!PARSE-TREE-NOT: ExecutionPartConstruct
57+
!PARSE-TREE: OmpEndSectionsDirective
58+
59+
!==============================================================================
60+
! multiple sections
61+
!==============================================================================
62+
!CHECK: !$omp sections
63+
!$omp sections
64+
!CHECK: !$omp section
65+
!$omp section
66+
!CHECK: CALL F1
67+
call F1
68+
!CHECK: !$omp section
69+
!$omp section
70+
!CHECK: CALL F2
71+
call F2
72+
!CHECK: !$omp section
73+
!$omp section
74+
!CHECK: CALL F3
75+
call F3
76+
!CHECK: !$omp end sections
77+
!$omp end sections
78+
79+
!PARSE-TREE: OpenMPConstruct -> OpenMPSectionsConstruct
80+
!PARSE-TREE: OmpBeginSectionsDirective
81+
!PARSE-TREE: OpenMPConstruct -> OpenMPSectionConstruct -> Block
82+
!PARSE-TREE: CallStmt
83+
!PARSE-TREE: OpenMPConstruct -> OpenMPSectionConstruct -> Block
84+
!PARSE-TREE: CallStmt
85+
!PARSE-TREE: OpenMPConstruct -> OpenMPSectionConstruct -> Block
86+
!PARSE-TREE: CallStmt
87+
!PARSE-TREE-NOT: ExecutionPartConstruct
88+
!PARSE-TREE: OmpEndSectionsDirective
89+
90+
!==============================================================================
91+
! multiple sections with clauses
92+
!==============================================================================
93+
!CHECK: !$omp sections PRIVATE(x) FIRSTPRIVATE(y)
94+
!$omp sections PRIVATE(x) FIRSTPRIVATE(y)
95+
!CHECK: !$omp section
96+
!$omp section
97+
!CHECK: CALL F1
98+
call F1
99+
!CHECK: !$omp section
100+
!$omp section
101+
!CHECK: CALL F2
102+
call F2
103+
!CHECK: !$omp section
104+
!$omp section
105+
!CHECK: CALL F3
106+
call F3
107+
!CHECK: !$omp end sections NOWAIT
108+
!$omp end sections NOWAIT
109+
110+
!PARSE-TREE: OpenMPConstruct -> OpenMPSectionsConstruct
111+
!PARSE-TREE: OmpBeginSectionsDirective
112+
!PARSE-TREE: OpenMPConstruct -> OpenMPSectionConstruct -> Block
113+
!PARSE-TREE: CallStmt
114+
!PARSE-TREE: OpenMPConstruct -> OpenMPSectionConstruct -> Block
115+
!PARSE-TREE: CallStmt
116+
!PARSE-TREE: OpenMPConstruct -> OpenMPSectionConstruct -> Block
117+
!PARSE-TREE: CallStmt
118+
!PARSE-TREE-NOT: ExecutionPartConstruct
119+
!PARSE-TREE: OmpEndSectionsDirective
120+
121+
END subroutine openmp_sections

0 commit comments

Comments
 (0)