Skip to content

Commit

Permalink
[flang] Unlimited polymoprhic allocated as character
Browse files Browse the repository at this point in the history
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
  • Loading branch information
clementval committed Feb 8, 2023
1 parent 1c10d5b commit 4143d3e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
20 changes: 14 additions & 6 deletions flang/lib/Lower/Allocatable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static void genRuntimeSetBounds(fir::FirOpBuilder &builder, mlir::Location loc,
static void genRuntimeInitCharacter(fir::FirOpBuilder &builder,
mlir::Location loc,
const fir::MutableBoxValue &box,
mlir::Value len) {
mlir::Value len, int64_t kind = 0) {
mlir::func::FuncOp callee =
box.isPointer()
? fir::runtime::getRuntimeFunc<mkRTKey(PointerNullifyCharacter)>(
Expand All @@ -155,7 +155,8 @@ static void genRuntimeInitCharacter(fir::FirOpBuilder &builder,
llvm::SmallVector<mlir::Value> args;
args.push_back(builder.createConvert(loc, inputTypes[0], box.getAddr()));
args.push_back(builder.createConvert(loc, inputTypes[1], len));
int kind = box.getEleTy().cast<fir::CharacterType>().getFKind();
if (kind == 0)
kind = box.getEleTy().cast<fir::CharacterType>().getFKind();
args.push_back(builder.createIntegerConstant(loc, inputTypes[2], kind));
int rank = box.rank();
args.push_back(builder.createIntegerConstant(loc, inputTypes[3], rank));
Expand Down Expand Up @@ -663,10 +664,17 @@ class AllocateStmtHelper {
// unlimited polymorphic entity.
if (typeSpec->AsIntrinsic() &&
fir::isUnlimitedPolymorphicType(fir::getBase(box).getType())) {
genInitIntrinsic(
box, typeSpec->AsIntrinsic()->category(),
Fortran::evaluate::ToInt64(typeSpec->AsIntrinsic()->kind()).value(),
alloc.getSymbol().Rank());
if (typeSpec->AsIntrinsic()->category() == TypeCategory::Character) {
genRuntimeInitCharacter(
builder, loc, box, lenParams[0],
Fortran::evaluate::ToInt64(typeSpec->AsIntrinsic()->kind())
.value());
} else {
genInitIntrinsic(
box, typeSpec->AsIntrinsic()->category(),
Fortran::evaluate::ToInt64(typeSpec->AsIntrinsic()->kind()).value(),
alloc.getSymbol().Rank());
}
return;
}

Expand Down
16 changes: 16 additions & 0 deletions flang/test/Lower/allocatable-polymorphic.f90
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,22 @@ subroutine test_allocatable_up_from_mold_rank(a)
! CHECK: %[[BOX_NONE_10:.*]] = fir.convert %[[EMBOX_10]] : (!fir.box<i32>) -> !fir.box<none>
! 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

subroutine test_allocatable_up_character()
class(*), allocatable :: a
allocate(character*10::a)
end subroutine

! CHECK-LABEL: func.func @_QMpolyPtest_allocatable_up_character() {
! CHECK: %[[A:.*]] = fir.alloca !fir.class<!fir.heap<none>> {bindc_name = "a", uniq_name = "_QMpolyFtest_allocatable_up_characterEa"}
! CHECK: %[[LEN:.*]] = arith.constant 10 : i64
! CHECK: %[[A_NONE:.*]] = fir.convert %[[A]] : (!fir.ref<!fir.class<!fir.heap<none>>>) -> !fir.ref<!fir.box<none>>
! CHECK: %[[KIND:.*]] = arith.constant 1 : i32
! CHECK: %[[RANK:.*]] = arith.constant 0 : i32
! CHECK: %[[CORANK:.*]] = arith.constant 0 : i32
! CHECK: %{{.*}} = fir.call @_FortranAAllocatableInitCharacter(%[[A_NONE]], %[[LEN]], %[[KIND]], %[[RANK]], %[[CORANK]]) {{.*}} : (!fir.ref<!fir.box<none>>, i64, i32, i32, i32) -> none
! CHECK: %[[A_NONE:.*]] = fir.convert %[[A:.*]] : (!fir.ref<!fir.class<!fir.heap<none>>>) -> !fir.ref<!fir.box<none>>
! CHECK: %{{.*}} = fir.call @_FortranAAllocatableAllocate(%[[A_NONE]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}) {{.*}} : (!fir.ref<!fir.box<none>>, i1, !fir.box<none>, !fir.ref<i8>, i32) -> i32

end module


Expand Down

0 comments on commit 4143d3e

Please sign in to comment.