Skip to content

Replace opened Pack Element types with the concrete type of the element when it is statically known#88591

Draft
aidan-hall wants to merge 6 commits intoswiftlang:mainfrom
aidan-hall:pack-opt-eliminate-element
Draft

Replace opened Pack Element types with the concrete type of the element when it is statically known#88591
aidan-hall wants to merge 6 commits intoswiftlang:mainfrom
aidan-hall:pack-opt-eliminate-element

Conversation

@aidan-hall
Copy link
Copy Markdown
Contributor

@aidan-hall aidan-hall commented Apr 21, 2026

Explanation

When using a @pack_element type to refer to an element of a concrete pack type at a compile-time-known index, we can replace the @pack_element with the concrete type of the element at that index.

  • Doing this for witness_method allows us to replace the witness table lookup and call with a direct call to the witness function for the concrete type.
  • Doing this for apply makes it possible to specialize calls to generic functions inside loops over pack elements.

This is intended to be a smaller version of the changes proposed in #85333, and has limitations on what it can handle (e.g. it can only get the concrete pack type for open_pack_element instructions that open a single pack type). Nonetheless, it enables more effective optimisations in many cases.

Scope

TODO

Issues

rdar://163046681 (at least partially)

Risk

TODO

Testing

TODO

  • Unit tests
  • Integration tests
  • Benchmarks

@aidan-hall aidan-hall force-pushed the pack-opt-eliminate-element branch from 1971cbd to 5c396a0 Compare April 21, 2026 18:36
Note that we have made GenericEnvironment::getOpenedPackParams public.
In some scenarios, it is possible to statically determine which element of a
pack a pack indexing instruction will access.
LocalArchetype is the shared base class of ArchetypeType (which SimplifyApply
previously worked with) and ElementArchetypeType (@pack_element() types).

This allows us to specialize calls to generic functions inside "repeat ... each"
expressions and loops within specialized variadic generic functions.
@aidan-hall aidan-hall force-pushed the pack-opt-eliminate-element branch from 5c396a0 to 37cc040 Compare April 21, 2026 19:52
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.

1 participant