Skip to content

Commit

Permalink
Enabled const-folding cancelation for e2e tests.
Browse files Browse the repository at this point in the history
commit-id:34243163
  • Loading branch information
orizi committed Sep 30, 2024
1 parent e37c27a commit 608b180
Show file tree
Hide file tree
Showing 6 changed files with 582 additions and 466 deletions.
10 changes: 9 additions & 1 deletion crates/cairo-lang-lowering/src/optimizations/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct OptimizationConfig {
pub inline_small_functions_threshold: usize,
/// Determines whether inlining is disabled.
pub inlining_strategy: InliningStrategy,
/// Should const folding be skipped.
pub skip_const_folding: bool,
}

impl OptimizationConfig {
Expand All @@ -44,11 +46,16 @@ impl OptimizationConfig {
self.inline_small_functions_threshold = inline_small_functions_threshold;
self
}
/// Sets the `inlining_strategy` flag
/// Sets the `inlining_strategy` flag.
pub fn with_inlining_strategy(mut self, inlining_strategy: InliningStrategy) -> Self {
self.inlining_strategy = inlining_strategy;
self
}
/// Sets the `skip_const_folding` flag.
pub fn with_skip_const_folding(mut self, skip_const_folding: bool) -> Self {
self.skip_const_folding = skip_const_folding;
self
}
}

