This is an archive of the discontinued LLVM Phabricator instance.

[flang] Create a temporary of the correct size when lowering SetLength
ClosedPublic

Authored by clementval on Aug 23 2022, 4:54 AM.

Details

Summary

This patch creates a temporary of the appropriate length while lowering SetLength.

The corresponding character can be truncated or padded if necessary.

This fix issue with array constructor in argument and also with statement function.

  character(7) :: str = "1234567"
  call s(str(1:1))
contains
 subroutine s(a)
  character(*) :: a
  call s2([Character(3)::a])
 end subroutine
 subroutine s2(c)
  character(3) :: c(1)
  print "(4a)", c(1), "end"
 end subroutine
end

The example prior the patch prints 123end instead of 1. end

Diff Detail

Event Timeline

clementval created this revision.Aug 23 2022, 4:54 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptAug 23 2022, 4:54 AM
Herald added a subscriber: mehdi_amini. · View Herald Transcript
clementval requested review of this revision.Aug 23 2022, 4:54 AM
jeanPerier added inline comments.Aug 23 2022, 5:51 AM
flang/lib/Lower/ConvertExpr.cpp
2370

I do not think the temp should happen on the arguments, the actual arguments must be longer than the dummy so that the program is compliant (F2018 standard 15.5.2.11 Sequence association). So replacing the character length for the argument should be enough: only "virtual" truncation can happen.

On the result side however, I think you will need to create a temp with the result symbol length and make an assignment (charHelper.createAssign) so that the padding can occure if needed (you can skip it if it can be proved at compile time that the result length is smaller or equal to the expression length, in which case replaceScalarCharacterLength should be sufficient to insert "virtual" truncation).

flang/test/Lower/statement-function.f90
172

The fact that this is printing a length 4 character seems wrong to me, stmt_fct has length 10. I think what should happen here is that the statement function result expression should be padded with blank in a temp.

This may not be visible when running this example, but if you try:

  character(4) arg
  character(10) stmt_fct
  stmt_fct(arg) = arg
  write(*,"(17A)") stmt_fct('longer_arg'), "THE_END"
end

I suspect this might print "longTHE_END" instead of "long THE_END".

PeteSteinfeld accepted this revision.Aug 23 2022, 6:12 AM

All builds and tests correctly and looks good.

This revision is now accepted and ready to land.Aug 23 2022, 6:12 AM

Create a temporary for the result with the stmt function result length and
assign to it the actual result.

clementval marked an inline comment as done.Aug 23 2022, 9:42 PM
clementval added inline comments.
flang/lib/Lower/ConvertExpr.cpp
2370

Truncated temporary for argument seems needed as well to get the expected result.

flang/test/Lower/statement-function.f90
172

Printing the correct length now.

clementval marked an inline comment as done.

Create a temproary on while lowering SetLength instead
of when dealing with statement function.

clementval retitled this revision from [flang] Truncate argument and result of statement function to [flang] Create a temporary of the correct size when lowering SetLength.Aug 24 2022, 5:29 AM
clementval edited the summary of this revision. (Show Details)
clementval marked an inline comment as done.
clementval added inline comments.
flang/lib/Lower/ConvertExpr.cpp
2370

Not needed anymore with the fix in SetLength.

jeanPerier accepted this revision.Aug 24 2022, 7:24 AM

Thanks, LGTM !