[flang-commits] [flang] [Flang][OpenMP] Reset REQUIRES directive with new program unit (PR #227829)
Michael Klemm via flang-commits
flang-commits at lists.llvm.org
Sat Oct 3 01:10:45 PDT 2026
https://github.com/mjklemm updated https://github.com/llvm/llvm-project/pull/227829
>From c02be319bc3723cb09cd57a82eed2ec15e1bea36 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Wed, 30 Sep 2026 21:09:07 +0200
Subject: [PATCH 1/2] [Flang][OpenMP] Reset REQUIRES directive with new program
unit
---
flang/lib/Semantics/check-omp-structure.cpp | 4 +++
flang/test/Semantics/OpenMP/requires03.f90 | 16 ++++-------
flang/test/Semantics/OpenMP/requires04.f90 | 8 ++----
flang/test/Semantics/OpenMP/requires05.f90 | 8 ++----
flang/test/Semantics/OpenMP/requires06.f90 | 8 ++----
flang/test/Semantics/OpenMP/requires07.f90 | 17 ++++-------
flang/test/Semantics/OpenMP/requires08.f90 | 17 ++++-------
flang/test/Semantics/OpenMP/requires11.f90 | 31 +++++++++++++++++++++
8 files changed, 61 insertions(+), 48 deletions(-)
create mode 100644 flang/test/Semantics/OpenMP/requires11.f90
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 041b80ccffad6d..3880128545f5ea 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 7b87a20dd59f85..22ff3219d83471 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 18790054e733df..ca8cf586dfbcc0 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 871d0c433685b3..07243b5d28e341 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 58d157772a07cc..ded69a36280c68 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 c435de6a706a13..f3996f3b2eaa90 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 098f4b9bd248f4..ebd9994ef802ef 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 00000000000000..20b9d6d7fddc05
--- /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 (LCOMPILER-1575).
+
+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
>From 4dac5afa49f5af23476b836f6f2681c2f16c9e67 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Thu, 1 Oct 2026 11:08:45 +0200
Subject: [PATCH 2/2] Remove trace of Jira ticket
---
flang/test/Semantics/OpenMP/requires11.f90 | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/flang/test/Semantics/OpenMP/requires11.f90 b/flang/test/Semantics/OpenMP/requires11.f90
index 20b9d6d7fddc05..ff7a6c8e0bc00e 100644
--- a/flang/test/Semantics/OpenMP/requires11.f90
+++ b/flang/test/Semantics/OpenMP/requires11.f90
@@ -6,7 +6,7 @@
! 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 (LCOMPILER-1575).
+! without diagnostics.
module m
!$omp requires unified_shared_memory
More information about the flang-commits
mailing list