Skip to content

Commit

Permalink
Add squashing of sierra_ids
Browse files Browse the repository at this point in the history
commit-id:aa709ff1
  • Loading branch information
ksew1 committed Aug 27, 2024
1 parent 6b1f8f6 commit 244ee60
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/cairo-coverage/src/input/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl TryFrom<&Utf8PathBuf> for InputData {

let statements_map = create_statements_map(&annotations)?;
let casm = compile_to_casm(&program)?;
let executed_sierra_ids = ExecutedSierraIds::new(&casm, &call_trace)?;
let executed_sierra_ids = ExecutedSierraIds::new(&casm, &call_trace, &statements_map)?;

Ok(Self {
executed_sierra_ids,
Expand Down
69 changes: 51 additions & 18 deletions crates/cairo-coverage/src/input/mappings/executed_sierra_ids.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::input::StatementsMap;
use anyhow::{Context, Result};
use cairo_lang_sierra::program::StatementIdx;
use cairo_lang_sierra_to_casm::compiler::CairoProgram;
Expand All @@ -9,7 +10,11 @@ use trace_data::{CallTrace, CasmLevelInfo};
pub struct ExecutedSierraIds(HashSet<StatementIdx>);

impl ExecutedSierraIds {
pub fn new(casm: &CairoProgram, call_trace: &CallTrace) -> Result<Self> {
pub fn new(
casm: &CairoProgram,
call_trace: &CallTrace,
statements_map: &StatementsMap,
) -> Result<Self> {
let CasmLevelInfo {
run_with_call_header,
vm_trace,
Expand All @@ -23,22 +28,50 @@ impl ExecutedSierraIds {
.then(|| vm_trace.last().map_or(1, |trace| trace.pc + 1))
.unwrap_or(1);

Ok(Self(
vm_trace
.iter()
.map(|step| step.pc)
.filter(|pc| pc >= &real_minimal_pc)
.map(|pc| {
let real_pc_code_offset = pc - real_minimal_pc;
casm.debug_info
.sierra_statement_info
.partition_point(|debug_info| {
debug_info.start_offset <= real_pc_code_offset
})
- 1
})
.map(StatementIdx)
.collect(),
))
Ok(vm_trace
.iter()
.map(|step| step.pc)
.filter(|pc| pc >= &real_minimal_pc)
.map(|pc| {
let real_pc_code_offset = pc - real_minimal_pc;
casm.debug_info
.sierra_statement_info
.partition_point(|debug_info| debug_info.start_offset <= real_pc_code_offset)
- 1
})
.map(StatementIdx)
.squash(statements_map))
}
}

trait Squash {
fn squash(self, statements_map: &StatementsMap) -> ExecutedSierraIds;
}

impl<I> Squash for I
where
I: Iterator<Item = StatementIdx>,
{
fn squash(self, statements_map: &StatementsMap) -> ExecutedSierraIds {
ExecutedSierraIds(
self.fold(Vec::new(), |mut acc, statement_idx| {
if points_to_different_statement(statements_map, acc.last(), statement_idx) {
acc.push(statement_idx);
}
acc
})
.into_iter()
.collect(),
)
}
}

fn points_to_different_statement(
statements_map: &StatementsMap,
last_idx: Option<&StatementIdx>,
current_idx: StatementIdx,
) -> bool {
last_idx.map_or(true, |last_idx| {
statements_map.get(last_idx) != statements_map.get(&current_idx)
})
}
1 change: 1 addition & 0 deletions crates/cairo-coverage/src/input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod data;
mod data_loader;
mod mappings;
pub use mappings::StatementsMap;

0 comments on commit 244ee60

Please sign in to comment.