[flang-commits] [flang] 8d692b4 - [flang] Avoid crash in lowering for unlimited polymorphic function return

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Fri Nov 25 04:47:57 PST 2022


Author: Valentin Clement
Date: 2022-11-25T13:47:50+01:00
New Revision: 8d692b4b8db82709355886939bfffb658db9e65a

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

LOG: [flang] Avoid crash in lowering for unlimited polymorphic function return

The dynamic type of an unlimited polymorphic entity has the
derived category but does not have derived type spec. This leads
to a crash for a nullptr dereference. This patch avoids this crash
by checking if that the dynamic type is not unlimited polymorphic
before dereferencing the derived type spec.

Reviewed By: PeteSteinfeld

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

Added: 
    

Modified: 
    flang/lib/Lower/CallInterface.cpp
    flang/test/Lower/polymorphic.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/CallInterface.cpp b/flang/lib/Lower/CallInterface.cpp
index 0832f101d4bb..d26eb278069b 100644
--- a/flang/lib/Lower/CallInterface.cpp
+++ b/flang/lib/Lower/CallInterface.cpp
@@ -251,7 +251,8 @@ void Fortran::lower::CallerInterface::walkResultLengths(
     if (std::optional<Fortran::evaluate::ExtentExpr> length =
             dynamicType.GetCharLength())
       visitor(toEvExpr(*length));
-  } else if (dynamicType.category() == common::TypeCategory::Derived) {
+  } else if (dynamicType.category() == common::TypeCategory::Derived &&
+             !dynamicType.IsUnlimitedPolymorphic()) {
     const Fortran::semantics::DerivedTypeSpec &derivedTypeSpec =
         dynamicType.GetDerivedTypeSpec();
     if (Fortran::semantics::CountLenParameters(derivedTypeSpec) > 0)

diff  --git a/flang/test/Lower/polymorphic.f90 b/flang/test/Lower/polymorphic.f90
index dadde2c5eac8..3d25bbd4e268 100644
--- a/flang/test/Lower/polymorphic.f90
+++ b/flang/test/Lower/polymorphic.f90
@@ -99,4 +99,21 @@ subroutine polymorphic_to_nonpolymorphic(p)
 ! CHECK-LABEL: func.func @_QMpolymorphic_testPpolymorphic_to_nonpolymorphic
 ! Just checking that FIR is generated without error.
 
+! Test that lowering does not crash for function return with unlimited
+! polymoprhic value.
+
+  function up_ret()
+    class(*), pointer :: up_ret(:)
+  end function
+
+! CHECK-LABEL: func.func @_QMpolymorphic_testPup_ret() -> !fir.class<!fir.ptr<!fir.array<?xnone>>> {
+
+  subroutine call_up_ret()
+    class(*), pointer :: p(:)
+    p => up_ret()
+  end subroutine
+
+! CHECK-LABEL: func.func @_QMpolymorphic_testPcall_up_ret() {
+! CHECK:         %{{.*}} = fir.call @_QMpolymorphic_testPup_ret() {{.*}} : () -> !fir.class<!fir.ptr<!fir.array<?xnone>>>
+
 end module


        


More information about the flang-commits mailing list