Skip to content

Commit 1322a28

Browse files
committed
Collect empirical constraints
1 parent e0e90bc commit 1322a28

File tree

8 files changed

+417
-26
lines changed

8 files changed

+417
-26
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::collections::BTreeMap;
2+
3+
use serde::{Deserialize, Serialize};
4+
5+
/// "Constraints" that were inferred from execution statistics.
6+
#[derive(Serialize, Deserialize, Clone, Default)]
7+
pub struct EmpiricalConstraints {
8+
/// For each program counter, the range constraints for each column.
9+
/// The range might not hold in 100% of cases.
10+
pub column_ranges_by_pc: BTreeMap<u32, Vec<(u32, u32)>>,
11+
/// For each basic block (identified by its starting PC), the equivalence classes of columns.
12+
/// Each equivalence class is a list of (instruction index in block, column index).
13+
pub equivalence_classes_by_block: BTreeMap<u64, Vec<Vec<(usize, usize)>>>,
14+
}
15+
16+
/// Debug information mapping AIR ids to program counters and column names.
17+
#[derive(Serialize, Deserialize)]
18+
pub struct DebugInfo {
19+
/// Mapping from program counter to AIR id.
20+
pub air_id_by_pc: BTreeMap<u32, usize>,
21+
/// Mapping from AIR id to column names.
22+
pub column_names_by_air_id: BTreeMap<usize, Vec<String>>,
23+
}
24+
25+
#[derive(Serialize, Deserialize)]
26+
pub struct EmpiricalConstraintsJson {
27+
pub empirical_constraints: EmpiricalConstraints,
28+
pub debug_info: DebugInfo,
29+
}

autoprecompiles/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub mod adapter;
2626
pub mod blocks;
2727
pub mod bus_map;
2828
pub mod constraint_optimizer;
29+
pub mod empirical_constraints;
2930
pub mod evaluation;
3031
pub mod execution_profile;
3132
pub mod expression;

cli-openvm/src/main.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,14 @@ fn run_command(command: Commands) {
146146
stdin_from(input),
147147
);
148148
let pgo_config = pgo_config(pgo, max_columns, execution_profile);
149-
let program =
150-
powdr_openvm::compile_guest(&guest, guest_opts, powdr_config, pgo_config).unwrap();
149+
let program = powdr_openvm::compile_guest(
150+
&guest,
151+
guest_opts,
152+
powdr_config,
153+
pgo_config,
154+
stdin_from(input),
155+
)
156+
.unwrap();
151157
write_program_to_file(program, &format!("{guest}_compiled.cbor")).unwrap();
152158
}
153159

@@ -172,9 +178,14 @@ fn run_command(command: Commands) {
172178
);
173179
let pgo_config = pgo_config(pgo, max_columns, execution_profile);
174180
let compile_and_exec = || {
175-
let program =
176-
powdr_openvm::compile_guest(&guest, guest_opts, powdr_config, pgo_config)
177-
.unwrap();
181+
let program = powdr_openvm::compile_guest(
182+
&guest,
183+
guest_opts,
184+
powdr_config,
185+
pgo_config,
186+
stdin_from(input),
187+
)
188+
.unwrap();
178189
powdr_openvm::execute(program, stdin_from(input)).unwrap();
179190
};
180191
if let Some(metrics_path) = metrics {
@@ -210,9 +221,14 @@ fn run_command(command: Commands) {
210221
);
211222
let pgo_config = pgo_config(pgo, max_columns, execution_profile);
212223
let compile_and_prove = || {
213-
let program =
214-
powdr_openvm::compile_guest(&guest, guest_opts, powdr_config, pgo_config)
215-
.unwrap();
224+
let program = powdr_openvm::compile_guest(
225+
&guest,
226+
guest_opts,
227+
powdr_config,
228+
pgo_config,
229+
stdin_from(input),
230+
)
231+
.unwrap();
216232
powdr_openvm::prove(&program, mock, recursion, stdin_from(input), None).unwrap()
217233
};
218234
if let Some(metrics_path) = metrics {

openvm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ derive_more = { version = "2.0.1", default-features = false, features = [
6161
"from",
6262
] }
6363
itertools = "0.14.0"
64+
serde_json = "1.0.140"
6465

6566
tracing = "0.1.40"
6667
tracing-subscriber = { version = "0.3.17", features = ["std", "env-filter"] }

openvm/src/customize_exe.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ use crate::memory_bus_interaction::OpenVmMemoryBusInteraction;
1313
use crate::opcode::branch_opcodes_bigint_set;
1414
use crate::powdr_extension::chip::PowdrAir;
1515
use crate::utils::UnsupportedOpenVmReferenceError;
16-
use crate::OriginalCompiledProgram;
16+
use crate::{empirical_constraints::detect_empirical_constraints, OriginalCompiledProgram};
1717
use crate::{CompiledProgram, SpecializedConfig};
1818
use itertools::Itertools;
1919
use openvm_instructions::instruction::Instruction as OpenVmInstruction;
2020
use openvm_instructions::program::{Program as OpenVmProgram, DEFAULT_PC_STEP};
2121
use openvm_instructions::VmOpcode;
22+
use openvm_sdk::StdIn;
2223
use openvm_stark_backend::{
2324
interaction::SymbolicInteraction,
2425
p3_field::{FieldAlgebra, PrimeField32},
@@ -147,6 +148,7 @@ pub fn customize<'a, P: PgoAdapter<Adapter = BabyBearOpenVmApcAdapter<'a>>>(
147148
}: OriginalCompiledProgram,
148149
config: PowdrConfig,
149150
pgo: P,
151+
inputs: StdIn,
150152
) -> CompiledProgram {
151153
let labels = elf.text_labels();
152154
let debug_info = elf.debug_info();
@@ -217,6 +219,17 @@ pub fn customize<'a, P: PgoAdapter<Adapter = BabyBearOpenVmApcAdapter<'a>>>(
217219
})
218220
.collect();
219221

222+
let _empirical_constraints = detect_empirical_constraints(
223+
exe.clone(),
224+
SpecializedConfig::new(
225+
original_config.clone(),
226+
vec![],
227+
config.degree_bound.identities,
228+
),
229+
inputs,
230+
&blocks,
231+
);
232+
220233
let start = std::time::Instant::now();
221234
let apcs = pgo.filter_blocks_and_create_apcs_with_pgo(blocks, &config, vm_config, labels);
222235
metrics::gauge!("total_apc_gen_time_ms").set(start.elapsed().as_millis() as f64);

0 commit comments

Comments
 (0)