Skip to content

Commit 161d576

Browse files
jeanPeriermahesh-attarde
authored andcommitted
[flang] handle scalars in getDescriptorWithNewBaseAddress (llvm#161515)
Follow up on llvm#161347 to allow scalar fir.box/class reconstruction (at least required for polymorphic types). The assert in genDimInfoFromBox was rejecting scalars while there is no functional reason for that (only assumed-rank are an issue there).
1 parent 539213e commit 161d576

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

flang/lib/Optimizer/Builder/FIRBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,7 @@ void fir::factory::genDimInfoFromBox(
19431943
return;
19441944

19451945
unsigned rank = fir::getBoxRank(boxType);
1946-
assert(rank != 0 && "must be an array of known rank");
1946+
assert(!boxType.isAssumedRank() && "must be an array of known rank");
19471947
mlir::Type idxTy = builder.getIndexType();
19481948
for (unsigned i = 0; i < rank; ++i) {
19491949
mlir::Value dim = builder.createIntegerConstant(loc, idxTy, i);

flang/unittests/Optimizer/Builder/FIRBuilderTest.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,3 +691,40 @@ TEST_F(FIRBuilderTest, getDescriptorWithNewBaseAddress) {
691691
auto lbOp = llvm::dyn_cast<fir::BoxDimsOp>(origin0.getDefiningOp());
692692
EXPECT_EQ(lbOp.getVal(), inputBox);
693693
}
694+
695+
TEST_F(FIRBuilderTest, getDescriptorWithNewBaseAddress_PolymorphicScalar) {
696+
auto builder = getBuilder();
697+
auto loc = builder.getUnknownLoc();
698+
699+
// Build a polymorphic scalar: fir.class<ptr<!fir.type<rec>>>.
700+
auto recTy = fir::RecordType::get(builder.getContext(), "poly_rec");
701+
auto ptrRecTy = fir::PointerType::get(recTy);
702+
auto classTy = fir::ClassType::get(ptrRecTy);
703+
704+
// Input descriptor is an undefined fir.class value.
705+
mlir::Value inputBox = fir::UndefOp::create(builder, loc, classTy);
706+
707+
// New base address of the same element type (reference to the record).
708+
auto refRecTy = fir::ReferenceType::get(recTy);
709+
mlir::Value newAddr = fir::UndefOp::create(builder, loc, refRecTy);
710+
711+
mlir::Value newBox = fir::factory::getDescriptorWithNewBaseAddress(
712+
builder, loc, inputBox, newAddr);
713+
714+
// Same descriptor type must be preserved.
715+
EXPECT_EQ(newBox.getType(), inputBox.getType());
716+
717+
// Must be an embox using the new base address and carrying the original box
718+
// as mold.
719+
ASSERT_TRUE(llvm::isa_and_nonnull<fir::EmboxOp>(newBox.getDefiningOp()));
720+
auto embox = llvm::dyn_cast<fir::EmboxOp>(newBox.getDefiningOp());
721+
EXPECT_EQ(embox.getMemref(), newAddr);
722+
723+
// Polymorphic scalar should have no shape operand.
724+
mlir::Value shape = embox.getShape();
725+
EXPECT_TRUE(shape == nullptr);
726+
727+
// The type descriptor/mold must be the original input box.
728+
mlir::Value tdesc = embox.getSourceBox();
729+
EXPECT_EQ(tdesc, inputBox);
730+
}

0 commit comments

Comments
 (0)