Skip to content

Commit 8c3b6bd

Browse files
authored
[flang][cuda] Do not lower device variables in main program as globals (#102512)
Flang considers arrays in main program larger than 32 bytes having the SAVE attribute and lowers them as globals. In CUDA Fortran, device variables are not allowed to have the SAVE attribute and should be allocated dynamically in the main program scope. This patch updates lowering so CUDA Fortran device variables are not considered with the SAVE attribute.
1 parent 9a070d6 commit 8c3b6bd

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

flang/include/flang/Evaluate/tools.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,18 @@ bool CheckForCoindexedObject(parser::ContextualMessages &,
12431243
const std::optional<ActualArgument> &, const std::string &procName,
12441244
const std::string &argName);
12451245

1246+
inline bool CanCUDASymbolHasSave(const Symbol &sym) {
1247+
if (const auto *details =
1248+
sym.GetUltimate().detailsIf<semantics::ObjectEntityDetails>()) {
1249+
if (details->cudaDataAttr() &&
1250+
*details->cudaDataAttr() != common::CUDADataAttr::Pinned &&
1251+
*details->cudaDataAttr() != common::CUDADataAttr::Unified) {
1252+
return false;
1253+
}
1254+
}
1255+
return true;
1256+
}
1257+
12461258
inline bool IsCUDADeviceSymbol(const Symbol &sym) {
12471259
if (const auto *details =
12481260
sym.GetUltimate().detailsIf<semantics::ObjectEntityDetails>()) {

flang/lib/Evaluate/tools.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1696,7 +1696,8 @@ bool IsSaved(const Symbol &original) {
16961696
(features.IsEnabled(common::LanguageFeature::SaveMainProgram) ||
16971697
(features.IsEnabled(
16981698
common::LanguageFeature::SaveBigMainProgramVariables) &&
1699-
symbol.size() > 32))) {
1699+
symbol.size() > 32)) &&
1700+
Fortran::evaluate::CanCUDASymbolHasSave(symbol)) {
17001701
// With SaveBigMainProgramVariables, keeping all unsaved main program
17011702
// variables of 32 bytes or less on the stack allows keeping numerical and
17021703
// logical scalars, small scalar characters or derived, small arrays, and
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s
22

3-
! Test lowering of program local variable that are global
3+
! Test lowering of program local variables. Make sure CUDA device variables are
4+
! not lowered as global.
45

56
program test
67
integer, device :: a(10)
8+
integer, unified :: u(10)
79
integer :: b(10)
810
integer :: i
911
print*,i
1012
end
1113

1214
! CHECK-LABEL: func.func @_QQmain()
13-
! CHECK: fir.address_of(@_QFEa) : !fir.ref<!fir.array<10xi32>>
15+
! CHECK: cuf.alloc !fir.array<10xi32> {bindc_name = "a", data_attr = #cuf.cuda<device>, uniq_name = "_QFEa"} -> !fir.ref<!fir.array<10xi32>>
1416
! CHECK: fir.address_of(@_QFEb) : !fir.ref<!fir.array<10xi32>>
1517
! CHECK: %[[ALLOCA:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFEi"}
1618
! CHECK: hlfir.declare %[[ALLOCA]] {uniq_name = "_QFEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
1719

18-
! CHECK: fir.global internal @_QFEa {data_attr = #cuf.cuda<device>} : !fir.array<10xi32> {{{$}}
20+
! CHECK-NOT: fir.global internal @_QFEa {data_attr = #cuf.cuda<device>} : !fir.array<10xi32> {{{$}}
1921
! CHECK: fir.global internal @_QFEb : !fir.array<10xi32> {{{$}}
22+
! CHECK: fir.global internal @_QFEu {data_attr = #cuf.cuda<unified>} : !fir.array<10xi32>

0 commit comments

Comments
 (0)