Issue 241462
Summary [flang][OpenMP] User-defined default mapper ignored or replaced, depending on DECLARE MAPPER order
Labels flang
Assignees chichunchen
Reporter chichunchen
    ### Summary

When a `DECLARE MAPPER` maps a component whose type has a user-defined default mapper, Flang attaches that mapper only if its `omp.declare_mapper` op already exists in the module. Mappers are lowered one at a time, so the result depends on declaration order:

1. If the component type's mapper is declared later, the component is mapped without a mapper.
2. If the component type also has an allocatable component, lowering generates an implicit default mapper under the user mapper's symbol name. Lowering then skips the user's `DECLARE MAPPER`, so the user mapper is discarded for **every** use of that type.
3. Imported mappers are materialized in symbol-name order, so renaming a type can change which mappers apply.

None of these cases emits a diagnostic.

### Reproducer 1: component mapper not attached

```fortran
subroutine later_inner_mapper()
  type :: inner
    integer :: a(4)
  end type
 type :: outer
    type(inner) :: in
  end type
  type(outer) :: x
  !$omp declare mapper(outer :: w) map(tofrom: w%in)
  !$omp declare mapper(inner :: v) map(to: v%a(1:2))
  !$omp target map(tofrom: x)
  x%in%a(1) = 1
  !$omp end target
end subroutine
```

```
flang -fc1 -emit-hlfir -fopenmp -fopenmp-version=52 -fopenmp-targets=amdgcn-amd-amdhsa repro1.f90 -o -
```

The `outer` mapper maps `w%in` without a mapper:

```
omp.declare_mapper @_QQFlater_inner_mapperouter_omp_default_mapper : ... {
  ...
 omp.map.info var_ptr(... !fir.type<_QFlater_inner_mapperTinner{...}>) map_clauses(tofrom) capture(ByRef) name("w%in")
```

If the two `DECLARE MAPPER` lines are swapped, the map has `mapper(@..._inner_omp_default_mapper) name("w%in")`. `-emit-llvm` succeeds in both cases, so the `inner` mapper is silently not applied to `x%in`.

### Reproducer 2: user mapper replaced by a compiler-generated mapper

```fortran
subroutine later_inner_mapper_alloc()
  type :: inner
    integer, allocatable :: p(:)
    integer :: n
  end type
  type :: outer
    type(inner) :: in
 end type
  type(inner) :: y
  !$omp declare mapper(outer :: w) map(tofrom: w%in)
  !$omp declare mapper(inner :: v) map(to: v%n)
  !$omp target map(tofrom: y)
  y%n = 1
  !$omp end target
end subroutine
```

Here `@..._inner_omp_default_mapper` contains the compiler-generated body:
- It maps `p` with `map_clauses(implicit, tofrom, ref_ptee)`.
- The user's `map(to: v%n)` does not appear anywhere in the module.
- `y`, which is mapped directly and not through `outer`, also uses this replacement mapper.

With `-fno-implicit-default-mapper-allocatable-members`, the user mapper is kept, but `w%in` again gets no mapper, as in reproducer 1. An older trunk build (`e38e87dbb0f3`) keeps the user mapper here, so this case may be newer.

### Reproducer 3: imported mappers depend on symbol-name order

```fortran
! m.f90
module m
  type :: b_inner
    integer :: a(4)
  end type
  type :: a_outer
    type(b_inner) :: in
  end type
 !$omp declare mapper(b_inner :: v) map(to: v%a(1:2))
  !$omp declare mapper(a_outer :: w) map(tofrom: w%in)
end module
```

```fortran
! use.f90
subroutine use_m()
  use m
  type(a_outer) :: x
  !$omp target map(tofrom: x)
  x%in%a(1) = 1
  !$omp end target
end subroutine
```

```
flang -fc1 -emit-hlfir -fopenmp -fopenmp-version=52 -module-dir . m.f90 -o /dev/null
flang -fc1 -emit-hlfir -fopenmp -fopenmp-version=52 -fopenmp-targets=amdgcn-amd-amdhsa -J . use.f90 -o -
```

When `m.f90` is compiled, its source order is correct, so `w%in` gets `mapper(@_QQMmb_inner_omp_default_mapper)`. In `use.f90`, the materialized `a_outer` mapper maps `w%in` without a mapper. After renaming the types so the inner type's name sorts first (`a_inner` and `b_outer`), `use.f90` attaches the mapper.

### Expected behavior

The `inner` default mapper is visible to both `DECLARE MAPPER` directives. Therefore, `w%in` should use it regardless of declaration order or module symbol names. A compiler-generated implicit mapper should never replace a user-declared default mapper.

Assisted with Copilot and Claude Opus 5.5.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to