[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 Dec 4 11:18:51 PST 2025


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

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

Thanks for testing this! You're absolutely right - I found the logic flaw in my test. 

The issue is that I'm mixing mappers inconsistently:
1. Line 37: I allocate device memory using `custom` mapper (which only maps field `a`)
2. Line 58: I try to update using `default` mapper (which maps both `a` and `b`)

Since field `b` was never allocated on the device, the update fails and reads back as 0.

Here's a corrected version that properly separates the two test scenarios:

```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

   ! ========== Test 1: Custom mapper (field 'a' only) ==========
   
   ! Initialize data on host
   do i = 1, n
      obj%a(i) = i
      obj%b(i) = i * 2
   end do

   ! Allocate and update using custom mapper (only 'a')
   !$omp target enter data map(mapper(custom), alloc: obj)

   obj%a = 10
   !$omp target update to(mapper(custom): obj)

   obj%a = 0
   !$omp target update from(mapper(custom): obj)
   
   sum_a = sum(obj%a)
   sum_b = sum(obj%b)

   ! CHECK: Sum of a (custom mapper): 1000
   print *, "Sum of a (custom mapper):", sum_a
   
   ! Field 'b' was never mapped with custom mapper
   ! CHECK: Sum of b (never mapped): 10100
   print *, "Sum of b (never mapped):", sum_b

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

   ! ========== Test 2: Default mapper (both fields) ==========
   
   ! Re-initialize
   do i = 1, n
      obj%a(i) = 20
      obj%b(i) = 30
   end do
   
   ! Allocate and update using default mapper (both 'a' and 'b')
   !$omp target enter data map(mapper(default), alloc: obj)
   
   !$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 (default mapper): 2000
   print *, "Sum of a (default mapper):", sum_a
   
   ! CHECK: Sum of b (default mapper): 3000
   print *, "Sum of b (default mapper):", sum_b

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

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

end program target_update_mapper_test
```

Would you prefer:

Option 1: I add this corrected test to the PR now (you can validate on GPU hardware through CI)
Option 2: We handle this as a follow-up PR or maybe someone who has GPU access can add it .🙃

Let me know what works best for you!

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


More information about the flang-commits mailing list