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 additional tests for bounded_int const folding. #6426

Merged
merged 2 commits into from
Sep 29, 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
8 changes: 8 additions & 0 deletions corelib/src/zeroable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ pub(crate) impl Felt252Zeroable = zero_based::ZeroableImpl<felt252>;
/// This type guarantees that the wrapped value is never zero.
#[derive(Copy, Drop)]
pub extern type NonZero<T>;
impl NonZeroNeg<T, +Neg<T>, +TryInto<T, NonZero<T>>> of Neg<NonZero<T>> {
fn neg(a: NonZero<T>) -> NonZero<T> {
// TODO(orizi): Optimize using bounded integers.
let value: T = a.into();
let negated: T = -value;
negated.try_into().unwrap()
}
}

/// Represents the result of checking whether a value is zero.
pub(crate) enum IsZeroResult<T> {
Expand Down
263 changes: 262 additions & 1 deletion crates/cairo-lang-lowering/src/optimizations/test_data/const_folding
Original file line number Diff line number Diff line change
Expand Up @@ -3348,7 +3348,7 @@ End:

//! > ==========================================================================

//! > `bounded_int_constrain` and `bounded_int_mul`.
//! > `AbsAndSign` on positive const.

//! > test_runner_name
test_match_optimizer
Expand Down Expand Up @@ -3439,6 +3439,97 @@ End:

//! > ==========================================================================

//! > `AbsAndSign` on negative const.

//! > test_runner_name
test_match_optimizer

//! > function
fn foo() -> (u8, bool) {
core::integer::AbsAndSign::<i8, u8>::abs_and_sign(-6_i8)
}

//! > function_name
foo

//! > module_code

//! > semantic_diagnostics

//! > before
Parameters:
blk0 (root):
Statements:
(v0: core::integer::i8) <- -6
End:
Match(match core::internal::bounded_int::bounded_int_constrain::<core::integer::i8, 0, core::internal::bounded_int::constrain0::Impl::<core::integer::i8, -128, 127>>(v0) {
Result::Ok(v1) => blk1,
Result::Err(v2) => blk2,
})

blk1:
Statements:
(v3: core::internal::bounded_int::BoundedInt::<-1, -1>) <- core::internal::bounded_int::minus_1::const_as_immediate::<core::internal::bounded_int::minus_1::Const::<core::internal::bounded_int::BoundedInt::<-1, -1>, -1>>()
(v4: core::internal::bounded_int::BoundedInt::<1, 128>) <- core::internal::bounded_int::bounded_int_mul::<core::internal::bounded_int::BoundedInt::<-128, -1>, core::internal::bounded_int::BoundedInt::<-1, -1>, core::internal::bounded_int::MulMinus1::<-128, -1, core::internal::bounded_int::neg_felt252::Impl::<-128, 128>, core::internal::bounded_int::neg_felt252::Impl::<-1, 1>>>(v1, v3)
(v5: core::integer::u8) <- core::integer::upcast::<core::internal::bounded_int::BoundedInt::<1, 128>, core::integer::u8>(v4)
(v6: ()) <- struct_construct()
(v7: core::bool) <- bool::True(v6)
(v8: (core::integer::u8, core::bool)) <- struct_construct(v5, v7)
End:
Goto(blk3, {v8 -> v9})

blk2:
Statements:
(v10: core::integer::u8) <- core::integer::upcast::<core::internal::bounded_int::BoundedInt::<0, 127>, core::integer::u8>(v2)
(v11: ()) <- struct_construct()
(v12: core::bool) <- bool::False(v11)
(v13: (core::integer::u8, core::bool)) <- struct_construct(v10, v12)
End:
Goto(blk3, {v13 -> v9})

blk3:
Statements:
End:
Return(v9)

//! > after
Parameters:
blk0 (root):
Statements:
(v0: core::integer::i8) <- -6
(v1: core::internal::bounded_int::BoundedInt::<-128, -1>) <- -6
End:
Goto(blk1, {})

blk1:
Statements:
(v3: core::internal::bounded_int::BoundedInt::<-1, -1>) <- core::internal::bounded_int::minus_1::const_as_immediate::<core::internal::bounded_int::minus_1::Const::<core::internal::bounded_int::BoundedInt::<-1, -1>, -1>>()
(v4: core::internal::bounded_int::BoundedInt::<1, 128>) <- core::internal::bounded_int::bounded_int_mul::<core::internal::bounded_int::BoundedInt::<-128, -1>, core::internal::bounded_int::BoundedInt::<-1, -1>, core::internal::bounded_int::MulMinus1::<-128, -1, core::internal::bounded_int::neg_felt252::Impl::<-128, 128>, core::internal::bounded_int::neg_felt252::Impl::<-1, 1>>>(v1, v3)
(v5: core::integer::u8) <- core::integer::upcast::<core::internal::bounded_int::BoundedInt::<1, 128>, core::integer::u8>(v4)
(v6: ()) <- struct_construct()
(v7: core::bool) <- bool::True(v6)
(v8: (core::integer::u8, core::bool)) <- struct_construct(v5, v7)
End:
Goto(blk3, {v8 -> v9})

blk2:
Statements:
(v10: core::integer::u8) <- core::integer::upcast::<core::internal::bounded_int::BoundedInt::<0, 127>, core::integer::u8>(v2)
(v11: ()) <- struct_construct()
(v12: core::bool) <- bool::False(v11)
(v13: (core::integer::u8, core::bool)) <- struct_construct(v10, v12)
End:
Goto(blk3, {v13 -> v9})

blk3:
Statements:
End:
Return(v9)

//! > lowering_diagnostics

//! > ==========================================================================

//! > Bitwise not const folding.

//! > test_runner_name
Expand Down Expand Up @@ -3479,3 +3570,173 @@ End:
Return(v3)

//! > lowering_diagnostics

//! > ==========================================================================

//! > bounded_int_constrain on NonZero below.

//! > test_runner_name
test_match_optimizer

//! > function
fn foo() -> Result<Box<NonZero<BoundedInt<-0x80, -1>>>, Box<NonZero<BoundedInt<0, 0x7f>>>> {
match core::internal::bounded_int::constrain::<NonZero<i8>, 0>(-5) {
Result::Ok(v) => Result::Ok(BoxTrait::new(v)),
Result::Err(v) => Result::Err(BoxTrait::new(v)),
}
}

//! > function_name
foo

//! > module_code
use core::internal::bounded_int::BoundedInt;

//! > semantic_diagnostics

//! > before
Parameters:
blk0 (root):
Statements:
(v0: core::zeroable::NonZero::<core::integer::i8>) <- NonZero(-5)
End:
Match(match core::internal::bounded_int::bounded_int_constrain::<core::zeroable::NonZero::<core::integer::i8>, 0, core::internal::bounded_int::NonZeroConstrainHelper::<core::integer::i8, 0, core::internal::bounded_int::constrain0::Impl::<core::integer::i8, -128, 127>>>(v0) {
Result::Ok(v1) => blk1,
Result::Err(v2) => blk2,
})

blk1:
Statements:
(v3: core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>) <- core::box::into_box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>(v1)
(v4: core::result::Result::<core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>, core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>>) <- Result::Ok(v3)
End:
Goto(blk3, {v4 -> v5})

blk2:
Statements:
(v6: core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>) <- core::box::into_box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>(v2)
(v7: core::result::Result::<core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>, core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>>) <- Result::Err(v6)
End:
Goto(blk3, {v7 -> v5})

blk3:
Statements:
End:
Return(v5)

//! > after
Parameters:
blk0 (root):
Statements:
(v0: core::zeroable::NonZero::<core::integer::i8>) <- NonZero(-5)
End:
Match(match core::internal::bounded_int::bounded_int_constrain::<core::zeroable::NonZero::<core::integer::i8>, 0, core::internal::bounded_int::NonZeroConstrainHelper::<core::integer::i8, 0, core::internal::bounded_int::constrain0::Impl::<core::integer::i8, -128, 127>>>(v0) {
Result::Ok(v1) => blk1,
Result::Err(v2) => blk2,
})

blk1:
Statements:
(v3: core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>) <- core::box::into_box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>(v1)
(v4: core::result::Result::<core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>, core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>>) <- Result::Ok(v3)
End:
Goto(blk3, {v4 -> v5})

blk2:
Statements:
(v6: core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>) <- core::box::into_box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>(v2)
(v7: core::result::Result::<core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>, core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>>) <- Result::Err(v6)
End:
Goto(blk3, {v7 -> v5})

blk3:
Statements:
End:
Return(v5)

//! > lowering_diagnostics

//! > ==========================================================================

//! > bounded_int_constrain on NonZero above.

//! > test_runner_name
test_match_optimizer

//! > function
fn foo() -> Result<Box<NonZero<BoundedInt<-0x80, -1>>>, Box<NonZero<BoundedInt<0, 0x7f>>>> {
match core::internal::bounded_int::constrain::<NonZero<i8>, 0>(5) {
Result::Ok(v) => Result::Ok(BoxTrait::new(v)),
Result::Err(v) => Result::Err(BoxTrait::new(v)),
}
}

//! > function_name
foo

//! > module_code
use core::internal::bounded_int::BoundedInt;

//! > semantic_diagnostics

//! > before
Parameters:
blk0 (root):
Statements:
(v0: core::zeroable::NonZero::<core::integer::i8>) <- NonZero(5)
End:
Match(match core::internal::bounded_int::bounded_int_constrain::<core::zeroable::NonZero::<core::integer::i8>, 0, core::internal::bounded_int::NonZeroConstrainHelper::<core::integer::i8, 0, core::internal::bounded_int::constrain0::Impl::<core::integer::i8, -128, 127>>>(v0) {
Result::Ok(v1) => blk1,
Result::Err(v2) => blk2,
})

blk1:
Statements:
(v3: core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>) <- core::box::into_box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>(v1)
(v4: core::result::Result::<core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>, core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>>) <- Result::Ok(v3)
End:
Goto(blk3, {v4 -> v5})

blk2:
Statements:
(v6: core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>) <- core::box::into_box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>(v2)
(v7: core::result::Result::<core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>, core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>>) <- Result::Err(v6)
End:
Goto(blk3, {v7 -> v5})

blk3:
Statements:
End:
Return(v5)

//! > after
Parameters:
blk0 (root):
Statements:
(v0: core::zeroable::NonZero::<core::integer::i8>) <- NonZero(5)
End:
Match(match core::internal::bounded_int::bounded_int_constrain::<core::zeroable::NonZero::<core::integer::i8>, 0, core::internal::bounded_int::NonZeroConstrainHelper::<core::integer::i8, 0, core::internal::bounded_int::constrain0::Impl::<core::integer::i8, -128, 127>>>(v0) {
Result::Ok(v1) => blk1,
Result::Err(v2) => blk2,
})

blk1:
Statements:
(v3: core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>) <- core::box::into_box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>(v1)
(v4: core::result::Result::<core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>, core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>>) <- Result::Ok(v3)
End:
Goto(blk3, {v4 -> v5})

blk2:
Statements:
(v6: core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>) <- core::box::into_box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>(v2)
(v7: core::result::Result::<core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<-128, -1>>>, core::box::Box::<core::zeroable::NonZero::<core::internal::bounded_int::BoundedInt::<0, 127>>>>) <- Result::Err(v6)
End:
Goto(blk3, {v7 -> v5})

blk3:
Statements:
End:
Return(v5)

//! > lowering_diagnostics