Skip to content

[flang][cuda] Generate cuf.allocate for descriptor with CUDA components #152041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions flang/include/flang/Semantics/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ inline bool HasCUDAAttr(const Symbol &sym) {
return false;
}

bool HasCUDAComponent(const Symbol &sym);

inline bool IsCUDAShared(const Symbol &sym) {
if (const auto *details{sym.GetUltimate().detailsIf<ObjectEntityDetails>()}) {
if (details->cudaDataAttr() &&
Expand Down
8 changes: 5 additions & 3 deletions flang/lib/Lower/Allocatable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,9 @@ class AllocateStmtHelper {

void genSimpleAllocation(const Allocation &alloc,
const fir::MutableBoxValue &box) {
bool isCudaSymbol = Fortran::semantics::HasCUDAAttr(alloc.getSymbol());
bool isCudaAllocate =
Fortran::semantics::HasCUDAAttr(alloc.getSymbol()) ||
Fortran::semantics::HasCUDAComponent(alloc.getSymbol());
bool isCudaDeviceContext = cuf::isCUDADeviceContext(builder.getRegion());
bool inlineAllocation = !box.isDerived() && !errorManager.hasStatSpec() &&
!alloc.type.IsPolymorphic() &&
Expand All @@ -475,7 +477,7 @@ class AllocateStmtHelper {
unsigned allocatorIdx = Fortran::lower::getAllocatorIdx(alloc.getSymbol());

if (inlineAllocation &&
((isCudaSymbol && isCudaDeviceContext) || !isCudaSymbol)) {
((isCudaAllocate && isCudaDeviceContext) || !isCudaAllocate)) {
// Pointers must use PointerAllocate so that their deallocations
// can be validated.
genInlinedAllocation(alloc, box);
Expand All @@ -494,7 +496,7 @@ class AllocateStmtHelper {
genSetDeferredLengthParameters(alloc, box);
genAllocateObjectBounds(alloc, box);
mlir::Value stat;
if (!isCudaSymbol) {
if (!isCudaAllocate) {
stat = genRuntimeAllocate(builder, loc, box, errorManager);
setPinnedToFalse();
} else {
Expand Down
6 changes: 4 additions & 2 deletions flang/lib/Lower/ConvertVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,8 +814,10 @@ initializeDeviceComponentAllocator(Fortran::lower::AbstractConverter &converter,
baseTy = boxTy.getEleTy();
baseTy = fir::unwrapRefType(baseTy);

if (mlir::isa<fir::SequenceType>(baseTy))
TODO(loc, "array of derived-type with device component");
if (mlir::isa<fir::SequenceType>(baseTy) &&
(fir::isAllocatableType(fir::getBase(exv).getType()) ||
fir::isPointerType(fir::getBase(exv).getType())))
return; // Allocator index need to be set after allocation.

auto recTy =
mlir::dyn_cast<fir::RecordType>(fir::unwrapSequenceType(baseTy));
Expand Down
15 changes: 15 additions & 0 deletions flang/lib/Semantics/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,21 @@ bool IsDeviceAllocatable(const Symbol &symbol) {
return false;
}

bool HasCUDAComponent(const Symbol &symbol) {
if (const auto *details{symbol.GetUltimate()
.detailsIf<Fortran::semantics::ObjectEntityDetails>()}) {
const Fortran::semantics::DeclTypeSpec *type{details->type()};
const Fortran::semantics::DerivedTypeSpec *derived{
type ? type->AsDerived() : nullptr};
if (derived) {
if (FindCUDADeviceAllocatableUltimateComponent(*derived)) {
return true;
}
}
}
return false;
}

UltimateComponentIterator::const_iterator
FindCUDADeviceAllocatableUltimateComponent(const DerivedTypeSpec &derived) {
UltimateComponentIterator ultimates{derived};
Expand Down
13 changes: 13 additions & 0 deletions flang/test/Lower/CUDA/cuda-allocatable.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module globals
real, device, allocatable :: a_device(:)
real, managed, allocatable :: a_managed(:)
real, pinned, allocatable :: a_pinned(:)
type :: t1
integer :: a
real, dimension(:), allocatable, device :: b
end type
end module

! CHECK-LABEL: fir.global @_QMglobalsEa_device {data_attr = #cuf.cuda<device>} : !fir.box<!fir.heap<!fir.array<?xf32>>>
Expand Down Expand Up @@ -222,3 +226,12 @@ end
! CHECK: %[[FALSE:.*]] = arith.constant false
! CHECK: %[[FLASE_CONV:.*]] = fir.convert %[[FALSE]] : (i1) -> !fir.logical<4>
! CHECK: fir.store %[[FLASE_CONV]] to %[[PLOG_DECL]]#0 : !fir.ref<!fir.logical<4>>

subroutine cuda_component()
use globals
type(t1), pointer, dimension(:) :: d
allocate(d(10))
end subroutine

! CHECK-LABEL: func.func @_QPcuda_component()
! CHECK: cuf.allocate