Issue 168889
Summary [flang][OpenMP] Custom mapper for nested derived type containing an allocatable segfaults during program runtime
Labels flang
Assignees
Reporter jfuchs-kmt
    Suppose a derived type contains another nested derived type. If the nested derived type holds an `ALLOCATABLE` and we try to use a custom mapper to specify the mapping behavior of the `ALLOCATABLE`, then we get a segfault during program runtime. 

To reproduce the issue, see this snippet (ignore the preprocessor macros for now):

```fortran
PROGRAM main
    IMPLICIT NONE

    TYPE nested_t
#ifdef _SCALAR_
        INTEGER :: y
#else
 INTEGER, ALLOCATABLE :: y(:)
#endif
    END TYPE nested_t
    !$omp declare mapper(nested_t :: n) map(n%y)

    TYPE typ_t
        INTEGER, ALLOCATABLE :: x(:)
        TYPE(nested_t) :: nested
    END TYPE typ_t
 !$omp declare mapper(typ_t :: t) map(t%x, t%nested)

    INTEGER, PARAMETER :: n = 4
    TYPE(typ_t) :: typ
    ALLOCATE(typ%x(n), source=1)
#ifdef _SCALAR_
    typ%nested%y = 42
#else
 ALLOCATE(typ%nested%y(n), source=42)
#endif

    !$omp target map(tofrom: typ)
    typ%x(:) = 2
#ifdef _SCALAR_
    typ%nested%y = 43
#else
 typ%nested%y(:) = 43
#endif
    !$omp end target
    ! Both x and y should be incremented on the device and mapped back
    PRINT*, typ%x ! Expected 2
    PRINT*, typ%nested%y ! Expected 43

    DEALLOCATE(typ%x)
#ifndef _SCALAR_
    DEALLOCATE(typ%nested%y)
#endif
END PROGRAM main
```
We can compile this with `flang -O2 -fopenmp -fopenmp-targets=nvptx64-nvidia-cuda -fopenmp-version=52 main.F90` (NVIDIA in my case).
Running the code will result in a `Segmentation fault (core dumped)` in the target region in the line `typ%nested%y(:) = 43`.

An additional interesting observation: If you compile this code with the additional flag `-D_SCALAR_` to replace the nested member variable y with a scalar instead of an allocatable, there is no segfault and we get the expected result.

For reference, I am using a recent `flang version 22.0.0git ([email protected]:llvm/llvm-project.git ee1abb8d80691e5ef24da3273587dc789c9c6f1b)`
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to