`Implied do` and array constructor: strange behavior of `flang` and related compilers

(The code mentioned below is available at GitHub)

Below is a piece of code involving implied do and array constructor. Gfortran, Intel ifort and ifx, NAG nagfor, Absoft af95, and Oracle sunf95 can all handle it, but the Classic flang 7.1.0, flang in Bisheng Compiler 1.3.3.b023, flang in AOCC 3.2, and NVIDIA nvfortran 21.11 & 22.1 encounter errors, which will be detailed in the sequel.

! test.f90
module test_implied_do_mod
implicit none
private
public :: test_implied_do

contains

subroutine test_implied_do()
implicit none
integer :: i
integer :: nfilt
integer, parameter :: maxfilt = 2
logical :: better(maxfilt)
real :: cfilt(maxfilt)
real :: cstrv
real :: f
real :: ffilt(maxfilt)

ffilt = [2.0, 3.0]
cfilt = [3.0, 2.0]
f = 1.0
cstrv = 4.0

nfilt = maxfilt
better(1:nfilt) = [(isbetter([ffilt(i), cfilt(i)], [f, cstrv]), i=1, nfilt)]
if (.not. any(better(1:nfilt))) then
    print *, 'Right'
else
    print *, 'Wrong'
    stop 1
end if
end subroutine test_implied_do

function isbetter(fc1, fc2) result(is_better)
implicit none
real, intent(in) :: fc1(:)
real, intent(in) :: fc2(:)
logical :: is_better

write (*, *) fc1, fc2
is_better = all(fc1 < fc2)
end function isbetter

end module test_implied_do_mod


program test
use, non_intrinsic :: test_implied_do_mod, only : test_implied_do
implicit none

print *, 'Test: Implied do.'
call test_implied_do()
print *, 'Succeed: Implied do.'

end program test

The flang-based compilers encounter the following errors.

1. Classic flang on Ubuntu 20.04

When invoked without any option:

$ flang test.f90 && /a.out 
 Test: Implied do.
Segmentation error

When invoked with -Mbounds:

$ flang -Mbounds test.f90 && ./a.out 
 Test: Implied do.
0: Subscript out of range for array ffilt (test.f90: 25)
    subscript=19, lower bound=1, upper bound=2, dimension=1

2. Bisheng flang on Kunpeng platform with Ubuntu 18.04

When invoked without any option:

$flang test.f90 && ./a.out
 Test: Implied do.
Segmentation fault (core dumped)

When invoked with -Mbounds

$flang -Mbounds test.f90 && ./a.out
 Test: Implied do.
0: Subscript out of range for array ffilt (test.f90: 26)
    subscript=-1556092340, lower bound=1, upper bound=2, dimension=1

3. AOCC flang on Ubuntu 20.04

When invoked without any option:

$ flang test.f90 && ./a.out
 Test: Implied do.
    0.000000        0.000000        1.000000        4.000000    
    0.000000        0.000000        1.000000        4.000000    
 Wrong
    1

When invoked with -Mbounds

$ flang -Mbounds test.f90 && ./a.out
 Test: Implied do.
0: Subscript out of range for array ffilt (test.f90: 25)
    subscript=0, lower bound=1, upper bound=2, dimension=1

4. NVIDIA nvfortran on Ubuntu 20.04

When invoked without any options

$ nvfortran test.f90 && ./a.out 
 Test: Implied do.
    0.000000        3.000000        1.000000        4.000000    
    0.000000        3.000000        1.000000        4.000000    
 Wrong
    1

When invoked with -Mbounds:

$ nvfortran -Mbounds test.f90 && ./a.out
 Test: Implied do.
0: Subscript out of range for array ffilt (test.f90: 25)
    subscript=0, lower bound=1, upper bound=2, dimension=1

Is there anything wrong with the code of test.f90? Is it standard-conforming? Thank you

1 Like

The code looks conformant to the standard as best as I can tell. You should feel confident in posting support requests toward the 3 compiler implementations where you notice errors.

1 Like

Thank you @FortranFan for the reassuring comment.

For the Classic flang, I have opened an issue at the GitHub repository.
For NVIDIA nvfortran, I have posted the problem at the NVIDIA Developer Forums.
For AOCC flang, I have not found a way of submitting a bug report yet — it seems that they accept feedback only from those who have an “AMD SSO login”, which I do not have and did not manage to register. Any suggestion?

Thank you very much!

1 Like

Update (20211221):

  1. NVIDIA engineers have received the bug report.

  2. A bug report for AOCC flang is posted at the AMD Community. In an email exchange on 28 Dec 2021, the technical support of AMD confirms that their developers will review this issue.

It is quite interesting to observe how the vendors handle bug reports. I have reported several bugs to Absoft Fortran in the past year. Their responses were impressively prompt. All reports got replies within two days, and most bugs were fixed within a week. NAG’s responses to bug reports are satisfactory although not as commendable as Absoft. They first send you an automated response claiming that your report is received, but even such an “automated” response takes one or two days to arrive. After receiving it, you can hope for a response from an engineer within a week. I have limited experience with Intel concerning bug reports, but a false positive of ifort -check shape that was reported to Intel in September 2021 have not been fixed despite several updates of the compiler.

2 Likes

Update (20220104):

The bug also affects the Bisheng Compiler (based on the Classic flang) running on the ARM-based Kunpeng Platform of Huawei. A bug report has been submitted to their engineers and the developers’ forum.

Who will fix the bug first? Huawei, NVIDIA, or AMD? Let us see …

1 Like

Thank you for reporting this. It appears fixed in development versions of AOCC.