Skip to content

[flang][cuda] Avoid generating cuf.data_transfer in OpenACC region #106435

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 2 commits into from
Aug 29, 2024
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: 8 additions & 2 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4380,9 +4380,14 @@ class FirConverter : public Fortran::lower::AbstractConverter {
// Check if the insertion point is currently in a device context. HostDevice
// subprogram are not considered fully device context so it will return false
// for it.
static bool isDeviceContext(fir::FirOpBuilder &builder) {
// If the insertion point is inside an OpenACC region op, it is considered
// device context.
static bool isCudaDeviceContext(fir::FirOpBuilder &builder) {
if (builder.getRegion().getParentOfType<cuf::KernelOp>())
return true;
if (builder.getRegion()
.getParentOfType<mlir::acc::ComputeRegionOpInterface>())
return true;
if (auto funcOp =
builder.getRegion().getParentOfType<mlir::func::FuncOp>()) {
if (auto cudaProcAttr =
Expand All @@ -4401,7 +4406,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
mlir::Location loc = getCurrentLocation();
fir::FirOpBuilder &builder = getFirOpBuilder();

bool isInDeviceContext = isDeviceContext(builder);
bool isInDeviceContext = isCudaDeviceContext(builder);

bool isCUDATransfer = (Fortran::evaluate::HasCUDADeviceAttrs(assign.lhs) ||
Fortran::evaluate::HasCUDADeviceAttrs(assign.rhs)) &&
!isInDeviceContext;
Expand Down
44 changes: 43 additions & 1 deletion flang/test/Lower/CUDA/cuda-data-transfer.cuf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s
! RUN: bbc -emit-hlfir -fopenacc -fcuda %s -o - | FileCheck %s

! Test CUDA Fortran data transfer using assignment statements.

Expand Down Expand Up @@ -290,3 +290,45 @@ end subroutine
! CHECK: %[[SHAPE:.*]] = fir.shape %{{.*}} : (index) -> !fir.shape<1>
! CHECK: %[[AHOST:.*]]:2 = hlfir.declare %[[ARG1]](%{{.*}}) dummy_scope %{{.*}} {uniq_name = "_QFsub15Ea_host"} : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, !fir.dscope) -> (!fir.box<!fir.array<?xf32>>, !fir.ref<!fir.array<?xf32>>)
! CHECK: cuf.data_transfer %[[AHOST]]#1 to %[[ADEV]]#1, %[[SHAPE]] : !fir.shape<1> {transfer_kind = #cuf.cuda_transfer<host_device>} : !fir.ref<!fir.array<?xf32>>, !fir.ref<!fir.array<?xf32>>

! Check that cuf.data_transfer are not generated within OpenACC region
subroutine sub16()
integer, parameter :: n = 10
real, device :: adev(n)
real :: ahost(n)
real, managed :: b
integer :: i

adev = ahost
!$acc parallel loop deviceptr(adev)
do i = 1, n
adev(i) = adev(i) + b
enddo

!$acc kernels deviceptr(adev)
do i = 1, n
adev(i) = adev(i) + b
enddo
!$acc end kernels


!$acc serial deviceptr(adev)
do i = 1, n
adev(i) = adev(i) + b
enddo
!$acc end serial
end subroutine

! CHECK-LABEL: func.func @_QPsub16()
! CHECK: cuf.data_transfer
! CHECK: acc.parallel
! CHECK-NOT: cuf.data_transfer
! CHECK: hlfir.assign

! CHECK: acc.kernels
! CHECK-NOT: cuf.data_transfer
! CHECK: hlfir.assign

! CHECK: acc.serial
! CHECK-NOT: cuf.data_transfer
! CHECK: hlfir.assign
Loading