mirror of
https://github.com/Z3Prover/z3
synced 2025-04-24 01:25:31 +00:00
First step towards explaining single bits
This commit is contained in:
parent
d5bc4b84a7
commit
ec06027515
8 changed files with 418 additions and 6 deletions
|
@ -8,6 +8,7 @@ z3_add_component(polysat
|
|||
constraint.cpp
|
||||
constraint_manager.cpp
|
||||
eq_explain.cpp
|
||||
fixed_bits.cpp
|
||||
forbidden_intervals.cpp
|
||||
inference_logger.cpp
|
||||
justification.cpp
|
||||
|
|
|
@ -17,6 +17,7 @@ Author:
|
|||
#include "math/polysat/interval.h"
|
||||
#include "math/polysat/assignment.h"
|
||||
#include "math/polysat/univariate/univariate_solver.h"
|
||||
#include "util/tbv.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace polysat {
|
||||
|
@ -84,6 +85,7 @@ namespace polysat {
|
|||
bool is_currently_false(solver const& s, bool is_positive) const { return is_currently_true(s, !is_positive); }
|
||||
|
||||
virtual void narrow(solver& s, bool is_positive, bool first) = 0;
|
||||
virtual void propagate_bits(solver& s, bool is_positive) {}
|
||||
/**
|
||||
* If possible, produce a lemma that contradicts the given assignment.
|
||||
* This method should not modify the solver's search state.
|
||||
|
|
222
src/math/polysat/fixed_bits.cpp
Normal file
222
src/math/polysat/fixed_bits.cpp
Normal file
|
@ -0,0 +1,222 @@
|
|||
/*++
|
||||
Copyright (c) 2022 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
fixed_bits
|
||||
|
||||
Abstract:
|
||||
|
||||
Associates every pdd with the set of already fixed bits and justifications for this
|
||||
|
||||
--*/
|
||||
|
||||
#include "math/polysat/fixed_bits.h"
|
||||
#include "math/polysat/solver.h"
|
||||
|
||||
namespace polysat {
|
||||
|
||||
tbv_manager& fixed_bits::get_manager(unsigned sz){
|
||||
m_tbv_managers.reserve(sz + 1);
|
||||
if (!m_tbv_managers[sz])
|
||||
m_tbv_managers.set(sz, alloc(tbv_manager, sz));
|
||||
return *m_tbv_managers[sz];
|
||||
}
|
||||
|
||||
tbv_manager& fixed_bits::get_manager(const pdd& v) {
|
||||
return get_manager(v.power_of_2());
|
||||
}
|
||||
|
||||
tbv_ref& fixed_bits::get_tbv(pvar v, unsigned sz) {
|
||||
if (m_var_to_tbv.size() <= v) {
|
||||
m_var_to_tbv.reserve(v + 1);
|
||||
auto& manager = get_manager(sz);
|
||||
m_var_to_tbv[v] = tbv_ref(manager, manager.allocate());
|
||||
return *m_var_to_tbv[v];
|
||||
}
|
||||
auto& old_manager = m_var_to_tbv[v]->manager();
|
||||
if (old_manager.num_tbits() >= sz)
|
||||
return *(m_var_to_tbv[v]);
|
||||
tbv* old_tbv = m_var_to_tbv[v]->detach();
|
||||
auto& new_manager = get_manager(sz);
|
||||
tbv* new_tbv = new_manager.allocate();
|
||||
old_manager.copy(*new_tbv, *old_tbv); // Copy the lower bits to the new (larger) tbv
|
||||
old_manager.deallocate(old_tbv);
|
||||
m_var_to_tbv[v] = tbv_ref(new_manager, new_tbv);
|
||||
return *m_var_to_tbv[v];
|
||||
}
|
||||
|
||||
tbv_ref& fixed_bits::get_tbv(const pdd& p) {
|
||||
SASSERT(p.is_var());
|
||||
return get_tbv(p.var(), p.power_of_2());
|
||||
}
|
||||
|
||||
tbit fixed_bits::get_value(const pdd& p, unsigned idx) {
|
||||
SASSERT(p.is_var());
|
||||
return get_tbv(p)[idx];
|
||||
}
|
||||
|
||||
bool fixed_bits::fix_value(solver& s, const pdd& p, unsigned idx, tbit val, constraint* c, bit_dependency& dep) {
|
||||
SASSERT(val != BIT_x); // We don't use don't-cares
|
||||
SASSERT(p.is_var());
|
||||
if (val == BIT_z)
|
||||
return true;
|
||||
tbv_ref& tbv = get_tbv(p);
|
||||
tbit curr_val = tbv[idx];
|
||||
|
||||
if (val == curr_val)
|
||||
return true;
|
||||
|
||||
auto& m = tbv.manager();
|
||||
|
||||
if (curr_val == BIT_z) {
|
||||
m.set(*tbv, idx, val);
|
||||
m_tbv_to_justification[std::pair(tbv.get(), idx)] = bit_justication(c, (bit_dependency&&)std::move(dep));
|
||||
return true;
|
||||
}
|
||||
SASSERT((curr_val == BIT_1 && val == BIT_0) || (curr_val == BIT_0 && val == BIT_1));
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
void fixed_bits::clear_value(const pdd& p, unsigned idx) {
|
||||
SASSERT(p.is_var());
|
||||
tbv_ref& tbv = get_tbv(p);
|
||||
auto& m = tbv.manager();
|
||||
m.set(*tbv, idx, BIT_z);
|
||||
|
||||
SASSERT(m_tbv_to_justification.contains(std::pair(tbv.get(), idx)));
|
||||
m_tbv_to_justification[std::pair(tbv.get(), idx)] = bit_justication();
|
||||
}
|
||||
|
||||
#define COUNT(DOWN, TO_COUNT) \
|
||||
do { \
|
||||
unsigned sz = ref.num_tbits(); \
|
||||
unsigned least = 0; \
|
||||
for (; least < sz; least++) { \
|
||||
if (ref[((DOWN) ? sz - least - 1 : least)] != (TO_COUNT)) \
|
||||
break; \
|
||||
} \
|
||||
if (least == sz) \
|
||||
return { sz, sz }; /* For sure TO_COUNT */ \
|
||||
unsigned most = least; \
|
||||
for (; most < sz; most++) { \
|
||||
if (ref[((DOWN) ? sz - most - 1 : most)] == ((TO_COUNT) == BIT_0 ? BIT_1 : BIT_0)) \
|
||||
break; \
|
||||
} \
|
||||
return { least, most }; /* There are between "least" and "most" leading/trailing TO_COUNT */ \
|
||||
} while(false)
|
||||
|
||||
std::pair<unsigned, unsigned> fixed_bits::leading_zeros(const tbv_ref& ref) { COUNT(false, BIT_0); }
|
||||
std::pair<unsigned, unsigned> fixed_bits::trailing_zeros(const tbv_ref& ref) { COUNT(true, BIT_0); }
|
||||
std::pair<unsigned, unsigned> fixed_bits::leading_ones(const tbv_ref& ref) { COUNT(false, BIT_1); }
|
||||
std::pair<unsigned, unsigned> fixed_bits::trailing_ones(const tbv_ref& ref) { COUNT(true, BIT_1); }
|
||||
|
||||
std::pair<rational, rational> fixed_bits::min_max(const tbv_ref& ref) {
|
||||
unsigned sz = ref.num_tbits();
|
||||
rational least(0);
|
||||
rational most(0);
|
||||
|
||||
for (unsigned i = 0; i < sz; i++) {
|
||||
tbit v = ref[i];
|
||||
least *= 2;
|
||||
most *= 2;
|
||||
if (v == BIT_1) {
|
||||
least++;
|
||||
most++;
|
||||
}
|
||||
else if (v == BIT_z)
|
||||
most++;
|
||||
}
|
||||
|
||||
return { least, most };
|
||||
}
|
||||
|
||||
// multiplication: (1*p0 + 2*p1 + 4*p2 + 8*p3 + ...) * (1*q0 + 2*q1 + 4*q2 + 8*q3 + ...) =
|
||||
// = 1 * (p0 q0) + 2 * (p0 q1 + p1 q0) + 4 * (p0 q2 + p1 q1 + p2 q0) + 8 * (p0 q3 + p1 q2 + p2 q1 + p3 q0) + ...
|
||||
// maintains
|
||||
void fixed_bits::multiply(tbv_ref& in_out, const tbv_ref& in2) {
|
||||
auto m= in_out.manager();
|
||||
m_aux_values.reserve(m.num_tbits());
|
||||
|
||||
unsigned min_bit_value = 0; // The value of the current bit assuming all unknown bits are 0
|
||||
unsigned max_bit_value = 0; // The value of the current bit assuming all unknown bits are 1
|
||||
|
||||
// TODO: Check: Is the performance too worse? It is O(k^2)
|
||||
for (unsigned i = 0; i < m.num_tbits(); i++) {
|
||||
for (unsigned x = 0, y = i; x <= i; x++, y--) {
|
||||
tbit bit1 = in_out[x];
|
||||
tbit bit2 = in2[y];
|
||||
|
||||
if (bit1 == BIT_1 && bit2 == BIT_1) {
|
||||
min_bit_value++; // we get two 1
|
||||
max_bit_value++;
|
||||
}
|
||||
else if (bit1 != BIT_0 && bit2 != BIT_0) {
|
||||
max_bit_value++; // we could get two 1
|
||||
}
|
||||
}
|
||||
if (min_bit_value == max_bit_value) {
|
||||
// We know the value of this bit
|
||||
// As we might access in_out in some later iteration again we first write to aux-list
|
||||
m_aux_values[i] = min_bit_value & 1 ? BIT_1 : BIT_0;
|
||||
}
|
||||
else {
|
||||
m_aux_values[i] = BIT_z;
|
||||
}
|
||||
// Subtract one; shift this to the next higher bit as "carry value"
|
||||
min_bit_value >>= 1;
|
||||
max_bit_value >>= 1;
|
||||
}
|
||||
|
||||
// Copy aux to result tbv
|
||||
for (unsigned i = 0; i < m.num_tbits(); i++) {
|
||||
m.set(*in_out, i, (tbit)m_aux_values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// similar to multiplying
|
||||
void fixed_bits::add(tbv_ref& in_out, const tbv_ref& in2) {
|
||||
auto m= in_out.manager();
|
||||
|
||||
unsigned min_bit_value = 0;
|
||||
unsigned max_bit_value = 0;
|
||||
|
||||
for (unsigned i = 0; i < m.num_tbits(); i++) {
|
||||
tbit bit1 = in_out[i];
|
||||
tbit bit2 = in2[i];
|
||||
if (bit1 == BIT_1 && bit2 == BIT_1) {
|
||||
min_bit_value++;
|
||||
max_bit_value++;
|
||||
}
|
||||
else if (bit1 != BIT_0 && bit2 != BIT_0) {
|
||||
max_bit_value++;
|
||||
}
|
||||
|
||||
if (min_bit_value == max_bit_value)
|
||||
// for addition we don't need previous values so we can directly write to the output variable
|
||||
m.set(*in_out, i, min_bit_value & 1 ? BIT_1 : BIT_0);
|
||||
else
|
||||
m.set(*in_out, i, BIT_z);
|
||||
|
||||
min_bit_value >>= 1;
|
||||
max_bit_value >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
tbv_ref fixed_bits::eval(const pdd& p) {
|
||||
tbv_manager m = get_manager(p);
|
||||
unsigned sz = m.num_tbits();
|
||||
tbv_ref ret = tbv_ref(m, m.allocate(0ull));
|
||||
for (const dd::pdd_monomial& s : p) {
|
||||
SASSERT(!s.coeff.is_zero());
|
||||
tbv_ref sum = tbv_ref(m, m.allocate(s.coeff));
|
||||
for (pvar fac : s.vars) {
|
||||
multiply(sum, get_tbv(fac, sz));
|
||||
}
|
||||
add(ret, sum);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
102
src/math/polysat/fixed_bits.h
Normal file
102
src/math/polysat/fixed_bits.h
Normal file
|
@ -0,0 +1,102 @@
|
|||
/*++
|
||||
Copyright (c) 2022 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
fixed_bits
|
||||
|
||||
Abstract:
|
||||
|
||||
Associates every pdd with the set of already fixed bits and justifications for this
|
||||
|
||||
--*/
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
#include "util/hash.h"
|
||||
#include "util/optional.h"
|
||||
#include "util/tbv.h"
|
||||
|
||||
|
||||
namespace polysat {
|
||||
|
||||
class solver;
|
||||
|
||||
using bit_dependency = vector<std::pair<pdd, unsigned>>;
|
||||
|
||||
struct bit_justication {
|
||||
constraint* m_constraint = nullptr;
|
||||
|
||||
// variables + resp., bit-index
|
||||
// (a variable might occur multiple times if more bits are relevant)
|
||||
bit_dependency m_dependencies;
|
||||
|
||||
public:
|
||||
bit_justication(constraint *pRaint, bit_dependency vector) = default;
|
||||
bit_justication(constraint* c) : m_constraint(c) { }
|
||||
bit_justication(constraint* c, bit_dependency&& dep) : m_constraint(c), m_dependencies(dep) { }
|
||||
};
|
||||
|
||||
class fixed_bits {
|
||||
|
||||
solver& m_solver;
|
||||
|
||||
scoped_ptr_vector<tbv_manager> m_tbv_managers;
|
||||
|
||||
char_vector m_aux_values;
|
||||
|
||||
//using pdd_to_tbv_key = optional<pdd>;
|
||||
//using pdd_to_tbv_eq = default_eq<pdd_to_tbv_key>;
|
||||
//struct pdd_to_tbv_hash {
|
||||
// unsigned operator()(pdd_to_tbv_key const& args) const {
|
||||
// return args ? args->hash() : 0;
|
||||
// }
|
||||
//};
|
||||
//using pdd_to_tbv_map = map<pdd_to_tbv_key, tbv_ref, pdd_to_tbv_hash, pdd_to_tbv_eq>;
|
||||
|
||||
using tbv_to_justification_key = std::pair<tbv*, unsigned>;
|
||||
using tbv_to_justification_eq = default_eq<tbv_to_justification_key>;
|
||||
struct tbv_to_justification_hash {
|
||||
unsigned operator()(tbv_to_justification_key const& args) const {
|
||||
return combine_hash((unsigned)args.first, args.second);
|
||||
}
|
||||
};
|
||||
using tbv_to_justification_map = map<tbv_to_justification_key, bit_justication, tbv_to_justification_hash, tbv_to_justification_eq>;
|
||||
|
||||
vector<optional<tbv_ref>> m_var_to_tbv;
|
||||
tbv_to_justification_map m_tbv_to_justification;
|
||||
|
||||
tbv_manager& get_manager(const pdd& v);
|
||||
tbv_manager& get_manager(unsigned sz);
|
||||
|
||||
void add(tbv_ref& in_out, const tbv_ref& in2);
|
||||
void multiply(tbv_ref& in_out, const tbv_ref& in2);
|
||||
|
||||
tbv_ref& get_tbv(pvar v, unsigned sz);
|
||||
tbv_ref& get_tbv(const pdd& p);
|
||||
|
||||
public:
|
||||
|
||||
fixed_bits(solver& s) : m_solver(s) {}
|
||||
|
||||
// #count [min; max]
|
||||
static std::pair<unsigned, unsigned> leading_zeros(const tbv_ref& ref);
|
||||
static std::pair<unsigned, unsigned> trailing_zeros(const tbv_ref& ref);
|
||||
static std::pair<unsigned, unsigned> leading_ones(const tbv_ref& ref);
|
||||
static std::pair<unsigned, unsigned> trailing_ones(const tbv_ref& ref);
|
||||
static std::pair<rational, rational> min_max(const tbv_ref& ref);
|
||||
|
||||
tbit get_value(const pdd& p, unsigned idx); // More efficient than calling "eval" and accessing the returned tbv elements
|
||||
bool fix_value(solver& s, const pdd& p, unsigned idx, tbit val, constraint* c, bit_dependency& dep);
|
||||
bool fix_value(solver& s, const pdd& p, unsigned idx, tbit val, constraint* c, std::pair<pdd, unsigned> v1, std::pair<pdd, unsigned> v2) {
|
||||
bit_dependency dep(2);
|
||||
dep.push_back(v1);
|
||||
dep.push_back(v2);
|
||||
return fix_value(s, p, idx, val, c, dep);
|
||||
}
|
||||
void clear_value(const pdd& p, unsigned idx);
|
||||
|
||||
tbv_ref eval(const pdd& p);
|
||||
|
||||
};
|
||||
}
|
|
@ -113,6 +113,9 @@ namespace polysat {
|
|||
if (first)
|
||||
activate(s);
|
||||
|
||||
if (!propagate_bits(s, is_positive))
|
||||
return; // conflict
|
||||
|
||||
if (clause_ref lemma = produce_lemma(s, s.assignment()))
|
||||
s.add_clause(*lemma);
|
||||
|
||||
|
@ -120,6 +123,20 @@ namespace polysat {
|
|||
s.set_conflict(signed_constraint(this, is_positive));
|
||||
}
|
||||
|
||||
bool op_constraint::propagate_bits(solver& s, bool is_positive) {
|
||||
switch (m_op) {
|
||||
case code::lshr_op:
|
||||
return propagate_bits_lshr(s, is_positive);
|
||||
case code::shl_op:
|
||||
return propagate_bits_shl(s, is_positive);
|
||||
case code::and_op:
|
||||
return propagate_bits_and(s, is_positive);
|
||||
default:
|
||||
NOT_IMPLEMENTED_YET();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce lemmas that contradict the given assignment.
|
||||
*
|
||||
|
@ -335,6 +352,39 @@ namespace polysat {
|
|||
return l_undef;
|
||||
}
|
||||
|
||||
bool op_constraint::propagate_bits_shl(solver& s, bool is_positive) {
|
||||
tbv_ref p_val = s.m_fixed_bits.eval(m_p);
|
||||
tbv_ref q_val = s.m_fixed_bits.eval(m_q);
|
||||
tbv_ref r_val = s.m_fixed_bits.eval(m_r);
|
||||
unsigned sz = m_p.power_of_2();
|
||||
|
||||
auto [shift_min, shift_max] = s.m_fixed_bits.min_max(q_val);
|
||||
|
||||
unsigned shift_min_u, shift_max_u;
|
||||
|
||||
if (!shift_min.is_unsigned() || shift_min.get_unsigned() > sz)
|
||||
shift_min_u = sz;
|
||||
else
|
||||
shift_min_u = shift_min.get_unsigned();
|
||||
|
||||
if (!shift_max.is_unsigned() || shift_max.get_unsigned() > sz)
|
||||
shift_max_u = sz;
|
||||
else
|
||||
shift_max_u = shift_max.get_unsigned();
|
||||
|
||||
SASSERT(shift_max_u <= sz);
|
||||
SASSERT(shift_min_u <= shift_max_u);
|
||||
|
||||
for (unsigned i = 0; i < shift_min_u; i++) {
|
||||
if (!s.m_fixed_bits.fix_value(s, m_r, i, BIT_0, this, s.))
|
||||
return false;
|
||||
}
|
||||
for (unsigned i = shift_min_u; i < sz; i++) {
|
||||
propagate_bit(s, m_r.var(), i, p_val[i - shift_min_u]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void op_constraint::activate_and(solver& s) {
|
||||
auto x = p(), y = q();
|
||||
if (x.is_val())
|
||||
|
@ -448,6 +498,31 @@ namespace polysat {
|
|||
return l_undef;
|
||||
}
|
||||
|
||||
bool op_constraint::propagate_bits_and(solver& s, bool is_positive){
|
||||
tbv_ref p_val = s.m_fixed_bits.eval(m_p);
|
||||
tbv_ref q_val = s.m_fixed_bits.eval(m_q);
|
||||
tbv_ref r_val = s.m_fixed_bits.eval(m_r);
|
||||
unsigned sz = m_p.power_of_2();
|
||||
|
||||
for (int i = 0; i < sz; i++) {
|
||||
tbit bp = p_val[i];
|
||||
tbit bq = q_val[i];
|
||||
tbit br = r_val[i];
|
||||
|
||||
// TODO: Propagate from the result to the operands. e.g., 110... = xx1... & yyy...
|
||||
// TODO: ==> x = 111..., y = 110...
|
||||
if (bp == BIT_0 || bq == BIT_0) {
|
||||
if (!s.m_fixed_bits.fix_value(s, m_r, i, BIT_0, this, std::pair(m_p, i), std::pair(m_q, i)))
|
||||
return false;
|
||||
}
|
||||
else if (bp == BIT_1 && bq == BIT_1) {
|
||||
if (!s.m_fixed_bits.fix_value(s, m_r, i, BIT_1, this, std::pair(m_p, i), std::pair(m_q, i)))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void op_constraint::add_to_univariate_solver(solver& s, univariate_solver& us, unsigned dep, bool is_positive) const {
|
||||
auto p_coeff = s.subst(p()).get_univariate_coefficients();
|
||||
auto q_coeff = s.subst(q()).get_univariate_coefficients();
|
||||
|
|
|
@ -31,9 +31,9 @@ namespace polysat {
|
|||
friend class constraint_manager;
|
||||
|
||||
code m_op;
|
||||
pdd m_p;
|
||||
pdd m_q;
|
||||
pdd m_r;
|
||||
pdd m_p; // operand1
|
||||
pdd m_q; // operand2
|
||||
pdd m_r; // result
|
||||
|
||||
op_constraint(constraint_manager& m, code c, pdd const& p, pdd const& q, pdd const& r);
|
||||
lbool eval(pdd const& p, pdd const& q, pdd const& r) const;
|
||||
|
@ -41,12 +41,15 @@ namespace polysat {
|
|||
|
||||
clause_ref lemma_lshr(solver& s, assignment const& a);
|
||||
static lbool eval_lshr(pdd const& p, pdd const& q, pdd const& r);
|
||||
bool propagate_bits_lshr(solver& s, bool is_positive);
|
||||
|
||||
clause_ref lemma_shl(solver& s, assignment const& a);
|
||||
static lbool eval_shl(pdd const& p, pdd const& q, pdd const& r);
|
||||
bool propagate_bits_shl(solver& s, bool is_positive);
|
||||
|
||||
clause_ref lemma_and(solver& s, assignment const& a);
|
||||
static lbool eval_and(pdd const& p, pdd const& q, pdd const& r);
|
||||
bool propagate_bits_and(solver& s, bool is_positive);
|
||||
|
||||
std::ostream& display(std::ostream& out, char const* eq) const;
|
||||
|
||||
|
@ -64,6 +67,7 @@ namespace polysat {
|
|||
lbool eval() const override;
|
||||
lbool eval(assignment const& a) const override;
|
||||
void narrow(solver& s, bool is_positive, bool first) override;
|
||||
bool propagate_bits(solver& s, bool is_positive) override;
|
||||
virtual clause_ref produce_lemma(solver& s, assignment const& a, bool is_positive) override;
|
||||
unsigned hash() const override;
|
||||
bool operator==(constraint const& other) const override;
|
||||
|
|
|
@ -24,6 +24,7 @@ Author:
|
|||
#include "math/polysat/constraint.h"
|
||||
#include "math/polysat/constraint_manager.h"
|
||||
#include "math/polysat/clause_builder.h"
|
||||
#include "math/polysat/fixed_bits.h"
|
||||
#include "math/polysat/simplify_clause.h"
|
||||
#include "math/polysat/simplify.h"
|
||||
#include "math/polysat/restart.h"
|
||||
|
@ -146,6 +147,7 @@ namespace polysat {
|
|||
viable m_viable; // viable sets per variable
|
||||
viable_fallback m_viable_fallback; // fallback for viable, using bitblasting over univariate constraints
|
||||
linear_solver m_linear_solver;
|
||||
fixed_bits m_fixed_bits;
|
||||
conflict m_conflict;
|
||||
simplify_clause m_simplify_clause;
|
||||
simplify m_simplify;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue