[flang-commits] [flang] fe9409b - [flang][RFC] Change the interface for non-BIND(C) CPTR type with VALUE attribute

Peixin Qiao via flang-commits flang-commits at lists.llvm.org
Fri Nov 4 07:20:53 PDT 2022


Author: Peixin Qiao
Date: 2022-11-04T22:19:38+08:00
New Revision: fe9409b9fad352bbad7064f6c8be1e1ef0b12586

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

LOG: [flang][RFC] Change the interface for non-BIND(C) CPTR type with VALUE attribute

When the `type(c_ptr/c_funptr)` argument has value attribute in non-BIND(C)
procedure, it is passed by VALUE in gfortran. ifort does not do this. Be
consistent with gfortran.

Fix #58756.

Reviewed By: PeteSteinfeld, jeanPerier

Differential Revision: https://reviews.llvm.org/D137237

Added: 
    

Modified: 
    flang/lib/Lower/CallInterface.cpp
    flang/test/Lower/call-by-value.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/CallInterface.cpp b/flang/lib/Lower/CallInterface.cpp
index 20258f37a8126..0832f101d4bb2 100644
--- a/flang/lib/Lower/CallInterface.cpp
+++ b/flang/lib/Lower/CallInterface.cpp
@@ -929,13 +929,15 @@ class Fortran::lower::CallInterfaceImpl {
       PassEntityBy passBy = PassEntityBy::BaseAddress;
       Property prop = Property::BaseAddress;
       if (isValueAttr) {
+        bool isBuiltinCptrType = fir::isa_builtin_cptr_type(type);
         if (isBindC || (!type.isa<fir::SequenceType>() &&
                         !obj.attrs.test(Attrs::Optional) &&
-                        dynamicType.category() !=
-                            Fortran::common::TypeCategory::Derived)) {
+                        (dynamicType.category() !=
+                             Fortran::common::TypeCategory::Derived ||
+                         isBuiltinCptrType))) {
           passBy = PassEntityBy::Value;
           prop = Property::Value;
-          if (fir::isa_builtin_cptr_type(type)) {
+          if (isBuiltinCptrType) {
             auto recTy = type.dyn_cast<fir::RecordType>();
             mlir::Type fieldTy = recTy.getTypeList()[0].second;
             passType = fir::ReferenceType::get(fieldTy);

diff  --git a/flang/test/Lower/call-by-value.f90 b/flang/test/Lower/call-by-value.f90
index 717da1afd99e5..b9f9dc1a24eff 100644
--- a/flang/test/Lower/call-by-value.f90
+++ b/flang/test/Lower/call-by-value.f90
@@ -73,3 +73,20 @@ subroutine test_char_value(x) bind(c)
   character(1), value :: x
   call internal_call4(x)
 end
+
+! CHECK-LABEL: func.func @_QPtest_cptr_value(
+! CHECK-SAME:                                %[[VAL_0:.*]]: !fir.ref<i64> {fir.bindc_name = "x"}) {
+! CHECK:         %[[VAL_1:.*]] = fir.alloca !fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>
+! CHECK:         %[[VAL_2:.*]] = fir.field_index __address, !fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>
+! CHECK:         %[[VAL_3:.*]] = fir.coordinate_of %[[VAL_1]], %[[VAL_2]] : (!fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>>, !fir.field) -> !fir.ref<i64>
+! CHECK:         %[[VAL_4:.*]] = fir.convert %[[VAL_0]] : (!fir.ref<i64>) -> i64
+! CHECK:         fir.store %[[VAL_4]] to %[[VAL_3]] : !fir.ref<i64>
+! CHECK:         fir.call @_QPinternal_call5(%[[VAL_1]]) : (!fir.ref<!fir.type<_QM__fortran_builtinsT__builtin_c_ptr{__address:i64}>>) -> ()
+! CHECK:         return
+! CHECK:       }
+
+subroutine test_cptr_value(x)
+  use iso_c_binding
+  type(c_ptr), value :: x
+  call internal_call5(x)
+end


        


More information about the flang-commits mailing list