From 15d695d4dbeecf2b7ad36fb91256e9c1bf4f8660 Mon Sep 17 00:00:00 2001 From: Chandra Ghale Date: Wed, 15 Oct 2025 06:46:27 -0500 Subject: [PATCH] Allow orphaned distribute construct --- flang/lib/Semantics/check-omp-loop.cpp | 19 +++++++++---------- .../Semantics/OpenMP/combined-constructs.f90 | 2 +- flang/test/Semantics/OpenMP/do05.f90 | 4 ++-- flang/test/Semantics/OpenMP/linear-iter.f90 | 6 +++--- .../Semantics/OpenMP/nested-distribute.f90 | 13 +++++++++++++ 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/flang/lib/Semantics/check-omp-loop.cpp b/flang/lib/Semantics/check-omp-loop.cpp index c9d0495850b6e..331397b035cf9 100644 --- a/flang/lib/Semantics/check-omp-loop.cpp +++ b/flang/lib/Semantics/check-omp-loop.cpp @@ -127,24 +127,23 @@ using namespace Fortran::semantics::omp; void OmpStructureChecker::HasInvalidDistributeNesting( const parser::OpenMPLoopConstruct &x) { - bool violation{false}; const parser::OmpDirectiveName &beginName{x.BeginDir().DirName()}; if (llvm::omp::topDistributeSet.test(beginName.v)) { // `distribute` region has to be nested - if (!CurrentDirectiveIsNested()) { - violation = true; - } else { + if (CurrentDirectiveIsNested()) { // `distribute` region has to be strictly nested inside `teams` if (!llvm::omp::bottomTeamsSet.test(GetContextParent().directive)) { - violation = true; + context_.Say(beginName.source, + "`DISTRIBUTE` region has to be strictly nested inside `TEAMS` " + "region."_err_en_US); } + } else { + // If not lexically nested (orphaned), issue a warning. + context_.Say(beginName.source, + "`DISTRIBUTE` must be dynamically enclosed in a `TEAMS` " + "region."_warn_en_US); } } - if (violation) { - context_.Say(beginName.source, - "`DISTRIBUTE` region has to be strictly nested inside `TEAMS` " - "region."_err_en_US); - } } void OmpStructureChecker::HasInvalidLoopBinding( const parser::OpenMPLoopConstruct &x) { diff --git a/flang/test/Semantics/OpenMP/combined-constructs.f90 b/flang/test/Semantics/OpenMP/combined-constructs.f90 index 49da56298d426..3e9c65434f695 100644 --- a/flang/test/Semantics/OpenMP/combined-constructs.f90 +++ b/flang/test/Semantics/OpenMP/combined-constructs.f90 @@ -7,7 +7,7 @@ program main real(8) :: a(256), b(256) N = 256 - !ERROR: `DISTRIBUTE` region has to be strictly nested inside `TEAMS` region. + !WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region. !$omp distribute simd do i = 1, N a(i) = 3.14d0 diff --git a/flang/test/Semantics/OpenMP/do05.f90 b/flang/test/Semantics/OpenMP/do05.f90 index 24844f9fe4f62..d8320520879ef 100644 --- a/flang/test/Semantics/OpenMP/do05.f90 +++ b/flang/test/Semantics/OpenMP/do05.f90 @@ -49,7 +49,7 @@ program omp_do end do !$omp end parallel do simd - !ERROR: `DISTRIBUTE` region has to be strictly nested inside `TEAMS` region. + !WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region. !$omp distribute parallel do do i=1,10 if( i == 3 ) then @@ -64,7 +64,7 @@ program omp_do end do !$omp end distribute parallel do - !ERROR: `DISTRIBUTE` region has to be strictly nested inside `TEAMS` region. + !WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region. !$omp distribute parallel do simd do i=1,10 if( i == 3 ) then diff --git a/flang/test/Semantics/OpenMP/linear-iter.f90 b/flang/test/Semantics/OpenMP/linear-iter.f90 index 1f40228be92ad..5c9f2d5fb34c4 100644 --- a/flang/test/Semantics/OpenMP/linear-iter.f90 +++ b/flang/test/Semantics/OpenMP/linear-iter.f90 @@ -53,7 +53,7 @@ SUBROUTINE LINEAR_BAD(N) !$omp end teams !$omp end target - !ERROR: `DISTRIBUTE` region has to be strictly nested inside `TEAMS` region. + !WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region. !ERROR: Variable 'j' not allowed in LINEAR clause, only loop iterator can be specified in LINEAR clause of a construct combined with DISTRIBUTE !$omp distribute simd linear(i,j) do i = 1, N @@ -63,7 +63,7 @@ SUBROUTINE LINEAR_BAD(N) enddo !$omp end distribute simd - !ERROR: `DISTRIBUTE` region has to be strictly nested inside `TEAMS` region. + !WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region. !ERROR: Variable 'j' not allowed in LINEAR clause, only loop iterator can be specified in LINEAR clause of a construct combined with DISTRIBUTE !$omp distribute simd linear(i,j) collapse(1) do i = 1, N @@ -73,7 +73,7 @@ SUBROUTINE LINEAR_BAD(N) enddo !$omp end distribute simd - !ERROR: `DISTRIBUTE` region has to be strictly nested inside `TEAMS` region. + !WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region. !$omp distribute simd linear(i,j) collapse(2) do i = 1, N do j = 1, N diff --git a/flang/test/Semantics/OpenMP/nested-distribute.f90 b/flang/test/Semantics/OpenMP/nested-distribute.f90 index cb4aea3bcdffa..4134ced7d81f6 100644 --- a/flang/test/Semantics/OpenMP/nested-distribute.f90 +++ b/flang/test/Semantics/OpenMP/nested-distribute.f90 @@ -1,6 +1,15 @@ ! RUN: %python %S/../test_errors.py %s %flang -fopenmp ! Check OpenMP clause validity for the following directives: ! 2.10 Device constructs + +subroutine f + integer :: i + !WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region. + !$omp distribute + do i = 1, 100 + print *, "hello" + end do +end subroutine program main real(8) :: arrayA(256), arrayB(256) @@ -108,4 +117,8 @@ program main end do !$omp end distribute !$omp end task + + !$omp teams + call foo + !$omp end teams end program main