Skip to content

[flang][cuda] Do not lower device variables in main program as globals #102512

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 4 commits into from
Aug 8, 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
12 changes: 12 additions & 0 deletions flang/include/flang/Evaluate/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,18 @@ bool CheckForCoindexedObject(parser::ContextualMessages &,
const std::optional<ActualArgument> &, const std::string &procName,
const std::string &argName);

inline bool CanCUDASymbolHasSave(const Symbol &sym) {
if (const auto *details =
sym.GetUltimate().detailsIf<semantics::ObjectEntityDetails>()) {
if (details->cudaDataAttr() &&
*details->cudaDataAttr() != common::CUDADataAttr::Pinned &&
*details->cudaDataAttr() != common::CUDADataAttr::Unified) {
return false;
}
}
return true;
}

inline bool IsCUDADeviceSymbol(const Symbol &sym) {
if (const auto *details =
sym.GetUltimate().detailsIf<semantics::ObjectEntityDetails>()) {
Expand Down
3 changes: 2 additions & 1 deletion flang/lib/Evaluate/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,8 @@ bool IsSaved(const Symbol &original) {
(features.IsEnabled(common::LanguageFeature::SaveMainProgram) ||
(features.IsEnabled(
common::LanguageFeature::SaveBigMainProgramVariables) &&
symbol.size() > 32))) {
symbol.size() > 32)) &&
Fortran::evaluate::CanCUDASymbolHasSave(symbol)) {
// With SaveBigMainProgramVariables, keeping all unsaved main program
// variables of 32 bytes or less on the stack allows keeping numerical and
// logical scalars, small scalar characters or derived, small arrays, and
Expand Down
9 changes: 6 additions & 3 deletions flang/test/Lower/CUDA/cuda-program-global.cuf
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s

! Test lowering of program local variable that are global
! Test lowering of program local variables. Make sure CUDA device variables are
! not lowered as global.

program test
integer, device :: a(10)
integer, unified :: u(10)
integer :: b(10)
integer :: i
print*,i
end

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

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