diff --git a/flang/lib/Lower/IntrinsicCall.cpp b/flang/lib/Lower/IntrinsicCall.cpp index 055e9260b8e95..26ae437d84b75 100644 --- a/flang/lib/Lower/IntrinsicCall.cpp +++ b/flang/lib/Lower/IntrinsicCall.cpp @@ -4098,8 +4098,9 @@ IntrinsicLibrary::genPack(mlir::Type resultType, // Create mutable fir.box to be passed to the runtime for the result. mlir::Type resultArrayType = builder.getVarLenSeqTy(resultType, 1); - fir::MutableBoxValue resultMutableBox = - fir::factory::createTempMutableBox(builder, loc, resultArrayType); + fir::MutableBoxValue resultMutableBox = fir::factory::createTempMutableBox( + builder, loc, resultArrayType, {}, + fir::isPolymorphicType(array.getType()) ? array : 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 4b8faf16e1215..190c86538738a 100644 --- a/flang/test/Lower/polymorphic-temp.f90 +++ b/flang/test/Lower/polymorphic-temp.f90 @@ -79,4 +79,26 @@ subroutine test_temp_from_intrinsic_reshape(i) ! 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 + subroutine check_pack(r) + class(p1) :: r(:) + end subroutine + + subroutine test_temp_from_intrinsic_pack(i, mask) + class(p1), intent(in) :: i(20, 20) + logical, intent(in) :: mask(20, 20) + call check_pack(pack(i, mask)) + end subroutine + +! CHECK-LABEL: func.func @_QMpoly_tmpPtest_temp_from_intrinsic_pack( +! CHECK-SAME: %[[I:.*]]: !fir.class>> {fir.bindc_name = "i"}, %[[MASK:.*]]: !fir.ref>> {fir.bindc_name = "mask"}) { +! CHECK: %[[TMP_RES:.*]] = fir.alloca !fir.class>>> +! CHECK: %[[EMBOXED_MASK:.*]] = fir.embox %[[MASK]](%{{.*}}) : (!fir.ref>>, !fir.shape<2>) -> !fir.box>> +! CHECK: %[[ZERO:.*]] = fir.zero_bits !fir.heap>> +! CHECK: %[[EMBOX_RES:.*]] = fir.embox %[[ZERO]](%{{.*}}) source_box %[[I]] : (!fir.heap>>, !fir.shape<1>, !fir.class>>) -> !fir.class>>> +! CHECK: fir.store %[[EMBOX_RES]] 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: %[[MASK_BOX_NONE:.*]] = fir.convert %[[EMBOXED_MASK]] : (!fir.box>>) -> !fir.box +! CHECK: %{{.*}} = fir.call @_FortranAPack(%[[RES_BOX_NONE]], %[[I_BOX_NONE]], %[[MASK_BOX_NONE]], %{{.*}}, %{{.*}}, %{{.*}}) {{.*}} : (!fir.ref>, !fir.box, !fir.box, !fir.box, !fir.ref, i32) -> none + end module