Skip to content

Commit a711df4

Browse files
authored
Always use first parameter as operation ID. (#2498)
1 parent c43182e commit a711df4

File tree

2 files changed

+50
-52
lines changed

2 files changed

+50
-52
lines changed

executor/src/witgen/jit/function_cache.rs

+31-19
Original file line numberDiff line numberDiff line change
@@ -81,31 +81,43 @@ impl<'a, T: FieldElement> FunctionCache<'a, T> {
8181
identity_id: u64,
8282
known_args: &BitVec,
8383
known_concrete: Option<(usize, T)>,
84-
) -> Option<&CacheEntry<T>> {
85-
let cache_key = CacheKey {
84+
) -> &Option<CacheEntry<T>> {
85+
// First try the generic version, then the specific.
86+
let mut key = CacheKey {
8687
identity_id,
8788
known_args: known_args.clone(),
88-
known_concrete,
89+
known_concrete: None,
8990
};
90-
self.ensure_cache(can_process, &cache_key);
91-
self.witgen_functions.get(&cache_key).unwrap().as_ref()
92-
}
9391

94-
fn ensure_cache(&mut self, can_process: impl CanProcessCall<T>, cache_key: &CacheKey<T>) {
95-
if self.witgen_functions.contains_key(cache_key) {
96-
return;
92+
if self.ensure_cache(can_process.clone(), &key).is_none() && known_concrete.is_some() {
93+
key = CacheKey {
94+
identity_id,
95+
known_args: known_args.clone(),
96+
known_concrete,
97+
};
98+
self.ensure_cache(can_process.clone(), &key);
9799
}
100+
self.ensure_cache(can_process, &key)
101+
}
98102

99-
record_start("Auto-witgen code derivation");
100-
let f = match T::known_field() {
101-
// Currently, we only support the Goldilocks fields
102-
Some(KnownField::GoldilocksField) => {
103-
self.compile_witgen_function(can_process, cache_key)
104-
}
105-
_ => None,
106-
};
107-
assert!(self.witgen_functions.insert(cache_key.clone(), f).is_none());
108-
record_end("Auto-witgen code derivation");
103+
fn ensure_cache(
104+
&mut self,
105+
can_process: impl CanProcessCall<T>,
106+
cache_key: &CacheKey<T>,
107+
) -> &Option<CacheEntry<T>> {
108+
if !self.witgen_functions.contains_key(cache_key) {
109+
record_start("Auto-witgen code derivation");
110+
let f = match T::known_field() {
111+
// Currently, we only support the Goldilocks fields
112+
Some(KnownField::GoldilocksField) => {
113+
self.compile_witgen_function(can_process, cache_key)
114+
}
115+
_ => None,
116+
};
117+
assert!(self.witgen_functions.insert(cache_key.clone(), f).is_none());
118+
record_end("Auto-witgen code derivation");
119+
}
120+
self.witgen_functions.get(cache_key).unwrap()
109121
}
110122

111123
fn compile_witgen_function(

executor/src/witgen/machines/block_machine.rs

+19-33
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,16 @@ impl<'a, T: FieldElement> Machine<'a, T> for BlockMachine<'a, T> {
172172
known_arguments: &BitVec,
173173
range_constraints: Vec<RangeConstraint<T>>,
174174
) -> (bool, Vec<RangeConstraint<T>>) {
175-
// We use the input range constraints to see if there is a column
176-
// containing the substring "operation_id" which is constrained to a
177-
// single value and use that value as part of the cache key.
178-
let operation_id = self.find_operation_id(identity_id).and_then(|index| {
179-
let v = range_constraints[index].try_to_single_value()?;
180-
Some((index, v))
181-
});
175+
let fixed_first_input = if !known_arguments.is_empty() && known_arguments[0] {
176+
range_constraints[0].try_to_single_value().map(|v| (0, v))
177+
} else {
178+
None
179+
};
182180
match self.function_cache.compile_cached(
183181
can_process,
184182
identity_id,
185183
known_arguments,
186-
operation_id,
184+
fixed_first_input,
187185
) {
188186
Some(entry) => (true, entry.range_constraints.clone()),
189187
None => (false, range_constraints),
@@ -200,12 +198,10 @@ impl<'a, T: FieldElement> Machine<'a, T> for BlockMachine<'a, T> {
200198
return Err(EvalError::RowsExhausted(self.name.clone()));
201199
}
202200

203-
let operation_id =
204-
self.find_operation_id(identity_id)
205-
.and_then(|index| match &values[index] {
206-
LookupCell::Input(v) => Some((index, **v)),
207-
LookupCell::Output(_) => None,
208-
});
201+
let fixed_first_input = match &values.first() {
202+
Some(LookupCell::Input(v)) => Some((0, **v)),
203+
None | Some(LookupCell::Output(_)) => None,
204+
};
209205

210206
self.data.finalize_all();
211207
let data = self.data.append_new_finalized_rows(self.block_size);
@@ -215,7 +211,7 @@ impl<'a, T: FieldElement> Machine<'a, T> for BlockMachine<'a, T> {
215211
identity_id,
216212
values,
217213
data,
218-
operation_id,
214+
fixed_first_input,
219215
)?;
220216
assert!(success);
221217
self.block_count_jit += 1;
@@ -452,13 +448,12 @@ impl<'a, T: FieldElement> BlockMachine<'a, T> {
452448
}
453449

454450
let known_inputs = arguments.iter().map(|e| e.is_constant()).collect();
455-
let operation_id = self.find_operation_id(identity_id).and_then(|index| {
456-
let v = arguments[index].constant_value()?;
457-
Some((index, v))
458-
});
451+
let fixed_first_input = arguments
452+
.first()
453+
.and_then(|a| a.constant_value().map(|v| (0, v)));
459454
if self
460455
.function_cache
461-
.compile_cached(mutable_state, identity_id, &known_inputs, operation_id)
456+
.compile_cached(mutable_state, identity_id, &known_inputs, fixed_first_input)
462457
.is_some()
463458
{
464459
let caller_data = CallerData::new(arguments, range_constraints);
@@ -537,12 +532,10 @@ impl<'a, T: FieldElement> BlockMachine<'a, T> {
537532
self.data.finalize_all();
538533

539534
let mut lookup_cells = caller_data.as_lookup_cells();
540-
let operation_id =
541-
self.find_operation_id(identity_id)
542-
.and_then(|index| match &lookup_cells[index] {
543-
LookupCell::Input(v) => Some((index, **v)),
544-
LookupCell::Output(_) => None,
545-
});
535+
let operation_id = match &lookup_cells.first() {
536+
Some(LookupCell::Input(v)) => Some((0, **v)),
537+
None | Some(LookupCell::Output(_)) => None,
538+
};
546539
let data = self.data.append_new_finalized_rows(self.block_size);
547540

548541
let success = self.function_cache.process_lookup_direct(
@@ -557,13 +550,6 @@ impl<'a, T: FieldElement> BlockMachine<'a, T> {
557550
caller_data.into()
558551
}
559552

560-
fn find_operation_id(&self, identity_id: u64) -> Option<usize> {
561-
let right = &self.parts.connections[&identity_id].right.expressions;
562-
right.iter().position(|r| {
563-
try_to_simple_poly(r).is_some_and(|poly| poly.name.contains("operation_id"))
564-
})
565-
}
566-
567553
fn process<'b, Q: QueryCallback<T>>(
568554
&self,
569555
mutable_state: &MutableState<'a, T, Q>,

0 commit comments

Comments
 (0)