Skip to content

Commit

Permalink
[flang] Handle type generation for unlimited polymorphic function result
Browse files Browse the repository at this point in the history
An unlimited polymorphic entity is considered to have a derived category
in its dynamic type but no type descriptor. Avoid a nullptr dereference when
an unlimited polymorphic type needs to be generated.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D139923
  • Loading branch information
clementval committed Dec 13, 2022
1 parent 864bb84 commit 7602e09
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions flang/lib/Lower/ConvertExpr.cpp
Expand Up @@ -1872,6 +1872,8 @@ class ScalarExprLowering {
mlir::Type genType(const Fortran::evaluate::DynamicType &dt) {
if (dt.category() != Fortran::common::TypeCategory::Derived)
return converter.genType(dt.category(), dt.kind());
if (dt.IsUnlimitedPolymorphic())
return mlir::NoneType::get(&converter.getMLIRContext());
return converter.genType(dt.GetDerivedTypeSpec());
}

Expand Down
16 changes: 16 additions & 0 deletions flang/test/Lower/polymorphic.f90
Expand Up @@ -722,6 +722,22 @@ subroutine test_polymorphic_io()
! CHECK: %[[BOX_NONE:.*]] = fir.convert %[[LOAD_P]] : (!fir.class<!fir.ptr<!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>) -> !fir.box<none>
! CHECK: %{{.*}} = fir.call @_FortranAioInputDescriptor(%{{.*}}, %[[BOX_NONE]]) {{.*}} : (!fir.ref<i8>, !fir.box<none>) -> i1

function unlimited_polymorphic_alloc_array_ret()
class(*), allocatable :: unlimited_polymorphic_alloc_array_ret(:)
end function

subroutine test_unlimited_polymorphic_alloc_array_ret()
select type (a => unlimited_polymorphic_alloc_array_ret())
type is (real)
print*, 'type is real'
end select
end subroutine

! CHECK-LABEL: func.func @_QMpolymorphic_testPtest_unlimited_polymorphic_alloc_array_ret() {
! CHECK: %[[RES_TMP:.*]] = fir.alloca !fir.class<!fir.heap<!fir.array<?xnone>>> {bindc_name = ".result"}
! CHECK: %[[RES:.*]] = fir.call @_QMpolymorphic_testPunlimited_polymorphic_alloc_array_ret() fastmath<contract> : () -> !fir.class<!fir.heap<!fir.array<?xnone>>>
! CHECK: fir.save_result %[[RES]] to %[[RES_TMP]] : !fir.class<!fir.heap<!fir.array<?xnone>>>, !fir.ref<!fir.class<!fir.heap<!fir.array<?xnone>>>>

end module

program test
Expand Down

0 comments on commit 7602e09

Please sign in to comment.