[PATCH] D130513: [Flang] Add -fconvert option to swap endianness for unformatted files
Peixin Qiao via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 26 20:24:41 PDT 2022
peixin added a comment.
$ cat fconvert_option_main_2.f90
!
! Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
! See https://llvm.org/LICENSE.txt for license information.
! SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
!
! checking for I/O: testing read with -fconvert=big-endian.
! The convert argument of open function has prior claim over fconvert option.
! Only main program intentionally compiled with -fconvert=big-endian.
program fconvert_option_main_2
use fconvert_option_openfile
use fconvert_option_readfile
integer, parameter :: n = 4
integer :: del_flag = 0 ! 1 for deleting data file after read
real :: arr(n) = [1.0, 2.0, 3.0, 4.0]
character(:), allocatable :: filename
integer :: arglen
call get_command_argument(1, length = arglen)
allocate(character(arglen) :: filename)
call get_command_argument(1, value = filename)
call open_for_read_LE(10, filename)
call readfile(10, del_flag, arr, n, 0)
call open_for_read_native(11, filename)
call readfile(11, del_flag, arr, n, 4)
print *, 'PASS'
end
$ cat fconvert_option_readfile.f90
!
! Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
! See https://llvm.org/LICENSE.txt for license information.
! SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
!
! checking for I/O: readfile subroutine
module fconvert_option_readfile
contains
subroutine readfile(fid, del_flag, expect, n, t)
integer :: n, t
integer :: fid, del_flag
real :: expect(n)
real :: buf(n)
read (fid) buf
if (del_flag .eq. 0) then
close (fid)
else
close (fid, status='delete')
end if
do i = 1, n
if (buf(i) .ne. expect(i)) stop (i+t)
enddo
end
end
$ cat fconvert_option_openfile.f90
!
! Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
! See https://llvm.org/LICENSE.txt for license information.
! SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
!
! checking for I/O: openfile subroutines
module fconvert_option_openfile
contains
subroutine open_for_read(fid, file_name)
integer :: fid
character(:), allocatable :: file_name
open (fid, file=file_name, form='UNFORMATTED', status='old')
end
subroutine open_for_read_BE(fid, file_name)
integer :: fid
character(:), allocatable :: file_name
open (fid, file=file_name, form='UNFORMATTED', status='old', convert="BIG_ENDIAN")
end
subroutine open_for_read_LE(fid, file_name)
integer :: fid
character(:), allocatable :: file_name
open (fid, file=file_name, form='UNFORMATTED', status='old', convert="LITTLE_ENDIAN")
end
subroutine open_for_read_native(fid, file_name)
integer :: fid
character(:), allocatable :: file_name
open (fid, file=file_name, form='UNFORMATTED', status='old', convert="NATIVE")
end
subroutine open_for_write(fid, file_name)
integer :: fid
character(:), allocatable :: file_name
open (fid, file=file_name, form='UNFORMATTED', status='new')
end
subroutine open_for_write_BE(fid, file_name)
integer :: fid
character(:), allocatable :: file_name
open (fid, file=file_name, form='UNFORMATTED', status='new', convert="BIG_ENDIAN")
end
subroutine open_for_write_LE(fid, file_name)
integer :: fid
character(:), allocatable :: file_name
open (fid, file=file_name, form='UNFORMATTED', status='new', convert="LITTLE_ENDIAN")
end
subroutine open_for_write_native(fid, file_name)
integer :: fid
character(:), allocatable :: file_name
open (fid, file=file_name, form='UNFORMATTED', status='new', convert="NATIVE")
end
end
$ cat fconvert_option_writefile.f90
!
! Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
! See https://llvm.org/LICENSE.txt for license information.
! SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
!
! checking for I/O: writefile subroutine
module fconvert_option_writefile
contains
subroutine writefile(fid, buf, n)
integer :: n
real :: buf(n)
integer :: fid
write (fid) buf
close (fid)
end
end
$ cat run.sh
#!/bin/bash
echo "---------- gfortran ----------"
gfortran fconvert_option_readfile.f90 -c
gfortran fconvert_option_openfile.f90 -c
gfortran fconvert_option_writefile.f90 -c
gfortran $1.f90 $2 -c
gfortran $1.o fconvert_option_readfile.o fconvert_option_openfile.o fconvert_option_writefile.o
./a.out Inputs/fc_opt_data_$3 && rm *.o *.mod a.out
echo && echo "---------- flang-new ----------"
flang-new fconvert_option_readfile.f90 -c
flang-new fconvert_option_openfile.f90 -c
flang-new fconvert_option_writefile.f90 -c
flang-new $1.f90 $2 -c
flang-new -flang-experimental-exec $1.o fconvert_option_readfile.o fconvert_option_openfile.o fconvert_option_writefile.o
./a.out Inputs/fc_opt_data_$3 && rm *.o *.mod a.out
$ bash run.sh fconvert_option_main_2 -fconvert=big-endian little
---------- gfortran ----------
PASS
---------- flang-new ----------
fatal Fortran runtime error(./fconvert_option_readfile.f90:17): Unformatted variable-length sequential file input failed at record #1 (file offset 0): hit EOF reading record with length 268435456 bytes
run.sh: line 17: 126614 Aborted (core dumped) ./a.out Inputs/fc_opt_data_$3
Not sure if this fail is caused by this patch or runtime IO functions. I will add our other internal tests to https://github.com/llvm/llvm-project/issues/55961 and you can also test those cases. The data is an array of "1.0 2.0 3.0 4.0" through little-endian or big-endian. You can create it by yourself.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D130513/new/
https://reviews.llvm.org/D130513
More information about the cfe-commits
mailing list