Skip to content

Commit 2b58629

Browse files
committed
Build optimistic precompiles
1 parent 1322a28 commit 2b58629

File tree

11 files changed

+194
-22
lines changed

11 files changed

+194
-22
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: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ use std::collections::BTreeMap;
22

33
use serde::{Deserialize, Serialize};
44

5+
use crate::{
6+
adapter::Adapter,
7+
blocks::BasicBlock,
8+
expression::{AlgebraicExpression, AlgebraicReference},
9+
SymbolicConstraint,
10+
};
11+
512
/// "Constraints" that were inferred from execution statistics.
613
#[derive(Serialize, Deserialize, Clone, Default)]
714
pub struct EmpiricalConstraints {
@@ -27,3 +34,84 @@ pub struct EmpiricalConstraintsJson {
2734
pub empirical_constraints: EmpiricalConstraints,
2835
pub debug_info: DebugInfo,
2936
}
37+
38+
pub fn add_empirical_constraints<A: Adapter>(
39+
empirical_constraints: &EmpiricalConstraints,
40+
subs: &[Vec<u64>],
41+
block: &BasicBlock<A::Instruction>,
42+
columns: impl Iterator<Item = AlgebraicReference>,
43+
) -> (
44+
Vec<SymbolicConstraint<A::PowdrField>>,
45+
Vec<SymbolicConstraint<A::PowdrField>>,
46+
) {
47+
let range_constraints = &empirical_constraints.column_ranges_by_pc;
48+
let equivalence_classes_by_block = &empirical_constraints.equivalence_classes_by_block;
49+
50+
let mut range_analyzer_constraints = Vec::new();
51+
let mut equivalence_analyzer_constraints = Vec::new();
52+
53+
// Mapping (instruction index, column index) -> AlgebraicReference
54+
let reverse_subs = subs
55+
.iter()
56+
.enumerate()
57+
.flat_map(|(instr_index, subs)| {
58+
subs.iter()
59+
.enumerate()
60+
.map(move |(col_index, &poly_id)| (poly_id, (instr_index, col_index)))
61+
})
62+
.collect::<BTreeMap<_, _>>();
63+
let algebraic_references = columns
64+
.map(|r| (reverse_subs.get(&r.id).unwrap().clone(), r.clone()))
65+
.collect::<BTreeMap<_, _>>();
66+
67+
for i in 0..block.statements.len() {
68+
let pc = (block.start_pc + (i * 4) as u64) as u32;
69+
let Some(range_constraints) = range_constraints.get(&pc) else {
70+
continue;
71+
};
72+
for (col_index, range) in range_constraints.iter().enumerate() {
73+
if range.0 == range.1 {
74+
let value = A::PowdrField::from(range.0 as u64);
75+
let Some(reference) = algebraic_references.get(&(i, col_index)).cloned() else {
76+
panic!(
77+
"Missing reference for (i: {}, col_index: {}, block_id: {})",
78+
i, col_index, block.start_pc
79+
);
80+
};
81+
let constraint =
82+
AlgebraicExpression::Reference(reference) - AlgebraicExpression::Number(value);
83+
84+
range_analyzer_constraints.push(SymbolicConstraint { expr: constraint });
85+
}
86+
}
87+
}
88+
89+
if let Some(equivalence_classes) = equivalence_classes_by_block.get(&block.start_pc) {
90+
for equivalence_class in equivalence_classes {
91+
let first = equivalence_class.first().unwrap();
92+
let Some(first_ref) = algebraic_references.get(first).cloned() else {
93+
// TODO: This fails in some blocks. For now, just return no extra constraints.
94+
println!(
95+
"Missing reference for (i: {}, col_index: {}, block_id: {})",
96+
first.0, first.1, block.start_pc
97+
);
98+
return (range_analyzer_constraints, vec![]);
99+
};
100+
for other in equivalence_class.iter().skip(1) {
101+
let Some(other_ref) = algebraic_references.get(other).cloned() else {
102+
// TODO: This fails in some blocks. For now, just return no extra constraints.
103+
println!(
104+
"Missing reference for (i: {}, col_index: {}, block_id: {})",
105+
other.0, other.1, block.start_pc
106+
);
107+
return (range_analyzer_constraints, vec![]);
108+
};
109+
let constraint = AlgebraicExpression::Reference(first_ref.clone())
110+
- AlgebraicExpression::Reference(other_ref.clone());
111+
equivalence_analyzer_constraints.push(SymbolicConstraint { expr: constraint });
112+
}
113+
}
114+
}
115+
116+
(range_analyzer_constraints, equivalence_analyzer_constraints)
117+
}

autoprecompiles/src/lib.rs

Lines changed: 54 additions & 8 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;
@@ -394,9 +395,10 @@ impl<T, I> Apc<T, I> {
394395
}
395396

396397
/// Allocates global poly_ids and keeps track of substitutions
398+
#[derive(Debug)]
397399
pub struct ColumnAllocator {
398400
/// For each original air, for each original column index, the associated poly_id in the APC air
399-
subs: Vec<Vec<u64>>,
401+
pub subs: Vec<Vec<u64>>,
400402
/// The next poly_id to issue
401403
next_poly_id: u64,
402404
}
@@ -421,6 +423,7 @@ pub fn build<A: Adapter>(
421423
vm_config: AdapterVmConfig<A>,
422424
degree_bound: DegreeBound,
423425
apc_candidates_dir_path: Option<&Path>,
426+
empirical_constraints: &EmpiricalConstraints,
424427
) -> Result<AdapterApc<A>, crate::constraint_optimizer::Error> {
425428
let start = std::time::Instant::now();
426429

@@ -430,6 +433,14 @@ pub fn build<A: Adapter>(
430433
&vm_config.bus_map,
431434
);
432435

436+
let (range_analyzer_constraints, _equivalence_analyzer_constraints) =
437+
add_empirical_constraints::<A>(
438+
empirical_constraints,
439+
&column_allocator.subs,
440+
&block,
441+
machine.main_columns(),
442+
);
443+
433444
let labels = [("apc_start_pc", block.start_pc.to_string())];
434445
metrics::counter!("before_opt_cols", &labels)
435446
.absolute(machine.unique_references().count() as u64);
@@ -438,13 +449,33 @@ pub fn build<A: Adapter>(
438449
metrics::counter!("before_opt_interactions", &labels)
439450
.absolute(machine.unique_references().count() as u64);
440451

452+
let mut baseline = machine;
453+
454+
let (machine, column_allocator) = optimizer::optimize::<A>(
455+
baseline.clone(),
456+
vm_config.bus_interaction_handler.clone(),
457+
degree_bound,
458+
&vm_config.bus_map,
459+
column_allocator,
460+
)
461+
.unwrap();
462+
let dumb_precompile = machine.render(&vm_config.bus_map);
463+
464+
baseline.constraints.extend(range_analyzer_constraints);
465+
// TODO: Appears to be buggy
466+
// baseline
467+
// .constraints
468+
// .extend(equivalence_analyzer_constraints);
469+
441470
let (machine, column_allocator) = optimizer::optimize::<A>(
442-
machine,
471+
baseline,
443472
vm_config.bus_interaction_handler,
444473
degree_bound,
445474
&vm_config.bus_map,
446475
column_allocator,
447-
)?;
476+
)
477+
.unwrap();
478+
let ai_precompile = machine.render(&vm_config.bus_map);
448479

449480
// add guards to constraints that are not satisfied by zeroes
450481
let (machine, column_allocator) = add_guards(machine, column_allocator);
@@ -469,6 +500,16 @@ pub fn build<A: Adapter>(
469500
std::fs::File::create(&ser_path).expect("Failed to create file for APC candidate");
470501
let writer = BufWriter::new(file);
471502
serde_cbor::to_writer(writer, &apc).expect("Failed to write APC candidate to file");
503+
504+
let dumb_path = path
505+
.join(format!("apc_candidate_{}_dumb.txt", apc.start_pc()))
506+
.with_extension("txt");
507+
std::fs::write(dumb_path, dumb_precompile).unwrap();
508+
509+
let ai_path = path
510+
.join(format!("apc_candidate_{}_ai.txt", apc.start_pc()))
511+
.with_extension("txt");
512+
std::fs::write(ai_path, ai_precompile).unwrap();
472513
}
473514

474515
metrics::gauge!("apc_gen_time_ms", &labels).set(start.elapsed().as_millis() as f64);
@@ -556,11 +597,16 @@ fn add_guards<T: FieldElement>(
556597

557598
machine.constraints.extend(is_valid_mults);
558599

559-
assert_eq!(
560-
pre_degree,
561-
machine.degree(),
562-
"Degree should not change after adding guards"
563-
);
600+
// TODO: Why do we need this?
601+
if pre_degree != 0 {
602+
assert_eq!(
603+
pre_degree,
604+
machine.degree(),
605+
"Degree should not change after adding guards, but changed from {} to {}",
606+
pre_degree,
607+
machine.degree(),
608+
);
609+
}
564610

565611
// This needs to be added after the assertion above because it's a quadratic constraint
566612
// so it may increase the degree of the machine.

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: 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 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,7 @@ 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>(blocks, config, vm_config, empirical_constraints)
7475
}
7576

7677
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
}

cli-openvm/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,14 @@ fn run_command(command: Commands) {
211211
apc_candidates_dir,
212212
} => {
213213
let mut powdr_config = default_powdr_openvm_config(autoprecompiles as u64, skip as u64);
214-
if let Some(apc_candidates_dir) = apc_candidates_dir {
214+
if let Some(apc_candidates_dir) = &apc_candidates_dir {
215215
powdr_config = powdr_config.with_apc_candidates_dir(apc_candidates_dir);
216216
}
217+
let inputs = stdin_from(input);
217218
let execution_profile = powdr_openvm::execution_profile_from_guest(
218219
&guest,
219220
guest_opts.clone(),
220-
stdin_from(input),
221+
inputs.clone(),
221222
);
222223
let pgo_config = pgo_config(pgo, max_columns, execution_profile);
223224
let compile_and_prove = || {

0 commit comments

Comments
 (0)