Skip to content

Commit 6658e1a

Browse files
authored
Adding parsing and semantic check support for omp masked (#91432)
omp masked directive in OpenMP 5.2 allows to specify code regions which are expected to be executed by thread ids specified by the programmer. Filter clause of the directive allows to specify the thread id. This change adds the parsing support for the directive
1 parent c9dc52d commit 6658e1a

File tree

8 files changed

+158
-2
lines changed

8 files changed

+158
-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
@@ -205,9 +205,11 @@ static const OmpDirectiveSet compositeConstructSet{
205205
};
206206

207207
static const OmpDirectiveSet blockConstructSet{
208+
Directive::OMPD_masked,
208209
Directive::OMPD_master,
209210
Directive::OMPD_ordered,
210211
Directive::OMPD_parallel,
212+
Directive::OMPD_parallel_masked,
211213
Directive::OMPD_parallel_workshare,
212214
Directive::OMPD_single,
213215
Directive::OMPD_target,

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,8 +1892,7 @@ static void genOMPDispatch(lower::AbstractConverter &converter,
18921892
break;
18931893
case llvm::omp::Directive::OMPD_loop:
18941894
case llvm::omp::Directive::OMPD_masked:
1895-
TODO(loc, "Unhandled loop directive (" +
1896-
llvm::omp::getOpenMPDirectiveName(dir) + ")");
1895+
TODO(loc, "Unhandled directive " + llvm::omp::getOpenMPDirectiveName(dir));
18971896
break;
18981897
case llvm::omp::Directive::OMPD_master:
18991898
genMasterOp(converter, symTable, semaCtx, eval, loc, queue, item);

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ TYPE_PARSER(
266266
construct<OmpClause>(construct<OmpClause::DynamicAllocators>()) ||
267267
"ENTER" >> construct<OmpClause>(construct<OmpClause::Enter>(
268268
parenthesized(Parser<OmpObjectList>{}))) ||
269+
"FILTER" >> construct<OmpClause>(construct<OmpClause::Filter>(
270+
parenthesized(scalarIntExpr))) ||
269271
"FINAL" >> construct<OmpClause>(construct<OmpClause::Final>(
270272
parenthesized(scalarLogicalExpr))) ||
271273
"FULL" >> construct<OmpClause>(construct<OmpClause::Full>()) ||
@@ -376,8 +378,15 @@ TYPE_PARSER(sourced(construct<OmpLoopDirective>(first(
376378
"DISTRIBUTE" >> pure(llvm::omp::Directive::OMPD_distribute),
377379
"DO SIMD" >> pure(llvm::omp::Directive::OMPD_do_simd),
378380
"DO" >> pure(llvm::omp::Directive::OMPD_do),
381+
"MASKED TASKLOOP SIMD" >>
382+
pure(llvm::omp::Directive::OMPD_masked_taskloop_simd),
383+
"MASKED TASKLOOP" >> pure(llvm::omp::Directive::OMPD_masked_taskloop),
379384
"PARALLEL DO SIMD" >> pure(llvm::omp::Directive::OMPD_parallel_do_simd),
380385
"PARALLEL DO" >> pure(llvm::omp::Directive::OMPD_parallel_do),
386+
"PARALLEL MASKED TASKLOOP SIMD" >>
387+
pure(llvm::omp::Directive::OMPD_parallel_masked_taskloop_simd),
388+
"PARALLEL MASKED TASKLOOP" >>
389+
pure(llvm::omp::Directive::OMPD_parallel_masked_taskloop),
381390
"SIMD" >> pure(llvm::omp::Directive::OMPD_simd),
382391
"TARGET PARALLEL DO SIMD" >>
383392
pure(llvm::omp::Directive::OMPD_target_parallel_do_simd),
@@ -487,8 +496,10 @@ TYPE_PARSER(
487496

488497
// Directives enclosing structured-block
489498
TYPE_PARSER(construct<OmpBlockDirective>(first(
499+
"MASKED" >> pure(llvm::omp::Directive::OMPD_masked),
490500
"MASTER" >> pure(llvm::omp::Directive::OMPD_master),
491501
"ORDERED" >> pure(llvm::omp::Directive::OMPD_ordered),
502+
"PARALLEL MASKED" >> pure(llvm::omp::Directive::OMPD_parallel_masked),
492503
"PARALLEL WORKSHARE" >> pure(llvm::omp::Directive::OMPD_parallel_workshare),
493504
"PARALLEL" >> pure(llvm::omp::Directive::OMPD_parallel),
494505
"SINGLE" >> pure(llvm::omp::Directive::OMPD_single),

flang/lib/Parser/unparse.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,12 +2194,24 @@ class UnparseVisitor {
21942194
case llvm::omp::Directive::OMPD_do_simd:
21952195
Word("DO SIMD ");
21962196
break;
2197+
case llvm::omp::Directive::OMPD_masked_taskloop_simd:
2198+
Word("MASKED TASKLOOP SIMD");
2199+
break;
2200+
case llvm::omp::Directive::OMPD_masked_taskloop:
2201+
Word("MASKED TASKLOOP");
2202+
break;
21972203
case llvm::omp::Directive::OMPD_parallel_do:
21982204
Word("PARALLEL DO ");
21992205
break;
22002206
case llvm::omp::Directive::OMPD_parallel_do_simd:
22012207
Word("PARALLEL DO SIMD ");
22022208
break;
2209+
case llvm::omp::Directive::OMPD_parallel_masked_taskloop_simd:
2210+
Word("PARALLEL MASKED TASKLOOP SIMD");
2211+
break;
2212+
case llvm::omp::Directive::OMPD_parallel_masked_taskloop:
2213+
Word("PARALLEL MASKED TASKLOOP");
2214+
break;
22032215
case llvm::omp::Directive::OMPD_simd:
22042216
Word("SIMD ");
22052217
break;
@@ -2283,12 +2295,18 @@ class UnparseVisitor {
22832295
}
22842296
void Unparse(const OmpBlockDirective &x) {
22852297
switch (x.v) {
2298+
case llvm::omp::Directive::OMPD_masked:
2299+
Word("MASKED");
2300+
break;
22862301
case llvm::omp::Directive::OMPD_master:
22872302
Word("MASTER");
22882303
break;
22892304
case llvm::omp::Directive::OMPD_ordered:
22902305
Word("ORDERED ");
22912306
break;
2307+
case llvm::omp::Directive::OMPD_parallel_masked:
2308+
Word("PARALLEL MASKED");
2309+
break;
22922310
case llvm::omp::Directive::OMPD_parallel_workshare:
22932311
Word("PARALLEL WORKSHARE ");
22942312
break;

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,8 @@ bool OmpAttributeVisitor::Pre(const parser::OpenMPBlockConstruct &x) {
15031503
const auto &beginBlockDir{std::get<parser::OmpBeginBlockDirective>(x.t)};
15041504
const auto &beginDir{std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
15051505
switch (beginDir.v) {
1506+
case llvm::omp::Directive::OMPD_masked:
1507+
case llvm::omp::Directive::OMPD_parallel_masked:
15061508
case llvm::omp::Directive::OMPD_master:
15071509
case llvm::omp::Directive::OMPD_ordered:
15081510
case llvm::omp::Directive::OMPD_parallel:
@@ -1532,6 +1534,8 @@ void OmpAttributeVisitor::Post(const parser::OpenMPBlockConstruct &x) {
15321534
const auto &beginBlockDir{std::get<parser::OmpBeginBlockDirective>(x.t)};
15331535
const auto &beginDir{std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
15341536
switch (beginDir.v) {
1537+
case llvm::omp::Directive::OMPD_masked:
1538+
case llvm::omp::Directive::OMPD_parallel_masked:
15351539
case llvm::omp::Directive::OMPD_parallel:
15361540
case llvm::omp::Directive::OMPD_single:
15371541
case llvm::omp::Directive::OMPD_target:
@@ -1598,8 +1602,12 @@ bool OmpAttributeVisitor::Pre(const parser::OpenMPLoopConstruct &x) {
15981602
case llvm::omp::Directive::OMPD_distribute_simd:
15991603
case llvm::omp::Directive::OMPD_do:
16001604
case llvm::omp::Directive::OMPD_do_simd:
1605+
case llvm::omp::Directive::OMPD_masked_taskloop_simd:
1606+
case llvm::omp::Directive::OMPD_masked_taskloop:
16011607
case llvm::omp::Directive::OMPD_parallel_do:
16021608
case llvm::omp::Directive::OMPD_parallel_do_simd:
1609+
case llvm::omp::Directive::OMPD_parallel_masked_taskloop_simd:
1610+
case llvm::omp::Directive::OMPD_parallel_masked_taskloop:
16031611
case llvm::omp::Directive::OMPD_simd:
16041612
case llvm::omp::Directive::OMPD_target_parallel_do:
16051613
case llvm::omp::Directive::OMPD_target_parallel_do_simd:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
! This test checks lowering of OpenMP masked Directive.
2+
3+
! RUN: %not_todo_cmd bbc -emit-fir -fopenmp -o - %s 2>&1 | FileCheck %s
4+
! RUN: %not_todo_cmd %flang_fc1 -emit-fir -fopenmp -o - %s 2>&1 | FileCheck %s
5+
6+
! CHECK: not yet implemented: Unhandled directive masked
7+
subroutine test_masked()
8+
integer :: c = 1
9+
!$omp masked
10+
c = c + 1
11+
!$omp end masked
12+
end subroutine
13+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 of masked directive with filter clause.
5+
6+
7+
subroutine test_masked()
8+
integer :: c = 1
9+
!PARSE-TREE: OmpBeginBlockDirective
10+
!PARSE-TREE-NEXT: OmpBlockDirective -> llvm::omp::Directive = masked
11+
!CHECK: !$omp masked
12+
!$omp masked
13+
c = c + 1
14+
!$omp end masked
15+
!PARSE-TREE: OmpBeginBlockDirective
16+
!PARSE-TREE-NEXT: OmpBlockDirective -> llvm::omp::Directive = masked
17+
!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> Filter -> Scalar -> Integer -> Expr = '1_4'
18+
!PARSE-TREE-NEXT: LiteralConstant -> IntLiteralConstant = '1'
19+
!CHECK: !$omp masked filter(1_4)
20+
!$omp masked filter(1)
21+
c = c + 2
22+
!$omp end masked
23+
end subroutine
24+
25+
subroutine test_masked_taskloop_simd()
26+
integer :: i, j = 1
27+
!PARSE-TREE: OmpBeginLoopDirective
28+
!PARSE-TREE-NEXT: OmpLoopDirective -> llvm::omp::Directive = masked taskloop simd
29+
!CHECK: !$omp masked taskloop simd
30+
!$omp masked taskloop simd
31+
do i=1,10
32+
j = j + 1
33+
end do
34+
!$omp end masked taskloop simd
35+
end subroutine
36+
37+
subroutine test_masked_taskloop
38+
integer :: i, j = 1
39+
!PARSE-TREE: OmpBeginLoopDirective
40+
!PARSE-TREE-NEXT: OmpLoopDirective -> llvm::omp::Directive = masked taskloop
41+
!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> Filter -> Scalar -> Integer -> Expr = '2_4'
42+
!PARSE-TREE-NEXT: LiteralConstant -> IntLiteralConstant = '2'
43+
!CHECK: !$omp masked taskloop filter(2_4)
44+
!$omp masked taskloop filter(2)
45+
do i=1,10
46+
j = j + 1
47+
end do
48+
!$omp end masked taskloop
49+
end subroutine
50+
51+
subroutine test_parallel_masked
52+
integer, parameter :: i = 1, j = 1
53+
integer :: c = 2
54+
!PARSE-TREE: OmpBeginBlockDirective
55+
!PARSE-TREE-NEXT: OmpBlockDirective -> llvm::omp::Directive = parallel masked
56+
!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> Filter -> Scalar -> Integer -> Expr = '2_4'
57+
!PARSE-TREE-NEXT: Add
58+
!PARSE-TREE-NEXT: Expr = '1_4'
59+
!PARSE-TREE-NEXT: Designator -> DataRef -> Name = 'i'
60+
!PARSE-TREE-NEXT: Expr = '1_4'
61+
!PARSE-TREE-NEXT: Designator -> DataRef -> Name = 'j'
62+
!CHECK: !$omp parallel masked filter(2_4)
63+
!$omp parallel masked filter(i+j)
64+
c = c + 2
65+
!$omp end parallel masked
66+
end subroutine
67+
68+
subroutine test_parallel_masked_taskloop_simd
69+
integer :: i, j = 1
70+
!PARSE-TREE: OmpBeginLoopDirective
71+
!PARSE-TREE-NEXT: OmpLoopDirective -> llvm::omp::Directive = parallel masked taskloop simd
72+
!CHECK: !$omp parallel masked taskloop simd
73+
!$omp parallel masked taskloop simd
74+
do i=1,10
75+
j = j + 1
76+
end do
77+
!$omp end parallel masked taskloop simd
78+
end subroutine
79+
80+
subroutine test_parallel_masked_taskloop
81+
integer :: i, j = 1
82+
!PARSE-TREE: OmpBeginLoopDirective
83+
!PARSE-TREE-NEXT: OmpLoopDirective -> llvm::omp::Directive = parallel masked taskloop
84+
!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> Filter -> Scalar -> Integer -> Expr = '2_4'
85+
!PARSE-TREE-NEXT: LiteralConstant -> IntLiteralConstant = '2'
86+
!CHECK: !$omp parallel masked taskloop filter(2_4)
87+
!$omp parallel masked taskloop filter(2)
88+
do i=1,10
89+
j = j + 1
90+
end do
91+
!$omp end parallel masked taskloop
92+
end subroutine
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
2+
3+
subroutine test_masked()
4+
integer :: c = 1
5+
!ERROR: At most one FILTER clause can appear on the MASKED directive
6+
!$omp masked filter(1) filter(2)
7+
c = c + 1
8+
!$omp end masked
9+
!ERROR: NOWAIT clause is not allowed on the MASKED directive
10+
!$omp masked nowait
11+
c = c + 2
12+
!$omp end masked
13+
end subroutine

0 commit comments

Comments
 (0)