Skip to content

Commit 374db67

Browse files
authored
[flang][openmp] Add parser and semantic support for workdistribute (llvm#154377)
This PR adds workdistribute parser and semantic support in flang. The work in this PR is c-p and updated from @ivanradanov commits from coexecute implementation: flang_workdistribute_iwomp_2024
1 parent 9642aad commit 374db67

File tree

10 files changed

+242
-2
lines changed

10 files changed

+242
-2
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ static const OmpDirectiveSet topTargetSet{
143143
Directive::OMPD_target_teams_distribute_parallel_do_simd,
144144
Directive::OMPD_target_teams_distribute_simd,
145145
Directive::OMPD_target_teams_loop,
146+
Directive::OMPD_target_teams_workdistribute,
146147
};
147148

148149
static const OmpDirectiveSet allTargetSet{topTargetSet};
@@ -172,6 +173,7 @@ static const OmpDirectiveSet topTeamsSet{
172173
Directive::OMPD_teams_distribute_parallel_do_simd,
173174
Directive::OMPD_teams_distribute_simd,
174175
Directive::OMPD_teams_loop,
176+
Directive::OMPD_teams_workdistribute,
175177
};
176178

177179
static const OmpDirectiveSet bottomTeamsSet{
@@ -187,6 +189,7 @@ static const OmpDirectiveSet allTeamsSet{
187189
Directive::OMPD_target_teams_distribute_parallel_do_simd,
188190
Directive::OMPD_target_teams_distribute_simd,
189191
Directive::OMPD_target_teams_loop,
192+
Directive::OMPD_target_teams_workdistribute,
190193
} | topTeamsSet,
191194
};
192195

@@ -230,6 +233,9 @@ static const OmpDirectiveSet blockConstructSet{
230233
Directive::OMPD_taskgroup,
231234
Directive::OMPD_teams,
232235
Directive::OMPD_workshare,
236+
Directive::OMPD_target_teams_workdistribute,
237+
Directive::OMPD_teams_workdistribute,
238+
Directive::OMPD_workdistribute,
233239
};
234240

235241
static const OmpDirectiveSet loopConstructSet{
@@ -376,6 +382,7 @@ static const OmpDirectiveSet nestedReduceWorkshareAllowedSet{
376382
};
377383

378384
static const OmpDirectiveSet nestedTeamsAllowedSet{
385+
Directive::OMPD_workdistribute,
379386
Directive::OMPD_distribute,
380387
Directive::OMPD_distribute_parallel_do,
381388
Directive::OMPD_distribute_parallel_do_simd,

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1870,11 +1870,15 @@ TYPE_PARSER( //
18701870
MakeBlockConstruct(llvm::omp::Directive::OMPD_target_data) ||
18711871
MakeBlockConstruct(llvm::omp::Directive::OMPD_target_parallel) ||
18721872
MakeBlockConstruct(llvm::omp::Directive::OMPD_target_teams) ||
1873+
MakeBlockConstruct(
1874+
llvm::omp::Directive::OMPD_target_teams_workdistribute) ||
18731875
MakeBlockConstruct(llvm::omp::Directive::OMPD_target) ||
18741876
MakeBlockConstruct(llvm::omp::Directive::OMPD_task) ||
18751877
MakeBlockConstruct(llvm::omp::Directive::OMPD_taskgroup) ||
18761878
MakeBlockConstruct(llvm::omp::Directive::OMPD_teams) ||
1877-
MakeBlockConstruct(llvm::omp::Directive::OMPD_workshare))
1879+
MakeBlockConstruct(llvm::omp::Directive::OMPD_teams_workdistribute) ||
1880+
MakeBlockConstruct(llvm::omp::Directive::OMPD_workshare) ||
1881+
MakeBlockConstruct(llvm::omp::Directive::OMPD_workdistribute))
18781882
#undef MakeBlockConstruct
18791883

18801884
// OMP SECTIONS Directive

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,64 @@ class OmpWorkshareBlockChecker {
143143
parser::CharBlock source_;
144144
};
145145

146+
// 'OmpWorkdistributeBlockChecker' is used to check the validity of the
147+
// assignment statements and the expressions enclosed in an OpenMP
148+
// WORKDISTRIBUTE construct
149+
class OmpWorkdistributeBlockChecker {
150+
public:
151+
OmpWorkdistributeBlockChecker(
152+
SemanticsContext &context, parser::CharBlock source)
153+
: context_{context}, source_{source} {}
154+
155+
template <typename T> bool Pre(const T &) { return true; }
156+
template <typename T> void Post(const T &) {}
157+
158+
bool Pre(const parser::AssignmentStmt &assignment) {
159+
const auto &var{std::get<parser::Variable>(assignment.t)};
160+
const auto &expr{std::get<parser::Expr>(assignment.t)};
161+
const auto *lhs{GetExpr(context_, var)};
162+
const auto *rhs{GetExpr(context_, expr)};
163+
if (lhs && rhs) {
164+
Tristate isDefined{semantics::IsDefinedAssignment(
165+
lhs->GetType(), lhs->Rank(), rhs->GetType(), rhs->Rank())};
166+
if (isDefined == Tristate::Yes) {
167+
context_.Say(expr.source,
168+
"Defined assignment statement is not allowed in a WORKDISTRIBUTE construct"_err_en_US);
169+
}
170+
}
171+
return true;
172+
}
173+
174+
bool Pre(const parser::Expr &expr) {
175+
if (const auto *e{GetExpr(context_, expr)}) {
176+
if (!e)
177+
return false;
178+
for (const Symbol &symbol : evaluate::CollectSymbols(*e)) {
179+
const Symbol &root{GetAssociationRoot(symbol)};
180+
if (IsFunction(root)) {
181+
std::vector<std::string> attrs;
182+
if (!IsElementalProcedure(root)) {
183+
attrs.push_back("non-ELEMENTAL");
184+
}
185+
if (root.attrs().test(Attr::IMPURE)) {
186+
attrs.push_back("IMPURE");
187+
}
188+
std::string attrsStr =
189+
attrs.empty() ? "" : " " + llvm::join(attrs, ", ");
190+
context_.Say(expr.source,
191+
"User defined%s function '%s' is not allowed in a WORKDISTRIBUTE construct"_err_en_US,
192+
attrsStr, root.name());
193+
}
194+
}
195+
}
196+
return false;
197+
}
198+
199+
private:
200+
SemanticsContext &context_;
201+
parser::CharBlock source_;
202+
};
203+
146204
// `OmpUnitedTaskDesignatorChecker` is used to check if the designator
147205
// can appear within the TASK construct
148206
class OmpUnitedTaskDesignatorChecker {
@@ -815,6 +873,12 @@ void OmpStructureChecker::Enter(const parser::OpenMPBlockConstruct &x) {
815873
"TARGET construct with nested TEAMS region contains statements or "
816874
"directives outside of the TEAMS construct"_err_en_US);
817875
}
876+
if (GetContext().directive == llvm::omp::Directive::OMPD_workdistribute &&
877+
GetContextParent().directive != llvm::omp::Directive::OMPD_teams) {
878+
context_.Say(x.BeginDir().DirName().source,
879+
"%s region can only be strictly nested within TEAMS region"_err_en_US,
880+
ContextDirectiveAsFortran());
881+
}
818882
}
819883

820884
CheckNoBranching(block, beginSpec.DirId(), beginSpec.source);
@@ -898,6 +962,17 @@ void OmpStructureChecker::Enter(const parser::OpenMPBlockConstruct &x) {
898962
HasInvalidWorksharingNesting(
899963
beginSpec.source, llvm::omp::nestedWorkshareErrSet);
900964
break;
965+
case llvm::omp::OMPD_workdistribute:
966+
if (!CurrentDirectiveIsNested()) {
967+
context_.Say(beginSpec.source,
968+
"A WORKDISTRIBUTE region must be nested inside TEAMS region only."_err_en_US);
969+
}
970+
CheckWorkdistributeBlockStmts(block, beginSpec.source);
971+
break;
972+
case llvm::omp::OMPD_teams_workdistribute:
973+
case llvm::omp::OMPD_target_teams_workdistribute:
974+
CheckWorkdistributeBlockStmts(block, beginSpec.source);
975+
break;
901976
case llvm::omp::Directive::OMPD_scope:
902977
case llvm::omp::Directive::OMPD_single:
903978
// TODO: This check needs to be extended while implementing nesting of
@@ -4546,6 +4621,27 @@ void OmpStructureChecker::CheckWorkshareBlockStmts(
45464621
}
45474622
}
45484623

4624+
void OmpStructureChecker::CheckWorkdistributeBlockStmts(
4625+
const parser::Block &block, parser::CharBlock source) {
4626+
unsigned version{context_.langOptions().OpenMPVersion};
4627+
unsigned since{60};
4628+
if (version < since)
4629+
context_.Say(source,
4630+
"WORKDISTRIBUTE construct is not allowed in %s, %s"_err_en_US,
4631+
ThisVersion(version), TryVersion(since));
4632+
4633+
OmpWorkdistributeBlockChecker ompWorkdistributeBlockChecker{context_, source};
4634+
4635+
for (auto it{block.begin()}; it != block.end(); ++it) {
4636+
if (parser::Unwrap<parser::AssignmentStmt>(*it)) {
4637+
parser::Walk(*it, ompWorkdistributeBlockChecker);
4638+
} else {
4639+
context_.Say(source,
4640+
"The structured block in a WORKDISTRIBUTE construct may consist of only SCALAR or ARRAY assignments"_err_en_US);
4641+
}
4642+
}
4643+
}
4644+
45494645
void OmpStructureChecker::CheckIfContiguous(const parser::OmpObject &object) {
45504646
if (auto contig{IsContiguous(context_, object)}; contig && !*contig) {
45514647
const parser::Name *name{GetObjectName(object)};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ class OmpStructureChecker
245245
llvmOmpClause clause, const parser::OmpObjectList &ompObjectList);
246246
bool CheckTargetBlockOnlyTeams(const parser::Block &);
247247
void CheckWorkshareBlockStmts(const parser::Block &, parser::CharBlock);
248+
void CheckWorkdistributeBlockStmts(const parser::Block &, parser::CharBlock);
248249

249250
void CheckIteratorRange(const parser::OmpIteratorSpecifier &x);
250251
void CheckIteratorModifier(const parser::OmpIterator &x);

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1740,10 +1740,13 @@ bool OmpAttributeVisitor::Pre(const parser::OpenMPBlockConstruct &x) {
17401740
case llvm::omp::Directive::OMPD_task:
17411741
case llvm::omp::Directive::OMPD_taskgroup:
17421742
case llvm::omp::Directive::OMPD_teams:
1743+
case llvm::omp::Directive::OMPD_workdistribute:
17431744
case llvm::omp::Directive::OMPD_workshare:
17441745
case llvm::omp::Directive::OMPD_parallel_workshare:
17451746
case llvm::omp::Directive::OMPD_target_teams:
1747+
case llvm::omp::Directive::OMPD_target_teams_workdistribute:
17461748
case llvm::omp::Directive::OMPD_target_parallel:
1749+
case llvm::omp::Directive::OMPD_teams_workdistribute:
17471750
PushContext(dirSpec.source, dirId);
17481751
break;
17491752
default:
@@ -1773,9 +1776,12 @@ void OmpAttributeVisitor::Post(const parser::OpenMPBlockConstruct &x) {
17731776
case llvm::omp::Directive::OMPD_target:
17741777
case llvm::omp::Directive::OMPD_task:
17751778
case llvm::omp::Directive::OMPD_teams:
1779+
case llvm::omp::Directive::OMPD_workdistribute:
17761780
case llvm::omp::Directive::OMPD_parallel_workshare:
17771781
case llvm::omp::Directive::OMPD_target_teams:
1778-
case llvm::omp::Directive::OMPD_target_parallel: {
1782+
case llvm::omp::Directive::OMPD_target_parallel:
1783+
case llvm::omp::Directive::OMPD_target_teams_workdistribute:
1784+
case llvm::omp::Directive::OMPD_teams_workdistribute: {
17791785
bool hasPrivate;
17801786
for (const auto *allocName : allocateNames_) {
17811787
hasPrivate = false;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
!RUN: %flang_fc1 -fdebug-unparse -fopenmp -fopenmp-version=60 %s | FileCheck --ignore-case --check-prefix="UNPARSE" %s
2+
!RUN: %flang_fc1 -fdebug-dump-parse-tree -fopenmp -fopenmp-version=60 %s | FileCheck --check-prefix="PARSE-TREE" %s
3+
4+
!UNPARSE: SUBROUTINE teams_workdistribute
5+
!UNPARSE: USE :: iso_fortran_env
6+
!UNPARSE: REAL(KIND=4_4) a
7+
!UNPARSE: REAL(KIND=4_4), DIMENSION(10_4) :: x
8+
!UNPARSE: REAL(KIND=4_4), DIMENSION(10_4) :: y
9+
!UNPARSE: !$OMP TEAMS WORKDISTRIBUTE
10+
!UNPARSE: y=a*x+y
11+
!UNPARSE: !$OMP END TEAMS WORKDISTRIBUTE
12+
!UNPARSE: END SUBROUTINE teams_workdistribute
13+
14+
!PARSE-TREE: | | | OmpBeginDirective
15+
!PARSE-TREE: | | | | OmpDirectiveName -> llvm::omp::Directive = teams workdistribute
16+
!PARSE-TREE: | | | OmpEndDirective
17+
!PARSE-TREE: | | | | OmpDirectiveName -> llvm::omp::Directive = teams workdistribute
18+
19+
subroutine teams_workdistribute()
20+
use iso_fortran_env
21+
real(kind=real32) :: a
22+
real(kind=real32), dimension(10) :: x
23+
real(kind=real32), dimension(10) :: y
24+
!$omp teams workdistribute
25+
y = a * x + y
26+
!$omp end teams workdistribute
27+
end subroutine teams_workdistribute
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
! RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=60
2+
! OpenMP Version 6.0
3+
! workdistribute Construct
4+
! Invalid do construct inside !$omp workdistribute
5+
6+
subroutine workdistribute()
7+
integer n, i
8+
!ERROR: A WORKDISTRIBUTE region must be nested inside TEAMS region only.
9+
!ERROR: The structured block in a WORKDISTRIBUTE construct may consist of only SCALAR or ARRAY assignments
10+
!$omp workdistribute
11+
do i = 1, n
12+
print *, "omp workdistribute"
13+
end do
14+
!$omp end workdistribute
15+
16+
end subroutine workdistribute
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
! RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=60
2+
! OpenMP Version 6.0
3+
! workdistribute Construct
4+
! The !omp workdistribute construct must not contain any user defined
5+
! function calls unless the function is ELEMENTAL.
6+
7+
module my_mod
8+
contains
9+
integer function my_func()
10+
my_func = 10
11+
end function my_func
12+
13+
impure integer function impure_my_func()
14+
impure_my_func = 20
15+
end function impure_my_func
16+
17+
impure elemental integer function impure_ele_my_func()
18+
impure_ele_my_func = 20
19+
end function impure_ele_my_func
20+
end module my_mod
21+
22+
subroutine workdistribute(aa, bb, cc, n)
23+
use my_mod
24+
integer n
25+
real aa(n), bb(n), cc(n)
26+
!$omp teams
27+
!$omp workdistribute
28+
!ERROR: User defined non-ELEMENTAL function 'my_func' is not allowed in a WORKDISTRIBUTE construct
29+
aa = my_func()
30+
aa = bb * cc
31+
!$omp end workdistribute
32+
!$omp end teams
33+
34+
end subroutine workdistribute
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
! RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=60
2+
! OpenMP Version 6.0
3+
! workdistribute Construct
4+
! All array assignments, scalar assignments, and masked array assignments
5+
! must be intrinsic assignments.
6+
7+
module defined_assign
8+
interface assignment(=)
9+
module procedure work_assign
10+
end interface
11+
12+
contains
13+
subroutine work_assign(a,b)
14+
integer, intent(out) :: a
15+
logical, intent(in) :: b(:)
16+
end subroutine work_assign
17+
end module defined_assign
18+
19+
program omp_workdistribute
20+
use defined_assign
21+
22+
integer :: a, aa(10), bb(10)
23+
logical :: l(10)
24+
l = .TRUE.
25+
26+
!$omp teams
27+
!$omp workdistribute
28+
!ERROR: Defined assignment statement is not allowed in a WORKDISTRIBUTE construct
29+
a = l
30+
aa = bb
31+
!$omp end workdistribute
32+
!$omp end teams
33+
34+
end program omp_workdistribute
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
! RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=50
2+
! OpenMP Version 6.0
3+
! workdistribute Construct
4+
! Unsuported OpenMP version
5+
6+
subroutine teams_workdistribute()
7+
use iso_fortran_env
8+
real(kind=real32) :: a
9+
real(kind=real32), dimension(10) :: x
10+
real(kind=real32), dimension(10) :: y
11+
!ERROR: WORKDISTRIBUTE construct is not allowed in OpenMP v5.0, try -fopenmp-version=60
12+
!$omp teams workdistribute
13+
y = a * x + y
14+
!$omp end teams workdistribute
15+
end subroutine teams_workdistribute

0 commit comments

Comments
 (0)