[flang-commits] [flang] 92e75c0 - Reland [flang][cuda] Add c_devptr and bypass output semantic check (#107353)

via flang-commits flang-commits at lists.llvm.org
Thu Sep 5 11:44:15 PDT 2024


Author: Valentin Clement (バレンタイン クレメン)
Date: 2024-09-05T11:44:12-07:00
New Revision: 92e75c095bb380039f32218534f78c4580bf76e4

URL: https://github.com/llvm/llvm-project/commit/92e75c095bb380039f32218534f78c4580bf76e4
DIFF: https://github.com/llvm/llvm-project/commit/92e75c095bb380039f32218534f78c4580bf76e4.diff

LOG: Reland [flang][cuda] Add c_devptr and bypass output semantic check (#107353)

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.

This version of the patch keeps the semantic error when -pedantic is
enabled to align with gfortran.

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

Modified: 
    flang/include/flang/Common/Fortran-features.h
    flang/lib/Semantics/check-io.cpp
    flang/module/__fortran_builtins.f90

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Common/Fortran-features.h b/flang/include/flang/Common/Fortran-features.h
index 6ef5f44c89db07..0c8a3d2bd5281f 100644
--- a/flang/include/flang/Common/Fortran-features.h
+++ b/flang/include/flang/Common/Fortran-features.h
@@ -51,7 +51,7 @@ ENUM_CLASS(LanguageFeature, BackslashEscapes, OldDebugLines,
     BadBranchTarget, ConvertedArgument, HollerithPolymorphic, ListDirectedSize,
     NonBindCInteroperability, CudaManaged, CudaUnified,
     PolymorphicActualAllocatableOrPointerToMonomorphicDummy, RelaxedPureDummy,
-    UndefinableAsynchronousOrVolatileActual, AutomaticInMainProgram)
+    UndefinableAsynchronousOrVolatileActual, AutomaticInMainProgram, PrintCptr)
 
 // Portability and suspicious usage warnings
 ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,

diff  --git a/flang/lib/Semantics/check-io.cpp b/flang/lib/Semantics/check-io.cpp
index 54e8e09cbf7e48..46f07842b92cbb 100644
--- a/flang/lib/Semantics/check-io.cpp
+++ b/flang/lib/Semantics/check-io.cpp
@@ -1171,6 +1171,12 @@ parser::Message *IoChecker::CheckForBadIoType(const evaluate::DynamicType &type,
             "Derived type '%s' in I/O may not be polymorphic unless using defined I/O"_err_en_US,
             derived.name());
       }
+      if ((IsBuiltinDerivedType(&derived, "c_ptr") ||
+              IsBuiltinDerivedType(&derived, "c_devptr")) &&
+          !context_.ShouldWarn(common::LanguageFeature::PrintCptr)) {
+        // Bypass the check below for c_ptr and c_devptr.
+        return nullptr;
+      }
       if (const Symbol *
           bad{FindInaccessibleComponent(which, derived, scope)}) {
         return &context_.Say(where,

diff  --git a/flang/module/__fortran_builtins.f90 b/flang/module/__fortran_builtins.f90
index 44b0f17339cd96..a9d3ac897eb583 100644
--- a/flang/module/__fortran_builtins.f90
+++ b/flang/module/__fortran_builtins.f90
@@ -102,6 +102,10 @@
     __builtin_threadIdx, __builtin_blockDim, __builtin_blockIdx, &
     __builtin_gridDim
   integer, parameter, public :: __builtin_warpsize = 32
+  
+  type, public, bind(c) :: __builtin_c_devptr
+    type(__builtin_c_ptr) :: cptr
+  end type
 
   intrinsic :: __builtin_fma
   intrinsic :: __builtin_ieee_is_nan, __builtin_ieee_is_negative, &

diff  --git a/flang/test/Lower/CUDA/cuda-devptr.cuf b/flang/test/Lower/CUDA/cuda-devptr.cuf
new file mode 100644
index 00000000000000..4e11e3c0fc8f85
--- /dev/null
+++ b/flang/test/Lower/CUDA/cuda-devptr.cuf
@@ -0,0 +1,16 @@
+! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s
+
+! Test CUDA Fortran specific type
+
+subroutine sub1()
+  use iso_c_binding
+  use __fortran_builtins, only : c_devptr => __builtin_c_devptr
+
+  type(c_ptr) :: ptr
+  type(c_devptr) :: dptr
+  print*,ptr
+  print*,dptr
+end
+
+! CHECK-LABEL: func.func @_QPsub1()
+! CHECK-COUNT-2: %{{.*}} = fir.call @_FortranAioOutputDerivedType


        


More information about the flang-commits mailing list