Skip to content

Commit 9c7e2d2

Browse files
committed
integrate sca into the simplify tool
1 parent e6c9d44 commit 9c7e2d2

File tree

6 files changed

+45
-9
lines changed

6 files changed

+45
-9
lines changed

patronus-sca/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,12 @@ pub struct ScaEqualityProblem {
387387
word_level: ExprRef,
388388
}
389389

390+
impl ScaEqualityProblem {
391+
pub fn equality_expr(&self) -> ExprRef {
392+
self.equality
393+
}
394+
}
395+
390396
pub fn find_sca_simplification_candidates(ctx: &Context, e: ExprRef) -> Vec<ScaEqualityProblem> {
391397
let mut problems = vec![];
392398
let _ = traversal::bottom_up(ctx, e, |ctx, e, children: &[AnalysisResult]| {

patronus/src/smt/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ pub fn parse_command(ctx: &mut Context, st: &SymbolTable, input: &[u8]) -> Resul
358358
Some(Token::Value(name)) => match name {
359359
b"exit" => SmtCommand::Exit,
360360
b"check-sat" => SmtCommand::CheckSat,
361+
b"reset" => SmtCommand::Reset,
361362
b"set-logic" => {
362363
let logic = parse_logic(&mut lexer)?;
363364
SmtCommand::SetLogic(logic)

patronus/src/smt/serialize.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ pub fn serialize_cmd(out: &mut impl Write, ctx: Option<&Context>, cmd: &SmtComma
290290
match cmd {
291291
SmtCommand::Exit => writeln!(out, "(exit)"),
292292
SmtCommand::CheckSat => writeln!(out, "(check-sat)"),
293+
SmtCommand::Reset => writeln!(out, "(reset)"),
293294
SmtCommand::SetLogic(logic) => writeln!(out, "(set-logic {})", logic.to_smt_str()),
294295
SmtCommand::SetOption(name, value) => {
295296
writeln!(out, "(set-option :{name} {})", escape_smt_identifier(value))

patronus/src/smt/solver.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ pub enum SmtCommand {
6565
Push(u64),
6666
Pop(u64),
6767
GetValue(ExprRef),
68+
/// Resets the solver to the startup state
69+
Reset,
6870
}
6971

7072
/// The result of a `(check-sat)` command.

tools/simplify/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ rust-version.workspace = true
1212
patronus.workspace = true
1313
clap.workspace = true
1414
rustc-hash.workspace = true
15+
patronus-sca = {path="../../patronus-sca"}

tools/simplify/src/main.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use clap::Parser;
66
use patronus::expr::*;
77
use patronus::smt::{SmtCommand, read_command, serialize_cmd};
8+
use patronus_sca::*;
89
use rustc_hash::FxHashMap;
910
use std::io::{BufReader, BufWriter};
1011
use std::path::PathBuf;
@@ -17,6 +18,8 @@ use std::path::PathBuf;
1718
struct Args {
1819
#[arg(long)]
1920
do_not_simplify: bool,
21+
#[arg(long)]
22+
skip_sca: bool,
2023
#[arg(value_name = "INPUT", index = 1)]
2124
input_file: PathBuf,
2225
#[arg(value_name = "OUTPUT", index = 2)]
@@ -45,22 +48,44 @@ fn main() {
4548
let cmd = if args.do_not_simplify {
4649
cmd
4750
} else {
48-
simplify(&mut ctx, &mut simplifier, cmd)
51+
simplify_cmd(cmd, |e| {
52+
let e_after_sca = if args.skip_sca {
53+
e
54+
} else {
55+
let p = find_sca_simplification_candidates(&ctx, e);
56+
let subs: FxHashMap<_, _> = p
57+
.into_iter()
58+
.flat_map(|p| match verify_word_level_equality(&mut ctx, p) {
59+
ScaVerifyResult::Equal => Some((p.equality_expr(), ctx.get_true())),
60+
ScaVerifyResult::Unknown => None,
61+
ScaVerifyResult::Unequal(_) => {
62+
Some((p.equality_expr(), ctx.get_false()))
63+
}
64+
})
65+
.collect();
66+
substitute(&mut ctx, e, subs)
67+
};
68+
simplifier.simplify(&mut ctx, e_after_sca)
69+
})
4970
};
5071
serialize_cmd(&mut out, Some(&ctx), &cmd).expect("failed to write command");
5172
}
5273
}
5374

54-
fn simplify<T: ExprMap<Option<ExprRef>>>(
55-
ctx: &mut Context,
56-
s: &mut Simplifier<T>,
57-
cmd: SmtCommand,
58-
) -> SmtCommand {
75+
fn substitute(ctx: &mut Context, e: ExprRef, subs: FxHashMap<ExprRef, ExprRef>) -> ExprRef {
76+
if subs.is_empty() {
77+
e
78+
} else {
79+
simple_transform_expr(ctx, e, |_, e, _| subs.get(&e).cloned())
80+
}
81+
}
82+
83+
fn simplify_cmd(cmd: SmtCommand, mut simplify: impl FnMut(ExprRef) -> ExprRef) -> SmtCommand {
5984
match cmd {
60-
SmtCommand::Assert(e) => SmtCommand::Assert(s.simplify(ctx, e)),
61-
SmtCommand::DefineConst(sym, value) => SmtCommand::DefineConst(sym, s.simplify(ctx, value)),
85+
SmtCommand::Assert(e) => SmtCommand::Assert(simplify(e)),
86+
SmtCommand::DefineConst(sym, value) => SmtCommand::DefineConst(sym, simplify(value)),
6287
SmtCommand::CheckSatAssuming(e) => {
63-
SmtCommand::CheckSatAssuming(e.into_iter().map(|e| s.simplify(ctx, e)).collect())
88+
SmtCommand::CheckSatAssuming(e.into_iter().map(|e| simplify(e)).collect())
6489
}
6590
other => other,
6691
}

0 commit comments

Comments
 (0)