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

Added option for gas disabling cfg. #6916

Merged
merged 1 commit into from
Dec 24, 2024
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
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.

25 changes: 25 additions & 0 deletions corelib/src/gas.cairo
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand All @@ -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 {}
}
1 change: 1 addition & 0 deletions crates/bin/cairo-run/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
5 changes: 4 additions & 1 deletion crates/bin/cairo-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()?;

Expand Down
2 changes: 2 additions & 0 deletions crates/cairo-lang-executable/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -57,6 +58,7 @@ pub fn compile_executable(
) -> Result<CompiledFunction> {
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()?;
Expand Down
38 changes: 9 additions & 29 deletions crates/cairo-lang-executable/src/compile_test_data/basic
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
2 changes: 2 additions & 0 deletions crates/cairo-lang-executable/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -17,6 +18,7 @@ pub static SHARED_DB: LazyLock<Mutex<RootDatabase>> = 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()
Expand Down
4 changes: 3 additions & 1 deletion crates/cairo-lang-test-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,15 @@ impl TestCompiler {
) -> Result<Self> {
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());
Expand Down
Loading