Skip to content

Commit

Permalink
Fix issue with PredicateState hash implementation
Browse files Browse the repository at this point in the history
Due to not initializing the template anywhere we incorrectly implemented the hash function leading to compile errors if anything attempts to use it. Fix this issue.

PiperOrigin-RevId: 553849088
  • Loading branch information
allight authored and copybara-github committed Aug 4, 2023
1 parent 92279c4 commit d77fe7b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
14 changes: 14 additions & 0 deletions xls/passes/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,20 @@ cc_test(
],
)

cc_test(
name = "predicate_state_test",
srcs = ["predicate_state_test.cc"],
deps = [
":predicate_state",
"@com_google_absl//absl/hash",
"@com_google_absl//absl/strings:str_format",
"//xls/common:xls_gunit",
"//xls/common:xls_gunit_main",
"//xls/ir",
"//xls/ir:function_builder",
],
)

cc_library(
name = "narrowing_pass",
srcs = ["narrowing_pass.cc"],
Expand Down
2 changes: 1 addition & 1 deletion xls/passes/predicate_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace xls {
struct DefaultArm : public std::monostate {
template <typename H>
friend H AbslHashValue(H h, const DefaultArm& a) {
return H::Combine(h, std::monostate{});
return H::combine(std::move(h), std::monostate{});
}

template <typename Sink>
Expand Down
50 changes: 50 additions & 0 deletions xls/passes/predicate_state_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2023 The XLS Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "xls/passes/predicate_state.h"

#include "gtest/gtest.h"
#include "absl/hash/hash.h"
#include "absl/strings/str_format.h"
#include "xls/ir/function_builder.h"
#include "xls/ir/nodes.h"
#include "xls/ir/package.h"

namespace xls {

namespace {

TEST(PredicateState, Hash) {
EXPECT_NE(absl::Hash<PredicateState>()(PredicateState()),
absl::Hash<PredicateState>()(PredicateState(nullptr, 33)));
}

TEST(PredicateState, Stringify) {
Package p("test");
FunctionBuilder fb("test", &p);
BValue sel = fb.Select(
fb.Param("w", p.GetBitsType(2)),
{fb.Param("x", p.GetBitsType(12)), fb.Param("y", p.GetBitsType(12))},
fb.Param("z", p.GetBitsType(12)));
EXPECT_EQ(
absl::StrFormat("%v", PredicateState(sel.node()->As<Select>(), 0)),
absl::StrFormat("PredicateState[sel.%d: arm: 0]", sel.node()->id()));
EXPECT_EQ(absl::StrFormat("%v", PredicateState(sel.node()->As<Select>(),
PredicateState::kDefaultArm)),
absl::StrFormat("PredicateState[sel.%d: arm: DEFAULT]",
sel.node()->id()));
}

} // namespace
} // namespace xls

0 comments on commit d77fe7b

Please sign in to comment.