Skip to content

Improve optimization of select between same values#8806

Merged
tlively merged 1 commit into
mainfrom
optimize-instructions-order-3396
Jun 6, 2026
Merged

Improve optimization of select between same values#8806
tlively merged 1 commit into
mainfrom
optimize-instructions-order-3396

Conversation

@tlively
Copy link
Copy Markdown
Member

@tlively tlively commented Jun 5, 2026

We previously optimized selects between the same value only when the ifTrue and ifFalse values had identical structure and no side effects and the condition could be dropped or reordered with the value.

Make this more robust by using the areConsecutiveInputs... utilities to check whether the arms have equal values and whether they are foldable. The old code avoided introducing blocks to keep code size down, but the blocks it would introduce are not named and will be elided in the output, so introducing blocks is not a problem. The old code also avoided introducing a scratch local, but if a scratch local is necessary, it is because the arms have some side effects that would prevent reordering. In that case it is more likely to be beneficial to execute only one arm, so go ahead and use scratch locals in the case where the second arm can be folded away.

We previously optimized selects between the same value only when the ifTrue and ifFalse values had identical structure and no side effects and the condition could be dropped or reordered with the value.

Make this more robust by using the `areConsecutiveInputs...` utilities to check whether the arms have equal values and whether they are foldable. The old code avoided introducing blocks to keep code size down, but the blocks it would introduce are not named and will be elided in the output, so introducing blocks is not a problem. The old code also avoided introducing a scratch local, but if a scratch local is necessary, it is because the arms have some side effects that would prevent reordering. In that case it is more likely to be beneficial to execute only one arm, so go ahead and use scratch locals in the case where the second arm can be folded away.
@tlively tlively requested a review from a team as a code owner June 5, 2026 20:53
@tlively tlively requested review from stevenfontanella and removed request for a team June 5, 2026 20:53
@tlively tlively merged commit c800b9d into main Jun 6, 2026
16 checks passed
@tlively tlively deleted the optimize-instructions-order-3396 branch June 6, 2026 00:15
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