[llvm] [flang][runtime] Allow some list-directed child output to advance (PR #166847)

Daniel Chen via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 10 09:26:42 PST 2025


DanielCChen wrote:

```
module m

   type :: data
      integer :: i, j
   end type

   type :: base
      type(data) :: d1
   end type

   type, extends(base) :: child
      type(data) :: d2
   end type

   interface write(formatted)
      subroutine writeformatted(dtv, unit, iotype, v_list, iostat, iomsg )
         import base
         class(base), intent(in) :: dtv
         integer,  intent(in) :: unit
         character(*), intent(in) :: iotype
         integer, intent(in)     :: v_list(:)
         integer,  intent(out) :: iostat
         character(*),  intent(inout) :: iomsg
      end subroutine
  end interface

   class(base), pointer :: b2(:,:)
   namelist /nml1/ b2

end module

program array001b
   use m

   integer :: stat
   character(200) :: msg = ''
   class(base), allocatable :: b1(:)

   allocate(b1(2), source = (/ child(data(1001,1002), data(1003,1004)), child(data(1005,1006), data(1007,1008)) /))
   allocate(b2(2,2), source = reshape ( source = (/ b1, b1 /) , shape = (/ 2, 2 /) )  )

   write (6,NML=nml1, iostat=stat, iomsg=msg)
   if ( stat /=  0 ) ERROR STOP(1_4)

end program

subroutine writeformatted (dtv, unit, iotype, v_list, iostat, iomsg)
   use m, only: base, child

   class(base), intent(in) :: dtv
   integer, intent(in) :: unit
   character(*), intent(in) :: iotype
   integer, intent(in)     :: v_list(:)
   integer, intent(out) :: iostat
   character(*), intent(inout) :: iomsg

   if ( iotype /= "NAMELIST" ) ERROR STOP(3_4)
   if ( size(v_list, 1) /= 0 ) ERROR STOP(4_4)

   select type ( m => dtv )
      class is (base)
         write (unit, *, iostat=iostat )      m%d1
      type is (child)
         write (unit, *, iostat=iostat )      m%d1
         write (unit, *, iostat=iostat )      m%d2
   end select

   iomsg = 'dtiowrite'

end subroutine
```

For this code, XLF has an compile time option to use the dynamic type of `b1` when it is used as the `source=` to allocate `b2`. The output then is
```
> a.out
 &NML1 B2=1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008/
 ```
 instead of 
 ```
 > a.out
 &NML1 B2=1001, 1002, 1005, 1006, 1001, 1002, 1005, 1006/
 ```
 
 This is not related to DTIO rather an extension to the array constructor.

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


More information about the llvm-commits mailing list