Skip to content

Commit 4442fce

Browse files
committed
[flang][openmp] Add parser/semantic support for workdistribute
1 parent f53da4f commit 4442fce

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
@@ -141,6 +141,67 @@ class OmpWorkshareBlockChecker {
141141
parser::CharBlock source_;
142142
};
143143

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

818886
CheckNoBranching(block, beginSpec.DirId(), beginSpec.source);
@@ -896,6 +964,17 @@ void OmpStructureChecker::Enter(const parser::OpenMPBlockConstruct &x) {
896964
HasInvalidWorksharingNesting(
897965
beginSpec.source, llvm::omp::nestedWorkshareErrSet);
898966
break;
967+
case llvm::omp::OMPD_workdistribute:
968+
if (!CurrentDirectiveIsNested()) {
969+
context_.Say(beginSpec.source,
970+
"A workdistribute region must be nested inside teams region only."_err_en_US);
971+
}
972+
CheckWorkdistributeBlockStmts(block, beginSpec.source);
973+
break;
974+
case llvm::omp::OMPD_teams_workdistribute:
975+
case llvm::omp::OMPD_target_teams_workdistribute:
976+
CheckWorkdistributeBlockStmts(block, beginSpec.source);
977+
break;
899978
case llvm::omp::Directive::OMPD_scope:
900979
case llvm::omp::Directive::OMPD_single:
901980
// TODO: This check needs to be extended while implementing nesting of
@@ -4497,6 +4576,22 @@ void OmpStructureChecker::CheckWorkshareBlockStmts(
44974576
}
44984577
}
44994578

4579+
void OmpStructureChecker::CheckWorkdistributeBlockStmts(
4580+
const parser::Block &block, parser::CharBlock source) {
4581+
OmpWorkdistributeBlockChecker ompWorkdistributeBlockChecker{context_, source};
4582+
4583+
for (auto it{block.begin()}; it != block.end(); ++it) {
4584+
if (parser::Unwrap<parser::AssignmentStmt>(*it)) {
4585+
parser::Walk(*it, ompWorkdistributeBlockChecker);
4586+
} else {
4587+
context_.Say(source,
4588+
"The structured block in a WORKDISTRIBUTE construct may consist of "
4589+
"only "
4590+
"SCALAR or ARRAY assignments"_err_en_US);
4591+
}
4592+
}
4593+
}
4594+
45004595
void OmpStructureChecker::CheckIfContiguous(const parser::OmpObject &object) {
45014596
if (auto contig{IsContiguous(context_, object)}; contig && !*contig) {
45024597
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
@@ -1736,10 +1736,13 @@ bool OmpAttributeVisitor::Pre(const parser::OpenMPBlockConstruct &x) {
17361736
case llvm::omp::Directive::OMPD_task:
17371737
case llvm::omp::Directive::OMPD_taskgroup:
17381738
case llvm::omp::Directive::OMPD_teams:
1739+
case llvm::omp::Directive::OMPD_workdistribute:
17391740
case llvm::omp::Directive::OMPD_workshare:
17401741
case llvm::omp::Directive::OMPD_parallel_workshare:
17411742
case llvm::omp::Directive::OMPD_target_teams:
1743+
case llvm::omp::Directive::OMPD_target_teams_workdistribute:
17421744
case llvm::omp::Directive::OMPD_target_parallel:
1745+
case llvm::omp::Directive::OMPD_teams_workdistribute:
17431746
PushContext(dirSpec.source, dirId);
17441747
break;
17451748
default:
@@ -1769,9 +1772,12 @@ void OmpAttributeVisitor::Post(const parser::OpenMPBlockConstruct &x) {
17691772
case llvm::omp::Directive::OMPD_target:
17701773
case llvm::omp::Directive::OMPD_task:
17711774
case llvm::omp::Directive::OMPD_teams:
1775+
case llvm::omp::Directive::OMPD_workdistribute:
17721776
case llvm::omp::Directive::OMPD_parallel_workshare:
17731777
case llvm::omp::Directive::OMPD_target_teams:
1774-
case llvm::omp::Directive::OMPD_target_parallel: {
1778+
case llvm::omp::Directive::OMPD_target_parallel:
1779+
case llvm::omp::Directive::OMPD_target_teams_workdistribute:
1780+
case llvm::omp::Directive::OMPD_teams_workdistribute: {
17751781
bool hasPrivate;
17761782
for (const auto *allocName : allocateNames_) {
17771783
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)