Skip to content

Commit 2f323e0

Browse files
committed
[flang] Fix for polymoprhic pointer component
Fix path that generates MutableBox for pointer component. Reviewed By: PeteSteinfeld Differential Revision: https://reviews.llvm.org/D137270
1 parent 35c9085 commit 2f323e0

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

flang/lib/Optimizer/Builder/FIRBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ fir::ExtendedValue fir::factory::componentToExtendedValue(
920920
auto fieldTy = component.getType();
921921
if (auto ty = fir::dyn_cast_ptrEleTy(fieldTy))
922922
fieldTy = ty;
923-
if (fieldTy.isa<fir::BoxType>()) {
923+
if (fieldTy.isa<fir::BaseBoxType>()) {
924924
llvm::SmallVector<mlir::Value> nonDeferredTypeParams;
925925
auto eleTy = fir::unwrapSequenceType(fir::dyn_cast_ptrOrBoxEleTy(fieldTy));
926926
if (auto charTy = eleTy.dyn_cast<fir::CharacterType>()) {

flang/test/Lower/allocatable-polymorphic.f90

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ module poly
1717
procedure :: proc2 => proc2_p2
1818
end type
1919

20+
type with_alloc
21+
class(p1), pointer :: element
22+
end type
23+
2024
contains
2125
subroutine proc1_p1()
2226
print*, 'call proc1_p1'
@@ -348,8 +352,31 @@ subroutine test_deallocate()
348352
allocate(p)
349353
deallocate(p)
350354
end subroutine
355+
356+
subroutine test_type_with_polymorphic_pointer_component()
357+
type(with_alloc), pointer :: a
358+
allocate(a)
359+
allocate(a%element)
360+
end subroutine
351361
end module
352362

363+
! CHECK-LABEL: func.func @_QMpolyPtest_type_with_polymorphic_pointer_component()
364+
! CHECK: %[[TYPE_PTR:.*]] = fir.alloca !fir.ptr<!fir.type<_QMpolyTwith_alloc{element:!fir.class<!fir.ptr<!fir.type<_QMpolyTp1{a:i32,b:i32}>>>}>> {uniq_name = "_QMpolyFtest_type_with_polymorphic_pointer_componentEa.addr"}
365+
! CHECK: %[[TYPE_PTR_LOAD:.*]] = fir.load %[[TYPE_PTR]] : !fir.ref<!fir.ptr<!fir.type<_QMpolyTwith_alloc{element:!fir.class<!fir.ptr<!fir.type<_QMpolyTp1{a:i32,b:i32}>>>}>>>
366+
! CHECK: %[[ELEMENT:.*]] = fir.field_index element, !fir.type<_QMpolyTwith_alloc{element:!fir.class<!fir.ptr<!fir.type<_QMpolyTp1{a:i32,b:i32}>>>}>
367+
! CHECK: %[[ELEMENT_DESC:.*]] = fir.coordinate_of %[[TYPE_PTR_LOAD]], %[[ELEMENT]] : (!fir.ptr<!fir.type<_QMpolyTwith_alloc{element:!fir.class<!fir.ptr<!fir.type<_QMpolyTp1{a:i32,b:i32}>>>}>>, !fir.field) -> !fir.ref<!fir.class<!fir.ptr<!fir.type<_QMpolyTp1{a:i32,b:i32}>>>>
368+
! CHECK: %[[ZERO:.*]] = fir.zero_bits !fir.ptr<!fir.type<_QMpolyTp1{a:i32,b:i32}>>
369+
! CHECK: %[[ZERO_DESC:.*]] = fir.embox %[[ZERO]] : (!fir.ptr<!fir.type<_QMpolyTp1{a:i32,b:i32}>>) -> !fir.class<!fir.ptr<!fir.type<_QMpolyTp1{a:i32,b:i32}>>>
370+
! CHECK: fir.store %[[ZERO_DESC]] to %[[ELEMENT_DESC]] : !fir.ref<!fir.class<!fir.ptr<!fir.type<_QMpolyTp1{a:i32,b:i32}>>>>
371+
! CHECK: %[[TYPE_DESC_P1:.*]] = fir.address_of(@_QMpolyE.dt.p1) : !fir.ref<!fir.type<{{.*}}>>
372+
! CHECK: %[[ELEMENT_DESC_CAST:.*]] = fir.convert %[[ELEMENT_DESC]] : (!fir.ref<!fir.class<!fir.ptr<!fir.type<_QMpolyTp1{a:i32,b:i32}>>>>) -> !fir.ref<!fir.box<none>>
373+
! CHECK: %[[TYPE_DESC_P1_CAST:.*]] = fir.convert %[[TYPE_DESC_P1]] : (!fir.ref<!fir.type<{{.*}}>>) -> !fir.ref<none>
374+
! CHECK: %[[RANK:.*]] = arith.constant 0 : i32
375+
! CHECK: %[[CORANK:.*]] = arith.constant 0 : i32
376+
! CHECK: %{{.*}} = fir.call @_FortranAPointerNullifyDerived(%[[ELEMENT_DESC_CAST]], %[[TYPE_DESC_P1_CAST]], %[[RANK]], %[[CORANK]]) : (!fir.ref<!fir.box<none>>, !fir.ref<none>, i32, i32) -> none
377+
! CHECK: %[[ELEMENT_DESC_CAST:.*]] = fir.convert %[[ELEMENT_DESC]] : (!fir.ref<!fir.class<!fir.ptr<!fir.type<_QMpolyTp1{a:i32,b:i32}>>>>) -> !fir.ref<!fir.box<none>>
378+
! CHECK: %{{.*}} = fir.call @_FortranAPointerAllocate(%[[ELEMENT_DESC_CAST]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, i1, !fir.box<none>, !fir.ref<i8>, i32) -> i32
379+
353380
program test_alloc
354381
use poly
355382

0 commit comments

Comments
 (0)