Skip to content

Commit a6924c6

Browse files
committed
[flang] Use input type when emboxing/reboxing polymorphic entities
When emboxing an entity to a polymorphic box, use the input type to compute the type code and element size as the box type is too generic. When reboxing a polymorphic box, get this information from the input box. Reviewed By: jeanPerier Differential Revision: https://reviews.llvm.org/D139916
1 parent a9fae58 commit a6924c6

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,18 +1554,18 @@ struct EmboxCommonConversion : public FIROpConversion<OP> {
15541554
mlir::Value typeDesc = {}) const {
15551555
auto loc = box.getLoc();
15561556
auto boxTy = box.getType().template dyn_cast<fir::BaseBoxType>();
1557-
bool isUnlimitedPolymorphic = fir::isUnlimitedPolymorphicType(boxTy);
1558-
bool useInputType =
1559-
isUnlimitedPolymorphic && !fir::isUnlimitedPolymorphicType(inputType);
1557+
bool useInputType = fir::isPolymorphicType(boxTy) &&
1558+
!fir::isUnlimitedPolymorphicType(inputType);
15601559
llvm::SmallVector<mlir::Value> typeparams = lenParams;
15611560
if constexpr (!std::is_same_v<BOX, fir::EmboxOp>) {
15621561
if (!box.getSubstr().empty() && fir::hasDynamicSize(boxTy.getEleTy()))
15631562
typeparams.push_back(box.getSubstr()[1]);
15641563
}
15651564

15661565
// Write each of the fields with the appropriate values.
1567-
// When emboxing an element to a unlimited polymorphic descriptor, use the
1568-
// input type since the destination descriptor type as no type information.
1566+
// When emboxing an element to a polymorphic descriptor, use the
1567+
// input type since the destination descriptor type has not the exact
1568+
// information.
15691569
auto [eleSize, cfiTy] = getSizeAndTypeCode(
15701570
loc, rewriter, useInputType ? inputType : boxTy.getEleTy(), typeparams);
15711571
auto mod = box->template getParentOfType<mlir::ModuleOp>();
@@ -1589,10 +1589,10 @@ struct EmboxCommonConversion : public FIROpConversion<OP> {
15891589
auto [eleSize, cfiTy] =
15901590
getSizeAndTypeCode(loc, rewriter, boxTy.getEleTy(), typeparams);
15911591

1592-
// Reboxing an unlimited polymorphic entities. eleSize and type code need to
1593-
// be retrived from the initial box.
1594-
if (fir::isUnlimitedPolymorphicType(boxTy) &&
1595-
fir::isUnlimitedPolymorphicType(box.getBox().getType())) {
1592+
// Reboxing a polymorphic entities. eleSize and type code need to
1593+
// be retrived from the initial box and propagated to the new box.
1594+
if (fir::isPolymorphicType(boxTy) &&
1595+
fir::isPolymorphicType(box.getBox().getType())) {
15961596
mlir::Type idxTy = this->lowerTy().indexType();
15971597
eleSize = this->loadElementSizeFromBox(loc, idxTy, loweredBox, rewriter);
15981598
cfiTy = this->getValueFromBox(loc, loweredBox, cfiTy.getType(), rewriter,

flang/test/Lower/allocatable-polymorphic.f90

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,18 +408,28 @@ program test_alloc
408408
! LLVM: store { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] } %[[C1_LOAD]], ptr %{{.*}}
409409
! LLVM: %[[GEP_TDESC_C1:.*]] = getelementptr { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] }, ptr %{{.*}}, i32 0, i32 7
410410
! LLVM: %[[TDESC_C1:.*]] = load ptr, ptr %[[GEP_TDESC_C1]]
411-
! LLVM: %[[BOX0:.*]] = insertvalue { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] } { ptr undef, i64 ptrtoint (ptr getelementptr (%_QMpolyTp1, ptr null, i32 1) to i64), i32 20180515, i8 0, i8 42, i8 0, i8 1, ptr undef, [1 x i64] undef }, ptr %[[TDESC_C1]], 7
412-
! LLVM: %[[BOX1:.*]] = insertvalue { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] } %[[BOX0]], ptr %{{.*}}, 0
413-
! LLVM: store { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] } %[[BOX1]], ptr %[[TMP:.*]]
411+
! LLVM: %[[ELEM_SIZE_GEP:.*]] = getelementptr { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] }, ptr %{{.}}, i32 0, i32 1
412+
! LLVM: %[[ELEM_SIZE:.*]] = load i64, ptr %[[ELEM_SIZE_GEP]]
413+
! LLVM: %[[TYPE_CODE_GEP:.*]] = getelementptr { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] }, ptr %{{.}}, i32 0, i32 4
414+
! LLVM: %[[TYPE_CODE:.*]] = load i32, ptr %[[TYPE_CODE_GEP]]
415+
! LLVM: %{{.*}} = insertvalue { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] } undef, i64 %[[ELEM_SIZE]], 1
416+
! LLVM: %[[TRUNC_TYPE_CODE:.*]] = trunc i32 %[[TYPE_CODE]] to i8
417+
! LLVM: %{{.*}} = insertvalue { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] } %{{.*}}, i8 %[[TRUNC_TYPE_CODE]], 4
418+
! LLVM: store { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] } %{{.*}}, ptr %[[TMP:.*]]
414419
! LLVM: call void %{{.*}}(ptr %{{.*}})
415420

