Skip to content

Commit c2fc332

Browse files
authored
[flang][cuda] Add c_devptr and bypass output semantic check (#107318)
Add a builtin type for c_devptr since it will need some special handling for some function like c_f_pointer. `c_ptr` is defined as a builtin type and was raising a semantic error if you try to use it in a I/O statement. This patch add a check for c_ptr and c_devptr to bypass the semantic check and allow the variables of these types to be used in I/O.
1 parent c28b1a1 commit c2fc332

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

flang/lib/Semantics/check-io.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,11 @@ parser::Message *IoChecker::CheckForBadIoType(const evaluate::DynamicType &type,
11711171
"Derived type '%s' in I/O may not be polymorphic unless using defined I/O"_err_en_US,
11721172
derived.name());
11731173
}
1174+
if (IsBuiltinDerivedType(&derived, "c_ptr") ||
1175+
IsBuiltinDerivedType(&derived, "c_devptr")) {
1176+
// Bypass the check below for c_ptr and c_devptr.
1177+
return nullptr;
1178+
}
11741179
if (const Symbol *
11751180
bad{FindInaccessibleComponent(which, derived, scope)}) {
11761181
return &context_.Say(where,

flang/module/__fortran_builtins.f90

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@
102102
__builtin_threadIdx, __builtin_blockDim, __builtin_blockIdx, &
103103
__builtin_gridDim
104104
integer, parameter, public :: __builtin_warpsize = 32
105+
106+
type, public, bind(c) :: __builtin_c_devptr
107+
type(__builtin_c_ptr) :: cptr
108+
end type
105109

106110
intrinsic :: __builtin_fma
107111
intrinsic :: __builtin_ieee_is_nan, __builtin_ieee_is_negative, &

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s
2+
3+
! Test CUDA Fortran specific type
4+
5+
subroutine sub1()
6+
use iso_c_binding
7+
use __fortran_builtins, only : c_devptr => __builtin_c_devptr
8+
9+
type(c_ptr) :: ptr
10+
type(c_devptr) :: dptr
11+
print*,ptr
12+
print*,dptr
13+
end
14+
15+
! CHECK-LABEL: func.func @_QPsub1()
16+
! CHECK-COUNT-2: %{{.*}} = fir.call @_FortranAioOutputDerivedType

0 commit comments

Comments
 (0)