Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement and improve simplification logic #33

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/clarirs_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ ahash = "0.8.11"
anyhow = "1.0.86"
clarirs_num = { path = "../clarirs_num" }
num-bigint = { version = "0.4.6", features = ["serde"] }
num-traits = "0.2"
paste = "1.0.15"
petgraph = "0.6.5"
rand = { version = "0.8.5", features = [ "small_rng"] }
rand = { version = "0.8.5", features = ["small_rng"] }
serde = { version = "1.0.209", features = ["derive", "rc"] }
smallvec = { version = "1.13.2", features = ["serde"] }
thiserror = "1.0.63"
Expand Down
1,075 changes: 978 additions & 97 deletions crates/clarirs_core/src/algorithms/simplify.rs

Large diffs are not rendered by default.

66 changes: 34 additions & 32 deletions crates/clarirs_core/src/algorithms/tests/test_bv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
];

for (a, b, expected) in table {
let a = ctx.bvv_prim(a).unwrap();

Check warning on line 22 in crates/clarirs_core/src/algorithms/tests/test_bv.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/clarirs/clarirs/crates/clarirs_core/src/algorithms/tests/test_bv.rs
let b = ctx.bvv_prim(b).unwrap();
let expected = ctx.bvv_prim(expected).unwrap();

let result = ctx.add(&a, &b)?.simplify()?;

let add_result = ctx.add(&a, &b)?;
let result = add_result.simplify()?;

assert_eq!(result, expected);
}

Expand Down Expand Up @@ -340,36 +342,36 @@
Ok(())
}

#[test]
fn test_shl() -> Result<()> {
let ctx = Context::new();

let table: Vec<(u64, u64, u64)> = vec![
(0, 0, 0),
(0, 1, 0),
(1, 0, 1),
(1, 1, 2),
(1, 2, 4),
(2, 1, 4),
(2, 2, 8),
(2, 3, 16),
(3, 2, 12),
(3, 3, 24),
(u64::MAX, 1, u64::MAX),
(u64::MAX, 2, u64::MAX),
];

for (a, b, expected) in table {
let a = ctx.bvv_prim(a).unwrap();
let b = ctx.bvv_prim(b).unwrap();
let expected = ctx.bvv_prim(expected).unwrap();

let result = ctx.shl(&a, &b)?.simplify()?;
assert_eq!(result, expected);
}

Ok(())
}
// #[test]
// fn test_shl() -> Result<()> {
// let ctx = Context::new();

// let table: Vec<(u64, u64, u64)> = vec![
// (0, 0, 0),
// (0, 1, 0),
// (1, 0, 1),
// (1, 1, 2),
// (1, 2, 4),
// (2, 1, 4),
// (2, 2, 8),
// (2, 3, 16),
// (3, 2, 12),
// (3, 3, 24),
// (u64::MAX, 1, u64::MAX),
// (u64::MAX, 2, u64::MAX),
// ];

// for (a, b, expected) in table {
// let a = ctx.bvv_prim(a).unwrap();
// let b = ctx.bvv_prim(b).unwrap();
// let expected = ctx.bvv_prim(expected).unwrap();

// let result = ctx.shl(&a, &b)?.simplify()?;
// assert_eq!(result, expected);
// }

// Ok(())
// }

#[test]
fn test_lshr() -> Result<()> {
Expand Down
209 changes: 163 additions & 46 deletions crates/clarirs_core/src/ast/astcache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::{RwLock, Weak};
use std::sync::{Arc, RwLock, Weak};

use ahash::HashMap;

Expand Down Expand Up @@ -69,70 +69,187 @@
#[derive(Debug, Default)]
pub struct AstCache<'c> {
inner: RwLock<HashMap<u64, AstCacheValue<'c>>>,
}

Check warning on line 72 in crates/clarirs_core/src/ast/astcache.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/clarirs/clarirs/crates/clarirs_core/src/ast/astcache.rs

impl<'c> AstCache<'c> {
pub fn get_or_insert_with_bool<F: FnOnce() -> BoolAst<'c>>(
&self,
hash: u64,
f: F,
) -> BoolAst<'c> {

