Skip to content

Commit

Permalink
Added additional tests for bounded_int const folding. (#6426)
Browse files Browse the repository at this point in the history
  • Loading branch information
orizi authored Sep 29, 2024
1 parent 30b0475 commit 816c4a6
Showing 1 changed file with 262 additions and 1 deletion.
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

0 comments on commit 816c4a6

Please sign in to comment.