Skip to content

Commit fa37976

Browse files
committed
merge main into amd-staging
2 parents d14a2ca + 8afea0d commit fa37976

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4299,13 +4299,30 @@ mapParentWithMembers(LLVM::ModuleTranslation &moduleTranslation,
42994299
assert(!ompBuilder.Config.isTargetDevice() &&
43004300
"function only supported for host device codegen");
43014301

4302-
// Map the first segment of our structure
4303-
const size_t parentIndex = combinedInfo.Types.size();
4304-
combinedInfo.Types.emplace_back(
4305-
(targetDirective == TargetDirective::Target &&
4306-
!mapData.IsDeclareTarget[mapDataIndex])
4302+
// Map the first segment of the parent. If a user-defined mapper is attached,
4303+
// include the parent's to/from-style bits (and common modifiers) in this
4304+
// base entry so the mapper receives correct copy semantics via its 'type'
4305+
// parameter. Also keep TARGET_PARAM when required for kernel arguments.
4306+
llvm::omp::OpenMPOffloadMappingFlags baseFlag =
4307+
isTargetParams
43074308
? llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TARGET_PARAM
4308-
: llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_NONE);
4309+
: llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_NONE;
4310+
4311+
// Detect if this mapping uses a user-defined mapper.
4312+
bool hasUserMapper = mapData.Mappers[mapDataIndex] != nullptr;
4313+
if (hasUserMapper) {
4314+
using mapFlags = llvm::omp::OpenMPOffloadMappingFlags;
4315+
// Preserve relevant map-type bits from the parent clause. These include
4316+
// the copy direction (TO/FROM), as well as commonly used modifiers that
4317+
// should be visible to the mapper for correct behaviour.
4318+
mapFlags parentFlags = mapData.Types[mapDataIndex];
4319+
mapFlags preserve = mapFlags::OMP_MAP_TO | mapFlags::OMP_MAP_FROM |
4320+
mapFlags::OMP_MAP_ALWAYS | mapFlags::OMP_MAP_CLOSE |
4321+
mapFlags::OMP_MAP_PRESENT | mapFlags::OMP_MAP_OMPX_HOLD;
4322+
baseFlag |= (parentFlags & preserve);
4323+
}
4324+
4325+
combinedInfo.Types.emplace_back(baseFlag);
43094326
combinedInfo.DevicePointers.emplace_back(
43104327
mapData.DevicePointers[mapDataIndex]);
43114328
combinedInfo.Mappers.emplace_back(mapData.Mappers[mapDataIndex]);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
! This test validates that declare mapper for a derived type with an
2+
! allocatable component preserves TO/FROM semantics for the component,
3+
! ensuring the payload is copied back to the host on target exit.
4+
5+
! REQUIRES: flang, amdgpu
6+
7+
! RUN: %libomptarget-compile-fortran-run-and-check-generic
8+
9+
program target_declare_mapper_allocatable
10+
implicit none
11+
12+
type :: real_t
13+
real, allocatable :: real_arr(:)
14+
end type real_t
15+
16+
! Map the allocatable array payload via a named mapper.
17+
!$omp declare mapper (xyz : real_t :: t) map(tofrom: t%real_arr)
18+
19+
type(real_t) :: r
20+
integer :: i
21+
logical :: ok
22+
23+
allocate(r%real_arr(10))
24+
r%real_arr = 1.0
25+
26+
!$omp target map(mapper(xyz), tofrom: r)
27+
do i = 1, size(r%real_arr)
28+
r%real_arr(i) = 3.0
29+
end do
30+
!$omp end target
31+
32+
ok = .true.
33+
do i = 1, size(r%real_arr)
34+
if (r%real_arr(i) /= 3.0) ok = .false.
35+
end do
36+
if (ok) then
37+
print *, "Test passed!"
38+
else
39+
print *, "Test failed!"
40+
do i = 1, size(r%real_arr)
41+
print *, r%real_arr(i)
42+
end do
43+
end if
44+
45+
deallocate(r%real_arr)
46+
end program target_declare_mapper_allocatable
47+
48+
! CHECK: Test passed!

0 commit comments

Comments
 (0)