diff --git a/flang/lib/Optimizer/Builder/HLFIRTools.cpp b/flang/lib/Optimizer/Builder/HLFIRTools.cpp index a11c235510fe9..99a699b35d27a 100644 --- a/flang/lib/Optimizer/Builder/HLFIRTools.cpp +++ b/flang/lib/Optimizer/Builder/HLFIRTools.cpp @@ -912,52 +912,11 @@ hlfir::translateToExtendedValue(mlir::Location loc, fir::FirOpBuilder &builder, std::pair> hlfir::convertToValue(mlir::Location loc, fir::FirOpBuilder &builder, const hlfir::Entity &entity) { - auto [exv, cleanup] = translateToExtendedValue(loc, builder, entity); // Load scalar references to integer, logical, real, or complex value // to an mlir value, dereference allocatable and pointers, and get rid // of fir.box that are not needed or create a copy into contiguous memory. - exv = exv.match( - [&](const fir::UnboxedValue &box) -> fir::ExtendedValue { - if (mlir::Type elementType = fir::dyn_cast_ptrEleTy(box.getType())) - if (fir::isa_trivial(elementType)) - return builder.create(loc, box); - return box; - }, - [&](const fir::CharBoxValue &box) -> fir::ExtendedValue { return box; }, - [&](const fir::ArrayBoxValue &box) -> fir::ExtendedValue { return box; }, - [&](const fir::CharArrayBoxValue &box) -> fir::ExtendedValue { - return box; - }, - [&](const fir::MutableBoxValue &box) -> fir::ExtendedValue { - if (box.rank() != 0) - TODO(loc, "lower array descriptor designator to HLFIR value"); - if (entity.isProcedure()) - TODO(loc, "lower proc descriptor designator to HLFIR value"); - - hlfir::Entity derefedEntity = - hlfir::derefPointersAndAllocatables(loc, builder, entity); - mlir::Type eleTy = derefedEntity.getFortranElementType(); - - // Trivial values are unboxed. - if (derefedEntity.isScalar() && fir::isa_trivial(eleTy)) - return builder.create(loc, derefedEntity); - - if (mlir::isa(eleTy)) { - if (mlir::isa(derefedEntity.getFirBase().getType())) - return genUnboxChar(loc, builder, derefedEntity.getFirBase()); - // Extract length from the original entity. - mlir::Value len = genCharacterVariableLength(loc, builder, entity); - return fir::CharBoxValue{derefedEntity, len}; - } - - // Keep derived type value boxed. - return fir::factory::genMutableBoxRead(builder, loc, box); - }, - [&](const auto &) -> fir::ExtendedValue { - // Can we end up here? - TODO(loc, "lower descriptor designator to HLFIR value"); - }); - return {exv, cleanup}; + auto derefedAndLoadedEntity = loadTrivialScalar(loc, builder, entity); + return translateToExtendedValue(loc, builder, derefedAndLoadedEntity); } static fir::ExtendedValue placeTrivialInMemory(mlir::Location loc, diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp index d5b266bb5702d..2f3722db85af8 100644 --- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp +++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp @@ -3638,20 +3638,21 @@ IntrinsicLibrary::genMerge(mlir::Type, // used. mlir::Value tsourceCast = tsource; mlir::Value fsourceCast = fsource; + auto convertToStaticType = [&](mlir::Value polymorphic, + mlir::Value other) -> mlir::Value { + mlir::Type otherType = other.getType(); + if (otherType.isa()) + return builder.create(loc, otherType, polymorphic, + /*shape*/ mlir::Value{}, + /*slice=*/mlir::Value{}); + return builder.create(loc, otherType, polymorphic); + }; if (fir::isPolymorphicType(tsource.getType()) && !fir::isPolymorphicType(fsource.getType())) { - tsourceCast = builder.create(loc, fsource.getType(), tsource, - /*shape*/ mlir::Value{}, - /*slice=*/mlir::Value{}); - - // builder.createConvert(loc, fsource.getType(), tsource); + tsourceCast = convertToStaticType(tsource, fsource); } else if (!fir::isPolymorphicType(tsource.getType()) && fir::isPolymorphicType(fsource.getType())) { - fsourceCast = builder.create(loc, tsource.getType(), fsource, - /*shape*/ mlir::Value{}, - /*slice=*/mlir::Value{}); - - // fsourceCast = builder.createConvert(loc, tsource.getType(), fsource); + fsourceCast = convertToStaticType(fsource, tsource); } else { // FSOURCE and TSOURCE are not polymorphic. // FSOURCE has the same type as TSOURCE, but they may not have the same MLIR diff --git a/flang/test/Lower/HLFIR/calls-character-singleton-result.f90 b/flang/test/Lower/HLFIR/calls-character-singleton-result.f90 index 0ef8490f7b776..3a3277c835645 100644 --- a/flang/test/Lower/HLFIR/calls-character-singleton-result.f90 +++ b/flang/test/Lower/HLFIR/calls-character-singleton-result.f90 @@ -14,7 +14,7 @@ subroutine scalar_char(c, i) ! CHECK: %[[VAL_4:.*]] = fir.convert %{{.*}}#0 : (!fir.ref>) -> !fir.ref> ! CHECK: %[[VAL_5:.*]]:2 = hlfir.declare %[[VAL_4]] typeparams %{{.*}} {{.*}}Ec ! CHECK: %[[VAL_6:.*]]:2 = hlfir.declare %{{.*}} {{.*}}Ei -! CHECK: %[[VAL_7:.*]] = fir.load %[[VAL_6]]#1 : !fir.ref +! CHECK: %[[VAL_7:.*]] = fir.load %[[VAL_6]]#0 : !fir.ref ! CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_7]] : (i64) -> i8 ! CHECK: %[[VAL_9:.*]] = fir.undefined !fir.char<1> ! CHECK: %[[VAL_10:.*]] = fir.insert_value %[[VAL_9]], %[[VAL_8]], [0 : index] : (!fir.char<1>, i8) -> !fir.char<1> diff --git a/flang/test/Lower/HLFIR/calls-f77.f90 b/flang/test/Lower/HLFIR/calls-f77.f90 index d736865a41beb..ec78718d513cf 100644 --- a/flang/test/Lower/HLFIR/calls-f77.f90 +++ b/flang/test/Lower/HLFIR/calls-f77.f90 @@ -157,7 +157,7 @@ subroutine return_char(n) ! CHECK-LABEL: func.func @_QPreturn_char( ! CHECK: %[[VAL_1:.*]]:2 = hlfir.declare {{.*}}n ! CHECK: %[[VAL_2:.*]] = arith.constant -1 : i32 -! CHECK: %[[VAL_7:.*]] = fir.load %[[VAL_1]]#1 : !fir.ref +! CHECK: %[[VAL_7:.*]] = fir.load %[[VAL_1]]#0 : !fir.ref ! CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_7]] : (i64) -> index ! CHECK: %[[VAL_9:.*]] = arith.constant 0 : index ! CHECK: %[[VAL_10:.*]] = arith.cmpi sgt, %[[VAL_8]], %[[VAL_9]] : index diff --git a/flang/test/Lower/HLFIR/convert-mbox-to-value.f90 b/flang/test/Lower/HLFIR/convert-mbox-to-value.f90 index 7c33beb16a0c1..52b9804fadc05 100644 --- a/flang/test/Lower/HLFIR/convert-mbox-to-value.f90 +++ b/flang/test/Lower/HLFIR/convert-mbox-to-value.f90 @@ -54,12 +54,13 @@ end subroutine test_char_allocatable ! CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_3]] {uniq_name = "_QFtest_char_allocatableEi"} : (!fir.ref) -> (!fir.ref, !fir.ref) ! CHECK: %[[VAL_5:.*]] = fir.load %[[VAL_2]]#0 : !fir.ref>>> ! CHECK: %[[VAL_6:.*]] = fir.box_addr %[[VAL_5]] : (!fir.box>>) -> !fir.heap> +! CHECK: %[[VAL_1B:.*]] = arith.constant 11 : index ! CHECK: %[[VAL_7:.*]] = arith.constant 1 : index ! CHECK: %[[VAL_8:.*]] = arith.constant -1 : index ! CHECK: %[[VAL_9:.*]] = arith.constant 0 : index ! CHECK: %[[VAL_10:.*]] = arith.constant true ! CHECK: %[[VAL_11:.*]] = arith.constant 32 : i8 -! CHECK: %[[VAL_12:.*]] = arith.subi %[[VAL_1]], %[[VAL_7]] : index +! CHECK: %[[VAL_12:.*]] = arith.subi %[[VAL_1B]], %[[VAL_7]] : index ! CHECK: %[[VAL_13:.*]]:2 = fir.iterate_while (%[[VAL_14:.*]] = %[[VAL_12]] to %[[VAL_9]] step %[[VAL_8]]) and (%[[VAL_15:.*]] = %[[VAL_10]]) iter_args(%[[VAL_16:.*]] = %[[VAL_12]]) -> (index) { ! CHECK: %[[VAL_17:.*]] = fir.convert %[[VAL_6]] : (!fir.heap>) -> !fir.ref>> ! CHECK: %[[VAL_18:.*]] = fir.coordinate_of %[[VAL_17]], %[[VAL_14]] : (!fir.ref>>, index) -> !fir.ref> @@ -88,12 +89,13 @@ end subroutine test_char_pointer ! CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_0]] typeparams %[[VAL_3]] {fortran_attrs = #fir.var_attrs, uniq_name = "_QFtest_char_pointerEp"} : (!fir.ref>>>, index) -> (!fir.ref>>>, !fir.ref>>>) ! CHECK: %[[VAL_5:.*]] = fir.load %[[VAL_4]]#0 : !fir.ref>>> ! CHECK: %[[VAL_6:.*]] = fir.box_addr %[[VAL_5]] : (!fir.box>>) -> !fir.ptr> +! CHECK: %[[VAL_3B:.*]] = arith.constant 11 : index ! CHECK: %[[VAL_7:.*]] = arith.constant 1 : index ! CHECK: %[[VAL_8:.*]] = arith.constant -1 : index ! CHECK: %[[VAL_9:.*]] = arith.constant 0 : index ! CHECK: %[[VAL_10:.*]] = arith.constant true ! CHECK: %[[VAL_11:.*]] = arith.constant 32 : i8 -! CHECK: %[[VAL_12:.*]] = arith.subi %[[VAL_3]], %[[VAL_7]] : index +! CHECK: %[[VAL_12:.*]] = arith.subi %[[VAL_3B]], %[[VAL_7]] : index ! CHECK: %[[VAL_13:.*]]:2 = fir.iterate_while (%[[VAL_14:.*]] = %[[VAL_12]] to %[[VAL_9]] step %[[VAL_8]]) and (%[[VAL_15:.*]] = %[[VAL_10]]) iter_args(%[[VAL_16:.*]] = %[[VAL_12]]) -> (index) { ! CHECK: %[[VAL_17:.*]] = fir.convert %[[VAL_6]] : (!fir.ptr>) -> !fir.ref>> ! CHECK: %[[VAL_18:.*]] = fir.coordinate_of %[[VAL_17]], %[[VAL_14]] : (!fir.ref>>, index) -> !fir.ref> @@ -205,14 +207,15 @@ end subroutine test_derived_allocatable ! CHECK: %[[VAL_12:.*]] = fir.embox %[[VAL_11]] : (!fir.heap>) -> !fir.class>> ! CHECK: fir.store %[[VAL_12]] to %[[VAL_10]] : !fir.ref>>> ! CHECK: %[[VAL_13:.*]]:2 = hlfir.declare %[[VAL_10]] {fortran_attrs = #fir.var_attrs, uniq_name = "_QFtest_derived_allocatableEr"} : (!fir.ref>>>) -> (!fir.ref>>>, !fir.ref>>>) -! CHECK: %[[VAL_14:.*]] = fir.load %[[VAL_4]]#1 : !fir.ref>>> -! CHECK: %[[VAL_15:.*]] = fir.load %[[VAL_8]]#1 : !fir.ref>>> -! CHECK: %[[VAL_16:.*]] = fir.load %[[VAL_9]]#1 : !fir.ref> +! CHECK: %[[VAL_14:.*]] = fir.load %[[VAL_4]]#0 : !fir.ref>>> +! CHECK: %[[VAL_14B:.*]] = fir.box_addr %[[VAL_14]] : (!fir.box>>) -> !fir.heap> +! CHECK: %[[VAL_15:.*]] = fir.load %[[VAL_8]]#0 : !fir.ref>>> +! CHECK: %[[VAL_16:.*]] = fir.load %[[VAL_9]]#0 : !fir.ref> ! CHECK: %[[VAL_17:.*]] = fir.convert %[[VAL_16]] : (!fir.logical<4>) -> i1 -! CHECK: %[[VAL_18:.*]] = fir.rebox %[[VAL_15]] : (!fir.class>>) -> !fir.box>> -! CHECK: %[[VAL_19:.*]] = arith.select %[[VAL_17]], %[[VAL_14]], %[[VAL_18]] : !fir.box>> -! CHECK: %[[VAL_20:.*]]:2 = hlfir.declare %[[VAL_19]] {uniq_name = ".tmp.intrinsic_result"} : (!fir.box>>) -> (!fir.box>>, !fir.box>>) -! CHECK: %[[VAL_21:.*]] = hlfir.as_expr %[[VAL_20]]#0 : (!fir.box>>) -> !hlfir.expr> +! CHECK: %[[VAL_18:.*]] = fir.box_addr %[[VAL_15]] : (!fir.class>>) -> !fir.heap> +! CHECK: %[[VAL_19:.*]] = arith.select %[[VAL_17]], %[[VAL_14B]], %[[VAL_18]] : !fir.heap> +! CHECK: %[[VAL_20:.*]]:2 = hlfir.declare %[[VAL_19]] {uniq_name = ".tmp.intrinsic_result"} : (!fir.heap>) -> (!fir.heap>, !fir.heap>) +! CHECK: %[[VAL_21:.*]] = hlfir.as_expr %[[VAL_20]]#0 : (!fir.heap>) -> !hlfir.expr> ! CHECK: hlfir.assign %[[VAL_21]] to %[[VAL_13]]#0 realloc : !hlfir.expr>, !fir.ref>>> ! CHECK: hlfir.destroy %[[VAL_21]] : !hlfir.expr> ! CHECK: return @@ -244,14 +247,15 @@ end subroutine test_derived_pointer ! CHECK: %[[VAL_12:.*]] = fir.embox %[[VAL_11]] : (!fir.heap>) -> !fir.class>> ! CHECK: fir.store %[[VAL_12]] to %[[VAL_10]] : !fir.ref>>> ! CHECK: %[[VAL_13:.*]]:2 = hlfir.declare %[[VAL_10]] {fortran_attrs = #fir.var_attrs, uniq_name = "_QFtest_derived_pointerEr"} : (!fir.ref>>>) -> (!fir.ref>>>, !fir.ref>>>) -! CHECK: %[[VAL_14:.*]] = fir.load %[[VAL_4]]#1 : !fir.ref>>> -! CHECK: %[[VAL_15:.*]] = fir.load %[[VAL_8]]#1 : !fir.ref>>> -! CHECK: %[[VAL_16:.*]] = fir.load %[[VAL_9]]#1 : !fir.ref> +! CHECK: %[[VAL_14:.*]] = fir.load %[[VAL_4]]#0 : !fir.ref>>> +! CHECK: %[[VAL_14B:.*]] = fir.box_addr %[[VAL_14]] : (!fir.box>>) -> !fir.ptr> +! CHECK: %[[VAL_15:.*]] = fir.load %[[VAL_8]]#0 : !fir.ref>>> +! CHECK: %[[VAL_16:.*]] = fir.load %[[VAL_9]]#0 : !fir.ref> ! CHECK: %[[VAL_17:.*]] = fir.convert %[[VAL_16]] : (!fir.logical<4>) -> i1 -! CHECK: %[[VAL_18:.*]] = fir.rebox %[[VAL_15]] : (!fir.class>>) -> !fir.box>> -! CHECK: %[[VAL_19:.*]] = arith.select %[[VAL_17]], %[[VAL_14]], %[[VAL_18]] : !fir.box>> -! CHECK: %[[VAL_20:.*]]:2 = hlfir.declare %[[VAL_19]] {uniq_name = ".tmp.intrinsic_result"} : (!fir.box>>) -> (!fir.box>>, !fir.box>>) -! CHECK: %[[VAL_21:.*]] = hlfir.as_expr %[[VAL_20]]#0 : (!fir.box>>) -> !hlfir.expr> +! CHECK: %[[VAL_18:.*]] = fir.box_addr %[[VAL_15]] : (!fir.class>>) -> !fir.ptr> +! CHECK: %[[VAL_19:.*]] = arith.select %[[VAL_17]], %[[VAL_14B]], %[[VAL_18]] : !fir.ptr> +! CHECK: %[[VAL_20:.*]]:2 = hlfir.declare %[[VAL_19]] {uniq_name = ".tmp.intrinsic_result"} : (!fir.ptr>) -> (!fir.ptr>, !fir.ptr>) +! CHECK: %[[VAL_21:.*]] = hlfir.as_expr %[[VAL_20]]#0 : (!fir.ptr>) -> !hlfir.expr> ! CHECK: hlfir.assign %[[VAL_21]] to %[[VAL_13]]#0 realloc : !hlfir.expr>, !fir.ref>>> ! CHECK: hlfir.destroy %[[VAL_21]] : !hlfir.expr> ! CHECK: return diff --git a/flang/test/Lower/HLFIR/expr-as-inquired.f90 b/flang/test/Lower/HLFIR/expr-as-inquired.f90 index 1186d08c9f3f9..455ddf6a44281 100644 --- a/flang/test/Lower/HLFIR/expr-as-inquired.f90 +++ b/flang/test/Lower/HLFIR/expr-as-inquired.f90 @@ -33,7 +33,7 @@ subroutine test_lbound(x, n) ! CHECK: %[[VAL_7:.*]] = arith.constant 3 : i64 ! CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_7]] : (i64) -> index ! CHECK: %[[VAL_10:.*]]:2 = hlfir.declare %{{.*}}(%{{.*}}) {{.*}}Ex -! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_4]]#1 : !fir.ref +! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_4]]#0 : !fir.ref ! CHECK: %[[VAL_12:.*]] = fir.shift %[[VAL_6]], %[[VAL_8]] : (index, index) -> !fir.shift<2> ! CHECK: %[[VAL_13:.*]] = fir.rebox %[[VAL_10]]#1(%[[VAL_12]]) : (!fir.box>, !fir.shift<2>) -> !fir.box> ! CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_13]] : (!fir.box>) -> !fir.box diff --git a/flang/test/Lower/HLFIR/expr-value.f90 b/flang/test/Lower/HLFIR/expr-value.f90 index bcb5f4deae29b..cd2f42533c273 100644 --- a/flang/test/Lower/HLFIR/expr-value.f90 +++ b/flang/test/Lower/HLFIR/expr-value.f90 @@ -13,6 +13,6 @@ subroutine foo() subroutine foo_designator(n) !CHECK: %[[n:.*]]:2 = hlfir.declare %[[arg0]] {uniq_name = "_QFfoo_designatorEn"} : (!fir.ref) -> (!fir.ref, !fir.ref) print *, n - ! CHECK: %[[nval:.*]] = fir.load %[[n]]#1 : !fir.ref + ! CHECK: %[[nval:.*]] = fir.load %[[n]]#0 : !fir.ref ! CHECK: fir.call @_FortranAioOutputInteger32(%{{.*}}, %[[nval]]) {{.*}}: (!fir.ref, i32) -> i1 end subroutine diff --git a/flang/test/Lower/HLFIR/forall.f90 b/flang/test/Lower/HLFIR/forall.f90 index 1485ceb60559a..9941ed1940103 100644 --- a/flang/test/Lower/HLFIR/forall.f90 +++ b/flang/test/Lower/HLFIR/forall.f90 @@ -52,7 +52,7 @@ subroutine test_forall_step(step) ! CHECK: %[[VAL_4:.*]]:2 = hlfir.declare {{.*}}Estep ! CHECK: %[[VAL_7:.*]]:2 = hlfir.declare {{.*}}Ex ! CHECK: %[[VAL_10:.*]]:2 = hlfir.declare {{.*}}Ey -! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_4]]#1 : !fir.ref +! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_4]]#0 : !fir.ref ! CHECK: hlfir.forall lb { ! CHECK: hlfir.yield %[[VAL_2]] : i32 ! CHECK: } ub {