diff --git a/flang/lib/Lower/Allocatable.cpp b/flang/lib/Lower/Allocatable.cpp index 2fd2f3dc359044..2cf6a34cf8f949 100644 --- a/flang/lib/Lower/Allocatable.cpp +++ b/flang/lib/Lower/Allocatable.cpp @@ -417,27 +417,35 @@ class MutablePropertyWriter { // Ignore lengths if already constant in the box type (this would trigger an // error in the embox). llvm::SmallVector cleanedLengths; - auto cleanedAddr = addr; - if (auto charTy = box.getEleTy().dyn_cast()) { - // Cast address to box type so that both input and output type have - // unknown or constant lengths. - auto bt = box.getBaseTy(); - auto addrTy = addr.getType(); - auto type = addrTy.isa() ? fir::HeapType::get(bt) - : addrTy.isa() - ? fir::PointerType::get(bt) - : builder.getRefType(bt); - cleanedAddr = builder.createConvert(loc, type, addr); - if (charTy.getLen() == fir::CharacterType::unknownLen()) - cleanedLengths.append(lengths.begin(), lengths.end()); - } else if (box.isDerivedWithLengthParameters()) { - TODO(loc, "updating mutablebox of derived type with length parameters"); - cleanedLengths = lengths; + mlir::Value irBox; + if (addr.getType().isa()) { + // The entity is already boxed. + irBox = builder.createConvert(loc, box.getBoxTy(), addr); + } else { + auto cleanedAddr = addr; + if (auto charTy = box.getEleTy().dyn_cast()) { + // Cast address to box type so that both input and output type have + // unknown or constant lengths. + auto bt = box.getBaseTy(); + auto addrTy = addr.getType(); + auto type = addrTy.isa() + ? fir::HeapType::get(bt) + : addrTy.isa() + ? fir::PointerType::get(bt) + : builder.getRefType(bt); + cleanedAddr = builder.createConvert(loc, type, addr); + if (charTy.getLen() == fir::CharacterType::unknownLen()) + cleanedLengths.append(lengths.begin(), lengths.end()); + } else if (box.isDerivedWithLengthParameters()) { + TODO(loc, "updating mutablebox of derived type with length parameters"); + cleanedLengths = lengths; + } + irBox = builder.create(loc, box.getBoxTy(), cleanedAddr, + shape, emptySlice, cleanedLengths); } - auto irBox = builder.create( - loc, box.getBoxTy(), cleanedAddr, shape, emptySlice, cleanedLengths); builder.create(loc, irBox, box.getAddr()); } + /// Update the set of property variables of the MutableBoxValue. void updateMutableProperties(mlir::Value addr, mlir::ValueRange lbounds, mlir::ValueRange extents,