Skip to content
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

Implied do and array constructor: a bug of flang and related compilers #1200

Closed
zaikunzhang opened this issue Dec 19, 2021 · 7 comments
Closed
Assignees

Comments

@zaikunzhang
Copy link

zaikunzhang commented Dec 19, 2021

(The code reflecting the issue is available at GitHub. The issue has been confirmed to be a bug and has been reported to Huawei, NVIDIA, and AMD. See also discussions at Fortran Discourse)

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 on Ubuntu 20.04, flang in Bisheng Compiler 1.3.3.b023 on Ubuntu 18.04, flang in AOCC 3.2 on Ubuntu20.04, and NVIDIA nvfortran 21.11 on Ubuntu 20.04 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

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

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

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

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
@zaikunzhang zaikunzhang changed the title Implied do and array constructor: strange behavior of flang and related compilers Implied do and array constructor: a bug of flang and related compilers Dec 24, 2021
@zaikunzhang
Copy link
Author

Thank @kiranchandramohan , @bryanpkc , @shivaramaarao , and @mleair for taking care of this issue. I look forward to seeing it fixed.

@bryanpkc
Copy link
Collaborator

We have a fix in the works.

@zaikunzhang
Copy link
Author

Thank you, @bryanpkc ! I am very happy to hear about that, and look forward to the fix.

@zhaochuanfeng
Copy link
Contributor

I've submitted a PR #1212 to fix this issue, please help to review it, thanks~

@zaikunzhang
Copy link
Author

Thank you @zhaochuanfeng so much for working on this issue. I much appreciate the efforts of the community and look forward to seeing the bug finally fixed.

@kiranchandramohan
Copy link
Collaborator

Could you check whether this is fixed by #1212 or top of trunk? If fixed please close this and 1227.

@zaikunzhang
Copy link
Author

Hello @kiranchandramohan ! Thank you and others for the huge efforts! I made a more detailed reply in #1227.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants