Skip to content

Commit b9e53b9

Browse files
committed
[flang] Handle polymorphic value when creating temporary
Reviewed By: PeteSteinfeld Differential Revision: https://reviews.llvm.org/D138921
1 parent 0aedf9d commit b9e53b9

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

flang/lib/Lower/ConvertExpr.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,19 @@ class ScalarExprLowering {
21892189
builder.create<fir::StoreOp>(loc, value, temp);
21902190
return temp;
21912191
},
2192+
[&](const fir::PolymorphicValue &p) -> ExtValue {
2193+
mlir::Type type = p.getAddr().getType();
2194+
mlir::Value value = p.getAddr();
2195+
if (fir::isa_ref_type(type))
2196+
value = builder.create<fir::LoadOp>(loc, value);
2197+
mlir::Value temp = builder.createTemporary(loc, value.getType());
2198+
builder.create<fir::StoreOp>(loc, value, temp);
2199+
mlir::Value empty;
2200+
mlir::ValueRange emptyRange;
2201+
auto boxTy = fir::ClassType::get(value.getType());
2202+
return builder.create<fir::EmboxOp>(loc, boxTy, temp, empty, empty,
2203+
emptyRange, p.getTdesc());
2204+
},
21922205
[&](const auto &) -> ExtValue {
21932206
fir::emitFatalError(loc, "expr is not a scalar value");
21942207
});

flang/test/Lower/polymorphic.f90

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,31 @@ subroutine derived_type_assignment_with_class()
192192
a = p3(b)
193193
end subroutine
194194

195+
subroutine takes_p1(p)
196+
class(p1), intent(in) :: p
197+
end subroutine
198+
199+
! CHECK-LABEL: func.func @_QMpolymorphic_testPtakes_p1
200+
201+
subroutine no_reassoc_poly_value(a, i)
202+
class(p1), intent(in) :: a(:)
203+
integer :: i
204+
call takes_p1((a(i)))
205+
end subroutine
206+
207+
! CHECK-LABEL: func.func @_QMpolymorphic_testPno_reassoc_poly_value(
208+
! CHECK-SAME: %[[ARG0:.*]]: !fir.class<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>> {fir.bindc_name = "a"}, %[[I:.*]]: !fir.ref<i32> {fir.bindc_name = "i"}) {
209+
! CHECK: %[[TEMP:.*]] = fir.alloca !fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>
210+
! CHECK: %[[LOADED_I:.*]] = fir.load %[[I]] : !fir.ref<i32>
211+
! CHECK: %[[I_I64:.*]] = fir.convert %[[LOADED_I]] : (i32) -> i64
212+
! CHECK: %[[C1:.*]] = arith.constant 1 : i64
213+
! CHECK: %[[IDX:.*]] = arith.subi %[[I_I64]], %[[C1]] : i64
214+
! CHECK: %[[COORD:.*]] = fir.coordinate_of %[[ARG0]], %[[IDX]] : (!fir.class<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>, i64) -> !fir.ref<!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>
215+
! CHECK: %[[TDESC:.*]] = fir.box_tdesc %[[ARG0]] : (!fir.class<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>) -> !fir.tdesc<none>
216+
! CHECK: %[[NO_REASSOC:.*]] = fir.no_reassoc %[[COORD]] : !fir.ref<!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>
217+
! CHECK: %[[LOAD:.*]] = fir.load %[[NO_REASSOC]] : !fir.ref<!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>
218+
! CHECK: fir.store %[[LOAD]] to %[[TEMP]] : !fir.ref<!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>
219+
! CHECK: %[[EMBOX:.*]] = fir.embox %[[TEMP]] tdesc %[[TDESC]] : (!fir.ref<!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>, !fir.tdesc<none>) -> !fir.class<!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>
220+
! CHECK: fir.call @_QMpolymorphic_testPtakes_p1(%[[EMBOX]]) {{.*}} : (!fir.class<!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>) -> ()
221+
195222
end module

0 commit comments

Comments
 (0)