Skip to content

Conversation

@cdleary
Copy link
Collaborator

@cdleary cdleary commented Jan 15, 2026

Add a SelectSimplificationPass rewrite to push CompareOp through sel / priority_sel / one_hot_sel when comparing against a literal and the select-like node has ≤1 non-literal arm (including default) plus a single-user guard.

e.g.

eq(priority_sel(s, cases=[a, b, c], default=d), t)
  -> priority_sel(s,
                  cases=[eq(a, t), eq(b, t), eq(c, t)],
                  default=eq(d, t))

The fact we only do it when (at most) one case is non-constant means it should be more area-preserving to push this through. (It does suggest maybe we want some knob like --speculation_level=N similar to how we have --opt_level=N in the future though.)

For one_hot_sel, apply only when the selector is provably exactly-one-hot (IsKnownOneHotSelector), including common “one-hot by construction” patterns.

Extend GenericSelect with a small helper to rebuild the same select-like op with new arms (MakeSelectLikeWithNewArms).

Add query_engine_utils (+ tests) and new select_simplification_pass tests covering prio/sel/onehot, non-commutative operand ordering, and all-literal arms.

@cdleary cdleary requested a review from meheff January 15, 2026 22:05
Copy link
Collaborator

@meheff meheff left a comment

Choose a reason for hiding this comment

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

Nice!

@cdleary cdleary force-pushed the cdleary/2026-01-15-push-cmp-across-sel-when-cheap branch from fd68e64 to 289925f Compare January 16, 2026 00:37
@cdleary cdleary marked this pull request as ready for review January 16, 2026 01:00
@cdleary cdleary changed the title [opt] Push comparisons across selects when it is cheap to do so. [opt] Hoist comparisons across selects when it is cheap to do so. Jan 16, 2026
@cdleary cdleary changed the title [opt] Hoist comparisons across selects when it is cheap to do so. [opt] Hoist comparisons across selects when it is cheap to do so Jan 16, 2026
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