This is an archive of the discontinued LLVM Phabricator instance.

[flang] fix fir.array_coor of fir.box with component references
ClosedPublic

Authored by jeanPerier on Apr 13 2023, 6:45 AM.

Details

Summary

When dealing with "derived_array(j)%component" where derived_array
is not a contiguous array, but for which we know the extent, lowering
generates a fir.array_coor op on a !fir.box<!fir.array<cst x T>> with
a fir.slice containing "j" in the component path.

Codegen first computes "derived_array(j)" address using the byte
strides inside the descriptor, and then computes the offset of "j"
from that address with a second GEP.
The type of the address in that second GEP matters since "j" is passed
in the GEP via an index indicating its component position in the type.

The code was using the LLVM type of "derived_array" instead of
"derived_array(j)".
In general, with fir.box, the extent ("cst" above) is unknown and those
types match. But if the extent of "derived_array" is a compile time
constant, its LLVM type will be [cst x T] instead of T*, and the produced
GEP will compute the address of the nth T instead of the nth component
inside T leading to undefined behaviors.

Fix this by computing the element type for the second GEP.

Diff Detail

Event Timeline

jeanPerier created this revision.Apr 13 2023, 6:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2023, 6:45 AM
jeanPerier requested review of this revision.Apr 13 2023, 6:45 AM
PeteSteinfeld accepted this revision.Apr 13 2023, 8:08 AM

All builds and tests correctly and looks good to me. But Valentin should take a look before you merge.

This revision is now accepted and ready to land.Apr 13 2023, 8:08 AM