diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp --- a/flang/lib/Lower/Bridge.cpp +++ b/flang/lib/Lower/Bridge.cpp @@ -2731,12 +2731,15 @@ Fortran::lower::StatementContext stmtCtx; if (Fortran::lower::isWholeAllocatable(assign.lhs)) TODO(loc, "HLFIR assignment to whole allocatable"); - hlfir::EntityWithAttributes rhs = - Fortran::lower::convertExprToHLFIR(loc, *this, assign.rhs, - localSymbols, stmtCtx); - hlfir::EntityWithAttributes lhs = - Fortran::lower::convertExprToHLFIR(loc, *this, assign.lhs, - localSymbols, stmtCtx); + hlfir::Entity rhs = Fortran::lower::convertExprToHLFIR( + loc, *this, assign.rhs, localSymbols, stmtCtx); + // Dereference pointers and allocatables RHS: the target is + // being assigned from. + rhs = hlfir::derefPointersAndAllocatables(loc, builder, rhs); + hlfir::Entity lhs = Fortran::lower::convertExprToHLFIR( + loc, *this, assign.lhs, localSymbols, stmtCtx); + // Dereference pointers LHS: the target is being assigned to. + lhs = hlfir::derefPointersAndAllocatables(loc, builder, lhs); builder.create(loc, rhs, lhs); }, // [2] User defined assignment. If the context is a scalar diff --git a/flang/lib/Optimizer/Builder/HLFIRTools.cpp b/flang/lib/Optimizer/Builder/HLFIRTools.cpp --- a/flang/lib/Optimizer/Builder/HLFIRTools.cpp +++ b/flang/lib/Optimizer/Builder/HLFIRTools.cpp @@ -570,9 +570,9 @@ if (entity.isMutableBox()) { hlfir::Entity boxLoad{builder.create(loc, entity)}; if (entity.isScalar()) { - mlir::Type elementType = boxLoad.getFortranElementType(); - if (fir::isa_trivial(elementType)) + if (!entity.isPolymorphic() && !entity.hasLengthParameters()) return hlfir::Entity{builder.create(loc, boxLoad)}; + mlir::Type elementType = boxLoad.getFortranElementType(); if (auto charType = elementType.dyn_cast()) { mlir::Value base = builder.create(loc, boxLoad); if (charType.hasConstantLen()) @@ -585,7 +585,10 @@ .getResult()}; } } - // Keep the entity boxed for now. + // Otherwise, the entity is either an array, a polymorphic entity, or a + // derived type with length parameters. All these entities require a fir.box + // or fir.class to hold bounds, dynamic type or length parameter + // information. Keep them boxed. return boxLoad; } return entity; diff --git a/flang/test/Lower/HLFIR/allocatable-and-pointer-subparts.f90 b/flang/test/Lower/HLFIR/allocatable-and-pointer-subparts.f90 --- a/flang/test/Lower/HLFIR/allocatable-and-pointer-subparts.f90 +++ b/flang/test/Lower/HLFIR/allocatable-and-pointer-subparts.f90 @@ -26,7 +26,8 @@ ! CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %{{.*}} {{.*}}Ex ! CHECK: %[[VAL_2:.*]] = hlfir.designate %[[VAL_1]]#0{"p"} {fortran_attrs = #fir.var_attrs} : (!fir.ref>>}>>) -> !fir.ref>>> ! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_2]] : !fir.ref>>> -! CHECK: %[[VAL_4:.*]] = hlfir.designate %[[VAL_3]]{"x"} : (!fir.box>>) -> !fir.ref +! CHECK: %[[VAL_4:.*]] = fir.box_addr %[[VAL_3:.*]] : (!fir.box>>) -> !fir.ptr> +! CHECK: hlfir.designate %[[VAL_4]]{"x"} : (!fir.ptr>) -> !fir.ref subroutine test_symbol_followed_by_ref(x) character(:), allocatable :: x(:) diff --git a/flang/test/Lower/HLFIR/assignment-intrinsics.f90 b/flang/test/Lower/HLFIR/assignment-intrinsics.f90 --- a/flang/test/Lower/HLFIR/assignment-intrinsics.f90 +++ b/flang/test/Lower/HLFIR/assignment-intrinsics.f90 @@ -135,6 +135,17 @@ ! CHECK: %[[VAL_11:.*]]:2 = hlfir.declare {{.*}} {uniq_name = "_QFarray_characterEy"} : (!fir.ref>>, !fir.shape<1>, index) -> (!fir.box>>, !fir.ref>>) ! CHECK: hlfir.assign %[[VAL_11]]#0 to %[[VAL_6]]#0 : !fir.box>>, !fir.box>> +subroutine array_pointer(x, y) + real, pointer :: x(:), y(:) + x = y +end subroutine +! CHECK-LABEL: func.func @_QParray_pointer( +! CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %{{.*}}Ex +! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %{{.*}}Ey +! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_2]]#0 : !fir.ref>>> +! CHECK: %[[VAL_4:.*]] = fir.load %[[VAL_1]]#0 : !fir.ref>>> +! CHECK: hlfir.assign %[[VAL_3]] to %[[VAL_4]] : !fir.box>>, !fir.box>> + ! ----------------------------------------------------------------------------- ! Test assignments with array LHS and scalar RHS ! -----------------------------------------------------------------------------