Skip to content

SSA-CFG: Improved handling of identical values in permutation - #17217

Open
blishko wants to merge 3 commits into
developfrom
ssa-cfg-permute-optimal-swaps
Open

blishko wants to merge 3 commits into
developfrom
ssa-cfg-permute-optimal-swaps

Conversation

@blishko

@blishko blishko commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

When implementing the actual permutation using swaps, we can take better
advantage of identical values.
Positions with equal values are interchangeable: we can exchange
destinations of two positions holding equal values.
We can take advantage of this in two ways, implemented as preprocessing
of the permutation:

  • Minimize the support of the permutation by fixing positions that would
    end up with identical value. This decreases the number of elements
    that have to move.
  • Join cycles holding identical values to decrease the number of swaps.
    By swapping destination of two positions in different cycles holding
    identical values we effectively join two cycles into one. This
    decreases number of swaps by one for every join performed.

AI Disclosure

Suggested by Opus 5.5 in the formalized version of the algorithm.

@blishko
blishko force-pushed the ssa-cfg-permute-optimal-swaps branch from c7fd775 to d499cd4 Compare November 12, 2026 15:04
@blishko
blishko force-pushed the ssa-cfg-permute-optimal-swaps branch 2 times, most recently from 1ce1cf6 to 1f5d893 Compare November 13, 2026 14:09
@blishko
blishko marked this pull request as ready for review November 13, 2026 14:10
@blishko
blishko force-pushed the ssa-cfg-permute-optimal-swaps branch from 1f5d893 to 1b1387c Compare November 13, 2026 14:11
@blishko
blishko requested a review from clonker November 13, 2026 14:11
@blishko
blishko force-pushed the ssa-cfg-permute-optimal-swaps branch from 1b1387c to 940932f Compare November 13, 2026 14:32
clonker
clonker previously approved these changes Nov 13, 2026
These tests only need to permute the source stack.
They demonstrate that we are currently not optimal in these scenarios.
When implementing the actual permutation using swaps, we can take better
advantage of identical values.
Positions with equal values are interchangeable: we can exchange
destinations of two positions holding equal values.
We can take advantage of this in two ways, implemented as preprocessing
of the permutation:
- Minimize the support of the permutation by fixing positions that would
  end up with identical value. This decreases the number of elements
  that have to move.
- Join cycles holding identical values to decrease the number of swaps.
  By swapping destination of two positions in different cycles holding
  identical values we effectively join two cycles into one. This
  decreases number of swaps by one for every join performed.

Suggested by Opus 5.5 in the formalized version of the algorithm.
When preprocessing permutation to minimize cycles without top we are
searching for same-value slots in different cycles.
Previous implementation was worst-case qubic.
For the new version we observe that once we find two non-fixed
same-value indices `a` and `b`, we know that `a` and `b` will be in the
same cycle: Either they already were, or we swap them and merge their
cycles. We don't need to continue searching indices after `b` for the
same `a`, we can directly continue with the next `a`. If there are
more indices with the same value, they will be examined when `a` because
the previous `b`.
This means that for every `a` `sameCycle` will be called at most once,
making the whole check quadratic in the worst case.
(Though average case should be much better.)
@blishko
blishko force-pushed the ssa-cfg-permute-optimal-swaps branch from 885c794 to 961b00e Compare November 14, 2026 21:22
@blishko
blishko requested a review from clonker November 14, 2026 21:25

@clonker clonker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

cool! thanks :)

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.

2 participants