Skip to content

Commit 6c915c8

Browse files
committed
Build optimistic precompiles
1 parent 661a429 commit 6c915c8

File tree

13 files changed

+301
-31
lines changed

13 files changed

+301
-31
lines changed

autoprecompiles/src/adapter.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{fmt::Display, sync::Arc};
66
use powdr_number::FieldElement;
77
use serde::{Deserialize, Serialize};
88

9+
use crate::EmpiricalConstraints;
910
use crate::{
1011
blocks::{BasicBlock, Instruction, Program},
1112
constraint_optimizer::IsBusStateful,
@@ -45,12 +46,19 @@ pub trait PgoAdapter {
4546
config: &PowdrConfig,
4647
vm_config: AdapterVmConfig<Self::Adapter>,
4748
labels: BTreeMap<u64, Vec<String>>,
49+
empirical_constraints: EmpiricalConstraints,
4850
) -> Vec<AdapterApcWithStats<Self::Adapter>> {
4951
let filtered_blocks = blocks
5052
.into_iter()
5153
.filter(|block| !Self::Adapter::should_skip_block(block))
5254
.collect();
53-
self.create_apcs_with_pgo(filtered_blocks, config, vm_config, labels)
55+
self.create_apcs_with_pgo(
56+
filtered_blocks,
57+
config,
58+
vm_config,
59+
labels,
60+
empirical_constraints,
61+
)
5462
}
5563

5664
fn create_apcs_with_pgo(
@@ -59,6 +67,7 @@ pub trait PgoAdapter {
5967
config: &PowdrConfig,
6068
vm_config: AdapterVmConfig<Self::Adapter>,
6169
labels: BTreeMap<u64, Vec<String>>,
70+
empirical_constraints: EmpiricalConstraints,
6271
) -> Vec<AdapterApcWithStats<Self::Adapter>>;
6372

6473
fn pc_execution_count(&self, _pc: u64) -> Option<u32> {

autoprecompiles/src/empirical_constraints.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ use std::hash::Hash;
66
use itertools::Itertools;
77
use serde::{Deserialize, Serialize};
88

9+
use crate::{
10+
adapter::Adapter,
11+
blocks::BasicBlock,
12+
expression::{AlgebraicExpression, AlgebraicReference},
13+
SymbolicConstraint,
14+
};
15+
916
/// "Constraints" that were inferred from execution statistics. They hold empirically
1017
/// (most of the time), but are not guaranteed to hold in all cases.
1118
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
@@ -16,6 +23,9 @@ pub struct EmpiricalConstraints {
1623
/// For each basic block (identified by its starting PC), the equivalence classes of columns.
1724
/// Each equivalence class is a list of (instruction index in block, column index).
1825
pub equivalence_classes_by_block: BTreeMap<u64, BTreeSet<BTreeSet<(usize, usize)>>>,
26+
/// Count of how many times each program counter was executed in the sampled executions.
27+
/// This can be used to set a threshold for applying constraints only to frequently executed PCs.
28+
pub pc_counts: BTreeMap<u32, u64>,
1929
}
2030

2131
/// Debug information mapping AIR ids to program counters and column names.
@@ -61,6 +71,24 @@ impl EmpiricalConstraints {
6171
})
6272
.or_insert(classes);
6373
}
74+
75+
// Combine pc counts
76+
for (pc, count) in other.pc_counts {
77+
*self.pc_counts.entry(pc).or_insert(0) += count;
78+
}
79+
}
80+
81+
pub fn with_thresholded_pc_count(mut self, threshold: u64) -> Self {
82+
self.column_ranges_by_pc
83+
.retain(|pc, _| self.pc_counts.get(pc).cloned().unwrap_or(0) >= threshold);
84+
self.equivalence_classes_by_block.retain(|block_id, _| {
85+
self.pc_counts
86+
.get(&(*block_id as u32))
87+
.cloned()
88+
.unwrap_or(0)
89+
>= threshold
90+
});
91+
self
6492
}
6593
}
6694

@@ -88,6 +116,99 @@ fn merge_maps<K: Ord, V: Eq + Debug>(map1: &mut BTreeMap<K, V>, map2: BTreeMap<K
88116
}
89117
}
90118

