Skip to content

Commit 5621929

Browse files
[Flang][OpenMP] Add parser support for grainsize and num_tasks clause (#113136)
These clauses are applicable only for the taskloop directive. Since the directive has a TODO error, skipping the addition of TODOs for these clauses.
1 parent eef3766 commit 5621929

File tree

9 files changed

+122
-15
lines changed

9 files changed

+122
-15
lines changed

flang/examples/FeatureList/FeatureList.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ struct NodeVisitor {
483483
READ_FEATURE(OmpEndCriticalDirective)
484484
READ_FEATURE(OmpEndLoopDirective)
485485
READ_FEATURE(OmpEndSectionsDirective)
486+
READ_FEATURE(OmpGrainsizeClause)
487+
READ_FEATURE(OmpGrainsizeClause::Prescriptiveness)
486488
READ_FEATURE(OmpIfClause)
487489
READ_FEATURE(OmpIfClause::DirectiveNameModifier)
488490
READ_FEATURE(OmpLinearClause)
@@ -494,6 +496,8 @@ struct NodeVisitor {
494496
READ_FEATURE(OmpMapClause)
495497
READ_FEATURE(OmpMapClause::TypeModifier)
496498
READ_FEATURE(OmpMapClause::Type)
499+
READ_FEATURE(OmpNumTasksClause)
500+
READ_FEATURE(OmpNumTasksClause::Prescriptiveness)
497501
READ_FEATURE(OmpObject)
498502
READ_FEATURE(OmpObjectList)
499503
READ_FEATURE(OmpOrderClause)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ class ParseTreeDumper {
547547
NODE_ENUM(OmpOrderClause, Type)
548548
NODE(parser, OmpOrderModifier)
549549
NODE_ENUM(OmpOrderModifier, Kind)
550+
NODE(parser, OmpGrainsizeClause)
551+
NODE_ENUM(OmpGrainsizeClause, Prescriptiveness)
552+
NODE(parser, OmpNumTasksClause)
553+
NODE_ENUM(OmpNumTasksClause, Prescriptiveness)
550554
NODE(parser, OmpProcBindClause)
551555
NODE_ENUM(OmpProcBindClause, Type)
552556
NODE_ENUM(OmpReductionClause, ReductionModifier)

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3559,6 +3559,13 @@ struct OmpDependClause {
35593559
std::variant<Source, Sink, InOut> u;
35603560
};
35613561

3562+
// OMP 5.2 12.6.1 grainsize-clause -> grainsize ([prescriptiveness :] value)
3563+
struct OmpGrainsizeClause {
3564+
TUPLE_CLASS_BOILERPLATE(OmpGrainsizeClause);
3565+
ENUM_CLASS(Prescriptiveness, Strict);
3566+
std::tuple<std::optional<Prescriptiveness>, ScalarIntExpr> t;
3567+
};
3568+
35623569
// 2.12 if-clause -> IF ([ directive-name-modifier :] scalar-logical-expr)
35633570
struct OmpIfClause {
35643571
TUPLE_CLASS_BOILERPLATE(OmpIfClause);
@@ -3688,6 +3695,13 @@ struct OmpScheduleClause {
36883695
t;
36893696
};
36903697

3698+
// OMP 5.2 12.6.2 num_tasks-clause -> num_tasks ([prescriptiveness :] value)
3699+
struct OmpNumTasksClause {
3700+
TUPLE_CLASS_BOILERPLATE(OmpNumTasksClause);
3701+
ENUM_CLASS(Prescriptiveness, Strict);
3702+
std::tuple<std::optional<Prescriptiveness>, ScalarIntExpr> t;
3703+
};
3704+
36913705
// OpenMP Clauses
36923706
struct OmpClause {
36933707
UNION_CLASS_BOILERPLATE(OmpClause);

flang/lib/Lower/OpenMP/Clauses.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,20 @@ From make(const parser::OmpClause::From &inp,
721721
// Full: empty
722722

723723
Grainsize make(const parser::OmpClause::Grainsize &inp,
724-
semantics::SemanticsContext &semaCtx) {
725-
// inp.v -> parser::ScalarIntExpr
726-
return Grainsize{{/*Prescriptiveness=*/std::nullopt,
727-
/*GrainSize=*/makeExpr(inp.v, semaCtx)}};
724+
semantics::SemanticsContext &semaCtx) {
725+
// inp.v -> parser::OmpGrainsizeClause
726+
using wrapped = parser::OmpGrainsizeClause;
727+
728+
CLAUSET_ENUM_CONVERT( //
729+
convert, parser::OmpGrainsizeClause::Prescriptiveness, Grainsize::Prescriptiveness,
730+
// clang-format off
731+
MS(Strict, Strict)
732+
// clang-format on
733+
);
734+
auto &t0 = std::get<std::optional<wrapped::Prescriptiveness>>(inp.v.t);
735+
auto &t1 = std::get<parser::ScalarIntExpr>(inp.v.t);
736+
return Grainsize{{/*Prescriptiveness=*/maybeApply(convert, t0),
737+
/*Grainsize=*/makeExpr(t1, semaCtx)}};
728738
}
729739

730740
HasDeviceAddr make(const parser::OmpClause::HasDeviceAddr &inp,
@@ -971,9 +981,20 @@ Novariants make(const parser::OmpClause::Novariants &inp,
971981

972982
NumTasks make(const parser::OmpClause::NumTasks &inp,
973983
semantics::SemanticsContext &semaCtx) {
974-
// inp.v -> parser::ScalarIntExpr
975-
return NumTasks{{/*Prescriptiveness=*/std::nullopt,
976-
/*NumTasks=*/makeExpr(inp.v, semaCtx)}};
984+
// inp.v -> parser::OmpNumTasksClause
985+
using wrapped = parser::OmpNumTasksClause;
986+
987+
CLAUSET_ENUM_CONVERT( //
988+
convert, parser::OmpNumTasksClause::Prescriptiveness,
989+
NumTasks::Prescriptiveness,
990+
// clang-format off
991+
MS(Strict, Strict)
992+
// clang-format on
993+
);
994+
auto &t0 = std::get<std::optional<wrapped::Prescriptiveness>>(inp.v.t);
995+
auto &t1 = std::get<parser::ScalarIntExpr>(inp.v.t);
996+
return NumTasks{{/*Prescriptiveness=*/maybeApply(convert, t0),
997+
/*NumTasks=*/makeExpr(t1, semaCtx)}};
977998
}
978999

9791000
NumTeams make(const parser::OmpClause::NumTeams &inp,

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,16 @@ TYPE_PARSER(construct<OmpOrderClause>(
408408
maybe(Parser<OmpOrderModifier>{} / ":"),
409409
"CONCURRENT" >> pure(OmpOrderClause::Type::Concurrent)))
410410

411+
// OMP 5.2 12.6.1 grainsize([ prescriptiveness :] scalar-integer-expression)
412+
TYPE_PARSER(construct<OmpGrainsizeClause>(
413+
maybe("STRICT" >> pure(OmpGrainsizeClause::Prescriptiveness::Strict) / ":"),
414+
scalarIntExpr))
415+
416+
// OMP 5.2 12.6.2 num_tasks([ prescriptiveness :] scalar-integer-expression)
417+
TYPE_PARSER(construct<OmpNumTasksClause>(
418+
maybe("STRICT" >> pure(OmpNumTasksClause::Prescriptiveness::Strict) / ":"),
419+
scalarIntExpr))
420+
411421
TYPE_PARSER(
412422
construct<OmpObject>(designator) || construct<OmpObject>("/" >> name / "/"))
413423

@@ -464,7 +474,7 @@ TYPE_PARSER(
464474
"FROM" >> construct<OmpClause>(construct<OmpClause::From>(
465475
parenthesized(Parser<OmpObjectList>{}))) ||
466476
"GRAINSIZE" >> construct<OmpClause>(construct<OmpClause::Grainsize>(
467-
parenthesized(scalarIntExpr))) ||
477+
parenthesized(Parser<OmpGrainsizeClause>{}))) ||
468478
"HAS_DEVICE_ADDR" >>
469479
construct<OmpClause>(construct<OmpClause::HasDeviceAddr>(
470480
parenthesized(Parser<OmpObjectList>{}))) ||
@@ -491,7 +501,7 @@ TYPE_PARSER(
491501
construct<OmpClause>(construct<OmpClause::Notinbranch>()) ||
492502
"NOWAIT" >> construct<OmpClause>(construct<OmpClause::Nowait>()) ||
493503
"NUM_TASKS" >> construct<OmpClause>(construct<OmpClause::NumTasks>(
494-
parenthesized(scalarIntExpr))) ||
504+
parenthesized(Parser<OmpNumTasksClause>{}))) ||
495505
"NUM_TEAMS" >> construct<OmpClause>(construct<OmpClause::NumTeams>(
496506
parenthesized(scalarIntExpr))) ||
497507
"NUM_THREADS" >> construct<OmpClause>(construct<OmpClause::NumThreads>(

flang/lib/Parser/unparse.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,16 @@ class UnparseVisitor {
21962196
Walk(std::get<std::optional<OmpOrderModifier>>(x.t), ":");
21972197
Walk(std::get<OmpOrderClause::Type>(x.t));
21982198
}
2199+
void Unparse(const OmpGrainsizeClause &x) {
2200+
Walk(std::get<std::optional<OmpGrainsizeClause::Prescriptiveness>>(x.t),
2201+
":");
2202+
Walk(std::get<ScalarIntExpr>(x.t));
2203+
}
2204+
void Unparse(const OmpNumTasksClause &x) {
2205+
Walk(
2206+
std::get<std::optional<OmpNumTasksClause::Prescriptiveness>>(x.t), ":");
2207+
Walk(std::get<ScalarIntExpr>(x.t));
2208+
}
21992209
void Unparse(const OmpDependSinkVecLength &x) {
22002210
Walk(std::get<DefinedOperator>(x.t));
22012211
Walk(std::get<ScalarIntConstantExpr>(x.t));
@@ -2829,6 +2839,9 @@ class UnparseVisitor {
28292839
WALK_NESTED_ENUM(OmpCancelType, Type) // OMP cancel-type
28302840
WALK_NESTED_ENUM(OmpOrderClause, Type) // OMP order-type
28312841
WALK_NESTED_ENUM(OmpOrderModifier, Kind) // OMP order-modifier
2842+
WALK_NESTED_ENUM(
2843+
OmpGrainsizeClause, Prescriptiveness) // OMP grainsize-modifier
2844+
WALK_NESTED_ENUM(OmpNumTasksClause, Prescriptiveness) // OMP numtasks-modifier
28322845
WALK_NESTED_ENUM(OmpMapClause, Type) // OMP map-type
28332846
WALK_NESTED_ENUM(OmpMapClause, TypeModifier) // OMP map-type-modifier
28342847
#undef WALK_NESTED_ENUM

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,12 +2479,14 @@ CHECK_SIMPLE_CLAUSE(Final, OMPC_final)
24792479
CHECK_SIMPLE_CLAUSE(Flush, OMPC_flush)
24802480
CHECK_SIMPLE_CLAUSE(From, OMPC_from)
24812481
CHECK_SIMPLE_CLAUSE(Full, OMPC_full)
2482+
CHECK_SIMPLE_CLAUSE(Grainsize, OMPC_grainsize)
24822483
CHECK_SIMPLE_CLAUSE(Hint, OMPC_hint)
24832484
CHECK_SIMPLE_CLAUSE(Holds, OMPC_holds)
24842485
CHECK_SIMPLE_CLAUSE(InReduction, OMPC_in_reduction)
24852486
CHECK_SIMPLE_CLAUSE(Inclusive, OMPC_inclusive)
24862487
CHECK_SIMPLE_CLAUSE(Match, OMPC_match)
24872488
CHECK_SIMPLE_CLAUSE(Nontemporal, OMPC_nontemporal)
2489+
CHECK_SIMPLE_CLAUSE(NumTasks, OMPC_num_tasks)
24882490
CHECK_SIMPLE_CLAUSE(Order, OMPC_order)
24892491
CHECK_SIMPLE_CLAUSE(Read, OMPC_read)
24902492
CHECK_SIMPLE_CLAUSE(Threadprivate, OMPC_threadprivate)
@@ -2535,8 +2537,6 @@ CHECK_SIMPLE_CLAUSE(OmpxBare, OMPC_ompx_bare)
25352537
CHECK_SIMPLE_CLAUSE(Fail, OMPC_fail)
25362538
CHECK_SIMPLE_CLAUSE(Weak, OMPC_weak)
25372539

2538-
CHECK_REQ_SCALAR_INT_CLAUSE(Grainsize, OMPC_grainsize)
2539-
CHECK_REQ_SCALAR_INT_CLAUSE(NumTasks, OMPC_num_tasks)
25402540
CHECK_REQ_SCALAR_INT_CLAUSE(NumTeams, OMPC_num_teams)
25412541
CHECK_REQ_SCALAR_INT_CLAUSE(NumThreads, OMPC_num_threads)
25422542
CHECK_REQ_SCALAR_INT_CLAUSE(OmpxDynCgroupMem, OMPC_ompx_dyn_cgroup_mem)

flang/test/Parser/OpenMP/taskloop.f90

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 parallel_work
5+
integer :: i
6+
7+
!CHECK: !$OMP TASKLOOP GRAINSIZE(STRICT:500_4)
8+
!PARSE-TREE: OmpBeginLoopDirective
9+
!PARSE-TREE-NEXT: OmpLoopDirective -> llvm::omp::Directive = taskloop
10+
!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> Grainsize -> OmpGrainsizeClause
11+
!PARSE-TREE-NEXT: Prescriptiveness = Strict
12+
!PARSE-TREE-NEXT: Scalar -> Integer -> Expr = '500_4'
13+
!$omp taskloop grainsize(strict: 500)
14+
do i=1,10000
15+
call loop_body(i)
16+
end do
17+
!$omp end taskloop
18+
19+
!CHECK: !$OMP TASKLOOP GRAINSIZE(500_4)
20+
!PARSE-TREE: OmpBeginLoopDirective
21+
!PARSE-TREE-NEXT: OmpLoopDirective -> llvm::omp::Directive = taskloop
22+
!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> Grainsize -> OmpGrainsizeClause
23+
!PARSE-TREE-NEXT: Scalar -> Integer -> Expr = '500_4'
24+
!$omp taskloop grainsize(500)
25+
do i=1,10000
26+
call loop_body(i)
27+
end do
28+
!$omp end taskloop
29+
30+
!CHECK: !$OMP TASKLOOP NUM_TASKS(STRICT:500_4)
31+
!PARSE-TREE: OmpBeginLoopDirective
32+
!PARSE-TREE-NEXT: OmpLoopDirective -> llvm::omp::Directive = taskloop
33+
!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> NumTasks -> OmpNumTasksClause
34+
!PARSE-TREE-NEXT: Prescriptiveness = Strict
35+
!PARSE-TREE-NEXT: Scalar -> Integer -> Expr = '500_4'
36+
!$omp taskloop num_tasks(strict: 500)
37+
do i=1,10000
38+
call loop_body(i)
39+
end do
40+
!$omp end taskloop
41+
end subroutine parallel_work

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ def OMPC_Full: Clause<"full"> {
185185
let clangClass = "OMPFullClause";
186186
}
187187
def OMP_GRAINSIZE_Strict : ClauseVal<"strict", 1, 1> {}
188-
def OMP_GRAINSIZE_Unknown : ClauseVal<"unkonwn", 2, 0> { let isDefault = 1; }
188+
def OMP_GRAINSIZE_Unknown : ClauseVal<"unknown", 2, 0> { let isDefault = 1; }
189189
def OMPC_GrainSize : Clause<"grainsize"> {
190190
let clangClass = "OMPGrainsizeClause";
191-
let flangClass = "ScalarIntExpr";
191+
let flangClass = "OmpGrainsizeClause";
192192
let enumClauseValue = "GrainsizeType";
193193
let allowedClauseValues = [
194194
OMP_GRAINSIZE_Strict,
@@ -301,10 +301,10 @@ def OMPC_NoWait : Clause<"nowait"> {
301301
let clangClass = "OMPNowaitClause";
302302
}
303303
def OMP_NUMTASKS_Strict : ClauseVal<"strict", 1, 1> {}
304-
def OMP_NUMTASKS_Unknown : ClauseVal<"unkonwn", 2, 0> { let isDefault = 1; }
304+
def OMP_NUMTASKS_Unknown : ClauseVal<"unknown", 2, 0> { let isDefault = 1; }
305305
def OMPC_NumTasks : Clause<"num_tasks"> {
306306
let clangClass = "OMPNumTasksClause";
307-
let flangClass = "ScalarIntExpr";
307+
let flangClass = "OmpNumTasksClause";
308308
let enumClauseValue = "NumTasksType";
309309
let allowedClauseValues = [
310310
OMP_NUMTASKS_Strict,

0 commit comments

Comments
 (0)