[llvm-branch-commits] [clang] [flang] [llvm] [mlir] [Flang][Clang][OpenMP][OpenMPIRBuilder] Implement has_attach_ptr TODO and tidy now deadcode (PR #236987)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Nov 11 09:52:57 PST 2026
https://github.com/agozillon updated https://github.com/llvm/llvm-project/pull/236987
>From 927b0d31dc345aac3fce64e7a1ced38c958581b1 Mon Sep 17 00:00:00 2001
From: agozillon <Andrew.Gozillon at amd.com>
Date: Thu, 29 Oct 2026 14:31:09 -0500
Subject: [PATCH] [Flang][Clang][OpenMP][OpenMPIRBuilder] Implement
has_attach_ptr TODO and tidy now deadcode
This patch is 3/3 for adding has_attach_ptr lowering to Flang, it removes the dependence on
a catch all boolean that was added previously in place of the has_attach_ptr mechanism that
was added a little later, allowing Flang to synch a little more closely to Clang.
Most of the machinary for actually setting has_attach_ptr is in the Flang frontend, this PR
mainly forwards the information from MLIR to the OMPIRBuilder, and then remove the old boolean
that's no longer requried. We also remove the boolean setting from Clang for it hence the Clang
tag.
Otherwise we also add/tweak a few tests that are dependent on all pieces being in place.
---
clang/lib/CodeGen/CGOpenMPRuntime.cpp | 2 +-
.../OpenMP/descriptor-has-attach-ptr.f90 | 133 ++++++++++++++++++
.../llvm/Frontend/OpenMP/OMPIRBuilder.h | 2 +-
llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 10 +-
.../OpenMP/OpenMPToLLVMIRTranslation.cpp | 34 ++---
mlir/test/Target/LLVMIR/omptarget-llvm.mlir | 6 +-
.../omptarget-mapper-has-attach-ptr.mlir | 63 +++++++++
7 files changed, 218 insertions(+), 32 deletions(-)
create mode 100644 flang/test/Integration/OpenMP/descriptor-has-attach-ptr.f90
create mode 100644 mlir/test/Target/LLVMIR/omptarget-mapper-has-attach-ptr.mlir
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index d3a6ffee3ea748..3d6495b0d8458c 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -10926,7 +10926,7 @@ void CGOpenMPRuntime::emitUserDefinedMapper(const OMPDeclareMapperDecl *D,
bool PropagatePresentToPointee = CGM.getLangOpts().OpenMP >= 60;
llvm::Function *NewFn = cantFail(OMPBuilder.emitUserDefinedMapper(
PrivatizeAndGenMapInfoCB, ElemTy, Name, CustomMapperCB,
- /*PreserveMemberOfFlags=*/false, PropagatePresentToPointee));
+ PropagatePresentToPointee));
UDMMap.try_emplace(D, NewFn);
if (CGF)
FunctionUDMMap[CGF->CurFn].push_back(D);
diff --git a/flang/test/Integration/OpenMP/descriptor-has-attach-ptr.f90 b/flang/test/Integration/OpenMP/descriptor-has-attach-ptr.f90
new file mode 100644
index 00000000000000..853498b545602e
--- /dev/null
+++ b/flang/test/Integration/OpenMP/descriptor-has-attach-ptr.f90
@@ -0,0 +1,133 @@
+! Verify that LLVM-IR lowering from Flang for descriptor-based maps drives
+! OMPIRBuilder's HasAttachPtr logic correctly in generated mapper functions.
+
+! RUN: %flang_fc1 -emit-llvm -fopenmp -fopenmp-version=52 -fopenmp-targets=amdgcn-amd-amdhsa -mllvm -ignore-missing-type-desc -o - %s 2>&1 | FileCheck %s
+
+module descriptor_has_attach_ptr_integ_m
+ implicit none
+
+ type :: dtype_a
+ integer :: num_vals
+ integer, allocatable :: var_a(:)
+ end type dtype_a
+
+ type :: mix_dtype
+ integer :: scalar
+ integer, allocatable :: arr(:)
+ end type mix_dtype
+
+ type :: pointee_t
+ integer :: x
+ integer :: y
+ end type pointee_t
+
+ type :: holder_t
+ type(pointee_t), pointer :: p
+ end type holder_t
+
+contains
+
+subroutine mapper_has_attach_ptr()
+ type(dtype_a) :: s
+
+ !$omp declare mapper (dtype_a :: var) map(var, var%var_a(1:var%num_vals))
+
+ !$omp target map(mapper(default) : s)
+ !$omp end target
+end subroutine mapper_has_attach_ptr
+
+subroutine mapper_implicit_mix(x)
+ type(mix_dtype) :: x
+
+ !$omp declare mapper (mix_dtype :: v) map(v, v%arr, v%scalar)
+
+ !$omp target map(mapper(default) : x)
+ !$omp end target
+
+ !$omp target
+ !$omp end target
+end subroutine mapper_implicit_mix
+
+subroutine mapper_member_of_has_attach_ptr()
+ type(holder_t) :: h
+
+ !$omp declare mapper(holder_t :: v) map(v, v%p%x, v%p%y)
+
+ !$omp target map(mapper(default): h)
+ !$omp end target
+end subroutine mapper_member_of_has_attach_ptr
+
+end module descriptor_has_attach_ptr_integ_m
+
+! CHECK-DAG: @.offload_maptypes{{.*}} = private unnamed_addr constant [2 x i64] [i64 35, i64 288]
+! CHECK-DAG: @.offload_maptypes{{.*}} = private unnamed_addr constant [1 x i64] [i64 288]
+
+! CHECK-DAG: %.offload_mappers{{.*}} = alloca [2 x ptr]
+! CHECK-DAG: store ptr @.omp_mapper._QQMdescriptor_has_attach_ptr_integ_mFmapper_has_attach_ptrdtype_a_omp_default_mapper, ptr %{{.*}}, align 8
+! CHECK-DAG: store ptr @.omp_mapper._QQMdescriptor_has_attach_ptr_integ_mFmapper_implicit_mixmix_dtype_omp_default_mapper, ptr %{{.*}}, align 8
+! CHECK-DAG: store ptr @.omp_mapper._QQMdescriptor_has_attach_ptr_integ_mFmapper_member_of_has_attach_ptrholder_t_omp_default_mapper, ptr %{{.*}}, align 8
+
+! CHECK-LABEL: define internal void @.omp_mapper._QQMdescriptor_has_attach_ptr_integ_mFmapper_has_attach_ptrdtype_a_omp_default_mapper
+! CHECK-SAME: (ptr noundef %{{.*}}, ptr noundef %{{.*}}, ptr noundef %{{.*}}, i64 noundef %{{.*}}, i64 noundef %{{.*}}, ptr noundef %{{.*}})
+! CHECK: %[[DTA_PREV:.*]] = call i64 @__tgt_mapper_num_components
+! CHECK: %[[DTA_SHIFT:.*]] = shl i64 %[[DTA_PREV]], 48
+
+! Descriptor/object entry for var%var_a is an ordinary MEMBER_OF entry and is
+! shifted by the current mapper component count.
+! CHECK: %[[DTA_DESC_MEMBER:.*]] = add nuw i64 281474976710659, %[[DTA_SHIFT]]
+! CHECK: %[[DTA_DESC_TYPE:.*]] = phi i64 [ {{.*}}, %omp.type.alloc2 ], [ {{.*}}, %omp.type.to4 ], [ {{.*}}, %omp.type.from6 ], [ %[[DTA_DESC_MEMBER]], %omp.type.to.else5 ]
+! CHECK: %[[DTA_DESC_WITH_MODS:.*]] = or i64 %[[DTA_DESC_TYPE]], %{{.*}}
+! CHECK: call void @__tgt_push_mapper_component(ptr %{{.*}}, ptr %{{.*}}, ptr %{{.*}}, i64 56, i64 %[[DTA_DESC_WITH_MODS]], ptr @{{[0-9]+}})
+
+! Pointee storage for var%var_a is HasAttachPtr without an inner MEMBER_OF, so
+! it must not get an outer MEMBER_OF added.
+! CHECK: %[[DTA_PTEE_TYPE:.*]] = phi i64 [ 0, %omp.type.alloc18 ], [ 1, %omp.type.to20 ], [ 2, %omp.type.from22 ], [ 3, %omp.type.to.else21 ]
+! CHECK: %[[DTA_PTEE_WITH_MODS:.*]] = or i64 %[[DTA_PTEE_TYPE]], %{{.*}}
+! CHECK: call void @__tgt_push_mapper_component(ptr %{{.*}}, ptr %{{.*}}, ptr %{{.*}}, i64 %{{.*}}, i64 %[[DTA_PTEE_WITH_MODS]], ptr @{{[0-9]+}})
+
+! ATTACH remains raw ATTACH.
+! CHECK: %[[DTA_ATTACH_TYPE:.*]] = phi i64 [ 16384, %omp.type.alloc26 ], [ 16384, %omp.type.to28 ], [ 16384, %omp.type.from30 ], [ 16384, %omp.type.to.else29 ]
+! CHECK: call void @__tgt_push_mapper_component(ptr %{{.*}}, ptr %{{.*}}, ptr %{{.*}}, i64 %{{.*}}, i64 %[[DTA_ATTACH_TYPE]], ptr @{{[0-9]+}})
+
+! CHECK-LABEL: define internal void @.omp_mapper._QQMdescriptor_has_attach_ptr_integ_mFmapper_implicit_mixmix_dtype_omp_default_mapper
+! CHECK-SAME: (ptr noundef %{{.*}}, ptr noundef %{{.*}}, ptr noundef %{{.*}}, i64 noundef %{{.*}}, i64 noundef %{{.*}}, ptr noundef %{{.*}})
+! CHECK: %[[MIX_PREV:.*]] = call i64 @__tgt_mapper_num_components
+! CHECK: %[[MIX_SHIFT:.*]] = shl i64 %[[MIX_PREV]], 48
+
+! Scalar member is not a pointee reached through an attach pointer and is
+! represented as a regular MEMBER_OF component.
+! CHECK: %[[MIX_SCALAR_MEMBER:.*]] = add nuw i64 281474976710659, %[[MIX_SHIFT]]
+! CHECK: %[[MIX_SCALAR_TYPE:.*]] = phi i64 [ {{.*}}, %omp.type.alloc2 ], [ {{.*}}, %omp.type.to4 ], [ {{.*}}, %omp.type.from6 ], [ %[[MIX_SCALAR_MEMBER]], %omp.type.to.else5 ]
+! CHECK: %[[MIX_SCALAR_WITH_MODS:.*]] = or i64 %[[MIX_SCALAR_TYPE]], %{{.*}}
+! CHECK: call void @__tgt_push_mapper_component(ptr %{{.*}}, ptr %{{.*}}, ptr %{{.*}}, i64 {{.*}}, i64 %[[MIX_SCALAR_WITH_MODS]], ptr @{{[0-9]+}})
+
+! CHECK-LABEL: define internal void @.omp_mapper._QQMdescriptor_has_attach_ptr_integ_mFmapper_member_of_has_attach_ptrholder_t_omp_default_mapper
+! CHECK-SAME: (ptr noundef %{{.*}}, ptr noundef %{{.*}}, ptr noundef %{{.*}}, i64 noundef %{{.*}}, i64 noundef %{{.*}}, ptr noundef %{{.*}})
+
+! The mapper for holder_t contains p%x and p%y entries in the p pointee block.
+! These entries are HasAttachPtr and already have an inner MEMBER_OF mask. The
+! existing MEMBER_OF value is shifted by the number of pre-existing mapper components.
+! CHECK: %[[H_PREV:.*]] = call i64 @__tgt_mapper_num_components
+! CHECK: %[[H_SHIFT:.*]] = shl i64 %[[H_PREV]], 48
+
+! Descriptor/object entry for p is an ordinary MEMBER_OF entry.
+! CHECK: %[[H_DESC_MEMBER:.*]] = add nuw i64 281474976710659, %[[H_SHIFT]]
+! CHECK: %[[H_DESC_TYPE:.*]] = phi i64 [ {{.*}}, %omp.type.alloc1 ], [ {{.*}}, %omp.type.to3 ], [ {{.*}}, %omp.type.from5 ], [ %[[H_DESC_MEMBER]], %omp.type.to.else4 ]
+! CHECK: %[[H_DESC_WITH_MODS:.*]] = or i64 %[[H_DESC_TYPE]], %{{.*}}
+! CHECK: call void @__tgt_push_mapper_component(ptr %{{.*}}, ptr %{{.*}}, ptr %{{.*}}, i64 40, i64 %[[H_DESC_WITH_MODS]], ptr @{{[0-9]+}})
+
+! p%x: HasAttachPtr and already MEMBER_OF the p pointee block.
+! CHECK: %[[H_X_MEMBER:.*]] = add nuw i64 281474976710659, %[[H_SHIFT]]
+! CHECK: %[[H_X_TYPE:.*]] = phi i64 [ {{.*}}, %omp.type.alloc25 ], [ {{.*}}, %omp.type.to27 ], [ {{.*}}, %omp.type.from29 ], [ %[[H_X_MEMBER]], %omp.type.to.else28 ]
+! CHECK: %[[H_X_WITH_MODS:.*]] = or i64 %[[H_X_TYPE]], %{{.*}}
+! CHECK: call void @__tgt_push_mapper_component(ptr %{{.*}}, ptr %{{.*}}, ptr %{{.*}}, i64 4, i64 %[[H_X_WITH_MODS]], ptr @{{[0-9]+}})
+
+! p%y: second HasAttachPtr entry that already has the same inner MEMBER_OF mask.
+! CHECK: %[[H_Y_MEMBER:.*]] = add nuw i64 281474976710659, %[[H_SHIFT]]
+! CHECK: %[[H_Y_TYPE:.*]] = phi i64 [ {{.*}}, %omp.type.alloc33 ], [ {{.*}}, %omp.type.to35 ], [ {{.*}}, %omp.type.from37 ], [ %[[H_Y_MEMBER]], %omp.type.to.else36 ]
+! CHECK: %[[H_Y_WITH_MODS:.*]] = or i64 %[[H_Y_TYPE]], %{{.*}}
+! CHECK: call void @__tgt_push_mapper_component(ptr %{{.*}}, ptr %{{.*}}, ptr %{{.*}}, i64 4, i64 %[[H_Y_WITH_MODS]], ptr @{{[0-9]+}})
+
+! ATTACH for p remains raw ATTACH and is not HasAttachPtr.
+! CHECK: %[[H_ATTACH_TYPE:.*]] = phi i64 [ 16384, %omp.type.alloc41 ], [ 16384, %omp.type.to43 ], [ 16384, %omp.type.from45 ], [ 16384, %omp.type.to.else44 ]
+! CHECK: call void @__tgt_push_mapper_component(ptr %{{.*}}, ptr %{{.*}}, ptr %{{.*}}, i64 %{{.*}}, i64 %[[H_ATTACH_TYPE]], ptr @{{[0-9]+}})
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
index a1ed0d2b065759..5b4b180622f511 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -3766,7 +3766,7 @@ class OpenMPIRBuilder {
InsertPointTy CodeGenIP, llvm::Value *PtrPHI, llvm::Value *BeginArg)>
PrivAndGenMapInfoCB,
llvm::Type *ElemTy, StringRef FuncName,
- CustomMapperCallbackTy CustomMapperCB, bool PreserveMemberOfFlags = false,
+ CustomMapperCallbackTy CustomMapperCB,
bool PropagatePresentToPointee = false);
/// Generator for '#omp target data'
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 2fbbeefff2d5c6..7918fd5e66f503 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -10702,7 +10702,7 @@ Expected<Function *> OpenMPIRBuilder::emitUserDefinedMapper(
llvm::Value *BeginArg)>
GenMapInfoCB,
Type *ElemTy, StringRef FuncName, CustomMapperCallbackTy CustomMapperCB,
- bool PreserveMemberOfFlags, bool PropagatePresentToPointee) {
+ bool PropagatePresentToPointee) {
SmallVector<Type *> Params;
Params.emplace_back(Builder.getPtrTy());
Params.emplace_back(Builder.getPtrTy());
@@ -10856,17 +10856,11 @@ Expected<Function *> OpenMPIRBuilder::emitUserDefinedMapper(
// shifted -- see (***).
// * (**) ATTACH entries: they are not a member of anything — they just
// link a ptr to its ptee.
- // * All entries when PreserveMemberOfFlags is set (the Flang/MLIR path):
- // its pre-shaped entries already carry their final MEMBER_OF bits.
- // TODO: set HasAttachPtr from Flang for entries whose storage is the
- // pointee's (e.g. s%p(0:10)) and drop PreserveMemberOfFlags in favor of
- // it.
//
// (***) If such an entry already has its own MEMBER_OF bits (e.g. the
// s1p->x/y entries above), those bits are still shifted by N.
Value *MemberMapType;
- if (PreserveMemberOfFlags || (RawType & AttachBit) ||
- Info->HasAttachPtr[I]) {
+ if ((RawType & AttachBit) || Info->HasAttachPtr[I]) {
if (RawType & MemberOfMask)
MemberMapType = Builder.CreateNUWAdd(OriMapType, ShiftedPreviousSize);
else
diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
index e8bcc22464439b..fe00ed47dc4a75 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -7611,8 +7611,7 @@ static void collectMapDataFromMapOperands(
builder, moduleTranslation));
mapData.MapClause.push_back(mapOp.getOperation());
mapData.Types.push_back(convertClauseMapFlags(mapOp.getMapType()));
- // TODO: set HasAttachPtr from Flang for pointee-storage entries.
- mapData.HasAttachPtr.push_back(false);
+ mapData.HasAttachPtr.push_back(mapOp.getHasAttachPtr());
mapData.Names.push_back(LLVM::createMappingInformation(
mapOp.getLoc(), *moduleTranslation.getOpenMPBuilder(),
getMapClauseName(mapOp)));
@@ -7682,8 +7681,7 @@ static void collectMapDataFromMapOperands(
mapData.MapClause.push_back(mapOp.getOperation());
mapData.Types.push_back(
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_RETURN_PARAM);
- // TODO: set HasAttachPtr from Flang for pointee-storage entries.
- mapData.HasAttachPtr.push_back(false);
+ mapData.HasAttachPtr.push_back(mapOp.getHasAttachPtr());
mapData.Names.push_back(LLVM::createMappingInformation(
mapOp.getLoc(), *moduleTranslation.getOpenMPBuilder(),
getMapClauseName(mapOp)));
@@ -7725,8 +7723,7 @@ static void collectMapDataFromMapOperands(
// rematerialized, so the address of the decriptor for a given object
// may change from one place to another.
mapData.Types.push_back(mapType);
- // TODO: set HasAttachPtr from Flang for pointee-storage entries.
- mapData.HasAttachPtr.push_back(false);
+ mapData.HasAttachPtr.push_back(mapOp.getHasAttachPtr());
// Technically it's possible for a non-descriptor mapping to have
// both has-device-addr and ALWAYS, so lookup the mapper in case it
// exists.
@@ -7743,8 +7740,7 @@ static void collectMapDataFromMapOperands(
mapData.Types.push_back(
isDevicePtr ? mapType
: llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_LITERAL);
- // TODO: set HasAttachPtr from Flang for pointee-storage entries.
- mapData.HasAttachPtr.push_back(false);
+ mapData.HasAttachPtr.push_back(mapOp.getHasAttachPtr());
mapData.Mappers.push_back(nullptr);
}
mapData.Names.push_back(LLVM::createMappingInformation(
@@ -8089,8 +8085,7 @@ processIndividualMap(llvm::IRBuilderBase &builder,
combinedInfo.Mappers.emplace_back(mapData.Mappers[mapDataIdx]);
combinedInfo.Names.emplace_back(mapData.Names[mapDataIdx]);
combinedInfo.Types.emplace_back(mapFlag);
- // TODO: set HasAttachPtr from Flang for pointee-storage entries.
- combinedInfo.HasAttachPtr.emplace_back(false);
+ combinedInfo.HasAttachPtr.emplace_back(mapData.HasAttachPtr[mapDataIdx]);
// A privatized attach map needs storage for the pointer or descriptor even
// when its pointee is null. Its size describes that storage, not the pointee,
// and is needed by the runtime for corresponding-pointer-initialization.
@@ -8160,8 +8155,7 @@ static void mapParentWithMembers(
}
combinedInfo.Types.emplace_back(baseFlag);
- // TODO: set HasAttachPtr from Flang for pointee-storage entries.
- combinedInfo.HasAttachPtr.emplace_back(false);
+ combinedInfo.HasAttachPtr.emplace_back(mapData.HasAttachPtr[mapDataIndex]);
combinedInfo.DevicePointers.emplace_back(
mapData.DevicePointers[mapDataIndex]);
// Only attach the mapper to the base entry when we are mapping the whole
@@ -8263,8 +8257,8 @@ static void mapParentWithMembers(
if (targetDirective == TargetDirectiveEnumTy::TargetUpdate || hasMapClose ||
overlapIdxs.size() == 1) {
combinedInfo.Types.emplace_back(mapFlag);
- // TODO: set HasAttachPtr from Flang for pointee-storage entries.
- combinedInfo.HasAttachPtr.emplace_back(false);
+ combinedInfo.HasAttachPtr.emplace_back(
+ mapData.HasAttachPtr[mapDataIndex]);
combinedInfo.DevicePointers.emplace_back(
mapData.DevicePointers[mapDataIndex]);
combinedInfo.Names.emplace_back(LLVM::createMappingInformation(
@@ -8307,8 +8301,8 @@ static void mapParentWithMembers(
auto isPtrMap = checkIfPointerMap(
llvm::cast<omp::MapInfoOp>(mapData.MapClause[mapDataOverlapIdx]));
combinedInfo.Types.emplace_back(mapFlag);
- // TODO: set HasAttachPtr from Flang for pointee-storage entries.
- combinedInfo.HasAttachPtr.emplace_back(false);
+ combinedInfo.HasAttachPtr.emplace_back(
+ mapData.HasAttachPtr[mapDataIndex]);
combinedInfo.DevicePointers.emplace_back(
llvm::OpenMPIRBuilder::DeviceInfoTy::None);
combinedInfo.Names.emplace_back(LLVM::createMappingInformation(
@@ -8340,8 +8334,8 @@ static void mapParentWithMembers(
}
combinedInfo.Types.emplace_back(mapFlag);
- // TODO: set HasAttachPtr from Flang for pointee-storage entries.
- combinedInfo.HasAttachPtr.emplace_back(false);
+ combinedInfo.HasAttachPtr.emplace_back(
+ mapData.HasAttachPtr[mapDataIndex]);
combinedInfo.DevicePointers.emplace_back(
llvm::OpenMPIRBuilder::DeviceInfoTy::None);
combinedInfo.Names.emplace_back(LLVM::createMappingInformation(
@@ -8651,8 +8645,7 @@ emitUserDefinedMapper(Operation *op, llvm::IRBuilderBase &builder,
};
llvm::Expected<llvm::Function *> newFn = ompBuilder->emitUserDefinedMapper(
- genMapInfoCB, varType, mapperFuncName, customMapperCB,
- /*PreserveMemberOfFlags=*/true);
+ genMapInfoCB, varType, mapperFuncName, customMapperCB);
if (!newFn)
return newFn.takeError();
if ([[maybe_unused]] llvm::Function *mappedFunc =
@@ -9969,7 +9962,6 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
combinedInfos.Types.push_back(
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TARGET_PARAM |
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_LITERAL);
- // TODO: set HasAttachPtr from Flang for pointee-storage entries.
combinedInfos.HasAttachPtr.push_back(false);
if (!combinedInfos.Names.empty())
combinedInfos.Names.push_back(nullPtr);
diff --git a/mlir/test/Target/LLVMIR/omptarget-llvm.mlir b/mlir/test/Target/LLVMIR/omptarget-llvm.mlir
index 908e8f0a02fc14..efda7ac8224c98 100644
--- a/mlir/test/Target/LLVMIR/omptarget-llvm.mlir
+++ b/mlir/test/Target/LLVMIR/omptarget-llvm.mlir
@@ -582,23 +582,27 @@ module attributes {omp.target_triples = ["amdgcn-amd-amdhsa"]} {
// CHECK: %[[VAL_45:.*]] = getelementptr %[[VAL_18]], ptr %[[VAL_43]], i32 0, i32 0
// CHECK: %[[VAL_46:.*]] = call i64 @__tgt_mapper_num_components(ptr %[[VAL_37]])
// CHECK: %[[VAL_47:.*]] = shl i64 %[[VAL_46]], 48
+// CHECK: %[[VAL_48:.*]] = add nuw i64 3, %[[VAL_47]]
// CHECK: %[[VAL_49:.*]] = and i64 %[[VAL_22]], 3
// CHECK: %[[VAL_50:.*]] = icmp eq i64 %[[VAL_49]], 0
// CHECK: br i1 %[[VAL_50]], label %[[VAL_51:.*]], label %[[VAL_52:.*]]
// CHECK: omp.type.alloc: ; preds = %[[VAL_41]]
+// CHECK: %[[ALLOC_MAPTYPE:.*]] = and i64 %[[VAL_48]], -4
// CHECK: br label %[[VAL_42]]
// CHECK: omp.type.alloc.else: ; preds = %[[VAL_41]]
// CHECK: %[[VAL_54:.*]] = icmp eq i64 %[[VAL_49]], 1
// CHECK: br i1 %[[VAL_54]], label %[[VAL_55:.*]], label %[[VAL_56:.*]]
// CHECK: omp.type.to: ; preds = %[[VAL_52]]
+// CHECK: %[[TO_MAPTYPE:.*]] = and i64 %[[VAL_48]], -3
// CHECK: br label %[[VAL_42]]
// CHECK: omp.type.to.else: ; preds = %[[VAL_52]]
// CHECK: %[[VAL_58:.*]] = icmp eq i64 %[[VAL_49]], 2
// CHECK: br i1 %[[VAL_58]], label %[[VAL_59:.*]], label %[[VAL_42]]
// CHECK: omp.type.from: ; preds = %[[VAL_56]]
+// CHECK: %[[FROM_MAPTYPE:.*]] = and i64 %[[VAL_48]], -2
// CHECK: br label %[[VAL_42]]
// CHECK: omp.type.end: ; preds = %[[VAL_59]], %[[VAL_56]], %[[VAL_55]], %[[VAL_51]]
-// CHECK: %[[VAL_61:.*]] = phi i64 [ 0, %[[VAL_51]] ], [ 1, %[[VAL_55]] ], [ 2, %[[VAL_59]] ], [ 3, %[[VAL_56]] ]
+// CHECK: %[[VAL_61:.*]] = phi i64 [ %[[ALLOC_MAPTYPE]], %[[VAL_51]] ], [ %[[TO_MAPTYPE]], %[[VAL_55]] ], [ %[[FROM_MAPTYPE]], %[[VAL_59]] ], [ %[[VAL_48]], %[[VAL_56]] ]
// Map-type modifiers (ALWAYS|DELETE|CLOSE = 1036) from the outer clause are
// propagated to each entry the mapper pushes.
// CHECK: %[[VAL_MODMASK:.*]] = and i64 %{{.*}}, 1036
diff --git a/mlir/test/Target/LLVMIR/omptarget-mapper-has-attach-ptr.mlir b/mlir/test/Target/LLVMIR/omptarget-mapper-has-attach-ptr.mlir
new file mode 100644
index 00000000000000..32ef84c760ce0d
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/omptarget-mapper-has-attach-ptr.mlir
@@ -0,0 +1,63 @@
+// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
+
+// Verify that HasAttachPtr controls mapper MEMBER_OF adjustment for a pointee
+// storage block reached through an attach pointer. The combined pointee block
+// entry must not be made MEMBER_OF the outer object, but member entries inside
+// that pointee block keep their inner MEMBER_OF relation, shifted by the
+// mapper's current component count. The ATTACH entry remains unmodified.
+
+module attributes {omp.target_triples = ["amdgcn-amd-amdhsa"]} {
+ omp.declare_mapper @mapper : !llvm.struct<"S2", (i32, ptr)> {
+ ^bb0(%arg0: !llvm.ptr):
+ %field = llvm.getelementptr %arg0[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"S2", (i32, ptr)>
+ %ptee = llvm.load %field : !llvm.ptr -> !llvm.ptr
+ %x = llvm.getelementptr %ptee[0, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"S1", (i32, i32)>
+ %y = llvm.getelementptr %ptee[0, 1] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"S1", (i32, i32)>
+ %map_x = omp.map.info var_ptr(%x : !llvm.ptr, i32)
+ map_clauses(tofrom) capture(ByRef) name("x") has_attach_ptr(true) -> !llvm.ptr
+ %map_y = omp.map.info var_ptr(%y : !llvm.ptr, i32)
+ map_clauses(tofrom) capture(ByRef) name("y") has_attach_ptr(true) -> !llvm.ptr
+ %map_ptee = omp.map.info var_ptr(%ptee : !llvm.ptr, !llvm.struct<"S1", (i32, i32)>)
+ map_clauses(tofrom) capture(ByRef) members(%map_x, %map_y : [0], [1] : !llvm.ptr, !llvm.ptr) name("s1p") partial_map(true) has_attach_ptr(true) -> !llvm.ptr
+ %map_attach = omp.map.info var_ptr(%arg0 : !llvm.ptr, !llvm.struct<"S2", (i32, ptr)>)
+ map_clauses(attach, ref_ptr, ref_ptee) capture(ByRef) var_ptr_ptr(%field : !llvm.ptr, !llvm.struct<"S1", (i32, i32)>) name("s1p") -> !llvm.ptr
+ omp.declare_mapper.info map_entries(%map_ptee, %map_x, %map_y, %map_attach : !llvm.ptr, !llvm.ptr, !llvm.ptr, !llvm.ptr)
+ }
+
+ llvm.func @test(%arg0: !llvm.ptr) {
+ %map = omp.map.info var_ptr(%arg0 : !llvm.ptr, !llvm.struct<"S2", (i32, ptr)>)
+ map_clauses(tofrom) capture(ByRef) mapper(@mapper) name("s2") -> !llvm.ptr
+ omp.target kernel_type(generic) map_entries(%map -> %arg1 : !llvm.ptr) {
+ omp.terminator
+ }
+ llvm.return
+ }
+}
+
+// CHECK-LABEL: define internal void @.omp_mapper.mapper
+// CHECK: %[[PREV:.*]] = call i64 @__tgt_mapper_num_components
+// CHECK: %[[SHIFT:.*]] = shl i64 %[[PREV]], 48
+// CHECK: %[[PTEE_TYPE:.*]] = phi i64 [ 32,
+// CHECK: %[[PTEE_MODS:.*]] = and i64 %{{.*}}, 1036
+// CHECK: %[[PTEE_WITH_MODS:.*]] = or i64 %[[PTEE_TYPE]], %[[PTEE_MODS]]
+// CHECK: call void @__tgt_push_mapper_component({{.*}}i64 %[[PTEE_WITH_MODS]],
+
+// The x/y entries are MEMBER_OF the pointee block already. HasAttachPtr
+// prevents adding an outer MEMBER_OF directly, but OMPIRBuilder still shifts
+// the existing MEMBER_OF field by the current mapper component count.
+// CHECK: %[[X_INNER:.*]] = add nuw i64 {{[0-9]+}}, %[[SHIFT]]
+// CHECK: %[[X_TYPE:.*]] = phi i64 [ {{.*%.*}}, %{{.*}} ], [ {{.*%.*}}, %{{.*}} ], [ {{.*%.*}}, %{{.*}} ], [ %[[X_INNER]], %{{.*}} ]
+// CHECK: %[[X_MODS:.*]] = and i64 %{{.*}}, 1036
+// CHECK: %[[X_WITH_MODS:.*]] = or i64 %[[X_TYPE]], %[[X_MODS]]
+// CHECK: call void @__tgt_push_mapper_component({{.*}}i64 %[[X_WITH_MODS]],
+// CHECK: %[[Y_INNER:.*]] = add nuw i64 {{[0-9]+}}, %[[SHIFT]]
+// CHECK: %[[Y_TYPE:.*]] = phi i64 [ {{.*%.*}}, %{{.*}} ], [ {{.*%.*}}, %{{.*}} ], [ {{.*%.*}}, %{{.*}} ], [ %[[Y_INNER]], %{{.*}} ]
+// CHECK: %[[Y_MODS:.*]] = and i64 %{{.*}}, 1036
+// CHECK: %[[Y_WITH_MODS:.*]] = or i64 %[[Y_TYPE]], %[[Y_MODS]]
+// CHECK: call void @__tgt_push_mapper_component({{.*}}i64 %[[Y_WITH_MODS]],
+
+// ATTACH entries are forced to keep their unmodified ATTACH map type.
+// CHECK: %[[ATTACH_TYPE:.*]] = phi i64 [ 16384,
+// CHECK: %[[ATTACH_MODS:.*]] = and i64 %{{.*}}, 1036
+// CHECK: %[[ATTACH_WITH_MODS:.*]] = or i64 %[[ATTACH_TYPE]], %[[ATTACH_MODS]]
+// CHECK: call void @__tgt_push_mapper_component({{.*}}i64 %[[ATTACH_TYPE]],
More information about the llvm-branch-commits
mailing list