Skip to content

Commit cc80696

Browse files
committed
Change result char type length to unknown
The runtime Trim function allocates the result, that may have a different length than its input character string.
1 parent e9756e6 commit cc80696

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -860,15 +860,11 @@ void hlfir::CmpCharOp::getEffects(
860860

861861
void hlfir::CharTrimOp::build(mlir::OpBuilder &builder,
862862
mlir::OperationState &result, mlir::Value chr) {
863-
fir::CharacterType::LenType len;
864-
if (auto cstLen = getCharacterLengthIfStatic(chr.getType()))
865-
len = *cstLen;
866-
else
867-
len = fir::CharacterType::unknownLen();
868863
unsigned kind = getCharacterKind(chr.getType());
869864
auto resultType = hlfir::ExprType::get(
870865
builder.getContext(), hlfir::ExprType::Shape{},
871-
fir::CharacterType::get(builder.getContext(), kind, len),
866+
fir::CharacterType::get(builder.getContext(), kind,
867+
fir::CharacterType::unknownLen()),
872868
/*polymorphic=*/false);
873869
build(builder, result, resultType, chr);
874870
}

flang/test/Lower/HLFIR/trim.f90

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@ subroutine trim_test(c)
2020
tc = trim(c)
2121
end subroutine
2222

23+
! Test trim with fixed length character.
24+
! The length of the returned character type must be unknown.
25+
! CHECK-LABEL: func.func @_QPtrim_test2(
26+
! CHECK: hlfir.char_trim %{{.*}}#0 : (!fir.ref<!fir.char<1,8>>) -> !hlfir.expr<!fir.char<1,?>>
27+
subroutine trim_test2(c)
28+
character(8) :: c
29+
character(8) :: tc
30+
31+
tc = trim(c)
32+
end subroutine

flang/test/Lower/volatile-string.f90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ subroutine assign_different_length(string)
6868
! CHECK: %[[VAL_37:.*]] = hlfir.as_expr %[[VAL_36]]#0 move %[[VAL_0]] : (!fir.heap<!fir.char<1,3>>, i1) -> !hlfir.expr<!fir.char<1,3>>
6969
! CHECK: hlfir.assign %[[VAL_37]] to %[[VAL_14]]#0 : !hlfir.expr<!fir.char<1,3>>, !fir.ref<!fir.char<1,3>, volatile>
7070
! CHECK: hlfir.destroy %[[VAL_37]] : !hlfir.expr<!fir.char<1,3>>
71-
! CHECK: %[[VAL_38:.*]] = hlfir.char_trim %[[VAL_14]]#0 : (!fir.ref<!fir.char<1,3>, volatile>) -> !hlfir.expr<!fir.char<1,3>>
72-
! CHECK: hlfir.assign %[[VAL_38]] to %[[VAL_11]]#0 : !hlfir.expr<!fir.char<1,3>>, !fir.ref<!fir.char<1,3>>
73-
! CHECK: hlfir.destroy %[[VAL_38]] : !hlfir.expr<!fir.char<1,3>>
71+
! CHECK: %[[VAL_38:.*]] = hlfir.char_trim %[[VAL_14]]#0 : (!fir.ref<!fir.char<1,3>, volatile>) -> !hlfir.expr<!fir.char<1,?>>
72+
! CHECK: hlfir.assign %[[VAL_38]] to %[[VAL_11]]#0 : !hlfir.expr<!fir.char<1,?>>, !fir.ref<!fir.char<1,3>>
73+
! CHECK: hlfir.destroy %[[VAL_38]] : !hlfir.expr<!fir.char<1,?>>
7474
! CHECK: hlfir.assign %[[VAL_14]]#0 to %[[VAL_11]]#0 : !fir.ref<!fir.char<1,3>, volatile>, !fir.ref<!fir.char<1,3>>
7575
! CHECK: return
7676
! CHECK: }

0 commit comments

Comments
 (0)