[llvm-branch-commits] [flang] [Flang][OpenMP] Teach MapInfoFinalization to apply has_attach_ptr to pointees (PR #236985)
Akash Banerjee via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Oct 30 08:53:03 PDT 2026
================
@@ -383,6 +384,122 @@ class MapInfoFinalizationPass
});
}
+ static bool isStrictPrefix(llvm::ArrayRef<int64_t> prefix,
+ llvm::ArrayRef<int64_t> path) {
+ return prefix.size() < path.size() &&
+ std::equal(prefix.begin(), prefix.end(), path.begin());
+ }
+
+ bool isAttachMap(mlir::omp::MapInfoOp mapOp) {
+ return bitEnumContainsAll(mapOp.getMapType(),
+ mlir::omp::ClauseMapFlags::attach);
+ }
+
+ bool isDescriptorOrPointerObjectMap(mlir::omp::MapInfoOp mapOp) {
+ mlir::Type varType = fir::unwrapRefType(mapOp.getVarPtrType());
+ if (!hasADescriptor(mapOp.getVarPtr().getDefiningOp(), varType))
+ return false;
+
+ mlir::omp::ClauseMapFlags mapType = mapOp.getMapType();
+ bool isPrivatizedDescriptor =
+ bitEnumContainsAll(mapType, mlir::omp::ClauseMapFlags::priv) &&
+ bitEnumContainsAll(mapType, mlir::omp::ClauseMapFlags::attach) &&
+ bitEnumContainsAll(mapType, mlir::omp::ClauseMapFlags::target_param);
+
+ // Treat as descriptor/object map if:
+ // - var_ptr_ptr is null (pure descriptor), OR
+ // - it is a privatized descriptor (PRIVATE|ATTACH|TARGET_PARAM).
+ if (!mapOp.getVarPtrPtr() || isPrivatizedDescriptor)
+ return true;
+
+ return false;
+ }
+
+ bool shouldSkipHasAttachPtrMarking(mlir::omp::MapInfoOp mapOp) {
+ if (mapOp.getHasAttachPtr() || isAttachMap(mapOp))
+ return true;
+
+ mlir::omp::ClauseMapFlags mapType = mapOp.getMapType();
+ bool isRefPtrOnly =
+ bitEnumContainsAll(mapType, mlir::omp::ClauseMapFlags::ref_ptr) &&
+ !bitEnumContainsAll(mapType, mlir::omp::ClauseMapFlags::ref_ptee);
+ if (isRefPtrOnly)
+ return true;
+
+ if (isDescriptorOrPointerObjectMap(mapOp))
+ return true;
+
+ return false;
+ }
+
+ void markHasAttachPtr(mlir::omp::MapInfoOp mapOp,
+ fir::FirOpBuilder &builder) {
+ if (!shouldSkipHasAttachPtrMarking(mapOp))
+ mapOp.setHasAttachPtrAttr(builder.getBoolAttr(true));
+ }
+
+ /// Mark member MapInfoOps whose members_index path is structurally below
+ /// \p pointeeBlockIndexPath. These entries describe storage inside the
+ /// pointee block reached through an attach pointer, so they must carry
+ /// has_attach_ptr. The attach entry itself and descriptor/pointer-object maps
+ /// are left unmarked.
+ void
+ markMembersWithAttachPtrUnder(mlir::omp::MapInfoOp parentOp,
+ llvm::ArrayRef<int64_t> pointeeBlockIndexPath,
+ fir::FirOpBuilder &builder) {
+ mlir::ArrayAttr memberIndicesAttr = parentOp.getMembersIndexAttr();
+ if (!memberIndicesAttr)
+ return;
+
+ for (auto [memberIdx, memberIndexAttr] :
+ llvm::enumerate(memberIndicesAttr)) {
+ llvm::SmallVector<int64_t> memberIndexPath;
+ getAsIntegers(mlir::cast<mlir::ArrayAttr>(memberIndexAttr).getValue(),
+ memberIndexPath);
+ if (!isStrictPrefix(pointeeBlockIndexPath, memberIndexPath))
+ continue;
+
+ auto memberOp = mlir::dyn_cast_if_present<mlir::omp::MapInfoOp>(
+ parentOp.getMembers()[memberIdx].getDefiningOp());
+ if (!memberOp)
+ continue;
+
+ markHasAttachPtr(memberOp, builder);
+ }
+ }
+
+ bool isPointeeStorageRoot(mlir::omp::MapInfoOp mapOp) {
+ if (isAttachMap(mapOp))
+ return false;
+ return mapOp.getHasAttachPtr();
+ }
+
+ void propagateHasAttachPtrToNestedMembers(mlir::omp::MapInfoOp parentOp,
+ fir::FirOpBuilder &builder) {
+ mlir::ArrayAttr memberIndicesAttr = parentOp.getMembersIndexAttr();
+ if (!memberIndicesAttr)
+ return;
+
+ // For each member entry that is itself a pointee-storage root, mark all
----------------
TIFitis wrote:
Should we also be checking if the `parentOp` has `has_attach_ptr`?
For a top-level pointer `p`, `map(ref_ptee, to: p) map(to: p%x)` produces a marked parent owning an unmarked p%x member.
https://github.com/llvm/llvm-project/pull/236985
More information about the llvm-branch-commits
mailing list