-
Notifications
You must be signed in to change notification settings - Fork 220
[opt] Optimize commutative op working on (explicitly) commuted operands. #3640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[opt] Optimize commutative op working on (explicitly) commuted operands. #3640
Conversation
…itly) commuted operands. [opt] Optimize commutative op operating on explicitly commuted operands.
|
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". |
| // For `sel(p, cases=[on_false, on_true])`, the selector is `p`. | ||
| // For a binary `priority_sel(p, cases=[on_true], default=on_false)`, the | ||
| // selector is `p` (which is required to be a single bit). | ||
| struct BinarySelectArms { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rename to BinarySelect since this includes both the arms and the selector?
| XLS_RET_CHECK_EQ(sel->selector()->BitCountOrDie(), 1); | ||
| return BinarySelectArms{.selector = sel->selector(), | ||
| .on_false = sel->get_case(0), | ||
| .on_true = sel->get_case(1)}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible for the select to have a single case and the default value to cover the case for selector == 0b1?
| BValue selector = fb.Param("p", p->GetBitsType(1)); | ||
| BValue a = fb.Param("a", p->GetBitsType(32)); | ||
| BValue b = fb.Param("b", p->GetBitsType(32)); | ||
| BValue sel = fb.Select(selector, {a, b}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused variable sel; same for the below 2 unit tests
| EXPECT_THAT(f->return_value(), m::Eq(m::Param("a"), m::Param("b"))); | ||
| } | ||
|
|
||
| TEST_F(BasicSimplificationPassTest, AddWithZero) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a unit test on a function that is non-commutative?
op(sel(p, cases=[a, b]), sel(p, cases=[b, a]))can be optimized toop(a, b)whenopis commutative.X(sel(p,[a,b]), sel(p,[b,a])) -> X(a,b)(applies to any 2-operand commutative, non-side-effecting op).select_simplification_passhas several 2-way-select peepholes (case swapping / collapsing nested selects / detecting binary select patterns), but not this "commutative op of swapped cases collapses" rule.