[flang-commits] [flang] [flang] Rely on global initialization for simpler derived types (PR #114002)

via flang-commits flang-commits at lists.llvm.org
Mon Oct 28 21:53:12 PDT 2024


NimishMishra wrote:

For the following test case, here are the runtimes with/without this patch: `flang -O3 <filename>.f90 -o out'. We have also tested out this patch with gfortran testsuite, and it is clean.

**Without patch**:
T1 =  0.690 seconds.
**T2 =  9.241 seconds.**

**With patch**:
T1 =  0.690 seconds.
**T2 =  0.695 seconds.**

About 9 seconds of improvement in runtime.

```
program main

  use iso_fortran_env
  implicit none
  integer(kind=int32) :: i32_
  integer, parameter :: c = kind(i32_)

  type :: t
    integer(c), dimension(:, :), allocatable :: n
  end type t

  type :: cm
    type(t) :: topo
  end type cm

  type :: cl
    type(cm), pointer :: m
    integer(c) :: index_p
  end type cl

  type :: fl
    type(cm), pointer :: m
    integer(c) :: index_p
    integer(c) :: cfc
  end type fl

  type :: nl
    type(cm), pointer :: m
    integer(c) :: index_p
    integer(c) :: nc
  end type nl

  type(fl) :: loc_f
  logical :: ib
  integer :: M, N, i, j
  real :: start, finish
  M = 10000
  N = 10000
  allocate (loc_f%m)
  allocate (loc_f%m%topo%n(M,N))
  do i = 1,M
    do j = 1,N
      loc_f%m%topo%n(i,j) = j+2
    end do
  end do
  call cpu_time(start)
  do i = 1,M
    do j = 1,N
      loc_f%cfc = i
      loc_f%index_p = j
      call foo(loc_f, ib)
      if (ib) then
          write (*,*) i,j
      end if
    end do
  end do
  call cpu_time(finish)
  print '("T1 = ",f6.3," seconds.")',finish-start
  call cpu_time(start)
  do i = 1,M
    do j = 1,N
      loc_f%cfc = i
      loc_f%index_p = j
      call bar(loc_f, ib)
      if (ib) then
          write (*,*) i,j
      end if
    end do
  end do
  call cpu_time(finish)
  print '("T2 = ",f6.3," seconds.")',finish-start
  contains
    subroutine foo(loc_f, ib)
      type(fl), intent(in) :: loc_f
      logical, intent(out) :: ib
      associate (m => loc_f%m, &
                 i => loc_f%index_p, &
                 j => loc_f%cfc)
        ib = m%topo%n(j, i) < 0
      end associate
    end subroutine

 

    subroutine bar(loc_f, ib)
      type(fl), intent(in) :: loc_f
      logical, intent(out) :: ib
      type(cl) :: loc_p

      type(nl) :: loc_nb
      loc_p%m => loc_f%m
      loc_p%index_p = loc_f%index_p
      loc_nb%m => loc_p%m
      loc_nb%index_p = loc_p%index_p
      loc_nb%nc = loc_f%cfc
      ib = loc_nb%m%topo%n(loc_nb%nc, loc_nb%index_p) < 0
    end subroutine
end program main
```



https://github.com/llvm/llvm-project/pull/114002


More information about the flang-commits mailing list