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

allow partially const tuples #206

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
48 changes: 34 additions & 14 deletions src/ir/opt/cfold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ fn cbv(b: BitVector) -> Option<Term> {
Some(const_(Value::BitVector(b)))
}

/// Create a tuple from children. If all children are const, then make the whole tuple const
fn new_tuple(children: Vec<Term>) -> Term {
children
.iter()
.map(|c| c.as_value_opt().cloned())
.collect::<Option<_>>()
.map(|v| const_(Value::Tuple(v)))
.unwrap_or_else(|| term(Op::Tuple, children))
}

/// Fold away operators over constants.
pub fn fold(node: &Term, ignore: &[Op]) -> Term {
FOLDS.with(|cache_handle| {
Expand Down Expand Up @@ -353,21 +363,31 @@ pub fn fold_cache(node: &Term, cache: &mut TermCache<TTerm>, ignore: &[Op]) -> T
(Some(arr), Some(idx)) => Some(const_(arr.select(idx))),
_ => None,
},
Op::Tuple => t
.cs()
.iter()
.map(|c| c_get(c).as_value_opt().cloned())
.collect::<Option<_>>()
.map(|v| const_(Value::Tuple(v))),
Op::Field(n) => get(0).as_tuple_opt().map(|t| const_(t[*n].clone())),
Op::Update(n) => match (get(0).as_tuple_opt(), get(1).as_value_opt()) {
(Some(t), Some(v)) => {
let mut new_vec = Vec::from(t).into_boxed_slice();
assert_eq!(new_vec[*n].sort(), v.sort());
new_vec[*n] = v.clone();
Some(const_(Value::Tuple(new_vec)))
Op::Tuple => {
Some(new_tuple(t
.cs()
.iter()
.map(c_get).collect::<Vec<_>>()))
},
Op::Field(n) => {
match get(0).op() {
Op::Tuple => {
let term = get(0).cs()[*n].clone();
Some(term.as_value_opt().cloned().map(const_).unwrap_or(term))
}
_ => None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another simple solution: just match on Op::Const(Value::Tuple(..)) too.

}
},
Op::Update(n) => {
match get(0).op() {
Op::Tuple => {
let mut children = get(0).cs().to_vec();
children[*n] = get(1).clone();
Some(new_tuple(children))

}
_ => None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

}
_ => None,
},
Op::BvConcat => t
.cs()
Expand Down
Loading