[flang-commits] [flang] [Flang][OpenMP] Remove special USM/Close handling for c_ptr's in MapInfoFinalization (PR #219508)
via flang-commits
flang-commits at lists.llvm.org
Fri Aug 28 08:57:05 PDT 2026
https://github.com/agozillon created https://github.com/llvm/llvm-project/pull/219508
This no longer appears to be neccessary, as can be evidenced by the usm_map_close.f90 check-offload test still passing with the removal of the code. We also shouldn't be applying the close map type to things without a user specifying it explicitly as this will force data transferral in zero-copy mode.
We also don't need special handling for c_ptr's as they are just a record type with an i64 member, this means it will be mapped as a whole and does not need a member map, any map type applied to the top level record type gets applied to the member in this scenario. So, map(close, to: c_ptr) will encompass the address and outer layer without this segment of code currently.
>From 19f43c3e96da21cb64769ce444ffb5972aab9d6c Mon Sep 17 00:00:00 2001
From: agozillon <Andrew.Gozillon at amd.com>
Date: Fri, 28 Aug 2026 09:57:46 -0500
Subject: [PATCH] [Flang][OpenMP] Remove special USM/Close handling for c_ptr's
in MapInfoFinalization
This no longer appears to be neccessary, as can be evidenced by the usm_map_close.f90
check-offload test still passing with the removal of the code. We also shouldn't be
applying the close map type to things without a user specifying it explicitly as this
will force data transferral in zero-copy mode.
We also don't need special handling for c_ptr's as they are just a record type with an
i64 member, this means it will be mapped as a whole and does not need a member map, any
map type applied to the top level record type gets applied to the member in this scenario.
So, map(close, to: c_ptr) will encompass the address and outer layer without this segment
of code currently.
---
.../Optimizer/OpenMP/MapInfoFinalization.cpp | 88 -------------------
.../cptr-usm-close-and-use-device-ptr.f90 | 21 -----
2 files changed, 109 deletions(-)
delete mode 100644 flang/test/Lower/OpenMP/cptr-usm-close-and-use-device-ptr.f90
diff --git a/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp b/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
index 0fdc2e1278589..e78d194c5c7ae 100644
--- a/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
+++ b/flang/lib/Optimizer/OpenMP/MapInfoFinalization.cpp
@@ -266,17 +266,6 @@ class MapInfoFinalizationPass
}
}
- /// Return true if the module has an OpenMP requires clause that includes
- /// unified_shared_memory.
- static bool moduleRequiresUSM(mlir::ModuleOp module) {
- assert(module && "invalid module");
- if (auto req = module->getAttrOfType<mlir::omp::ClauseRequiresAttr>(
- "omp.requires"))
- return mlir::omp::bitEnumContainsAll(
- req.getValue(), mlir::omp::ClauseRequires::unified_shared_memory);
- return false;
- }
-
/// Create the member map for coordRef and append it (and its index
/// path) to the provided new* vectors, if it is not already present.
void appendMemberMapIfNew(
@@ -978,72 +967,6 @@ class MapInfoFinalizationPass
return implicitAttachMap;
}
- // Expand mappings of type(C_PTR) to map their `__address` field explicitly
- // as a single pointer-sized member (USM-gated at callsite). This helps in
- // USM scenarios to ensure the pointer-sized mapping is used.
- mlir::omp::MapInfoOp genCptrMemberMap(mlir::omp::MapInfoOp op,
- fir::FirOpBuilder &builder) {
- if (!op.getMembers().empty())
- return op;
-
- mlir::Type varTy = fir::unwrapRefType(op.getVarPtr().getType());
- if (!mlir::isa<fir::RecordType>(varTy))
- return op;
- auto recTy = mlir::cast<fir::RecordType>(varTy);
- // If not a builtin C_PTR record, skip.
- if (!recTy.getName().ends_with("__builtin_c_ptr"))
- return op;
-
- // Find the index of the c_ptr address component named "__address".
- int32_t fieldIdx = recTy.getFieldIndex("__address");
- if (fieldIdx < 0)
- return op;
-
- mlir::Location loc = op.getVarPtr().getLoc();
- mlir::Type memTy = recTy.getType(fieldIdx);
- fir::IntOrValue idxConst =
- mlir::IntegerAttr::get(builder.getI32Type(), fieldIdx);
- mlir::Value coord = fir::CoordinateOp::create(
- builder, loc, builder.getRefType(memTy), op.getVarPtr(),
- llvm::SmallVector<fir::IntOrValue, 1>{idxConst});
-
- // Child for the `__address` member.
- llvm::SmallVector<llvm::SmallVector<int64_t>> memberIdx = {{0}};
- mlir::ArrayAttr newMembersAttr = builder.create2DI64ArrayAttr(memberIdx);
- // Force CLOSE in USM paths so the pointer gets device-local placement
- // when required by tests relying on USM + close semantics.
- mlir::omp::ClauseMapFlagsAttr mapTypeAttr =
- builder.getAttr<mlir::omp::ClauseMapFlagsAttr>(
- op.getMapType() | mlir::omp::ClauseMapFlags::close);
-
- mlir::omp::MapInfoOp memberMap = mlir::omp::MapInfoOp::create(
- builder, loc, coord.getType(), coord,
- mlir::TypeAttr::get(fir::unwrapRefType(coord.getType())), mapTypeAttr,
- builder.getAttr<mlir::omp::VariableCaptureKindAttr>(
- mlir::omp::VariableCaptureKind::ByRef),
- /*varPtrPtr=*/mlir::Value{}, /*varPtrPtrType=*/mlir::TypeAttr{},
- /*members=*/llvm::SmallVector<mlir::Value>{},
- /*member_index=*/mlir::ArrayAttr{},
- /*bounds=*/op.getBounds(),
- /*mapperId=*/mlir::FlatSymbolRefAttr(),
- /*name=*/op.getNameAttr(),
- /*partial_map=*/builder.getBoolAttr(false));
-
- // Rebuild the parent as a container with the `__address` member.
- mlir::omp::MapInfoOp newParent = mlir::omp::MapInfoOp::create(
- builder, op.getLoc(), op.getResult().getType(), op.getVarPtr(),
- op.getVarPtrTypeAttr(), mapTypeAttr, op.getMapCaptureTypeAttr(),
- /*varPtrPtr=*/mlir::Value{}, mlir::TypeAttr{},
- /*members=*/llvm::SmallVector<mlir::Value>{memberMap},
- /*member_index=*/newMembersAttr,
- /*bounds=*/llvm::SmallVector<mlir::Value>{},
- /*mapperId=*/mlir::FlatSymbolRefAttr(), op.getNameAttr(),
- /*partial_map=*/builder.getBoolAttr(false));
- op.replaceAllUsesWith(newParent.getResult());
- op->erase();
- return newParent;
- }
-
// If the operation that we are expanding with a descriptor has a user
// (parent), then we have to expand the parent's member indices to reflect
// the adjusted member indices for the base address insertion. However, if
@@ -1714,17 +1637,6 @@ class MapInfoFinalizationPass
return mlir::WalkResult::advance();
});
- // Expand type(C_PTR) only when unified_shared_memory is required,
- // to ensure device-visible pointer size/behavior in USM scenarios
- // without changing default expectations elsewhere.
- func->walk([&](mlir::omp::MapInfoOp op) {
- // Only expand C_PTR members when unified_shared_memory is required.
- if (!moduleRequiresUSM(func->getParentOfType<mlir::ModuleOp>()))
- return;
- builder.setInsertionPoint(op);
- genCptrMemberMap(op, builder);
- });
-
func->walk([&](mlir::omp::MapInfoOp op) {
// NOTE: Currently only supports a single user for the MapInfoOp. This
// is fine for the moment, as the Fortran frontend will generate a
diff --git a/flang/test/Lower/OpenMP/cptr-usm-close-and-use-device-ptr.f90 b/flang/test/Lower/OpenMP/cptr-usm-close-and-use-device-ptr.f90
deleted file mode 100644
index ed07eca7b7aa9..0000000000000
--- a/flang/test/Lower/OpenMP/cptr-usm-close-and-use-device-ptr.f90
+++ /dev/null
@@ -1,21 +0,0 @@
-! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %s -o - | FileCheck %s
-!
-! Checks:
-! - C_PTR mappings expand to `__address` member with CLOSE under USM paths.
-! - use_device_ptr does not implicitly expand member operands in the clause.
-
-subroutine only_cptr_use_device_ptr
- use iso_c_binding
- type(c_ptr) :: cptr
- integer :: i
-
- !$omp target data use_device_ptr(cptr) map(tofrom: i)
- !$omp end target data
-end subroutine
-
-! CHECK-LABEL: func.func @_QPonly_cptr_use_device_ptr()
-! CHECK: %[[I_MAP:.*]] = omp.map.info var_ptr(%{{.*}} : !fir.ref<i32>, i32) map_clauses(tofrom) capture(ByRef) name("i") -> !fir.ref<i32>
-! CHECK: %[[CP_MAP:.*]] = omp.map.info var_ptr(%{{.*}} : !fir.ref<!fir.type<{{.*}}__builtin_c_ptr{{.*}}>>, !fir.type<{{.*}}__builtin_c_ptr{{.*}}>) map_clauses(return_param) capture(ByRef) name("cptr") -> !fir.ref<!fir.type<{{.*}}__builtin_c_ptr{{.*}}>>
-! CHECK: omp.target_data map_entries(%[[I_MAP]] : !fir.ref<i32>) use_device_ptr(%[[CP_MAP]] -> %{{.*}} : !fir.ref<!fir.type<{{.*}}__builtin_c_ptr{{.*}}>>) {
-! CHECK: omp.terminator
-! CHECK: }
More information about the flang-commits
mailing list