416421
! LLVM: %[[LOAD_C2:.*]] = load { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] }, ptr %{{.*}}
417422
! LLVM: store { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] } %[[LOAD_C2]], ptr %{{.*}}
418423
! LLVM: %[[GEP_TDESC_C2:.*]] = getelementptr { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] }, ptr %{{.*}}, i32 0, i32 7
419424
! LLVM: %[[TDESC_C2:.*]] = load ptr, ptr %[[GEP_TDESC_C2]]
420-
! LLVM: %[[BOX0:.*]] = insertvalue { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] } { ptr undef, i64 ptrtoint (ptr getelementptr (%_QMpolyTp1, ptr null, i32 1) to i64), i32 20180515, i8 0, i8 42, i8 0, i8 1, ptr undef, [1 x i64] undef }, ptr %[[TDESC_C2]], 7
421-
! LLVM: %[[BOX1:.*]] = insertvalue { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] } %[[BOX0]], ptr %{{.*}}, 0
422-
! LLVM: store { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] } %[[BOX1]], ptr %{{.*}}
425+
! LLVM: %[[ELEM_SIZE_GEP:.*]] = getelementptr { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] }, ptr %{{.}}, i32 0, i32 1
426+
! LLVM: %[[ELEM_SIZE:.*]] = load i64, ptr %[[ELEM_SIZE_GEP]]
427+
! LLVM: %[[TYPE_CODE_GEP:.*]] = getelementptr { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] }, ptr %{{.}}, i32 0, i32 4
428+
! LLVM: %[[TYPE_CODE:.*]] = load i32, ptr %[[TYPE_CODE_GEP]]
429+
! LLVM: %{{.*}} = insertvalue { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] } undef, i64 %[[ELEM_SIZE]], 1
430+
! LLVM: %[[TRUNC_TYPE_CODE:.*]] = trunc i32 %[[TYPE_CODE]] to i8
431+
! LLVM: %{{.*}} = insertvalue { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] } %{{.*}}, i8 %[[TRUNC_TYPE_CODE]], 4
432+
! LLVM: store { ptr, i64, i32, i8, i8, i8, i8, ptr, [1 x i64] } %{{.*}}, ptr %{{.*}}
423433
! LLVM: call void %{{.*}}(ptr %{{.*}})
424434

425435
! LLVM: %[[C3_LOAD:.*]] = load { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]], ptr, [1 x i64] }, ptr %{{.*}}

0 commit comments

Comments
 (0)