Issue 207858
Summary [flang][mif] Ucobounds not properly allocated for corank > 1
Labels flang:fir-hlfir
Assignees JDPailleux
Reporter bonachea
    The recent merge of #192944 has revealed a latent defect in ucobounds allocation for coarrays with corank > 1.

MRE:
```fortran
program coarray_bounds_check
  integer :: coarray[100:200, 300:400, 500:*]
 write(*,'(A,3I4)') "LCOBOUND: ", LCOBOUND(coarray)
  write(*,'(A,3I4)') "UCOBOUND: ", UCOBOUND(coarray)
  write(*,'(A,3I4)') "COSHAPE:  ", COSHAPE(coarray)
end program
```

Expected correct output (verified from a single-image run of NAG 7.2):
```
LCOBOUND:  100 300 500
UCOBOUND:  200 400 500
COSHAPE:   101 101   1
```

Flang's incorrect output: (from `main` @ b0a64cf46adcb6da2cef28bac25f910077277923
```
root@8d810d7d1e55:/home# flang --version
flang version 23.0.0git (https://github.com/llvm/llvm-project.git b0a64cf46adcb6da2cef28bac25f910077277923)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-23/bin

root@8d810d7d1e55:/home# flang -g -O0  -fcoarray coarray-bounds-bug.F90 -Linst/lib -lcaffeine-smp-seq -lgasnet-smp-seq && env GASNET_PSHM_NODES=1 ./a.out
warning: Support for multi image Fortran features is still experimental and in development.
LCOBOUND:  100 300 500
UCOBOUND:  100 300 500
COSHAPE:     1   1   1
```

Note the `UCOBOUND(1:2)` values for have incorrectly been copied from `LCOBOUND(1:2)`, instead of having the user-specified values. Using a debugger I've verified the wrong values are being passed in the `ucobounds` argument to `prif::prif_allocate_coarray` (at coarray allocation time):

```
(gdb) where
#0  prif::prif_allocate_coarray (lcobounds=..., ucobounds=..., size_in_bytes=4, final_proc=0x0, coarray_handle=..., 
 allocated_memory=..., stat=0, errmsg=..., errmsg_alloc=...) at <redacted>/caffeine/allocation_s.F90:24
#1  0x000055555555fea9 in COARRAY_BOUNDS_CHECK () at coarray-bounds-bug.F90:2
(gdb) print lcobounds 
$1 = (100, 300, 500)
(gdb) print ucobounds
$2 = (100, 300)
```

Dumping the MLIR, it appears the correct `ucobound(1:2)` values `(200, 400)` have been lost somewhere before the insertion of `mif.alloc_coarray` operation:

```
root@8d810d7d1e55:/home# flang -g -O0 -fcoarray coarray-bounds-bug.F90 -c  -mmlir --mlir-print-ir-before=mif-convert 2>&1 | grep -i -B50 mif.alloc  
warning: Support for multi image Fortran features is still experimental and in development.
// -----// IR Dump Before MIFOpConversion: mif-convert //----- //
module attributes {dlti.dl_spec = #dlti.dl_spec<!llvm.ptr<270> = dense<32> : vector<4xi64>, !llvm.ptr<271> = dense<32> : vector<4xi64>, !llvm.ptr<272> = dense<64> : vector<4xi64>, i64 = dense<64> : vector<2xi64>, i128 = dense<128> : vector<2xi64>, f80 = dense<128> : vector<2xi64>, !llvm.ptr = dense<64> : vector<4xi64>, i1 = dense<8> : vector<2xi64>, i8 = dense<8> : vector<2xi64>, i16 = dense<16> : vector<2xi64>, i32 = dense<32> : vector<2xi64>, f16 = dense<16> : vector<2xi64>, f64 = dense<64> : vector<2xi64>, f128 = dense<128> : vector<2xi64>, "dlti.endianness" = "little", "dlti.mangling_mode" = "e", "dlti.legal_int_widths" = array<i32: 8, 16, 32, 64>, "dlti.stack_alignment" = 128 : i64>, fir.defaultkind = "a1c4d8i4l4r4", fir.is_pie, fir.kindmap = "", fir.relocation_model = 1 : i32, fir.target_cpu = "x86-64", llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128", llvm.ident = "flang version 23.0.0 (https://github.com/llvm/llvm-project.git b0a64cf46adcb6da2cef28bac25f910077277923)", llvm.target_triple = "x86_64-unknown-linux-gnu"} {
  func.func @_QQmain() attributes {fir.bindc_name = "COARRAY_BOUNDS_CHECK"} {
    %c5_i32 = arith.constant 5 : i32
    %c2_i32 = arith.constant 2 : i32
    %c1_i32 = arith.constant 1 : i32
    %c4_i32 = arith.constant 4 : i32
    %c3 = arith.constant 3 : index
    %c10 = arith.constant 10 : index
    %c3_i32 = arith.constant 3 : i32
    %c6_i32 = arith.constant 6 : i32
    %c7 = arith.constant 7 : index
    %c2 = arith.constant 2 : index
    %c500_i64 = arith.constant 500 : i64
    %c1 = arith.constant 1 : index
    %c300_i64 = arith.constant 300 : i64
    %c0 = arith.constant 0 : index
    %c100_i64 = arith.constant 100 : i64
    %0 = fir.alloca !fir.array<2xi64>
    %1 = fir.alloca !fir.array<3xi64>
    %2 = fir.dummy_scope : !fir.dscope
    %3 = fir.address_of(@_QFEcoarray) : !fir.ref<i32>
    %4 = fir.coordinate_of %1, %c0 : (!fir.ref<!fir.array<3xi64>>, index) -> !fir.ref<i64>
    fir.store %c100_i64 to %4 : !fir.ref<i64>
    %5 = fir.coordinate_of %1, %c1 : (!fir.ref<!fir.array<3xi64>>, index) -> !fir.ref<i64>
    fir.store %c300_i64 to %5 : !fir.ref<i64>
    %6 = fir.coordinate_of %1, %c2 : (!fir.ref<!fir.array<3xi64>>, index) -> !fir.ref<i64>
    fir.store %c500_i64 to %6 : !fir.ref<i64>
    %7 = fir.embox %1 : (!fir.ref<!fir.array<3xi64>>) -> !fir.box<!fir.array<3xi64>>
    %8 = fir.coordinate_of %0, %c0 : (!fir.ref<!fir.array<2xi64>>, index) -> !fir.ref<i64>
    fir.store %c100_i64 to %8 : !fir.ref<i64>
    %9 = fir.coordinate_of %0, %c1 : (!fir.ref<!fir.array<2xi64>>, index) -> !fir.ref<i64>
    fir.store %c300_i64 to %9 : !fir.ref<i64>
    %10 = fir.embox %0 : (!fir.ref<!fir.array<2xi64>>) -> !fir.box<!fir.array<2xi64>>
 mif.alloc_coarray %3 lcobounds %7 ucobounds %10 {uniq_name = "_QFEcoarray"} : (!fir.ref<i32>, !fir.box<!fir.array<3xi64>>, !fir.box<!fir.array<2xi64>>) -> ()
```

This problem only affects coarrays with corank > 1, because coarrays with corank == 1 have an empty static ucobound.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to