llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Flang] Incorrect execution result of EXTENDS_TYPE_OF intrinsic function

Open ohno-fj opened this issue 1 year ago • 2 comments

Version of flang : 21.0.0(06cb7b1e14a117e8fe19b72689c8616c772c0807)/AArch64

As in the attached program, the two arguments supplied to EXTENDS_TYPE_OF intrinsic function appear to be different types defined in different modules.
According to Fortran Standard 2023: 16.9.86 EXTENDS_TYPE_OF (A, MOLD), the result is false if dynamic type of first argument (dmyptr) is not an extension type of the dynamic type of the second argument (baseobj).
Therefore, the execution result of Flang is probably incorrect.

The following are the test program, Flang, Gfortran and ifx compilation/execution result.

Polymorphism_2.f90:

MODULE MODULE3
  IMPLICIT NONE
  LOGICAL::x
  TYPE base
     integer ::int1
  END TYPE base
  TYPE ,EXTENDS(base)::deriv
     INTEGER :: int2
  END TYPE deriv
END MODULE MODULE3

MODULE MODULE4
  IMPLICIT NONE
  TYPE base
     INTEGER ::ii
  END TYPE base
  TYPE ,EXTENDS(base):: deriv
     INTEGER :: kk
  END TYPE deriv
END MODULE

PROGRAM MAIN
  IMPLICIT NONE
  INTERFACE
     SUBROUTINE sub2
     END SUBROUTINE sub2
  END INTERFACE
  call sub2()
END PROGRAM MAIN

SUBROUTINE sub1(dmyptr)
  use MODULE3
  CLASS(*)::dmyptr
  TYPE(deriv)::baseobj
  LOGICAL::res
  res=EXTENDS_TYPE_OF(dmyptr,baseobj)
  print*,'res = ', res
  if(res .eqv. .false.) then
     print*,'pass'
  else
     print*,'fail'
  endif
END SUBROUTINE sub1

SUBROUTINE sub2()
  use MODULE4
  INTERFACE
     SUBROUTINE sub1(dmy1)
       CLASS(*)::dmy1
     END SUBROUTINE sub1
  end interface
  CLASS(base),POINTER::ptr_base
  TYPE(deriv),TARGET::tar_ty1
  ptr_base=>tar_ty1
  call sub1(ptr_base)
END SUBROUTINE sub2
$ flang Polymorphism_2.f90; ./a.out
 res =  T
 fail
$
$ gfortran Polymorphism_2.f90; ./a.out
 res =  F
 pass
$
$ ifx Polymorphism_2.f90; ./a.out
 res =  F
 pass
$

ohno-fj avatar Apr 07 '25 02:04 ohno-fj

@ohno-fj this seems to be fixed. Could you please check?

ceseo avatar Jun 05 '25 17:06 ceseo

@ceseo thank you for your comment.

I confirmed with flang built on June 9 that the program works correctly.
I close this issue.
The log is attached below.

$  flang --version
flang version 21.0.0git (https://github.com/llvm/llvm-project.git 414590bae8cdd2a59979e5ae4cb297f6a1967ec5)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /work/groups/ssoft/compiler/llvm/aarch64/main-20250609-414590bae8cd/bin
Build config: +assertions
$
$  flang Polymorphism_2.f90; ./a.out
 res =  F
 pass
$

ohno-fj avatar Jun 09 '25 05:06 ohno-fj