Skip to content

Commit

Permalink
[flang][hlfir] Fixed nullptr dereference.
Browse files Browse the repository at this point in the history
In case of unlimited polymorphic type, there is no derived type
spec, so use NoneType explicitly.

Reviewed By: tblah

Differential Revision: https://reviews.llvm.org/D157751
  • Loading branch information
vzakhari committed Aug 14, 2023
1 parent 3a7a74c commit 9184eb8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 5 additions & 2 deletions flang/lib/Lower/ConvertExprToHLFIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1525,8 +1525,11 @@ class HlfirBuilder {
// Elemental expression.
mlir::Type elementType;
if constexpr (R::category == Fortran::common::TypeCategory::Derived) {
elementType = Fortran::lower::translateDerivedTypeToFIRType(
getConverter(), op.derived().GetType().GetDerivedTypeSpec());
if (op.derived().GetType().IsUnlimitedPolymorphic())
elementType = mlir::NoneType::get(builder.getContext());
else
elementType = Fortran::lower::translateDerivedTypeToFIRType(
getConverter(), op.derived().GetType().GetDerivedTypeSpec());
} else {
elementType =
Fortran::lower::getFIRType(builder.getContext(), R::category, R::kind,
Expand Down
24 changes: 24 additions & 0 deletions flang/test/Lower/HLFIR/elemental-array-ops.f90
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,27 @@ end subroutine polymorphic_parenthesis
! CHECK: hlfir.destroy %[[VAL_7]] : !hlfir.expr<?x!fir.type<_QFpolymorphic_parenthesisTt>?>
! CHECK: return
! CHECK: }

subroutine unlimited_polymorphic_parenthesis(x, y)
class(*), allocatable :: x(:)
class(*), intent(in) :: y(:)
x = (y)
end subroutine unlimited_polymorphic_parenthesis
! CHECK-LABEL: func.func @_QPunlimited_polymorphic_parenthesis(
! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.class<!fir.heap<!fir.array<?xnone>>>> {fir.bindc_name = "x"},
! CHECK-SAME: %[[VAL_1:.*]]: !fir.class<!fir.array<?xnone>> {fir.bindc_name = "y"}) {
! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %[[VAL_0]] {fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFunlimited_polymorphic_parenthesisEx"} : (!fir.ref<!fir.class<!fir.heap<!fir.array<?xnone>>>>) -> (!fir.ref<!fir.class<!fir.heap<!fir.array<?xnone>>>>, !fir.ref<!fir.class<!fir.heap<!fir.array<?xnone>>>>)
! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_1]] {fortran_attrs = #fir.var_attrs<intent_in>, uniq_name = "_QFunlimited_polymorphic_parenthesisEy"} : (!fir.class<!fir.array<?xnone>>) -> (!fir.class<!fir.array<?xnone>>, !fir.class<!fir.array<?xnone>>)
! CHECK: %[[VAL_4:.*]] = arith.constant 0 : index
! CHECK: %[[VAL_5:.*]]:3 = fir.box_dims %[[VAL_3]]#0, %[[VAL_4]] : (!fir.class<!fir.array<?xnone>>, index) -> (index, index, index)
! CHECK: %[[VAL_6:.*]] = fir.shape %[[VAL_5]]#1 : (index) -> !fir.shape<1>
! CHECK: %[[VAL_7:.*]] = hlfir.elemental %[[VAL_6]] mold %[[VAL_3]]#0 unordered : (!fir.shape<1>, !fir.class<!fir.array<?xnone>>) -> !hlfir.expr<?xnone?> {
! CHECK: ^bb0(%[[VAL_8:.*]]: index):
! CHECK: %[[VAL_9:.*]] = hlfir.designate %[[VAL_3]]#0 (%[[VAL_8]]) : (!fir.class<!fir.array<?xnone>>, index) -> !fir.class<none>
! CHECK: %[[VAL_10:.*]] = hlfir.as_expr %[[VAL_9]] : (!fir.class<none>) -> !hlfir.expr<none?>
! CHECK: hlfir.yield_element %[[VAL_10]] : !hlfir.expr<none?>
! CHECK: }
! CHECK: hlfir.assign %[[VAL_7]] to %[[VAL_2]]#0 realloc : !hlfir.expr<?xnone?>, !fir.ref<!fir.class<!fir.heap<!fir.array<?xnone>>>>
! CHECK: hlfir.destroy %[[VAL_7]] : !hlfir.expr<?xnone?>
! CHECK: return
! CHECK: }

0 comments on commit 9184eb8

Please sign in to comment.