Issue 241033
Summary [flang] Incorrect handling of `STAT=` with kind /= 4 in MIF lowering
Labels flang:multi-image
Assignees
Reporter bonachea
    Multi-image features that include a `STAT=` specifier are currently miscompiled when the provided *stat-variable* is not a 32-bit integer.

MRE:
```fortran
program test_sync_all_stat
  use, intrinsic :: iso_fortran_env, only: int64, int32, int16
  implicit none

 integer(kind=int16) :: stat16 = -1_int32
  integer(kind=int32) :: stat32 = -1_int32
  integer(kind=int64) :: stat64 = -1_int64

  sync all (stat=stat32)
  if (this_image() == 1) print '(A, I0)', 'stat32 = ', stat32

  sync all (stat=stat64)
  if (this_image() == 1) print '(A, I0)', 'stat64 = ', stat64

  sync all (stat=stat16)
  if (this_image() == 1) print '(A, I0)', 'stat16 = ', stat16

end program
```

gfortran 16.2 output:
```
$ gfortran -fcoarray=single sync-stat64.F90 && ./a.out
stat32 = 0
stat64 = 0
stat16 = 0
```

flang 23.1.1 output (with Caffeine 0.8.4):
```
$ flang -fcoarray sync-stat64.F90 -L$CAF/lib -lcaffeine-smp-seq -lgasnet-smp-seq && ./a.out
warning: Support for multi image Fortran features is still experimental and in development.
stat32 = 0
stat64 = -4294967296
stat16 = 0
```

The problem is believed to arise in MIF lowering, which passes the user-provided STAT= variable by reference directly to the PRIF call (which expects a 32-bit `INTENT(OUT)` argument), instead of providing type conversion through a temporary when needed. This results in garbage in the upper 32-bits of a 64-bit STAT= variable, and silent data corruption with a 16-bit STAT= variable:

```
flang -fcoarray sync-stat64.F90 -mmlir --mlir-print-ir-after=mif-convert | & grep -B4 'fir.call @_QMprifPprif_sync_all' 
    %13 = fir.declare %12 {uniq_name = "_QFEstat32"} : (!fir.ref<i32>) -> !fir.ref<i32>
    %14 = fir.address_of(@_QFEstat64) : !fir.ref<i64>
    %15 = fir.declare %14 {uniq_name = "_QFEstat64"} : (!fir.ref<i64>) -> !fir.ref<i64>
    %16 = fir.absent !fir.box<!fir.char<1,?>>
    fir.call @_QMprifPprif_sync_all(%13, %16, %16) : (!fir.ref<i32>, !fir.box<!fir.char<1,?>>, !fir.box<!fir.char<1,?>>) -> ()
--
    cf.br ^bb2
  ^bb2:  // 2 preds: ^bb0, ^bb1
    %36 = fir.absent !fir.box<!fir.char<1,?>>
    %37 = fir.convert %15 : (!fir.ref<i64>) -> !fir.ref<i32>
    fir.call @_QMprifPprif_sync_all(%37, %36, %36) : (!fir.ref<i32>, !fir.box<!fir.char<1,?>>, !fir.box<!fir.char<1,?>>) -> ()
--
    cf.br ^bb4
  ^bb4:  // 2 preds: ^bb2, ^bb3
    %57 = fir.absent !fir.box<!fir.char<1,?>>
    %58 = fir.convert %11 : (!fir.ref<i16>) -> !fir.ref<i32>
    fir.call @_QMprifPprif_sync_all(%58, %57, %57) : (!fir.ref<i32>, !fir.box<!fir.char<1,?>>, !fir.box<!fir.char<1,?>>) -> ()
```

This same defect is believed to impact MIF lowering of *stat-variable* for all multi-image features. 

Recommended interrim workaround for end users is to either omit the STAT= specifier from *sync-stat*, or use a 32-bit *stat-variable*.

Identified during review of https://github.com/llvm/llvm-project/pull/237317#discussion_r4463007427

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to