[flang-commits] [flang] 4aaffe8 - [Flang][OpenMP] Reset REQUIRES directive with new program unit (#227829)
via flang-commits
flang-commits at lists.llvm.org
Sat Oct 3 01:40:07 PDT 2026
Author: Michael Klemm
Date: 2026-10-03T08:39:59Z
New Revision: 4aaffe88596a4de3570ebf887ed74fa3b8a90a0c
URL: https://github.com/llvm/llvm-project/commit/4aaffe88596a4de3570ebf887ed74fa3b8a90a0c
DIFF: https://github.com/llvm/llvm-project/commit/4aaffe88596a4de3570ebf887ed74fa3b8a90a0c.diff
LOG: [Flang][OpenMP] Reset REQUIRES directive with new program unit (#227829)
A REQUIRES directive with unified_address, unified_shared_memory, or
reverse_offload must appear lexically before any device construct or
device routine is scoped to a program unit.
Added:
flang/test/Semantics/OpenMP/requires11.f90
Modified:
flang/lib/Semantics/check-omp-structure.cpp
flang/test/Semantics/OpenMP/requires03.f90
flang/test/Semantics/OpenMP/requires04.f90
flang/test/Semantics/OpenMP/requires05.f90
flang/test/Semantics/OpenMP/requires06.f90
flang/test/Semantics/OpenMP/requires07.f90
flang/test/Semantics/OpenMP/requires08.f90
Removed:
################################################################################
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 041b80ccffad6..3880128545f5e 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -171,6 +171,10 @@ OmpStructureChecker::OmpStructureChecker(SemanticsContext &context)
void OmpStructureChecker::Enter(const parser::ProgramUnit &) { //
ClearLabels();
declareVariantPairs_.clear();
+ // A REQUIRES directive with unified_address, unified_shared_memory, or
+ // reverse_offload must appear lexically before any device construct or
+ // device routine is scoped to a program unit.
+ deviceConstructFound_ = false;
}
void OmpStructureChecker::Leave(const parser::ProgramUnit &) {
diff --git a/flang/test/Semantics/OpenMP/requires03.f90 b/flang/test/Semantics/OpenMP/requires03.f90
index 7b87a20dd59f8..22ff3219d8347 100644
--- a/flang/test/Semantics/OpenMP/requires03.f90
+++ b/flang/test/Semantics/OpenMP/requires03.f90
@@ -1,8 +1,9 @@
! RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=50
! OpenMP Version 5.0
! 2.4 Requires directive
-! Target-related clauses in 'requires' directives must come strictly before any
-! device constructs, such as target regions.
+! The 'lexically before any device construct' restriction is scoped to a
+! program unit: a device construct (here a target region) in one program unit
+! must not make a REQUIRES directive in a separate program unit ill-formed.
subroutine f
!$omp target
@@ -10,13 +11,6 @@ subroutine f
end subroutine f
subroutine g
- !ERROR: REQUIRES directive with 'DYNAMIC_ALLOCATORS' clause found lexically after device construct
- !$omp requires dynamic_allocators
- !WARNING: REVERSE_OFFLOAD clause is not supported and will be ignored
- !ERROR: REQUIRES directive with 'REVERSE_OFFLOAD' clause found lexically after device construct
- !$omp requires reverse_offload
- !ERROR: REQUIRES directive with 'UNIFIED_ADDRESS' clause found lexically after device construct
- !$omp requires unified_address
- !ERROR: REQUIRES directive with 'UNIFIED_SHARED_MEMORY' clause found lexically after device construct
!$omp requires unified_shared_memory
-end subroutine g
+ !$omp requires unified_address
+end subroutine g
\ No newline at end of file
diff --git a/flang/test/Semantics/OpenMP/requires04.f90 b/flang/test/Semantics/OpenMP/requires04.f90
index 18790054e733d..ca8cf586dfbcc 100644
--- a/flang/test/Semantics/OpenMP/requires04.f90
+++ b/flang/test/Semantics/OpenMP/requires04.f90
@@ -2,16 +2,14 @@
! OpenMP Version 5.0
! 2.4 Requires directive
! Target-related clauses in 'requires' directives must come strictly before any
-! device constructs, such as declare target with device_type=nohost|any.
+! device constructs in the same program unit, such as declare target with
+! device_type=nohost|any.
subroutine f
integer, save :: x
!WARNING: The usage of TO clause on DECLARE TARGET directive has been deprecated. Use ENTER clause instead. [-Wopenmp-usage]
!$omp declare target to(x) device_type(nohost)
!$omp declare target enter(x) device_type(nohost)
-end subroutine f
-
-subroutine g
!ERROR: REQUIRES directive with 'DYNAMIC_ALLOCATORS' clause found lexically after device construct
!$omp requires dynamic_allocators
!WARNING: REVERSE_OFFLOAD clause is not supported and will be ignored
@@ -21,4 +19,4 @@ subroutine g
!$omp requires unified_address
!ERROR: REQUIRES directive with 'UNIFIED_SHARED_MEMORY' clause found lexically after device construct
!$omp requires unified_shared_memory
-end subroutine g
+end subroutine f
\ No newline at end of file
diff --git a/flang/test/Semantics/OpenMP/requires05.f90 b/flang/test/Semantics/OpenMP/requires05.f90
index 871d0c433685b..07243b5d28e34 100644
--- a/flang/test/Semantics/OpenMP/requires05.f90
+++ b/flang/test/Semantics/OpenMP/requires05.f90
@@ -2,15 +2,13 @@
! OpenMP Version 5.0
! 2.4 Requires directive
! Target-related clauses in 'requires' directives must come strictly before any
-! device constructs, such as declare target with 'to' clause and no device_type.
+! device constructs in the same program unit, such as declare target with 'to'
+! clause and no device_type.
subroutine f
!WARNING: The usage of TO clause on DECLARE TARGET directive has been deprecated. Use ENTER clause instead. [-Wopenmp-usage]
!$omp declare target to(f)
!$omp declare target enter(f)
-end subroutine f
-
-subroutine g
!ERROR: REQUIRES directive with 'DYNAMIC_ALLOCATORS' clause found lexically after device construct
!$omp requires dynamic_allocators
!WARNING: REVERSE_OFFLOAD clause is not supported and will be ignored
@@ -20,4 +18,4 @@ subroutine g
!$omp requires unified_address
!ERROR: REQUIRES directive with 'UNIFIED_SHARED_MEMORY' clause found lexically after device construct
!$omp requires unified_shared_memory
-end subroutine g
+end subroutine f
\ No newline at end of file
diff --git a/flang/test/Semantics/OpenMP/requires06.f90 b/flang/test/Semantics/OpenMP/requires06.f90
index 58d157772a07c..ded69a36280c6 100644
--- a/flang/test/Semantics/OpenMP/requires06.f90
+++ b/flang/test/Semantics/OpenMP/requires06.f90
@@ -2,13 +2,11 @@
! OpenMP Version 5.0
! 2.4 Requires directive
! Target-related clauses in 'requires' directives must come strictly before any
-! device constructs, such as declare target with extended list.
+! device constructs in the same program unit, such as declare target with
+! extended list.
subroutine f
!$omp declare target (f)
-end subroutine f
-
-subroutine g
!ERROR: REQUIRES directive with 'DYNAMIC_ALLOCATORS' clause found lexically after device construct
!$omp requires dynamic_allocators
!WARNING: REVERSE_OFFLOAD clause is not supported and will be ignored
@@ -18,4 +16,4 @@ subroutine g
!$omp requires unified_address
!ERROR: REQUIRES directive with 'UNIFIED_SHARED_MEMORY' clause found lexically after device construct
!$omp requires unified_shared_memory
-end subroutine g
+end subroutine f
\ No newline at end of file
diff --git a/flang/test/Semantics/OpenMP/requires07.f90 b/flang/test/Semantics/OpenMP/requires07.f90
index c435de6a706a1..f3996f3b2eaa9 100644
--- a/flang/test/Semantics/OpenMP/requires07.f90
+++ b/flang/test/Semantics/OpenMP/requires07.f90
@@ -1,8 +1,10 @@
! RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=50
! OpenMP Version 5.0
! 2.4 Requires directive
-! Target-related clauses in 'requires' directives must come strictly before any
-! device constructs, such as target parallel regions.
+! The 'lexically before any device construct' restriction is scoped to a
+! program unit: a device construct (here a target parallel region) in one
+! program unit must not make a REQUIRES directive in a separate program unit
+! ill-formed.
subroutine f
!$omp target parallel
@@ -10,13 +12,6 @@ subroutine f
end subroutine f
subroutine g
- !ERROR: REQUIRES directive with 'DYNAMIC_ALLOCATORS' clause found lexically after device construct
- !$omp requires dynamic_allocators
- !WARNING: REVERSE_OFFLOAD clause is not supported and will be ignored
- !ERROR: REQUIRES directive with 'REVERSE_OFFLOAD' clause found lexically after device construct
- !$omp requires reverse_offload
- !ERROR: REQUIRES directive with 'UNIFIED_ADDRESS' clause found lexically after device construct
- !$omp requires unified_address
- !ERROR: REQUIRES directive with 'UNIFIED_SHARED_MEMORY' clause found lexically after device construct
!$omp requires unified_shared_memory
-end subroutine g
+ !$omp requires unified_address
+end subroutine g
\ No newline at end of file
diff --git a/flang/test/Semantics/OpenMP/requires08.f90 b/flang/test/Semantics/OpenMP/requires08.f90
index 098f4b9bd248f..ebd9994ef802e 100644
--- a/flang/test/Semantics/OpenMP/requires08.f90
+++ b/flang/test/Semantics/OpenMP/requires08.f90
@@ -1,8 +1,10 @@
! RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=50
! OpenMP Version 5.0
! 2.4 Requires directive
-! Target-related clauses in 'requires' directives must come strictly before any
-! device constructs, such as target teams distribute parallel do loops.
+! The 'lexically before any device construct' restriction is scoped to a
+! program unit: a device construct (here a target teams distribute parallel do
+! loop) in one program unit must not make a REQUIRES directive in a separate
+! program unit ill-formed.
subroutine f
!$omp target teams distribute parallel do
@@ -12,13 +14,6 @@ subroutine f
end subroutine f
subroutine g
- !ERROR: REQUIRES directive with 'DYNAMIC_ALLOCATORS' clause found lexically after device construct
- !$omp requires dynamic_allocators
- !WARNING: REVERSE_OFFLOAD clause is not supported and will be ignored
- !ERROR: REQUIRES directive with 'REVERSE_OFFLOAD' clause found lexically after device construct
- !$omp requires reverse_offload
- !ERROR: REQUIRES directive with 'UNIFIED_ADDRESS' clause found lexically after device construct
- !$omp requires unified_address
- !ERROR: REQUIRES directive with 'UNIFIED_SHARED_MEMORY' clause found lexically after device construct
!$omp requires unified_shared_memory
-end subroutine g
+ !$omp requires unified_address
+end subroutine g
\ No newline at end of file
diff --git a/flang/test/Semantics/OpenMP/requires11.f90 b/flang/test/Semantics/OpenMP/requires11.f90
new file mode 100644
index 0000000000000..ff7a6c8e0bc00
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/requires11.f90
@@ -0,0 +1,31 @@
+! RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=50
+! OpenMP Version 5.0
+! 2.4 Requires directive
+! A unified_shared_memory requirement must be specified in every program unit
+! that contains device constructs. A module and a main program in the same file
+! are separate program units, each with its own specification part, so the
+! REQUIRES directive in the program's specification part is not "lexically
+! after" a device construct that appears in the module. This must compile
+! without diagnostics.
+
+module m
+ !$omp requires unified_shared_memory
+contains
+ subroutine init(x, n)
+ integer, intent(in) :: n
+ real, intent(inout) :: x(:)
+ integer :: i
+ !$omp target teams distribute parallel do
+ do i = 1, n
+ x(i) = 1.0
+ end do
+ end subroutine init
+end module m
+
+program main
+ use m, only: init
+ !$omp requires unified_shared_memory
+ integer, parameter :: n = 100
+ real :: x(n)
+ call init(x, n)
+end program main
\ No newline at end of file
More information about the flang-commits
mailing list