Skip to content

[flang][cuda] Update implicit data transfer for device component #147882

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
Jul 10, 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
19 changes: 1 addition & 18 deletions flang/include/flang/Evaluate/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -1359,24 +1359,7 @@ inline bool IsCUDADataTransfer(const A &lhs, const B &rhs) {

/// Check if the expression is a mix of host and device variables that require
/// implicit data transfer.
inline bool HasCUDAImplicitTransfer(const Expr<SomeType> &expr) {
unsigned hostSymbols{0};
unsigned deviceSymbols{0};
for (const Symbol &sym : CollectCudaSymbols(expr)) {
if (IsCUDADeviceSymbol(sym)) {
++deviceSymbols;
} else {
if (sym.owner().IsDerivedType()) {
if (IsCUDADeviceSymbol(sym.owner().GetSymbol()->GetUltimate())) {
++deviceSymbols;
}
}
++hostSymbols;
}
}
bool hasConstant{HasConstant(expr)};
return (hasConstant || (hostSymbols > 0)) && deviceSymbols > 0;
}
bool HasCUDAImplicitTransfer(const Expr<SomeType> &expr);

// Checks whether the symbol on the LHS is present in the RHS expression.
bool CheckForSymbolMatch(const Expr<SomeType> *lhs, const Expr<SomeType> *rhs);
Expand Down
25 changes: 25 additions & 0 deletions flang/lib/Evaluate/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,31 @@ template semantics::UnorderedSymbolSet CollectCudaSymbols(
template semantics::UnorderedSymbolSet CollectCudaSymbols(
const Expr<SubscriptInteger> &);

bool HasCUDAImplicitTransfer(const Expr<SomeType> &expr) {
semantics::UnorderedSymbolSet hostSymbols;
semantics::UnorderedSymbolSet deviceSymbols;

SymbolVector symbols{GetSymbolVector(expr)};
std::reverse(symbols.begin(), symbols.end());
bool skipNext{false};
for (const Symbol &sym : symbols) {
bool isComponent{sym.owner().IsDerivedType()};
bool skipComponent{false};
if (!skipNext) {
if (IsCUDADeviceSymbol(sym)) {
deviceSymbols.insert(sym);
} else if (isComponent) {
skipComponent = true; // Component is not device. Look on the base.
} else {
hostSymbols.insert(sym);
}
}
skipNext = isComponent && !skipComponent;
}
bool hasConstant{HasConstant(expr)};
return (hasConstant || (hostSymbols.size() > 0)) && deviceSymbols.size() > 0;
}

// HasVectorSubscript()
struct HasVectorSubscriptHelper
: public AnyTraverse<HasVectorSubscriptHelper, bool,
Expand Down
2 changes: 0 additions & 2 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4842,8 +4842,6 @@ class FirConverter : public Fortran::lower::AbstractConverter {
.detailsIf<Fortran::semantics::ObjectEntityDetails>()) {
if (details->cudaDataAttr() &&
*details->cudaDataAttr() != Fortran::common::CUDADataAttr::Pinned) {
if (sym.owner().IsDerivedType() && IsAllocatable(sym.GetUltimate()))
TODO(loc, "Device resident allocatable derived-type component");
// TODO: This should probably being checked in semantic and give a
// proper error.
assert(
Expand Down
17 changes: 17 additions & 0 deletions flang/test/Lower/CUDA/cuda-data-transfer.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module mod1
integer :: i
end type

type :: t2
integer, device, allocatable, dimension(:) :: x
end type

integer, device, dimension(11:20) :: cdev

contains
Expand Down Expand Up @@ -419,3 +423,16 @@ end subroutine
! CHECK: fir.do_concurrent.loop
! CHECK-NOT: cuf.data_transfer
! CHECK: hlfir.assign


subroutine sub22()
use mod1
type(t2) :: a
integer :: b(100)
allocate(a%x(100))

b = a%x
end subroutine

! CHECK-LABEL: func.func @_QPsub22()
! CHECK: cuf.data_transfer
Loading