restore pgo API (#2941)

We need the `CompiledProgram` entry point because in the reth benchmark
we already have the elf binary
This commit is contained in:
Leo
2025-06-24 11:17:22 +02:00
committed by GitHub
parent 81a099c3db
commit 63c1944d34
2 changed files with 35 additions and 16 deletions

View File

@@ -104,7 +104,7 @@ fn run_command(command: Commands) {
input,
} => {
let powdr_config = PowdrConfig::new(autoprecompiles as u64, skip as u64);
let pgo_config = get_pgo_config(guest.clone(), guest_opts.clone(), pgo, input);
let pgo_config = pgo_config(guest.clone(), guest_opts.clone(), pgo, input);
let program =
powdr_openvm::compile_guest(&guest, guest_opts, powdr_config, pgo_config).unwrap();
write_program_to_file(program, &format!("{guest}_compiled.cbor")).unwrap();
@@ -118,7 +118,7 @@ fn run_command(command: Commands) {
input,
} => {
let powdr_config = PowdrConfig::new(autoprecompiles as u64, skip as u64);
let pgo_config = get_pgo_config(guest.clone(), guest_opts.clone(), pgo, input);
let pgo_config = pgo_config(guest.clone(), guest_opts.clone(), pgo, input);
let program =
powdr_openvm::compile_guest(&guest, guest_opts, powdr_config, pgo_config).unwrap();
powdr_openvm::execute(program, stdin_from(input)).unwrap();
@@ -135,7 +135,7 @@ fn run_command(command: Commands) {
metrics,
} => {
let powdr_config = PowdrConfig::new(autoprecompiles as u64, skip as u64);
let pgo_config = get_pgo_config(guest.clone(), guest_opts.clone(), pgo, input);
let pgo_config = pgo_config(guest.clone(), guest_opts.clone(), pgo, input);
let program =
powdr_openvm::compile_guest(&guest, guest_opts, powdr_config, pgo_config).unwrap();
let prove =
@@ -168,7 +168,7 @@ fn stdin_from(input: Option<u32>) -> StdIn {
s
}
fn get_pgo_config(
fn pgo_config(
guest: String,
guest_opts: GuestOptions,
pgo: PgoType,
@@ -176,13 +176,19 @@ fn get_pgo_config(
) -> PgoConfig {
match pgo {
PgoType::Cell => {
let pc_idx_count =
powdr_openvm::get_pc_idx_count(&guest, guest_opts.clone(), stdin_from(input));
let pc_idx_count = powdr_openvm::execution_profile_from_guest(
&guest,
guest_opts.clone(),
stdin_from(input),
);
PgoConfig::Cell(pc_idx_count)
}
PgoType::Instruction => {
let pc_idx_count =
powdr_openvm::get_pc_idx_count(&guest, guest_opts.clone(), stdin_from(input));
let pc_idx_count = powdr_openvm::execution_profile_from_guest(
&guest,
guest_opts.clone(),
stdin_from(input),
);
PgoConfig::Instruction(pc_idx_count)
}
PgoType::None => PgoConfig::None,

View File

@@ -600,10 +600,20 @@ pub fn prove(
Ok(())
}
// Same as execution_profile below but for guest path inputs.
pub fn execution_profile_from_guest(
guest: &str,
guest_opts: GuestOptions,
inputs: StdIn,
) -> HashMap<u32, u32> {
let program = compile_openvm(guest, guest_opts).unwrap();
execution_profile(program, inputs)
}
// Produces execution count by pc_index
// Used in Pgo::Cell and Pgo::Instruction to help rank basic blocks to create APCs for
pub fn get_pc_idx_count(guest: &str, guest_opts: GuestOptions, inputs: StdIn) -> HashMap<u32, u32> {
let OriginalCompiledProgram { exe, sdk_vm_config } = compile_openvm(guest, guest_opts).unwrap();
pub fn execution_profile(program: OriginalCompiledProgram, inputs: StdIn) -> HashMap<u32, u32> {
let OriginalCompiledProgram { exe, sdk_vm_config } = program;
// in memory collector storage
let collected = Arc::new(Mutex::new(Vec::new()));
@@ -821,7 +831,7 @@ mod tests {
let mut stdin = StdIn::default();
stdin.write(&GUEST_ITER);
let config = PowdrConfig::new(GUEST_APC, GUEST_SKIP);
let pgo_data = get_pc_idx_count(GUEST, GuestOptions::default(), stdin.clone());
let pgo_data = execution_profile_from_guest(GUEST, GuestOptions::default(), stdin.clone());
prove_recursion(GUEST, config, stdin, PgoConfig::Instruction(pgo_data), None);
}
@@ -857,7 +867,8 @@ mod tests {
fn keccak_prove_many_apcs() {
let mut stdin = StdIn::default();
stdin.write(&GUEST_KECCAK_ITER);
let pgo_data = get_pc_idx_count(GUEST_KECCAK, GuestOptions::default(), stdin.clone());
let pgo_data =
execution_profile_from_guest(GUEST_KECCAK, GuestOptions::default(), stdin.clone());
let config = PowdrConfig::new(GUEST_KECCAK_APC_PGO_LARGE, GUEST_KECCAK_SKIP);
prove_recursion(
@@ -882,7 +893,8 @@ mod tests {
fn keccak_prove_large() {
let mut stdin = StdIn::default();
stdin.write(&GUEST_KECCAK_ITER_LARGE);
let pgo_data = get_pc_idx_count(GUEST_KECCAK, GuestOptions::default(), stdin.clone());
let pgo_data =
execution_profile_from_guest(GUEST_KECCAK, GuestOptions::default(), stdin.clone());
let config = PowdrConfig::new(GUEST_KECCAK_APC_PGO, GUEST_KECCAK_SKIP);
prove_recursion(
@@ -932,7 +944,8 @@ mod tests {
let config = PowdrConfig::new(GUEST_KECCAK_APC_PGO, GUEST_KECCAK_SKIP);
// Pgo data
let pgo_data = get_pc_idx_count(GUEST_KECCAK, GuestOptions::default(), stdin.clone());
let pgo_data =
execution_profile_from_guest(GUEST_KECCAK, GuestOptions::default(), stdin.clone());
// Pgo Cell mode
let start = Instant::now();
@@ -997,7 +1010,7 @@ mod tests {
fn guest_machine_pgo() {
let mut stdin = StdIn::default();
stdin.write(&GUEST_ITER);
let pgo_data = get_pc_idx_count(GUEST, GuestOptions::default(), stdin);
let pgo_data = execution_profile_from_guest(GUEST, GuestOptions::default(), stdin);
test_guest_machine(PgoConfig::Instruction(pgo_data.clone()));
test_guest_machine(PgoConfig::Cell(pgo_data));
}
@@ -1025,7 +1038,7 @@ mod tests {
fn keccak_machine_pgo() {
let mut stdin = StdIn::default();
stdin.write(&GUEST_KECCAK_ITER_SMALL);
let pgo_data = get_pc_idx_count(GUEST_KECCAK, GuestOptions::default(), stdin);
let pgo_data = execution_profile_from_guest(GUEST_KECCAK, GuestOptions::default(), stdin);
test_keccak_machine(PgoConfig::Instruction(pgo_data.clone()));
test_keccak_machine(PgoConfig::Cell(pgo_data));
}