Skip to content

Commit 63e45ef

Browse files
authored
To fix polymorphic pointer assignment in FORALL when LHS is unlimited polymorphic and RHS is intrinsic type target (#164999)
Fixes #143569.
1 parent 9eb3aee commit 63e45ef

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

flang/lib/Lower/Bridge.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4876,6 +4876,10 @@ class FirConverter : public Fortran::lower::AbstractConverter {
48764876
mlir::Value shape = builder->genShape(loc, lbounds, extents);
48774877
rhsBox = fir::ReboxOp::create(*builder, loc, lhsBoxType, rhsBox, shape,
48784878
/*slice=*/mlir::Value{});
4879+
} else if (fir::isClassStarType(lhsBoxType) &&
4880+
!fir::ConvertOp::canBeConverted(rhsBoxType, lhsBoxType)) {
4881+
rhsBox = fir::ReboxOp::create(*builder, loc, lhsBoxType, rhsBox,
4882+
mlir::Value{}, mlir::Value{});
48794883
}
48804884
return rhsBox;
48814885
}

flang/test/Lower/forall-polymorphic.f90

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
! Test lower of FORALL polymorphic pointer assignment
22
! RUN: bbc -emit-fir %s -o - | FileCheck %s
33

4+
45
!! Test when LHS is polymorphic and RHS is not polymorphic
56
! CHECK-LABEL: c.func @_QPforallpolymorphic
67
subroutine forallPolymorphic()
@@ -46,6 +47,7 @@ subroutine forallPolymorphic()
4647

4748
end subroutine forallPolymorphic
4849

50+
4951
!! Test when LHS is not polymorphic but RHS is polymorphic
5052
! CHECK-LABEL: c.func @_QPforallpolymorphic2(
5153
! CHECK-SAME: %arg0: !fir.ref<!fir.class<!fir.heap<!fir.array<?x!fir.type<_QFforallpolymorphic2Tdt{ptr:!fir.box<!fir.ptr<!fir.array<?x!fir.type<_QFforallpolymorphic2Tdt>>>>}>>>>> {fir.bindc_name = "tar1", fir.target}) {
@@ -87,3 +89,42 @@ subroutine forallPolymorphic2(Tar1)
8789

8890
end subroutine forallPolymorphic2
8991

92+
93+
!! Test when LHS is unlimited polymorphic and RHS non-polymorphic intrinsic
94+
!! type target.
95+
! CHECK-LABEL: c.func @_QPforallpolymorphic3
96+
subroutine forallPolymorphic3()
97+
TYPE :: DT
98+
CLASS(*), POINTER :: Ptr => NULL()
99+
END TYPE
100+
101+
TYPE(DT) :: D1(10)
102+
CHARACTER*1, TARGET :: TAR1(10)
103+
INTEGER :: I
104+
105+
FORALL (I=1:10)
106+
D1(I)%Ptr => Tar1(I)
107+
END FORALL
108+
109+
! CHECK: %[[V_7:[0-9]+]] = fir.alloca !fir.array<10x!fir.type<_QFforallpolymorphic3Tdt{ptr:!fir.class<!fir.ptr<none>>}>> {bindc_name = "d1", uniq_name = "_QFforallpolymorphic3Ed1"}
110+
! CHECK: %[[V_8:[0-9]+]] = fir.shape %c10 : (index) -> !fir.shape<1>
111+
! CHECK: %[[V_9:[0-9]+]] = fir.declare %[[V_7]](%[[V_8]]) {uniq_name = "_QFforallpolymorphic3Ed1"} : (!fir.ref<!fir.array<10x!fir.type<_QFforallpolymorphic3Tdt{ptr:!fir.class<!fir.ptr<none>>}>>>, !fir.shape<1>) -> !fir.ref<!fir.array<10x!fir.type<_QFforallpolymorphic3Tdt{ptr:!fir.class<!fir.ptr<none>>}>>>
112+
! CHECK: %[[V_16:[0-9]+]] = fir.alloca !fir.array<10x!fir.char<1>> {bindc_name = "tar1", fir.target, uniq_name = "_QFforallpolymorphic3Etar1"}
113+
! CHECK: %[[V_17:[0-9]+]] = fir.declare %[[V_16]](%[[V_8]]) typeparams %c1 {fortran_attrs = #fir.var_attrs<target>, uniq_name = "_QFforallpolymorphic3Etar1"} : (!fir.ref<!fir.array<10x!fir.char<1>>>, !fir.shape<1>, index) -> !fir.ref<!fir.array<10x!fir.char<1>>>
114+
! CHECK: %[[V_24:[0-9]+]] = fir.convert %c1_i32 : (i32) -> index
115+
! CHECK: %[[V_25:[0-9]+]] = fir.convert %c10_i32 : (i32) -> index
116+
! CHECK: fir.do_loop %arg0 = %[[V_24]] to %[[V_25]] step %c1
117+
! CHECK: {
118+
! CHECK: %[[V_26:[0-9]+]] = fir.convert %arg0 : (index) -> i32
119+
! CHECK: %[[V_27:[0-9]+]] = fir.convert %[[V_26]] : (i32) -> i64
120+
! CHECK: %[[V_28:[0-9]+]] = fir.array_coor %[[V_9]](%[[V_8]]) %[[V_27]] : (!fir.ref<!fir.array<10x!fir.type<_QFforallpolymorphic3Tdt{ptr:!fir.class<!fir.ptr<none>>}>>>, !fir.shape<1>, i64) -> !fir.ref<!fir.type<_QFforallpolymorphic3Tdt{ptr:!fir.class<!fir.ptr<none>>}>>
121+
! CHECK: %[[V_29:[0-9]+]] = fir.field_index ptr, !fir.type<_QFforallpolymorphic3Tdt{ptr:!fir.class<!fir.ptr<none>>}>
122+
! CHECK: %[[V_30:[0-9]+]] = fir.coordinate_of %[[V_28]], ptr : (!fir.ref<!fir.type<_QFforallpolymorphic3Tdt{ptr:!fir.class<!fir.ptr<none>>}>>) -> !fir.ref<!fir.class<!fir.ptr<none>>>
123+
! CHECK: %[[V_31:[0-9]+]] = fir.convert %[[V_26]] : (i32) -> i64
124+
! CHECK: %[[V_32:[0-9]+]] = fir.array_coor %[[V_17]](%[[V_8]]) %31 : (!fir.ref<!fir.array<10x!fir.char<1>>>, !fir.shape<1>, i64) -> !fir.ref<!fir.char<1>>
125+
! CHECK: %[[V_33:[0-9]+]] = fir.embox %[[V_32]] : (!fir.ref<!fir.char<1>>) -> !fir.box<!fir.ptr<!fir.char<1>>>
126+
! CHECK: %[[V_34:[0-9]+]] = fir.rebox %[[V_33]] : (!fir.box<!fir.ptr<!fir.char<1>>>) -> !fir.class<!fir.ptr<none>>
127+
! CHECK: fir.store %[[V_34]] to %[[V_30]] : !fir.ref<!fir.class<!fir.ptr<none>>>
128+
! CHECK: }
129+
130+
end subroutine forallPolymorphic3

0 commit comments

Comments
 (0)