[flang-commits] [flang] [flang][OpenMP] Support custom mappers in target update to/from clauses (PR #169673)

Krish Gupta via flang-commits flang-commits at lists.llvm.org
Thu Nov 27 12:33:25 PST 2025


================

----------------
KrxGu wrote:

I can add an offloading test in `offload/test/offloading/fortran/target-update-custom-mapper.f90`, however I don't have access to GPU hardware and can only test on CPU. 

I've prepared the test and verified to the best of my capability:
- Correct Fortran syntax (no syntax errors)
- Compiles successfully to MLIR
- Generated IR contains proper `omp.declare_mapper` declarations for both custom and default mappers
- `omp.target_update` operations correctly reference the mappers (`mapper(@_QQFcustom)` and `mapper(@_QQFmy_type_omp_default_mapper)`)

The test follows the same pattern as existing offloading tests like `target-custom-mapper.f90`. It will require GPU hardware to actually execute (`REQUIRES: flang, amdgpu`).

Here's the test code:

```fortran
! Test custom mappers with target update to/from clauses
! REQUIRES: flang, amdgpu

! RUN: %libomptarget-compile-fortran-run-and-check-generic

program target_update_mapper_test
   implicit none
   integer, parameter :: n = 100
   
   type :: my_type
      integer :: a(n)
      integer :: b(n)
   end type my_type

   ! Declare custom mapper that only maps field 'a'
   !$omp declare mapper(custom : my_type :: t) map(t%a)
   
   ! Declare default mapper that maps both fields
   !$omp declare mapper(my_type :: t) map(t%a, t%b)

   type(my_type) :: obj
   integer :: i, sum_a, sum_b

   ! Initialize data on host
   do i = 1, n
      obj%a(i) = i
      obj%b(i) = i * 2
   end do

   ! Allocate space on device using custom mapper (only maps 'a')
   !$omp target enter data map(mapper(custom), alloc: obj)

   ! Update field 'a' to device using custom mapper
   obj%a = 10
   !$omp target update to(mapper(custom): obj)

   ! Read back field 'a' from device (should be 10)
   obj%a = 0
   !$omp target update from(mapper(custom): obj)
   
   sum_a = sum(obj%a)
   sum_b = sum(obj%b)

   ! CHECK: Sum of a after update from device: 1000
   print *, "Sum of a after update from device:", sum_a
   
   ! Field 'b' was never mapped, so should still have original values
   ! CHECK: Sum of b (never mapped): 10100
   print *, "Sum of b (never mapped):", sum_b

   ! Now test with default mapper (maps both fields)
   obj%a = 20
   obj%b = 30
   
   !$omp target update to(mapper(default): obj)
   
   obj%a = 0
   obj%b = 0
   
   !$omp target update from(mapper(default): obj)
   
   sum_a = sum(obj%a)
   sum_b = sum(obj%b)
   
   ! CHECK: Sum of a with default mapper: 2000
   print *, "Sum of a with default mapper:", sum_a
   
   ! CHECK: Sum of b with default mapper: 3000
   print *, "Sum of b with default mapper:", sum_b

   !$omp target exit data map(delete: obj)

   ! CHECK: Test passed!
   print *, "Test passed!"

end program target_update_mapper_test
```
Let me know if you'd like me to include this in the PR, or if you prefer to validate it on GPU hardware first.

https://github.com/llvm/llvm-project/pull/169673


More information about the flang-commits mailing list