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

Various preparatory changes. #2228

Merged
merged 2 commits into from
Dec 12, 2024
Merged
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
38 changes: 27 additions & 11 deletions executor/src/witgen/data_structures/mutable_state.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{
cell::RefCell,
cell::{RefCell, RefMut},
collections::{BTreeMap, HashMap},
};

use powdr_number::FieldElement;

use crate::witgen::{
machines::{KnownMachine, Machine},
machines::{KnownMachine, LookupCell, Machine},
rows::RowPair,
EvalError, EvalResult, QueryCallback,
};
Expand Down Expand Up @@ -52,19 +52,35 @@ impl<'a, T: FieldElement, Q: QueryCallback<T>> MutableState<'a, T, Q> {
/// Call the machine responsible for the right-hand-side of an identity given its ID
/// and the row pair of the caller.
pub fn call(&self, identity_id: u64, caller_rows: &RowPair<'_, 'a, T>) -> EvalResult<'a, T> {
self.responsible_machine(identity_id)?
.process_plookup_timed(self, identity_id, caller_rows)
}

/// Call the machine responsible for the right-hand-side of an identity given its ID,
/// use the direct interface.
#[allow(unused)]
pub fn call_direct(
&self,
identity_id: u64,
values: &mut [LookupCell<'_, T>],
) -> Result<bool, EvalError<T>> {
self.responsible_machine(identity_id)?
.process_lookup_direct_timed(self, identity_id, values)
}

fn responsible_machine(
&self,
identity_id: u64,
) -> Result<RefMut<KnownMachine<'a, T>>, EvalError<T>> {
let machine_index = *self
.identity_to_machine_index
.get(&identity_id)
.unwrap_or_else(|| panic!("No executor machine matched identity ID: {identity_id}"));

self.machines[machine_index]
.try_borrow_mut()
.map_err(|_| {
EvalError::RecursiveMachineCalls(format!(
"Detected when processing identity with ID {identity_id}"
))
})?
.process_plookup_timed(self, identity_id, caller_rows)
self.machines[machine_index].try_borrow_mut().map_err(|_| {
EvalError::RecursiveMachineCalls(format!(
"Detected when processing identity with ID {identity_id}"
))
})
}

/// Extracts the witness column values from the machines.
Expand Down
14 changes: 14 additions & 0 deletions executor/src/witgen/machines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ pub trait Machine<'a, T: FieldElement>: Send + Sync {
result
}

/// Like 'process_lookup_direct', but also records the time spent in this machine.
fn process_lookup_direct_timed<'b, 'c, Q: QueryCallback<T>>(
&mut self,
mutable_state: &'b MutableState<'a, T, Q>,
identity_id: u64,
values: &mut [LookupCell<'c, T>],
) -> Result<bool, EvalError<T>> {
record_start(self.name());
let result = self.process_lookup_direct(mutable_state, identity_id, values);
record_end(self.name());
result
}

/// Returns a unique name for this machine.
fn name(&self) -> &str;

Expand Down Expand Up @@ -106,6 +119,7 @@ pub trait Machine<'a, T: FieldElement>: Send + Sync {
fn identity_ids(&self) -> Vec<u64>;
}

#[repr(C)]
pub enum LookupCell<'a, T> {
/// Value is known (i.e. an input)
Input(&'a T),
Expand Down
4 changes: 2 additions & 2 deletions jit-compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ pub fn call_cargo(code: &str) -> Result<PathInTempDir, String> {
if log::log_enabled!(log::Level::Debug) {
let stderr = from_utf8(&out.stderr).unwrap_or("UTF-8 error in error message.");
return Err(format!(
"Rust compiler error when JIT-compiling. Will use evaluator for all symbols. Error message:\n{stderr}."
"Rust compiler error when JIT-compiling. Will use interpreter instead. Error message:\n{stderr}."
));
} else {
return Err("Rust compiler error when JIT-compiling. Will use evaluator for all symbols. Set log level to DEBUG for reason.".to_string());
return Err("Rust compiler error when JIT-compiling. Will use interpreter instead. Set log level to DEBUG for reason.".to_string());
}
}
let extension = if cfg!(target_os = "windows") {
Expand Down
2 changes: 1 addition & 1 deletion jit-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn compile<T: FieldElement>(
let successful_hash = successful_symbol_names.iter().collect::<HashSet<_>>();
// TODO this should be changed back to Info after the introduction of the ToCol trait.
log::debug!(
"Unable to generate code during JIT-compilation for the following symbols. Will use evaluator instead.\n{}",
"Unable to generate code during JIT-compilation for the following symbols. Will use interpreter instead.\n{}",
requested_symbols
.iter()
.filter(|&sym| !successful_hash.contains(sym))
Expand Down
4 changes: 4 additions & 0 deletions number/src/goldilocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ impl FieldElement for GoldilocksField {
// Undo the shift
Some(v.wrapping_sub(SHIFT as u32) as i32)
}

fn has_direct_repr() -> bool {
true
}
}

impl LowerHex for GoldilocksField {
Expand Down
4 changes: 4 additions & 0 deletions number/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ macro_rules! powdr_field {
// Undo the shift
Some(v.wrapping_sub(SHIFT as u32) as i32)
}

fn has_direct_repr() -> bool {
false
}
}

impl From<$ark_type> for $name {
Expand Down
5 changes: 5 additions & 0 deletions number/src/plonky3_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ macro_rules! powdr_field_plonky3 {
fn try_into_i32(&self) -> Option<i32> {
Some(self.to_canonical_u32() as i32)
}

fn has_direct_repr() -> bool {
// No direct repr, because 'mod' is not always applied.
false
}
}

impl LowerHex for $name {
Expand Down
6 changes: 6 additions & 0 deletions number/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ pub trait FieldElement:
/// As conventional, negative values are in relation to 0 in the field.
/// Returns None if out of the range [0 - 2^31, 2^31).
fn try_into_i32(&self) -> Option<i32>;

/// Returns `true` if values of this type are directly stored as their integer
/// value (i.e. not in montgomery representation and there are also no
/// additional fields), i.e. the `to_integer` function can be implemented as
/// a mem::transmute operation on pointers.
fn has_direct_repr() -> bool;
}

#[cfg(test)]
Expand Down
Loading