119+
type ConstraintList<A> = Vec<SymbolicConstraint<<A as Adapter>::PowdrField>>;
120+
121+
/// For any program line that was not executed at least this many times in the traces,
122+
/// discard any empirical constraints associated with it.
123+
const EXECUTION_COUNT_THRESHOLD: u64 = 100;
124+
125+
pub fn add_empirical_constraints<A: Adapter>(
126+
empirical_constraints: &EmpiricalConstraints,
127+
subs: &[Vec<u64>],
128+
block: &BasicBlock<A::Instruction>,
129+
columns: impl Iterator<Item = AlgebraicReference>,
130+
) -> (ConstraintList<A>, ConstraintList<A>) {
131+
// Apply execution count threshold to avoid overfitting on rarely executed code
132+
let empirical_constraints = empirical_constraints
133+
.clone()
134+
.with_thresholded_pc_count(EXECUTION_COUNT_THRESHOLD);
135+
136+
let range_constraints = &empirical_constraints.column_ranges_by_pc;
137+
let equivalence_classes_by_block = &empirical_constraints.equivalence_classes_by_block;
138+
139+
let mut range_analyzer_constraints = Vec::new();
140+
let mut equivalence_analyzer_constraints = Vec::new();
141+
142+
// Mapping (instruction index, column index) -> AlgebraicReference
143+
let reverse_subs = subs
144+
.iter()
145+
.enumerate()
146+
.flat_map(|(instr_index, subs)| {
147+
subs.iter()
148+
.enumerate()
149+
.map(move |(col_index, &poly_id)| (poly_id, (instr_index, col_index)))
150+
})
151+
.collect::<BTreeMap<_, _>>();
152+
let algebraic_references = columns
153+
.map(|r| (*reverse_subs.get(&r.id).unwrap(), r.clone()))
154+
.collect::<BTreeMap<_, _>>();
155+
156+
for i in 0..block.statements.len() {
157+
let pc = (block.start_pc + (i * 4) as u64) as u32;
158+
let Some(range_constraints) = range_constraints.get(&pc) else {
159+
continue;
160+
};
161+
for (col_index, range) in range_constraints.iter().enumerate() {
162+
if range.0 == range.1 {
163+
let value = A::PowdrField::from(range.0 as u64);
164+
let Some(reference) = algebraic_references.get(&(i, col_index)).cloned() else {
165+
panic!(
166+
"Missing reference for (i: {}, col_index: {}, block_id: {})",
167+
i, col_index, block.start_pc
168+
);
169+
};
170+
let constraint =
171+
AlgebraicExpression::Reference(reference) - AlgebraicExpression::Number(value);
172+
173+
range_analyzer_constraints.push(SymbolicConstraint { expr: constraint });
174+
}
175+
}
176+
}
177+
178+
if let Some(equivalence_classes) = equivalence_classes_by_block.get(&block.start_pc) {
179+
for equivalence_class in equivalence_classes {
180+
let first = equivalence_class.first().unwrap();
181+
let Some(first_ref) = algebraic_references.get(first).cloned() else {
182+
// TODO: This fails in some blocks. For now, just return no extra constraints.
183+
tracing::warn!(
184+
"Missing reference for (i: {}, col_index: {}, block_id: {})",
185+
first.0,
186+
first.1,
187+
block.start_pc
188+
);
189+
return (range_analyzer_constraints, vec![]);
190+
};
191+
for other in equivalence_class.iter().skip(1) {
192+
let Some(other_ref) = algebraic_references.get(other).cloned() else {
193+
// TODO: This fails in some blocks. For now, just return no extra constraints.
194+
tracing::warn!(
195+
"Missing reference for (i: {}, col_index: {}, block_id: {})",
196+
other.0,
197+
other.1,
198+
block.start_pc
199+
);
200+
return (range_analyzer_constraints, vec![]);
201+
};
202+
let constraint = AlgebraicExpression::Reference(first_ref.clone())
203+
- AlgebraicExpression::Reference(other_ref.clone());
204+
equivalence_analyzer_constraints.push(SymbolicConstraint { expr: constraint });
205+
}
206+
}
207+
}
208+
209+
(range_analyzer_constraints, equivalence_analyzer_constraints)
210+
}
211+
91212
/// Intersects multiple partitions of the same universe into a single partition.
92213
/// In other words, two elements are in the same equivalence class in the resulting partition
93214
/// if and only if they are in the same equivalence class in all input partitions.

