Skip to content

Commit

Permalink
Simplify outer query (#2467)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth authored Feb 11, 2025
1 parent 4683e58 commit 40318d5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 41 deletions.
25 changes: 20 additions & 5 deletions executor/src/witgen/data_structures/caller_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use itertools::Itertools;
use powdr_number::FieldElement;

use crate::witgen::{
global_constraints::RangeConstraintSet,
machines::LookupCell,
processor::{Arguments, OuterQuery},
AlgebraicVariable, EvalError, EvalResult, EvalValue,
global_constraints::RangeConstraintSet, machines::LookupCell, processor::OuterQuery,
AffineExpression, AlgebraicVariable, EvalError, EvalResult, EvalValue,
};

/// A representation of the caller's data.
Expand All @@ -15,7 +13,7 @@ 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.
arguments: &'b Arguments<'a, T>,
arguments: &'b [AffineExpression<AlgebraicVariable<'a>, T>],
/// Range constraints coming from the caller.
range_constraints: &'b dyn RangeConstraintSet<AlgebraicVariable<'a>, T>,
}
Expand All @@ -36,6 +34,23 @@ impl<'a, 'b, T: FieldElement> From<&'b OuterQuery<'a, '_, T>> for CallerData<'a,
}
}

impl<'a, 'b, T: FieldElement> CallerData<'a, 'b, T> {
pub fn new(
arguments: &'b [AffineExpression<AlgebraicVariable<'a>, T>],
range_constraints: &'b dyn RangeConstraintSet<AlgebraicVariable<'a>, T>,
) -> Self {
let data = arguments
.iter()
.map(|l| l.constant_value().unwrap_or_default())
.collect();
Self {
data,
arguments,
range_constraints,
}
}
}

