Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/lean_compiler/src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Display for SemanticError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)?;
if let Some(context) = &self.context {
write!(f, " (in {})", context)?;
write!(f, " (in {context})")?;
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/lean_compiler/src/parser/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn next_inner<'i>(
expected: &str,
) -> Option<ParsePair<'i>> {
pairs.next().or_else(|| {
eprintln!("Warning: Expected {} but found nothing", expected);
eprintln!("Warning: Expected {expected} but found nothing");
None
})
}
Expand Down
4 changes: 2 additions & 2 deletions crates/lean_compiler/src/parser/parsers/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Parse<(String, usize)> for ConstantDeclarationParser {
)
.ok_or_else(|| {
SemanticError::with_context(
format!("Failed to evaluate constant: {}", name),
format!("Failed to evaluate constant: {name}"),
"constant declaration",
)
})?
Expand Down Expand Up @@ -123,7 +123,7 @@ impl Parse<usize> for ConstExprParser {
Ok(value)
} else {
Err(SemanticError::with_context(
format!("Invalid constant expression in match pattern: {}", text),
format!("Invalid constant expression in match pattern: {text}"),
"match pattern",
)
.into())
Expand Down
2 changes: 1 addition & 1 deletion crates/lean_compiler/src/parser/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl ParseContext {
pub fn add_constant(&mut self, name: String, value: usize) -> Result<(), SemanticError> {
if self.constants.insert(name.clone(), value).is_some() {
Err(SemanticError::with_context(
format!("Multiply defined constant: {}", name),
format!("Multiply defined constant: {name}"),
"constant declaration",
))
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/lean_compiler/src/parser/parsers/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Parse<(Program, BTreeMap<usize, String>)> for ProgramParser {

if functions.insert(name.clone(), function).is_some() {
return Err(SemanticError::with_context(
format!("Multiply defined function: {}", name),
format!("Multiply defined function: {name}"),
"function definition",
)
.into());
Expand Down
2 changes: 1 addition & 1 deletion crates/lean_vm/src/diagnostics/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) fn profiling_report(
let mut call_stack: Vec<String> = Vec::new();
let mut prev_function_name = String::new();

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

if prev_function_name != current_function_name {
Expand Down
6 changes: 3 additions & 3 deletions crates/lean_vm/src/execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::BTreeMap;
#[derive(Debug, Clone, Default)]
pub struct ExecutionHistory {
pub lines: Vec<SourceLineNumber>,
pub cycles: Vec<usize>, // for each line, how many cycles it took
pub lines_cycles: Vec<usize>, // for each line, how many cycles it took
}

impl ExecutionHistory {
Expand All @@ -14,11 +14,11 @@ impl ExecutionHistory {

pub fn add_line(&mut self, location: SourceLineNumber, cycles: usize) {
self.lines.push(location);
self.cycles.push(cycles);
self.lines_cycles.push(cycles);
}

pub fn total_cycles(&self) -> usize {
self.cycles.iter().sum()
self.lines_cycles.iter().sum()
}

pub const fn len(&self) -> usize {
Expand Down
48 changes: 45 additions & 3 deletions crates/lean_vm/src/execution/runner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! VM execution runner

use crate::HintExecutionContext;
use crate::core::{
DIMENSION, F, ONE_VEC_PTR, POSEIDON_16_NULL_HASH_PTR, POSEIDON_24_NULL_HASH_PTR,
PUBLIC_INPUT_START, VECTOR_LEN, ZERO_VEC_PTR,
Expand All @@ -12,6 +11,7 @@ use crate::isa::instruction::InstructionContext;
use crate::witness::{
WitnessDotProduct, WitnessMultilinearEval, WitnessPoseidon16, WitnessPoseidon24,
};
use crate::{CodeAddress, HintExecutionContext, SourceLineNumber};
use p3_field::PrimeCharacteristicRing;
use p3_symmetric::Permutation;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn execute_bytecode(
) -> ExecutionResult {
let mut std_out = String::new();
let mut instruction_history = ExecutionHistory::new();
execute_bytecode_helper(
let result = execute_bytecode_helper(
bytecode,
public_input,
private_input,
Expand Down Expand Up @@ -101,7 +101,49 @@ pub fn execute_bytecode(
print!("{std_out}");
}
panic!("Error during bytecode execution: {err}");
})
});
if profiler {
print_line_cycle_counts(instruction_history);
print_instruction_cycle_counts(bytecode, result.pcs.clone());
}
result
}

fn print_line_cycle_counts(history: ExecutionHistory) {
println!("Line by line cycle counts");
println!("=========================\n");

let mut gross_cycle_counts: BTreeMap<SourceLineNumber, usize> = BTreeMap::new();
for (line, cycle_count) in history.lines.iter().zip(history.lines_cycles.iter()) {
let prev_count = gross_cycle_counts.get(line).unwrap_or(&0);
gross_cycle_counts.insert(*line, *prev_count + cycle_count);
}
for (line, cycle_count) in gross_cycle_counts.iter() {
println!("line {line}: {cycle_count} cycles");
}
println!();
}

fn print_instruction_cycle_counts(bytecode: &Bytecode, pcs: Vec<CodeAddress>) {
println!("Instruction level cycle counts");
println!("==============================");

let mut gross_cycle_counts: BTreeMap<CodeAddress, usize> = BTreeMap::new();
for pc in pcs.iter() {
let prev_count = gross_cycle_counts.get(pc).unwrap_or(&0);
gross_cycle_counts.insert(*pc, *prev_count + 1);
}
for (pc, cycle_count) in gross_cycle_counts.iter() {
let instruction = &bytecode.instructions[*pc];
let hints = bytecode.hints.get(pc);
if let Some(hints) = hints {
for hint in hints {
println!("hint: {hint}");
}
}
println!("pc {pc}: {cycle_count} cycles: {instruction}");
}
println!();
}

/// Helper function that performs the actual bytecode execution
Expand Down
2 changes: 1 addition & 1 deletion crates/lean_vm/src/isa/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl Hint {
Self::LocationReport { location } => {
ctx.instruction_history.lines.push(*location);
ctx.instruction_history
.cycles
.lines_cycles
.push(*ctx.cpu_cycles_before_new_line);
*ctx.cpu_cycles_before_new_line = 0;
}
Expand Down
Loading