[flang-commits] [flang] [flang] Keep a LOGICAL input item unchanged on big-endian targets (PR #228588)

Eugene Epshteyn via flang-commits flang-commits at lists.llvm.org
Mon Oct 5 10:52:43 PDT 2026


================
@@ -0,0 +1,22 @@
+! A LOGICAL input item is passed to the runtime as a bool&. Its current value
+! is stored as a bool before the call, so that it is kept when the runtime does
+! not set it (null value in list-directed or namelist input). Otherwise the old
+! value would be lost on big-endian targets, where the first byte of a
+! LOGICAL(4) .TRUE. is zero.
+! RUN: %flang_fc1 -emit-hlfir %s -o - | FileCheck %s
+
+! CHECK-LABEL: func.func @_QPread_logical(
+subroutine read_logical(l)
+  logical :: l
+  read (*, *) l
+end subroutine
+! CHECK:  %[[L:.*]]:2 = hlfir.declare {{.*}}"_QFread_logicalEl"
+! CHECK:  %[[ARG:.*]] = fir.convert %[[L]]#0 : (!fir.ref<!fir.logical<4>>) -> !fir.ref<i1>
+! CHECK:  %[[OLD:.*]] = fir.load %[[L]]#0 : !fir.ref<!fir.logical<4>>
+! CHECK:  %[[B:.*]] = fir.convert %[[OLD]] : (!fir.logical<4>) -> i1
+! CHECK:  %[[BA:.*]] = fir.convert %[[L]]#0 : (!fir.ref<!fir.logical<4>>) -> !fir.ref<i1>
+! CHECK:  fir.store %[[B]] to %[[BA]] : !fir.ref<i1>
+! CHECK:  fir.call @_FortranAioInputLogical(%{{.*}}, %[[ARG]])
+! CHECK:  %[[NEW:.*]] = fir.load %{{.*}} : !fir.ref<i1>
+! CHECK:  %[[V:.*]] = fir.convert %[[NEW]] : (i1) -> !fir.logical<4>
+! CHECK:  fir.store %[[V]] to %[[L]]#0 : !fir.ref<!fir.logical<4>>
----------------
eugeneepshteyn wrote:

(This comment was generated with the help of AI)

Optional: a vector-subscripted item reaches the same helper from the element loop, which is a second caller. A case for it would also cover a non-default kind:

```fortran
! CHECK-LABEL: func.func @_QPread_vector_subscript(
subroutine read_vector_subscript(a, v)
  logical(8) :: a(:)
  integer :: v(:)
  read (*, *) a(v)
end subroutine
! CHECK:  fir.do_loop
! CHECK:    %[[E:.*]] = fir.array_coor {{.*}} -> !fir.ref<!fir.logical<8>>
! CHECK:    %[[OLD:.*]] = fir.load %[[E]] : !fir.ref<!fir.logical<8>>
! CHECK:    %[[B:.*]] = fir.convert %[[OLD]] : (!fir.logical<8>) -> i1
! CHECK:    %[[BA:.*]] = fir.convert %[[E]] : (!fir.ref<!fir.logical<8>>) -> !fir.ref<i1>
! CHECK:    fir.store %[[B]] to %[[BA]] : !fir.ref<i1>
! CHECK-NEXT: fir.call @_FortranAioInputLogical(
! CHECK:    %[[RA:.*]] = fir.convert %[[E]] : (!fir.ref<!fir.logical<8>>) -> !fir.ref<i1>
! CHECK:    %[[NEW:.*]] = fir.load %[[RA]] : !fir.ref<i1>
! CHECK:    %[[V:.*]] = fir.convert %[[NEW]] : (i1) -> !fir.logical<8>
! CHECK:    fir.store %[[V]] to %[[E]] : !fir.ref<!fir.logical<8>>
```

It passes with this PR and fails without it (`flang -fc1 -emit-hlfir t.f90 -o - | FileCheck t.f90`).

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


More information about the flang-commits mailing list