impl Default for OptimizationConfig {
Expand All @@ -57,6 +64,7 @@ impl Default for OptimizationConfig {
moveable_functions: vec![],
inline_small_functions_threshold: DEFAULT_INLINE_SMALL_FUNCTIONS_THRESHOLD,
inlining_strategy: InliningStrategy::Default,
skip_const_folding: false,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ enum VarInfo {
/// Performs constant folding on the lowered program.
/// The optimization works better when the blocks are topologically sorted.
pub fn const_folding(db: &dyn LoweringGroup, lowered: &mut FlatLowered) {
if lowered.blocks.is_empty() {
if db.optimization_config().skip_const_folding || lowered.blocks.is_empty() {
return;
}
let libfunc_info = priv_const_folding_info(db);
Expand Down
61 changes: 52 additions & 9 deletions tests/e2e_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ use itertools::Itertools;

/// Salsa databases configured to find the corelib, when reused by different tests should be able to
/// use the cached queries that rely on the corelib's code, which vastly reduces the tests runtime.
static SHARED_DB_WITH_GAS: LazyLock<Mutex<RootDatabase>> = LazyLock::new(|| {
static SHARED_DB_WITH_GAS_NO_OPTS: LazyLock<Mutex<RootDatabase>> = LazyLock::new(|| {
let mut db = RootDatabase::builder().detect_corelib().build().unwrap();
db.set_optimization_config(Arc::new(
OptimizationConfig::default().with_minimal_movable_functions(),
OptimizationConfig::default().with_skip_const_folding(true),
));
Mutex::new(db)
});
static SHARED_DB_NO_GAS: LazyLock<Mutex<RootDatabase>> = LazyLock::new(|| {
static SHARED_DB_NO_GAS_NO_OPTS: LazyLock<Mutex<RootDatabase>> = LazyLock::new(|| {
let mut db = RootDatabase::builder().detect_corelib().skip_auto_withdraw_gas().build().unwrap();
db.set_optimization_config(Arc::new(
OptimizationConfig::default().with_minimal_movable_functions(),
OptimizationConfig::default().with_skip_const_folding(true),
));
Mutex::new(db)
});
static SHARED_DB_WITH_OPTS: LazyLock<Mutex<RootDatabase>> = LazyLock::new(|| {
let mut db = RootDatabase::builder().detect_corelib().skip_auto_withdraw_gas().build().unwrap();
db.set_optimization_config(Arc::new(OptimizationConfig::default()));
Mutex::new(db)
});

cairo_lang_test_utils::test_file_test_with_runner!(
general_e2e,
Expand All @@ -58,7 +63,6 @@ cairo_lang_test_utils::test_file_test_with_runner!(
builtin_costs: "builtin_costs",
casts: "casts",
circuit: "circuit",
consts: "consts",
coupon: "coupon",
ec: "ec",
enum_: "enum",
Expand Down Expand Up @@ -97,6 +101,15 @@ cairo_lang_test_utils::test_file_test_with_runner!(
SmallE2ETestRunnerSkipAddGas
);

cairo_lang_test_utils::test_file_test_with_runner!(
libfunc_e2e_withopts,
"e2e_test_data/libfuncs",
{
consts: "consts",
},
WithOptsE2ETestRunner
);

cairo_lang_test_utils::test_file_test_with_runner!(
starknet_libfunc_e2e,
"e2e_test_data/libfuncs/starknet",
Expand Down Expand Up @@ -132,6 +145,21 @@ impl TestFileRunner for SmallE2ETestRunner {
}
}

#[derive(Default)]
struct WithOptsE2ETestRunner;
impl TestFileRunner for WithOptsE2ETestRunner {
fn run(
&mut self,
inputs: &OrderedHashMap<String, String>,
_args: &OrderedHashMap<String, String>,
) -> TestRunnerResult {
run_e2e_test(
inputs,
E2eTestParams { skip_optimization_passes: false, ..Default::default() },
)
}
}

#[derive(Default)]
struct SmallE2ETestRunnerSkipAddGas;
impl TestFileRunner for SmallE2ETestRunnerSkipAddGas {
Expand All @@ -152,7 +180,14 @@ impl TestFileRunner for SmallE2ETestRunnerMetadataComputation {
inputs: &OrderedHashMap<String, String>,
_args: &OrderedHashMap<String, String>,
) -> TestRunnerResult {
run_e2e_test(inputs, E2eTestParams { add_withdraw_gas: false, metadata_computation: true })
run_e2e_test(
inputs,
E2eTestParams {
add_withdraw_gas: false,
metadata_computation: true,
skip_optimization_passes: true,
},
)
}
}

Expand All @@ -165,12 +200,15 @@ struct E2eTestParams {
/// Argument for `run_e2e_test` that controls whether to add metadata computation information
/// to the test outputs.
metadata_computation: bool,

/// Argument for `run_e2e_test` that controls whether to skip optimization passes.
skip_optimization_passes: bool,
}

/// Implements default for `E2eTestParams`.
impl Default for E2eTestParams {
fn default() -> Self {
Self { add_withdraw_gas: true, metadata_computation: false }
Self { add_withdraw_gas: true, metadata_computation: false, skip_optimization_passes: true }
}
}

Expand All @@ -179,8 +217,13 @@ fn run_e2e_test(
inputs: &OrderedHashMap<String, String>,
params: E2eTestParams,
) -> TestRunnerResult {
let mut locked_db =
test_lock(if params.add_withdraw_gas { &SHARED_DB_WITH_GAS } else { &SHARED_DB_NO_GAS });
let mut locked_db = test_lock(if !params.skip_optimization_passes {
&SHARED_DB_WITH_OPTS
} else if params.add_withdraw_gas {
&SHARED_DB_WITH_GAS_NO_OPTS
} else {
&SHARED_DB_NO_GAS_NO_OPTS
});
// Parse code and create semantic model.
let test_module = setup_test_module(locked_db.deref_mut(), inputs["cairo"].as_str()).unwrap();
let db = locked_db.snapshot();
Expand Down
34 changes: 17 additions & 17 deletions tests/e2e_test_data/libfuncs/consts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! > const felt252

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
const V: felt252 = 100;
Expand Down Expand Up @@ -45,7 +45,7 @@ test::foo@3() -> (Box<felt252>);
//! > const u8

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
const V: u8 = 100;
Expand Down Expand Up @@ -89,7 +89,7 @@ test::foo@3() -> (Box<u8>);
//! > const i16

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
const V: i16 = -1000;
Expand Down Expand Up @@ -133,7 +133,7 @@ test::foo@3() -> (Box<i16>);
//! > const u256

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
const V1: u256 = 0x2000000000000000000000000000000010;
Expand Down Expand Up @@ -200,7 +200,7 @@ test::foo2@8() -> (Box<core::integer::u256>);
//! > const bool

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
fn bar() -> bool { false }
Expand Down Expand Up @@ -246,7 +246,7 @@ test::foo@4() -> (Box<core::bool>);
//! > const complex

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
enum ThreeOptions {
Expand Down Expand Up @@ -307,7 +307,7 @@ test::foo@3() -> (Box<test::ThreeOptions>);
//! > const complex with padding

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
enum ThreeOptions {
Expand Down Expand Up @@ -366,7 +366,7 @@ test::foo@3() -> (Box<test::ThreeOptions>);
//! > const several complex enums

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
enum ThreeOptions {
Expand Down Expand Up @@ -453,7 +453,7 @@ test::foo@3() -> (Box<test::ThreeOptionsPair>);
//! > const and builtin costs

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
fn foo() -> (Box<felt252>, BuiltinCosts) {
Expand Down Expand Up @@ -499,7 +499,7 @@ test::foo@0() -> (Tuple<Box<felt252>, BuiltinCosts>);
//! > const segmentation

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
extern type Const<T, const VALUE: T>;
Expand Down Expand Up @@ -582,7 +582,7 @@ test::foo@0() -> (Tuple<Box<felt252>, Box<felt252>, Box<felt252>, Box<felt252>,
//! > const felt252 span

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
fn foo() -> @Array<felt252> {
Expand Down Expand Up @@ -632,7 +632,7 @@ test::foo: OrderedHashMap({Const: 500})
//! > const felt252 quad span

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
fn foo() -> @Array<(felt252, felt252, felt252, felt252)> {
Expand Down Expand Up @@ -701,7 +701,7 @@ test::foo: OrderedHashMap({Const: 500})
//! > const NonZero<felt252>

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
const V: NonZero<felt252> = 100;
Expand Down Expand Up @@ -747,7 +747,7 @@ test::foo: OrderedHashMap({Const: 300})
//! > const NonZero<u8>

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
const V: NonZero<u8> = 100;
Expand Down Expand Up @@ -793,7 +793,7 @@ test::foo: OrderedHashMap({Const: 300})
//! > const NonZero<u256> 0 low.

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
const V: NonZero<u256> = 0x2000000000000000000000000000000000;
Expand Down Expand Up @@ -844,7 +844,7 @@ test::foo: OrderedHashMap({Const: 300})
//! > const NonZero<u256> 0 high.

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
const V: NonZero<u256> = 16;
Expand Down Expand Up @@ -895,7 +895,7 @@ test::foo: OrderedHashMap({Const: 300})
//! > const NonZero<u512> 0 high.

//! > test_runner_name
SmallE2ETestRunner
WithOptsE2ETestRunner

//! > cairo
use core::integer::u512;
Expand Down
Loading

0 comments on commit 608b180

Please sign in to comment.