Skip to content

Commit 0212468

Browse files
committed
Pass execution stats directly
1 parent 594177f commit 0212468

File tree

8 files changed

+35
-25
lines changed

8 files changed

+35
-25
lines changed

autoprecompiles/src/adapter.rs

Lines changed: 4 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::ExecutionStats;
910
use crate::{
1011
blocks::{BasicBlock, Instruction, Program},
1112
constraint_optimizer::IsBusStateful,
@@ -45,12 +46,13 @@ pub trait PgoAdapter {
4546
config: &PowdrConfig,
4647
vm_config: AdapterVmConfig<Self::Adapter>,
4748
labels: BTreeMap<u64, Vec<String>>,
49+
execution_stats: ExecutionStats,
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(filtered_blocks, config, vm_config, labels, execution_stats)
5456
}
5557

5658
fn create_apcs_with_pgo(
@@ -59,6 +61,7 @@ pub trait PgoAdapter {
5961
config: &PowdrConfig,
6062
vm_config: AdapterVmConfig<Self::Adapter>,
6163
labels: BTreeMap<u64, Vec<String>>,
64+
execution_stats: ExecutionStats,
6265
) -> Vec<AdapterApcWithStats<Self::Adapter>>;
6366

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

autoprecompiles/src/lib.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ impl ColumnAllocator {
410410
}
411411

412412
#[derive(Serialize, Deserialize)]
413-
pub struct JsonExport {
413+
pub struct ExecutionStats {
414414
pub air_id_by_pc: BTreeMap<u32, usize>,
415415
pub column_names_by_air_id: BTreeMap<usize, Vec<String>>,
416416
pub column_ranges_by_pc: BTreeMap<u32, Vec<(u32, u32)>>,
@@ -422,6 +422,7 @@ pub fn build<A: Adapter>(
422422
vm_config: AdapterVmConfig<A>,
423423
degree_bound: DegreeBound,
424424
apc_candidates_dir_path: Option<&Path>,
425+
execution_stats: &ExecutionStats,
425426
) -> Result<AdapterApc<A>, crate::constraint_optimizer::Error> {
426427
let start = std::time::Instant::now();
427428

@@ -430,14 +431,8 @@ pub fn build<A: Adapter>(
430431
vm_config.instruction_handler,
431432
&vm_config.bus_map,
432433
);
433-
434-
let pgo_range_constraints_json = std::fs::read_to_string("pgo_range_constraints.json")
435-
.expect("Failed to read pgo range constraints json file");
436-
437-
let pgo_range_constraints: JsonExport = serde_json::from_str(&pgo_range_constraints_json)
438-
.expect("JSON format does not match JsonExport struct");
439-
let range_constraints = pgo_range_constraints.column_ranges_by_pc;
440-
let equivalence_classes_by_block = pgo_range_constraints.equivalence_classes_by_block;
434+
let range_constraints = &execution_stats.column_ranges_by_pc;
435+
let equivalence_classes_by_block = &execution_stats.equivalence_classes_by_block;
441436

442437
// Mapping (instruction index, column index) -> AlgebraicReference
443438
let reverse_subs = column_allocator

autoprecompiles/src/pgo/cell/mod.rs

Lines changed: 5 additions & 3 deletions
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+
ExecutionStats, PowdrConfig,
1717
};
1818

1919
mod selection;
@@ -78,8 +78,8 @@ impl<A, C> CellPgo<A, C> {
7878
}
7979

8080
#[derive(Serialize, Deserialize)]
81-
pub struct JsonExport {
82-
pub apcs: Vec<ApcCandidateJsonExport>,
81+
struct JsonExport {
82+
apcs: Vec<ApcCandidateJsonExport>,
8383
labels: BTreeMap<u64, Vec<String>>,
8484
}
8585

@@ -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+
execution_stats: ExecutionStats,
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+
&execution_stats,
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+
ExecutionStats, 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+
execution_stats: ExecutionStats,
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, execution_stats)
7475
}
7576

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

autoprecompiles/src/pgo/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ use strum::{Display, EnumString};
66
use crate::{
77
adapter::{Adapter, AdapterApcWithStats, AdapterVmConfig, ApcWithStats},
88
blocks::BasicBlock,
9-
PowdrConfig,
9+
ExecutionStats, PowdrConfig,
1010
};
1111

1212
mod cell;
1313
mod instruction;
1414
mod none;
1515

1616
pub use {
17-
cell::{ApcCandidateJsonExport, Candidate, CellPgo, JsonExport, KnapsackItem},
17+
cell::{ApcCandidateJsonExport, Candidate, CellPgo, KnapsackItem},
1818
instruction::InstructionPgo,
1919
none::NonePgo,
2020
};
@@ -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+
execution_stats: ExecutionStats,
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+
&execution_stats,
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+
ExecutionStats, 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+
execution_stats: ExecutionStats,
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, execution_stats)
4647
}
4748
}

