Skip to content

Commit b209722

Browse files
authored
Merge pull request #949 from flang-compiler/ch-i917
Fix issue #917.
2 parents 9065951 + b5bb1ef commit b209722

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

flang/lib/Lower/Allocatable.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -417,27 +417,35 @@ class MutablePropertyWriter {
417417
// Ignore lengths if already constant in the box type (this would trigger an
418418
// error in the embox).
419419
llvm::SmallVector<mlir::Value> cleanedLengths;
420-
auto cleanedAddr = addr;
421-
if (auto charTy = box.getEleTy().dyn_cast<fir::CharacterType>()) {
422-
// Cast address to box type so that both input and output type have
423-
// unknown or constant lengths.
424-
auto bt = box.getBaseTy();
425-
auto addrTy = addr.getType();
426-
auto type = addrTy.isa<fir::HeapType>() ? fir::HeapType::get(bt)
427-
: addrTy.isa<fir::PointerType>()
428-
? fir::PointerType::get(bt)
429-
: builder.getRefType(bt);
430-
cleanedAddr = builder.createConvert(loc, type, addr);
431-
if (charTy.getLen() == fir::CharacterType::unknownLen())
432-
cleanedLengths.append(lengths.begin(), lengths.end());
433-
} else if (box.isDerivedWithLengthParameters()) {
434-
TODO(loc, "updating mutablebox of derived type with length parameters");
435-
cleanedLengths = lengths;
420+
mlir::Value irBox;
421+
if (addr.getType().isa<fir::BoxType>()) {
422+
// The entity is already boxed.
423+
irBox = builder.createConvert(loc, box.getBoxTy(), addr);
424+
} else {
425+
auto cleanedAddr = addr;
426+
if (auto charTy = box.getEleTy().dyn_cast<fir::CharacterType>()) {
427+
// Cast address to box type so that both input and output type have
428+
// unknown or constant lengths.
429+
auto bt = box.getBaseTy();
430+
auto addrTy = addr.getType();
431+
auto type = addrTy.isa<fir::HeapType>()
432+
? fir::HeapType::get(bt)
433+
: addrTy.isa<fir::PointerType>()
434+
? fir::PointerType::get(bt)
435+
: builder.getRefType(bt);
436+
cleanedAddr = builder.createConvert(loc, type, addr);
437+
if (charTy.getLen() == fir::CharacterType::unknownLen())
438+
cleanedLengths.append(lengths.begin(), lengths.end());
439+
} else if (box.isDerivedWithLengthParameters()) {
440+
TODO(loc, "updating mutablebox of derived type with length parameters");
441+
cleanedLengths = lengths;
442+
}
443+
irBox = builder.create<fir::EmboxOp>(loc, box.getBoxTy(), cleanedAddr,
444+
shape, emptySlice, cleanedLengths);
436445
}
437-
auto irBox = builder.create<fir::EmboxOp>(
438-
loc, box.getBoxTy(), cleanedAddr, shape, emptySlice, cleanedLengths);
439446
builder.create<fir::StoreOp>(loc, irBox, box.getAddr());
440447
}
448+
441449
/// Update the set of property variables of the MutableBoxValue.
442450
void updateMutableProperties(mlir::Value addr, mlir::ValueRange lbounds,
443451
mlir::ValueRange extents,

0 commit comments

Comments
 (0)