Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add statements->functions map to SierraDebugInfo #5325

Merged
merged 10 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions crates/bin/cairo-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn main() -> anyhow::Result<()> {
}

let SierraProgramWithDebug { program: sierra_program, debug_info } = Arc::unwrap_or_clone(
db.get_sierra_program(main_crate_ids.clone())
db.get_sierra_program(main_crate_ids.clone(), false)
.to_option()
.with_context(|| "Compilation failed without any diagnostics.")?,
);
Expand Down Expand Up @@ -97,7 +97,7 @@ fn main() -> anyhow::Result<()> {
let profiling_info_processor = ProfilingInfoProcessor::new(
Some(db),
sierra_program,
debug_info.statements_locations.get_statements_functions_map(db),
debug_info.statements_locations.get_statements_functions_map_for_tests(db),
);
match result.profiling_info {
Some(raw_profiling_info) => {
Expand Down
16 changes: 5 additions & 11 deletions crates/cairo-lang-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod diagnostics;
pub mod project;

/// Configuration for the compiler.
#[derive(Default)]
pub struct CompilerConfig<'c> {
pub diagnostics_reporter: DiagnosticsReporter<'c>,

Expand All @@ -31,17 +32,10 @@ pub struct CompilerConfig<'c> {
/// The name of the allowed libfuncs list to use in compilation.
/// If None the default list of audited libfuncs will be used.
pub allowed_libfuncs_list_name: Option<String>,
}

/// The default compiler configuration.
impl Default for CompilerConfig<'static> {
fn default() -> Self {
CompilerConfig {
diagnostics_reporter: DiagnosticsReporter::default(),
replace_ids: false,
allowed_libfuncs_list_name: None,
}
}
/// Adds mappings used by [cairo-profiler](https://github.com/software-mansion/cairo-profiler)
/// to debug info annotations.
pub add_location_annotations: bool,
}

/// Compiles a Cairo project at the given path.
Expand Down Expand Up @@ -123,7 +117,7 @@ pub fn compile_prepared_db(
compiler_config.diagnostics_reporter.ensure(db)?;

let mut sierra_program_with_debug = Arc::unwrap_or_clone(
db.get_sierra_program(main_crate_ids)
db.get_sierra_program(main_crate_ids, compiler_config.add_location_annotations)
.to_option()
.context("Compilation failed without any diagnostics")?,
);
Expand Down
5 changes: 3 additions & 2 deletions crates/cairo-lang-runner/src/profiling_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ pub fn test_profiling(

// Compile to Sierra.
let SierraProgramWithDebug { program: sierra_program, debug_info } =
Arc::unwrap_or_clone(db.get_sierra_program(vec![test_module.crate_id]).unwrap());
Arc::unwrap_or_clone(db.get_sierra_program(vec![test_module.crate_id], false).unwrap());
let sierra_program = replace_sierra_ids_in_program(&db, &sierra_program);
let statements_functions = debug_info.statements_locations.get_statements_functions_map(&db);
let statements_functions =
debug_info.statements_locations.get_statements_functions_map_for_tests(&db);
let runner = SierraCasmRunner::new(
sierra_program.clone(),
Some(Default::default()),
Expand Down
2 changes: 2 additions & 0 deletions crates/cairo-lang-sierra-generator/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,15 @@ pub trait SierraGenGroup: LoweringGroup + Upcast<dyn LoweringGroup> {
fn get_sierra_program_for_functions(
&self,
requested_function_ids: Vec<ConcreteFunctionWithBodyId>,
add_location_annotations: bool,
) -> Maybe<Arc<SierraProgramWithDebug>>;

/// Returns the [cairo_lang_sierra::program::Program] object of the requested crates.
#[salsa::invoke(program_generator::get_sierra_program)]
fn get_sierra_program(
&self,
requested_crate_ids: Vec<CrateId>,
add_location_annotations: bool,
) -> Maybe<Arc<SierraProgramWithDebug>>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ fn foo() -> @Array<felt252> {
foo

//! > module_code

// Function needs to be a real libfunc for the test
extern fn span_from_tuple<T, +Copy<T>>(x: Box<@T>) -> @Array<felt252> nopanic;

Expand Down
18 changes: 14 additions & 4 deletions crates/cairo-lang-sierra-generator/src/program_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use cairo_lang_debug::DebugWithDb;
use cairo_lang_diagnostics::{get_location_marks, Maybe};
use cairo_lang_filesystem::ids::CrateId;
use cairo_lang_lowering::ids::ConcreteFunctionWithBodyId;
use cairo_lang_sierra::debug_info::StatementsFunctions;
use cairo_lang_sierra::extensions::core::CoreLibfunc;
use cairo_lang_sierra::extensions::GenericLibfuncEx;
use cairo_lang_sierra::ids::{ConcreteLibfuncId, ConcreteTypeId};
Expand Down Expand Up @@ -222,11 +223,13 @@ impl DebugWithDb<dyn SierraGenGroup> for SierraProgramWithDebug {
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SierraProgramDebugInfo {
pub statements_locations: StatementsLocations,
pub statements_functions: Option<StatementsFunctions>,
}

pub fn get_sierra_program_for_functions(
db: &dyn SierraGenGroup,
requested_function_ids: Vec<ConcreteFunctionWithBodyId>,
add_location_annotations: bool,
) -> Maybe<Arc<SierraProgramWithDebug>> {
let mut functions: Vec<Arc<pre_sierra::Function>> = vec![];
let mut statements: Vec<pre_sierra::StatementWithLocation> = vec![];
Expand Down Expand Up @@ -274,11 +277,17 @@ pub fn get_sierra_program_for_functions(
})
.collect(),
};

let statements_locations = StatementsLocations::from_locations_vec(&statements_locations);
let statements_functions = if add_location_annotations {
Some(statements_locations.extract_statements_functions(db.upcast()))
} else {
None
};

Ok(Arc::new(SierraProgramWithDebug {
program,
debug_info: SierraProgramDebugInfo {
statements_locations: StatementsLocations::from_locations_vec(&statements_locations),
},
debug_info: SierraProgramDebugInfo { statements_locations, statements_functions },
}))
}

Expand Down Expand Up @@ -324,6 +333,7 @@ fn try_get_function_with_body_id(
pub fn get_sierra_program(
db: &dyn SierraGenGroup,
requested_crate_ids: Vec<CrateId>,
add_location_annotations: bool,
) -> Maybe<Arc<SierraProgramWithDebug>> {
let mut requested_function_ids = vec![];
for crate_id in requested_crate_ids {
Expand All @@ -338,5 +348,5 @@ pub fn get_sierra_program(
}
}
}
db.get_sierra_program_for_functions(requested_function_ids)
db.get_sierra_program_for_functions(requested_function_ids, add_location_annotations)
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn test_only_include_dependencies(func_name: &str, sierra_used_funcs: &[&str]) {
)
.unwrap();
let SierraProgramWithDebug { program, .. } =
Arc::unwrap_or_clone(db.get_sierra_program_for_functions(vec![func_id]).unwrap());
Arc::unwrap_or_clone(db.get_sierra_program_for_functions(vec![func_id], false).unwrap());
assert_eq!(
replace_sierra_ids_in_program(&db, &program)
.funcs
Expand Down
19 changes: 16 additions & 3 deletions crates/cairo-lang-sierra-generator/src/statements_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::diagnostic_utils::StableLocation;
use cairo_lang_diagnostics::ToOption;
use cairo_lang_filesystem::ids::{FileId, FileLongId, VirtualFile};
use cairo_lang_sierra::debug_info::StatementsFunctions;
use cairo_lang_sierra::program::StatementIdx;
use cairo_lang_syntax::node::{Terminal, TypedSyntaxNode};
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
Expand Down Expand Up @@ -138,14 +139,26 @@ impl StatementsLocations {
Self { locations }
}
/// Builds a map between each Sierra statement index and a string representation of the Cairo
/// function that it was generated from. The function representation is composed of the function
/// name and the path (modules and impls) to the function in the file. It is used for places
/// function that it was generated from. It is used for places
/// without db access such as the profiler.
// TODO(Gil): Add a db access to the profiler and remove this function.
pub fn get_statements_functions_map(
pub fn get_statements_functions_map_for_tests(
&self,
db: &dyn DefsGroup,
) -> UnorderedHashMap<StatementIdx, String> {
self.locations.map(|s| containing_function_identifier_for_tests(db, *s))
}

/// Creates a new [StatementsFunctions] struct using [StatementsLocations] and [DefsGroup].
pub fn extract_statements_functions(&self, db: &dyn DefsGroup) -> StatementsFunctions {
StatementsFunctions {
statements_to_functions_map: self
.locations
.iter_sorted()
.map(|(statement_idx, stable_location)| {
(*statement_idx, containing_function_identifier(db, *stable_location))
})
.collect(),
}
}
}
2 changes: 1 addition & 1 deletion crates/cairo-lang-sierra-generator/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub fn checked_compile_to_sierra(content: &str) -> cairo_lang_sierra::program::P
let (db, crate_id) = setup_db_and_get_crate_id(content);

let SierraProgramWithDebug { program, .. } =
Arc::unwrap_or_clone(db.get_sierra_program(vec![crate_id]).unwrap());
Arc::unwrap_or_clone(db.get_sierra_program(vec![crate_id], false).unwrap());
replace_sierra_ids_in_program(&db, &program)
}

Expand Down
20 changes: 19 additions & 1 deletion crates/cairo-lang-sierra/src/debug_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
use smol_str::SmolStr;

use crate::ids::{ConcreteLibfuncId, ConcreteTypeId, FunctionId};
use crate::program::{GenericArg, Program, Statement};
use crate::program::{GenericArg, Program, Statement, StatementIdx};

#[cfg(test)]
#[path = "debug_info_test.rs"]
Expand Down Expand Up @@ -61,6 +61,24 @@ pub struct DebugInfo {
/// - `scarb.swmansion.com/build-info/v1`
pub type Annotations = OrderedHashMap<String, serde_json::Value>;

/// The mapping from sierra statement index to fully qualified Cairo path of the Cairo function
/// (if obtainable) which caused the statement to be generated.
/// Should be created using [`StatementsLocation::extract_statements_functions`].
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct StatementsFunctions {
pub statements_to_functions_map: HashMap<StatementIdx, Option<String>>,
}

impl From<StatementsFunctions> for Annotations {
fn from(value: StatementsFunctions) -> Self {
let mapping = serde_json::to_value(value.statements_to_functions_map).unwrap();
OrderedHashMap::from([(
"github.com/software-mansion/cairo-profiler".to_string(),
serde_json::Value::from_iter([("statements_functions", mapping)]),
)])
}
}

impl DebugInfo {
/// Extracts the existing debug info from a program.
pub fn extract(program: &Program) -> Self {
Expand Down
8 changes: 7 additions & 1 deletion crates/cairo-lang-starknet-classes/src/contract_class.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cairo_lang_sierra as sierra;
use cairo_lang_sierra::debug_info::{Annotations, StatementsFunctions};
use cairo_lang_utils::bigint::{deserialize_big_uint, serialize_big_uint, BigUintAsHex};
use num_bigint::BigUint;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -36,14 +37,19 @@ impl ContractClass {
program: &sierra::program::Program,
entry_points_by_type: ContractEntryPoints,
abi: Option<Contract>,
statements_functions: Option<StatementsFunctions>,
) -> Result<Self, Felt252SerdeError> {
let mut sierra_program_debug_info = sierra::debug_info::DebugInfo::extract(program);
let location_annotations = Annotations::from(statements_functions.unwrap_or_default());
sierra_program_debug_info.annotations.extend(location_annotations);

Ok(Self {
sierra_program: sierra_to_felt252s(
current_sierra_version_id(),
current_compiler_version_id(),
program,
)?,
sierra_program_debug_info: Some(sierra::debug_info::DebugInfo::extract(program)),
sierra_program_debug_info: Some(sierra_program_debug_info),
contract_class_version: DEFAULT_CONTRACT_CLASS_VERSION.into(),
entry_points_by_type,
abi,
Expand Down
4 changes: 3 additions & 1 deletion crates/cairo-lang-starknet/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ fn compile_contract_with_prepared_and_checked_db(
) -> Result<ContractClass> {
let SemanticEntryPoints { external, l1_handler, constructor } =
extract_semantic_entrypoints(db, contract)?;
let SierraProgramWithDebug { program: mut sierra_program, .. } = Arc::unwrap_or_clone(
let SierraProgramWithDebug { program: mut sierra_program, debug_info } = Arc::unwrap_or_clone(
db.get_sierra_program_for_functions(
chain!(&external, &l1_handler, &constructor).map(|f| f.value).collect(),
compiler_config.add_location_annotations,
)
.to_option()
.with_context(|| "Compilation failed without any diagnostics.")?,
Expand Down Expand Up @@ -156,6 +157,7 @@ fn compile_contract_with_prepared_and_checked_db(
.finalize()
.with_context(|| "Could not create ABI from contract submodule")?,
),
debug_info.statements_functions,
)?;
contract_class.sanity_check();
Ok(contract_class)
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-starknet/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub fn get_test_contract(example_file_name: &str) -> ContractClass {
replace_ids: true,
allowed_libfuncs_list_name: Some(BUILTIN_ALL_LIBFUNCS_LIST.to_string()),
diagnostics_reporter,
add_location_annotations: false,
},
)
.expect("compile_path failed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2412,7 +2412,12 @@
13,
"core::array::SpanFelt252Serde::deserialize"
]
]
],
"annotations": {
"github.com/software-mansion/cairo-profiler": {
"statements_functions": {}
}
}
},
"contract_class_version": "0.1.0",
"entry_points_by_type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3200,7 +3200,12 @@
19,
"cairo_level_tests::contracts::erc20::erc_20::EventIsEvent::append_keys_and_data"
]
]
],
"annotations": {
"github.com/software-mansion/cairo-profiler": {
"statements_functions": {}
}
}
},
"contract_class_version": "0.1.0",
"entry_points_by_type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,12 @@
2,
"cairo_level_tests::contracts::hello_starknet::hello_starknet::HelloStarknetImpl::increase_balance"
]
]
],
"annotations": {
"github.com/software-mansion/cairo-profiler": {
"statements_functions": {}
}
}
},
"contract_class_version": "0.1.0",
"entry_points_by_type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,12 @@
0,
"cairo_level_tests::contracts::minimal_contract::minimal_contract::__wrapper__empty"
]
]
],
"annotations": {
"github.com/software-mansion/cairo-profiler": {
"statements_functions": {}
}
}
},
"contract_class_version": "0.1.0",
"entry_points_by_type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4096,7 +4096,12 @@
26,
"cairo_level_tests::components::erc20::erc20::EventIsEvent::append_keys_and_data"
]
]
],
"annotations": {
"github.com/software-mansion/cairo-profiler": {
"statements_functions": {}
}
}
},
"contract_class_version": "0.1.0",
"entry_points_by_type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4432,7 +4432,12 @@
29,
"cairo_level_tests::components::erc20::erc20::EventIsEvent::append_keys_and_data"
]
]
],
"annotations": {
"github.com/software-mansion/cairo-profiler": {
"statements_functions": {}
}
}
},
"contract_class_version": "0.1.0",
"entry_points_by_type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,12 @@
7,
"cairo_level_tests::contracts::new_syntax_test_contract::counter_contract::ContractStateEventEmitter::emit::<cairo_level_tests::contracts::new_syntax_test_contract::counter_contract::CounterDecreased, cairo_level_tests::contracts::new_syntax_test_contract::counter_contract::EventCounterDecreasedIntoEvent>"
]
]
],
"annotations": {
"github.com/software-mansion/cairo-profiler": {
"statements_functions": {}
}
}
},
"contract_class_version": "0.1.0",
"entry_points_by_type": {
Expand Down
Loading