autoprecompiles/src/lib.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::adapter::{Adapter, AdapterApc, AdapterVmConfig};
22
use crate::blocks::BasicBlock;
33
use crate::bus_map::{BusMap, BusType};
4+
use crate::empirical_constraints::{add_empirical_constraints, EmpiricalConstraints};
45
use crate::evaluation::AirStats;
56
use crate::expression_conversion::algebraic_to_grouped_expression;
67
use crate::symbolic_machine_generator::convert_machine_field_type;
@@ -429,6 +430,7 @@ pub fn build<A: Adapter>(
429430
vm_config: AdapterVmConfig<A>,
430431
degree_bound: DegreeBound,
431432
apc_candidates_dir_path: Option<&Path>,
433+
empirical_constraints: &EmpiricalConstraints,
432434
) -> Result<AdapterApc<A>, crate::constraint_optimizer::Error> {
433435
let start = std::time::Instant::now();
434436

@@ -438,6 +440,14 @@ pub fn build<A: Adapter>(
438440
&vm_config.bus_map,
439441
);
440442

443+
let (range_analyzer_constraints, _equivalence_analyzer_constraints) =
444+
add_empirical_constraints::<A>(
445+
empirical_constraints,
446+
&column_allocator.subs,
447+
&block,
448+
machine.main_columns(),
449+
);
450+
441451
let labels = [("apc_start_pc", block.start_pc.to_string())];
442452
metrics::counter!("before_opt_cols", &labels)
443453
.absolute(machine.unique_references().count() as u64);
@@ -446,13 +456,33 @@ pub fn build<A: Adapter>(
446456
metrics::counter!("before_opt_interactions", &labels)
447457
.absolute(machine.unique_references().count() as u64);
448458

459+
let mut baseline = machine;
460+
449461
let (machine, column_allocator) = optimizer::optimize::<A>(
450-
machine,
462+
baseline.clone(),
463+
vm_config.bus_interaction_handler.clone(),
464+
degree_bound,
465+
&vm_config.bus_map,
466+
column_allocator,
467+
)
468+
.unwrap();
469+
let dumb_precompile = machine.render(&vm_config.bus_map);
470+
471+
baseline.constraints.extend(range_analyzer_constraints);
472+
// TODO: Appears to be buggy
473+
// baseline
474+
// .constraints
475+
// .extend(equivalence_analyzer_constraints);
476+
477+
let (machine, column_allocator) = optimizer::optimize::<A>(
478+
baseline,
451479
vm_config.bus_interaction_handler,
452480
degree_bound,
453481
&vm_config.bus_map,
454482
column_allocator,
455-
)?;
483+
)
484+
.unwrap();
485+
let ai_precompile = machine.render(&vm_config.bus_map);
456486

457487
// add guards to constraints that are not satisfied by zeroes
458488
let (machine, column_allocator) = add_guards(machine, column_allocator);
@@ -477,6 +507,16 @@ pub fn build<A: Adapter>(
477507
std::fs::File::create(&ser_path).expect("Failed to create file for APC candidate");
478508
let writer = BufWriter::new(file);
479509
serde_cbor::to_writer(writer, &apc).expect("Failed to write APC candidate to file");
510+
511+
let dumb_path = path
512+
.join(format!("apc_candidate_{}_dumb.txt", apc.start_pc()))
513+
.with_extension("txt");
514+
std::fs::write(dumb_path, dumb_precompile).unwrap();
515+
516+
let ai_path = path
517+
.join(format!("apc_candidate_{}_ai.txt", apc.start_pc()))
518+
.with_extension("txt");
519+
std::fs::write(ai_path, ai_precompile).unwrap();
480520
}
481521

482522
metrics::gauge!("apc_gen_time_ms", &labels).set(start.elapsed().as_millis() as f64);

autoprecompiles/src/memory_optimizer.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ fn redundant_memory_interactions_indices<
187187
// In that case, we can replace both bus interactions with equality constraints
188188
// between the data that would have been sent and received.
189189
if let Some((previous_send, existing_values)) = memory_contents.remove(&addr) {
190+
if existing_values.len() != mem_int.data().len() {
191+
log::error!(
192+
"Memory interaction data length mismatch: existing values = {}, new values = {}. Resetting memory.",
193+
existing_values.iter().map(ToString::to_string).join(", "),
194+
mem_int.data().iter().map(ToString::to_string).join(", ")
195+
);
196+
memory_contents.clear();
197+
continue;
198+
}
199+
190200
for (existing, new) in existing_values.iter().zip_eq(mem_int.data().iter()) {
191201
new_constraints.push(AlgebraicConstraint::assert_zero(
192202
existing.clone() - new.clone(),

autoprecompiles/src/pgo/cell/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
blocks::BasicBlock,
1414
evaluation::EvaluationResult,
1515
pgo::cell::selection::parallel_fractional_knapsack,
16-
PowdrConfig,
16+
EmpiricalConstraints, PowdrConfig,
1717
};
1818

1919
mod selection;
@@ -92,6 +92,7 @@ impl<A: Adapter + Send + Sync, C: Candidate<A> + Send + Sync> PgoAdapter for Cel
9292
config: &PowdrConfig,
9393
vm_config: AdapterVmConfig<Self::Adapter>,
9494
labels: BTreeMap<u64, Vec<String>>,
95+
empirical_constraints: EmpiricalConstraints,
9596
) -> Vec<AdapterApcWithStats<Self::Adapter>> {
9697
tracing::info!(
9798
"Generating autoprecompiles with cell PGO for {} blocks",
@@ -131,6 +132,7 @@ impl<A: Adapter + Send + Sync, C: Candidate<A> + Send + Sync> PgoAdapter for Cel
131132
vm_config.clone(),
132133
config.degree_bound,
133134
config.apc_candidates_dir_path.as_deref(),
135+
&empirical_constraints,
134136
)
135137
.ok()?;
136138
let candidate = C::create(

autoprecompiles/src/pgo/instruction.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
adapter::{Adapter, AdapterApcWithStats, AdapterVmConfig, PgoAdapter},
55
blocks::BasicBlock,
66
pgo::create_apcs_for_all_blocks,
7-
PowdrConfig,
7+
EmpiricalConstraints, PowdrConfig,
88
};
99

1010
pub struct InstructionPgo<A> {
@@ -30,6 +30,7 @@ impl<A: Adapter> PgoAdapter for InstructionPgo<A> {
3030
config: &PowdrConfig,
3131
vm_config: AdapterVmConfig<Self::Adapter>,
3232
_labels: BTreeMap<u64, Vec<String>>,
33+
empirical_constraints: EmpiricalConstraints,
3334
) -> Vec<AdapterApcWithStats<Self::Adapter>> {
3435
tracing::info!(
3536
"Generating autoprecompiles with instruction PGO for {} blocks",
@@ -70,7 +71,12 @@ impl<A: Adapter> PgoAdapter for InstructionPgo<A> {
7071
);
7172
}
7273

73-
create_apcs_for_all_blocks::<Self::Adapter>(blocks, config, vm_config)
74+
create_apcs_for_all_blocks::<Self::Adapter>(
75+
blocks,
76+
config,
77+
vm_config,
78+
empirical_constraints,
79+
)
7480
}
7581

7682
fn pc_execution_count(&self, pc: u64) -> Option<u32> {

autoprecompiles/src/pgo/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use strum::{Display, EnumString};
66
use crate::{
77
adapter::{Adapter, AdapterApcWithStats, AdapterVmConfig, ApcWithStats},
88
blocks::BasicBlock,
9-
PowdrConfig,
9+
EmpiricalConstraints, PowdrConfig,
1010
};
1111

1212
mod cell;
@@ -77,6 +77,7 @@ fn create_apcs_for_all_blocks<A: Adapter>(
7777
blocks: Vec<BasicBlock<A::Instruction>>,
7878
config: &PowdrConfig,
7979
vm_config: AdapterVmConfig<A>,
80+
empirical_constraints: EmpiricalConstraints,
8081
) -> Vec<AdapterApcWithStats<A>> {
8182
let n_acc = config.autoprecompiles as usize;
8283
tracing::info!("Generating {n_acc} autoprecompiles in parallel");
@@ -97,6 +98,7 @@ fn create_apcs_for_all_blocks<A: Adapter>(
9798
vm_config.clone(),
9899
config.degree_bound,
99100
config.apc_candidates_dir_path.as_deref(),
101+
&empirical_constraints,
100102
)
101103
.unwrap()
102104
})

autoprecompiles/src/pgo/none.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
adapter::{Adapter, AdapterApcWithStats, AdapterVmConfig, PgoAdapter},
55
blocks::BasicBlock,
66
pgo::create_apcs_for_all_blocks,
7-
PowdrConfig,
7+
EmpiricalConstraints, PowdrConfig,
88
};
99

1010
pub struct NonePgo<A> {
@@ -29,6 +29,7 @@ impl<A: Adapter> PgoAdapter for NonePgo<A> {
2929
config: &PowdrConfig,
3030
vm_config: AdapterVmConfig<Self::Adapter>,
3131
_labels: BTreeMap<u64, Vec<String>>,
32+
empirical_constraints: EmpiricalConstraints,
3233
) -> Vec<AdapterApcWithStats<Self::Adapter>> {
3334
// cost = number_of_original_instructions
3435
blocks.sort_by(|a, b| b.statements.len().cmp(&a.statements.len()));
@@ -42,6 +43,6 @@ impl<A: Adapter> PgoAdapter for NonePgo<A> {
4243
);
4344
}
4445

45-
create_apcs_for_all_blocks::<Self::Adapter>(blocks, config, vm_config)
46+
create_apcs_for_all_blocks::<Self::Adapter>(blocks, config, vm_config, empirical_constraints)
4647
}
4748
}

0 commit comments

Comments
 (0)