Skip to content

Commit

Permalink
Automated rollback of commit 6dd9ed9.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 652612156
  • Loading branch information
ericastor authored and copybara-github committed Jul 15, 2024
1 parent f137e0b commit 473d522
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 66 deletions.
12 changes: 8 additions & 4 deletions xls/dslx/ir_convert/function_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -987,9 +987,11 @@ absl::Status FunctionConverter::HandleMatch(const Match* node) {
BValue not_any_prev_selected;
if (prior_selectors.empty()) {
not_any_prev_selected = function_builder_->Literal(UBits(1, 1));
} else if (prior_selectors.size() == 1) {
not_any_prev_selected =
function_builder_->Not(prior_selectors.front());
} else {
not_any_prev_selected = function_builder_->Not(
function_builder_->Or(prior_selectors));
not_any_prev_selected = function_builder_->Nor(prior_selectors);
}
BValue this_arm_selected = arm_selectors.back();
return function_builder_->And({orig_control_predicate(),
Expand All @@ -1014,9 +1016,11 @@ absl::Status FunctionConverter::HandleMatch(const Match* node) {
BValue not_any_prev_selected;
if (prior_selectors.empty()) {
not_any_prev_selected = function_builder_->Literal(UBits(1, 1));
} else {
} else if (prior_selectors.size() == 1) {
not_any_prev_selected =
function_builder_->Not(function_builder_->Or(prior_selectors));
function_builder_->Not(prior_selectors.front());
} else {
not_any_prev_selected = function_builder_->Nor(prior_selectors);
}
return function_builder_->And(
{orig_control_predicate(), not_any_prev_selected});
Expand Down
17 changes: 8 additions & 9 deletions xls/dslx/ir_convert/testdata/ir_converter_test_FailInMatch.ir
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ fn __itok__test_module__main(__token: token, __activated: bits[1], x: bits[32])
eq.5: bits[1] = eq(literal.4, x, id=5)
and.8: bits[1] = and(__activated, literal.7, eq.5, id=8)
not.9: bits[1] = not(and.8, id=9)
concat.13: bits[1] = concat(eq.5, id=13)
assert.10: token = assert(__token, not.9, message="Assertion failure via fail! @ test_module.x:4:20-4:34", label="failure", id=10)
one_hot.14: bits[2] = one_hot(concat.13, lsb_prio=true, id=14)
concat.13: bits[1] = concat(eq.5, id=13)
identity.11: bits[32] = identity(x, id=11)
after_all.16: token = after_all(assert.10, id=16)
one_hot_sel.15: bits[32] = one_hot_sel(one_hot.14, cases=[identity.11, x], id=15)
after_all.15: token = after_all(assert.10, id=15)
priority_sel.14: bits[32] = priority_sel(concat.13, cases=[identity.11], default=x, id=14)
literal.6: bits[8][7] = literal(value=[102, 97, 105, 108, 117, 114, 101], id=6)
literal.12: bits[1] = literal(value=1, id=12)
ret tuple.17: (token, bits[32]) = tuple(after_all.16, one_hot_sel.15, id=17)
ret tuple.16: (token, bits[32]) = tuple(after_all.15, priority_sel.14, id=16)
}

fn __test_module__main(x: bits[32]) -> bits[32] {
after_all.19: token = after_all(id=19)
literal.20: bits[1] = literal(value=1, id=20)
invoke.21: (token, bits[32]) = invoke(after_all.19, literal.20, x, to_apply=__itok__test_module__main, id=21)
ret tuple_index.22: bits[32] = tuple_index(invoke.21, index=1, id=22)
after_all.18: token = after_all(id=18)
literal.19: bits[1] = literal(value=1, id=19)
invoke.20: (token, bits[32]) = invoke(after_all.18, literal.19, x, to_apply=__itok__test_module__main, id=20)
ret tuple_index.21: bits[32] = tuple_index(invoke.20, index=1, id=21)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ fn __itok__test_module__main(__token: token, __activated: bits[1], x: bits[32])
eq.14: bits[1] = eq(literal.13, x, id=14)
and.16: bits[1] = and(__activated, literal.15, eq.14, id=16)
invoke.17: (token, bits[32]) = invoke(__token, and.16, x, to_apply=__itok__test_module__do_fail, id=17)
concat.21: bits[1] = concat(eq.14, id=21)
tuple_index.18: token = tuple_index(invoke.17, index=0, id=18)
one_hot.22: bits[2] = one_hot(concat.21, lsb_prio=true, id=22)
concat.21: bits[1] = concat(eq.14, id=21)
tuple_index.19: bits[32] = tuple_index(invoke.17, index=1, id=19)
after_all.24: token = after_all(tuple_index.18, id=24)
one_hot_sel.23: bits[32] = one_hot_sel(one_hot.22, cases=[tuple_index.19, x], id=23)
after_all.23: token = after_all(tuple_index.18, id=23)
priority_sel.22: bits[32] = priority_sel(concat.21, cases=[tuple_index.19], default=x, id=22)
literal.20: bits[1] = literal(value=1, id=20)
ret tuple.25: (token, bits[32]) = tuple(after_all.24, one_hot_sel.23, id=25)
ret tuple.24: (token, bits[32]) = tuple(after_all.23, priority_sel.22, id=24)
}
3 changes: 1 addition & 2 deletions xls/dslx/ir_convert/testdata/ir_converter_test_Match.ir
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ fn __test_module__f(x: bits[8]) -> bits[2] {
eq.6: bits[1] = eq(literal.5, x, id=6)
eq.3: bits[1] = eq(literal.2, x, id=3)
concat.10: bits[2] = concat(eq.6, eq.3, id=10)
one_hot.11: bits[3] = one_hot(concat.10, lsb_prio=true, id=11)
literal.4: bits[2] = literal(value=0, id=4)
literal.7: bits[2] = literal(value=1, id=7)
literal.9: bits[2] = literal(value=2, id=9)
literal.8: bits[1] = literal(value=1, id=8)
ret one_hot_sel.12: bits[2] = one_hot_sel(one_hot.11, cases=[literal.4, literal.7, literal.9], id=12)
ret priority_sel.11: bits[2] = priority_sel(concat.10, cases=[literal.4, literal.7], default=literal.9, id=11)
}
3 changes: 1 addition & 2 deletions xls/dslx/ir_convert/testdata/ir_converter_test_MatchDense.ir
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ fn __test_module__f(x: bits[2]) -> bits[8] {
eq.6: bits[1] = eq(literal.5, x, id=6)
eq.3: bits[1] = eq(literal.2, x, id=3)
concat.13: bits[3] = concat(eq.9, eq.6, eq.3, id=13)
one_hot.14: bits[4] = one_hot(concat.13, lsb_prio=true, id=14)
literal.4: bits[8] = literal(value=42, id=4)
literal.7: bits[8] = literal(value=64, id=7)
literal.10: bits[8] = literal(value=128, id=10)
literal.12: bits[8] = literal(value=255, id=12)
literal.11: bits[1] = literal(value=1, id=11)
ret one_hot_sel.15: bits[8] = one_hot_sel(one_hot.14, cases=[literal.4, literal.7, literal.10, literal.12], id=15)
ret priority_sel.14: bits[8] = priority_sel(concat.13, cases=[literal.4, literal.7, literal.10], default=literal.12, id=14)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ fn __test_module__f(x: bits[2]) -> bits[8] {
eq.7: bits[1] = eq(ONE, x, id=7)
eq.5: bits[1] = eq(ZERO, x, id=5)
concat.13: bits[3] = concat(eq.9, eq.7, eq.5, id=13)
one_hot.14: bits[4] = one_hot(concat.13, lsb_prio=true, id=14)
literal.6: bits[8] = literal(value=42, id=6)
literal.8: bits[8] = literal(value=64, id=8)
literal.10: bits[8] = literal(value=128, id=10)
literal.12: bits[8] = literal(value=255, id=12)
literal.11: bits[1] = literal(value=1, id=11)
ret one_hot_sel.15: bits[8] = one_hot_sel(one_hot.14, cases=[literal.6, literal.8, literal.10, literal.12], id=15)
ret priority_sel.14: bits[8] = priority_sel(concat.13, cases=[literal.6, literal.8, literal.10], default=literal.12, id=14)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ fn __test_module__f(x: bits[8]) -> bits[2] {
literal.2: bits[8] = literal(value=42, id=2)
eq.3: bits[1] = eq(literal.2, x, id=3)
concat.7: bits[1] = concat(eq.3, id=7)
one_hot.8: bits[2] = one_hot(concat.7, lsb_prio=true, id=8)
literal.4: bits[2] = literal(value=3, id=4)
bit_slice.6: bits[2] = bit_slice(x, start=0, width=2, id=6)
literal.5: bits[1] = literal(value=1, id=5)
ret one_hot_sel.9: bits[2] = one_hot_sel(one_hot.8, cases=[literal.4, bit_slice.6], id=9)
ret priority_sel.8: bits[2] = priority_sel(concat.7, cases=[literal.4], default=bit_slice.6, id=8)
}
28 changes: 13 additions & 15 deletions xls/dslx/ir_convert/testdata/ir_converter_test_MatchMultiFail.ir
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,30 @@ file_number 0 "test_module.x"
fn __itok__test_module__main(__token: token, __activated: bits[1], x: bits[32]) -> (token, bits[32]) {
literal.4: bits[32] = literal(value=42, id=4)
eq.5: bits[1] = eq(literal.4, x, id=5)
or.16: bits[1] = or(eq.5, id=16)
literal.7: bits[1] = literal(value=1, id=7)
not.17: bits[1] = not(or.16, id=17)
not.16: bits[1] = not(eq.5, id=16)
and.8: bits[1] = and(__activated, literal.7, eq.5, id=8)
and.18: bits[1] = and(__activated, not.17, id=18)
and.17: bits[1] = and(__activated, not.16, id=17)
literal.14: bits[32] = literal(value=1, id=14)
not.9: bits[1] = not(and.8, id=9)
not.19: bits[1] = not(and.18, id=19)
concat.22: bits[1] = concat(eq.5, id=22)
not.18: bits[1] = not(and.17, id=18)
add.15: bits[32] = add(x, literal.14, id=15)
assert.10: token = assert(__token, not.9, message="Assertion failure via fail! @ test_module.x:4:20-4:36", label="failure_0", id=10)
assert.20: token = assert(__token, not.19, message="Assertion failure via fail! @ test_module.x:5:15-5:37", label="failure_1", id=20)
one_hot.23: bits[2] = one_hot(concat.22, lsb_prio=true, id=23)
assert.19: token = assert(__token, not.18, message="Assertion failure via fail! @ test_module.x:5:15-5:37", label="failure_1", id=19)
concat.21: bits[1] = concat(eq.5, id=21)
identity.11: bits[32] = identity(x, id=11)
identity.21: bits[32] = identity(add.15, id=21)
after_all.25: token = after_all(assert.10, assert.20, id=25)
one_hot_sel.24: bits[32] = one_hot_sel(one_hot.23, cases=[identity.11, identity.21], id=24)
identity.20: bits[32] = identity(add.15, id=20)
after_all.23: token = after_all(assert.10, assert.19, id=23)
priority_sel.22: bits[32] = priority_sel(concat.21, cases=[identity.11], default=identity.20, id=22)
literal.6: bits[8][9] = literal(value=[102, 97, 105, 108, 117, 114, 101, 95, 48], id=6)
literal.12: bits[1] = literal(value=1, id=12)
literal.13: bits[8][9] = literal(value=[102, 97, 105, 108, 117, 114, 101, 95, 49], id=13)
ret tuple.26: (token, bits[32]) = tuple(after_all.25, one_hot_sel.24, id=26)
ret tuple.24: (token, bits[32]) = tuple(after_all.23, priority_sel.22, id=24)
}

fn __test_module__main(x: bits[32]) -> bits[32] {
after_all.28: token = after_all(id=28)
literal.29: bits[1] = literal(value=1, id=29)
invoke.30: (token, bits[32]) = invoke(after_all.28, literal.29, x, to_apply=__itok__test_module__main, id=30)
ret tuple_index.31: bits[32] = tuple_index(invoke.30, index=1, id=31)
after_all.26: token = after_all(id=26)
literal.27: bits[1] = literal(value=1, id=27)
invoke.28: (token, bits[32]) = invoke(after_all.26, literal.27, x, to_apply=__itok__test_module__main, id=28)
ret tuple_index.29: bits[32] = tuple_index(invoke.28, index=1, id=29)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ fn __test_module__f(x: bits[8]) -> bits[2] {
FOO: bits[8] = literal(value=255, id=2)
eq.3: bits[1] = eq(FOO, x, id=3)
concat.7: bits[1] = concat(eq.3, id=7)
one_hot.8: bits[2] = one_hot(concat.7, lsb_prio=true, id=8)
literal.4: bits[2] = literal(value=0, id=4)
bit_slice.6: bits[2] = bit_slice(x, start=0, width=2, id=6)
literal.5: bits[1] = literal(value=1, id=5)
ret one_hot_sel.9: bits[2] = one_hot_sel(one_hot.8, cases=[literal.4, bit_slice.6], id=9)
ret priority_sel.8: bits[2] = priority_sel(concat.7, cases=[literal.4], default=bit_slice.6, id=8)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ fn __test_module__main(x: bits[8]) -> bits[8] {
literal.2: bits[8] = literal(value=42, id=2)
eq.3: bits[1] = eq(literal.2, x, id=3)
concat.6: bits[1] = concat(eq.3, id=6)
one_hot.7: bits[2] = one_hot(concat.6, lsb_prio=true, id=7)
literal.4: bits[8] = literal(value=255, id=4)
literal.5: bits[1] = literal(value=1, id=5)
ret t: bits[8] = one_hot_sel(one_hot.7, cases=[literal.4, x], id=8)
ret t: bits[8] = priority_sel(concat.6, cases=[literal.4], default=x, id=7)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ fn __test_module__f(x: bits[8]) -> bits[2] {
eq.6: bits[1] = eq(literal.5, x, id=6)
eq.3: bits[1] = eq(literal.2, x, id=3)
concat.10: bits[2] = concat(eq.6, eq.3, id=10)
one_hot.11: bits[3] = one_hot(concat.10, lsb_prio=true, id=11)
x__1: bits[2] = literal(value=0, id=4)
x__2: bits[2] = literal(value=1, id=7)
x__3: bits[2] = literal(value=2, id=9)
literal.8: bits[1] = literal(value=1, id=8)
ret one_hot_sel.12: bits[2] = one_hot_sel(one_hot.11, cases=[x__1, x__2, x__3], id=12)
ret priority_sel.11: bits[2] = priority_sel(concat.10, cases=[x__1, x__2], default=x__3, id=11)
}
5 changes: 1 addition & 4 deletions xls/ir/function_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,14 @@ BValue BuilderBase::MatchTrue(absl::Span<const Case> cases,
selector_bits.push_back(cases[i].clause);
case_values.push_back(cases[i].value);
}
case_values.push_back(default_value);

// Reverse the order of the bits because bit index and indexing of concat
// elements are reversed. That is, the zero-th operand of concat becomes the
// most-significant part (highest index) of the result.
std::reverse(selector_bits.begin(), selector_bits.end());

BValue concat = Concat(selector_bits, loc);
BValue one_hot = OneHot(concat, /*priority=*/LsbOrMsb::kLsb, loc);

return OneHotSelect(one_hot, case_values, loc, name);
return PrioritySelect(concat, case_values, default_value, loc, name);
}

BValue BuilderBase::AfterAll(absl::Span<const BValue> dependencies,
Expand Down
29 changes: 14 additions & 15 deletions xls/ir/function_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ TEST(FunctionBuilderTest, Match) {
{b.Literal(UBits(8, 8)), z}},
the_default);
XLS_ASSERT_OK_AND_ASSIGN(Function * f, b.Build());
EXPECT_THAT(f->return_value(),
m::OneHotSelect(
m::OneHot(m::Concat(m::Eq(m::Param("cond"), m::Literal(8)),
m::Eq(m::Param("cond"), m::Literal(123)),
m::Eq(m::Param("cond"), m::Literal(42)))),
/*cases=*/{m::Param("x"), m::Param("y"), m::Param("z"),
m::Param("default")}));
EXPECT_THAT(
f->return_value(),
m::PrioritySelect(m::Concat(m::Eq(m::Param("cond"), m::Literal(8)),
m::Eq(m::Param("cond"), m::Literal(123)),
m::Eq(m::Param("cond"), m::Literal(42))),
/*cases=*/{m::Param("x"), m::Param("y"), m::Param("z")},
m::Param("default")));
}

TEST(FunctionBuilderTest, MatchTrue) {
Expand All @@ -207,12 +207,11 @@ TEST(FunctionBuilderTest, MatchTrue) {
BValue the_default = b.Param("default", value_type);
b.MatchTrue({{p0, x}, {p1, y}, {p2, z}}, the_default);
XLS_ASSERT_OK_AND_ASSIGN(Function * f, b.Build());
EXPECT_THAT(
f->return_value(),
m::OneHotSelect(
m::OneHot(m::Concat(m::Param("p2"), m::Param("p1"), m::Param("p0"))),
/*cases=*/{m::Param("x"), m::Param("y"), m::Param("z"),
m::Param("default")}));
EXPECT_THAT(f->return_value(),
m::PrioritySelect(
m::Concat(m::Param("p2"), m::Param("p1"), m::Param("p0")),
/*cases=*/{m::Param("x"), m::Param("y"), m::Param("z")},
m::Param("default")));
}

// Note: for API consistency we allow the definition of MatchTrue to work when
Expand All @@ -225,8 +224,8 @@ TEST(FunctionBuilderTest, MatchTrueNoCaseArmsOnlyDefault) {
b.MatchTrue({}, the_default);
XLS_ASSERT_OK_AND_ASSIGN(Function * f, b.Build());
EXPECT_THAT(f->return_value(),
m::OneHotSelect(m::OneHot(),
/*cases=*/{m::Param("default")}));
m::PrioritySelect(m::Concat(),
/*cases=*/{}, m::Param("default")));
}

TEST(FunctionBuilderTest, PrioritySelectOp) {
Expand Down

0 comments on commit 473d522

Please sign in to comment.