This is an archive of the discontinued LLVM Phabricator instance.

[flang][hlfir] Fixed character allocatable in structure constructor.
ClosedPublic

Authored by vzakhari on Jul 12 2023, 9:05 PM.

Details

Summary

The problem appeared as a segfault for case like this:

type t
  character(11), allocatable :: c
end type
character(12), alloctable :: x
type(t) y
y = t(x)

The frontend representes y = t(x) as y=t(c=%SET_LENGTH(x,11_8)).
When 'x' is unallocated the hlfir.set_length lowering results in
segfault. It could probably be handled in hlfir.set_length lowering
by using NULL base for the hlfir.declare depending on the allocation
status of 'x', but I am not sure if !hlfir.expr, in general, is supposed
to represent an expression created from unallocated allocatable.
I believe in Fortran that would mean referencing an unallocated
allocatable, which is not allowed.

I decided to special case SET_LENGTH in structure constructor,
so that we use its 'x' operand as the RHS for the assign operation
implying the isAllocatable check for cases when 'x' is allocatable.
This requires setting keep_lhs_length_if_realloc flag for the assign
operation. Note that when the component being intialized has
deferred length the frontend does not produce SET_LENGTH.

Diff Detail

Event Timeline

vzakhari created this revision.Jul 12 2023, 9:05 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 12 2023, 9:05 PM
vzakhari requested review of this revision.Jul 12 2023, 9:05 PM
vzakhari added inline comments.Jul 12 2023, 9:09 PM
flang/test/Lower/HLFIR/structure-constructor.f90
158

The side-effect of the change is that we do not create temporary for the result of SET_LENGTH, when the expression is array. This matters when the RHS is allocated, because we would create a copy for the whole operand of SET_LENGTH.

tblah accepted this revision.Jul 13 2023, 3:00 AM

LGTM. Nice work!

This revision is now accepted and ready to land.Jul 13 2023, 3:00 AM