llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Flang][OpenMP] Missing diagnostic for INTENT(IN) POINTER in LASTPRIVATE clause

Open Saieiei opened this issue 10 months ago • 2 comments

What's happening?

Flang currently accepts code that uses an INTENT(IN) pointer in a LASTPRIVATE clause without any error. However, this should be rejected according to the OpenMP specification - and both gfortran and Cray compilers correctly flag this as an error.

Reproducer

https://godbolt.org/z/rah1d7zbd

subroutine test(p)
  implicit none
  integer, pointer, intent(in) :: p
  integer :: i

  !$omp parallel do lastprivate(p)
  do i = 1, 10
  end do
  !$omp end parallel do
end subroutine

Compile with:

flang-new -fopenmp -fsyntax-only test.f90

What I expected

An error like:

error: Pointer 'p' with the INTENT(IN) attribute may not appear in a LASTPRIVATE clause

What actually happens

Flang compiles successfully with no errors (apart from the experimental OpenMP warning).

Comparison with other compilers

gfortran 15.2 (tested on https://godbolt.org with -fopenmp):

Error: INTENT(IN) POINTER 'p' in LASTPRIVATE clause at (1)

Cray Fortran:

ftn-1492 ftn: ERROR
  Dummy argument "P" has the intent(in) attribute. It must not be declared 
  in the LASTPRIVATE clause because it is not definable.

Why this matters

According to OpenMP 5.2, Section 5.4.5:

  • A variable in a lastprivate clause must be definable (Fortran-specific restriction)
  • For pointers, the update happens "as if by pointer assignment"

An INTENT(IN) pointer can't have its association changed, so it's not definable for pointer assignment purposes.

Root cause (I think)

Looking at flang/lib/Semantics/check-omp-structure.cpp, the LASTPRIVATE clause handler doesn't check for INTENT(IN) pointers, but similar clauses like PRIVATE, COPYPRIVATE, and REDUCTION do have this check.

Happy to submit a PR if this analysis looks correct!

Saieiei avatar Jan 28 '26 11:01 Saieiei

@llvm/issue-subscribers-flang-frontend

Author: Sairudra More (Saieiei)

## What's happening?

Flang currently accepts code that uses an INTENT(IN) pointer in a LASTPRIVATE clause without any error. However, this should be rejected according to the OpenMP specification - and both gfortran and Cray compilers correctly flag this as an error.

Reproducer

subroutine test(p)
  implicit none
  integer, pointer, intent(in) :: p
  integer :: i

  !$omp parallel do lastprivate(p)
  do i = 1, 10
  end do
  !$omp end parallel do
end subroutine

Compile with:

flang-new -fopenmp -fsyntax-only test.f90

What I expected

An error like:

error: Pointer 'p' with the INTENT(IN) attribute may not appear in a LASTPRIVATE clause

What actually happens

Flang compiles successfully with no errors (apart from the experimental OpenMP warning).

Comparison with other compilers

gfortran 15.2 (tested on https://godbolt.org with -fopenmp):

Error: INTENT(IN) POINTER 'p' in LASTPRIVATE clause at (1)

Cray Fortran:

ftn-1492 ftn: ERROR
  Dummy argument "P" has the intent(in) attribute. It must not be declared 
  in the LASTPRIVATE clause because it is not definable.

Why this matters

According to OpenMP 5.2, Section 5.4.5:

  • A variable in a lastprivate clause must be definable (Fortran-specific restriction)
  • For pointers, the update happens "as if by pointer assignment"

An INTENT(IN) pointer can't have its association changed, so it's not definable for pointer assignment purposes.

Root cause (I think)

Looking at flang/lib/Semantics/check-omp-structure.cpp, the LASTPRIVATE clause handler doesn't check for INTENT(IN) pointers, but similar clauses like PRIVATE, COPYPRIVATE, and REDUCTION do have this check.

Happy to submit a PR if this analysis looks correct!

llvmbot avatar Jan 28 '26 15:01 llvmbot

Tagging @kiranchandramohan

This analysis looks reasonable to me.

eugeneepshteyn avatar Jan 28 '26 20:01 eugeneepshteyn