Skip to content

Commit 8b13775

Browse files
authored
[flang] Improve length information in character transformational (#65771)
Intrinsic resolution currently does not resolve constant length information for character transformational (with "sameChar") where the argument has constant length but is not a variable or a constant expression. It is not required to fold those expressions (only inquiry on constant expression or variable with constant length is required to be a constant expression). But constant length information for character is valuable for lowering, so I think this is a nice and easy to have.
1 parent 6942c64 commit 8b13775

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,15 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
20932093
CHECK(sameArg);
20942094
if (std::optional<DynamicType> aType{sameArg->GetType()}) {
20952095
if (result.categorySet.test(aType->category())) {
2096-
resultType = *aType;
2096+
if (const auto *sameChar{UnwrapExpr<Expr<SomeCharacter>>(*sameArg)}) {
2097+
if (auto len{ToInt64(Fold(context, sameChar->LEN()))}) {
2098+
resultType = DynamicType{aType->kind(), *len};
2099+
} else {
2100+
resultType = *aType;
2101+
}
2102+
} else {
2103+
resultType = *aType;
2104+
}
20972105
} else {
20982106
resultType = DynamicType{*category, aType->kind()};
20992107
}

flang/test/Evaluate/fold-substr.f90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ module m
2020
logical, parameter :: test_07d = ca(1)(5:) == ""
2121
logical, parameter :: test_07e = ca(1)(:) == "abcd"
2222
end module
23+
24+
subroutine foo(x)
25+
character(4) :: x(:, :)
26+
logical, parameter :: test_01 = len(transpose(x(:, :)(:))) == 4
27+
logical, parameter :: test_02 = len(transpose(x(:, :)(1:2))) == 2
28+
logical, parameter :: test_03 = len(maxval(x(:, :)(:))) == 4
29+
logical, parameter :: test_04 = len(maxval(x(:, :)(1:2))) == 2
30+
end subroutine

0 commit comments

Comments
 (0)