Skip to content

Commit 942bc18

Browse files
committed
[flang] Rebox the polymorphic argument in merge intrinsic
When fsource or tsource is not polymorphic, the result is not polymorphic. Rebox the polymoprhic arguement so the dynamic type of the result is correct. Reviewed By: PeteSteinfeld Differential Revision: https://reviews.llvm.org/D146133
1 parent ede827f commit 942bc18

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4035,10 +4035,18 @@ IntrinsicLibrary::genMerge(mlir::Type,
40354035
mlir::Value fsourceCast = fsource;
40364036
if (fir::isPolymorphicType(tsource.getType()) &&
40374037
!fir::isPolymorphicType(fsource.getType())) {
4038-
tsourceCast = builder.createConvert(loc, fsource.getType(), tsource);
4038+
tsourceCast = builder.create<fir::ReboxOp>(loc, fsource.getType(), tsource,
4039+
/*shape*/ mlir::Value{},
4040+
/*slice=*/mlir::Value{});
4041+
4042+
// builder.createConvert(loc, fsource.getType(), tsource);
40394043
} else if (!fir::isPolymorphicType(tsource.getType()) &&
40404044
fir::isPolymorphicType(fsource.getType())) {
4041-
fsourceCast = builder.createConvert(loc, tsource.getType(), fsource);
4045+
fsourceCast = builder.create<fir::ReboxOp>(loc, tsource.getType(), fsource,
4046+
/*shape*/ mlir::Value{},
4047+
/*slice=*/mlir::Value{});
4048+
4049+
// fsourceCast = builder.createConvert(loc, tsource.getType(), fsource);
40424050
} else {
40434051
// FSOURCE and TSOURCE are not polymorphic.
40444052
// FSOURCE has the same type as TSOURCE, but they may not have the same MLIR

flang/test/Lower/polymorphic-temp.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ subroutine test_merge_intrinsic2(a, b, i)
222222
! CHECK: %[[LOAD_I:.*]] = fir.load %[[I]] : !fir.ref<i32>
223223
! CHECK: %[[C1:.*]] = arith.constant 1 : i32
224224
! CHECK: %[[CMPI:.*]] = arith.cmpi eq, %[[LOAD_I]], %[[C1]] : i32
225-
! CHECK: %[[A_CONV:.*]] = fir.convert %[[LOAD_A]] : (!fir.class<!fir.heap<!fir.type<_QMpoly_tmpTp1{a:i32}>>>) -> !fir.box<!fir.heap<!fir.type<_QMpoly_tmpTp1{a:i32}>>>
226-
! CHECK: %{{.*}} = arith.select %[[CMPI]], %[[A_CONV]], %[[LOAD_B]] : !fir.box<!fir.heap<!fir.type<_QMpoly_tmpTp1{a:i32}>>>
225+
! CHECK: %[[A_REBOX:.*]] = fir.rebox %[[LOAD_A]] : (!fir.class<!fir.heap<!fir.type<_QMpoly_tmpTp1{a:i32}>>>) -> !fir.box<!fir.heap<!fir.type<_QMpoly_tmpTp1{a:i32}>>>
226+
! CHECK: %{{.*}} = arith.select %[[CMPI]], %[[A_REBOX]], %[[LOAD_B]] : !fir.box<!fir.heap<!fir.type<_QMpoly_tmpTp1{a:i32}>>>
227227

228228
end module

0 commit comments

Comments
 (0)