-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -417,6 +417,23 @@ bool IsBinarySelect(Node* node); | |
| // default value) | ||
| bool IsBinaryPrioritySelect(Node* node); | ||
|
|
||
| // A uniform view of a "binary select-like" node. | ||
| // | ||
| // 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| Node* selector; | ||
| Node* on_false; | ||
| Node* on_true; | ||
| }; | ||
|
|
||
| // Returns a uniform view of a binary `sel` or a binary `priority_sel`. | ||
| // | ||
| // Returns std::nullopt if `node` is not one of those binary forms. | ||
| absl::StatusOr<std::optional<BinarySelectArms>> MatchBinarySelectLike( | ||
| Node* node); | ||
|
|
||
| // Returns the op which is the inverse of the given comparison. | ||
| // | ||
| // That is (not (op L R)) == ((InvertComparisonOp op) L R). | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -177,6 +177,54 @@ TEST_F(NodeUtilTest, GatherAllTheBits) { | |
| EXPECT_THAT(f->return_value(), m::Param("x")); | ||
| } | ||
|
|
||
| TEST_F(NodeUtilTest, MatchBinarySelectLikeSelect) { | ||
| auto p = CreatePackage(); | ||
| FunctionBuilder fb(TestName(), p.get()); | ||
| 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}); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused variable |
||
| XLS_ASSERT_OK_AND_ASSIGN(Function * f, fb.Build()); | ||
|
|
||
| XLS_ASSERT_OK_AND_ASSIGN(std::optional<BinarySelectArms> arms, | ||
| MatchBinarySelectLike(f->return_value())); | ||
| ASSERT_TRUE(arms.has_value()); | ||
| EXPECT_EQ(arms->selector, f->param(0)); | ||
| EXPECT_EQ(arms->on_false, f->param(1)); | ||
| EXPECT_EQ(arms->on_true, f->param(2)); | ||
| } | ||
|
|
||
| TEST_F(NodeUtilTest, MatchBinarySelectLikePrioritySelect) { | ||
| auto p = CreatePackage(); | ||
| FunctionBuilder fb(TestName(), p.get()); | ||
| 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.PrioritySelect(selector, {a}, b); | ||
| XLS_ASSERT_OK_AND_ASSIGN(Function * f, fb.Build()); | ||
|
|
||
| XLS_ASSERT_OK_AND_ASSIGN(std::optional<BinarySelectArms> arms, | ||
| MatchBinarySelectLike(f->return_value())); | ||
| ASSERT_TRUE(arms.has_value()); | ||
| EXPECT_EQ(arms->selector, f->param(0)); | ||
| EXPECT_EQ(arms->on_false, f->param(2)); | ||
| EXPECT_EQ(arms->on_true, f->param(1)); | ||
| } | ||
|
|
||
| TEST_F(NodeUtilTest, MatchBinarySelectLikeNonMatch) { | ||
| auto p = CreatePackage(); | ||
| FunctionBuilder fb(TestName(), p.get()); | ||
| BValue selector = fb.Param("p", p->GetBitsType(2)); | ||
| BValue a = fb.Param("a", p->GetBitsType(32)); | ||
| BValue b = fb.Param("b", p->GetBitsType(32)); | ||
| BValue sel = fb.Select(selector, {a, b}, /*default_value=*/a); | ||
| XLS_ASSERT_OK_AND_ASSIGN(Function * f, fb.Build()); | ||
|
|
||
| XLS_ASSERT_OK_AND_ASSIGN(std::optional<BinarySelectArms> arms, | ||
| MatchBinarySelectLike(f->return_value())); | ||
| EXPECT_FALSE(arms.has_value()); | ||
| } | ||
|
|
||
| TEST_F(NodeUtilTest, GatherTreeBits) { | ||
| auto p = CreatePackage(); | ||
| FunctionBuilder fb(TestName(), p.get()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -501,6 +501,54 @@ TEST_F(BasicSimplificationPassTest, XorDuplicateOperandsCollapseToXor) { | |
| EXPECT_THAT(f->return_value(), m::Xor(m::Param("x"), m::Param("y"))); | ||
| } | ||
|
|
||
| TEST_F(BasicSimplificationPassTest, AndOfSwappedTwoWaySelects) { | ||
| auto p = CreatePackage(); | ||
| FunctionBuilder fb(TestName(), p.get()); | ||
| BValue p_sel = fb.Param("p", p->GetBitsType(1)); | ||
| BValue a = fb.Param("a", p->GetBitsType(32)); | ||
| BValue b = fb.Param("b", p->GetBitsType(32)); | ||
| BValue sel_ab = fb.Select(p_sel, {a, b}); | ||
| BValue sel_ba = fb.Select(p_sel, {b, a}); | ||
| fb.And(sel_ab, sel_ba); | ||
| XLS_ASSERT_OK_AND_ASSIGN(Function * f, fb.Build()); | ||
|
|
||
| ScopedVerifyEquivalence sve(f); | ||
| ASSERT_THAT(Run(p.get()), IsOkAndHolds(true)); | ||
| EXPECT_THAT(f->return_value(), m::And(m::Param("a"), m::Param("b"))); | ||
| } | ||
|
|
||
| TEST_F(BasicSimplificationPassTest, XorOfSwappedTwoWaySelects) { | ||
| auto p = CreatePackage(); | ||
| FunctionBuilder fb(TestName(), p.get()); | ||
| BValue p_sel = fb.Param("p", p->GetBitsType(1)); | ||
| BValue a = fb.Param("a", p->GetBitsType(32)); | ||
| BValue b = fb.Param("b", p->GetBitsType(32)); | ||
| BValue sel_ab = fb.Select(p_sel, {a, b}); | ||
| BValue sel_ba = fb.Select(p_sel, {b, a}); | ||
| fb.Xor({sel_ab, sel_ba}); | ||
| XLS_ASSERT_OK_AND_ASSIGN(Function * f, fb.Build()); | ||
|
|
||
| ScopedVerifyEquivalence sve(f); | ||
| ASSERT_THAT(Run(p.get()), IsOkAndHolds(true)); | ||
| EXPECT_THAT(f->return_value(), m::Xor(m::Param("a"), m::Param("b"))); | ||
| } | ||
|
|
||
| TEST_F(BasicSimplificationPassTest, EqOfSwappedTwoWaySelects) { | ||
| auto p = CreatePackage(); | ||
| FunctionBuilder fb(TestName(), p.get()); | ||
| BValue p_sel = fb.Param("p", p->GetBitsType(1)); | ||
| BValue a = fb.Param("a", p->GetBitsType(32)); | ||
| BValue b = fb.Param("b", p->GetBitsType(32)); | ||
| BValue sel_ab = fb.Select(p_sel, {a, b}); | ||
| BValue sel_ba = fb.Select(p_sel, {b, a}); | ||
| fb.Eq(sel_ab, sel_ba); | ||
| XLS_ASSERT_OK_AND_ASSIGN(Function * f, fb.Build()); | ||
|
|
||
| ScopedVerifyEquivalence sve(f); | ||
| ASSERT_THAT(Run(p.get()), IsOkAndHolds(true)); | ||
| EXPECT_THAT(f->return_value(), m::Eq(m::Param("a"), m::Param("b"))); | ||
| } | ||
|
|
||
| TEST_F(BasicSimplificationPassTest, AddWithZero) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| auto p = CreatePackage(); | ||
| XLS_ASSERT_OK_AND_ASSIGN(Function * f, ParseFunction(R"( | ||
|
|
||
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?