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
6 changes: 5 additions & 1 deletion flang/include/flang/Evaluate/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,9 @@ extern template semantics::UnorderedSymbolSet CollectCudaSymbols(
// Predicate: does a variable contain a vector-valued subscript (not a triplet)?
bool HasVectorSubscript(const Expr<SomeType> &);

// Predicate: does an expression contain constant?
bool HasConstant(const Expr<SomeType> &);

// Utilities for attaching the location of the declaration of a symbol
// of interest to a message. Handles the case of USE association gracefully.
parser::Message *AttachDeclaration(parser::Message &, const Symbol &);
Expand Down Expand Up @@ -1319,7 +1322,8 @@ inline bool HasCUDAImplicitTransfer(const Expr<SomeType> &expr) {
++hostSymbols;
}
}
return hostSymbols > 0 && deviceSymbols > 0;
bool hasConstant{HasConstant(expr)};
return (hasConstant || (hostSymbols > 0)) && deviceSymbols > 0;
}

} // namespace Fortran::evaluate
Expand Down
17 changes: 17 additions & 0 deletions flang/lib/Evaluate/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,23 @@ bool HasVectorSubscript(const Expr<SomeType> &expr) {
return HasVectorSubscriptHelper{}(expr);
}

// HasConstant()
struct HasConstantHelper : public AnyTraverse<HasConstantHelper, bool,
/*TraverseAssocEntityDetails=*/false> {
using Base = AnyTraverse<HasConstantHelper, bool, false>;
HasConstantHelper() : Base{*this} {}
using Base::operator();
template <typename T> bool operator()(const Constant<T> &) const {
return true;
}
// Only look for constant not in subscript.
bool operator()(const Subscript &) const { return false; }
};

bool HasConstant(const Expr<SomeType> &expr) {
return HasConstantHelper{}(expr);
}

parser::Message *AttachDeclaration(
parser::Message &message, const Symbol &symbol) {
const Symbol *unhosted{&symbol};
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4416,6 +4416,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
bool hasCUDAImplicitTransfer =
Fortran::evaluate::HasCUDAImplicitTransfer(assign.rhs);
llvm::SmallVector<mlir::Value> implicitTemps;

if (hasCUDAImplicitTransfer && !isInDeviceContext)
implicitTemps = genCUDAImplicitDataTransfer(builder, loc, assign);

Expand Down
15 changes: 14 additions & 1 deletion flang/test/Lower/CUDA/cuda-data-transfer.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ subroutine sub1()
adev = 10

cdev = 0

end

! CHECK-LABEL: func.func @_QPsub1()
Expand Down Expand Up @@ -381,3 +380,17 @@ end subroutine

! CHECK-LABEL: func.func @_QPsub18
! CHECK-NOT: cuf.data_transfer

subroutine sub19()
integer, device :: adev(10)
integer :: ahost(10)
! Implicit data transfer of adev and then addition on the host
ahost = adev + 2
end subroutine

! CHECK-LABEL: func.func @_QPsub19()
! CHECK: %[[ADEV_DECL:.*]]:2 = hlfir.declare %{{.*}}(%{{.*}}) {data_attr = #cuf.cuda<device>, uniq_name = "_QFsub19Eadev"} : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<10xi32>>, !fir.ref<!fir.array<10xi32>>)
! CHECK: %[[ALLOC_TMP:.*]] = fir.allocmem !fir.array<10xi32> {bindc_name = ".tmp", uniq_name = ""}
! CHECK: %[[TMP:.*]]:2 = hlfir.declare %[[ALLOC_TMP]](%{{.*}}) {uniq_name = ".tmp"} : (!fir.heap<!fir.array<10xi32>>, !fir.shape<1>) -> (!fir.heap<!fir.array<10xi32>>, !fir.heap<!fir.array<10xi32>>)
! CHECK: cuf.data_transfer %[[ADEV_DECL]]#1 to %[[TMP]]#0 {transfer_kind = #cuf.cuda_transfer<device_host>} : !fir.ref<!fir.array<10xi32>>, !fir.heap<!fir.array<10xi32>>
! CHECL: hlfir.assign