Skip to content

Commit

Permalink
clippy.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Nov 13, 2024
1 parent e801140 commit 1a86dfa
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 28 deletions.
4 changes: 2 additions & 2 deletions executor/src/witgen/auto/eval_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<T: FieldElement> Display for KnownValue<T> {
match self {
KnownValue::Concrete(n) => {
if n.is_in_lower_half() {
write!(f, "{}", n)
write!(f, "{n}")
} else {
write!(f, "-{}", -*n)
}
Expand Down Expand Up @@ -350,7 +350,7 @@ impl<T: FieldElement> EvalResult<T> {
let rhs = if coeff.is_one() {
masked
} else if coeff == -T::from(1) {
format!("-({})", masked)
format!("-({masked})")
} else {
format!("({masked}) / {coeff}")
};
Expand Down
26 changes: 11 additions & 15 deletions executor/src/witgen/auto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ pub fn infer_witgen<'a, T: FieldElement>(
// Let's only look at the first connection. TODO
let conn = parts.connections.values().next().unwrap().right;
for num_known in 1..conn.expressions.len() {
if num_known != 13 {
continue;
}
for inputs_known in conn.expressions.iter().combinations(num_known) {
println!(
"Known inputs: {}",
log::debug!(
"Trying to auto-generate witgen code for known inputs: {}",
inputs_known.iter().map(|e| e.to_string()).join(", ")
);
if inputs_known.len() != 3 {
// TODO remove
continue;
}
let Some(inputs_known) = inputs_known
.iter()
.map(|e| try_to_simple_poly(e))
Expand All @@ -42,15 +41,12 @@ pub fn infer_witgen<'a, T: FieldElement>(

let inputs_known = inputs_known.into_iter().map(|p| p.poly_id);

let mut inference = WitgenInference::new(
fixed_data,
parts,
block_size,
latch_row,
inputs_known,
&conn,
);
inference.run();
let mut inference =
WitgenInference::new(fixed_data, parts, block_size, latch_row, inputs_known, conn);
if inference.run() {
log::info!("Successfully generated witgen code for some machine.");
log::trace!("Generated code:\n{}", inference.code());
}
}
}
Some(())
Expand Down
60 changes: 49 additions & 11 deletions executor/src/witgen/auto/witgen_inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ use powdr_ast::{
LookupIdentity, PhantomLookupIdentity, PolyID, PolynomialIdentity, PolynomialType,
SelectedExpressions,
},
parsed::visitor::{AllChildren, Children},
parsed::visitor::AllChildren,
};
use powdr_number::FieldElement;

use crate::witgen::{
auto::eval_result::KnownValue, global_constraints::RangeConstraintSet, util::try_to_simple_poly,
};
use crate::witgen::{auto::eval_result::KnownValue, global_constraints::RangeConstraintSet};

use super::{
super::{machines::MachineParts, range_constraints::RangeConstraint, FixedData},
Expand Down Expand Up @@ -69,12 +67,14 @@ impl<'a, T: FieldElement> WitgenInference<'a, T> {
}
}

pub fn run(&mut self) {
pub fn run(&mut self) -> bool {
self.process_effects(self.set_unreferenced_cells_to_zero());
while !self.all_cells_known() {
loop {
let code_len = self.code.len();

self.considered_row_range().for_each(|offset| {
// TODO structure that better
if offset < self.block_size as i32 && offset > -(self.block_size as i32) {
if self.minimum_range_around_latch().contains(&offset) {
// Set selector to one on latch row and zero on others.
let value = KnownValue::from(T::from(u32::from(offset == 0))).into();
if let Some(r) = self.evaluate(&self.lookup_rhs.selector, offset) {
Expand All @@ -86,8 +86,46 @@ impl<'a, T: FieldElement> WitgenInference<'a, T> {
}
});
self.force_selector_array_element_zero_if_last_remaining();

// TODO does not consider newly learnt range constraints
if self.code.len() == code_len {
break;
}
}
if self.all_cells_known() {
true
} else {
let unknown_columns = self
.unknown_columns()
.map(|id| PolyID {
id,
ptype: PolynomialType::Committed,
})
.collect::<BTreeSet<_>>();
log::debug!(
"Not all cells known. The following columns still have missing entries: {}",
unknown_columns
.iter()
.map(|poly_id| self.fixed_data.column_name(poly_id))
.join(", ")
);
log::trace!(
"The identities concerned with these columns are:\n{}",
self.parts
.identities
.iter()
.filter(|id| id.all_children().any(|e| match e {
Expression::Reference(r) => unknown_columns.contains(&r.poly_id),
_ => false,
}))
.format("\n")
);
false
}
println!("Successfully generated code: {}", self.code.join("\n"));
}

pub fn code(&self) -> String {
self.code.join("\n")
}

fn cell_at_row(&self, id: u64, row_offset: i32) -> Cell {
Expand Down Expand Up @@ -188,7 +226,7 @@ impl<'a, T: FieldElement> WitgenInference<'a, T> {

for row_offset in self.minimum_range_around_latch() {
let cell = self.cell_at_row(id, row_offset);
self.process_effects((&EvalResult::from_unknown_cell(&cell)).solve(self));
self.process_effects(EvalResult::from_unknown_cell(&cell).solve(self));
}
}

Expand Down Expand Up @@ -283,7 +321,7 @@ impl<'a, T: FieldElement> WitgenInference<'a, T> {
"fixed_lookup_machine.process_lookup_direct(({lookup_id}, vec![{query}]))",
);
// TODO range constraints?
let output_expr = inputs.iter().filter(|i| !i.is_known()).next().unwrap();
let output_expr = inputs.iter().find(|i| !i.is_known()).unwrap();
return once(Effect::Code(var_decl))
.chain(once(Effect::Code(machine_call)))
.chain(
Expand Down Expand Up @@ -349,7 +387,7 @@ impl<'a, T: FieldElement> WitgenInference<'a, T> {
}
}
Expression::PublicReference(_) => return None, // TODO
Expression::Challenge(challenge) => return None, // TODO
Expression::Challenge(_) => return None, // TODO
Expression::Number(n) => EvalResult::from_number(*n),
Expression::BinaryOperation(op) => self.evaulate_binary_operation(op, offset)?,
Expression::UnaryOperation(op) => self.evaluate_unary_operation(op, offset)?,
Expand Down

0 comments on commit 1a86dfa

Please sign in to comment.