-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactorings around
process_lookup_direct
(#2209)
This PR refactors a few things: - `process_lookup_direct` no longer has a default implementation. Eventually, we want all machines to implement it, so I figured it would be better to explicitly panic in each machine. - Refactored the implementation of `FixedLookupMachine::process_plookup`, pulling some stuff out into a new `CallerData` struct. This is similar to what @chriseth has done on [`call_jit_from_block`](main...call_jit_from_block), see the comment below. - As a first test, I implemented `process_lookup_direct` for the "large"-field memory machine (and `process_plookup` by wrapping `process_lookup_direct`)
- Loading branch information
1 parent
11c3c70
commit 0180542
Showing
12 changed files
with
239 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use itertools::Itertools; | ||
use powdr_number::FieldElement; | ||
|
||
use crate::witgen::{ | ||
machines::LookupCell, | ||
processor::{Left, OuterQuery}, | ||
EvalError, EvalResult, EvalValue, | ||
}; | ||
|
||
/// A representation of the caller's data. | ||
/// | ||
/// Useful for implementing [Machine::process_plookup] in terms of [Machine::process_lookup_direct]. | ||
pub struct CallerData<'a, 'b, T> { | ||
/// The raw data of the caller. Unknown values should be ignored. | ||
data: Vec<T>, | ||
/// The affine expressions of the caller. | ||
left: &'b Left<'a, T>, | ||
} | ||
|
||
impl<'a, 'b, T: FieldElement> From<&'b OuterQuery<'a, '_, T>> for CallerData<'a, 'b, T> { | ||
/// Builds a `CallerData` from an `OuterQuery`. | ||
fn from(outer_query: &'b OuterQuery<'a, '_, T>) -> Self { | ||
let data = outer_query | ||
.left | ||
.iter() | ||
.map(|l| l.constant_value().unwrap_or_default()) | ||
.collect(); | ||
Self { | ||
data, | ||
left: &outer_query.left, | ||
} | ||
} | ||
} | ||
|
||
impl<'a, 'b, T: FieldElement> CallerData<'a, 'b, T> { | ||
/// Returns the data as a list of `LookupCell`s, as expected by `Machine::process_lookup_direct`. | ||
pub fn as_lookup_cells(&mut self) -> Vec<LookupCell<'_, T>> { | ||
self.data | ||
.iter_mut() | ||
.zip_eq(self.left.iter()) | ||
.map(|(value, left)| match left.constant_value().is_some() { | ||
true => LookupCell::Input(value), | ||
false => LookupCell::Output(value), | ||
}) | ||
.collect() | ||
} | ||
} | ||
|
||
impl<'a, 'b, T: FieldElement> From<CallerData<'a, 'b, T>> for EvalResult<'a, T> { | ||
/// Turns the result of a direct lookup into an `EvalResult`, as used by `Machine::process_plookup`. | ||
/// | ||
/// Note that this function assumes that the lookup was successful and complete. | ||
fn from(data: CallerData<'a, 'b, T>) -> EvalResult<'a, T> { | ||
let mut result = EvalValue::complete(vec![]); | ||
for (l, v) in data.left.iter().zip_eq(data.data.iter()) { | ||
if !l.is_constant() { | ||
let evaluated = l.clone() - (*v).into(); | ||
match evaluated.solve() { | ||
Ok(constraints) => { | ||
result.combine(constraints); | ||
} | ||
Err(_) => { | ||
// Fail the whole lookup | ||
return Err(EvalError::ConstraintUnsatisfiable(format!( | ||
"Constraint is invalid ({l} != {v}).", | ||
))); | ||
} | ||
} | ||
} | ||
} | ||
Ok(result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod caller_data; | ||
pub mod column_map; | ||
pub mod copy_constraints; | ||
pub mod finalizable_data; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.