-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathsingle_step_processor.rs
172 lines (151 loc) · 5.84 KB
/
single_step_processor.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#![allow(dead_code)]
use std::collections::HashSet;
use itertools::Itertools;
use powdr_ast::analyzed::AlgebraicReference;
use powdr_number::FieldElement;
use crate::witgen::{machines::MachineParts, FixedData};
use super::{
effect::Effect,
variable::{Cell, Variable},
witgen_inference::{CanProcessCall, FixedEvaluator, WitgenInference},
};
/// A processor for generating JIT code that computes the next row from the previous row.
pub struct SingleStepProcessor<'a, T: FieldElement> {
fixed_data: &'a FixedData<'a, T>,
machine_parts: MachineParts<'a, T>,
}
impl<'a, T: FieldElement> SingleStepProcessor<'a, T> {
pub fn new(fixed_data: &'a FixedData<'a, T>, machine_parts: MachineParts<'a, T>) -> Self {
SingleStepProcessor {
fixed_data,
machine_parts,
}
}
pub fn generate_code<CanProcess: CanProcessCall<T> + Clone>(
&self,
can_process: CanProcess,
) -> Result<Vec<Effect<T, Variable>>, String> {
// All witness columns in row 0 are known.
let known_variables = self.machine_parts.witnesses.iter().map(|id| {
Variable::Cell(Cell {
column_name: self.fixed_data.column_name(id).to_string(),
id: id.id,
row_offset: 0,
})
});
let mut witgen = WitgenInference::new(self.fixed_data, self, known_variables);
let mut complete = HashSet::new();
for iteration in 0.. {
let mut progress = false;
for id in &self.machine_parts.identities {
if !complete.contains(&id.id()) {
let row_offset = if id.contains_next_ref() { 0 } else { 1 };
let result = witgen.process_identity(can_process.clone(), id, row_offset);
if result.complete {
complete.insert(id.id());
}
progress |= result.progress;
}
}
if !progress {
log::debug!("Finishing after {iteration} iterations");
break;
}
}
// Check that we could derive all witness values in the next row.
let unknown_witnesses = self
.machine_parts
.witnesses
.iter()
.filter(|wit| {
!witgen.is_known(&Variable::Cell(Cell {
column_name: self.fixed_data.column_name(wit).to_string(),
id: wit.id,
row_offset: 1,
}))
})
.sorted()
.collect_vec();
let missing_identities = self.machine_parts.identities.len() - complete.len();
if unknown_witnesses.is_empty() && missing_identities == 0 {
Ok(witgen.code())
} else {
Err(format!(
"Unable to derive algorithm to compute values for witness columns in the next row for the following columns:\n{}\nand {missing_identities} identities are missing.",
unknown_witnesses.iter().map(|wit| self.fixed_data.column_name(wit)).format(", ")
))
}
}
}
impl<T: FieldElement> FixedEvaluator<T> for &SingleStepProcessor<'_, T> {
fn evaluate(&self, _var: &AlgebraicReference, _row_offset: i32) -> Option<T> {
// We can only return something here if the fixed column is constant
// in the region we are considering.
// This might be the case if we know we are not evaluating the first or the last
// row, but this is not yet implemented.
None
}
}
#[cfg(test)]
mod test {
use itertools::Itertools;
use powdr_number::GoldilocksField;
use crate::witgen::{
data_structures::mutable_state::MutableState,
global_constraints,
jit::{
effect::{format_code, Effect},
test_util::read_pil,
},
machines::{machine_extractor::MachineExtractor, KnownMachine, Machine},
FixedData,
};
use super::{SingleStepProcessor, Variable};
fn generate_single_step(
input_pil: &str,
machine_name: &str,
) -> Result<Vec<Effect<GoldilocksField, Variable>>, String> {
let (analyzed, fixed_col_vals) = read_pil(input_pil);
let fixed_data = FixedData::new(&analyzed, &fixed_col_vals, &[], Default::default(), 0);
let (fixed_data, retained_identities) =
global_constraints::set_global_constraints(fixed_data, &analyzed.identities);
let machines = MachineExtractor::new(&fixed_data).split_out_machines(retained_identities);
let [KnownMachine::DynamicMachine(machine)] = machines
.iter()
.filter(|m| m.name().contains(machine_name))
.collect_vec()
.as_slice()
else {
panic!("Expected exactly one matching dynamic machine")
};
let machine_parts = machine.machine_parts().clone();
let mutable_state = MutableState::new(machines.into_iter(), &|_| {
Err("Query not implemented".to_string())
});
SingleStepProcessor {
fixed_data: &fixed_data,
machine_parts,
}
.generate_code(&mutable_state)
}
#[test]
fn fib() {
let input = "namespace M(256); let X; let Y; X' = Y; Y' = X + Y;";
let code = generate_single_step(input, "M").unwrap();
assert_eq!(
format_code(&code),
"M::X[1] = M::Y[0];\nM::Y[1] = (M::X[0] + M::Y[0]);"
);
}
#[test]
fn no_progress() {
let input = "namespace M(256); let X; let Y; X' = X;";
let err = generate_single_step(input, "M").err().unwrap();
assert_eq!(
err.to_string(),
"Unable to derive algorithm to compute values for witness columns in the next row for the following columns:\n\
M::Y\n\
and 0 identities are missing."
);
}
}