Skip to content

Commit

Permalink
[flang][hlfir] Added allocatable/pointer support in hlfir::convertToV…
Browse files Browse the repository at this point in the history
…alue.

The code is used, for example, when passing arguments to IO or intrinsic
calls as value. The allocatable/pointer boxes must be dereferenced,
and trivial values have to be loaded. Character and derived values
have to stay boxed.
I am not sure what to do for the array cases, and I have not seen
any test triggering it, so I leave it as a TODO.

Reviewed By: tblah, clementval

Differential Revision: https://reviews.llvm.org/D151925
  • Loading branch information
vzakhari committed Jun 2, 2023
1 parent 99dc683 commit 1c8b7c5
Show file tree
Hide file tree
Showing 2 changed files with 285 additions and 1 deletion.
28 changes: 27 additions & 1 deletion flang/lib/Optimizer/Builder/HLFIRTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ translateVariableToExtendedValue(mlir::Location loc, fir::FirOpBuilder &builder,
llvm::SmallVector<mlir::Value> nonDefaultLbounds;
if (variable.getType().isa<fir::BaseBoxType>() &&
!variable.getIfVariableInterface()) {
// This special case avoids generating two generating to sets of identical
// This special case avoids generating two sets of identical
// fir.box_dim to get both the lower bounds and extents.
genLboundsAndExtentsFromBox(loc, builder, variable, nonDefaultLbounds,
&extents);
Expand Down Expand Up @@ -928,7 +928,33 @@ hlfir::convertToValue(mlir::Location loc, fir::FirOpBuilder &builder,
[&](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<fir::LoadOp>(loc, derefedEntity);

if (mlir::isa<fir::CharacterType>(eleTy)) {
if (mlir::isa<fir::BoxCharType>(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};
Expand Down
Loading

0 comments on commit 1c8b7c5

Please sign in to comment.