This is an archive of the discontinued LLVM Phabricator instance.

[Flang] Fix code for ALLOCATE statement with MOLD argument for scalars
ClosedPublic

Authored by PeteSteinfeld on Feb 24 2023, 2:48 PM.

Details

Summary

When we allocate a variable using a MOLD argument, the function that
applies the type of the MOLD argument first checks to see if the
variable is already allocated by looking at its descriptor. But in the
case of allocating a scalar, the descriptor has not yet been created and
the associated memory was uninitialized. This update fixes that.

Note that there's already a test for allocations, but it uses the argument
"use-alloc-runtime", which causes different code to be generated. So I
added a new test.

Diff Detail

Event Timeline

PeteSteinfeld created this revision.Feb 24 2023, 2:48 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptFeb 24 2023, 2:48 PM
Herald added a subscriber: sunshaoce. · View Herald Transcript
PeteSteinfeld requested review of this revision.Feb 24 2023, 2:48 PM

I think removing this check is not correct from a semantic point of view because we are required to gracefully catch double allocation errors when STAT/ERRMSG is provided.

I think the bug may be caused by the fact that the descriptor is obtained using box.getAddr() at [1] before emitting the runtime call, and this is not correct with the current lowering: fir::factory::getMutableIRBox(builder, loc, box); should be used instead to ensure the descriptor of local allocatables is synced with the local variables tracking the allocatable in a "descriptor-less" fashion.

[1]: https://github.com/llvm/llvm-project/blob/4143d3ebd2d8fbefc8d7ba2cded304a6049aed7f/flang/lib/Lower/Allocatable.cpp#L223

I think removing this check is not correct from a semantic point of view because we are required to gracefully catch double allocation errors when STAT/ERRMSG is provided.

I think the bug may be caused by the fact that the descriptor is obtained using box.getAddr() at [1] before emitting the runtime call, and this is not correct with the current lowering: fir::factory::getMutableIRBox(builder, loc, box); should be used instead to ensure the descriptor of local allocatables is synced with the local variables tracking the allocatable in a "descriptor-less" fashion.

[1]: https://github.com/llvm/llvm-project/blob/4143d3ebd2d8fbefc8d7ba2cded304a6049aed7f/flang/lib/Lower/Allocatable.cpp#L223

I agree with Jean here. We likely have the same problem with source allocation as well.

Responding to Valentin's and Jean's comments to actually fix the code. Also,
I added a test.

PeteSteinfeld retitled this revision from [Flang] Remove partial check for duplicate allocation to [Flang] Fix code for ALLOCATE statement with MOLD argument for scalars.Feb 27 2023, 4:18 PM
PeteSteinfeld edited the summary of this revision. (Show Details)
PeteSteinfeld edited the summary of this revision. (Show Details)
jeanPerier accepted this revision.Feb 28 2023, 12:11 AM

Thanks Pete, LGTM.

This revision is now accepted and ready to land.Feb 28 2023, 12:11 AM
This revision was automatically updated to reflect the committed changes.