Skip to content

[flang][cuda] Add c_devptr and bypass output semantic check #107318

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 1 commit into from
Sep 5, 2024

Conversation

clementval
Copy link
Contributor

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.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir flang:semantics labels Sep 4, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 4, 2024

@llvm/pr-subscribers-flang-semantics

@llvm/pr-subscribers-flang-fir-hlfir

Author: Valentin Clement (バレンタイン クレメン) (clementval)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/107318.diff

3 Files Affected:

  • (modified) flang/lib/Semantics/check-io.cpp (+5)
  • (modified) flang/module/__fortran_builtins.f90 (+4)
  • (added) flang/test/Lower/CUDA/cuda-devptr.cuf (+16)
diff --git a/flang/lib/Semantics/check-io.cpp b/flang/lib/Semantics/check-io.cpp
index 54e8e09cbf7e48..d7d2f0fa322cbb 100644
--- a/flang/lib/Semantics/check-io.cpp
+++ b/flang/lib/Semantics/check-io.cpp
@@ -1171,6 +1171,11 @@ 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")) {
+        // 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

@clementval clementval merged commit c2fc332 into llvm:main Sep 5, 2024
13 checks passed
@clementval clementval deleted the cuf_devptr branch September 5, 2024 03:41
@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 5, 2024

LLVM Buildbot has detected a new failure on builder clang-aarch64-sve-vls running on linaro-g3-04 while building flang at step 13 "test-suite".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/143/builds/1933

Here is the relevant piece of the build log for the reference
Step 13 (test-suite) failure: test (failure)
******************** TEST 'test-suite :: Fortran/gfortran/regression/gfortran-regression-compile-regression__pr32601_f03.test' FAILED ********************

/home/tcwg-buildbot/worker/clang-aarch64-sve-vls/test/sandbox/build/tools/timeit-target --timeout 7200 --limit-core 0 --limit-cpu 7200 --limit-file-size 209715200 --limit-rss-size 838860800 --redirect-input /dev/null --summary /home/tcwg-buildbot/worker/clang-aarch64-sve-vls/test/sandbox/build/Fortran/gfortran/regression/Output/gfortran-regression-compile-regression__pr32601_f03.test.time /home/tcwg-buildbot/worker/clang-aarch64-sve-vls/test/sandbox/build/Fortran/gfortran/regression/..//dummy
/home/tcwg-buildbot/worker/clang-aarch64-sve-vls/test/sandbox/build/tools/not /usr/bin/diff /home/tcwg-buildbot/worker/clang-aarch64-sve-vls/test/sandbox/build/Fortran/gfortran/regression/..//gfortran-compile-empty.reference.out /home/tcwg-buildbot/worker/clang-aarch64-sve-vls/test/sandbox/build/Fortran/gfortran/regression/gfortran-regression-compile-regression__pr32601_f03.out

+ /home/tcwg-buildbot/worker/clang-aarch64-sve-vls/test/sandbox/build/tools/not /usr/bin/diff /home/tcwg-buildbot/worker/clang-aarch64-sve-vls/test/sandbox/build/Fortran/gfortran/regression/..//gfortran-compile-empty.reference.out /home/tcwg-buildbot/worker/clang-aarch64-sve-vls/test/sandbox/build/Fortran/gfortran/regression/gfortran-regression-compile-regression__pr32601_f03.out

********************


@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 5, 2024

LLVM Buildbot has detected a new failure on builder clang-aarch64-sve-vls-2stage running on linaro-g3-03 while building flang at step 18 "test-suite".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/4/builds/1900

Here is the relevant piece of the build log for the reference
Step 18 (test-suite) failure: test (failure)
******************** TEST 'test-suite :: Fortran/gfortran/regression/gfortran-regression-compile-regression__pr32601_f03.test' FAILED ********************

/home/tcwg-buildbot/worker/clang-aarch64-sve-vls-2stage/test/sandbox/build/tools/timeit-target --timeout 7200 --limit-core 0 --limit-cpu 7200 --limit-file-size 209715200 --limit-rss-size 838860800 --redirect-input /dev/null --summary /home/tcwg-buildbot/worker/clang-aarch64-sve-vls-2stage/test/sandbox/build/Fortran/gfortran/regression/Output/gfortran-regression-compile-regression__pr32601_f03.test.time /home/tcwg-buildbot/worker/clang-aarch64-sve-vls-2stage/test/sandbox/build/Fortran/gfortran/regression/..//dummy
/home/tcwg-buildbot/worker/clang-aarch64-sve-vls-2stage/test/sandbox/build/tools/not /usr/bin/diff /home/tcwg-buildbot/worker/clang-aarch64-sve-vls-2stage/test/sandbox/build/Fortran/gfortran/regression/..//gfortran-compile-empty.reference.out /home/tcwg-buildbot/worker/clang-aarch64-sve-vls-2stage/test/sandbox/build/Fortran/gfortran/regression/gfortran-regression-compile-regression__pr32601_f03.out