pub fn get_or_insert_with_bool<F>(&self, hash: u64, f: F) -> Result<BoolAst<'c>, ClarirsError>
where
F: FnOnce() -> Result<BoolAst<'c>, ClarirsError>,
{
let mut inner = self.inner.write().unwrap();
match inner.get(&hash).and_then(|v| v.as_bool()) {
Some(value) => value,
None => {
let this = f();
inner.insert(hash, this.clone().into());
this
let entry = inner
.entry(hash)
.or_insert_with(|| AstCacheValue::Boolean(Weak::new()));
match entry {
AstCacheValue::Boolean(weak) => {
if let Some(arc) = weak.upgrade() {
Ok(arc)
} else {
let arc = f()?;
*entry = AstCacheValue::Boolean(Arc::downgrade(&arc));
Ok(arc)
}
}
_ => unreachable!(),

Check warning on line 94 in crates/clarirs_core/src/ast/astcache.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/clarirs/clarirs/crates/clarirs_core/src/ast/astcache.rs
}
}

pub fn get_or_insert_with_bv<F: FnOnce() -> BitVecAst<'c>>(
&self,
hash: u64,
f: F,
) -> BitVecAst<'c> {

pub fn get_or_insert_with_bv<F>(&self, hash: u64, f: F) -> Result<BitVecAst<'c>, ClarirsError>
where
F: FnOnce() -> Result<BitVecAst<'c>, ClarirsError>,
{
let mut inner = self.inner.write().unwrap();
match inner.get(&hash).and_then(|v| v.as_bv()) {
Some(value) => value,
None => {
let this = f();
inner.insert(hash, this.clone().into());
this
let entry = inner
.entry(hash)
.or_insert_with(|| AstCacheValue::BitVec(Weak::new()));
match entry {
AstCacheValue::BitVec(weak) => {
if let Some(arc) = weak.upgrade() {
Ok(arc)
} else {
let arc = f()?;
*entry = AstCacheValue::BitVec(Arc::downgrade(&arc));
Ok(arc)
}
}
_ => unreachable!(),
}
}

pub fn get_or_insert_with_float<F: FnOnce() -> FloatAst<'c>>(
&self,
hash: u64,
f: F,
) -> FloatAst<'c> {
pub fn get_or_insert_with_float<F>(&self, hash: u64, f: F) -> Result<FloatAst<'c>, ClarirsError>
where
F: FnOnce() -> Result<FloatAst<'c>, ClarirsError>,
{
let mut inner = self.inner.write().unwrap();
match inner.get(&hash).and_then(|v| v.as_float()) {
Some(value) => value,
None => {
let this = f();
inner.insert(hash, this.clone().into());
this
let entry = inner
.entry(hash)
.or_insert_with(|| AstCacheValue::Float(Weak::new()));
match entry {
AstCacheValue::Float(weak) => {
if let Some(arc) = weak.upgrade() {
Ok(arc)
} else {
let arc = f()?;
*entry = AstCacheValue::Float(Arc::downgrade(&arc));
Ok(arc)
}
}
_ => unreachable!(),
}

Check warning on line 139 in crates/clarirs_core/src/ast/astcache.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/clarirs/clarirs/crates/clarirs_core/src/ast/astcache.rs
}

pub fn get_or_insert_with_string<F: FnOnce() -> StringAst<'c>>(
&self,
hash: u64,
f: F,
) -> StringAst<'c> {
pub fn get_or_insert_with_string<F>(&self, hash: u64, f: F) -> Result<StringAst<'c>, ClarirsError>
where
F: FnOnce() -> Result<StringAst<'c>, ClarirsError>,
{
let mut inner = self.inner.write().unwrap();
match inner.get(&hash).and_then(|v| v.as_string()) {
Some(value) => value,
None => {
let this = f();
inner.insert(hash, this.clone().into());
this
let entry = inner
.entry(hash)
.or_insert_with(|| AstCacheValue::String(Weak::new()));
match entry {
AstCacheValue::String(weak) => {
if let Some(arc) = weak.upgrade() {
Ok(arc)
} else {
let arc = f()?;
*entry = AstCacheValue::String(Arc::downgrade(&arc));
Ok(arc)
}
}
_ => unreachable!(),
}
}

// pub fn get_or_insert_with_bool<F: FnOnce() -> BoolAst<'c>>(
// &self,
// hash: u64,
// f: F,
// ) -> BoolAst<'c> {
// let mut inner = self.inner.write().unwrap();
// let entry = inner
// .entry(hash)
// .or_insert_with(|| AstCacheValue::Boolean(Weak::new()));
// match entry {
// AstCacheValue::Boolean(weak) => {
// if let Some(arc) = weak.upgrade() {
// arc
// } else {
// let arc = f();
// *entry = AstCacheValue::Boolean(Arc::downgrade(&arc));
// arc
// }
// }
// _ => unreachable!(),
// }
// }

// pub fn get_or_insert_with_bv<F: FnOnce() -> BitVecAst<'c>>(
// &self,
// hash: u64,
// f: F,
// ) -> BitVecAst<'c> {
// let mut inner = self.inner.write().unwrap();
// let entry = inner
// .entry(hash)
// .or_insert_with(|| AstCacheValue::BitVec(Weak::new()));
// match entry {
// AstCacheValue::BitVec(weak) => {
// if let Some(arc) = weak.upgrade() {
// arc
// } else {
// let arc = f();
// *entry = AstCacheValue::BitVec(Arc::downgrade(&arc));
// arc
// }
// }
// _ => unreachable!(),
// }
// }

// pub fn get_or_insert_with_float<F: FnOnce() -> FloatAst<'c>>(
// &self,
// hash: u64,
// f: F,
// ) -> FloatAst<'c> {
// let mut inner = self.inner.write().unwrap();
// let entry = inner
// .entry(hash)
// .or_insert_with(|| AstCacheValue::Float(Weak::new()));
// match entry {
// AstCacheValue::Float(weak) => {
// if let Some(arc) = weak.upgrade() {
// arc
// } else {
// let arc = f();
// *entry = AstCacheValue::Float(Arc::downgrade(&arc));
// arc
// }
// }
// _ => unreachable!(),
// }
// }

// pub fn get_or_insert_with_string<F: FnOnce() -> StringAst<'c>>(
// &self,
// hash: u64,
// f: F,
// ) -> StringAst<'c> {
// let mut inner = self.inner.write().unwrap();
// let entry = inner
// .entry(hash)
// .or_insert_with(|| AstCacheValue::String(Weak::new()));
// match entry {
// AstCacheValue::String(weak) => {
// if let Some(arc) = weak.upgrade() {
// arc
// } else {
// let arc = f();
// *entry = AstCacheValue::String(Arc::downgrade(&arc));
// arc
// }
// }
// _ => unreachable!(),
// }
// }
}
12 changes: 9 additions & 3 deletions crates/clarirs_core/src/ast/bitvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ pub enum BitVecOp<'c> {
URem(BitVecAst<'c>, BitVecAst<'c>),
SRem(BitVecAst<'c>, BitVecAst<'c>),
Pow(BitVecAst<'c>, BitVecAst<'c>),
ShL(BitVecAst<'c>, BitVecAst<'c>),
// ShL(BitVecAst<'c>, BitVecAst<'c>),
LShL(BitVecAst<'c>, BitVecAst<'c>),
LShR(BitVecAst<'c>, BitVecAst<'c>),
AShL(BitVecAst<'c>, BitVecAst<'c>),
AShR(BitVecAst<'c>, BitVecAst<'c>),
RotateLeft(BitVecAst<'c>, BitVecAst<'c>),
RotateRight(BitVecAst<'c>, BitVecAst<'c>),
Expand Down Expand Up @@ -75,8 +77,10 @@ impl<'c> Op<'c> for BitVecOp<'c> {
| BitVecOp::URem(a, b)
| BitVecOp::SRem(a, b)
| BitVecOp::Pow(a, b)
| BitVecOp::ShL(a, b)
// | BitVecOp::ShL(a, b)
| BitVecOp::LShL(a, b)
| BitVecOp::LShR(a, b)
| BitVecOp::AShL(a, b)
| BitVecOp::AShR(a, b)
| BitVecOp::RotateLeft(a, b)
| BitVecOp::RotateRight(a, b)
Expand Down Expand Up @@ -141,8 +145,10 @@ impl<'c> BitVecExt<'c> for BitVecAst<'c> {
| BitVecOp::URem(a, _)
| BitVecOp::SRem(a, _)
| BitVecOp::Pow(a, _)
| BitVecOp::ShL(a, _)
// | BitVecOp::ShL(a, _)
| BitVecOp::LShL(a, _)
| BitVecOp::LShR(a, _)
| BitVecOp::AShL(a, _)
| BitVecOp::AShR(a, _)
| BitVecOp::RotateLeft(a, _)
| BitVecOp::RotateRight(a, _) => a.size(),
Expand Down
Loading
Loading