Skip to content

Commit 4143d3e

Browse files
committed
[flang] Unlimited polymoprhic allocated as character
Allocation of unlimited polymorphic allocatable with character intrinsic type is now done through `PointerNullifyCharacter` or `AllocatableInitCharacter` so the length is correctly set. Reviewed By: jeanPerier Differential Revision: https://reviews.llvm.org/D143580
1 parent 1c10d5b commit 4143d3e

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

flang/lib/Lower/Allocatable.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static void genRuntimeSetBounds(fir::FirOpBuilder &builder, mlir::Location loc,
141141
static void genRuntimeInitCharacter(fir::FirOpBuilder &builder,
142142
mlir::Location loc,
143143
const fir::MutableBoxValue &box,
144-
mlir::Value len) {
144+
mlir::Value len, int64_t kind = 0) {
145145
mlir::func::FuncOp callee =
146146
box.isPointer()
147147
? fir::runtime::getRuntimeFunc<mkRTKey(PointerNullifyCharacter)>(
@@ -155,7 +155,8 @@ static void genRuntimeInitCharacter(fir::FirOpBuilder &builder,
155155
llvm::SmallVector<mlir::Value> args;
156156
args.push_back(builder.createConvert(loc, inputTypes[0], box.getAddr()));
157157
args.push_back(builder.createConvert(loc, inputTypes[1], len));
158-
int kind = box.getEleTy().cast<fir::CharacterType>().getFKind();
158+
if (kind == 0)
159+
kind = box.getEleTy().cast<fir::CharacterType>().getFKind();
159160
args.push_back(builder.createIntegerConstant(loc, inputTypes[2], kind));
160161
int rank = box.rank();
161162
args.push_back(builder.createIntegerConstant(loc, inputTypes[3], rank));
@@ -663,10 +664,17 @@ class AllocateStmtHelper {
663664
// unlimited polymorphic entity.
664665
if (typeSpec->AsIntrinsic() &&
665666
fir::isUnlimitedPolymorphicType(fir::getBase(box).getType())) {
666-
genInitIntrinsic(
667-
box, typeSpec->AsIntrinsic()->category(),
668-
Fortran::evaluate::ToInt64(typeSpec->AsIntrinsic()->kind()).value(),
669-
alloc.getSymbol().Rank());
667+
if (typeSpec->AsIntrinsic()->category() == TypeCategory::Character) {
668+
genRuntimeInitCharacter(
669+
builder, loc, box, lenParams[0],
670+
Fortran::evaluate::ToInt64(typeSpec->AsIntrinsic()->kind())
671+
.value());
672+
} else {
673+
genInitIntrinsic(
674+
box, typeSpec->AsIntrinsic()->category(),
675+
Fortran::evaluate::ToInt64(typeSpec->AsIntrinsic()->kind()).value(),
676+
alloc.getSymbol().Rank());
677+
}
670678
return;
671679
}
672680

flang/test/Lower/allocatable-polymorphic.f90

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,22 @@ subroutine test_allocatable_up_from_mold_rank(a)
536536
! CHECK: %[[BOX_NONE_10:.*]] = fir.convert %[[EMBOX_10]] : (!fir.box<i32>) -> !fir.box<none>
537537
! CHECK: %{{.*}} = fir.call @_FortranAAllocatableAllocateSource(%[[A_BOX_NONE]], %[[BOX_NONE_10]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}) {{.*}} : (!fir.ref<!fir.box<none>>, !fir.box<none>, i1, !fir.box<none>, !fir.ref<i8>, i32) -> i32
538538

539+
subroutine test_allocatable_up_character()
540+
class(*), allocatable :: a
541+
allocate(character*10::a)
542+
end subroutine
543+
544+
! CHECK-LABEL: func.func @_QMpolyPtest_allocatable_up_character() {
545+
! CHECK: %[[A:.*]] = fir.alloca !fir.class<!fir.heap<none>> {bindc_name = "a", uniq_name = "_QMpolyFtest_allocatable_up_characterEa"}
546+
! CHECK: %[[LEN:.*]] = arith.constant 10 : i64
547+
! CHECK: %[[A_NONE:.*]] = fir.convert %[[A]] : (!fir.ref<!fir.class<!fir.heap<none>>>) -> !fir.ref<!fir.box<none>>
548+
! CHECK: %[[KIND:.*]] = arith.constant 1 : i32
549+
! CHECK: %[[RANK:.*]] = arith.constant 0 : i32
550+
! CHECK: %[[CORANK:.*]] = arith.constant 0 : i32
551+
! CHECK: %{{.*}} = fir.call @_FortranAAllocatableInitCharacter(%[[A_NONE]], %[[LEN]], %[[KIND]], %[[RANK]], %[[CORANK]]) {{.*}} : (!fir.ref<!fir.box<none>>, i64, i32, i32, i32) -> none
552+
! CHECK: %[[A_NONE:.*]] = fir.convert %[[A:.*]] : (!fir.ref<!fir.class<!fir.heap<none>>>) -> !fir.ref<!fir.box<none>>
553+
! CHECK: %{{.*}} = fir.call @_FortranAAllocatableAllocate(%[[A_NONE]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}) {{.*}} : (!fir.ref<!fir.box<none>>, i1, !fir.box<none>, !fir.ref<i8>, i32) -> i32
554+
539555
end module
540556

541557

0 commit comments

Comments
 (0)