openvm/src/customize_exe.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ pub fn customize<'a, P: PgoAdapter<Adapter = BabyBearOpenVmApcAdapter<'a>>>(
219219
})
220220
.collect();
221221

222-
execution_stats(
222+
let execution_stats = execution_stats(
223223
exe.clone(),
224224
SpecializedConfig::new(
225225
original_config.clone(),
@@ -232,7 +232,13 @@ pub fn customize<'a, P: PgoAdapter<Adapter = BabyBearOpenVmApcAdapter<'a>>>(
232232
.unwrap();
233233

234234
let start = std::time::Instant::now();
235-
let apcs = pgo.filter_blocks_and_create_apcs_with_pgo(blocks, &config, vm_config, labels);
235+
let apcs = pgo.filter_blocks_and_create_apcs_with_pgo(
236+
blocks,
237+
&config,
238+
vm_config,
239+
labels,
240+
execution_stats,
241+
);
236242
metrics::gauge!("total_apc_gen_time_ms").set(start.elapsed().as_millis() as f64);
237243

238244
let pc_base = exe.program.pc_base;

openvm/src/execution_stats.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use openvm_stark_sdk::config::FriParameters;
1919
use openvm_stark_sdk::openvm_stark_backend::p3_field::PrimeField32;
2020
use openvm_stark_sdk::p3_baby_bear::BabyBear;
2121
use powdr_autoprecompiles::blocks::BasicBlock;
22-
use powdr_autoprecompiles::JsonExport;
22+
use powdr_autoprecompiles::ExecutionStats;
2323
use std::collections::hash_map::Entry;
2424
use std::collections::BTreeMap;
2525
use std::{collections::HashMap, sync::Arc};
@@ -85,7 +85,7 @@ pub fn execution_stats(
8585
vm_config: SpecializedConfig,
8686
inputs: StdIn,
8787
blocks: &[BasicBlock<Instr<BabyBear>>],
88-
) -> Result<(), Box<dyn std::error::Error>> {
88+
) -> Result<ExecutionStats, Box<dyn std::error::Error>> {
8989
// Set app configuration
9090
let app_fri_params =
9191
FriParameters::standard_with_100_bits_conjectured_security(DEFAULT_APP_LOG_BLOWUP);
@@ -297,7 +297,7 @@ pub fn execution_stats(
297297
})
298298
.collect();
299299

300-
let export = JsonExport {
300+
let export = ExecutionStats {
301301
air_id_by_pc: air_id_by_pc.into_iter().collect(),
302302
column_names_by_air_id: column_names_by_air_id.into_iter().collect(),
303303
column_ranges_by_pc: column_ranges_by_pc.into_iter().collect(),
@@ -308,5 +308,5 @@ pub fn execution_stats(
308308
let json = serde_json::to_string_pretty(&export).unwrap();
309309
std::fs::write("pgo_range_constraints.json", json).unwrap();
310310

311-
Ok(())
311+
Ok(export)
312312
}

0 commit comments

Comments
 (0)