Skip to content

Commit d6a61ad

Browse files
estewart08skatrak
andauthored
[Flang][OpenMP] Update semantics checks for 'teams' nesting (llvm#126922) (llvm#2605)
This patch introduces a directive set for combined constructs where `teams` is the last leaf. This is used in a couple places to simplify checks, which is NFC, but it also replaces two incorrect uses of `topTeamsSet`. Before, these checks would incorrectly skip combined constructs where `teams` was the last leaf construct when checking for allowed nested constructs inside of a `teams` region. Similarly, it would also incorrectly perform these checks whenever a compound `teams` construct where `teams` was the first leaf construct was found. Co-authored-by: Sergio Afonso <[email protected]>
1 parent ae71285 commit d6a61ad

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace llvm::omp {
2121
//===----------------------------------------------------------------------===//
2222
// - top<Directive>Set: The directive appears alone or as the first in a
2323
// compound construct.
24+
// - bottom<Directive>Set: The directive appears alone or as the last in a
25+
// compound construct.
2426
// - all<Directive>Set: All standalone or compound uses of the directive.
2527

2628
static const OmpDirectiveSet topDistributeSet{
@@ -172,6 +174,11 @@ static const OmpDirectiveSet topTeamsSet{
172174
Directive::OMPD_teams_loop,
173175
};
174176

177+
static const OmpDirectiveSet bottomTeamsSet{
178+
Directive::OMPD_target_teams,
179+
Directive::OMPD_teams,
180+
};
181+
175182
static const OmpDirectiveSet allTeamsSet{
176183
OmpDirectiveSet{
177184
Directive::OMPD_target_teams,

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,7 @@ void OmpStructureChecker::HasInvalidDistributeNesting(
475475
violation = true;
476476
} else {
477477
// `distribute` region has to be strictly nested inside `teams`
478-
if (!OmpDirectiveSet{llvm::omp::OMPD_teams, llvm::omp::OMPD_target_teams}
479-
.test(GetContextParent().directive)) {
478+
if (!llvm::omp::bottomTeamsSet.test(GetContextParent().directive)) {
480479
violation = true;
481480
}
482481
}
@@ -506,8 +505,7 @@ void OmpStructureChecker::HasInvalidLoopBinding(
506505

507506
if (llvm::omp::Directive::OMPD_loop == beginDir.v &&
508507
CurrentDirectiveIsNested() &&
509-
OmpDirectiveSet{llvm::omp::OMPD_teams, llvm::omp::OMPD_target_teams}.test(
510-
GetContextParent().directive)) {
508+
llvm::omp::bottomTeamsSet.test(GetContextParent().directive)) {
511509
teamsBindingChecker(
512510
"`BIND(TEAMS)` must be specified since the `LOOP` region is "
513511
"strictly nested inside a `TEAMS` region."_err_en_US);
@@ -698,7 +696,7 @@ void OmpStructureChecker::Enter(const parser::OpenMPLoopConstruct &x) {
698696
HasInvalidDistributeNesting(x);
699697
HasInvalidLoopBinding(x);
700698
if (CurrentDirectiveIsNested() &&
701-
llvm::omp::topTeamsSet.test(GetContextParent().directive)) {
699+
llvm::omp::bottomTeamsSet.test(GetContextParent().directive)) {
702700
HasInvalidTeamsNesting(beginDir.v, beginDir.source);
703701
}
704702
if ((beginDir.v == llvm::omp::Directive::OMPD_distribute_parallel_do_simd) ||
@@ -1141,7 +1139,7 @@ void OmpStructureChecker::Enter(const parser::OpenMPBlockConstruct &x) {
11411139
}
11421140

11431141
if (CurrentDirectiveIsNested()) {
1144-
if (llvm::omp::topTeamsSet.test(GetContextParent().directive)) {
1142+
if (llvm::omp::bottomTeamsSet.test(GetContextParent().directive)) {
11451143
HasInvalidTeamsNesting(beginDir.v, beginDir.source);
11461144
}
11471145
if (GetContext().directive == llvm::omp::Directive::OMPD_master) {

flang/test/Semantics/OpenMP/nested-target.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ program main
5454
n2 = 10
5555
!$omp target teams map(to:a)
5656
!PORTABILITY: If TARGET DATA directive is nested inside TARGET region, the behaviour is unspecified
57+
!ERROR: Only `DISTRIBUTE`, `PARALLEL`, or `LOOP` regions are allowed to be strictly nested inside `TEAMS` region.
5758
!$omp target data map(n1,n2)
5859
do i=1, n1
5960
do j=1, n2
@@ -65,6 +66,7 @@ program main
6566

6667
!$omp target teams map(to:a) map(from:n1,n2)
6768
!PORTABILITY: If TARGET TEAMS DISTRIBUTE PARALLEL DO directive is nested inside TARGET region, the behaviour is unspecified
69+
!ERROR: Only `DISTRIBUTE`, `PARALLEL`, or `LOOP` regions are allowed to be strictly nested inside `TEAMS` region.
6870
!$omp target teams distribute parallel do
6971
do i=1, n1
7072
do j=1, n2

flang/test/Semantics/OpenMP/nested-teams.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ program main
6868
!$omp end target
6969

7070
!$omp target teams
71+
!ERROR: Only `DISTRIBUTE`, `PARALLEL`, or `LOOP` regions are allowed to be strictly nested inside `TEAMS` region.
7172
!ERROR: TEAMS region can only be strictly nested within the implicit parallel region or TARGET region
7273
!$omp teams
7374
a = 3.14

0 commit comments

Comments
 (0)