Skip to content

Conversation

@cdleary
Copy link
Collaborator

@cdleary cdleary commented Jan 2, 2026

op(sel(p, cases=[a, b]), sel(p, cases=[b, a])) can be optimized to op(a, b) when op is commutative.

  • Add MatchBinarySelectLike() (and BinarySelectArms) in node_util to normalize binary sel and binary priority_sel into {selector, on_false, on_true}.
  • Use that helper in BasicSimplificationPass to fold commutative ops over swapped binary selects:
    X(sel(p,[a,b]), sel(p,[b,a])) -> X(a,b) (applies to any 2-operand commutative, non-side-effecting op).

select_simplification_pass has several 2-way-select peepholes (case swapping / collapsing nested selects / detecting binary select patterns), but not this "commutative op of swapped cases collapses" rule.

@cdleary cdleary changed the title [opt] Optimize commutative op operating on explicitly commuted operands. [opt] Optimize commutative op working on (explicitly) commuted operands. Jan 2, 2026
@cdleary cdleary marked this pull request as ready for review January 2, 2026 04:18
cdleary added a commit to xlsynth/xlsynth that referenced this pull request Jan 2, 2026
…itly) commuted operands.

[opt] Optimize commutative op operating on explicitly commuted operands.
@ericastor
Copy link
Contributor

This seems good to me... but continues to suggest that we may need better pass organization, since I'm not sure this should qualify as "basic".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants