<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/138471>138471</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[flang] bug passing non-contiguous array to MPI procedure
</td>
</tr>
<tr>
<th>Labels</th>
<td>
flang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
nncarlson
</td>
</tr>
</table>
<pre>
I've run into errors with flang 20.1.0 when passing a non-contiguous dummy array to MPI procedures (MPICH 4.3.0), where it needs to be doing copy-in/copy-out. It appears that the address of the initial array element is being passed instead of the address of a _contiguous copy_ of the array. I'm not sure if this is a bug with flang, or a problem with MPICH where it doesn't know how to provide a correct `mpi` module for flang.
I've pared it down to a small (serial) reproducer that doesn't use MPI at all, but mimics the `mpi` module from MPICH 4.3.0. There are two files `mpi_bcast.c`:
```C
// Dummy version of MPI_Bcast to dump the received buffer
#include <stdio.h>
void mpi_bcast_(void *buffer, int *count) {
float *vector = (float*) buffer;
printf(" in mpi_bcast: buffer=");
for (int i = 0; i < *count; ++i) {
printf(" %f", vector[i]);
}
printf("\n");
}
```
and the main program `flang-20240504.F90`:
```fortran
module mpi_dummy
!! Actual form of the interface from the MPICH 4.3.0 mpi module
interface MPI_Bcast
subroutine MPI_Bcast(buffer, count)!, datatype, root, comm, ierror)
implicit none
#ifdef __flang__
!DIR$ IGNORE_TKR buffer
#endif
#ifdef __INTEL_COMPILER
!DEC$ ATTRIBUTES NO_ARG_CHECK :: buffer
#endif
#ifdef __GFORTRAN__
!GCC$ ATTRIBUTES NO_ARG_CHECK :: buffer
#endif
real :: buffer
integer :: count
!integer :: datatype
!integer :: root
!integer :: comm
!integer :: ierror
end subroutine
end interface
end module
program main
use mpi_dummy
real :: x(5)
x = [1,2,3,4,5]
print *, 'x=', x
call bcast(x(1::2))
contains
subroutine bcast(buffer)
real, intent(inout) :: buffer(:)
print *, ' in bcast: buffer=', buffer
! A CONTIGUOUS COPY OF BUFFER MUST BE PASSED HERE
call MPI_Bcast(buffer, size(buffer))
end subroutine
end program
```
To compile:
```sh
clang -c mpi_bcast.c
flang flang-20240504.F90 mpi_bcast.o
```
The expected output from running should be
```
$ ./a.out
x= 1. 2. 3. 4. 5.
in bcast: buffer= 1. 3. 5.
in mpi_bcast: buffer= 1.000000 3.000000 5.000000
```
But with flang I'm getting
```
$ ./a.out
x= 1. 2. 3. 4. 5.
in bcast: buffer= 1. 3. 5.
in mpi_bcast: buffer= 1.000000 2.000000 3.000000
```
Both Intel ifx and gfortran produce the expected results.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzEV09v6jAS_zTmMmpkHChw4BAC9KH3-keUHvaEnNgB7yZ2ZDu03U-_GidASvv2sJdFSCT2_Pl55jfjgTunDlrKORkvyHg54I0_GjvXOue2dEYPMiM-5xvCJicJttGgtDcgrTXWwbvyRyhKrg_AaDSMKLwfpYYajeoDcNBG3-VGe3VoTONANFX1Cdxa_gnewOPLBmprcikaKx0QNn182aS_YBTFESVsRliKBq0E5UFLKRxqZRKEQfO5qT_vlCZsHZ5M4yPYeOB1Lbl14I_cgz9K4EJY6RyYIrwqrbziZQdDlrKS2oNykEm0iuClAKWdl1yclXo2OOx7R0LX-4sUmowAo1WBNh5cg-BxUzl0wSFrDr2w4QmNBY5hyEpZtVttFC4nF0Y6TdjEw7-0eYejeccw1NaclJDAITfWytwDuadVrcg9hcqIppRQGNu6iQhNCE26LNbc4gHR8LtGUxxcxcsSE-CkVbwkbAZW1taIJpe2jeQVReNkSB33wMsSj5A1HipVqdyFMHwHYk0FvdxGsAuH41aCfzdQqBLzH9T2Wc6dj3JyT0mMqPEhfFN8YWvC1rAMRDpJ65TRGP3Hl81-gYp4HtFUdQBiZS7VSQrImqKQNujHSudlIySQOHVeKBMdSbwiNDkZJeACYE_YNKwQlnTaLEXy40JuGu0xSGSyIDQBKErDw85J5t5YIPESoxmWCUtQtDMSB4XaKu0LwqaEMcCP0lfXJE4u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