Skip to content

5.9: [IRGen] Add metadata pack markers for more instructions. #67497

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

Conversation

nate-chandler
Copy link
Contributor

@nate-chandler nate-chandler commented Jul 24, 2023

Description: Cleanup pack metadata when it's allocated for a destroy, alloc_stack, or other instruction when it involves a pack type.

SIL has no notion of metadata or witness tables. SIL, however, is the right level at which to fixup stack nesting, ensuring that the various stack allocated things obey stack discipline. When certain instructions are IRGen'd, packs of metadata and witness tables may be allocated on the stack. Forcing these stack allocations to obey stack has two parts: (1) IRGen's SIL pass PackMetadataMarkerInserter and (2) IRGen itself. In (1), a list (which this patch extends) of instructions which may result in pack metadata being allocated on the stack is consulted; each such instruction results in new marker instructions being inserted: a single alloc_pack_metadata instruction is inserted before it, and some number of dealloc_pack_metadata instructions are inserted on its boundary. In (2), the on-stack metadata and witness tables emitted while IRGen'ing each such instruction are recorded. Then when each dealloc_pack_metadata instruction is visited, those metadata and witness tables are cleaned up in reverse order.

The purpose of the list of instructions used by (1) is to limit the number of such marker instructions that are created. Here, three new instructions are conditionally added to the list: destroy_addr, destroy_value and alloc_stack. Such instructions only get markers if the type that they are destroying involves a pack.

Finally, all (non-stack allocating, non-function exiting) other instructions are also given markers if any of their operands or results involve a pack type. This will help to avoid similar problems in the future.
Risk: Low. The change is just to expand the list of instructions that get these markers.
Scope: Narrow. This only affects variadic generics code.
Original PR: #67493 , #67567 , #67569 , #67778
Reviewed By: @aschwaighofer
Testing: Added @slavapestov's source-level test cases that previously hit an assertion failure.
Resolves: rdar://112792831

In preparation for adding addition unary instructions which
`mayRequirePackMetadata`, group the instructions which already may
produce pack metadata depending on their single operand's type together.
Destroys of values whose types feature a pack may require allocating the
pack on-stack.

Thanks to @slavapestov for the test case.

rdar://112792831
@nate-chandler nate-chandler requested a review from a team as a code owner July 24, 2023 22:48
When it's a tuple containing a pack.
Add the recursive property.
Eliminated HasConcretePack and added HasPack and HasPackArchetype.
Renamed the old `hasPack` to `hasAnyPack`; as before, it means that the
type has a parameter pack, a pack, or a pack archetype.
Instead of assuming that the list of instructions known to allocate pack
metadata is exhaustive and returning false from mayRequirePackMetadata
for all others, consider the types of the results and operands of other
instructions and look for packs.
@nate-chandler nate-chandler changed the title 5.9: [IRGen] Add metadata pack markers for destroys. 5.9: [IRGen] Add metadata pack markers for more instructions. Jul 28, 2023
Function exiting terminators don't allocate on-stack pack metadata
packs.  The packs would have been materialized when the value is
defined.

Fixes a SILVerifier failure resulting from a sequence like
```
alloc_pack_metadata
dealloc_pack_metadata
return
```
resulting from inserting the `alloc_pack_metadata` on behalf of the
return, inserting the `dealloc_pack_metadata` on the dominance frontier,
and fixing up stack nesting.
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

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 requested a review from tbkka August 11, 2023 14:11
@nate-chandler nate-chandler merged commit 2211b2c into swiftlang:release/5.9 Aug 14, 2023
@nate-chandler nate-chandler deleted the cherrypick/release/5.9/rdar112792831 branch August 14, 2023 18:35
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.

3 participants