Skip to content

Commit

Permalink
make max_bv_sharing a simplifier
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolajBjorner committed Nov 25, 2022
1 parent db74e23 commit 5af6e1a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 74 deletions.
1 change: 1 addition & 0 deletions src/ast/simplifiers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ z3_add_component(simplifiers
eliminate_predicates.cpp
euf_completion.cpp
extract_eqs.cpp
max_bv_sharing.cpp
model_reconstruction_trail.cpp
propagate_values.cpp
solve_context_eqs.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Copyright (c) 2011 Microsoft Corporation

Module Name:

max_bv_sharing_tactic.cpp
max_bv_sharing.cpp

Abstract:

Expand All @@ -12,7 +12,7 @@ Module Name:
This rewriter is particularly useful for reducing
the number of Adders and Multipliers before "bit-blasting".

Author:
Author

Leonardo de Moura (leonardo) 2011-12-29.

Expand All @@ -23,9 +23,10 @@ Revision History:
#include "ast/bv_decl_plugin.h"
#include "ast/rewriter/rewriter_def.h"
#include "util/obj_pair_hashtable.h"
#include "ast/simplifiers/dependent_expr_state.h"
#include "ast/ast_lt.h"

class max_bv_sharing_tactic : public tactic {
class max_bv_sharing : public dependent_expr_simplifier {

struct rw_cfg : public default_rewriter_cfg {
typedef std::pair<expr *, expr *> expr_pair;
Expand Down Expand Up @@ -224,64 +225,22 @@ class max_bv_sharing_tactic : public tactic {
}
};

struct imp {
rw m_rw;
unsigned m_num_steps;
rw m_rw;
unsigned m_num_steps = 0;

imp(ast_manager & m, params_ref const & p):
m_rw(m, p) {
}

ast_manager & m() const { return m_rw.m(); }

void operator()(goal_ref const & g,
goal_ref_buffer & result) {
tactic_report report("max-bv-sharing", *g);
bool produce_proofs = g->proofs_enabled();

expr_ref new_curr(m());
proof_ref new_pr(m());
unsigned size = g->size();
for (unsigned idx = 0; idx < size; idx++) {
if (g->inconsistent())
break;
expr * curr = g->form(idx);
m_rw(curr, new_curr, new_pr);
m_num_steps += m_rw.get_num_steps();

if (produce_proofs) {
proof * pr = g->pr(idx);
new_pr = m().mk_modus_ponens(pr, new_pr);
}
g->update(idx, new_curr, new_pr, g->dep(idx));
}
m_rw.cfg().cleanup();
g->inc_depth();
result.push_back(g.get());
}
};

imp * m_imp;
params_ref m_params;
public:
max_bv_sharing_tactic(ast_manager & m, params_ref const & p):
m_params(p) {
m_imp = alloc(imp, m, p);
}

tactic * translate(ast_manager & m) override {
return alloc(max_bv_sharing_tactic, m, m_params);
}

~max_bv_sharing_tactic() override {
dealloc(m_imp);
}
params_ref m_params;

char const* name() const override { return "max_bv_sharing"; }
public:
max_bv_sharing(ast_manager & m, params_ref const & p, dependent_expr_state& fmls):
dependent_expr_simplifier(m, fmls),
m_params(p),
m_rw(m, p) {
}

void updt_params(params_ref const & p) override {
m_params.append(p);
m_imp->m_rw.cfg().updt_params(m_params);
m_rw.cfg().updt_params(m_params);
}

void collect_param_descrs(param_descrs & r) override {
Expand All @@ -290,21 +249,22 @@ class max_bv_sharing_tactic : public tactic {
r.insert("max_args", CPK_UINT,
"(default: 128) maximum number of arguments (per application) that will be considered by the greedy (quadratic) heuristic.");
}

void operator()(goal_ref const & in,
goal_ref_buffer & result) override {
(*m_imp)(in, result);
}

void cleanup() override {
ast_manager & m = m_imp->m();
params_ref p = std::move(m_params);
m_imp->~imp();
new (m_imp) imp(m, p);
}

void reduce() override {
expr_ref new_curr(m);
proof_ref new_pr(m);
for (unsigned idx = 0; idx < m_fmls.size() && !m_fmls.inconsistent(); idx++) {
auto [curr, d] = m_fmls[idx]();
m_rw(curr, new_curr, new_pr);
// Proof reconstruction: new_pr = m.mk_modus_ponens(old_pr, new_pr);
m_num_steps += m_rw.get_num_steps();
m_fmls.update(idx, dependent_expr(m, new_curr, d));
}
m_rw.cfg().cleanup();
}
};

tactic * mk_max_bv_sharing_tactic(ast_manager & m, params_ref const & p) {
return clean(alloc(max_bv_sharing_tactic, m, p));
dependent_expr_simplifier * mk_max_bv_sharing(ast_manager & m, params_ref const & p, dependent_expr_state& fmls) {
return alloc(max_bv_sharing, m, p, fmls);
}

25 changes: 25 additions & 0 deletions src/ast/simplifiers/max_bv_sharing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*++
Copyright (c) 2022 Microsoft Corporation
Module Name:
max_bv_sharing.h
Abstract:
Rewriter for "maximing" the number of shared terms.
The idea is to rewrite AC terms to maximize sharing.
This rewriter is particularly useful for reducing
the number of Adders and Multipliers before "bit-blasting".
Author:
Leonardo de Moura (leonardo) 2011-12-29.
--*/

#pragma once

#include "ast/simplifiers/dependent_expr_state.h"

dependent_expr_simplifier * mk_max_bv_sharing(ast_manager & m, params_ref const & p, dependent_expr_state& fmls);
1 change: 0 additions & 1 deletion src/tactic/bv/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ z3_add_component(bv_tactics
bv_slice_tactic.cpp
dt2bv_tactic.cpp
elim_small_bv_tactic.cpp
max_bv_sharing_tactic.cpp
COMPONENT_DEPENDENCIES
bit_blaster
core_tactics
Expand Down
17 changes: 13 additions & 4 deletions src/tactic/bv/max_bv_sharing_tactic.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ Revision History:
--*/
#pragma once

#include "util/params.h"
class ast_manager;
class tactic;
#include "ast/simplifiers/max_bv_sharing.h"
#include "tactic/dependent_expr_state_tactic.h"

class max_bv_sharing_tactic_factory : public dependent_expr_simplifier_factory {
public:
dependent_expr_simplifier* mk(ast_manager& m, params_ref const& p, dependent_expr_state& s) override {
return mk_max_bv_sharing(m, p, s);
}
};

inline tactic* mk_max_bv_sharing_tactic(ast_manager& m, params_ref const& p = params_ref()) {
return alloc(dependent_expr_state_tactic, m, p, alloc(max_bv_sharing_tactic_factory), "max-bv-sharing");
}

tactic * mk_max_bv_sharing_tactic(ast_manager & m, params_ref const & p = params_ref());
/*
ADD_TACTIC("max-bv-sharing", "use heuristics to maximize the sharing of bit-vector expressions such as adders and multipliers.", "mk_max_bv_sharing_tactic(m, p)")
*/
Expand Down

0 comments on commit 5af6e1a

Please sign in to comment.