+ /home/tcwg-buildbot/worker/clang-aarch64-sve-vls-2stage/test/sandbox/build/tools/not /usr/bin/diff /home/tcwg-buildbot/worker/clang-aarch64-sve-vls-2stage/test/sandbox/build/Fortran/gfortran/regression/..//gfortran-compile-empty.reference.out /home/tcwg-buildbot/worker/clang-aarch64-sve-vls-2stage/test/sandbox/build/Fortran/gfortran/regression/gfortran-regression-compile-regression__pr32601_f03.out

********************


@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 5, 2024

LLVM Buildbot has detected a new failure on builder clang-aarch64-sve-vla-2stage running on linaro-g3-01 while building flang at step 18 "test-suite".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/41/builds/1809

Here is the relevant piece of the build log for the reference
Step 18 (test-suite) failure: test (failure)
******************** TEST 'test-suite :: Fortran/gfortran/regression/gfortran-regression-compile-regression__pr32601_f03.test' FAILED ********************

/home/tcwg-buildbot/worker/clang-aarch64-sve-vla-2stage/test/sandbox/build/tools/timeit-target --timeout 7200 --limit-core 0 --limit-cpu 7200 --limit-file-size 209715200 --limit-rss-size 838860800 --redirect-input /dev/null --summary /home/tcwg-buildbot/worker/clang-aarch64-sve-vla-2stage/test/sandbox/build/Fortran/gfortran/regression/Output/gfortran-regression-compile-regression__pr32601_f03.test.time /home/tcwg-buildbot/worker/clang-aarch64-sve-vla-2stage/test/sandbox/build/Fortran/gfortran/regression/..//dummy
/home/tcwg-buildbot/worker/clang-aarch64-sve-vla-2stage/test/sandbox/build/tools/not /usr/bin/diff /home/tcwg-buildbot/worker/clang-aarch64-sve-vla-2stage/test/sandbox/build/Fortran/gfortran/regression/..//gfortran-compile-empty.reference.out /home/tcwg-buildbot/worker/clang-aarch64-sve-vla-2stage/test/sandbox/build/Fortran/gfortran/regression/gfortran-regression-compile-regression__pr32601_f03.out

+ /home/tcwg-buildbot/worker/clang-aarch64-sve-vla-2stage/test/sandbox/build/tools/not /usr/bin/diff /home/tcwg-buildbot/worker/clang-aarch64-sve-vla-2stage/test/sandbox/build/Fortran/gfortran/regression/..//gfortran-compile-empty.reference.out /home/tcwg-buildbot/worker/clang-aarch64-sve-vla-2stage/test/sandbox/build/Fortran/gfortran/regression/gfortran-regression-compile-regression__pr32601_f03.out

********************


@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 5, 2024

LLVM Buildbot has detected a new failure on builder premerge-monolithic-linux running on premerge-linux-1 while building flang at step 6 "build-unified-tree".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/153/builds/7923

Here is the relevant piece of the build log for the reference
Step 6 (build-unified-tree) failure: build (failure)
...
66.784 [141/16/2958] Linking CXX executable tools/flang/unittests/Evaluate/integer.test
66.918 [141/15/2959] Linking CXX executable tools/flang/unittests/Evaluate/expression.test
66.926 [141/14/2960] Linking CXX executable tools/flang/unittests/Evaluate/folding.test
68.273 [141/13/2961] Generating builtins.opt.cypress-r600--.bc
68.454 [140/13/2962] Generating builtins.opt.cedar-r600--.bc
69.031 [139/13/2963] Linking CXX executable bin/bbc
69.168 [139/12/2964] Linking CXX executable bin/flang-new
69.220 [137/13/2965] Generating ../../../../include/flang/__ppc_types.mod
69.249 [135/14/2966] Generating ../../../../include/flang/__fortran_builtins.mod
69.274 [134/14/2967] Generating ../../../../include/flang/mma.mod
FAILED: include/flang/mma.mod /build/buildbot/premerge-monolithic-linux/build/include/flang/mma.mod 
cd /build/buildbot/premerge-monolithic-linux/build/tools/flang/tools/f18 && /etc/cmake/bin/cmake -E make_directory /build/buildbot/premerge-monolithic-linux/build/include/flang && /build/buildbot/premerge-monolithic-linux/build/bin/flang-new --target=ppc64le -cpp -fsyntax-only -module-dir /build/buildbot/premerge-monolithic-linux/build/include/flang /build/buildbot/premerge-monolithic-linux/llvm-project/flang/module/mma.f90
error: Semantic errors in /build/buildbot/premerge-monolithic-linux/llvm-project/flang/module/mma.f90
/build/buildbot/premerge-monolithic-linux/build/bin/../include/flang/__fortran_type_info.mod:4:16: error: Cannot read module file for module '__fortran_builtins': File is not the right module file for '__fortran_builtins': /build/buildbot/premerge-monolithic-linux/build/bin/../include/flang/__fortran_builtins.mod
  use,intrinsic::__fortran_builtins,only:__builtin_c_ptr
                 ^^^^^^^^^^^^^^^^^^
/build/buildbot/premerge-monolithic-linux/build/bin/../include/flang/__fortran_type_info.mod:5:16: error: Cannot read module file for module '__fortran_builtins': File is not the right module file for '__fortran_builtins': /build/buildbot/premerge-monolithic-linux/build/bin/../include/flang/__fortran_builtins.mod
  use,intrinsic::__fortran_builtins,only:__builtin_c_funptr
                 ^^^^^^^^^^^^^^^^^^
/build/buildbot/premerge-monolithic-linux/build/bin/../include/flang/__fortran_type_info.mod:6:10: error: No explicit type declared for '__builtin_c_ptr'
  private::__builtin_c_ptr
           ^^^^^^^^^^^^^^^
/build/buildbot/premerge-monolithic-linux/build/bin/../include/flang/__fortran_type_info.mod:7:10: error: No explicit type declared for '__builtin_c_funptr'
  private::__builtin_c_funptr
           ^^^^^^^^^^^^^^^^^^
/build/buildbot/premerge-monolithic-linux/build/bin/../include/flang/__fortran_type_info.mod:29:6: error: Derived type '__builtin_c_funptr' not found
  type(__builtin_c_funptr)::proc
       ^^^^^^^^^^^^^^^^^^
/build/buildbot/premerge-monolithic-linux/build/bin/../include/flang/__fortran_type_info.mod:62:6: error: Derived type '__builtin_c_ptr' not found
  type(__builtin_c_ptr)::initialization
       ^^^^^^^^^^^^^^^
/build/buildbot/premerge-monolithic-linux/build/bin/../include/flang/__fortran_type_info.mod:67:6: error: Derived type '__builtin_c_funptr' not found
  type(__builtin_c_funptr)::initialization
       ^^^^^^^^^^^^^^^^^^
/build/buildbot/premerge-monolithic-linux/build/bin/../include/flang/__fortran_type_info.mod:84:6: error: Derived type '__builtin_c_funptr' not found
  type(__builtin_c_funptr)::proc
       ^^^^^^^^^^^^^^^^^^
69.291 [134/13/2968] Generating ../../../../include/flang/__fortran_type_info.mod
69.305 [134/12/2969] Generating ../../../../include/flang/__ppc_intrinsics.mod
69.525 [134/11/2970] Generating cedar-r600--.bc
69.546 [134/10/2971] Generating cypress-r600--.bc
70.063 [134/9/2972] Generating builtins.opt.barts-r600--.bc
70.624 [134/8/2973] Generating builtins.opt.cayman-r600--.bc
77.334 [134/7/2974] Generating builtins.opt.nvptx--.bc
77.438 [134/6/2975] Generating builtins.opt.nvptx--nvidiacl.bc
78.574 [134/5/2976] Generating builtins.opt.nvptx64--.bc
79.813 [134/4/2977] Generating builtins.opt.amdgcn--amdhsa.bc
79.996 [134/3/2978] Generating builtins.opt.nvptx64--nvidiacl.bc
80.383 [134/2/2979] Generating builtins.opt.tahiti-amdgcn--.bc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:cuf flang:fir-hlfir flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants