Skip to content

Commit

Permalink
This change allows select to have no cases.
Browse files Browse the repository at this point in the history
Prior to this change, both the verifier and the select constructors expect such IR node to have at least one case.
However, bdd_simplification_pass can generate select nodes with no cases (line 430).

Since a select node with no cases (but with a default one) has a well defined semantics (i.e., always propagate the default case), we opted to allow it in the XLS IR.

PiperOrigin-RevId: 698845577
  • Loading branch information
scampanoni authored and copybara-github committed Nov 21, 2024
1 parent a87b3a1 commit 77f618f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion xls/ir/nodes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,9 @@ Select::Select(const SourceInfo& loc, Node* selector,
absl::Span<Node* const> cases,
std::optional<Node*> default_value, std::string_view name,
FunctionBase* function)
: Node(Op::kSel, cases[0]->GetType(), loc, name, function),
: Node(Op::kSel,
cases.empty() ? (*default_value)->GetType() : cases[0]->GetType(),
loc, name, function),
cases_size_(cases.size()),
has_default_value_(default_value.has_value()) {
CHECK(IsOpClass<Select>(op_))
Expand Down
4 changes: 3 additions & 1 deletion xls/ir/verify_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,9 @@ class NodeChecker : public DfsVisitor {
XLS_RETURN_IF_ERROR(ExpectHasBitsType(sel->selector()));
const int64_t selector_width = sel->selector()->BitCountOrDie();
const int64_t minimum_selector_width =
Bits::MinBitCountUnsigned(sel->cases().size() - 1);
sel->cases().empty()
? 0
: Bits::MinBitCountUnsigned(sel->cases().size() - 1);
const bool power_of_2_cases = IsPowerOfTwo(sel->cases().size());
if (selector_width < minimum_selector_width) {
return absl::InternalError(absl::StrFormat(
Expand Down

0 comments on commit 77f618f

Please sign in to comment.