Skip to content

Commit 8dd68b0

Browse files
committed
print line by line cycle counts in execution history
1 parent 20cc639 commit 8dd68b0

File tree

1 file changed

+50
-31
lines changed

1 file changed

+50
-31
lines changed

crates/lean_vm/src/execution/runner.rs

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! VM execution runner
22
3-
use crate::HintExecutionContext;
3+
use crate::{SourceLineNumber, HintExecutionContext};
44
use crate::core::{
55
DIMENSION, F, ONE_VEC_PTR, POSEIDON_16_NULL_HASH_PTR, POSEIDON_24_NULL_HASH_PTR,
66
PUBLIC_INPUT_START, VECTOR_LEN, ZERO_VEC_PTR,
@@ -72,36 +72,55 @@ pub fn execute_bytecode(
7272
) -> ExecutionResult {
7373
let mut std_out = String::new();
7474
let mut instruction_history = ExecutionHistory::new();
75-
execute_bytecode_helper(
76-
bytecode,
77-
public_input,
78-
private_input,
79-
no_vec_runtime_memory,
80-
&mut std_out,
81-
&mut instruction_history,
82-
profiler,
83-
function_locations,
84-
)
85-
.unwrap_or_else(|err| {
86-
let lines_history = &instruction_history.lines;
87-
let latest_instructions =
88-
&lines_history[lines_history.len().saturating_sub(STACK_TRACE_INSTRUCTIONS)..];
89-
println!(
90-
"\n{}",
91-
crate::diagnostics::pretty_stack_trace(
92-
source_code,
93-
latest_instructions,
94-
function_locations
95-
)
96-
);
97-
if !std_out.is_empty() {
98-
println!("╔══════════════════════════════════════════════════════════════╗");
99-
println!("║ STD-OUT ║");
100-
println!("╚══════════════════════════════════════════════════════════════╝\n");
101-
print!("{std_out}");
102-
}
103-
panic!("Error during bytecode execution: {err}");
104-
})
75+
let result =
76+
execute_bytecode_helper(
77+
bytecode,
78+
public_input,
79+
private_input,
80+
no_vec_runtime_memory,
81+
&mut std_out,
82+
&mut instruction_history,
83+
profiler,
84+
function_locations,
85+
)
86+
.unwrap_or_else(|err| {
87+
let lines_history = &instruction_history.lines;
88+
let latest_instructions =
89+
&lines_history[lines_history.len().saturating_sub(STACK_TRACE_INSTRUCTIONS)..];
90+
println!(
91+
"\n{}",
92+
crate::diagnostics::pretty_stack_trace(
93+
source_code,
94+
latest_instructions,
95+
function_locations
96+
)
97+
);
98+
if !std_out.is_empty() {
99+
println!("╔══════════════════════════════════════════════════════════════╗");
100+
println!("║ STD-OUT ║");
101+
println!("╚══════════════════════════════════════════════════════════════╝\n");
102+
print!("{std_out}");
103+
}
104+
panic!("Error during bytecode execution: {err}");
105+
});
106+
if profiler {
107+
print_line_cycle_counts(bytecode, instruction_history);
108+
}
109+
result
110+
}
111+
112+
fn print_line_cycle_counts(bytecode: &Bytecode, history: ExecutionHistory) {
113+
println!("Line by line cycle counts");
114+
println!("=========================\n");
115+
116+
let mut gross_cycle_counts: BTreeMap<SourceLineNumber, usize> = BTreeMap::new();
117+
for (line, cycle_count) in history.lines.iter().zip(history.cycles.iter()) {
118+
let prev_count = gross_cycle_counts.get(line).unwrap_or(&0);
119+
gross_cycle_counts.insert(*line, *prev_count + cycle_count);
120+
}
121+
for (line, cycle_count) in gross_cycle_counts.iter() {
122+
println!("line {line}: {cycle_count} cycles");
123+
}
105124
}
106125

107126
/// Helper function that performs the actual bytecode execution

0 commit comments

Comments
 (0)