Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28435 Discussions

Compiler bug: bad guess of allocated array status

vmironov
Beginner
1,302 Views

I've played with modern Fortran features and found a bug in classic Intel Fortran compiler.

It appears at the optimization level  -O2 and higher.

IFX (2022.1.0) and GFortran (10.3.1) don't have this bug.

Below is the test code. The compiler skips check for allocation status at line 22 if allocated array is passed to the subroutine. It ignores the fact that it can be deallocated at lines 14-16.

 

module testrealloc
    implicit none
    logical, parameter :: debug = .true.
contains

subroutine my_realloc(a, a_shape)
    real(kind=8), allocatable :: a(..)
    integer :: a_shape(:)

    if (allocated(a)) then
        if ( any((shape(a) - a_shape)/=0) ) then
            if (debug) write(*,'(A,I3)') 'deallocating array of rank ', rank(a)
            select rank(a)
                rank(1) ; deallocate(a)
                rank(2) ; deallocate(a)
                rank(3) ; deallocate(a)
            rank default
            end select
        end if
    end if

    if (.not.allocated(a)) then
        if (debug) write(*,'(A,I3,A,*(G0,: ", "))') 'allocating array of rank ', rank(a), ' and shape: ', a_shape(1:rank(a))
        select rank(a)
            rank(1) ; allocate(a(a_shape(1)), source=0.0d0)
            rank(2) ; allocate(a(a_shape(1), a_shape(2)), source=0.0d0)
            rank(3) ; allocate(a(a_shape(1), a_shape(2), a_shape(3)), source=0.0d0)
        rank default
        end select
    else
        if (debug) write(*,'(A,I3,A,*(G0,: ", "))') 'zeroing array of rank ', rank(a), ' and shape: ', shape(a)
        select rank(a)
            rank(1) ; a = 0
            rank(2) ; a = 0
            rank(3) ; a = 0
        rank default
        end select
    end if

end subroutine

end module


program test
   use testrealloc
   real(8), allocatable :: a(:,:)
   allocate(a(1,1))
   call my_realloc(a, [5,5])
   write(*,'(*(A,G0", "))') 'allocated=', allocated(a), 'shape=',shape(a)
   write(*,*) 'sum(a)=', sum(a)
end program

 

 

The above code runs well at -O1 optimization level...

 

$ ifort testbug.f90 -O1
$ ./a.out
deallocating array of rank   2
allocating array of rank   2 and shape: 5, 5
allocated=T, shape=5,
 sum(a)=  0.000000000000000E+000

 

...and fails at -O2:

 

$ ifort testbug.f90 -O2
$ ./a.out
deallocating array of rank   2
zeroing array of rank   2 and shape: 1, 1
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image              PC                Routine            Line        Source
a.out              00000000004069AA  Unknown               Unknown  Unknown
libpthread-2.28.s  00007F03CB582CE0  Unknown               Unknown  Unknown
a.out              0000000000404C2A  Unknown               Unknown  Unknown
a.out              00000000004039D8  Unknown               Unknown  Unknown
a.out              0000000000403862  Unknown               Unknown  Unknown
libc-2.28.so       00007F03CB1E4CA3  __libc_start_main     Unknown  Unknown
a.out              000000000040376E  Unknown               Unknown  Unknown

 

 

Compiler version:

 

$ ifort --version
ifort (IFORT) 2021.6.0 20220226
Copyright (C) 1985-2022 Intel Corporation.  All rights reserved.

 

Labels (1)
0 Kudos
1 Solution
Shiquan_Su
Employee
1,250 Views

Dear User:

Thank you for reporting this bug in Fortran compiler. I performed the initial test and reproduced your bug with the ifort (IFORT) 2021.6.0 20220226 version in the latest oneapi release . I passed this issue to our backend engineer. Meanwhile, I observe a work around that the ifx in the same oneapi release doesn't show this bug. So you may try to work around by replacing ifort with ifx. Can this help you move forward with you work? Thank you. The following is my test:


$ ifx testbug.f90 -O1

$ ./a.out

deallocating array of rank  2

allocating array of rank  2 and shape: 5, 5

allocated=T, shape=5,

 sum(a)= 0.000000000000000E+000

$ ifx testbug.f90 -O2

$ ./a.out

deallocating array of rank  2

allocating array of rank  2 and shape: 5, 5

allocated=T, shape=5,

 sum(a)= 0.000000000000000E+000



View solution in original post

0 Kudos
3 Replies
Shiquan_Su
Employee
1,251 Views

Dear User:

Thank you for reporting this bug in Fortran compiler. I performed the initial test and reproduced your bug with the ifort (IFORT) 2021.6.0 20220226 version in the latest oneapi release . I passed this issue to our backend engineer. Meanwhile, I observe a work around that the ifx in the same oneapi release doesn't show this bug. So you may try to work around by replacing ifort with ifx. Can this help you move forward with you work? Thank you. The following is my test:


$ ifx testbug.f90 -O1

$ ./a.out

deallocating array of rank  2

allocating array of rank  2 and shape: 5, 5

allocated=T, shape=5,

 sum(a)= 0.000000000000000E+000

$ ifx testbug.f90 -O2

$ ./a.out

deallocating array of rank  2

allocating array of rank  2 and shape: 5, 5

allocated=T, shape=5,

 sum(a)= 0.000000000000000E+000



0 Kudos
Shiquan_Su
Employee
1,192 Views

Hi,

We have performed more testing. We found that in the oneapi/2021.3.0, the code can run successfully, then the problem shows up at any version starting at oneapi/2021.4.0. All the updates have been passed to the developer, and the developer is working on them.


$:~/osc_05502564$ ifort --version

ifort (IFORT) 2021.3.0 20210609

Copyright (C) 1985-2021 Intel Corporation. All rights reserved.


$:~/osc_05502564$ ifort testbug.f90 -O2

$:~/osc_05502564$ ./a.out

deallocating array of rank  2

allocating array of rank  2 and shape: 5, 5

allocated=T, shape=5,

 sum(a)= 0.000000000000000E+000



0 Kudos
Shiquan_Su
Employee
1,072 Views

Hi,

Our engineer pointed out that:  use memcpy command when we want to copy descriptor information in another descriptor.


0 Kudos
Reply