Index: flang/lib/Lower/ConvertExpr.cpp =================================================================== --- flang/lib/Lower/ConvertExpr.cpp +++ flang/lib/Lower/ConvertExpr.cpp @@ -2103,17 +2103,58 @@ /// inquired: create a descriptor that will be inquired. if (Fortran::evaluate::IsVariable(expr) && isArray(expr) && !Fortran::evaluate::UnwrapWholeSymbolOrComponentDataRef(expr)) - return lowerIntrinsicArgumentAsBox(expr); + return lowerIntrinsicArgumentAsBox(expr, /*intrinsic=*/"", /*arg=*/""); return gen(expr); } /// Helper to lower intrinsic arguments to a fir::BoxValue. /// It preserves all the non default lower bounds/non deferred length /// parameter information. - ExtValue lowerIntrinsicArgumentAsBox(const Fortran::lower::SomeExpr &expr) { + ExtValue lowerIntrinsicArgumentAsBox(const Fortran::lower::SomeExpr &expr, + llvm::StringRef intrinsic, + llvm::StringRef arg) { mlir::Location loc = getLoc(); ExtValue exv = genBoxArg(expr); mlir::Value box = builder.createBox(loc, exv); + if (intrinsic == "reshape" && arg == "shape") { + // The `reshape` is the only intrinsic whose `shape` argument requires the + // `SIZE` of it to be constant expression. So, convert the box value so + // that the type has constant size. + mlir::Type boxEleTy = fir::dyn_cast_ptrOrBoxEleTy(box.getType()); + unsigned size = 0; + // The box type may have non constant size since it has slice. + if (fir::hasDynamicSize(boxEleTy)) { + std::optional<Fortran::evaluate::DataRef> dref = + Fortran::evaluate::ExtractDataRef(expr); + // Non-constant subscript has been checked in semantic analysis. + if (auto *aRef{std::get_if<Fortran::evaluate::ArrayRef>(&dref->u)}) { + assert(aRef->size() == 1 && "The shape must be one-rank array"); + Fortran::evaluate::Subscript &ss = aRef->at(0); + std::visit(Fortran::common::visitors{ + [&](const Fortran::evaluate::Triplet &trip) { + mlir::Value lbValue = + fir::getBase(genval(*trip.GetLower())); + mlir::Value ubValue = + fir::getBase(genval(*trip.GetUpper())); + mlir::Value strideValue = + fir::getBase(genval(trip.GetStride())); + auto lb = fir::factory::getIntIfConstant(lbValue); + auto ub = fir::factory::getIntIfConstant(ubValue); + auto stride = + fir::factory::getIntIfConstant(strideValue); + size = + 1 + (ub.value() - lb.value()) / stride.value(); + }, + [&](auto) {}}, + ss.u); + } + auto newBoxEleTy = + fir::SequenceType::get(fir::SequenceType::Shape(1, size), + fir::unwrapSeqOrBoxedSeqType(boxEleTy)); + auto newBoxTy = fir::BoxType::get(newBoxEleTy); + box = builder.createConvert(loc, newBoxTy, box); + } + } return fir::BoxValue( box, fir::factory::getNonDefaultLowerBounds(builder, loc, exv), fir::factory::getNonDeferredLengthParams(exv)); @@ -2212,7 +2253,11 @@ operands.emplace_back(gen(*expr)); continue; case Fortran::lower::LowerIntrinsicArgAs::Box: - operands.emplace_back(lowerIntrinsicArgumentAsBox(*expr)); + operands.emplace_back( + lowerIntrinsicArgumentAsBox(*expr, name, + intrinsic->characteristics.value() + .dummyArguments[arg.index()] + .name)); continue; case Fortran::lower::LowerIntrinsicArgAs::Inquired: operands.emplace_back(lowerIntrinsicArgumentAsInquired(*expr)); Index: flang/test/Lower/Intrinsics/reshape.f90 =================================================================== --- flang/test/Lower/Intrinsics/reshape.f90 +++ flang/test/Lower/Intrinsics/reshape.f90 @@ -54,3 +54,97 @@ ! CHECK: %[[VAL_39:.*]] = fir.convert %[[VAL_28]] : (!fir.box<!fir.ptr<!fir.array<?xi32>>>) -> !fir.box<none> ! CHECK: %[[VAL_41:.*]] = fir.call @_FortranAReshape({{.*}}, {{.*}}, %{{.*}}, %[[VAL_38]], %[[VAL_39]], %{{.*}}, %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.box<none>, !fir.box<none>, !fir.box<none>, !fir.ref<i8>, i32) -> none end subroutine + +! CHECK-LABEL: func.func @_QPtest_reshape_shape_slice() { +! CHECK: %[[VAL_0:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?x?xf32>>> +! CHECK: %[[VAL_1:.*]] = fir.address_of(@_QFtest_reshape_shape_sliceEdims) : !fir.ref<!fir.array<4xi32>> +! CHECK: %[[VAL_2:.*]] = arith.constant 4 : index +! CHECK: %[[VAL_3:.*]] = fir.address_of(@_QFtest_reshape_shape_sliceEtmp) : !fir.ref<!fir.array<4xf32>> +! CHECK: %[[VAL_4:.*]] = arith.constant 4 : index +! CHECK: %[[VAL_5:.*]] = fir.alloca !fir.box<!fir.ptr<!fir.array<?x?xf32>>> {bindc_name = "v2d", uniq_name = "_QFtest_reshape_shape_sliceEv2d"} +! CHECK: %[[VAL_6:.*]] = fir.zero_bits !fir.ptr<!fir.array<?x?xf32>> +! CHECK: %[[VAL_7:.*]] = arith.constant 0 : index +! CHECK: %[[VAL_8:.*]] = fir.shape %[[VAL_7]], %[[VAL_7]] : (index, index) -> !fir.shape<2> +! CHECK: %[[VAL_9:.*]] = fir.embox %[[VAL_6]](%[[VAL_8]]) : (!fir.ptr<!fir.array<?x?xf32>>, !fir.shape<2>) -> !fir.box<!fir.ptr<!fir.array<?x?xf32>>> +! CHECK: fir.store %[[VAL_9]] to %[[VAL_5]] : !fir.ref<!fir.box<!fir.ptr<!fir.array<?x?xf32>>>> +! CHECK: %[[VAL_10:.*]] = arith.constant 2 : i32 +! CHECK: %[[VAL_11:.*]] = fir.convert %[[VAL_10]] : (i32) -> index +! CHECK: %[[VAL_12:.*]] = arith.constant 2 : i32 +! CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_12]] : (i32) -> index +! CHECK: %[[VAL_14:.*]] = arith.constant 0 : index +! CHECK: %[[VAL_15:.*]] = arith.cmpi sgt, %[[VAL_11]], %[[VAL_14]] : index +! CHECK: %[[VAL_16:.*]] = arith.select %[[VAL_15]], %[[VAL_11]], %[[VAL_14]] : index +! CHECK: %[[VAL_17:.*]] = arith.constant 0 : index +! CHECK: %[[VAL_18:.*]] = arith.cmpi sgt, %[[VAL_13]], %[[VAL_17]] : index +! CHECK: %[[VAL_19:.*]] = arith.select %[[VAL_18]], %[[VAL_13]], %[[VAL_17]] : index +! CHECK: %[[VAL_20:.*]] = fir.allocmem !fir.array<?x?xf32>, %[[VAL_16]], %[[VAL_19]] {uniq_name = "_QFtest_reshape_shape_sliceEv2d.alloc"} +! CHECK: %[[VAL_21:.*]] = fir.shape %[[VAL_16]], %[[VAL_19]] : (index, index) -> !fir.shape<2> +! CHECK: %[[VAL_22:.*]] = fir.embox %[[VAL_20]](%[[VAL_21]]) : (!fir.heap<!fir.array<?x?xf32>>, !fir.shape<2>) -> !fir.box<!fir.ptr<!fir.array<?x?xf32>>> +! CHECK: fir.store %[[VAL_22]] to %[[VAL_5]] : !fir.ref<!fir.box<!fir.ptr<!fir.array<?x?xf32>>>> +! CHECK: %[[VAL_23:.*]] = fir.load %[[VAL_5]] : !fir.ref<!fir.box<!fir.ptr<!fir.array<?x?xf32>>>> +! CHECK: %[[VAL_24:.*]] = arith.constant 0 : index +! CHECK: %[[VAL_25:.*]]:3 = fir.box_dims %[[VAL_23]], %[[VAL_24]] : (!fir.box<!fir.ptr<!fir.array<?x?xf32>>>, index) -> (index, index, index) +! CHECK: %[[VAL_26:.*]] = arith.constant 1 : index +! CHECK: %[[VAL_27:.*]]:3 = fir.box_dims %[[VAL_23]], %[[VAL_26]] : (!fir.box<!fir.ptr<!fir.array<?x?xf32>>>, index) -> (index, index, index) +! CHECK: %[[VAL_28:.*]] = arith.constant 1 : index +! CHECK: %[[VAL_29:.*]] = arith.constant 1 : i64 +! CHECK: %[[VAL_30:.*]] = fir.convert %[[VAL_29]] : (i64) -> index +! CHECK: %[[VAL_31:.*]] = arith.constant 0 : index +! CHECK: %[[VAL_32:.*]]:3 = fir.box_dims %[[VAL_23]], %[[VAL_31]] : (!fir.box<!fir.ptr<!fir.array<?x?xf32>>>, index) -> (index, index, index) +! CHECK: %[[VAL_33:.*]] = arith.addi %[[VAL_25]]#0, %[[VAL_32]]#1 : index +! CHECK: %[[VAL_34:.*]] = arith.subi %[[VAL_33]], %[[VAL_28]] : index +! CHECK: %[[VAL_35:.*]] = arith.constant 0 : index +! CHECK: %[[VAL_36:.*]] = arith.subi %[[VAL_34]], %[[VAL_25]]#0 : index +! CHECK: %[[VAL_37:.*]] = arith.addi %[[VAL_36]], %[[VAL_30]] : index +! CHECK: %[[VAL_38:.*]] = arith.divsi %[[VAL_37]], %[[VAL_30]] : index +! CHECK: %[[VAL_39:.*]] = arith.cmpi sgt, %[[VAL_38]], %[[VAL_35]] : index +! CHECK: %[[VAL_40:.*]] = arith.select %[[VAL_39]], %[[VAL_38]], %[[VAL_35]] : index +! CHECK: %[[VAL_41:.*]] = arith.constant 1 : i64 +! CHECK: %[[VAL_42:.*]] = fir.convert %[[VAL_41]] : (i64) -> index +! CHECK: %[[VAL_43:.*]] = arith.constant 1 : index +! CHECK: %[[VAL_44:.*]]:3 = fir.box_dims %[[VAL_23]], %[[VAL_43]] : (!fir.box<!fir.ptr<!fir.array<?x?xf32>>>, index) -> (index, index, index) +! CHECK: %[[VAL_45:.*]] = arith.addi %[[VAL_27]]#0, %[[VAL_44]]#1 : index +! CHECK: %[[VAL_46:.*]] = arith.subi %[[VAL_45]], %[[VAL_28]] : index +! CHECK: %[[VAL_47:.*]] = arith.constant 0 : index +! CHECK: %[[VAL_48:.*]] = arith.subi %[[VAL_46]], %[[VAL_27]]#0 : index +! CHECK: %[[VAL_49:.*]] = arith.addi %[[VAL_48]], %[[VAL_42]] : index +! CHECK: %[[VAL_50:.*]] = arith.divsi %[[VAL_49]], %[[VAL_42]] : index +! CHECK: %[[VAL_51:.*]] = arith.cmpi sgt, %[[VAL_50]], %[[VAL_47]] : index +! CHECK: %[[VAL_52:.*]] = arith.select %[[VAL_51]], %[[VAL_50]], %[[VAL_47]] : index +! CHECK: %[[VAL_53:.*]] = fir.shift %[[VAL_25]]#0, %[[VAL_27]]#0 : (index, index) -> !fir.shift<2> +! CHECK: %[[VAL_54:.*]] = fir.slice %[[VAL_25]]#0, %[[VAL_34]], %[[VAL_30]], %[[VAL_27]]#0, %[[VAL_46]], %[[VAL_42]] : (index, index, index, index, index, index) -> !fir.slice<2> +! CHECK: %[[VAL_55:.*]] = fir.array_load %[[VAL_23]](%[[VAL_53]]) [%[[VAL_54]]] : (!fir.box<!fir.ptr<!fir.array<?x?xf32>>>, !fir.shift<2>, !fir.slice<2>) -> !fir.array<?x?xf32> +! CHECK: %[[VAL_56:.*]] = fir.shape %[[VAL_4]] : (index) -> !fir.shape<1> +! CHECK: %[[VAL_57:.*]] = fir.embox %[[VAL_3]](%[[VAL_56]]) : (!fir.ref<!fir.array<4xf32>>, !fir.shape<1>) -> !fir.box<!fir.array<4xf32>> +! CHECK: %[[VAL_58:.*]] = arith.constant 1 : i64 +! CHECK: %[[VAL_59:.*]] = fir.convert %[[VAL_58]] : (i64) -> index +! CHECK: %[[VAL_60:.*]] = arith.constant 1 : i64 +! CHECK: %[[VAL_61:.*]] = fir.convert %[[VAL_60]] : (i64) -> index +! CHECK: %[[VAL_62:.*]] = arith.constant 2 : i64 +! CHECK: %[[VAL_63:.*]] = fir.convert %[[VAL_62]] : (i64) -> index +! CHECK: %[[VAL_64:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1> +! CHECK: %[[VAL_65:.*]] = fir.slice %[[VAL_59]], %[[VAL_63]], %[[VAL_61]] : (index, index, index) -> !fir.slice<1> +! CHECK: %[[VAL_66:.*]] = fir.embox %[[VAL_1]](%[[VAL_64]]) {{\[}}%[[VAL_65]]] : (!fir.ref<!fir.array<4xi32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.box<!fir.array<?xi32>> +! CHECK: %[[VAL_67:.*]] = fir.convert %[[VAL_66]] : (!fir.box<!fir.array<?xi32>>) -> !fir.box<!fir.array<2xi32>> +! CHECK: %[[VAL_68:.*]] = fir.absent !fir.box<i1> +! CHECK: %[[VAL_69:.*]] = fir.absent !fir.box<i1> +! CHECK: %[[VAL_70:.*]] = fir.zero_bits !fir.heap<!fir.array<?x?xf32>> +! CHECK: %[[VAL_71:.*]] = arith.constant 0 : index +! CHECK: %[[VAL_72:.*]] = fir.shape %[[VAL_71]], %[[VAL_71]] : (index, index) -> !fir.shape<2> +! CHECK: %[[VAL_73:.*]] = fir.embox %[[VAL_70]](%[[VAL_72]]) : (!fir.heap<!fir.array<?x?xf32>>, !fir.shape<2>) -> !fir.box<!fir.heap<!fir.array<?x?xf32>>> +! CHECK: fir.store %[[VAL_73]] to %[[VAL_0]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>> +! CHECK: %[[VAL_76:.*]] = fir.convert %[[VAL_0]] : (!fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>) -> !fir.ref<!fir.box<none>> +! CHECK: %[[VAL_77:.*]] = fir.convert %[[VAL_57]] : (!fir.box<!fir.array<4xf32>>) -> !fir.box<none> +! CHECK: %[[VAL_78:.*]] = fir.convert %[[VAL_67]] : (!fir.box<!fir.array<2xi32>>) -> !fir.box<none> +! CHECK: %[[VAL_79:.*]] = fir.convert %[[VAL_68]] : (!fir.box<i1>) -> !fir.box<none> +! CHECK: %[[VAL_80:.*]] = fir.convert %[[VAL_69]] : (!fir.box<i1>) -> !fir.box<none> +! CHECK: %[[VAL_82:.*]] = fir.call @_FortranAReshape(%[[VAL_76]], %[[VAL_77]], %[[VAL_78]], %[[VAL_79]], %[[VAL_80]], %[[VAL_81:.*]], %[[VAL_75:.*]]) : (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.box<none>, !fir.box<none>, !fir.box<none>, !fir.ref<i8>, i32) -> none +! CHECK: } +subroutine test_reshape_shape_slice() + integer, parameter :: i = 1 + real, pointer :: v2d(:,:) + real :: tmp(4) = [1,2,3,4] + integer :: dims(4) = [2,2,2,2] + allocate(v2d(2,2)) + v2d(:,:) = reshape(tmp, dims(i:2)) +end