Skip to content

Commit 16975ad

Browse files
authored
[flang][cuda] Emit error when host array is used in CUF kernel (#100693)
Restriction from the standard 2.11.2. Arrays used or assigned in the loop must have the device, managed or unifed attribute.
1 parent 4826079 commit 16975ad

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

flang/lib/Semantics/check-cuda.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,25 @@ template <bool IsCUFKernelDo> class DeviceContextChecker {
307307
WarnOnIoStmt(source);
308308
}
309309
}
310+
template <typename A>
311+
void ErrorIfHostSymbol(const A &expr, const parser::CharBlock &source) {
312+
for (const Symbol &sym : CollectCudaSymbols(expr)) {
313+
if (const auto *details =
314+
sym.GetUltimate().detailsIf<semantics::ObjectEntityDetails>()) {
315+
if (details->IsArray() &&
316+
(!details->cudaDataAttr() ||
317+
(details->cudaDataAttr() &&
318+
*details->cudaDataAttr() != common::CUDADataAttr::Device &&
319+
*details->cudaDataAttr() != common::CUDADataAttr::Managed &&
320+
*details->cudaDataAttr() !=
321+
common::CUDADataAttr::Unified))) {
322+
context_.Say(source,
323+
"Host array '%s' cannot be present in CUF kernel"_err_en_US,
324+
sym.name());
325+
}
326+
}
327+
}
328+
}
310329
void Check(const parser::ActionStmt &stmt, const parser::CharBlock &source) {
311330
common::visit(
312331
common::visitors{
@@ -349,6 +368,19 @@ template <bool IsCUFKernelDo> class DeviceContextChecker {
349368
[&](const common::Indirection<parser::IfStmt> &x) {
350369
Check(x.value());
351370
},
371+
[&](const common::Indirection<parser::AssignmentStmt> &x) {
372+
if (IsCUFKernelDo) {
373+
const evaluate::Assignment *assign{
374+
semantics::GetAssignment(x.value())};
375+
if (assign) {
376+
ErrorIfHostSymbol(assign->lhs, source);
377+
ErrorIfHostSymbol(assign->rhs, source);
378+
}
379+
}
380+
if (auto msg{ActionStmtChecker<IsCUFKernelDo>::WhyNotOk(x)}) {
381+
context_.Say(source, std::move(*msg));
382+
}
383+
},
352384
[&](const auto &x) {
353385
if (auto msg{ActionStmtChecker<IsCUFKernelDo>::WhyNotOk(x)}) {
354386
context_.Say(source, std::move(*msg));

flang/test/Lower/CUDA/cuda-data-transfer.cuf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ subroutine sub4()
133133
integer, parameter :: n = 10
134134
real, device :: adev(n)
135135
real :: ahost(n)
136-
real :: b
136+
real, managed :: b
137137
integer :: i
138138

139139
adev = ahost

flang/test/Lower/CUDA/cuda-kernel-loop-directive.cuf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ subroutine sub1()
77
integer :: i, j
88
integer, parameter :: n = 100
99
integer(8) :: istream
10-
real :: a(n), b(n)
11-
real :: c(n,n), d(n,n)
10+
real, device :: a(n), b(n)
11+
real, device :: c(n,n), d(n,n)
1212

1313
! CHECK-LABEL: func.func @_QPsub1()
1414
! CHECK: %[[IV:.*]]:2 = hlfir.declare %{{.*}} {uniq_name = "_QFsub1Ei"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)

flang/test/Parser/cuf-sanity-common

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module m
2323
end subroutine
2424
subroutine test
2525
logical isPinned
26-
real a(10), x, y, z
26+
real, device :: a(10)
27+
real :: x, y, z
2728
!$cuf kernel do(1) <<<*, *, stream = 1>>>
2829
do j = 1, 10
2930
end do

flang/test/Semantics/cuf09.cuf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module m
1818
end
1919

2020
program main
21+
integer, device :: a_d(10 ,10)
22+
integer :: b(10, 10)
2123
!$cuf kernel do <<< *, * >>> ! ok
2224
do j = 1, 0
2325
end do
@@ -90,4 +92,12 @@ program main
9092
else if (ifunc() /= 1) then
9193
end if
9294
end do
95+
96+
!$cuf kernel do (2) <<<*, *>>>
97+
do j = 1, 10
98+
do i = 1, 10
99+
!ERROR: Host array 'b' cannot be present in CUF kernel
100+
a_d(i,j) = b(i,j)
101+
enddo
102+
enddo
93103
end

flang/test/Semantics/reduce.cuf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
! RUN: %python %S/test_errors.py %s %flang_fc1
22
subroutine s(n,m,a,l)
33
integer, intent(in) :: n
4-
integer, intent(in) :: m(n)
5-
real, intent(in) :: a(n)
6-
logical, intent(in) :: l(n)
4+
integer, device, intent(in) :: m(n)
5+
real, device, intent(in) :: a(n)
6+
logical, device, intent(in) :: l(n)
77
integer j, mr
88
real ar
99
logical lr

0 commit comments

Comments
 (0)