This series fixes three defects in libgfortran's asynchronous I/O and
stops scalarizing numeric and logical pointer arrays in I/O lists. I
found them while using asynchronous unformatted I/O for the output and
restart files of the FESOM2 ocean model
(https://github.com/FESOM/fesom2).
1/4 PR128719: WAIT with ID= returned when the operation before the
requested one had completed; WAIT on the first ID of a unit did
not wait.
2/4 PR128720: A derived type with defined I/O in an asynchronous
statement crashed. Such statements are now completed
synchronously.
3/4 PR128721: After an error, skipped queue items leaked their
parameter blocks and descriptors, and io_lock was unlocked once
too often.
4/4 PR128722: Pointer arrays were transferred element by element:
one library call each, and one queue item each when
asynchronous. The library now uses the descriptor span, and
numeric and logical pointer arrays are passed by descriptor.
This needs two new library entry points (GFORTRAN_17), so that
such programs do not run with an older libgfortran that ignores
the span.
The tests of 1/4 and 2/4 fail without the fixes (the second one
crashes); the test of 3/4 shows the leaks under -fsanitize=leak. With
4/4, writing a 5 million element pointer array asynchronously takes
0.014 s instead of 1.85 s.
LeakSanitizer still reports a few queue items at program exit, as
described in PR libfortran/100371; this series does not change that.
Bootstrapped and regression tested on x86_64-pc-linux-gnu.
OK for trunk?
From 21692726cdc449c39c8c045fde38f62ccbefca5b Mon Sep 17 00:00:00 2001
From: Suvarchal Kumar Cheedela <[email protected]>
Date: Sat, 14 Nov 2026 10:55:25 +0100
Subject: [PATCH 1/4] libgfortran: Fix WAIT with ID= returning too early
[PR128719]
au->id.low counts the completed operations of an asynchronous unit, so
the operation with ID i is complete once id.low > i. async_wait_id
waited for id.low >= i and thus returned as soon as the operation
before the requested one had finished; WAIT on the first ID of a unit
did not wait at all.
A pending id.done signal left over from an earlier operation could also
end the wait before the condition held, so clear it first. IDs equal
to id.high have never been handed out and are now rejected like larger
ones.
libgfortran/ChangeLog:
PR libfortran/128719
* io/async.c (async_wait_id): Wait until id.low exceeds the
requested ID. Clear a stale id.done signal before waiting.
Reject IDs that have not been assigned.
libgomp/ChangeLog:
PR libfortran/128719
* testsuite/libgomp.fortran/async_io_10.f90: New test.
Signed-off-by: Suvarchal Kumar Cheedela <[email protected]>
---
libgfortran/io/async.c | 5 ++--
.../testsuite/libgomp.fortran/async_io_10.f90 | 25 +++++++++++++++++++
2 files changed, 28 insertions(+), 2 deletions(-)
create mode 100644 libgomp/testsuite/libgomp.fortran/async_io_10.f90
diff --git a/libgfortran/io/async.c b/libgfortran/io/async.c
index b54cc76fa87..d3aa9ce74b8 100644
--- a/libgfortran/io/async.c
+++ b/libgfortran/io/async.c
@@ -426,7 +426,7 @@ async_wait_id (st_parameter_common *cmp, async_unit *au, int i)
}
LOCK (&au->lock);
- if (i > au->id.high)
+ if (i < 0 || i >= au->id.high)
{
generate_error_common (cmp, LIBERROR_BAD_WAIT_ID, NULL);
UNLOCK (&au->lock);
@@ -437,8 +437,9 @@ async_wait_id (st_parameter_common *cmp, async_unit *au, int i)
if (au->id.waiting < i)
au->id.waiting = i;
SIGNAL (&(au->work));
+ REVOKE_SIGNAL (&(au->id.done));
WAIT_SIGNAL_MUTEX (&(au->id.done),
- (au->id.low >= au->id.waiting || au->empty), &au->lock);
+ (au->id.low > au->id.waiting || au->empty), &au->lock);
LOCK (&au->lock);
ret = collect_async_errors (cmp, au);
UNLOCK (&au->lock);
diff --git a/libgomp/testsuite/libgomp.fortran/async_io_10.f90 b/libgomp/testsuite/libgomp.fortran/async_io_10.f90
new file mode 100644
index 00000000000..839e0360c60
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/async_io_10.f90
@@ -0,0 +1,25 @@
+! { dg-do run }
+! PR libfortran/128719 - WAIT with ID= returned before the statement completed.
+program main
+ implicit none
+ integer, parameter :: n = 500000
+ real(8), allocatable, asynchronous :: a(:)
+ integer :: id1, id2, i
+ integer(8) :: sz
+ logical :: pending
+ allocate (a(n))
+ a = [(1.0d0 + i, i = 1, n)]
+ open (10, file='async_io_10.dat', asynchronous='yes', status='replace')
+ write (10, '(es24.16)', asynchronous='yes', id=id1) a
+ write (10, '(es24.16)', asynchronous='yes', id=id2) a(1:10)
+ if (id1 == id2) stop 1
+ wait (10, id=id1)
+ inquire (file='async_io_10.dat', size=sz)
+ if (sz < int (n, 8) * 25 - 8192) stop 2
+ inquire (10, id=id1, pending=pending)
+ if (pending) stop 3
+ wait (10, id=id2)
+ inquire (10, id=id2, pending=pending)
+ if (pending) stop 4
+ close (10, status='delete')
+end program main
--
2.51.0
From 34ed54b17d35f3feb36ad191ea8f13e33a91c74f Mon Sep 17 00:00:00 2001
From: Suvarchal Kumar Cheedela <[email protected]>
Date: Sat, 14 Nov 2026 10:55:25 +0100
Subject: [PATCH 2/4] libgfortran: Fix defined I/O in asynchronous data
transfer statements [PR128720]
A derived type with defined I/O (DTIO) in a data transfer statement
with ASYNCHRONOUS='YES' crashed. transfer_derived stores the DTIO
procedure in the caller's parameter block, which the I/O thread never
sees, as it works on its own copy. Running the DTIO procedure on the
I/O thread would not work either: its child data transfer statements
need the unit lock, which is held by the parent statement or by a WAIT
or CLOSE of the main thread.
Complete such statements synchronously instead, after any pending
operations, as the standard permits. A statement with ID= still gets
an ID, which is immediately complete. st_read_done now runs the
completion worker for synchronous statements on asynchronous units, as
st_write_done already did. transfer_derived reports an error rather
than crashing should a DTIO item reach an asynchronous statement.
libgfortran/ChangeLog:
PR libfortran/128720
* io/async.c (async_complete_id): New function.
* io/async.h (async_complete_id): Declare.
* io/transfer.c (transfer_derived): Report an error for DTIO
items in asynchronous statements.
(data_transfer_init): Perform statements with DTIO items
synchronously.
(st_read_done): Run st_read_done_worker for synchronous
statements on asynchronous units. Assign an ID to statements
completed synchronously.
(st_write_done): Assign an ID to statements completed
synchronously.
libgomp/ChangeLog:
PR libfortran/128720
* testsuite/libgomp.fortran/async_io_11.f90: New test.
Signed-off-by: Suvarchal Kumar Cheedela <[email protected]>
---
libgfortran/io/async.c | 22 +++++++
libgfortran/io/async.h | 3 +
libgfortran/io/transfer.c | 38 ++++++++----
.../testsuite/libgomp.fortran/async_io_11.f90 | 61 +++++++++++++++++++
4 files changed, 111 insertions(+), 13 deletions(-)
create mode 100644 libgomp/testsuite/libgomp.fortran/async_io_11.f90
diff --git a/libgfortran/io/async.c b/libgfortran/io/async.c
index d3aa9ce74b8..9ab7bebd3fa 100644
--- a/libgfortran/io/async.c
+++ b/libgfortran/io/async.c
@@ -446,6 +446,20 @@ async_wait_id (st_parameter_common *cmp, async_unit *au, int i)
return ret;
}
+/* Assign an ID to a statement on an asynchronous unit that was
+ completed synchronously. */
+
+int
+async_complete_id (async_unit *au)
+{
+ int ret;
+ LOCK (&au->lock);
+ ret = au->id.high++;
+ au->id.low++;
+ UNLOCK (&au->lock);
+ return ret;
+}
+
/* Perform a wait operation an an asynchronous unit without an ID. */
bool
@@ -557,6 +571,14 @@ async_wait_id (st_parameter_common *cmp, async_unit *au, int i)
/* Do-nothing function, which will not be called. */
+int
+async_complete_id (async_unit *au)
+{
+ return 0;
+}
+
+/* Do-nothing function, which will not be called. */
+
bool
async_wait (st_parameter_common *cmp, async_unit *au)
{
diff --git a/libgfortran/io/async.h b/libgfortran/io/async.h
index 596b6d6f5f7..e0214b71548 100644
--- a/libgfortran/io/async.h
+++ b/libgfortran/io/async.h
@@ -638,6 +638,9 @@ internal_proto (async_wait);
bool async_wait_id (st_parameter_common *, async_unit *, int);
internal_proto (async_wait_id);
+int async_complete_id (async_unit *);
+internal_proto (async_complete_id);
+
bool collect_async_errors (st_parameter_common *, async_unit *);
internal_proto (collect_async_errors);
diff --git a/libgfortran/io/transfer.c b/libgfortran/io/transfer.c
index 618c7ef8b60..f8097c30409 100644
--- a/libgfortran/io/transfer.c
+++ b/libgfortran/io/transfer.c
@@ -2976,6 +2976,14 @@ transfer_array_write (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
void
transfer_derived (st_parameter_dt *parent, void *dtio_source, void *dtio_proc)
{
+ if (parent->u.p.current_unit && parent->u.p.current_unit->au
+ && parent->u.p.async)
+ {
+ generate_error (&parent->common, LIBERROR_OPTION_CONFLICT,
+ "Derived type with defined I/O in asynchronous "
+ "data transfer statement");
+ return;
+ }
if (parent->u.p.current_unit)
{
if (parent->u.p.current_unit->flags.form == FORM_UNFORMATTED)
@@ -3296,6 +3304,8 @@ data_transfer_init (st_parameter_dt *dtp, int read_flag)
}
au = dtp->u.p.current_unit->au;
+ if (au && dtp->u.p.async && (cf & IOPARM_DT_HAS_UDTIO))
+ dtp->u.p.async = 0;
if (au)
{
if (dtp->u.p.async)
@@ -4727,19 +4737,21 @@ st_read_done (st_parameter_dt *dtp)
{
if (dtp->u.p.current_unit)
{
- if (dtp->u.p.current_unit->au)
+ if (dtp->u.p.current_unit->au && dtp->u.p.async)
{
if (dtp->common.flags & IOPARM_DT_HAS_ID)
*dtp->id = enqueue_done_id (dtp->u.p.current_unit->au, AIO_READ_DONE);
else
- {
- if (dtp->u.p.async)
- enqueue_done (dtp->u.p.current_unit->au, AIO_READ_DONE);
- }
+ enqueue_done (dtp->u.p.current_unit->au, AIO_READ_DONE);
unlock_unit (dtp->u.p.current_unit);
}
else
- st_read_done_worker (dtp, true); /* Calls unlock_unit. */
+ {
+ if (dtp->u.p.current_unit->au
+ && (dtp->common.flags & IOPARM_DT_HAS_ID))
+ *dtp->id = async_complete_id (dtp->u.p.current_unit->au);
+ st_read_done_worker (dtp, true); /* Calls unlock_unit. */
+ }
}
library_end ();
@@ -4832,16 +4844,16 @@ st_write_done (st_parameter_dt *dtp)
*dtp->id = enqueue_done_id (dtp->u.p.current_unit->au,
AIO_WRITE_DONE);
else
- {
- /* We perform synchronous I/O on an asynchronous unit, so no need
- to enqueue AIO_READ_DONE. */
- if (dtp->u.p.async)
- enqueue_done (dtp->u.p.current_unit->au, AIO_WRITE_DONE);
- }
+ enqueue_done (dtp->u.p.current_unit->au, AIO_WRITE_DONE);
unlock_unit (dtp->u.p.current_unit);
}
else
- st_write_done_worker (dtp, true); /* Calls unlock_unit. */
+ {
+ if (dtp->u.p.current_unit->au
+ && (dtp->common.flags & IOPARM_DT_HAS_ID))
+ *dtp->id = async_complete_id (dtp->u.p.current_unit->au);
+ st_write_done_worker (dtp, true); /* Calls unlock_unit. */
+ }
}
library_end ();
diff --git a/libgomp/testsuite/libgomp.fortran/async_io_11.f90 b/libgomp/testsuite/libgomp.fortran/async_io_11.f90
new file mode 100644
index 00000000000..3000ae87e83
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/async_io_11.f90
@@ -0,0 +1,61 @@
+! { dg-do run }
+! PR libfortran/128720 - Derived types with defined I/O in asynchronous data
+! transfer statements crashed.
+module async_io_11_m
+ implicit none
+ type :: pt
+ real :: x, y
+ contains
+ procedure :: w => wr
+ procedure :: r => rd
+ generic :: write(unformatted) => w
+ generic :: read(unformatted) => r
+ end type pt
+contains
+ subroutine wr (d, u, ios, msg)
+ class(pt), intent(in) :: d
+ integer, intent(in) :: u
+ integer, intent(out) :: ios
+ character(*), intent(inout) :: msg
+ write (u, iostat=ios, iomsg=msg) d%y, d%x
+ end subroutine wr
+ subroutine rd (d, u, ios, msg)
+ class(pt), intent(inout) :: d
+ integer, intent(in) :: u
+ integer, intent(out) :: ios
+ character(*), intent(inout) :: msg
+ read (u, iostat=ios, iomsg=msg) d%y, d%x
+ end subroutine rd
+end module async_io_11_m
+
+program main
+ use async_io_11_m
+ implicit none
+ type(pt), asynchronous :: a, b
+ real, allocatable, asynchronous :: big(:)
+ integer :: id0, id1, id2
+ logical :: pend
+ allocate (big(2000000))
+ a = pt(3.0, 4.0)
+ big = 7.0
+ open (10, file='async_io_11.bin', form='unformatted', &
+ asynchronous='yes', status='replace')
+ write (10, asynchronous='yes', id=id0) big
+ write (10, asynchronous='yes', id=id1) a
+ write (10, asynchronous='yes', id=id2) big
+ if (id1 <= id0 .or. id2 <= id1) stop 1
+ inquire (10, id=id1, pending=pend)
+ if (pend) stop 2
+ wait (10, id=id2)
+ rewind (10)
+ big = 0.0
+ read (10) big
+ if (any (big /= 7.0)) stop 3
+ read (10, asynchronous='yes') b
+ big = 0.0
+ read (10, asynchronous='yes') big
+ wait (10)
+ close (10, status='delete')
+ if (b%x /= 3.0 .or. b%y /= 4.0) stop 4
+ if (any (big /= 7.0)) stop 5
+end program main
--
2.51.0
From 52a7e89fad90a37466c6f06a647aa5cb0e4809ea Mon Sep 17 00:00:00 2001
From: Suvarchal Kumar Cheedela <[email protected]>
Date: Sat, 14 Nov 2026 10:55:26 +0100
Subject: [PATCH 3/4] libgfortran: Release skipped items after an asynchronous
I/O error [PR128721]
Once an error is pending on an asynchronous unit, the I/O thread skips
the remaining queue items. It did not free the parameter blocks of
skipped AIO_DATA_TRANSFER_INIT items or the array descriptors of
skipped AIO_TRANSFER_ARRAY items, and it unlocked io_lock for every
skipped AIO_*_DONE item although the matching init had not locked it.
Built with -fsanitize=leak, the new test reported some 300 leaked
blocks before this change; none of them remain.
libgfortran/ChangeLog:
PR libfortran/128721
* io/async.c (async_io): For skipped AIO_DATA_TRANSFER_INIT items
lock io_lock and free the parameter block, for skipped
AIO_TRANSFER_ARRAY items free the descriptor.
libgomp/ChangeLog:
PR libfortran/128721
* testsuite/libgomp.fortran/async_io_12.f90: New test.
Signed-off-by: Suvarchal Kumar Cheedela <[email protected]>
---
libgfortran/io/async.c | 7 ++++
.../testsuite/libgomp.fortran/async_io_12.f90 | 35 +++++++++++++++++++
2 files changed, 42 insertions(+)
create mode 100644 libgomp/testsuite/libgomp.fortran/async_io_12.f90
diff --git a/libgfortran/io/async.c b/libgfortran/io/async.c
index 9ab7bebd3fa..da66d59fef7 100644
--- a/libgfortran/io/async.c
+++ b/libgfortran/io/async.c
@@ -179,6 +179,13 @@ async_io (void *arg)
NOTE ("Received AIO_CLOSE during error condition");
goto finish_thread;
}
+ else if (ctq->type == AIO_DATA_TRANSFER_INIT)
+ {
+ LOCK (&au->io_lock);
+ free (ctq->new_pdt);
+ }
+ else if (ctq->type == AIO_TRANSFER_ARRAY)
+ free (ctq->arg.array.desc);
}
NOTE ("Next ctq, current id: %d", au->id.low);
diff --git a/libgomp/testsuite/libgomp.fortran/async_io_12.f90 b/libgomp/testsuite/libgomp.fortran/async_io_12.f90
new file mode 100644
index 00000000000..562322b49ed
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/async_io_12.f90
@@ -0,0 +1,35 @@
+! { dg-do run }
+! PR libfortran/128721 - Asynchronous statements queued after one that fails are
+! skipped without leaking memory.
+program main
+ implicit none
+ integer, parameter :: n = 2000000
+ real, allocatable, asynchronous :: a(:)
+ real, asynchronous :: b(100), c(100)
+ integer :: i, ios, nerr
+ allocate (a(n))
+ a = 1.0
+ open (10, file='async_io_12.dat', form='unformatted', &
+ asynchronous='yes', status='replace')
+ write (10) a
+ rewind (10)
+ a = 0.0
+ nerr = 0
+ read (10, asynchronous='yes', iostat=ios) a
+ if (ios /= 0) nerr = nerr + 1
+ do i = 1, 100
+ read (10, asynchronous='yes', iostat=ios) b, c
+ if (ios /= 0) nerr = nerr + 1
+ end do
+ wait (10, iostat=ios)
+ if (ios /= 0) nerr = nerr + 1
+ if (nerr == 0) stop 1
+ if (any (a /= 1.0)) stop 2
+ rewind (10)
+ b = 0.0
+ read (10, asynchronous='yes') b
+ wait (10)
+ if (any (b /= 1.0)) stop 3
+ close (10, status='delete')
+ deallocate (a)
+end program main
--
2.51.0
From 962cd2fddc3e5b067f4cce3dbb91e9ea2b1ebaf4 Mon Sep 17 00:00:00 2001
From: Suvarchal Kumar Cheedela <[email protected]>
Date: Sat, 14 Nov 2026 10:55:26 +0100
Subject: [PATCH 4/4] fortran: Transfer numeric and logical pointer arrays as
arrays [PR128722]
gfc_trans_transfer scalarizes every pointer array in an I/O list,
because the descriptor of a pointer may have a span that differs from
its element size, for example when it is associated with a component
of an array of derived type, and the library computed byte strides
from the element size. The result is one library call per element.
For asynchronous I/O it is one queue item per element: writing the
restart files of an ocean model whose fields are pointer arrays took
120 s instead of 4 s.
Make transfer_array_inner compute byte strides from the descriptor
span, and pass pointer arrays of numeric and logical type by
descriptor. Character and derived-type pointer arrays are scalarized
as before.
The new code depends on the library honoring the span. It calls the
new entry points transfer_array_span and transfer_array_span_write, so
that a program built with this compiler fails to load with an older
libgfortran instead of transferring wrong data.
gcc/fortran/ChangeLog:
PR fortran/128722
* trans-io.cc (enum iocall): Add IOCALL_X_ARRAY_SPAN and
IOCALL_X_ARRAY_SPAN_WRITE.
(gfc_build_io_library_fndecls): Build their decls.
(transfer_array_desc): Add argument SPAN to select them.
(gfc_trans_transfer): Do not scalarize pointer arrays of numeric
or logical type, pass them to the span entry points.
libgfortran/ChangeLog:
PR fortran/128722
* gfortran.map (GFORTRAN_17): Add _gfortran_transfer_array_span
and _gfortran_transfer_array_span_write.
* io/transfer.c (transfer_array_inner): Compute byte strides from
the descriptor span.
(transfer_array_span, transfer_array_span_write): New functions.
gcc/testsuite/ChangeLog:
PR fortran/128722
* gfortran.dg/pointer_array_io_1.f90: New test.
Signed-off-by: Suvarchal Kumar Cheedela <[email protected]>
---
gcc/fortran/trans-io.cc | 41 +++++++---
.../gfortran.dg/pointer_array_io_1.f90 | 76 +++++++++++++++++++
libgfortran/gfortran.map | 2 +
libgfortran/io/transfer.c | 31 +++++++-
4 files changed, 139 insertions(+), 11 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/pointer_array_io_1.f90
diff --git a/gcc/fortran/trans-io.cc b/gcc/fortran/trans-io.cc
index 732c59250d9..a1519dad35c 100644
--- a/gcc/fortran/trans-io.cc
+++ b/gcc/fortran/trans-io.cc
@@ -136,6 +136,8 @@ enum iocall
IOCALL_X_COMPLEX128_WRITE,
IOCALL_X_ARRAY,
IOCALL_X_ARRAY_WRITE,
+ IOCALL_X_ARRAY_SPAN,
+ IOCALL_X_ARRAY_SPAN_WRITE,
IOCALL_X_DERIVED,
IOCALL_OPEN,
IOCALL_CLOSE,
@@ -416,6 +418,17 @@ gfc_build_io_library_fndecls (void)
void_type_node, 4, dt_parm_type, pvoid_type_node,
integer_type_node, gfc_charlen_type_node);
+ iocall[IOCALL_X_ARRAY_SPAN] = gfc_build_library_function_decl_with_spec (
+ get_identifier (PREFIX("transfer_array_span")), ". w w . . ",
+ void_type_node, 4, dt_parm_type, pvoid_type_node,
+ integer_type_node, gfc_charlen_type_node);
+
+ iocall[IOCALL_X_ARRAY_SPAN_WRITE]
+ = gfc_build_library_function_decl_with_spec (
+ get_identifier (PREFIX("transfer_array_span_write")), ". w r . . ",
+ void_type_node, 4, dt_parm_type, pvoid_type_node,
+ integer_type_node, gfc_charlen_type_node);
+
iocall[IOCALL_X_DERIVED] = gfc_build_library_function_decl_with_spec (
get_identifier (PREFIX("transfer_derived")), ". w w ",
void_type_node, 2, dt_parm_type, pvoid_type_node);
@@ -2574,7 +2587,8 @@ transfer_expr (gfc_se * se, gfc_typespec * ts, tree addr_expr,
array should be of one of the intrinsic types. */
static void
-transfer_array_desc (gfc_se * se, gfc_typespec * ts, tree addr_expr)
+transfer_array_desc (gfc_se * se, gfc_typespec * ts, tree addr_expr,
+ bool span)
{
tree tmp, charlen_arg, kind_arg, io_call;
@@ -2587,9 +2601,9 @@ transfer_array_desc (gfc_se * se, gfc_typespec * ts, tree addr_expr)
tmp = gfc_build_addr_expr (NULL_TREE, dt_parm);
if (last_dt == READ)
- io_call = iocall[IOCALL_X_ARRAY];
+ io_call = iocall[span ? IOCALL_X_ARRAY_SPAN : IOCALL_X_ARRAY];
else
- io_call = iocall[IOCALL_X_ARRAY_WRITE];
+ io_call = iocall[span ? IOCALL_X_ARRAY_SPAN_WRITE : IOCALL_X_ARRAY_WRITE];
tmp = build_call_expr_loc (UNKNOWN_LOCATION,
io_call, 4,
@@ -2613,6 +2627,7 @@ gfc_trans_transfer (gfc_code * code)
tree tmp;
tree vptr;
int n;
+ bool span_array = false;
gfc_start_block (&block);
gfc_init_block (&body);
@@ -2650,15 +2665,23 @@ gfc_trans_transfer (gfc_code * code)
}
/* These expressions don't always have the dtype element length set
- correctly, rendering them useless for array transfer. */
+ correctly, rendering them useless for array transfer. Numeric and
+ logical pointer arrays are passed to the transfer functions that
+ use the descriptor span. */
+ span_array = (expr->ts.type != BT_CLASS
+ && expr->expr_type == EXPR_VARIABLE
+ && (gfc_expr_attr (expr).pointer
+ || (expr->symtree->n.sym->attr.pointer
+ && gfc_expr_attr (expr).target)));
+ if (span_array
+ && !gfc_numeric_ts (&expr->ts)
+ && expr->ts.type != BT_LOGICAL)
+ goto scalarize;
if (expr->ts.type != BT_CLASS
&& expr->expr_type == EXPR_VARIABLE
&& ((expr->symtree->n.sym->ts.type == BT_DERIVED && expr->ts.deferred)
|| (expr->symtree->n.sym->assoc
- && expr->symtree->n.sym->assoc->variable)
- || gfc_expr_attr (expr).pointer
- || (expr->symtree->n.sym->attr.pointer
- && gfc_expr_attr (expr).target)))
+ && expr->symtree->n.sym->assoc->variable)))
goto scalarize;
if (gfc_expr_attr (expr).pdt_string)
@@ -2716,7 +2739,7 @@ gfc_trans_transfer (gfc_code * code)
tmp = gfc_build_addr_expr (NULL_TREE, se.expr);
}
- transfer_array_desc (&se, &expr->ts, tmp);
+ transfer_array_desc (&se, &expr->ts, tmp, span_array);
goto finish_block_label;
}
diff --git a/gcc/testsuite/gfortran.dg/pointer_array_io_1.f90 b/gcc/testsuite/gfortran.dg/pointer_array_io_1.f90
new file mode 100644
index 00000000000..131ccba7853
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pointer_array_io_1.f90
@@ -0,0 +1,76 @@
+! { dg-do run }
+! { dg-options "-fdump-tree-original" }
+! PR fortran/128722 - Numeric and logical pointer arrays in I/O lists are
+! transferred as arrays, also when they point to a component of an array of
+! derived type.
+module m
+ implicit none
+contains
+ subroutine wr (u, p, q, l)
+ integer, intent(in) :: u
+ real(8), pointer, intent(in) :: p(:)
+ integer, pointer, intent(in) :: q(:)
+ logical, pointer, intent(in) :: l(:)
+ write (u) p, q, l
+ end subroutine wr
+ subroutine wr2 (u, p)
+ integer, intent(in) :: u
+ real(8), pointer, intent(in) :: p(:,:)
+ write (u) p
+ end subroutine wr2
+ subroutine rd (u, p)
+ integer, intent(in) :: u
+ real(8), pointer, intent(in) :: p(:)
+ read (u) p
+ end subroutine rd
+end module m
+
+program main
+ use m
+ implicit none
+ type :: t
+ real(8) :: x
+ integer :: tag
+ logical :: l
+ real(8) :: y
+ end type t
+ type(t), target :: d(100)
+ real(8), pointer :: px(:), p2(:,:)
+ integer, pointer :: pt(:)
+ logical, pointer :: pl(:)
+ real(8), allocatable, target :: a(:,:)
+ real(8) :: rx(100), r2(20,30)
+ integer :: rt(100), i
+ logical :: rl(100)
+
+ do i = 1, 100
+ d(i) = t (1.0d0 * i, 1000 + i, mod (i, 3) == 0, -1.0d0 * i)
+ end do
+ a = reshape ([(1.0d0 * i, i = 1, 2400)], [40, 60])
+ px => d%x
+ pt => d%tag
+ pl => d%l
+ p2 => a(1:40:2, 1:60:2)
+
+ open (10, file='pointer_array_io_1.bin', form='unformatted', status='replace')
+ call wr (10, px, pt, pl)
+ call wr2 (10, p2)
+ rewind (10)
+ read (10) rx, rt, rl
+ read (10) r2
+ if (any (rx /= d%x)) stop 1
+ if (any (rt /= d%tag)) stop 2
+ if (any (rl .neqv. d%l)) stop 3
+ if (any (r2 /= a(1:40:2, 1:60:2))) stop 4
+
+ rewind (10)
+ d%x = 0
+ call rd (10, px)
+ close (10, status='delete')
+ if (any (d%x /= [(1.0d0 * i, i = 1, 100)])) stop 5
+ if (any (d%y /= [(-1.0d0 * i, i = 1, 100)])) stop 6
+ if (any (d%tag /= [(1000 + i, i = 1, 100)])) stop 7
+end program main
+! { dg-final { scan-tree-dump-times "_gfortran_transfer_array_span_write" 4 "original" } }
+! { dg-final { scan-tree-dump-times "_gfortran_transfer_array_span " 1 "original" } }
+! { dg-final { scan-tree-dump-not "_gfortran_transfer_real_write" "original" } }
diff --git a/libgfortran/gfortran.map b/libgfortran/gfortran.map
index 6e04842e953..731bb827536 100644
--- a/libgfortran/gfortran.map
+++ b/libgfortran/gfortran.map
@@ -2077,4 +2077,6 @@ GFORTRAN_17 {
_gfortran_specific__tand_r17;
_gfortran_specific__tand_r4;
_gfortran_specific__tand_r8;
+ _gfortran_transfer_array_span;
+ _gfortran_transfer_array_span_write;
} GFORTRAN_16;
diff --git a/libgfortran/io/transfer.c b/libgfortran/io/transfer.c
index f8097c30409..7c0f0bf613d 100644
--- a/libgfortran/io/transfer.c
+++ b/libgfortran/io/transfer.c
@@ -129,6 +129,14 @@ extern void transfer_array_write (st_parameter_dt *, gfc_array_char *, int,
gfc_charlen_type);
export_proto(transfer_array_write);
+extern void transfer_array_span (st_parameter_dt *, gfc_array_char *, int,
+ gfc_charlen_type);
+export_proto(transfer_array_span);
+
+extern void transfer_array_span_write (st_parameter_dt *, gfc_array_char *,
+ int, gfc_charlen_type);
+export_proto(transfer_array_span_write);
+
/* User defined derived type input/output. */
extern void
transfer_derived (st_parameter_dt *dtp, void *dtio_source, void *dtio_proc);
@@ -2826,7 +2834,7 @@ transfer_array_inner (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
index_type count[GFC_MAX_DIMENSIONS];
index_type extent[GFC_MAX_DIMENSIONS];
index_type stride[GFC_MAX_DIMENSIONS];
- index_type stride0, rank, size, n;
+ index_type stride0, rank, size, span, n;
size_t tsize;
char *data;
bt iotype;
@@ -2840,11 +2848,12 @@ transfer_array_inner (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
size = iotype == BT_CHARACTER ? charlen : GFC_DESCRIPTOR_SIZE (desc);
rank = GFC_DESCRIPTOR_RANK (desc);
+ span = desc->span > 0 ? desc->span : GFC_DESCRIPTOR_SIZE (desc);
for (n = 0; n < rank; n++)
{
count[n] = 0;
- stride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(desc,n);
+ stride[n] = GFC_DESCRIPTOR_STRIDE (desc, n) * span;
extent[n] = GFC_DESCRIPTOR_EXTENT(desc,n);
/* If the extent of even one dimension is zero, then the entire
@@ -2968,6 +2977,24 @@ transfer_array_write (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
transfer_array (dtp, desc, kind, charlen);
}
+/* Entry points for arrays whose span may differ from the element size.
+ They behave like transfer_array; the separate symbols keep programs
+ that depend on the span from running with an older library. */
+
+void
+transfer_array_span (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
+ gfc_charlen_type charlen)
+{
+ transfer_array (dtp, desc, kind, charlen);
+}
+
+void
+transfer_array_span_write (st_parameter_dt *dtp, gfc_array_char *desc,
+ int kind, gfc_charlen_type charlen)
+{
+ transfer_array (dtp, desc, kind, charlen);
+}
+
/* User defined input/output iomsg. */
--
2.51.0