Skip to content

Commit 658595d

Browse files
committed
[flang] Handle polymorphic entities with rank > 0 in entry statement
Correctly create the temporary for argument absent in the entry statement. Reviewed By: PeteSteinfeld Differential Revision: https://reviews.llvm.org/D146447
1 parent 8325d46 commit 658595d

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

flang/include/flang/Optimizer/Builder/MutableBox.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ mlir::Value createUnallocatedBox(fir::FirOpBuilder &builder, mlir::Location loc,
5252
fir::MutableBoxValue createTempMutableBox(fir::FirOpBuilder &builder,
5353
mlir::Location loc, mlir::Type type,
5454
llvm::StringRef name = {},
55-
mlir::Value sourceBox = {});
55+
mlir::Value sourceBox = {},
56+
bool isPolymorphic = false);
5657

5758
/// Update a MutableBoxValue to describe entity \p source (that must be in
5859
/// memory). If \lbounds is not empty, it is used to defined the MutableBoxValue

flang/lib/Lower/ConvertVariable.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,12 +1690,18 @@ void Fortran::lower::mapSymbolAttributes(
16901690
"handled above");
16911691
// The box is read right away because lowering code does not expect
16921692
// a non pointer/allocatable symbol to be mapped to a MutableBox.
1693+
mlir::Type ty = converter.genType(var);
1694+
bool isPolymorphic = false;
1695+
if (auto boxTy = ty.dyn_cast<fir::BaseBoxType>()) {
1696+
isPolymorphic = ty.isa<fir::ClassType>();
1697+
ty = boxTy.getEleTy();
1698+
}
16931699
Fortran::lower::genDeclareSymbol(
16941700
converter, symMap, sym,
16951701
fir::factory::genMutableBoxRead(
16961702
builder, loc,
1697-
fir::factory::createTempMutableBox(builder, loc,
1698-
converter.genType(var))));
1703+
fir::factory::createTempMutableBox(builder, loc, ty, {}, {},
1704+
isPolymorphic)));
16991705
return true;
17001706
}
17011707
return false;

flang/lib/Optimizer/Builder/MutableBox.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,9 @@ mlir::Value fir::factory::createUnallocatedBox(
366366

367367
fir::MutableBoxValue fir::factory::createTempMutableBox(
368368
fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type type,
369-
llvm::StringRef name, mlir::Value typeSourceBox) {
369+
llvm::StringRef name, mlir::Value typeSourceBox, bool isPolymorphic) {
370370
mlir::Type boxType;
371-
if (typeSourceBox)
371+
if (typeSourceBox || isPolymorphic)
372372
boxType = fir::ClassType::get(fir::HeapType::get(type));
373373
else
374374
boxType = fir::BoxType::get(fir::HeapType::get(type));

flang/test/Lower/polymorphic.f90

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,32 @@ subroutine class_with_entry(a)
11121112
! CHECK-SAME: %[[B:.*]]: !fir.class<!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>> {fir.bindc_name = "b"}) {
11131113
! CHECK: %[[A:.*]] = fir.alloca !fir.class<!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>> {bindc_name = "a", uniq_name = "_QMpolymorphic_testFclass_with_entryEa"}
11141114

1115+
subroutine class_array_with_entry(a)
1116+
class(p1) :: a(:), b(:)
1117+
select type (a)
1118+
type is(p2)
1119+
print*, a%c
1120+
class default
1121+
print*, a%a
1122+
end select
1123+
return
1124+
entry g(b)
1125+
select type(b)
1126+
type is(p2)
1127+
print*,b%c
1128+
class default
1129+
print*,b%a
1130+
end select
1131+
end subroutine
1132+
1133+
! CHECK-LABEL: func.func @_QMpolymorphic_testPclass_array_with_entry(
1134+
! CHECK-SAME: %[[A:.*]]: !fir.class<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>> {fir.bindc_name = "a"}) {
1135+
! CHECK: %[[B:.*]] = fir.alloca !fir.class<!fir.heap<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>>
1136+
1137+
! CHECK-LABEL: func.func @_QMpolymorphic_testPg(
1138+
! CHECK-SAME: %[[B:.*]]: !fir.class<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>> {fir.bindc_name = "b"}) {
1139+
! CHECK: %[[A:.*]] = fir.alloca !fir.class<!fir.heap<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>>
1140+
11151141
end module
11161142

11171143
program test

0 commit comments

Comments
 (0)