Skip to content

Commit a557c7d

Browse files
committed
print instruction level cycle counts
1 parent 8dd68b0 commit a557c7d

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

crates/lean_vm/src/diagnostics/profiler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub(crate) fn profiling_report(
2121
let mut call_stack: Vec<String> = Vec::new();
2222
let mut prev_function_name = String::new();
2323

24-
for (&line_num, &cycle_count) in instructions.lines.iter().zip(&instructions.cycles) {
24+
for (&line_num, &cycle_count) in instructions.lines.iter().zip(&instructions.lines_cycles) {
2525
let (_, current_function_name) = find_function_for_line(line_num, function_locations);
2626

2727
if prev_function_name != current_function_name {

crates/lean_vm/src/execution/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::core::SourceLineNumber;
1+
use crate::core::{SourceLineNumber};
22
use std::collections::BTreeMap;
33

44
#[derive(Debug, Clone, Default)]
55
pub struct ExecutionHistory {
66
pub lines: Vec<SourceLineNumber>,
7-
pub cycles: Vec<usize>, // for each line, how many cycles it took
7+
pub lines_cycles: Vec<usize>, // for each line, how many cycles it took
88
}
99

1010
impl ExecutionHistory {
@@ -14,11 +14,11 @@ impl ExecutionHistory {
1414

1515
pub fn add_line(&mut self, location: SourceLineNumber, cycles: usize) {
1616
self.lines.push(location);
17-
self.cycles.push(cycles);
17+
self.lines_cycles.push(cycles);
1818
}
1919

2020
pub fn total_cycles(&self) -> usize {
21-
self.cycles.iter().sum()
21+
self.lines_cycles.iter().sum()
2222
}
2323

2424
pub const fn len(&self) -> usize {

crates/lean_vm/src/execution/runner.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! VM execution runner
22
3-
use crate::{SourceLineNumber, HintExecutionContext};
3+
use crate::{CodeAddress, 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,
@@ -104,23 +104,50 @@ pub fn execute_bytecode(
104104
panic!("Error during bytecode execution: {err}");
105105
});
106106
if profiler {
107-
print_line_cycle_counts(bytecode, instruction_history);
107+
print_line_cycle_counts(instruction_history);
108+
print_instruction_cycle_counts(bytecode, result.pcs.clone());
108109
}
109110
result
110111
}
111112

112-
fn print_line_cycle_counts(bytecode: &Bytecode, history: ExecutionHistory) {
113+
fn print_line_cycle_counts(history: ExecutionHistory) {
113114
println!("Line by line cycle counts");
114115
println!("=========================\n");
115116

116117
let mut gross_cycle_counts: BTreeMap<SourceLineNumber, usize> = BTreeMap::new();
117-
for (line, cycle_count) in history.lines.iter().zip(history.cycles.iter()) {
118+
for (line, cycle_count) in history.lines.iter().zip(history.lines_cycles.iter()) {
118119
let prev_count = gross_cycle_counts.get(line).unwrap_or(&0);
119120
gross_cycle_counts.insert(*line, *prev_count + cycle_count);
120121
}
121122
for (line, cycle_count) in gross_cycle_counts.iter() {
122123
println!("line {line}: {cycle_count} cycles");
123124
}
125+
println!("");
126+
}
127+
128+
fn print_instruction_cycle_counts(bytecode: &Bytecode, pcs: Vec<CodeAddress>) {
129+
println!("Instruction level cycle counts");
130+
println!("==============================");
131+
132+
let mut gross_cycle_counts: BTreeMap<CodeAddress, usize> = BTreeMap::new();
133+
for pc in pcs.iter() {
134+
let prev_count = gross_cycle_counts.get(pc).unwrap_or(&0);
135+
gross_cycle_counts.insert(*pc, *prev_count + 1);
136+
}
137+
for (pc, cycle_count) in gross_cycle_counts.iter() {
138+
let instruction = &bytecode.instructions[*pc];
139+
let hints = bytecode.hints.get(pc);
140+
match hints {
141+
Some(hints) => {
142+
for hint in hints {
143+
println!("hint: {hint}");
144+
}
145+
},
146+
None => {},
147+
}
148+
println!("pc {pc}: {cycle_count} cycles: {instruction}");
149+
}
150+
println!("");
124151
}
125152

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

crates/lean_vm/src/isa/hint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl Hint {
187187
Self::LocationReport { location } => {
188188
ctx.instruction_history.lines.push(*location);
189189
ctx.instruction_history
190-
.cycles
190+
.lines_cycles
191191
.push(*ctx.cpu_cycles_before_new_line);
192192
*ctx.cpu_cycles_before_new_line = 0;
193193
}

0 commit comments

Comments
 (0)