impl<T: FieldElement> CallerData<'_, '_, 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>> {
Expand Down
33 changes: 13 additions & 20 deletions executor/src/witgen/machines/block_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,19 +433,10 @@ impl<'a, T: FieldElement> BlockMachine<'a, T> {
arguments: &[AffineExpression<AlgebraicVariable<'a>, T>],
range_constraints: &dyn RangeConstraintSet<AlgebraicVariable<'a>, T>,
) -> EvalResult<'a, T> {
let outer_query = match OuterQuery::try_new(
arguments,
range_constraints,
self.parts.connections[&identity_id],
) {
Ok(outer_query) => outer_query,
Err(incomplete_cause) => return Ok(EvalValue::incomplete(incomplete_cause)),
};

log::trace!("Start processing block machine '{}'", self.name());
log::trace!("Left values of lookup:");
if log::log_enabled!(log::Level::Trace) {
for l in &outer_query.arguments {
for l in arguments {
log::trace!(" {}", l);
}
}
Expand All @@ -454,11 +445,7 @@ impl<'a, T: FieldElement> BlockMachine<'a, T> {
return Err(EvalError::RowsExhausted(self.name.clone()));
}

let known_inputs = outer_query
.arguments
.iter()
.map(|e| e.is_constant())
.collect();
let known_inputs = arguments.iter().map(|e| e.is_constant()).collect();
let operation_id = self.find_operation_id(identity_id).and_then(|index| {
let v = arguments[index].constant_value()?;
Some((index, v))
Expand All @@ -468,12 +455,19 @@ impl<'a, T: FieldElement> BlockMachine<'a, T> {
.compile_cached(mutable_state, identity_id, &known_inputs, operation_id)
.is_some()
{
let updates = self.process_lookup_via_jit(mutable_state, identity_id, outer_query)?;
let caller_data = CallerData::new(arguments, range_constraints);
let updates = self.process_lookup_via_jit(mutable_state, identity_id, caller_data)?;
assert!(updates.is_complete());
self.block_count_jit += 1;
return Ok(updates);
}

let outer_query = OuterQuery::new(
arguments,
range_constraints,
self.parts.connections[&identity_id],
);

// TODO this assumes we are always using the same lookup for this machine.
let mut sequence_iterator = self
.processing_sequence_cache
Expand Down Expand Up @@ -528,16 +522,15 @@ impl<'a, T: FieldElement> BlockMachine<'a, T> {
&mut self,
mutable_state: &MutableState<'a, T, Q>,
identity_id: u64,
outer_query: OuterQuery<'a, 'b, T>,
mut caller_data: CallerData<'a, 'b, T>,
) -> EvalResult<'a, T> {
assert!(
(self.rows() + self.block_size as DegreeType) <= self.degree,
"Block machine is full (this should have been checked before)"
);
self.data.finalize_all();

let mut values = CallerData::from(&outer_query);
let mut lookup_cells = values.as_lookup_cells();
let mut lookup_cells = caller_data.as_lookup_cells();
let operation_id =
self.find_operation_id(identity_id)
.and_then(|index| match &lookup_cells[index] {
Expand All @@ -555,7 +548,7 @@ impl<'a, T: FieldElement> BlockMachine<'a, T> {
)?;
assert!(success);

values.into()
caller_data.into()
}

fn find_operation_id(&self, identity_id: u64) -> Option<usize> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,7 @@ impl<'a, T: FieldElement> Machine<'a, T> for DoubleSortedWitnesses32<'a, T> {
range_constraints: &dyn RangeConstraintSet<AlgebraicVariable<'a>, T>,
) -> EvalResult<'a, T> {
let connection = self.parts.connections[&identity_id];
let outer_query = match OuterQuery::try_new(arguments, range_constraints, connection) {
Ok(outer_query) => outer_query,
Err(incomplete_cause) => return Ok(EvalValue::incomplete(incomplete_cause)),
};
let outer_query = OuterQuery::new(arguments, range_constraints, connection);
let mut data = CallerData::from(&outer_query);
if self.process_lookup_direct(mutable_state, identity_id, &mut data.as_lookup_cells())? {
Ok(EvalResult::from(data)?.report_side_effect())
Expand Down
5 changes: 1 addition & 4 deletions executor/src/witgen/machines/dynamic_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ impl<'a, T: FieldElement> Machine<'a, T> for DynamicMachine<'a, T> {
range_constraints: &dyn RangeConstraintSet<AlgebraicVariable<'a>, T>,
) -> EvalResult<'a, T> {
let identity = *self.parts.connections.get(&identity_id).unwrap();
let outer_query = match OuterQuery::try_new(arguments, range_constraints, identity) {
Ok(outer_query) => outer_query,
Err(incomplete_cause) => return Ok(EvalValue::incomplete(incomplete_cause)),
};
let outer_query = OuterQuery::new(arguments, range_constraints, identity);

log::trace!("Start processing secondary VM '{}'", self.name());
log::trace!("Arguments:");
Expand Down
5 changes: 1 addition & 4 deletions executor/src/witgen/machines/fixed_lookup_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,7 @@ impl<'a, T: FieldElement> Machine<'a, T> for FixedLookup<'a, T> {
) -> EvalResult<'a, T> {
let identity = self.connections[&identity_id];

let outer_query = match OuterQuery::try_new(arguments, range_constraints, identity) {
Ok(outer_query) => outer_query,
Err(incomplete_cause) => return Ok(EvalValue::incomplete(incomplete_cause)),
};
let outer_query = OuterQuery::new(arguments, range_constraints, identity);
self.process_plookup_internal(mutable_state, identity_id, outer_query)
}

Expand Down
8 changes: 4 additions & 4 deletions executor/src/witgen/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ pub struct OuterQuery<'a, 'b, T: FieldElement> {
}

impl<'a, 'b, T: FieldElement> OuterQuery<'a, 'b, T> {
pub fn try_new(
pub fn new(
arguments: &'b [AffineExpression<AlgebraicVariable<'a>, T>],
range_constraints: &'b dyn RangeConstraintSet<AlgebraicVariable<'a>, T>,
connection: Connection<'a, T>,
) -> Result<Self, IncompleteCause<AlgebraicVariable<'a>>> {
Ok(Self {
) -> Self {
Self {
range_constraints,
connection,
arguments: arguments.to_vec(),
})
}
}

pub fn is_complete(&self) -> bool {
Expand Down

0 comments on commit 40318d5

Please sign in to comment.