Skip to content

Commit d7da0b7

Browse files
committed
[flang][openmp] Add parser/semantic support for workdistribute
1 parent 7681855 commit d7da0b7

File tree

9 files changed

+226
-2
lines changed

9 files changed

+226
-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: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,67 @@ 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 "
169+
"allowed in a WORKDISTRIBUTE construct"_err_en_US);
170+
}
171+
}
172+
return true;
173+
}
174+
175+
bool Pre(const parser::Expr &expr) {
176+
if (const auto *e{GetExpr(context_, expr)}) {
177+
for (const Symbol &symbol : evaluate::CollectSymbols(*e)) {
178+
const Symbol &root{GetAssociationRoot(symbol)};
179+
if (IsFunction(root)) {
180+
std::string attrs{""};
181+
if (!IsElementalProcedure(root)) {
182+
attrs = " non-ELEMENTAL";
183+
}
184+
if (root.attrs().test(Attr::IMPURE)) {
185+
if (attrs != "") {
186+
attrs = "," + attrs;
187+
}
188+
attrs = " IMPURE" + attrs;
189+
}
190+
if (attrs != "") {
191+
context_.Say(expr.source,
192+
"User defined%s function '%s' is not allowed in a "
193+
"WORKDISTRIBUTE construct"_err_en_US,
194+
attrs, root.name());
195+
}
196+
}
197+
}
198+
}
199+
return false;
200+
}
201+
202+
private:
203+
SemanticsContext &context_;
204+
parser::CharBlock source_;
205+
};
206+
146207
// `OmpUnitedTaskDesignatorChecker` is used to check if the designator
147208
// can appear within the TASK construct
148209
class OmpUnitedTaskDesignatorChecker {
@@ -815,6 +876,13 @@ void OmpStructureChecker::Enter(const parser::OpenMPBlockConstruct &x) {
815876
"TARGET construct with nested TEAMS region contains statements or "
816877
"directives outside of the TEAMS construct"_err_en_US);
817878
}
879+
if (GetContext().directive == llvm::omp::Directive::OMPD_workdistribute &&
880+
GetContextParent().directive != llvm::omp::Directive::OMPD_teams) {
881+
context_.Say(x.BeginDir().DirName().source,
882+
"%s region can only be strictly nested within the "
883+
"teams region"_err_en_US,
884+
ContextDirectiveAsFortran());
885+
}
818886
}
819887

820888
CheckNoBranching(block, beginSpec.DirId(), beginSpec.source);
@@ -898,6 +966,17 @@ void OmpStructureChecker::Enter(const parser::OpenMPBlockConstruct &x) {
898966
HasInvalidWorksharingNesting(
899967
beginSpec.source, llvm::omp::nestedWorkshareErrSet);
900968
break;
969+
case llvm::omp::OMPD_workdistribute:
970+
if (!CurrentDirectiveIsNested()) {
971+
context_.Say(beginSpec.source,
972+
"A workdistribute region must be nested inside teams region only."_err_en_US);
973+
}
974+
CheckWorkdistributeBlockStmts(block, beginSpec.source);
975+
break;
976+
case llvm::omp::OMPD_teams_workdistribute:
977+
case llvm::omp::OMPD_target_teams_workdistribute:
978+
CheckWorkdistributeBlockStmts(block, beginSpec.source);
979+
break;
901980
case llvm::omp::Directive::OMPD_scope:
902981
case llvm::omp::Directive::OMPD_single:
903982
// TODO: This check needs to be extended while implementing nesting of
@@ -4546,6 +4625,22 @@ void OmpStructureChecker::CheckWorkshareBlockStmts(
45464625
}
45474626
}
45484627

4628+
void OmpStructureChecker::CheckWorkdistributeBlockStmts(
4629+
const parser::Block &block, parser::CharBlock source) {
4630+
OmpWorkdistributeBlockChecker ompWorkdistributeBlockChecker{context_, source};
4631+
4632+
for (auto it{block.begin()}; it != block.end(); ++it) {
4633+
if (parser::Unwrap<parser::AssignmentStmt>(*it)) {
4634+
parser::Walk(*it, ompWorkdistributeBlockChecker);
4635+
} else {
4636+
context_.Say(source,
4637+
"The structured block in a WORKDISTRIBUTE construct may consist of "
4638+
"only "
4639+
"SCALAR or ARRAY assignments"_err_en_US);
4640+
}
4641+
}
4642+
}
4643+
45494644
void OmpStructureChecker::CheckIfContiguous(const parser::OmpObject &object) {
45504645
if (auto contig{IsContiguous(context_, object)}; contig && !*contig) {
45514646
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 %s | FileCheck --ignore-case --check-prefix="UNPARSE" %s
2+
!RUN: %flang_fc1 -fdebug-dump-parse-tree -fopenmp -fopenmp-version=61 %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
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
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
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

0 commit comments

Comments
 (0)