From 1a0dfef45869200e11eab59d0cbed7fcc81717a7 Mon Sep 17 00:00:00 2001 From: Ori Ziv Date: Tue, 24 Dec 2024 09:42:47 +0200 Subject: [PATCH] Added option for gas disabling cfg. --- Cargo.lock | 1 + corelib/src/gas.cairo | 25 ++++++++++++ crates/bin/cairo-run/Cargo.toml | 1 + crates/bin/cairo-run/src/main.rs | 5 ++- crates/cairo-lang-executable/src/compile.rs | 2 + .../src/compile_test_data/basic | 38 +++++-------------- crates/cairo-lang-executable/src/test.rs | 2 + crates/cairo-lang-test-runner/src/lib.rs | 4 +- 8 files changed, 47 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b7f5cd5934..31cf774982c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1150,6 +1150,7 @@ dependencies = [ "anyhow", "cairo-lang-compiler", "cairo-lang-diagnostics", + "cairo-lang-filesystem", "cairo-lang-runner", "cairo-lang-sierra-generator", "cairo-lang-starknet", diff --git a/corelib/src/gas.cairo b/corelib/src/gas.cairo index c1c1c5bbd2c..ffe943b1e3b 100644 --- a/corelib/src/gas.cairo +++ b/corelib/src/gas.cairo @@ -1,9 +1,16 @@ +#[cfg(not(gas: "disabled"))] use crate::RangeCheck; /// Type representing the table of the costs of the different builtin usages. +#[cfg(not(gas: "disabled"))] #[derive(Copy, Drop)] pub extern type BuiltinCosts; +/// Placeholder when gas mechanism is disabled. +#[cfg(gas: "disabled")] +#[derive(Copy, Drop)] +pub struct BuiltinCosts {} + /// The gas builtin. /// This type is used to handle gas in the Cairo code. /// Contains the amount of gas available for the current run. @@ -25,15 +32,27 @@ pub extern type GasBuiltin; /// Option::None => cheap_not_enough_gas_case(), /// } /// ``` +#[cfg(not(gas: "disabled"))] pub extern fn withdraw_gas() -> Option<()> implicits(RangeCheck, GasBuiltin) nopanic; +/// Placeholder when gas mechanism is disabled. +#[cfg(gas: "disabled")] +pub fn withdraw_gas() -> Option<()> nopanic { + Option::Some(()) +} /// Same as `withdraw_gas`, but directly receives `BuiltinCosts`, which enables optimizations /// by removing the need for repeated internal calls for fetching the table of consts that may /// internally happen in calls to `withdraw_gas`. /// Should be used with caution. +#[cfg(not(gas: "disabled"))] pub extern fn withdraw_gas_all( costs: BuiltinCosts, ) -> Option<()> implicits(RangeCheck, GasBuiltin) nopanic; +/// Placeholder when gas mechanism is disabled. +#[cfg(gas: "disabled")] +pub fn withdraw_gas_all(costs: BuiltinCosts) -> Option<()> nopanic { + Option::Some(()) +} /// Returns unused gas into the gas builtin. @@ -43,4 +62,10 @@ pub extern fn withdraw_gas_all( pub extern fn redeposit_gas() implicits(GasBuiltin) nopanic; /// Returns the `BuiltinCosts` table to be used in `withdraw_gas_all`. +#[cfg(not(gas: "disabled"))] pub extern fn get_builtin_costs() -> BuiltinCosts nopanic; +/// Placeholder when gas mechanism is disabled. +#[cfg(gas: "disabled")] +pub fn get_builtin_costs() -> BuiltinCosts nopanic { + BuiltinCosts {} +} diff --git a/crates/bin/cairo-run/Cargo.toml b/crates/bin/cairo-run/Cargo.toml index a51b2532e1d..da88db1553d 100644 --- a/crates/bin/cairo-run/Cargo.toml +++ b/crates/bin/cairo-run/Cargo.toml @@ -12,6 +12,7 @@ clap.workspace = true cairo-lang-compiler = { path = "../../cairo-lang-compiler", version = "~2.9.2" } cairo-lang-diagnostics = { path = "../../cairo-lang-diagnostics", version = "~2.9.2" } +cairo-lang-filesystem = { path = "../../cairo-lang-filesystem", version = "~2.9.2" } cairo-lang-runner = { path = "../../cairo-lang-runner", version = "~2.9.2" } cairo-lang-sierra-generator = { path = "../../cairo-lang-sierra-generator", version = "~2.9.2" } cairo-lang-starknet = { path = "../../cairo-lang-starknet", version = "~2.9.2" } diff --git a/crates/bin/cairo-run/src/main.rs b/crates/bin/cairo-run/src/main.rs index 7f58efe855b..3b70aff8e38 100644 --- a/crates/bin/cairo-run/src/main.rs +++ b/crates/bin/cairo-run/src/main.rs @@ -8,6 +8,7 @@ use cairo_lang_compiler::db::RootDatabase; use cairo_lang_compiler::diagnostics::DiagnosticsReporter; use cairo_lang_compiler::project::{check_compiler_path, setup_project}; use cairo_lang_diagnostics::ToOption; +use cairo_lang_filesystem::cfg::{Cfg, CfgSet}; use cairo_lang_runner::casm_run::format_next_item; use cairo_lang_runner::profiling::ProfilingInfoProcessor; use cairo_lang_runner::{ProfilingInfoCollectionConfig, SierraCasmRunner, StarknetState}; @@ -51,7 +52,9 @@ fn main() -> anyhow::Result<()> { let mut db_builder = RootDatabase::builder(); db_builder.detect_corelib(); if args.available_gas.is_none() { - db_builder.skip_auto_withdraw_gas(); + db_builder + .skip_auto_withdraw_gas() + .with_cfg(CfgSet::from_iter([Cfg::kv("gas", "disabled")])); } let db = &mut db_builder.build()?; diff --git a/crates/cairo-lang-executable/src/compile.rs b/crates/cairo-lang-executable/src/compile.rs index 79d6ba6a31e..8bc47c6a685 100644 --- a/crates/cairo-lang-executable/src/compile.rs +++ b/crates/cairo-lang-executable/src/compile.rs @@ -5,6 +5,7 @@ use anyhow::{Context, Result}; use cairo_lang_compiler::db::RootDatabase; use cairo_lang_compiler::diagnostics::DiagnosticsReporter; use cairo_lang_compiler::project::setup_project; +use cairo_lang_filesystem::cfg::{Cfg, CfgSet}; use cairo_lang_filesystem::ids::CrateId; use cairo_lang_lowering::ids::ConcreteFunctionWithBodyId; use cairo_lang_runnable_utils::builder::{ @@ -57,6 +58,7 @@ pub fn compile_executable( ) -> Result { let mut db = RootDatabase::builder() .skip_auto_withdraw_gas() + .with_cfg(CfgSet::from_iter([Cfg::kv("gas", "disabled")])) .detect_corelib() .with_plugin_suite(executable_plugin_suite()) .build()?; diff --git a/crates/cairo-lang-executable/src/compile_test_data/basic b/crates/cairo-lang-executable/src/compile_test_data/basic index 31c1d23dded..b58fc6f7858 100644 --- a/crates/cairo-lang-executable/src/compile_test_data/basic +++ b/crates/cairo-lang-executable/src/compile_test_data/basic @@ -418,55 +418,35 @@ fn require_gas() -> felt252 { } //! > generated_casm_code -# builtins: output, range_check +# builtins: output # header # -[ap + 0] = [fp + -3], ap++; -[ap + 0] = 170141183460469231731687303715884105727, ap++; %{ raise NotImplementedError("memory[ap + 0].. = params[0])") %} -[ap + 2] = [fp + -4] + 1, ap++; +[ap + 2] = [fp + -3] + 1, ap++; [ap + 2] = [ap + 1], ap++; ap += 2; -call rel 13; +call rel 12; jmp rel 5 if [ap + -3] != 0, ap++; [ap + -1] = [ap + -2]; jmp rel 4; -[ap + -1] = [fp + -4] + 1; -[ap + -4] = [[fp + -4] + 0]; +[ap + -1] = [fp + -3] + 1; +[ap + -4] = [[fp + -3] + 0]; [ap + 0] = [ap + -1], ap++; -[ap + 0] = [ap + -7], ap++; ret; # sierra based code # [fp + -5] = [ap + 0] + [fp + -6], ap++; jmp rel 4 if [ap + -1] != 0; -jmp rel 17; -ap += 3; +jmp rel 13; %{ memory[ap + 0] = segments.add() %} ap += 1; [ap + 0] = 117999715903629884655797335944760714204113152088920212735095598, ap++; [ap + -1] = [[ap + -2] + 0]; -[ap + 0] = [fp + -8], ap++; -[ap + 0] = [fp + -7], ap++; [ap + 0] = 1, ap++; -[ap + 0] = [ap + -5], ap++; -[ap + 0] = [ap + -6] + 1, ap++; +[ap + 0] = [ap + -3], ap++; +[ap + 0] = [ap + -4] + 1, ap++; ret; -%{ memory[ap + 0] = 0 <= memory[fp + -7] %} -jmp rel 7 if [ap + 0] != 0, ap++; -[ap + 0] = [fp + -7] + 340282366920938463463374607431768211456, ap++; -[ap + -1] = [[fp + -8] + 0]; -jmp rel 12; -[fp + -7] = [ap + 0] + 0, ap++; -[ap + -1] = [[fp + -8] + 0]; -[ap + 0] = [fp + -8] + 1, ap++; -[ap + 0] = [ap + -2], ap++; +ap += 1; [ap + 0] = 1, ap++; -jmp rel 7; -[ap + 0] = [fp + -8] + 1, ap++; -[ap + 0] = [fp + -7], ap++; -[ap + 0] = 2, ap++; [ap + -1] = [[fp + -3] + 0]; -[ap + 0] = [ap + -3], ap++; -[ap + 0] = [ap + -3], ap++; [ap + 0] = 0, ap++; [ap + 0] = [fp + -4], ap++; [ap + 0] = [fp + -3] + 1, ap++; diff --git a/crates/cairo-lang-executable/src/test.rs b/crates/cairo-lang-executable/src/test.rs index d280a8184d7..7e2b68194ef 100644 --- a/crates/cairo-lang-executable/src/test.rs +++ b/crates/cairo-lang-executable/src/test.rs @@ -2,6 +2,7 @@ use std::sync::{LazyLock, Mutex}; use cairo_lang_compiler::db::RootDatabase; use cairo_lang_compiler::diagnostics::DiagnosticsReporter; +use cairo_lang_filesystem::cfg::{Cfg, CfgSet}; use cairo_lang_plugins::test_utils::expand_module_text; use cairo_lang_semantic::test_utils::setup_test_module; use cairo_lang_test_utils::parse_test_file::{TestFileRunner, TestRunnerResult}; @@ -17,6 +18,7 @@ pub static SHARED_DB: LazyLock> = LazyLock::new(|| { Mutex::new( RootDatabase::builder() .skip_auto_withdraw_gas() + .with_cfg(CfgSet::from_iter([Cfg::kv("gas", "disabled")])) .detect_corelib() .with_plugin_suite(executable_plugin_suite()) .build() diff --git a/crates/cairo-lang-test-runner/src/lib.rs b/crates/cairo-lang-test-runner/src/lib.rs index 86574e7e2ed..cfffdc61f6c 100644 --- a/crates/cairo-lang-test-runner/src/lib.rs +++ b/crates/cairo-lang-test-runner/src/lib.rs @@ -227,13 +227,15 @@ impl TestCompiler { ) -> Result { let db = &mut { let mut b = RootDatabase::builder(); + let mut cfg = CfgSet::from_iter([Cfg::name("test"), Cfg::kv("target", "test")]); if !gas_enabled { + cfg.insert(Cfg::kv("gas", "disabled")); b.skip_auto_withdraw_gas(); } else { b.with_add_redeposit_gas(); } b.detect_corelib(); - b.with_cfg(CfgSet::from_iter([Cfg::name("test"), Cfg::kv("target", "test")])); + b.with_cfg(cfg); b.with_plugin_suite(test_plugin_suite()); if config.starknet { b.with_plugin_suite(starknet_plugin_suite());