Skip to content

Commit 8949f51

Browse files
committed
To address review comments and also check if LHS is polymorphic to decide which type to use.
1 parent 0807368 commit 8949f51

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

flang/include/flang/Optimizer/Builder/HLFIRTools.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ class Entity : public mlir::Value {
9898
mlir::Type getElementOrSequenceType() const {
9999
return hlfir::getFortranElementOrSequenceType(getType());
100100
}
101+
/// Return the fir.class or fir.box type needed to describe this entity.
102+
fir::BaseBoxType getBoxType() const {
103+
if (isBoxAddressOrValue())
104+
return llvm::cast<fir::BaseBoxType>(fir::unwrapRefType(getType()));
105+
const bool isVolatile = fir::isa_volatile_type(getType());
106+
return fir::BoxType::get(getElementOrSequenceType(), isVolatile);
107+
}
101108

102109
bool hasLengthParameters() const {
103110
mlir::Type eleTy = getFortranElementType();

flang/lib/Lower/Bridge.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4730,14 +4730,23 @@ class FirConverter : public Fortran::lower::AbstractConverter {
47304730
if (Fortran::evaluate::UnwrapExpr<Fortran::evaluate::NullPointer>(
47314731
assign.rhs))
47324732
return fir::factory::createUnallocatedBox(*builder, loc, lhsBoxType, {});
4733-
hlfir::Entity rhs{fir::getBase(genExprBox(loc, assign.rhs, rhsContext))};
4734-
auto rhsBoxType =
4735-
llvm::cast<fir::BaseBoxType>(fir::unwrapRefType(rhs.getType()));
4733+
hlfir::Entity rhs = Fortran::lower::convertExprToHLFIR(
4734+
loc, *this, assign.rhs, localSymbols, rhsContext);
4735+
auto rhsBoxType = rhs.getBoxType();
47364736
// Create pointer descriptor value from the RHS.
47374737
if (rhs.isMutableBox())
47384738
rhs = hlfir::Entity{fir::LoadOp::create(*builder, loc, rhs)};
4739-
mlir::Value rhsBox = hlfir::genVariableBox(
4740-
loc, *builder, rhs, rhsBoxType.getBoxTypeWithNewShape(rhs.getRank()));
4739+
4740+
// Use LHS type if LHS is not polymorphic.
4741+
fir::BaseBoxType targetBoxType;
4742+
if (assign.lhs.GetType()->IsPolymorphic())
4743+
targetBoxType = rhsBoxType.getBoxTypeWithNewAttr(
4744+
fir::BaseBoxType::Attribute::Pointer);
4745+
else
4746+
targetBoxType = lhsBoxType.getBoxTypeWithNewShape(rhs.getRank());
4747+
mlir::Value rhsBox =
4748+
hlfir::genVariableBox(loc, *builder, rhs, targetBoxType);
4749+
47414750
// Apply lower bounds or reshaping if any.
47424751
if (const auto *lbExprs =
47434752
std::get_if<Fortran::evaluate::Assignment::BoundsSpec>(&assign.u);

0 commit comments

Comments
 (0)