Skip to content

[Flang][OpenMP] Deprecate Allocate Directive #142378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 4, 2025

Conversation

Stylie777
Copy link
Contributor

@Stylie777 Stylie777 commented Jun 2, 2025

As part of OpenMP 5.2, the allocate directive has been deprecated in favour of the allocators construct for Fortran when an ALLOCATE statement follows the OpenMP allocate directive.

To enable this in flang, a warning has been added informing the user of this. Tests to ensure this behaviour is continued are also included.

See also: #110008

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir flang:openmp flang:semantics labels Jun 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 2, 2025

@llvm/pr-subscribers-flang-fir-hlfir
@llvm/pr-subscribers-flang-semantics

@llvm/pr-subscribers-flang-openmp

Author: Jack Styles (Stylie777)

Changes

As part of OpenMP 5.2, the allocate directive has been deprecated in favour of the allocators construct for Fortran. To enable this in flang, a warning has been added informing the user of this. Tests to ensure this behaviour is continued are also included.

See also: #110008


Full diff: https://github.com/llvm/llvm-project/pull/142378.diff

13 Files Affected:

  • (modified) flang/lib/Semantics/resolve-directives.cpp (+5)
  • (modified) flang/test/Lower/OpenMP/Todo/omp-declarative-allocate-align.f90 (+1)
  • (modified) flang/test/Lower/OpenMP/Todo/omp-declarative-allocate.f90 (+1)
  • (modified) flang/test/Semantics/OpenMP/allocate-align01.f90 (+2)
  • (modified) flang/test/Semantics/OpenMP/allocate01.f90 (+2)
  • (modified) flang/test/Semantics/OpenMP/allocate02.f90 (+4)
  • (modified) flang/test/Semantics/OpenMP/allocate03.f90 (+2)
  • (modified) flang/test/Semantics/OpenMP/allocate04.f90 (+5)
  • (modified) flang/test/Semantics/OpenMP/allocate05.f90 (+2)
  • (modified) flang/test/Semantics/OpenMP/allocate06.f90 (+2)
  • (modified) flang/test/Semantics/OpenMP/allocate07.f90 (+5)
  • (modified) flang/test/Semantics/OpenMP/allocate08.f90 (+10)
  • (modified) flang/test/Semantics/OpenMP/allocate09.f90 (+8)
diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index 9fa7bc8964854..10121898817db 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -2024,6 +2024,7 @@ bool OmpAttributeVisitor::Pre(const parser::OpenMPThreadprivate &x) {
 
 bool OmpAttributeVisitor::Pre(const parser::OpenMPDeclarativeAllocate &x) {
   PushContext(x.source, llvm::omp::Directive::OMPD_allocate);
+  IssueNonConformanceWarning(llvm::omp::Directive::OMPD_allocate, x.source);
   const auto &list{std::get<parser::OmpObjectList>(x.t)};
   ResolveOmpObjectList(list, Symbol::Flag::OmpDeclarativeAllocateDirective);
   return false;
@@ -2036,6 +2037,7 @@ bool OmpAttributeVisitor::Pre(const parser::OpenMPDispatchConstruct &x) {
 
 bool OmpAttributeVisitor::Pre(const parser::OpenMPExecutableAllocate &x) {
   PushContext(x.source, llvm::omp::Directive::OMPD_allocate);
+  IssueNonConformanceWarning(llvm::omp::Directive::OMPD_allocate, x.source);
   const auto &list{std::get<std::optional<parser::OmpObjectList>>(x.t)};
   if (list) {
     ResolveOmpObjectList(*list, Symbol::Flag::OmpExecutableAllocateDirective);
@@ -3074,6 +3076,9 @@ void OmpAttributeVisitor::IssueNonConformanceWarning(
   case llvm::omp::OMPD_parallel_master_taskloop_simd:
     setAlternativeStr("PARALLEL_MASKED TASKLOOP SIMD");
     break;
+  case llvm::omp::OMPD_allocate:
+    setAlternativeStr("ALLOCATORS");
+    break;
   case llvm::omp::OMPD_target_loop:
   default:;
   }
diff --git a/flang/test/Lower/OpenMP/Todo/omp-declarative-allocate-align.f90 b/flang/test/Lower/OpenMP/Todo/omp-declarative-allocate-align.f90
index 8daf20e1ae400..d9d7890222467 100644
--- a/flang/test/Lower/OpenMP/Todo/omp-declarative-allocate-align.f90
+++ b/flang/test/Lower/OpenMP/Todo/omp-declarative-allocate-align.f90
@@ -5,6 +5,7 @@
 program main
   integer :: x
 
+  ! CHECK: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   ! CHECK: not yet implemented: OpenMPDeclarativeAllocate
   !$omp allocate(x) align(32)
 end
diff --git a/flang/test/Lower/OpenMP/Todo/omp-declarative-allocate.f90 b/flang/test/Lower/OpenMP/Todo/omp-declarative-allocate.f90
index 425ccbc5dd56c..44bfc32e1565d 100644
--- a/flang/test/Lower/OpenMP/Todo/omp-declarative-allocate.f90
+++ b/flang/test/Lower/OpenMP/Todo/omp-declarative-allocate.f90
@@ -5,6 +5,7 @@
 program main
   integer :: x, y
 
+  // CHECK: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   // CHECK: not yet implemented: OpenMPDeclarativeAllocate
   !$omp allocate(x, y)
 end
diff --git a/flang/test/Semantics/OpenMP/allocate-align01.f90 b/flang/test/Semantics/OpenMP/allocate-align01.f90
index ba0776cf46a6d..2217505d13895 100644
--- a/flang/test/Semantics/OpenMP/allocate-align01.f90
+++ b/flang/test/Semantics/OpenMP/allocate-align01.f90
@@ -11,8 +11,10 @@ program allocate_align_tree
     integer :: z, t, xx
     t = 2
     z = 3
+    !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
     !ERROR: The alignment value should be a constant positive integer
 !$omp allocate(j) align(xx)
+    !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
     !ERROR: The alignment value should be a constant positive integer
 !$omp allocate(xarray) align(-32) allocator(omp_large_cap_mem_alloc)
     allocate(j(z), xarray(t))
diff --git a/flang/test/Semantics/OpenMP/allocate01.f90 b/flang/test/Semantics/OpenMP/allocate01.f90
index 6ccb8bb09e830..c8aa6cf8ad977 100644
--- a/flang/test/Semantics/OpenMP/allocate01.f90
+++ b/flang/test/Semantics/OpenMP/allocate01.f90
@@ -15,10 +15,12 @@ subroutine sema()
     integer :: a, b
     real, dimension (:,:), allocatable :: darray
 
+    !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
     !ERROR: List items must be declared in the same scoping unit in which the ALLOCATE directive appears
     !$omp allocate(y)
         print *, a
 
+    !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
     !ERROR: List items must be declared in the same scoping unit in which the ALLOCATE directive appears
     !$omp allocate(x) allocator(omp_default_mem_alloc)
       allocate ( x(a), darray(a, b) )
diff --git a/flang/test/Semantics/OpenMP/allocate02.f90 b/flang/test/Semantics/OpenMP/allocate02.f90
index 8f0579e810bb9..fa175d0ab7ced 100644
--- a/flang/test/Semantics/OpenMP/allocate02.f90
+++ b/flang/test/Semantics/OpenMP/allocate02.f90
@@ -11,14 +11,18 @@ subroutine allocate()
   integer :: a, b
   real, dimension (:,:), allocatable :: darray
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(x, y) allocator(omp_default_mem_alloc)
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: At most one ALLOCATOR clause can appear on the ALLOCATE directive
   !$omp allocate(x, y) allocator(omp_default_mem_alloc) allocator(omp_default_mem_alloc)
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(darray) allocator(omp_default_mem_alloc)
       allocate ( darray(a, b) )
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: At most one ALLOCATOR clause can appear on the ALLOCATE directive
   !$omp allocate(darray) allocator(omp_default_mem_alloc) allocator(omp_default_mem_alloc)
       allocate ( darray(a, b) )
diff --git a/flang/test/Semantics/OpenMP/allocate03.f90 b/flang/test/Semantics/OpenMP/allocate03.f90
index e35115f3897cc..15f924bf2ca1f 100644
--- a/flang/test/Semantics/OpenMP/allocate03.f90
+++ b/flang/test/Semantics/OpenMP/allocate03.f90
@@ -15,9 +15,11 @@ subroutine allocate()
   real, dimension (:,:), allocatable :: darray
   integer :: a, b
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: A variable that is part of another variable (as an array or structure element) cannot appear on the ALLOCATE directive
   !$omp allocate(my_var%array)
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: A variable that is part of another variable (as an array or structure element) cannot appear on the ALLOCATE directive
   !$omp allocate(darray, my_var%array) allocator(omp_default_mem_alloc)
     allocate ( darray(a, b) )
diff --git a/flang/test/Semantics/OpenMP/allocate04.f90 b/flang/test/Semantics/OpenMP/allocate04.f90
index bbd74eb2ca101..2882e62acc2af 100644
--- a/flang/test/Semantics/OpenMP/allocate04.f90
+++ b/flang/test/Semantics/OpenMP/allocate04.f90
@@ -15,14 +15,19 @@ subroutine allocate(z)
   integer :: x, y, z
 
   associate (a => x)
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(x) allocator(omp_default_mem_alloc)
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: PRIVATE clause is not allowed on the ALLOCATE directive
   !$omp allocate(y) private(y)
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: List item 'z' in ALLOCATE directive must not be a dummy argument
   !$omp allocate(z)
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: List item 'p' in ALLOCATE directive must not have POINTER attribute
   !$omp allocate(p)
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: List item 'a' in ALLOCATE directive must not be an associate name
   !$omp allocate(a)
   end associate
diff --git a/flang/test/Semantics/OpenMP/allocate05.f90 b/flang/test/Semantics/OpenMP/allocate05.f90
index a787e8bb32a4c..2c81c4dbc82c7 100644
--- a/flang/test/Semantics/OpenMP/allocate05.f90
+++ b/flang/test/Semantics/OpenMP/allocate05.f90
@@ -13,11 +13,13 @@ subroutine allocate()
   real, dimension (:,:), allocatable :: darray
 
   !$omp target
+      !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
       !$omp allocate allocator(omp_default_mem_alloc)
           allocate ( darray(a, b) )
   !$omp end target
 
   !$omp target
+      !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
       !ERROR: ALLOCATE directives that appear in a TARGET region must specify an allocator clause
       !$omp allocate
           allocate ( darray(a, b) )
diff --git a/flang/test/Semantics/OpenMP/allocate06.f90 b/flang/test/Semantics/OpenMP/allocate06.f90
index e14134cd07301..f6172c9d905bb 100644
--- a/flang/test/Semantics/OpenMP/allocate06.f90
+++ b/flang/test/Semantics/OpenMP/allocate06.f90
@@ -11,9 +11,11 @@ subroutine allocate()
   integer :: a, b, x
   real, dimension (:,:), allocatable :: darray
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: List items specified in the ALLOCATE directive must not have the ALLOCATABLE attribute unless the directive is associated with an ALLOCATE statement
   !$omp allocate(darray) allocator(omp_default_mem_alloc)
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(darray) allocator(omp_default_mem_alloc)
     allocate(darray(a, b))
 
diff --git a/flang/test/Semantics/OpenMP/allocate07.f90 b/flang/test/Semantics/OpenMP/allocate07.f90
index 396df598b2521..cad55a5adf4bf 100644
--- a/flang/test/Semantics/OpenMP/allocate07.f90
+++ b/flang/test/Semantics/OpenMP/allocate07.f90
@@ -18,18 +18,23 @@ subroutine allocate()
   CHARACTER(LEN=32) :: w
   INTEGER, DIMENSION(:), ALLOCATABLE :: y
   
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: A type parameter inquiry cannot appear on the ALLOCATE directive
   !$omp allocate(x%KIND)
   
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: A type parameter inquiry cannot appear on the ALLOCATE directive
   !$omp allocate(w%LEN)
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: A type parameter inquiry cannot appear on the ALLOCATE directive
   !$omp allocate(y%KIND)
   
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: A type parameter inquiry cannot appear on the ALLOCATE directive
   !$omp allocate(my_var%kind_param)
  
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: A type parameter inquiry cannot appear on the ALLOCATE directive
   !$omp allocate(my_var%len_param)
 
diff --git a/flang/test/Semantics/OpenMP/allocate08.f90 b/flang/test/Semantics/OpenMP/allocate08.f90
index fc950ea4fca36..6ceb87bfa9bdc 100644
--- a/flang/test/Semantics/OpenMP/allocate08.f90
+++ b/flang/test/Semantics/OpenMP/allocate08.f90
@@ -25,20 +25,30 @@ subroutine allocate()
   trait(1)%value = default_mem_fb
   custom_allocator = omp_init_allocator(memspace, 1, trait)
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(x) allocator(omp_default_mem_alloc)
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(y) allocator(omp_default_mem_alloc)
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(z) allocator(omp_default_mem_alloc)
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(x)
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(y)
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(z)
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(w) allocator(custom_allocator)
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: If list items within the ALLOCATE directive have the SAVE attribute, are a common block name, or are declared in the scope of a module, then only predefined memory allocator parameters can be used in the allocator clause
   !$omp allocate(x) allocator(custom_allocator)
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: If list items within the ALLOCATE directive have the SAVE attribute, are a common block name, or are declared in the scope of a module, then only predefined memory allocator parameters can be used in the allocator clause
   !$omp allocate(y) allocator(custom_allocator)
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: If list items within the ALLOCATE directive have the SAVE attribute, are a common block name, or are declared in the scope of a module, then only predefined memory allocator parameters can be used in the allocator clause
   !$omp allocate(z) allocator(custom_allocator)
 end subroutine allocate
diff --git a/flang/test/Semantics/OpenMP/allocate09.f90 b/flang/test/Semantics/OpenMP/allocate09.f90
index 0f93a340fe1e4..c078f060d620a 100644
--- a/flang/test/Semantics/OpenMP/allocate09.f90
+++ b/flang/test/Semantics/OpenMP/allocate09.f90
@@ -12,23 +12,31 @@ subroutine allocate()
   integer, dimension(:), allocatable :: a, b, c, d, e, f, &
                                         g, h, i, j, k, l
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(a) allocator(omp_default_mem_alloc)
     allocate(a(1), b(2))
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(c, d) allocator(omp_default_mem_alloc)
     allocate(c(3), d(4))
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(e) allocator(omp_default_mem_alloc)
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(f, g) allocator(omp_default_mem_alloc)
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate
     allocate(e(5), f(6), g(7))
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: Object 'i' in ALLOCATE directive not found in corresponding ALLOCATE statement
   !$omp allocate(h, i) allocator(omp_default_mem_alloc)
     allocate(h(8))
 
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !ERROR: Object 'j' in ALLOCATE directive not found in corresponding ALLOCATE statement
   !$omp allocate(j, k) allocator(omp_default_mem_alloc)
+  !WARNING: OpenMP directive ALLOCATE has been deprecated, please use ALLOCATORS instead.
   !$omp allocate(l) allocator(omp_default_mem_alloc)
     allocate(k(9), l(10))
 

@kparzysz
Copy link
Contributor

kparzysz commented Jun 2, 2025

Only the form of ALLOCATE directive that was associated with an ALLOCATE statement was deprecated:
[5.2:625:9-10] "The use of one or more allocate directives with an associated ALLOCATE statement was deprecated."

@kparzysz
Copy link
Contributor

kparzysz commented Jun 2, 2025

In terms of AST nodes, it's the OpenMPExecutableAllocate form that is deprecated.

Edit: an example of executable ALLOCATE:

subroutine f00
  integer, allocatable :: x(:)
  continue
  !$omp allocate(x)
  allocate(x(10))
  x = 1
end

The executable ALLOCATE must be preceded by an executable statement (hence the "continue").

Note that declarative ALLOCATE does not allow variables with the ALLOCATABLE attribute, so there can't even be an ALLOCATE statement with the same variables.

@raghavendhra
Copy link
Contributor

Only the form of ALLOCATE directive that was associated with an ALLOCATE statement was deprecated: [5.2:625:9-10] "The use of one or more allocate directives with an associated ALLOCATE statement was deprecated."

+1

OMP ALLOCATE directive is present in the released OpenMP 6.0 standards document(https://www.openmp.org/wp-content/uploads/OpenMP-API-Specification-6-0.pdf). 8.5 section

Stylie777 added 4 commits June 2, 2025 16:08
As part of OpenMP 5.2, the allocate directive has been deprecated
in favour of the allocators construct for Fortran. To enable this in
flang, a warning has been added informing the user of this. Tests to
ensure this behaviour is continued are also included.

See also: llvm#110008
The following has been fixed:
- Updated comments to be `!` instead of `\\` in test file
As per the spec for OpenMP 5.2, it is only when the allocate
directive is followed by an allocate statement that has been
deprecated. This commit removes the deprecation for the declarative
version of `allocate`, and updates the appropriate tests to
accomodate this.
@Stylie777 Stylie777 force-pushed the deprecate_allocate_directive branch from 4ec4158 to 66f1fa9 Compare June 3, 2025 08:26
@Stylie777
Copy link
Contributor Author

Thanks @kparzysz and @raghavendhra for your review.
I have reworked the PR to only add the deprecation warning for executable allocate directives.

Copy link
Contributor

@kparzysz kparzysz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, thanks. Could you add a version check, and only emit the warning when the OpenMP version is >= 52? You can get the version from SemanticsContext:
context_.langOptions().OpenMPVersion.

@tblah
Copy link
Contributor

tblah commented Jun 3, 2025

This looks good, thanks. Could you add a version check, and only emit the warning when the OpenMP version is >= 52? You can get the version from SemanticsContext: context_.langOptions().OpenMPVersion.

+1 but we decided not to that in this patch because there's no version check for the other deprecation warnings. Would it be okay to fix those in a separate patch?

@raghavendhra raghavendhra self-requested a review June 3, 2025 19:02
@Stylie777 Stylie777 merged commit 9ba332f into llvm:main Jun 4, 2025
11 checks passed
@Stylie777
Copy link
Contributor Author

Thanks everyone for your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang:openmp flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants