Skip to content

[Flang][OpenMP] Add parser support for grainsize and num_tasks clause #113136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions flang/examples/FeatureList/FeatureList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ struct NodeVisitor {
READ_FEATURE(OmpEndCriticalDirective)
READ_FEATURE(OmpEndLoopDirective)
READ_FEATURE(OmpEndSectionsDirective)
READ_FEATURE(OmpGrainsizeClause)
READ_FEATURE(OmpGrainsizeClause::Prescriptiveness)
READ_FEATURE(OmpIfClause)
READ_FEATURE(OmpIfClause::DirectiveNameModifier)
READ_FEATURE(OmpLinearClause)
Expand All @@ -494,6 +496,8 @@ struct NodeVisitor {
READ_FEATURE(OmpMapClause)
READ_FEATURE(OmpMapClause::TypeModifier)
READ_FEATURE(OmpMapClause::Type)
READ_FEATURE(OmpNumTasksClause)
READ_FEATURE(OmpNumTasksClause::Prescriptiveness)
READ_FEATURE(OmpObject)
READ_FEATURE(OmpObjectList)
READ_FEATURE(OmpOrderClause)
Expand Down
4 changes: 4 additions & 0 deletions flang/include/flang/Parser/dump-parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,10 @@ class ParseTreeDumper {
NODE_ENUM(OmpOrderClause, Type)
NODE(parser, OmpOrderModifier)
NODE_ENUM(OmpOrderModifier, Kind)
NODE(parser, OmpGrainsizeClause)
NODE_ENUM(OmpGrainsizeClause, Prescriptiveness)
NODE(parser, OmpNumTasksClause)
NODE_ENUM(OmpNumTasksClause, Prescriptiveness)
NODE(parser, OmpProcBindClause)
NODE_ENUM(OmpProcBindClause, Type)
NODE_ENUM(OmpReductionClause, ReductionModifier)
Expand Down
14 changes: 14 additions & 0 deletions flang/include/flang/Parser/parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3559,6 +3559,13 @@ struct OmpDependClause {
std::variant<Source, Sink, InOut> u;
};

// OMP 5.2 12.6.1 grainsize-clause -> grainsize ([prescriptiveness :] value)
struct OmpGrainsizeClause {
TUPLE_CLASS_BOILERPLATE(OmpGrainsizeClause);
ENUM_CLASS(Prescriptiveness, Strict);
std::tuple<std::optional<Prescriptiveness>, ScalarIntExpr> t;
};

// 2.12 if-clause -> IF ([ directive-name-modifier :] scalar-logical-expr)
struct OmpIfClause {
TUPLE_CLASS_BOILERPLATE(OmpIfClause);
Expand Down Expand Up @@ -3688,6 +3695,13 @@ struct OmpScheduleClause {
t;
};

// OMP 5.2 12.6.2 num_tasks-clause -> num_tasks ([prescriptiveness :] value)
struct OmpNumTasksClause {
TUPLE_CLASS_BOILERPLATE(OmpNumTasksClause);
ENUM_CLASS(Prescriptiveness, Strict);
std::tuple<std::optional<Prescriptiveness>, ScalarIntExpr> t;
};

// OpenMP Clauses
struct OmpClause {
UNION_CLASS_BOILERPLATE(OmpClause);
Expand Down
35 changes: 28 additions & 7 deletions flang/lib/Lower/OpenMP/Clauses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,10 +721,20 @@ From make(const parser::OmpClause::From &inp,
// Full: empty

Grainsize make(const parser::OmpClause::Grainsize &inp,
semantics::SemanticsContext &semaCtx) {
// inp.v -> parser::ScalarIntExpr
return Grainsize{{/*Prescriptiveness=*/std::nullopt,
/*GrainSize=*/makeExpr(inp.v, semaCtx)}};
semantics::SemanticsContext &semaCtx) {
// inp.v -> parser::OmpGrainsizeClause
using wrapped = parser::OmpGrainsizeClause;

CLAUSET_ENUM_CONVERT( //
convert, parser::OmpGrainsizeClause::Prescriptiveness, Grainsize::Prescriptiveness,
// clang-format off
MS(Strict, Strict)
// clang-format on
);
auto &t0 = std::get<std::optional<wrapped::Prescriptiveness>>(inp.v.t);
auto &t1 = std::get<parser::ScalarIntExpr>(inp.v.t);
return Grainsize{{/*Prescriptiveness=*/maybeApply(convert, t0),
/*Grainsize=*/makeExpr(t1, semaCtx)}};
}

HasDeviceAddr make(const parser::OmpClause::HasDeviceAddr &inp,
Expand Down Expand Up @@ -971,9 +981,20 @@ Novariants make(const parser::OmpClause::Novariants &inp,

NumTasks make(const parser::OmpClause::NumTasks &inp,
semantics::SemanticsContext &semaCtx) {
// inp.v -> parser::ScalarIntExpr
return NumTasks{{/*Prescriptiveness=*/std::nullopt,
/*NumTasks=*/makeExpr(inp.v, semaCtx)}};
// inp.v -> parser::OmpNumTasksClause
using wrapped = parser::OmpNumTasksClause;

CLAUSET_ENUM_CONVERT( //
convert, parser::OmpNumTasksClause::Prescriptiveness,
NumTasks::Prescriptiveness,
// clang-format off
MS(Strict, Strict)
// clang-format on
);
auto &t0 = std::get<std::optional<wrapped::Prescriptiveness>>(inp.v.t);
auto &t1 = std::get<parser::ScalarIntExpr>(inp.v.t);
return NumTasks{{/*Prescriptiveness=*/maybeApply(convert, t0),
/*NumTasks=*/makeExpr(t1, semaCtx)}};
}

NumTeams make(const parser::OmpClause::NumTeams &inp,
Expand Down
14 changes: 12 additions & 2 deletions flang/lib/Parser/openmp-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,16 @@ TYPE_PARSER(construct<OmpOrderClause>(
maybe(Parser<OmpOrderModifier>{} / ":"),
"CONCURRENT" >> pure(OmpOrderClause::Type::Concurrent)))

// OMP 5.2 12.6.1 grainsize([ prescriptiveness :] scalar-integer-expression)
TYPE_PARSER(construct<OmpGrainsizeClause>(
maybe("STRICT" >> pure(OmpGrainsizeClause::Prescriptiveness::Strict) / ":"),
scalarIntExpr))

// OMP 5.2 12.6.2 num_tasks([ prescriptiveness :] scalar-integer-expression)
TYPE_PARSER(construct<OmpNumTasksClause>(
maybe("STRICT" >> pure(OmpNumTasksClause::Prescriptiveness::Strict) / ":"),
scalarIntExpr))

TYPE_PARSER(
construct<OmpObject>(designator) || construct<OmpObject>("/" >> name / "/"))

Expand Down Expand Up @@ -464,7 +474,7 @@ TYPE_PARSER(
"FROM" >> construct<OmpClause>(construct<OmpClause::From>(
parenthesized(Parser<OmpObjectList>{}))) ||
"GRAINSIZE" >> construct<OmpClause>(construct<OmpClause::Grainsize>(
parenthesized(scalarIntExpr))) ||
parenthesized(Parser<OmpGrainsizeClause>{}))) ||
"HAS_DEVICE_ADDR" >>
construct<OmpClause>(construct<OmpClause::HasDeviceAddr>(
parenthesized(Parser<OmpObjectList>{}))) ||
Expand All @@ -491,7 +501,7 @@ TYPE_PARSER(
construct<OmpClause>(construct<OmpClause::Notinbranch>()) ||
"NOWAIT" >> construct<OmpClause>(construct<OmpClause::Nowait>()) ||
"NUM_TASKS" >> construct<OmpClause>(construct<OmpClause::NumTasks>(
parenthesized(scalarIntExpr))) ||
parenthesized(Parser<OmpNumTasksClause>{}))) ||
"NUM_TEAMS" >> construct<OmpClause>(construct<OmpClause::NumTeams>(
parenthesized(scalarIntExpr))) ||
"NUM_THREADS" >> construct<OmpClause>(construct<OmpClause::NumThreads>(
Expand Down
13 changes: 13 additions & 0 deletions flang/lib/Parser/unparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2196,6 +2196,16 @@ class UnparseVisitor {
Walk(std::get<std::optional<OmpOrderModifier>>(x.t), ":");
Walk(std::get<OmpOrderClause::Type>(x.t));
}
void Unparse(const OmpGrainsizeClause &x) {
Walk(std::get<std::optional<OmpGrainsizeClause::Prescriptiveness>>(x.t),
":");
Walk(std::get<ScalarIntExpr>(x.t));
}
void Unparse(const OmpNumTasksClause &x) {
Walk(
std::get<std::optional<OmpNumTasksClause::Prescriptiveness>>(x.t), ":");
Walk(std::get<ScalarIntExpr>(x.t));
}
void Unparse(const OmpDependSinkVecLength &x) {
Walk(std::get<DefinedOperator>(x.t));
Walk(std::get<ScalarIntConstantExpr>(x.t));
Expand Down Expand Up @@ -2829,6 +2839,9 @@ class UnparseVisitor {
WALK_NESTED_ENUM(OmpCancelType, Type) // OMP cancel-type
WALK_NESTED_ENUM(OmpOrderClause, Type) // OMP order-type
WALK_NESTED_ENUM(OmpOrderModifier, Kind) // OMP order-modifier
WALK_NESTED_ENUM(
OmpGrainsizeClause, Prescriptiveness) // OMP grainsize-modifier
WALK_NESTED_ENUM(OmpNumTasksClause, Prescriptiveness) // OMP numtasks-modifier
WALK_NESTED_ENUM(OmpMapClause, Type) // OMP map-type
WALK_NESTED_ENUM(OmpMapClause, TypeModifier) // OMP map-type-modifier
#undef WALK_NESTED_ENUM
Expand Down
4 changes: 2 additions & 2 deletions flang/lib/Semantics/check-omp-structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2479,12 +2479,14 @@ CHECK_SIMPLE_CLAUSE(Final, OMPC_final)
CHECK_SIMPLE_CLAUSE(Flush, OMPC_flush)
CHECK_SIMPLE_CLAUSE(From, OMPC_from)
CHECK_SIMPLE_CLAUSE(Full, OMPC_full)
CHECK_SIMPLE_CLAUSE(Grainsize, OMPC_grainsize)
CHECK_SIMPLE_CLAUSE(Hint, OMPC_hint)
CHECK_SIMPLE_CLAUSE(Holds, OMPC_holds)
CHECK_SIMPLE_CLAUSE(InReduction, OMPC_in_reduction)
CHECK_SIMPLE_CLAUSE(Inclusive, OMPC_inclusive)
CHECK_SIMPLE_CLAUSE(Match, OMPC_match)
CHECK_SIMPLE_CLAUSE(Nontemporal, OMPC_nontemporal)
CHECK_SIMPLE_CLAUSE(NumTasks, OMPC_num_tasks)
CHECK_SIMPLE_CLAUSE(Order, OMPC_order)
CHECK_SIMPLE_CLAUSE(Read, OMPC_read)
CHECK_SIMPLE_CLAUSE(Threadprivate, OMPC_threadprivate)
Expand Down Expand Up @@ -2535,8 +2537,6 @@ CHECK_SIMPLE_CLAUSE(OmpxBare, OMPC_ompx_bare)
CHECK_SIMPLE_CLAUSE(Fail, OMPC_fail)
CHECK_SIMPLE_CLAUSE(Weak, OMPC_weak)

CHECK_REQ_SCALAR_INT_CLAUSE(Grainsize, OMPC_grainsize)
CHECK_REQ_SCALAR_INT_CLAUSE(NumTasks, OMPC_num_tasks)
CHECK_REQ_SCALAR_INT_CLAUSE(NumTeams, OMPC_num_teams)
CHECK_REQ_SCALAR_INT_CLAUSE(NumThreads, OMPC_num_threads)
CHECK_REQ_SCALAR_INT_CLAUSE(OmpxDynCgroupMem, OMPC_ompx_dyn_cgroup_mem)
Expand Down
41 changes: 41 additions & 0 deletions flang/test/Parser/OpenMP/taskloop.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
! RUN: %flang_fc1 -fdebug-unparse -fopenmp %s | FileCheck --ignore-case %s
! RUN: %flang_fc1 -fdebug-dump-parse-tree -fopenmp %s | FileCheck --check-prefix="PARSE-TREE" %s

subroutine parallel_work
integer :: i

!CHECK: !$OMP TASKLOOP GRAINSIZE(STRICT:500_4)
!PARSE-TREE: OmpBeginLoopDirective
!PARSE-TREE-NEXT: OmpLoopDirective -> llvm::omp::Directive = taskloop
!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> Grainsize -> OmpGrainsizeClause
!PARSE-TREE-NEXT: Prescriptiveness = Strict
!PARSE-TREE-NEXT: Scalar -> Integer -> Expr = '500_4'
!$omp taskloop grainsize(strict: 500)
do i=1,10000
call loop_body(i)
end do
!$omp end taskloop

!CHECK: !$OMP TASKLOOP GRAINSIZE(500_4)
!PARSE-TREE: OmpBeginLoopDirective
!PARSE-TREE-NEXT: OmpLoopDirective -> llvm::omp::Directive = taskloop
!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> Grainsize -> OmpGrainsizeClause
!PARSE-TREE-NEXT: Scalar -> Integer -> Expr = '500_4'
!$omp taskloop grainsize(500)
do i=1,10000
call loop_body(i)
end do
!$omp end taskloop

!CHECK: !$OMP TASKLOOP NUM_TASKS(STRICT:500_4)
!PARSE-TREE: OmpBeginLoopDirective
!PARSE-TREE-NEXT: OmpLoopDirective -> llvm::omp::Directive = taskloop
!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> NumTasks -> OmpNumTasksClause
!PARSE-TREE-NEXT: Prescriptiveness = Strict
!PARSE-TREE-NEXT: Scalar -> Integer -> Expr = '500_4'
!$omp taskloop num_tasks(strict: 500)
do i=1,10000
call loop_body(i)
end do
!$omp end taskloop
end subroutine parallel_work
8 changes: 4 additions & 4 deletions llvm/include/llvm/Frontend/OpenMP/OMP.td
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ def OMPC_Full: Clause<"full"> {
let clangClass = "OMPFullClause";
}
def OMP_GRAINSIZE_Strict : ClauseVal<"strict", 1, 1> {}
def OMP_GRAINSIZE_Unknown : ClauseVal<"unkonwn", 2, 0> { let isDefault = 1; }
def OMP_GRAINSIZE_Unknown : ClauseVal<"unknown", 2, 0> { let isDefault = 1; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

def OMPC_GrainSize : Clause<"grainsize"> {
let clangClass = "OMPGrainsizeClause";
let flangClass = "ScalarIntExpr";
let flangClass = "OmpGrainsizeClause";
let enumClauseValue = "GrainsizeType";
let allowedClauseValues = [
OMP_GRAINSIZE_Strict,
Expand Down Expand Up @@ -301,10 +301,10 @@ def OMPC_NoWait : Clause<"nowait"> {
let clangClass = "OMPNowaitClause";
}
def OMP_NUMTASKS_Strict : ClauseVal<"strict", 1, 1> {}
def OMP_NUMTASKS_Unknown : ClauseVal<"unkonwn", 2, 0> { let isDefault = 1; }
def OMP_NUMTASKS_Unknown : ClauseVal<"unknown", 2, 0> { let isDefault = 1; }
def OMPC_NumTasks : Clause<"num_tasks"> {
let clangClass = "OMPNumTasksClause";
let flangClass = "ScalarIntExpr";
let flangClass = "OmpNumTasksClause";
let enumClauseValue = "NumTasksType";
let allowedClauseValues = [
OMP_NUMTASKS_Strict,
Expand Down
Loading