diff --git a/flang/lib/Lower/IntrinsicCall.cpp b/flang/lib/Lower/IntrinsicCall.cpp index daf3aa6c992bf..7d3d4d113bd35 100644 --- a/flang/lib/Lower/IntrinsicCall.cpp +++ b/flang/lib/Lower/IntrinsicCall.cpp @@ -4222,8 +4222,9 @@ IntrinsicLibrary::genReshape(mlir::Type resultType, // Create mutable fir.box to be passed to the runtime for the result. mlir::Type type = builder.getVarLenSeqTy(resultType, resultRank); - fir::MutableBoxValue resultMutableBox = - fir::factory::createTempMutableBox(builder, loc, type); + fir::MutableBoxValue resultMutableBox = fir::factory::createTempMutableBox( + builder, loc, type, {}, + fir::isPolymorphicType(source.getType()) ? source : mlir::Value{}); mlir::Value resultIrBox = fir::factory::getMutableIRBox(builder, loc, resultMutableBox); diff --git a/flang/test/Lower/polymorphic-temp.f90 b/flang/test/Lower/polymorphic-temp.f90 index 6cd715581a7d2..02245f2d1bf8d 100644 --- a/flang/test/Lower/polymorphic-temp.f90 +++ b/flang/test/Lower/polymorphic-temp.f90 @@ -47,4 +47,24 @@ subroutine test_temp_from_intrinsic_spread() ! CHECK: fir.call @_QMpoly_tmpPpass_unlimited_poly_1d(%[[REBOX]]) {{.*}} : (!fir.class>) -> () ! CHECK: fir.freemem %[[RES_ADDR]] : !fir.heap> + subroutine test_temp_from_intrinsic_reshape(i) + class(*), allocatable :: a(:,:) + class(*), intent(in) :: i(20,20) + allocate(a(10,10), source=reshape(i,[10,10])) + end subroutine + +! CHECK-LABEL: func.func @_QMpoly_tmpPtest_temp_from_intrinsic_reshape( +! CHECK-SAME: %[[I:.*]]: !fir.class> {fir.bindc_name = "i"}) { +! CHECK: %[[TMP_RES:.*]] = fir.alloca !fir.class>> +! CHECK: %[[A:.*]] = fir.alloca !fir.class>> {bindc_name = "a", uniq_name = "_QMpoly_tmpFtest_temp_from_intrinsic_reshapeEa"} +! CHECK: %[[EMBOX_WITH_SOURCE:.*]] = fir.embox %{{.*}}(%{{.*}}) source_box %[[I]] : (!fir.heap>, !fir.shape<2>, !fir.class>) -> !fir.class>> +! CHECK: fir.store %[[EMBOX_WITH_SOURCE]] to %[[TMP_RES]] : !fir.ref>>> +! CHECK: %[[RES_BOX_NONE:.*]] = fir.convert %[[TMP_RES]] : (!fir.ref>>>) -> !fir.ref> +! CHECK: %[[I_BOX_NONE:.*]] = fir.convert %[[I]] : (!fir.class>) -> !fir.box +! CHECK: %{{.*}} = fir.call @_FortranAReshape(%[[RES_BOX_NONE]], %[[I_BOX_NONE]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}) {{.*}} : (!fir.ref>, !fir.box, !fir.box, !fir.box, !fir.box, !fir.ref, i32) -> none +! CHECK: %[[LOAD_RES:.*]] = fir.load %[[TMP_RES]] : !fir.ref>>> +! CHECK: %[[A_BOX_NONE:.*]] = fir.convert %[[A]] : (!fir.ref>>>) -> !fir.ref> +! CHECK: %[[RES_BOX_NONE:.*]] = fir.convert %[[LOAD_RES]] : (!fir.class>>) -> !fir.box +! CHECK: %{{.*}} = fir.call @_FortranAAllocatableApplyMold(%[[A_BOX_NONE]], %[[RES_BOX_NONE]]) {{.*}} : (!fir.ref>, !fir.box) -> none + end module