Skip to content

Commit 2ac2c2b

Browse files
[Flang][OpenMP] Add parser support for grainsize and num_tasks clause
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 e13f1d1 commit 2ac2c2b

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
@@ -486,6 +486,8 @@ struct NodeVisitor {
486486
READ_FEATURE(OmpIfClause)
487487
READ_FEATURE(OmpIfClause::DirectiveNameModifier)
488488
READ_FEATURE(OmpLinearClause)
489+
READ_FEATURE(OmpGrainsizeClause)
490+
READ_FEATURE(OmpGrainsizeClause::Prescriptiveness)
489491
READ_FEATURE(OmpLinearClause::WithModifier)
490492
READ_FEATURE(OmpLinearClause::WithoutModifier)
491493
READ_FEATURE(OmpLinearModifier)
@@ -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
@@ -544,6 +544,10 @@ class ParseTreeDumper {
544544
NODE_ENUM(OmpOrderClause, Type)
545545
NODE(parser, OmpOrderModifier)
546546
NODE_ENUM(OmpOrderModifier, Kind)
547+
NODE(parser, OmpGrainsizeClause)
548+
NODE_ENUM(OmpGrainsizeClause, Prescriptiveness)
549+
NODE(parser, OmpNumTasksClause)
550+
NODE_ENUM(OmpNumTasksClause, Prescriptiveness)
547551
NODE(parser, OmpProcBindClause)
548552
NODE_ENUM(OmpProcBindClause, Type)
549553
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
@@ -3536,6 +3536,20 @@ struct OmpOrderClause {
35363536
std::tuple<std::optional<OmpOrderModifier>, Type> t;
35373537
};
35383538

3539+
// OMP 5.2 12.6.1 grainsize-clause -> grainsize ([prescriptiveness :] value)
3540+
struct OmpGrainsizeClause {
3541+
TUPLE_CLASS_BOILERPLATE(OmpGrainsizeClause);
3542+
ENUM_CLASS(Prescriptiveness, Strict);
3543+
std::tuple<std::optional<Prescriptiveness>, ScalarIntExpr> t;
3544+
};
3545+
3546+
// OMP 5.2 12.6.2 num_tasks-clause -> num_tasks ([prescriptiveness :] value)
3547+
struct OmpNumTasksClause {
3548+
TUPLE_CLASS_BOILERPLATE(OmpNumTasksClause);
3549+
ENUM_CLASS(Prescriptiveness, Strict);
3550+
std::tuple<std::optional<Prescriptiveness>, ScalarIntExpr> t;
3551+
};
3552+
35393553
// 2.15.3.7 linear-modifier -> REF | VAL | UVAL
35403554
struct OmpLinearModifier {
35413555
ENUM_CLASS(Type, Ref, Val, Uval)

flang/lib/Lower/OpenMP/Clauses.cpp

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

676676
Grainsize make(const parser::OmpClause::Grainsize &inp,
677-
semantics::SemanticsContext &semaCtx) {
678-
// inp.v -> parser::ScalarIntExpr
679-
return Grainsize{{/*Prescriptiveness=*/std::nullopt,
680-
/*GrainSize=*/makeExpr(inp.v, semaCtx)}};
677+
semantics::SemanticsContext &semaCtx) {
678+
// inp.v -> parser::OmpGrainsizeClause
679+
using wrapped = parser::OmpGrainsizeClause;
680+
681+
CLAUSET_ENUM_CONVERT( //
682+
convert, parser::OmpGrainsizeClause::Prescriptiveness, Grainsize::Prescriptiveness,
683+
// clang-format off
684+
MS(Strict, Strict)
685+
// clang-format on
686+
);
687+
auto &t0 = std::get<std::optional<wrapped::Prescriptiveness>>(inp.v.t);
688+
auto &t1 = std::get<parser::ScalarIntExpr>(inp.v.t);
689+
return Grainsize{{/*Prescriptiveness=*/maybeApply(convert, t0),
690+
/*Grainsize=*/makeExpr(t1, semaCtx)}};
681691
}
682692

683693
HasDeviceAddr make(const parser::OmpClause::HasDeviceAddr &inp,
@@ -910,9 +920,20 @@ Novariants make(const parser::OmpClause::Novariants &inp,
910920

911921
NumTasks make(const parser::OmpClause::NumTasks &inp,
912922
semantics::SemanticsContext &semaCtx) {
913-
// inp.v -> parser::ScalarIntExpr
914-
return NumTasks{{/*Prescriptiveness=*/std::nullopt,
915-
/*NumTasks=*/makeExpr(inp.v, semaCtx)}};
923+
// inp.v -> parser::OmpNumTasksClause
924+
using wrapped = parser::OmpNumTasksClause;
925+
926+
CLAUSET_ENUM_CONVERT( //
927+
convert, parser::OmpNumTasksClause::Prescriptiveness,
928+
NumTasks::Prescriptiveness,
929+
// clang-format off
930+
MS(Strict, Strict)
931+
// clang-format on
932+
);
933+
auto &t0 = std::get<std::optional<wrapped::Prescriptiveness>>(inp.v.t);
934+
auto &t1 = std::get<parser::ScalarIntExpr>(inp.v.t);
935+
return NumTasks{{/*Prescriptiveness=*/maybeApply(convert, t0),
936+
/*NumTasks=*/makeExpr(t1, semaCtx)}};
916937
}
917938

918939
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
@@ -312,6 +312,16 @@ TYPE_PARSER(construct<OmpOrderClause>(
312312
maybe(Parser<OmpOrderModifier>{} / ":"),
313313
"CONCURRENT" >> pure(OmpOrderClause::Type::Concurrent)))
314314

315+
// OMP 5.2 12.6.1 grainsize([ prescriptiveness :] scalar-integer-expression)
316+
TYPE_PARSER(construct<OmpGrainsizeClause>(
317+
maybe("STRICT" >> pure(OmpGrainsizeClause::Prescriptiveness::Strict) / ":"),
318+
scalarIntExpr))
319+
320+
// OMP 5.2 12.6.2 num_tasks([ prescriptiveness :] scalar-integer-expression)
321+
TYPE_PARSER(construct<OmpNumTasksClause>(
322+
maybe("STRICT" >> pure(OmpNumTasksClause::Prescriptiveness::Strict) / ":"),
323+
scalarIntExpr))
324+
315325
TYPE_PARSER(
316326
construct<OmpObject>(designator) || construct<OmpObject>("/" >> name / "/"))
317327

@@ -366,7 +376,7 @@ TYPE_PARSER(
366376
"FROM" >> construct<OmpClause>(construct<OmpClause::From>(
367377
parenthesized(Parser<OmpObjectList>{}))) ||
368378
"GRAINSIZE" >> construct<OmpClause>(construct<OmpClause::Grainsize>(
369-
parenthesized(scalarIntExpr))) ||
379+
parenthesized(Parser<OmpGrainsizeClause>{}))) ||
370380
"HAS_DEVICE_ADDR" >>
371381
construct<OmpClause>(construct<OmpClause::HasDeviceAddr>(
372382
parenthesized(Parser<OmpObjectList>{}))) ||
@@ -393,7 +403,7 @@ TYPE_PARSER(
393403
construct<OmpClause>(construct<OmpClause::Notinbranch>()) ||
394404
"NOWAIT" >> construct<OmpClause>(construct<OmpClause::Nowait>()) ||
395405
"NUM_TASKS" >> construct<OmpClause>(construct<OmpClause::NumTasks>(
396-
parenthesized(scalarIntExpr))) ||
406+
parenthesized(Parser<OmpNumTasksClause>{}))) ||
397407
"NUM_TEAMS" >> construct<OmpClause>(construct<OmpClause::NumTeams>(
398408
parenthesized(scalarIntExpr))) ||
399409
"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
@@ -2170,6 +2170,16 @@ class UnparseVisitor {
21702170
Walk(std::get<std::optional<OmpOrderModifier>>(x.t), ":");
21712171
Walk(std::get<OmpOrderClause::Type>(x.t));
21722172
}
2173+
void Unparse(const OmpGrainsizeClause &x) {
2174+
Walk(std::get<std::optional<OmpGrainsizeClause::Prescriptiveness>>(x.t),
2175+
":");
2176+
Walk(std::get<ScalarIntExpr>(x.t));
2177+
}
2178+
void Unparse(const OmpNumTasksClause &x) {
2179+
Walk(
2180+
std::get<std::optional<OmpNumTasksClause::Prescriptiveness>>(x.t), ":");
2181+
Walk(std::get<ScalarIntExpr>(x.t));
2182+
}
21732183
void Unparse(const OmpDependSinkVecLength &x) {
21742184
Walk(std::get<DefinedOperator>(x.t));
21752185
Walk(std::get<ScalarIntConstantExpr>(x.t));
@@ -2800,6 +2810,9 @@ class UnparseVisitor {
28002810
WALK_NESTED_ENUM(OmpCancelType, Type) // OMP cancel-type
28012811
WALK_NESTED_ENUM(OmpOrderClause, Type) // OMP order-type
28022812
WALK_NESTED_ENUM(OmpOrderModifier, Kind) // OMP order-modifier
2813+
WALK_NESTED_ENUM(
2814+
OmpGrainsizeClause, Prescriptiveness) // OMP grainsize-modifier
2815+
WALK_NESTED_ENUM(OmpNumTasksClause, Prescriptiveness) // OMP numtasks-modifier
28032816
WALK_NESTED_ENUM(OmpMapClause, Type) // OMP map-type
28042817
#undef WALK_NESTED_ENUM
28052818
void Unparse(const ReductionOperator::Operator x) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,12 +2404,14 @@ CHECK_SIMPLE_CLAUSE(Final, OMPC_final)
24042404
CHECK_SIMPLE_CLAUSE(Flush, OMPC_flush)
24052405
CHECK_SIMPLE_CLAUSE(From, OMPC_from)
24062406
CHECK_SIMPLE_CLAUSE(Full, OMPC_full)
2407+
CHECK_SIMPLE_CLAUSE(Grainsize, OMPC_grainsize)
24072408
CHECK_SIMPLE_CLAUSE(Hint, OMPC_hint)
24082409
CHECK_SIMPLE_CLAUSE(Holds, OMPC_holds)
24092410
CHECK_SIMPLE_CLAUSE(InReduction, OMPC_in_reduction)
24102411
CHECK_SIMPLE_CLAUSE(Inclusive, OMPC_inclusive)
24112412
CHECK_SIMPLE_CLAUSE(Match, OMPC_match)
24122413
CHECK_SIMPLE_CLAUSE(Nontemporal, OMPC_nontemporal)
2414+
CHECK_SIMPLE_CLAUSE(NumTasks, OMPC_num_tasks)
24132415
CHECK_SIMPLE_CLAUSE(Order, OMPC_order)
24142416
CHECK_SIMPLE_CLAUSE(Read, OMPC_read)
24152417
CHECK_SIMPLE_CLAUSE(Threadprivate, OMPC_threadprivate)
@@ -2460,8 +2462,6 @@ CHECK_SIMPLE_CLAUSE(OmpxBare, OMPC_ompx_bare)
24602462
CHECK_SIMPLE_CLAUSE(Fail, OMPC_fail)
24612463
CHECK_SIMPLE_CLAUSE(Weak, OMPC_weak)
24622464

2463-
CHECK_REQ_SCALAR_INT_CLAUSE(Grainsize, OMPC_grainsize)
2464-
CHECK_REQ_SCALAR_INT_CLAUSE(NumTasks, OMPC_num_tasks)
24652465
CHECK_REQ_SCALAR_INT_CLAUSE(NumTeams, OMPC_num_teams)
24662466
CHECK_REQ_SCALAR_INT_CLAUSE(NumThreads, OMPC_num_threads)
24672467
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
@@ -184,10 +184,10 @@ def OMPC_Full: Clause<"full"> {
184184
let clangClass = "OMPFullClause";
185185
}
186186
def OMP_GRAINSIZE_Strict : ClauseVal<"strict", 1, 1> {}
187-
def OMP_GRAINSIZE_Unknown : ClauseVal<"unkonwn", 2, 0> { let isDefault = 1; }
187+
def OMP_GRAINSIZE_Unknown : ClauseVal<"unknown", 2, 0> { let isDefault = 1; }
188188
def OMPC_GrainSize : Clause<"grainsize"> {
189189
let clangClass = "OMPGrainsizeClause";
190-
let flangClass = "ScalarIntExpr";
190+
let flangClass = "OmpGrainsizeClause";
191191
let enumClauseValue = "GrainsizeType";
192192
let allowedClauseValues = [
193193
OMP_GRAINSIZE_Strict,
@@ -300,10 +300,10 @@ def OMPC_NoWait : Clause<"nowait"> {
300300
let clangClass = "OMPNowaitClause";
301301
}
302302
def OMP_NUMTASKS_Strict : ClauseVal<"strict", 1, 1> {}
303-
def OMP_NUMTASKS_Unknown : ClauseVal<"unkonwn", 2, 0> { let isDefault = 1; }
303+
def OMP_NUMTASKS_Unknown : ClauseVal<"unknown", 2, 0> { let isDefault = 1; }
304304
def OMPC_NumTasks : Clause<"num_tasks"> {
305305
let clangClass = "OMPNumTasksClause";
306-
let flangClass = "ScalarIntExpr";
306+
let flangClass = "OmpNumTasksClause";
307307
let enumClauseValue = "NumTasksType";
308308
let allowedClauseValues = [
309309
OMP_NUMTASKS_Strict,

0 commit comments

Comments
 (0)