flang-compiler / flang

Flang is a Fortran language front-end designed for integration with LLVM.
Other
803 stars 137 forks source link

[Performance issue] Dead code don't remove in flang front end #1230

Open vfdff opened 2 years ago

vfdff commented 2 years ago

Base the following code, all the pointer malloced are not used outside the function, so all of them are dead code. We'll see that gfortran will delete the dead code the the very early tree-dump, and only leave 3 movl to retrun the arguments(reference to https://godbolt.org/z/3dMc1n1x1). while flang doen't do similar optimization.

subroutine test(ij, l, k, N)
    integer :: ij, k, l, N
    integer, parameter :: ADM_gall = 1156
    integer, parameter :: ADM_kall = 42
    integer, parameter :: ADM_lall = 2
    real(8), dimension(N,N,N) :: rhog, rhogvx, rhogvy, rhogvz, rhogw, rhoge, rhog_split
    real(8), dimension(N,N,N) :: rhogvx_split, rhogvy_split, rhogvz_split, rhogw_split, rhoge_split 
    !---------------------------------------------------------------------------

    !--- update prognostic variables
    do l = 1, ADM_lall
       do k  = 1, ADM_kall
       do ij = 1, ADM_gall
             rhog  (ij,k,l) = rhog  (ij,k,l) + rhog_split  (ij,k,l)
             rhogvx(ij,k,l) = rhogvx(ij,k,l) + rhogvx_split(ij,k,l)
             rhogvy(ij,k,l) = rhogvy(ij,k,l) + rhogvy_split(ij,k,l)
             rhogvz(ij,k,l) = rhogvz(ij,k,l) + rhogvz_split(ij,k,l)
             rhogw (ij,k,l) = rhogw (ij,k,l) + rhogw_split (ij,k,l)
             rhoge (ij,k,l) = rhoge (ij,k,l) + rhoge_split (ij,k,l)
       enddo
       enddo
    enddo
 end
vfdff commented 2 years ago

while gfortran doesn't delete the initializtion in following smart case . https://godbolt.org/z/oenTK4rMn

subroutine t(v, row, col)
  integer :: row
  integer :: col
  integer(8) :: v (row, col)

  v = 2 // initializtion all elements 
end