Skip to content

Commit

Permalink
Addedd SierraProgramWithDebug (#4994)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvalsw authored Feb 4, 2024
1 parent e5b2200 commit 709f0c2
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 50 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions crates/bin/cairo-run/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ description = "Runner executable for the Cairo programming language"
anyhow.workspace = true
clap.workspace = true

cairo-lang-runner = { path = "../../cairo-lang-runner", version = "2.5.3" }
cairo-lang-starknet = { path = "../../cairo-lang-starknet", version = "2.5.3" }
cairo-lang-compiler = { path = "../../cairo-lang-compiler", version = "2.5.3" }
cairo-lang-diagnostics = { path = "../../cairo-lang-diagnostics", version = "2.5.3" }
cairo-lang-runner = { path = "../../cairo-lang-runner", version = "2.5.3" }
cairo-lang-sierra-generator = { path = "../../cairo-lang-sierra-generator", version = "2.5.3" }
cairo-lang-compiler = { path = "../../cairo-lang-compiler", version = "2.5.3" }
cairo-lang-starknet = { path = "../../cairo-lang-starknet", version = "2.5.3" }
cairo-lang-utils = { path = "../../cairo-lang-utils", version = "2.5.3" }
13 changes: 8 additions & 5 deletions crates/bin/cairo-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ use cairo_lang_runner::profiling::ProfilingInfoProcessor;
use cairo_lang_runner::short_string::as_cairo_short_string;
use cairo_lang_runner::{SierraCasmRunner, StarknetState};
use cairo_lang_sierra_generator::db::SierraGenGroup;
use cairo_lang_sierra_generator::program_generator::SierraProgramWithDebug;
use cairo_lang_sierra_generator::replace_ids::{DebugReplacer, SierraIdReplacer};
use cairo_lang_starknet::contract::get_contracts_info;
use cairo_lang_utils::arc_unwrap_or_clone;
use clap::Parser;

/// Command line args parser.
Expand Down Expand Up @@ -57,10 +59,11 @@ fn main() -> anyhow::Result<()> {
anyhow::bail!("failed to compile: {}", args.path.display());
}

let (sierra_program, statements_locations) = db
.get_sierra_program(main_crate_ids.clone())
.to_option()
.with_context(|| "Compilation failed without any diagnostics.")?;
let SierraProgramWithDebug { program: sierra_program, debug_info } = arc_unwrap_or_clone(
db.get_sierra_program(main_crate_ids.clone())
.to_option()
.with_context(|| "Compilation failed without any diagnostics.")?,
);
let replacer = DebugReplacer { db };
if args.available_gas.is_none() && sierra_program.requires_gas_counter() {
anyhow::bail!("Program requires gas counter, please provide `--available-gas` argument.");
Expand Down Expand Up @@ -89,7 +92,7 @@ fn main() -> anyhow::Result<()> {
let profiling_info_processor = ProfilingInfoProcessor::new(
Some(db),
sierra_program,
statements_locations.get_statements_functions_map(db),
debug_info.statements_locations.get_statements_functions_map(db),
);
match result.profiling_info {
Some(raw_profiling_info) => {
Expand Down
15 changes: 7 additions & 8 deletions crates/cairo-lang-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
//! This crate is responsible for compiling a Cairo project into a Sierra program.
//! It is the main entry point for the compiler.
use std::path::Path;
use std::sync::Arc;

use ::cairo_lang_diagnostics::ToOption;
use anyhow::{Context, Result};
use cairo_lang_filesystem::ids::CrateId;
use cairo_lang_sierra::program::Program;
use cairo_lang_sierra_generator::db::SierraGenGroup;
use cairo_lang_sierra_generator::program_generator::SierraProgramWithDebug;
use cairo_lang_sierra_generator::replace_ids::replace_sierra_ids_in_program;
use cairo_lang_utils::arc_unwrap_or_clone;

use crate::db::RootDatabase;
use crate::diagnostics::DiagnosticsReporter;
Expand Down Expand Up @@ -99,13 +100,11 @@ pub fn compile_prepared_db(
) -> Result<Program> {
compiler_config.diagnostics_reporter.ensure(db)?;

let (sierra_program, _statements_locations) = db
.get_sierra_program(main_crate_ids)
.to_option()
.context("Compilation failed without any diagnostics")?;

// Try to move the program out of the Arc if it is not cached in Salsa, or just clone it.
let mut sierra_program = Arc::try_unwrap(sierra_program).unwrap_or_else(|arc| (*arc).clone());
let SierraProgramWithDebug { program: mut sierra_program, .. } = arc_unwrap_or_clone(
db.get_sierra_program(main_crate_ids)
.to_option()
.context("Compilation failed without any diagnostics")?,
);

if compiler_config.replace_ids {
sierra_program = replace_sierra_ids_in_program(db, &sierra_program);
Expand Down
8 changes: 5 additions & 3 deletions crates/cairo-lang-runner/src/profiling_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_compiler::diagnostics::DiagnosticsReporter;
use cairo_lang_semantic::test_utils::setup_test_module;
use cairo_lang_sierra_generator::db::SierraGenGroup;
use cairo_lang_sierra_generator::program_generator::SierraProgramWithDebug;
use cairo_lang_sierra_generator::replace_ids::replace_sierra_ids_in_program;
use cairo_lang_starknet::starknet_plugin_suite;
use cairo_lang_test_utils::get_direct_or_file_content;
use cairo_lang_test_utils::parse_test_file::TestRunnerResult;
use cairo_lang_utils::arc_unwrap_or_clone;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;

use super::ProfilingInfoProcessor;
Expand Down Expand Up @@ -40,10 +42,10 @@ pub fn test_profiling(
DiagnosticsReporter::stderr().with_crates(&[test_module.crate_id]).ensure(&db).unwrap();

// Compile to Sierra.
let (sierra_program, statements_locations) =
db.get_sierra_program(vec![test_module.crate_id]).unwrap();
let SierraProgramWithDebug { program: sierra_program, debug_info } =
arc_unwrap_or_clone(db.get_sierra_program(vec![test_module.crate_id]).unwrap());
let sierra_program = replace_sierra_ids_in_program(&db, &sierra_program);
let statements_functions = statements_locations.get_statements_functions_map(&db);
let statements_functions = debug_info.statements_locations.get_statements_functions_map(&db);
let runner = SierraCasmRunner::new(
sierra_program.clone(),
Some(Default::default()),
Expand Down
7 changes: 3 additions & 4 deletions crates/cairo-lang-sierra-generator/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use lowering::ids::ConcreteFunctionWithBodyId;
use semantic::items::imp::ImplLookupContext;
use {cairo_lang_lowering as lowering, cairo_lang_semantic as semantic};

use crate::program_generator::{self};
use crate::program_generator::{self, SierraProgramWithDebug};
use crate::specialization_context::SierraSignatureSpecializationContext;
use crate::statements_locations::StatementsLocations;
use crate::{ap_change, function_generator, pre_sierra};

/// Helper type for Sierra long ids, which can be either a type long id or a cycle breaker.
Expand Down Expand Up @@ -119,14 +118,14 @@ pub trait SierraGenGroup: LoweringGroup + Upcast<dyn LoweringGroup> {
fn get_sierra_program_for_functions(
&self,
requested_function_ids: Vec<ConcreteFunctionWithBodyId>,
) -> Maybe<(Arc<cairo_lang_sierra::program::Program>, Arc<StatementsLocations>)>;
) -> 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>,
) -> Maybe<(Arc<cairo_lang_sierra::program::Program>, Arc<StatementsLocations>)>;
) -> Maybe<Arc<SierraProgramWithDebug>>;
}

fn get_function_signature(
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-sierra-generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod lifetime;
mod local_variables;
mod next_statement_index_fetch;
pub mod pre_sierra;
mod program_generator;
pub mod program_generator;
pub mod replace_ids;
mod resolve_labels;
mod specialization_context;
Expand Down
24 changes: 18 additions & 6 deletions crates/cairo-lang-sierra-generator/src/program_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,20 @@ fn collect_used_types(
.collect()
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SierraProgramWithDebug {
pub program: cairo_lang_sierra::program::Program,
pub debug_info: SierraProgramDebugInfo,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SierraProgramDebugInfo {
pub statements_locations: StatementsLocations,
}

pub fn get_sierra_program_for_functions(
db: &dyn SierraGenGroup,
requested_function_ids: Vec<ConcreteFunctionWithBodyId>,
) -> Maybe<(Arc<cairo_lang_sierra::program::Program>, Arc<StatementsLocations>)> {
) -> Maybe<Arc<SierraProgramWithDebug>> {
let mut functions: Vec<Arc<pre_sierra::Function>> = vec![];
let mut statements: Vec<pre_sierra::StatementWithLocation> = vec![];
let mut processed_function_ids = UnorderedHashSet::<ConcreteFunctionWithBodyId>::default();
Expand Down Expand Up @@ -210,10 +220,12 @@ pub fn get_sierra_program_for_functions(
})
.collect(),
};
Ok((
Arc::new(program),
Arc::new(StatementsLocations::from_locations_vec(&statements_locations)),
))
Ok(Arc::new(SierraProgramWithDebug {
program,
debug_info: SierraProgramDebugInfo {
statements_locations: StatementsLocations::from_locations_vec(&statements_locations),
},
}))
}

/// Tries extracting a ConcreteFunctionWithBodyId from a pre-Sierra statement.
Expand Down Expand Up @@ -243,7 +255,7 @@ fn try_get_function_with_body_id(
pub fn get_sierra_program(
db: &dyn SierraGenGroup,
requested_crate_ids: Vec<CrateId>,
) -> Maybe<(Arc<cairo_lang_sierra::program::Program>, Arc<StatementsLocations>)> {
) -> Maybe<Arc<SierraProgramWithDebug>> {
let mut requested_function_ids = vec![];
for crate_id in requested_crate_ids {
for module_id in db.crate_modules(crate_id).iter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::ids::ModuleItemId;
use cairo_lang_lowering::ids::ConcreteFunctionWithBodyId;
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_utils::try_extract_matches;
use cairo_lang_utils::{arc_unwrap_or_clone, try_extract_matches};
use indoc::indoc;
use itertools::Itertools;
use pretty_assertions::assert_eq;
use test_case::test_case;
use test_log::test;

use crate::db::SierraGenGroup;
use crate::program_generator::SierraProgramWithDebug;
use crate::replace_ids::replace_sierra_ids_in_program;
use crate::test_utils::{checked_compile_to_sierra, setup_db_and_get_crate_id};

Expand Down Expand Up @@ -132,8 +133,8 @@ fn test_only_include_dependencies(func_name: &str, sierra_used_funcs: &[&str]) {
.unwrap(),
)
.unwrap();
let (program, _statements_locations) =
db.get_sierra_program_for_functions(vec![func_id]).unwrap();
let SierraProgramWithDebug { program, .. } =
arc_unwrap_or_clone(db.get_sierra_program_for_functions(vec![func_id]).unwrap());
assert_eq!(
replace_sierra_ids_in_program(&db, &program)
.funcs
Expand Down
6 changes: 4 additions & 2 deletions crates/cairo-lang-sierra-generator/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use cairo_lang_semantic::test_utils::setup_test_crate;
use cairo_lang_sierra::ids::{ConcreteLibfuncId, GenericLibfuncId};
use cairo_lang_sierra::program;
use cairo_lang_syntax::node::db::{SyntaxDatabase, SyntaxGroup};
use cairo_lang_utils::{Upcast, UpcastMut};
use cairo_lang_utils::{arc_unwrap_or_clone, Upcast, UpcastMut};
use defs::ids::FreeFunctionId;
use lowering::ids::ConcreteFunctionWithBodyLongId;
use lowering::optimizations::config::OptimizationConfig;
Expand All @@ -25,6 +25,7 @@ use {cairo_lang_defs as defs, cairo_lang_lowering as lowering, cairo_lang_semant

use crate::db::{SierraGenDatabase, SierraGenGroup};
use crate::pre_sierra::{self, LabelLongId};
use crate::program_generator::SierraProgramWithDebug;
use crate::replace_ids::replace_sierra_ids_in_program;
use crate::utils::{jump_statement, return_statement, simple_statement};

Expand Down Expand Up @@ -123,7 +124,8 @@ impl Upcast<dyn LoweringGroup> for SierraGenDatabaseForTesting {
pub fn checked_compile_to_sierra(content: &str) -> cairo_lang_sierra::program::Program {
let (db, crate_id) = setup_db_and_get_crate_id(content);

let (program, _statements_locations) = db.get_sierra_program(vec![crate_id]).unwrap();
let SierraProgramWithDebug { program, .. } =
arc_unwrap_or_clone(db.get_sierra_program(vec![crate_id]).unwrap());
replace_sierra_ids_in_program(&db, &program)
}

Expand Down
12 changes: 7 additions & 5 deletions crates/cairo-lang-starknet/src/compile.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::path::{Path, PathBuf};
use std::sync::Arc;

use anyhow::{Context, Result};
use cairo_lang_compiler::db::RootDatabase;
Expand All @@ -12,11 +11,13 @@ use cairo_lang_lowering::db::LoweringGroup;
use cairo_lang_lowering::ids::ConcreteFunctionWithBodyId;
use cairo_lang_sierra_generator::canonical_id_replacer::CanonicalReplacer;
use cairo_lang_sierra_generator::db::SierraGenGroup;
use cairo_lang_sierra_generator::program_generator::SierraProgramWithDebug;
use cairo_lang_sierra_generator::replace_ids::{replace_sierra_ids_in_program, SierraIdReplacer};
use cairo_lang_starknet_classes::allowed_libfuncs::ListSelector;
use cairo_lang_starknet_classes::contract_class::{
ContractClass, ContractEntryPoint, ContractEntryPoints,
};
use cairo_lang_utils::arc_unwrap_or_clone;
use itertools::{chain, Itertools};

use crate::abi::AbiBuilder;
Expand Down Expand Up @@ -125,15 +126,16 @@ fn compile_contract_with_prepared_and_checked_db(
) -> Result<ContractClass> {
let SemanticEntryPoints { external, l1_handler, constructor } =
extract_semantic_entrypoints(db, contract)?;
let (mut sierra_program, _statements_locations) = db
.get_sierra_program_for_functions(
let SierraProgramWithDebug { program: mut sierra_program, .. } = arc_unwrap_or_clone(
db.get_sierra_program_for_functions(
chain!(&external, &l1_handler, &constructor).map(|f| f.value).collect(),
)
.to_option()
.with_context(|| "Compilation failed without any diagnostics.")?;
.with_context(|| "Compilation failed without any diagnostics.")?,
);

if compiler_config.replace_ids {
sierra_program = Arc::new(replace_sierra_ids_in_program(db, &sierra_program));
sierra_program = replace_sierra_ids_in_program(db, &sierra_program);
}
let replacer = CanonicalReplacer::from_program(&sierra_program);
let sierra_program = replacer.apply(&sierra_program);
Expand Down
11 changes: 7 additions & 4 deletions crates/cairo-lang-test-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ use cairo_lang_sierra::extensions::gas::CostTokenType;
use cairo_lang_sierra::ids::FunctionId;
use cairo_lang_sierra::program::{Program, StatementIdx};
use cairo_lang_sierra_generator::db::SierraGenGroup;
use cairo_lang_sierra_generator::program_generator::SierraProgramWithDebug;
use cairo_lang_sierra_generator::replace_ids::{DebugReplacer, SierraIdReplacer};
use cairo_lang_starknet::contract::{
find_contracts, get_contract_abi_functions, get_contracts_info, ContractInfo,
};
use cairo_lang_starknet::plugin::consts::{CONSTRUCTOR_MODULE, EXTERNAL_MODULE, L1_HANDLER_MODULE};
use cairo_lang_starknet_classes::casm_contract_class::ENTRY_POINT_COST;
use cairo_lang_utils::arc_unwrap_or_clone;
use cairo_lang_utils::ordered_hash_map::{
deserialize_ordered_hashmap_vec, serialize_ordered_hashmap_vec, OrderedHashMap,
};
Expand Down Expand Up @@ -82,8 +84,8 @@ pub fn compile_test_prepared_db(
})
.collect();
let all_tests = find_all_tests(db, test_crate_ids.clone());
let (sierra_program, statements_locations) = db
.get_sierra_program_for_functions(
let SierraProgramWithDebug { program: sierra_program, debug_info } = arc_unwrap_or_clone(
db.get_sierra_program_for_functions(
chain!(
all_entry_points.into_iter(),
all_tests.iter().flat_map(|(func_id, _cfg)| {
Expand All @@ -93,10 +95,11 @@ pub fn compile_test_prepared_db(
.collect(),
)
.to_option()
.with_context(|| "Compilation failed without any diagnostics.")?;
.with_context(|| "Compilation failed without any diagnostics.")?,
);
let replacer = DebugReplacer { db };
let sierra_program = replacer.apply(&sierra_program);
let statements_functions = statements_locations.get_statements_functions_map(db);
let statements_functions = debug_info.statements_locations.get_statements_functions_map(db);

let named_tests = all_tests
.into_iter()
Expand Down
10 changes: 10 additions & 0 deletions crates/cairo-lang-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::sync::Arc;
use core::fmt;
#[cfg(feature = "std")]
use std::sync::Arc;

pub mod bigint;
pub mod byte_array;
Expand Down Expand Up @@ -157,3 +161,9 @@ impl<T: ?Sized> UpcastMut<T> for T {
self
}
}

// TODO(yuval): use Arc::unwrap_or_clone once it's stable.
/// Moves the content out of the Arc if possible, otherwise just clones it.
pub fn arc_unwrap_or_clone<T: Clone>(arc: Arc<T>) -> T {
Arc::try_unwrap(arc).unwrap_or_else(|arc| (*arc).clone())
}
7 changes: 4 additions & 3 deletions tests/e2e_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ use cairo_lang_sierra::extensions::gas::CostTokenType;
use cairo_lang_sierra::ids::FunctionId;
use cairo_lang_sierra::program::{Function, Program};
use cairo_lang_sierra_generator::db::SierraGenGroup;
use cairo_lang_sierra_generator::program_generator::SierraProgramWithDebug;
use cairo_lang_sierra_generator::replace_ids::replace_sierra_ids_in_program;
use cairo_lang_sierra_to_casm::metadata::{calc_metadata, MetadataComputationConfig};
use cairo_lang_test_utils::parse_test_file::{TestFileRunner, TestRunnerResult};
use cairo_lang_test_utils::test_lock;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use cairo_lang_utils::Upcast;
use cairo_lang_utils::{arc_unwrap_or_clone, Upcast};
use itertools::Itertools;
use once_cell::sync::Lazy;

Expand Down Expand Up @@ -183,8 +184,8 @@ fn run_e2e_test(
DiagnosticsReporter::stderr().with_crates(&[test_module.crate_id]).ensure(&db).unwrap();

// Compile to Sierra.
let (sierra_program, _statements_locations) =
db.get_sierra_program(vec![test_module.crate_id]).unwrap();
let SierraProgramWithDebug { program: sierra_program, .. } =
arc_unwrap_or_clone(db.get_sierra_program(vec![test_module.crate_id]).unwrap());
let sierra_program = replace_sierra_ids_in_program(&db, &sierra_program);
let sierra_program_str = sierra_program.to_string();

Expand Down
Loading

0 comments on commit 709f0c2

Please sign in to comment.