Skip to content

Commit

Permalink
[flang][cuda] Make sure CUDA attribute are imported when using module…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
clementval committed Feb 26, 2024
1 parent d8406d4 commit e2d80a3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
7 changes: 5 additions & 2 deletions flang/lib/Lower/ConvertVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,12 @@ static fir::GlobalOp declareGlobal(Fortran::lower::AbstractConverter &converter,
!Fortran::semantics::IsProcedurePointer(ultimate))
mlir::emitError(loc, "processing global declaration: symbol '")
<< toStringRef(sym.name()) << "' has unexpected details\n";
fir::CUDADataAttributeAttr cudaAttr =
Fortran::lower::translateSymbolCUDADataAttribute(
converter.getFirOpBuilder().getContext(), sym);
return builder.createGlobal(loc, converter.genType(var), globalName, linkage,
mlir::Attribute{}, isConstant(ultimate),
var.isTarget());
var.isTarget(), cudaAttr);
}

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

Expand Down
9 changes: 9 additions & 0 deletions flang/test/Lower/CUDA/cuda-mod.cuf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s

! Simple module to test module use in

module cuf_mod
real, device :: md
end module

! CHECK: fir.global @_QMcuf_modEmd {cuda_attr = #fir.cuda<device>} : f32
15 changes: 15 additions & 0 deletions flang/test/Lower/CUDA/cuda-module-use.cuf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
! RUN: bbc -emit-hlfir -fcuda %S/cuda-mod.cuf
! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s

! Test importing module with variable with CUDA attributes.

subroutine sub1()
use cuf_mod
md = 1.0
end

! CHECK-LABEL: func.func @_QPsub1()
! CHECK: %[[ADDR:.*]] = fir.address_of(@_QMcuf_modEmd) : !fir.ref<f32>
! CHECK: %{{.*}}:2 = hlfir.declare %[[ADDR]] {cuda_attr = #fir.cuda<device>, uniq_name = "_QMcuf_modEmd"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>)

! CHECK: fir.global @_QMcuf_modEmd {cuda_attr = #fir.cuda<device>} : f32

0 comments on commit e2d80a3

Please sign in to comment.