Skip to content

Commit 843c2fb

Browse files
Add parser+semantics support for scope construct (#113700)
Test parsing, semantics and a couple of basic semantic checks for block/worksharing constructs. Add TODO message in lowering.
1 parent 14db069 commit 843c2fb

File tree

11 files changed

+87
-2
lines changed

11 files changed

+87
-2
lines changed

flang/include/flang/Semantics/openmp-directive-sets.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ static const OmpDirectiveSet blockConstructSet{
211211
Directive::OMPD_parallel,
212212
Directive::OMPD_parallel_masked,
213213
Directive::OMPD_parallel_workshare,
214+
Directive::OMPD_scope,
214215
Directive::OMPD_single,
215216
Directive::OMPD_target,
216217
Directive::OMPD_target_data,
@@ -281,6 +282,7 @@ static const OmpDirectiveSet workShareSet{
281282
Directive::OMPD_workshare,
282283
Directive::OMPD_parallel_workshare,
283284
Directive::OMPD_parallel_sections,
285+
Directive::OMPD_scope,
284286
Directive::OMPD_sections,
285287
Directive::OMPD_single,
286288
} | allDoSet,

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,15 @@ genSectionsOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
16501650
return sectionsOp;
16511651
}
16521652

1653+
static void genScopeOp(lower::AbstractConverter &converter,
1654+
lower::SymMap &symTable,
1655+
semantics::SemanticsContext &semaCtx,
1656+
lower::pft::Evaluation &eval, mlir::Location loc,
1657+
const ConstructQueue &queue,
1658+
ConstructQueue::const_iterator item) {
1659+
TODO(loc, "Scope construct");
1660+
}
1661+
16531662
static mlir::omp::SingleOp
16541663
genSingleOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
16551664
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
@@ -2478,6 +2487,9 @@ static void genOMPDispatch(lower::AbstractConverter &converter,
24782487
case llvm::omp::Directive::OMPD_simd:
24792488
genStandaloneSimd(converter, symTable, semaCtx, eval, loc, queue, item);
24802489
break;
2490+
case llvm::omp::Directive::OMPD_scope:
2491+
genScopeOp(converter, symTable, semaCtx, eval, loc, queue, item);
2492+
break;
24812493
case llvm::omp::Directive::OMPD_single:
24822494
genSingleOp(converter, symTable, semaCtx, eval, loc, queue, item);
24832495
break;

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ TYPE_PARSER(construct<OmpBlockDirective>(first(
697697
"PARALLEL MASKED" >> pure(llvm::omp::Directive::OMPD_parallel_masked),
698698
"PARALLEL WORKSHARE" >> pure(llvm::omp::Directive::OMPD_parallel_workshare),
699699
"PARALLEL" >> pure(llvm::omp::Directive::OMPD_parallel),
700+
"SCOPE" >> pure(llvm::omp::Directive::OMPD_scope),
700701
"SINGLE" >> pure(llvm::omp::Directive::OMPD_single),
701702
"TARGET DATA" >> pure(llvm::omp::Directive::OMPD_target_data),
702703
"TARGET PARALLEL" >> pure(llvm::omp::Directive::OMPD_target_parallel),

flang/lib/Parser/unparse.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,6 +2386,9 @@ class UnparseVisitor {
23862386
case llvm::omp::Directive::OMPD_parallel:
23872387
Word("PARALLEL ");
23882388
break;
2389+
case llvm::omp::Directive::OMPD_scope:
2390+
Word("SCOPE ");
2391+
break;
23892392
case llvm::omp::Directive::OMPD_single:
23902393
Word("SINGLE ");
23912394
break;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,7 @@ void OmpStructureChecker::Enter(const parser::OpenMPBlockConstruct &x) {
972972
HasInvalidWorksharingNesting(
973973
beginDir.source, llvm::omp::nestedWorkshareErrSet);
974974
break;
975+
case llvm::omp::Directive::OMPD_scope:
975976
case llvm::omp::Directive::OMPD_single:
976977
// TODO: This check needs to be extended while implementing nesting of
977978
// regions checks.
@@ -1864,6 +1865,9 @@ void OmpStructureChecker::Enter(const parser::OmpEndBlockDirective &x) {
18641865
const auto &dir{std::get<parser::OmpBlockDirective>(x.t)};
18651866
ResetPartialContext(dir.source);
18661867
switch (dir.v) {
1868+
case llvm::omp::Directive::OMPD_scope:
1869+
PushContextAndClauseSets(dir.source, llvm::omp::Directive::OMPD_end_scope);
1870+
break;
18671871
// 2.7.3 end-single-clause -> copyprivate-clause |
18681872
// nowait-clause
18691873
case llvm::omp::Directive::OMPD_single:
@@ -1886,7 +1890,8 @@ void OmpStructureChecker::Enter(const parser::OmpEndBlockDirective &x) {
18861890
// end_workshareare popped as they are pushed while entering the
18871891
// EndBlockDirective.
18881892
void OmpStructureChecker::Leave(const parser::OmpEndBlockDirective &x) {
1889-
if ((GetContext().directive == llvm::omp::Directive::OMPD_end_single) ||
1893+
if ((GetContext().directive == llvm::omp::Directive::OMPD_end_scope) ||
1894+
(GetContext().directive == llvm::omp::Directive::OMPD_end_single) ||
18901895
(GetContext().directive == llvm::omp::Directive::OMPD_end_workshare)) {
18911896
dirContext_.pop_back();
18921897
}

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,7 @@ bool OmpAttributeVisitor::Pre(const parser::OpenMPBlockConstruct &x) {
15261526
case llvm::omp::Directive::OMPD_master:
15271527
case llvm::omp::Directive::OMPD_ordered:
15281528
case llvm::omp::Directive::OMPD_parallel:
1529+
case llvm::omp::Directive::OMPD_scope:
15291530
case llvm::omp::Directive::OMPD_single:
15301531
case llvm::omp::Directive::OMPD_target:
15311532
case llvm::omp::Directive::OMPD_target_data:
@@ -1557,6 +1558,7 @@ void OmpAttributeVisitor::Post(const parser::OpenMPBlockConstruct &x) {
15571558
case llvm::omp::Directive::OMPD_masked:
15581559
case llvm::omp::Directive::OMPD_parallel_masked:
15591560
case llvm::omp::Directive::OMPD_parallel:
1561+
case llvm::omp::Directive::OMPD_scope:
15601562
case llvm::omp::Directive::OMPD_single:
15611563
case llvm::omp::Directive::OMPD_target:
15621564
case llvm::omp::Directive::OMPD_task:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
! RUN: %not_todo_cmd bbc -emit-fir -fopenmp -o - %s -fopenmp-version=51 2>&1 | FileCheck %s
2+
! RUN: %not_todo_cmd %flang_fc1 -emit-fir -fopenmp -o - %s -fopenmp-version=51 2>&1 | FileCheck %s
3+
4+
! CHECK: not yet implemented: Scope construct
5+
program omp_scope
6+
integer i
7+
i = 10
8+
9+
!$omp scope private(i)
10+
print *, "omp scope", i
11+
!$omp end scope
12+
13+
end program omp_scope

flang/test/Parser/OpenMP/scope.f90

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
! RUN: %flang_fc1 -fdebug-unparse -fopenmp -fopenmp-version=51 %s | FileCheck --ignore-case %s
2+
! RUN: %flang_fc1 -fdebug-dump-parse-tree -fopenmp -fopenmp-version=51 %s | FileCheck --check-prefix="PARSE-TREE" %s
3+
4+
program omp_scope
5+
integer i
6+
i = 10
7+
8+
!CHECK: !$OMP SCOPE PRIVATE(i)
9+
!CHECK: !$OMP END SCOPE
10+
11+
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPBlockConstruct
12+
!PARSE-TREE: OmpBeginBlockDirective
13+
!PARSE-TREE: OmpBlockDirective -> llvm::omp::Directive = scope
14+
!PARSE-TREE: OmpClauseList -> OmpClause -> Private -> OmpObjectList -> OmpObject -> Designator -> DataRef -> Name = 'i'
15+
!PARSE-TREE: Block
16+
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> ActionStmt -> PrintStmt
17+
!PARSE-TREE: OmpEndBlockDirective
18+
!PARSE-TREE: OmpBlockDirective -> llvm::omp::Directive = scope
19+
!PARSE-TREE: OmpClauseList -> OmpClause -> Nowait
20+
21+
!$omp scope private(i)
22+
print *, "omp scope", i
23+
!$omp end scope nowait
24+
end program omp_scope

flang/test/Semantics/OpenMP/invalid-branch.f90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,12 @@ program omp_invalid_branch
105105
!$omp end parallel
106106
9 print *, "2nd alternate return"
107107

108+
!CHECK: invalid branch into an OpenMP structured block
109+
goto 100
110+
!$omp scope
111+
100 continue
112+
!CHECK: invalid branch leaving an OpenMP structured block
113+
goto 200
114+
!$omp end scope
115+
200 continue
108116
end program

flang/test/Semantics/OpenMP/nested01.f90

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
!$omp end target
2626
enddo
2727

28+
!$omp do
29+
do i = 1, N
30+
!ERROR: A worksharing region may not be closely nested inside a worksharing, explicit task, taskloop, critical, ordered, atomic, or master region
31+
!$omp scope
32+
!$omp end scope
33+
end do
34+
!$omp end do
2835

2936
!$omp do
3037
do i = 1, N

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ def OMP_Scan : Directive<"scan"> {
892892
let association = AS_Separating;
893893
let category = CA_Subsidiary;
894894
}
895-
def OMP_scope : Directive<"scope"> {
895+
def OMP_Scope : Directive<"scope"> {
896896
let allowedClauses = [
897897
VersionedClause<OMPC_Private, 51>,
898898
VersionedClause<OMPC_Reduction, 51>,
@@ -905,6 +905,14 @@ def OMP_scope : Directive<"scope"> {
905905
let association = AS_Block;
906906
let category = CA_Executable;
907907
}
908+
def OMP_EndScope : Directive<"end scope"> {
909+
let allowedOnceClauses = [
910+
VersionedClause<OMPC_NoWait>,
911+
];
912+
let leafConstructs = OMP_Scope.leafConstructs;
913+
let association = OMP_Scope.association;
914+
let category = OMP_Scope.category;
915+
}
908916
def OMP_Section : Directive<"section"> {
909917
let association = AS_Separating;
910918
let category = CA_Subsidiary;

0 commit comments

Comments
 (0)