8000 xls/passes: Skip redundant predicate conjunction in ConditionalSpecializationPass by copybara-service[bot] · Pull Request #5594 · google/xls · GitHub
[go: up one dir, main page]

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions xls/passes/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2348,6 +2348,7 @@ cc_test(
"//xls/solvers",
"//xls/solvers:prover_matchers",
"//xls/solvers:solver",
"@abseil-cpp//absl/container:btree",
"@googletest//:gtest",
],
)
Expand Down Expand Up @@ -2855,6 +2856,7 @@ cc_library(
"//xls/ir:value",
"//xls/ir:value_utils",
"@abseil-cpp//absl/algorithm:container",
"@abseil-cpp//absl/base:core_headers",
"@abseil-cpp//absl/container:btree",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/container:flat_hash_set",
Expand Down
109 changes: 53 additions & 56 deletions xls/passes/bdd_query_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -974,29 +974,39 @@ std::optional<Bits> BddQueryEngine::ImpliedNodeValue(
/*assumption=*/std::nullopt);
}

std::optional<Bits> BddQueryEngine::ImpliedNodeValue(
std::optional<BddNodeIndex> BddQueryEngine::AndPredicates(
absl::Span<const std::pair<TreeBitLocation, bool>> predicate_bit_values,
Node* node, std::optional<BddNodeIndex> assumption) const {
if (!IsTracked(node) || !node->GetType()->IsBits()) {
return std::nullopt;
std::optional<BddNodeIndex> assumption) const {
if (assumption.has_value() && *assumption == bdd().zero()) {
return bdd().zero();
}
if (node->GetType()->GetFlatBitCount() == 0) {
return Bits(0);
if (predicate_bit_values.empty()) {
return assumption.value_or(bdd().one());
}

// Create a Bdd node for the predicate_bit_values.
SaturatingBddNodeVector bdd_predicate_bits;
for (const auto& [conjuction_bit_location, conjunction_value] :
bdd_predicate_bits.reserve(predicate_bit_values.size());
for (const auto& [conjunction_bit_location, conjunction_value] :
predicate_bit_values) {
std::optional<BddNodeIndex> conjuction_bit =
GetBddNode(conjuction_bit_location);
if (!conjuction_bit.has_value()) {
std::optional<BddNodeIndex> conjunction_bit =
GetBddNode(conjunction_bit_location);
if (!conjunction_bit.has_value()) {
// Skip this predicate; we don't recognize the node, so we can't see the
// effects of assuming it.
continue;
}
bdd_predicate_bits.push_back(
conjunction_value ? *conjuction_bit : bdd().Not(*conjuction_bit));
BddNodeIndex predicate_bit =
conjunction_value ? *conjunction_bit : bdd().Not(*conjunction_bit);
if (predicate_bit == bdd().zero()) {
return bdd().zero();
}
if (predicate_bit == bdd().one()) {
continue;
}
bdd_predicate_bits.push_back(predicate_bit);
}
if (bdd_predicate_bits.empty()) {
return assumption.value_or(bdd().one());
}
SaturatingBddNodeIndex bdd_predicate =
evaluator_->AndReduce(bdd_predicate_bits).front();
Expand All @@ -1006,16 +1016,30 @@ std::optional<Bits> BddQueryEngine::ImpliedNodeValue(
if (IsBddSaturated(bdd_predicate)) {
return std::nullopt;
}
BddNodeIndex bdd_predicate_bit = ToBddNode(bdd_predicate);
return ToBddNode(bdd_predicate);
}

// If the predicate evaluates to false, we can't determine
// what node value it implies. That is, !predicate || node_bit
// evaluates to true for both node_bit == 1 and == 0.
if (bdd_predicate_bit == bdd().zero()) {
std::optional<Bits> BddQueryEngine::ImpliedNodeValue(
absl::Span<const std::pair<TreeBitLocation, bool>> predicate_bit_values,
Node* node, std::optional<BddNodeIndex> assumption) const {
if (!IsTracked(node) || !node->GetType()->IsBits()) {
return std::nullopt;
}
if (node->GetType()->GetFlatBitCount() == 0) {
return Bits(0);
}
std::optional<BddNodeIndex> bdd_predicate =
AndPredicates(predicate_bit_values, assumption);
if (!bdd_predicate.has_value()) {
return std::nullopt;
}
if (*bdd_predicate == bdd().zero()) {
// If the predicate evaluates to false, we can't determine what node value
// it implies: `!predicate || node_bit` is vacuously true for both 0 and 1.
return std::nullopt;
}

// Check if bdd_predicate_bit implies anything about the node.
// Check if bdd_predicate implies anything about the node.
CHECK(node->GetType()->IsBits());
InlineBitmap bitmap(node->BitCountOrDie());
for (int node_idx = 0; node_idx < node->BitCountOrDie(); ++node_idx) {
Expand All @@ -1024,7 +1048,7 @@ std::optional<Bits> BddQueryEngine::ImpliedNodeValue(
if (!bit.has_value()) {
return std::nullopt;
}
std::optional<bool> bit_value = KnownValue(*bit, bdd_predicate_bit);
std::optional<bool> bit_value = KnownValue(*bit, *bdd_predicate);
if (!bit_value.has_value()) {
return std::nullopt;
}
Expand All @@ -1046,41 +1070,14 @@ std::optional<TernaryVector> BddQueryEngine::ImpliedNodeTernary(
if (!IsTracked(node) || !node->GetType()->IsBits()) {
return std::nullopt;
}

// Create a Bdd node for the predicate_bit_values.
SaturatingBddNodeVector bdd_predicate_bits;
for (const auto& [conjuction_bit_location, conjunction_value] :
predicate_bit_values) {
std::optional<BddNodeIndex> conjuction_bit =
GetBddNode(conjuction_bit_location);
if (!conjuction_bit.has_value()) {
// Skip this predicate; we don't recognize the node, so we can't see the
// effects of assuming it.
continue;
}
bdd_predicate_bits.push_back(
conjunction_value ? *conjuction_bit : bdd().Not(*conjuction_bit));
if (bdd_predicate_bits.back() == SaturatingBddNodeIndex(bdd().zero())) {
// If the predicate evaluates to false, we can't determine
// what node value it implies. That is, !predicate || node_bit
// evaluates to true for both node_bit == 1 and == 0.
return std::nullopt;
}
}
SaturatingBddNodeIndex bdd_predicate =
evaluator_->AndReduce(bdd_predicate_bits).front();
if (assumption.has_value()) {
bdd_predicate = evaluator_->And(bdd_predicate, *assumption);
}
if (IsBddSaturated(bdd_predicate)) {
std::optional<BddNodeIndex> bdd_predicate =
AndPredicates(predicate_bit_values, assumption);
if (!bdd_predicate.has_value()) {
return std::nullopt;
}
BddNodeIndex bdd_predicate_bit = ToBddNode(bdd_predicate);

// If the predicate evaluates to false, we can't determine
// what node value it implies. That is, !predicate || node_bit
// evaluates to true for both node_bit == 1 and == 0.
if (bdd_predicate_bit == bdd().zero()) {
if (*bdd_predicate == bdd().zero()) {
// If the predicate evaluates to false, we can't determine what bit values
// it implies: `!predicate || node_bit` is vacuously true for both 0 and 1.
return std::nullopt;
}

Expand All @@ -1090,16 +1087,16 @@ std::optional<TernaryVector> BddQueryEngine::ImpliedNodeTernary(
if (!bdd_node_bit.has_value()) {
return TernaryValue::kUnknown;
}
if (MutuallyExclusive(bdd_predicate_bit, *bdd_node_bit)) {
if (MutuallyExclusive(*bdd_predicate, *bdd_node_bit)) {
return TernaryValue::kKnownZero;
}
if (Implies(bdd_predicate_bit, *bdd_node_bit)) {
if (Implies(*bdd_predicate, *bdd_node_bit)) {
return TernaryValue::kKnownOne;
}
return TernaryValue::kUnknown;
};

// Check if bdd_predicate_bit implies anything about the node.
// Check if bdd_predicate implies anything about the node.
CHECK(node->GetType()->IsBits());
TernaryVector ternary(node->BitCountOrDie(), TernaryValue::kUnknown);
for (int node_idx = 0; node_idx < node->BitCountOrDie(); ++node_idx) {
Expand Down
6 changes: 6 additions & 0 deletions xls/passes/bdd_query_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ class BddQueryEngine
std::optional<TernaryVector> ImpliedNodeTernary(
absl::Span<const std::pair<TreeBitLocation, bool>> predicate_bit_values,
Node* node, std::optional<BddNodeIndex> assumption) const;
// Returns the logical AND of `predicate_bit_values` (ignoring untracked bits)
// and `assumption`, or `std::nullopt` if the result exceeds the complexity
// limit.
std::optional<BddNodeIndex> AndPredicates(
absl::Span<const std::pair<TreeBitLocation, bool>> predicate_bit_values,
std::optional<BddNodeIndex> assumption) const;
bool KnownEquals(const TreeBitLocation& a, const TreeBitLocation& b,
std::optional<BddNodeIndex> assumption) const;
bool KnownNotEquals(const TreeBitLocation& a, const TreeBitLocation& b,
Expand Down
103 changes: 103 additions & 0 deletions xls/passes/bdd_query_engine_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#include "xls/passes/bdd_query_engine.h"

#include <cstdint>
#include <initializer_list>
#include <memory>
#include <optional>
#include <utility>
#include <vector>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/container/btree_map.h"
#include "xls/common/status/matchers.h"
#include "xls/data_structures/leaf_type_tree.h"
#include "xls/ir/bits.h"
Expand Down Expand Up @@ -556,5 +558,106 @@ TEST_F(BddQueryEngineTest, NestedSpecializeGivenRespectsComplexityLimit) {
EXPECT_TRUE(s2->IsAllOnes(extra.node()));
}

// Shared 1-bit test graph for `ImpliedNodeValue` / `ImpliedNodeTernary` queries
// on base and specialized `BddQueryEngine`s:
// - `a_` (and unconstrained 1-bit param `b`): unconstrained 1-bit inputs
// - `one_lit_`, `zero_lit_`: constant 1-bit literals (tautology /
// known-false)
// - `pair_val_`: `concat(a_, b)` (bit 0 is `b`, bit 1 is `a_`)
// - `implies_a_`: `or(a_, b)` (implied to `1` whenever `a_ == 1`)
// - `not_a_`: `not(a_)` (contradicts `a_ == 1`)
class BddQueryEngineImpliedQueryTest : public BddQueryEngineTest {
protected:
void SetUp() override {
p_ = CreatePackage();
FunctionBuilder fb(TestName(), p_.get());
BValue a = fb.Param("a", p_->GetBitsType(1));
BValue b = fb.Param("b", p_->GetBitsType(1));
BValue one_lit = fb.Literal(UBits(1, 1));
BValue zero_lit = fb.Literal(UBits(0, 1));
BValue pair_val = fb.Concat({a, b});
BValue implies_a = fb.Or(a, b);
BValue not_a = fb.Not(a);
XLS_ASSERT_OK_AND_ASSIGN(
Function * f, fb.BuildWithReturnValue(fb.Tuple(
{pair_val, implies_a, not_a, one_lit, zero_lit})));
a_ = a.node();
one_lit_ = one_lit.node();
zero_lit_ = zero_lit.node();
pair_val_ = pair_val.node();
implies_a_ = implies_a.node();
not_a_ = not_a.node();
XLS_ASSERT_OK(qe_.Populate(f).status());
}

// Returns a `QueryEngine` specialized from `qe_` by assuming each 1-bit node
// in `nodes` is `kKnownOne`.
std::unique_ptr<QueryEngine> SpecializeKnownOne(
std::initializer_list<Node*> nodes) {
absl::btree_map<Node*, ValueKnowledge, Node::NodeIdLessThan> givens;
for (Node* node : nodes) {
givens[node].ternary = TernaryTree::CreateSingleElementTree(
node->GetType(), TernaryVector{TernaryValue::kKnownOne});
}
return qe_.SpecializeGiven(givens);
}

std::unique_ptr<VerifiedPackage> p_;
BddQueryEngine qe_;
Node* a_ = nullptr;
Node* one_lit_ = nullptr;
Node* zero_lit_ = nullptr;
Node* pair_val_ = nullptr;
Node* implies_a_ = nullptr;
Node* not_a_ = nullptr;
};

TEST_F(BddQueryEngineImpliedQueryTest, SpecializedEngineWithEmptyPredicates) {
std::unique_ptr<QueryEngine> spec = SpecializeKnownOne({a_});
EXPECT_THAT(spec->ImpliedNodeValue({}, implies_a_),
testing::Optional(UBits(1, 1)));
EXPECT_THAT(spec->ImpliedNodeTernary({}, pair_val_),
testing::Optional(TernaryVector{TernaryValue::kUnknown,
TernaryValue::kKnownOne}));
}

TEST_F(BddQueryEngineImpliedQueryTest,
SpecializedEngineWithContradictoryAssumption) {
std::unique_ptr<QueryEngine> unsat = SpecializeKnownOne({a_, not_a_});
EXPECT_EQ(unsat->ImpliedNodeValue({}, implies_a_), std::nullopt);
EXPECT_EQ(unsat->ImpliedNodeTernary({}, pair_val_), std::nullopt);
}

TEST_F(BddQueryEngineImpliedQueryTest, KnownFalsePredicateBitReturnsNullopt) {
EXPECT_EQ(qe_.ImpliedNodeValue({{TreeBitLocation(zero_lit_, 0), true},
{TreeBitLocation(a_, 0), true}},
implies_a_),
std::nullopt);
EXPECT_EQ(qe_.ImpliedNodeTernary({{TreeBitLocation(zero_lit_, 0), true},
{TreeBitLocation(a_, 0), true}},
pair_val_),
std::nullopt);
}

TEST_F(BddQueryEngineImpliedQueryTest, TautologyPredicateBitIsSkipped) {
EXPECT_THAT(qe_.ImpliedNodeValue({{TreeBitLocation(one_lit_, 0), true},
{TreeBitLocation(a_, 0), true}},
implies_a_),
testing::Optional(UBits(1, 1)));
EXPECT_THAT(qe_.ImpliedNodeTernary({{TreeBitLocation(one_lit_, 0), true},
{TreeBitLocation(a_, 0), true}},
pair_val_),
testing::Optional(TernaryVector{TernaryValue::kUnknown,
TernaryValue::kKnownOne}));
EXPECT_EQ(
qe_.ImpliedNodeValue({{TreeBitLocation(one_lit_, 0), true}}, implies_a_),
std::nullopt);

std::unique_ptr<QueryEngine> spec = SpecializeKnownOne({a_});
EXPECT_THAT(spec->ImpliedNodeValue({{TreeBitLocation(one_lit_, 0), true}},
implies_a_),
testing::Optional(UBits(1, 1)));
}

} // namespace
} // namespace xls
Loading
Loading
0