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
10 changes: 6 additions & 4 deletions flang/include/flang/Evaluate/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -1342,10 +1342,12 @@ inline bool IsCUDADataTransfer(const A &lhs, const B &rhs) {
int rhsNbManagedSymbols = {GetNbOfCUDAManagedOrUnifiedSymbols(rhs)};
int rhsNbSymbols{GetNbOfCUDADeviceSymbols(rhs)};

// Special case where only managed or unifed symbols are involved. This is
// performed on the host.
if (lhsNbManagedSymbols == 1 && rhsNbManagedSymbols == 1 &&
rhsNbSymbols == 1) {
// Special cases perforemd on the host:
// - Only managed or unifed symbols are involved on RHS and LHS.
// - LHS is managed or unified and the RHS is host only.
if ((lhsNbManagedSymbols == 1 && rhsNbManagedSymbols == 1 &&
rhsNbSymbols == 1) ||
(lhsNbManagedSymbols == 1 && rhsNbSymbols == 0)) {
return false;
}
return HasCUDADeviceAttrs(lhs) || rhsNbSymbols > 0;
Expand Down
20 changes: 20 additions & 0 deletions flang/test/Lower/CUDA/cuda-data-transfer.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ module mod1

real(kind=8), device, allocatable, dimension(:) :: p

interface
function __sum(a_d) result(res_h)
integer(4), managed, intent(in) :: a_d(:,:,:,:)
integer(4), allocatable, managed :: res_h(:,:,:)
end function
end interface

contains
function dev1(a)
integer, device :: a(:)
Expand Down Expand Up @@ -522,3 +529,16 @@ end subroutine
! CHECK: hlfir.yield_element %[[CONV]] : f32
! CHECK: }
! CHECKL: hlfir.assign %[[ELE]] to %[[HD]]#0 : !hlfir.expr<10x20x30xf32>, !fir.ref<!fir.array<10x20x30xf32>>

subroutine sub28(N1,N2,N3,N4)
use mod1
integer(4), managed :: a(N1,N2,N3,N4)
integer(4), managed :: bres(N1,N2,N3)
bres = __sum(a)
end subroutine

! CHECK-LABEL: func.func @_QPsub28
! CHECK: fir.call @_QP__sum
! CHECK-NOT: cuf.data_transfer
! CHECK: hlfir.assign
! CHECK-NOT: cuf.data_transfer
20 changes: 8 additions & 12 deletions flang/test/Lower/CUDA/cuda-managed.cuf
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s

! Check for implicit data transfer of managed variable

subroutine testr2(N1,N2)
real(4), managed :: ai4(N1,N2)
real(4), allocatable :: bRefi4(:)

integer :: i1, i2

do i2 = 1, N2
do i1 = 1, N1
ai4(i1,i2) = i1 + N1*(i2-1)
enddo
enddo

allocate(bRefi4 (N1))
allocate(bRefi4(N1))
do i1 = 1, N1
bRefi4(i1) = (ai4(i1,1)+ai4(i1,N2))*N2/2
enddo
deallocate(bRefi4)

end subroutine

!CHECK-LABEL: func.func @_QPtestr2
!CHECK: %[[ALLOC:.*]] = cuf.alloc !fir.array<?x?xf32>, %{{.*}}, %{{.*}} : index, index {bindc_name = "ai4", data_attr = #cuf.cuda<managed>, uniq_name = "_QFtestr2Eai4"} -> !fir.ref<!fir.array<?x?xf32>>
!CHECK: %[[DECLARE:.*]]:2 = hlfir.declare %[[ALLOC]](%{{.*}}) {data_attr = #cuf.cuda<managed>, uniq_name = "_QFtestr2Eai4"} : (!fir.ref<!fir.array<?x?xf32>>, !fir.shape<2>) -> (!fir.box<!fir.array<?x?xf32>>, !fir.ref<!fir.array<?x?xf32>>)
!CHECK: %[[DEST:.*]] = hlfir.designate %[[DECLARE]]#0 (%{{.*}}, %{{.*}}) : (!fir.box<!fir.array<?x?xf32>>, i64, i64) -> !fir.ref<f32>
!CHECK: cuf.data_transfer %{{.*}}#0 to %[[DEST]] {transfer_kind = #cuf.cuda_transfer<host_device>} : !fir.ref<f32>, !fir.ref<f32>
! CHECK-LABEL: func.func @_QPtestr2
! CHECK: %[[MANAGED:.*]]:2 = hlfir.declare %22(%23) {data_attr = #cuf.cuda<managed>, uniq_name = "_QFtestr2Eai4"} : (!fir.ref<!fir.array<?x?xf32>>, !fir.shape<2>) -> (!fir.box<!fir.array<?x?xf32>>, !fir.ref<!fir.array<?x?xf32>>)
! CHECK: %[[TMP:.*]] = fir.allocmem !fir.array<?x?xf32>, %16, %21 {bindc_name = ".tmp", uniq_name = ""}
! CHECK: %[[TMP_DECL:.*]]:2 = hlfir.declare %[[TMP]](%{{.*}}) {uniq_name = ".tmp"} : (!fir.heap<!fir.array<?x?xf32>>, !fir.shape<2>) -> (!fir.box<!fir.array<?x?xf32>>, !fir.heap<!fir.array<?x?xf32>>)
! CHECK: cuf.data_transfer %[[MANAGED]]#1 to %[[TMP_DECL]]#0 {transfer_kind = #cuf.cuda_transfer<device_host>} : !fir.ref<!fir.array<?x?xf32>>, !fir.box<!fir.array<?x?xf32>>