Skip to content

Commit 4523a26

Browse files
authored
[flang][cuda] Enforce DEVICE attribute when ALLOCATE with STREAM option (#89459)
When the STREAM option is specified on an ALLOCATE statement, the object must have the DEVICE attribute.
1 parent 16e3464 commit 4523a26

File tree

6 files changed

+21
-8
lines changed

6 files changed

+21
-8
lines changed

flang/lib/Semantics/check-allocate.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,13 @@ bool AllocationCheckerHelper::RunChecks(SemanticsContext &context) {
618618
"Object in ALLOCATE must have PINNED attribute when PINNED option is specified"_err_en_US);
619619
}
620620
}
621+
if (allocateInfo_.gotStream) {
622+
std::optional<common::CUDADataAttr> cudaAttr{GetCUDADataAttr(ultimate_)};
623+
if (!cudaAttr || *cudaAttr != common::CUDADataAttr::Device) {
624+
context.Say(name_.source,
625+
"Object in ALLOCATE must have DEVICE attribute when STREAM option is specified"_err_en_US);
626+
}
627+
}
621628
return RunCoarrayRelatedChecks(context);
622629
}
623630

flang/test/Lower/CUDA/cuda-allocatable.cuf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,21 @@ end subroutine
6868
! CHECK: }
6969

7070
subroutine sub4()
71-
real, allocatable, unified :: a(:)
71+
real, allocatable, device :: a(:)
7272
integer :: istream
7373
allocate(a(10), stream=istream)
7474
end subroutine
7575

7676
! CHECK-LABEL: func.func @_QPsub4()
7777
! CHECK: %[[BOX:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?xf32>>> {bindc_name = "a", uniq_name = "_QFsub4Ea"}
78-
! CHECK: %[[BOX_DECL:.*]]:2 = hlfir.declare %0 {cuda_attr = #fir.cuda<unified>, fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFsub4Ea"} : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>) -> (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>)
78+
! CHECK: %[[BOX_DECL:.*]]:2 = hlfir.declare %0 {cuda_attr = #fir.cuda<device>, fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFsub4Ea"} : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>) -> (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>)
7979
! CHECK: %[[ISTREAM:.*]] = fir.alloca i32 {bindc_name = "istream", uniq_name = "_QFsub4Eistream"}
8080
! CHECK: %[[ISTREAM_DECL:.*]]:2 = hlfir.declare %[[ISTREAM]] {uniq_name = "_QFsub4Eistream"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
8181
! CHECK: fir.call @_FortranAAllocatableSetBounds
8282
! CHECK: %[[STREAM:.*]] = fir.load %[[ISTREAM_DECL]]#0 : !fir.ref<i32>
83-
! CHECK: %{{.*}} = fir.cuda_allocate %[[BOX_DECL]]#1 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>> stream(%[[STREAM]] : i32) {cuda_attr = #fir.cuda<unified>} -> i32
83+
! CHECK: %{{.*}} = fir.cuda_allocate %[[BOX_DECL]]#1 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>> stream(%[[STREAM]] : i32) {cuda_attr = #fir.cuda<device>} -> i32
8484
! CHECK: fir.if %{{.*}} {
85-
! CHECK: %{{.*}} = fir.cuda_deallocate %[[BOX_DECL]]#1 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>> {cuda_attr = #fir.cuda<unified>} -> i32
85+
! CHECK: %{{.*}} = fir.cuda_deallocate %[[BOX_DECL]]#1 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>> {cuda_attr = #fir.cuda<device>} -> i32
8686
! CHECK: }
8787

8888
subroutine sub5()

flang/test/Parser/cuf-sanity-common

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ module m
3232
call globalsub<<<1, 2>>>
3333
call globalsub<<<1, 2, 3>>>
3434
call globalsub<<<1, 2, 3, 4>>>
35-
allocate(pa(32), stream = 1, pinned = isPinned)
35+
allocate(pa(32), pinned = isPinned)
3636
end subroutine
3737
end module

flang/test/Parser/cuf-sanity-tree.CUF

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ include "cuf-sanity-common"
199199
!CHECK: | | | | | | AllocateShapeSpec
200200
!CHECK: | | | | | | | Scalar -> Integer -> Expr = '32_4'
201201
!CHECK: | | | | | | | | LiteralConstant -> IntLiteralConstant = '32'
202-
!CHECK: | | | | | AllocOpt -> Stream -> Scalar -> Integer -> Expr = '1_4'
203-
!CHECK: | | | | | | LiteralConstant -> IntLiteralConstant = '1'
204202
!CHECK: | | | | | AllocOpt -> Pinned -> Scalar -> Logical -> Variable = 'ispinned'
205203
!CHECK: | | | | | | Designator -> DataRef -> Name = 'ispinned'
206204
!CHECK: | | | EndSubroutineStmt ->

flang/test/Parser/cuf-sanity-unparse.CUF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ include "cuf-sanity-common"
3737
!CHECK: CALL globalsub<<<1_4,2_4>>>()
3838
!CHECK: CALL globalsub<<<1_4,2_4,3_4>>>()
3939
!CHECK: CALL globalsub<<<1_4,2_4,3_4,4_4>>>()
40-
!CHECK: ALLOCATE(pa(32_4), STREAM=1_4, PINNED=ispinned)
40+
!CHECK: ALLOCATE(pa(32_4), PINNED=ispinned)
4141
!CHECK: END SUBROUTINE
4242
!CHECK: END MODULE

flang/test/Semantics/cuf07.cuf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ module m
3131
!ERROR: Object in ALLOCATE must have PINNED attribute when PINNED option is specified
3232
allocate(ia(100), pinned = plog)
3333
end subroutine
34+
35+
subroutine host2()
36+
integer, allocatable, pinned :: ia(:)
37+
integer :: istream
38+
39+
!ERROR: Object in ALLOCATE must have DEVICE attribute when STREAM option is specified
40+
allocate(ia(100), stream = istream)
41+
end subroutine
3442
end module

0 commit comments

Comments
 (0)