Skip to content

[IRGen] Dealloc on-stack metadata/wtable packs on the dominance frontier. #66201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jun 5, 2023

Conversation

nate-chandler
Copy link
Contributor

@nate-chandler nate-chandler commented May 29, 2023

Deallocate dynamic allocas done for metadata/wtable packs. These stackrestore calls are inserted on the dominance frontier and then stack nesting is fixed up. That was achieved as follows:

Added a new IRGen pass PackMetadataMarkerInserter; it

  • determines if there are any instructions which might allocate on-stack pack metadata
  • if there aren't, no changes are made
  • if there are, alloc_pack_metadata just before instructions that could allocate pack metadata on the stack and dealloc_pack_metadata on the dominance frontier of those instructions
  • fixup stack nesting

During IRGen, the allocations done for metadata/wtable packs are recorded and IRGenSILFunction associates them with the instruction that lowered. It must be the instruction after some alloc_pack_metadata instruction. Then, when visiting the dealloc_pack_metadata instructions corresponding to that alloc_pack_metadata, deallocate those packs.

See individual commits for more details.

rdar://110268881

@nate-chandler nate-chandler force-pushed the dealloc-on-stack-packs branch 2 times, most recently from c37d161 to abc6973 Compare May 29, 2023 21:03
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler
Copy link
Contributor Author

@swift-ci please test source compatibility

@nate-chandler
Copy link
Contributor Author

The source compat failures match the baseline failures.

@xedin xedin removed their request for review May 30, 2023 16:25
Copy link
Contributor

@aschwaighofer aschwaighofer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nate-chandler nate-chandler force-pushed the dealloc-on-stack-packs branch 2 times, most recently from 1f206f7 to c3cb49d Compare June 2, 2023 18:11
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

If the -enable-pack-metadata-stack-promotion[=true] flag is passed, skip
eager heapification.
Provide template instantiations of llvm::DenseMapInfo at
swift::irgen::Address and swift::irgen::StackAddress.
Just pull the notion that the alloc corresponds to operand 0 into a
helper method.
Stop requiring that allocation instructions produce single values.
Both stackAlloc and unprotectedStackAlloc are paired with stackDealloc
builtins.
The algorithm requires no critical edges, but that doesn't mean
require ownership.  Remove the assert to allow the utility to be called
from code where the caller has manually split edges.

In the fullness of time, there should no passes should introduce
critical edges.
The new alloc_pack_metadata and dealloc_pack_metadata are inserted as
part of IRGen lowering.  The former indicates that the next instruction
might result in on-stack pack metadata being emitted.  The latter
indicates that this is the position at which metadata emitted on behalf
of its operand should be cleaned up.
When lowering a SIL function, if it bears the no_onstack_pack_metadata
annotation, disable on-stack pack metadata.
Deallocate dynamic allocas done for metadata/wtable packs.  These
stackrestore calls are inserted on the dominance frontier and then stack
nesting is fixed up.  That was achieved as follows:

Added a new IRGen pass PackMetadataMarkerInserter; it
- determines if there are any instructions which might allocate on-stack
  pack metadata
- if there aren't, no changes are made
- if there are, alloc_pack_metadata just before instructions that could
  allocate pack metadata on the stack and dealloc_pack_metadata on the
  dominance frontier of those instructions
- fixup stack nesting

During IRGen, the allocations done for metadata/wtable packs are
recorded and IRGenSILFunction associates them with the instruction that
lowered.  It must be the instruction after some alloc_pack_metadata
instruction.  Then, when visiting the dealloc_pack_metadata instructions
corresponding to that alloc_pack_metadata, deallocate those packs.
In debug builds, before SIL function emission, pass over the function to
collect cleanups.  After emitting an instruction for which an on-stack
metadata/wtable pack was emitted, assert that there were cleanups for
it in the function.
Rather than emitting markers for every single instruction of the
relevant sorts, check whether the instructions' types involve packs.
Only record them as potential on-stack pack metadata emitters if they
do.
Set the flag and updated the tests.
@nate-chandler nate-chandler force-pushed the dealloc-on-stack-packs branch from c3cb49d to c5699c9 Compare June 5, 2023 15:11
@nate-chandler
Copy link
Contributor Author

@swift-ci please test and merge

@swift-ci swift-ci merged commit 4ef135c into swiftlang:main Jun 5, 2023
@nate-chandler nate-chandler deleted the dealloc-on-stack-packs branch June 5, 2023 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants