Skip to content

Commit 99f31ba

Browse files
authored
[flang][cuda] Fix semantic for the CONSTANT attribute (#82821)
Object with the CONSTANT attribute cannot be declared in the host subprogram. It can be declared in a module or a device subprogram. Adapt the semantic check to trigger the error in host subprogram.
1 parent ae91a42 commit 99f31ba

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

flang/lib/Semantics/check-declarations.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,12 @@ void CheckHelper::CheckObjectEntity(
920920
auto attr{*details.cudaDataAttr()};
921921
switch (attr) {
922922
case common::CUDADataAttr::Constant:
923-
if (IsAllocatableOrPointer(symbol) || symbol.attrs().test(Attr::TARGET)) {
923+
if (subpDetails && !inDeviceSubprogram) {
924+
messages_.Say(
925+
"Object '%s' with ATTRIBUTES(CONSTANT) may not be declared in a host subprogram"_err_en_US,
926+
symbol.name());
927+
} else if (IsAllocatableOrPointer(symbol) ||
928+
symbol.attrs().test(Attr::TARGET)) {
924929
messages_.Say(
925930
"Object '%s' with ATTRIBUTES(CONSTANT) may not be allocatable, pointer, or target"_err_en_US,
926931
symbol.name());

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,20 @@ module cuda_var
1616
contains
1717

1818
subroutine local_var_attrs
19-
real, constant :: rc
2019
real, device :: rd
2120
real, allocatable, managed :: rm
2221
real, allocatable, pinned :: rp
2322
end subroutine
2423

2524
! CHECK-LABEL: func.func @_QMcuda_varPlocal_var_attrs()
26-
! CHECK: %{{.*}}:2 = hlfir.declare %{{.*}} {cuda_attr = #fir.cuda<constant>, uniq_name = "_QMcuda_varFlocal_var_attrsErc"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>)
2725
! CHECK: %{{.*}}:2 = hlfir.declare %{{.*}} {cuda_attr = #fir.cuda<device>, uniq_name = "_QMcuda_varFlocal_var_attrsErd"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>)
2826
! CHECK: %{{.*}}:2 = hlfir.declare %{{.*}} {cuda_attr = #fir.cuda<managed>, fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QMcuda_varFlocal_var_attrsErm"} : (!fir.ref<!fir.box<!fir.heap<f32>>>) -> (!fir.ref<!fir.box<!fir.heap<f32>>>, !fir.ref<!fir.box<!fir.heap<f32>>>)
2927
! CHECK: %{{.*}}:2 = hlfir.declare %{{.*}} {cuda_attr = #fir.cuda<pinned>, fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QMcuda_varFlocal_var_attrsErp"} : (!fir.ref<!fir.box<!fir.heap<f32>>>) -> (!fir.ref<!fir.box<!fir.heap<f32>>>, !fir.ref<!fir.box<!fir.heap<f32>>>)
3028

31-
! FIR: %{{.*}} = fir.declare %{{.*}} {cuda_attr = #fir.cuda<constant>, uniq_name = "_QMcuda_varFlocal_var_attrsErc"} : (!fir.ref<f32>) -> !fir.ref<f32>
3229
! FIR: %{{.*}} = fir.declare %{{.*}} {cuda_attr = #fir.cuda<device>, uniq_name = "_QMcuda_varFlocal_var_attrsErd"} : (!fir.ref<f32>) -> !fir.ref<f32>
3330
! FIR: %{{.*}} = fir.declare %{{.*}} {cuda_attr = #fir.cuda<managed>, fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QMcuda_varFlocal_var_attrsErm"} : (!fir.ref<!fir.box<!fir.heap<f32>>>) -> !fir.ref<!fir.box<!fir.heap<f32>>>
3431
! FIR: %{{.*}} = fir.declare %{{.*}} {cuda_attr = #fir.cuda<pinned>, fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QMcuda_varFlocal_var_attrsErp"} : (!fir.ref<!fir.box<!fir.heap<f32>>>) -> !fir.ref<!fir.box<!fir.heap<f32>>>
3532

36-
subroutine dummy_arg_constant(dc)
37-
real, constant :: dc
38-
end subroutine
39-
! CHECK-LABEL: func.func @_QMcuda_varPdummy_arg_constant(
40-
! CHECK-SAME: %[[ARG0:.*]]: !fir.ref<f32> {fir.bindc_name = "dc", fir.cuda_attr = #fir.cuda<constant>}
41-
! CHECK: %{{.*}}:2 = hlfir.declare %[[ARG0]] {cuda_attr = #fir.cuda<constant>, uniq_name = "_QMcuda_varFdummy_arg_constantEdc"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>)
42-
4333
subroutine dummy_arg_device(dd)
4434
real, device :: dd
4535
end subroutine

flang/test/Semantics/cuf03.cuf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,11 @@ module m
5555
real, managed :: ma(n) ! ok
5656
!WARNING: Pointer 'dp' may not be associated in a device subprogram
5757
real, device, pointer :: dp
58+
real, constant :: rc ! ok
59+
end subroutine
60+
61+
subroutine host()
62+
!ERROR: Object 'rc' with ATTRIBUTES(CONSTANT) may not be declared in a host subprogram
63+
real, constant :: rc
5864
end subroutine
5965
end module

0 commit comments

Comments
 (0)