Skip to content

Commit e2d80a3

Browse files
authored
[flang][cuda] Make sure CUDA attribute are imported when using module variable (#82844)
CUDA attribute are correctly propagated to the module file but were not imported currently so they did not appear on the hlfir.declare and fir.global operations for module variables.
1 parent d8406d4 commit e2d80a3

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

flang/lib/Lower/ConvertVariable.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,12 @@ static fir::GlobalOp declareGlobal(Fortran::lower::AbstractConverter &converter,
172172
!Fortran::semantics::IsProcedurePointer(ultimate))
173173
mlir::emitError(loc, "processing global declaration: symbol '")
174174
<< toStringRef(sym.name()) << "' has unexpected details\n";
175+
fir::CUDADataAttributeAttr cudaAttr =
176+
Fortran::lower::translateSymbolCUDADataAttribute(
177+
converter.getFirOpBuilder().getContext(), sym);
175178
return builder.createGlobal(loc, converter.genType(var), globalName, linkage,
176179
mlir::Attribute{}, isConstant(ultimate),
177-
var.isTarget());
180+
var.isTarget(), cudaAttr);
178181
}
179182

180183
/// Temporary helper to catch todos in initial data target lowering.
@@ -1586,7 +1589,7 @@ fir::FortranVariableFlagsAttr Fortran::lower::translateSymbolAttributes(
15861589
fir::CUDADataAttributeAttr Fortran::lower::translateSymbolCUDADataAttribute(
15871590
mlir::MLIRContext *mlirContext, const Fortran::semantics::Symbol &sym) {
15881591
std::optional<Fortran::common::CUDADataAttr> cudaAttr =
1589-
Fortran::semantics::GetCUDADataAttr(&sym);
1592+
Fortran::semantics::GetCUDADataAttr(&sym.GetUltimate());
15901593
return fir::getCUDADataAttribute(mlirContext, cudaAttr);
15911594
}
15921595

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s
2+
3+
! Simple module to test module use in
4+
5+
module cuf_mod
6+
real, device :: md
7+
end module
8+
9+
! CHECK: fir.global @_QMcuf_modEmd {cuda_attr = #fir.cuda<device>} : f32
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
! RUN: bbc -emit-hlfir -fcuda %S/cuda-mod.cuf
2+
! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s
3+
4+
! Test importing module with variable with CUDA attributes.
5+
6+
subroutine sub1()
7+
use cuf_mod
8+
md = 1.0
9+
end
10+
11+
! CHECK-LABEL: func.func @_QPsub1()
12+
! CHECK: %[[ADDR:.*]] = fir.address_of(@_QMcuf_modEmd) : !fir.ref<f32>
13+
! CHECK: %{{.*}}:2 = hlfir.declare %[[ADDR]] {cuda_attr = #fir.cuda<device>, uniq_name = "_QMcuf_modEmd"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>)
14+
15+
! CHECK: fir.global @_QMcuf_modEmd {cuda_attr = #fir.cuda<device>} : f32

0 commit comments

Comments
 (0)