Skip to content
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 @@ -656,6 +656,8 @@ DirectComponentIterator::const_iterator FindAllocatableOrPointerDirectComponent(
const DerivedTypeSpec &);
PotentialComponentIterator::const_iterator
FindPolymorphicAllocatablePotentialComponent(const DerivedTypeSpec &);
UltimateComponentIterator::const_iterator
FindCUDADeviceAllocatableUltimateComponent(const DerivedTypeSpec &);

// The LabelEnforce class (given a set of labels) provides an error message if
// there is a branch to a label which is not in the given set.
Expand Down
27 changes: 25 additions & 2 deletions flang/lib/Lower/ConvertVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,29 @@ static void instantiateGlobal(Fortran::lower::AbstractConverter &converter,
mapSymbolAttributes(converter, var, symMap, stmtCtx, cast);
}

bool needCUDAAlloc(const Fortran::semantics::Symbol &sym) {
if (Fortran::semantics::IsDummy(sym))
return false;
if (const auto *details{
sym.GetUltimate()
.detailsIf<Fortran::semantics::ObjectEntityDetails>()}) {
if (details->cudaDataAttr() &&
(*details->cudaDataAttr() == Fortran::common::CUDADataAttr::Device ||
*details->cudaDataAttr() == Fortran::common::CUDADataAttr::Managed ||
*details->cudaDataAttr() == Fortran::common::CUDADataAttr::Unified ||
*details->cudaDataAttr() == Fortran::common::CUDADataAttr::Shared ||
*details->cudaDataAttr() == Fortran::common::CUDADataAttr::Pinned))
return true;
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;
}

//===----------------------------------------------------------------===//
// Local variables instantiation (not for alias)
//===----------------------------------------------------------------===//
Expand Down Expand Up @@ -732,7 +755,7 @@ static mlir::Value createNewLocal(Fortran::lower::AbstractConverter &converter,
if (ultimateSymbol.test(Fortran::semantics::Symbol::Flag::CrayPointee))
return builder.create<fir::ZeroOp>(loc, fir::ReferenceType::get(ty));

if (Fortran::semantics::NeedCUDAAlloc(ultimateSymbol)) {
if (needCUDAAlloc(ultimateSymbol)) {
cuf::DataAttributeAttr dataAttr =
Fortran::lower::translateSymbolCUFDataAttribute(builder.getContext(),
ultimateSymbol);
Expand Down Expand Up @@ -1087,7 +1110,7 @@ static void instantiateLocal(Fortran::lower::AbstractConverter &converter,
Fortran::lower::defaultInitializeAtRuntime(converter, var.getSymbol(),
symMap);
auto *builder = &converter.getFirOpBuilder();
if (Fortran::semantics::NeedCUDAAlloc(var.getSymbol()) &&
if (needCUDAAlloc(var.getSymbol()) &&
!cuf::isCUDADeviceContext(builder->getRegion())) {
cuf::DataAttributeAttr dataAttr =
Fortran::lower::translateSymbolCUFDataAttribute(builder->getContext(),
Expand Down
23 changes: 17 additions & 6 deletions flang/lib/Semantics/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1081,12 +1081,6 @@ const Scope *FindCUDADeviceContext(const Scope *scope) {
});
}

std::optional<common::CUDADataAttr> GetCUDADataAttr(const Symbol *symbol) {
const auto *object{
symbol ? symbol->detailsIf<ObjectEntityDetails>() : nullptr};
return object ? object->cudaDataAttr() : std::nullopt;
}

bool IsDeviceAllocatable(const Symbol &symbol) {
if (IsAllocatable(symbol)) {
if (const auto *details{
Expand Down Expand Up @@ -1133,6 +1127,23 @@ bool CanCUDASymbolBeGlobal(const Symbol &sym) {
return true;
}

std::optional<common::CUDADataAttr> GetCUDADataAttr(const Symbol *symbol) {
const auto *details{
symbol ? symbol->detailsIf<ObjectEntityDetails>() : nullptr};
if (details) {
const Fortran::semantics::DeclTypeSpec *type{details->type()};
const Fortran::semantics::DerivedTypeSpec *derived{
type ? type->AsDerived() : nullptr};
if (derived) {
if (FindCUDADeviceAllocatableUltimateComponent(*derived)) {
return common::CUDADataAttr::Managed;
}
}
return details->cudaDataAttr();
}
return std::nullopt;
}

bool IsAccessible(const Symbol &original, const Scope &scope) {
const Symbol &ultimate{original.GetUltimate()};
if (ultimate.attrs().test(Attr::PRIVATE)) {
Expand Down
14 changes: 12 additions & 2 deletions flang/test/Lower/CUDA/cuda-derived.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ module m1

type t1; real, device, allocatable :: a(:); end type
type t2; type(t1) :: b; end type
contains
subroutine sub1()
type(ty_device) :: a
end subroutine

! CHECK-LABEL: func.func @_QMm1Psub1()
! CHECK: %[[ALLOC:.*]] = cuf.alloc !fir.type<_QMm1Tty_device{x:!fir.box<!fir.heap<!fir.array<?xi32>>>}> {bindc_name = "a", data_attr = #cuf.cuda<managed>, uniq_name = "_QMm1Fsub1Ea"} -> !fir.ref<!fir.type<_QMm1Tty_device{x:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
! CHECK: %[[DECL:.*]]:2 = hlfir.declare %[[ALLOC]] {data_attr = #cuf.cuda<managed>, uniq_name = "_QMm1Fsub1Ea"} : (!fir.ref<!fir.type<_QMm1Tty_device{x:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> (!fir.ref<!fir.type<_QMm1Tty_device{x:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.ref<!fir.type<_QMm1Tty_device{x:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>)
! CHECK: cuf.free %[[DECL]]#0 : !fir.ref<!fir.type<_QMm1Tty_device{x:!fir.box<!fir.heap<!fir.array<?xi32>>>}>> {data_attr = #cuf.cuda<managed>}

end module

program main
Expand All @@ -16,5 +26,5 @@ program main
end

! CHECK-LABEL: func.func @_QQmain() attributes {fir.bindc_name = "main"}
! CHECK: %{{.*}} = fir.alloca !fir.type<_QMm1Tty_device{x:!fir.box<!fir.heap<!fir.array<?xi32>>>}> {bindc_name = "a", uniq_name = "_QFEa"}
! CHECK: %{{.*}} = fir.alloca !fir.type<_QMm1Tt2{b:!fir.type<_QMm1Tt1{a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>}> {bindc_name = "b", uniq_name = "_QFEb"}
! CHECK: %{{.*}} = cuf.alloc !fir.type<_QMm1Tty_device{x:!fir.box<!fir.heap<!fir.array<?xi32>>>}> {bindc_name = "a", data_attr = #cuf.cuda<managed>, uniq_name = "_QFEa"}
! CHECK: %{{.*}} = cuf.alloc !fir.type<_QMm1Tt2{b:!fir.type<_QMm1Tt1{a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>}> {bindc_name = "b", data_attr = #cuf.cuda<managed>, uniq_name = "_QFEb"}
Loading