From 2b945fc503c748236149ab390481a1d75b0b762e Mon Sep 17 00:00:00 2001 From: Edgar Date: Mon, 25 Nov 2024 22:19:01 +0100 Subject: [PATCH] Small refactor with BlockExt (#939) * extend blockext * arugment -> arg * extui * extsi * shrui ext * shrui * shli * cmpi * fix * mul addi * muli * addi * check * trunci * trunci --- src/compiler.rs | 2 +- src/libfuncs/array.rs | 351 +++++++++------------------- src/libfuncs/bitwise.rs | 7 +- src/libfuncs/bool.rs | 10 +- src/libfuncs/bounded_int.rs | 118 +++++----- src/libfuncs/box.rs | 12 +- src/libfuncs/bytes31.rs | 18 +- src/libfuncs/cast.rs | 62 ++--- src/libfuncs/circuit.rs | 213 +++++++---------- src/libfuncs/debug.rs | 30 +-- src/libfuncs/drop.rs | 4 +- src/libfuncs/dup.rs | 4 +- src/libfuncs/ec.rs | 42 ++-- src/libfuncs/enum.rs | 21 +- src/libfuncs/felt252.rs | 114 ++++----- src/libfuncs/felt252_dict.rs | 23 +- src/libfuncs/felt252_dict_entry.rs | 17 +- src/libfuncs/gas.rs | 47 ++-- src/libfuncs/int_range.rs | 18 +- src/libfuncs/mem.rs | 2 +- src/libfuncs/nullable.rs | 2 +- src/libfuncs/pedersen.rs | 13 +- src/libfuncs/poseidon.rs | 16 +- src/libfuncs/sint128.rs | 80 ++----- src/libfuncs/sint16.rs | 90 +++---- src/libfuncs/sint32.rs | 90 +++---- src/libfuncs/sint64.rs | 90 +++---- src/libfuncs/sint8.rs | 90 +++---- src/libfuncs/starknet.rs | 362 +++++++++-------------------- src/libfuncs/struct.rs | 2 +- src/libfuncs/uint128.rs | 149 ++++-------- src/libfuncs/uint16.rs | 140 ++++------- src/libfuncs/uint256.rs | 69 ++---- src/libfuncs/uint32.rs | 124 ++++------ src/libfuncs/uint512.rs | 58 ++--- src/libfuncs/uint64.rs | 143 ++++-------- src/libfuncs/uint8.rs | 122 ++++------ src/metadata/debug_utils.rs | 2 +- src/types/array.rs | 57 ++--- src/types/box.rs | 4 +- src/types/enum.rs | 20 +- src/types/felt252_dict.rs | 15 +- src/types/nullable.rs | 9 +- src/types/snapshot.rs | 18 +- src/types/starknet.rs | 13 +- src/types/struct.rs | 4 +- src/utils/block_ext.rs | 154 +++++++++++- 47 files changed, 1123 insertions(+), 1928 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index a469b5f7c..d7c35f235 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -793,7 +793,7 @@ fn compile_func( block.append_operation(llvm::store( context, ptr, - pre_entry_block.argument(0)?.into(), + pre_entry_block.arg(0)?, location, LoadStoreOptions::new().align(Some(IntegerAttribute::new( IntegerType::new(context, 64).into(), diff --git a/src/libfuncs/array.rs b/src/libfuncs/array.rs index d7d1ae97d..7cf2ac77a 100644 --- a/src/libfuncs/array.rs +++ b/src/libfuncs/array.rs @@ -161,17 +161,16 @@ pub fn build_append<'ctx, 'this>( let elem_stride = entry.const_int(context, location, elem_stride, 64)?; - let array_end = entry.extract_value(context, location, entry.argument(0)?.into(), len_ty, 2)?; - let array_capacity = - entry.extract_value(context, location, entry.argument(0)?.into(), len_ty, 3)?; + let array_end = entry.extract_value(context, location, entry.arg(0)?, len_ty, 2)?; + let array_capacity = entry.extract_value(context, location, entry.arg(0)?, len_ty, 3)?; - let has_tail_space = entry.append_op_result(arith::cmpi( + let has_tail_space = entry.cmpi( context, CmpiPredicate::Ult, array_end, array_capacity, location, - ))?; + )?; let handle_block = helper.append_block(Block::new(&[])); let memmove_block = helper.append_block(Block::new(&[])); @@ -183,7 +182,7 @@ pub fn build_append<'ctx, 'this>( has_tail_space, append_block, handle_block, - &[entry.argument(0)?.into()], + &[entry.arg(0)?], &[], location, )); @@ -191,15 +190,10 @@ pub fn build_append<'ctx, 'this>( { let k0 = handle_block.const_int(context, location, 0, 32)?; let array_start = - handle_block.extract_value(context, location, entry.argument(0)?.into(), len_ty, 1)?; + handle_block.extract_value(context, location, entry.arg(0)?, len_ty, 1)?; - let has_head_space = handle_block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ne, - array_start, - k0, - location, - ))?; + let has_head_space = + handle_block.cmpi(context, CmpiPredicate::Ne, array_start, k0, location)?; handle_block.append_operation(cf::cond_br( context, has_head_space, @@ -213,18 +207,13 @@ pub fn build_append<'ctx, 'this>( { let array_start = - memmove_block.extract_value(context, location, entry.argument(0)?.into(), len_ty, 1)?; + memmove_block.extract_value(context, location, entry.arg(0)?, len_ty, 1)?; - let start_offset = memmove_block.append_op_result(arith::extui( - array_start, - IntegerType::new(context, 64).into(), - location, - ))?; let start_offset = - memmove_block.append_op_result(arith::muli(start_offset, elem_stride, location))?; + memmove_block.extui(array_start, IntegerType::new(context, 64).into(), location)?; + let start_offset = memmove_block.muli(start_offset, elem_stride, location)?; - let dst_ptr = - memmove_block.extract_value(context, location, entry.argument(0)?.into(), ptr_ty, 0)?; + let dst_ptr = memmove_block.extract_value(context, location, entry.arg(0)?, ptr_ty, 0)?; let src_ptr = memmove_block.append_op_result(llvm::get_element_ptr_dynamic( context, dst_ptr, @@ -236,14 +225,10 @@ pub fn build_append<'ctx, 'this>( let array_len = memmove_block.append_op_result(arith::subi(array_end, array_start, location))?; - let memmove_len = memmove_block.append_op_result(arith::extui( - array_len, - IntegerType::new(context, 64).into(), - location, - ))?; - let memmove_len = - memmove_block.append_op_result(arith::muli(memmove_len, elem_stride, location))?; + memmove_block.extui(array_len, IntegerType::new(context, 64).into(), location)?; + + let memmove_len = memmove_block.muli(memmove_len, elem_stride, location)?; memmove_block.append_operation( ods::llvm::intr_memmove( context, @@ -257,8 +242,7 @@ pub fn build_append<'ctx, 'this>( ); let k0 = memmove_block.const_int_from_type(context, location, 0, len_ty)?; - let value = - memmove_block.insert_value(context, location, entry.argument(0)?.into(), k0, 1)?; + let value = memmove_block.insert_value(context, location, entry.arg(0)?, k0, 1)?; let value = memmove_block.insert_value(context, location, value, array_len, 2)?; memmove_block.append_operation(cf::br(append_block, &[value], location)); @@ -270,25 +254,23 @@ pub fn build_append<'ctx, 'this>( // Array allocation growth formula: // new_len = max(8, old_len + min(1024, 2 * old_len)); - let new_capacity = realloc_block.append_op_result(arith::shli(array_end, k1, location))?; + let new_capacity = realloc_block.shli(array_end, k1, location)?; let new_capacity = realloc_block.append_op_result(arith::minui(new_capacity, k1024, location))?; - let new_capacity = - realloc_block.append_op_result(arith::addi(new_capacity, array_end, location))?; + let new_capacity = realloc_block.addi(new_capacity, array_end, location)?; let new_capacity = realloc_block.append_op_result(arith::maxui(new_capacity, k8, location))?; let realloc_size = { - let new_capacity = realloc_block.append_op_result(arith::extui( + let new_capacity = realloc_block.extui( new_capacity, IntegerType::new(context, 64).into(), location, - ))?; - realloc_block.append_op_result(arith::muli(new_capacity, elem_stride, location))? + )?; + realloc_block.muli(new_capacity, elem_stride, location)? }; - let ptr = - realloc_block.extract_value(context, location, entry.argument(0)?.into(), ptr_ty, 0)?; + let ptr = realloc_block.extract_value(context, location, entry.arg(0)?, ptr_ty, 0)?; let ptr = realloc_block.append_op_result(ReallocBindingsMeta::realloc( context, ptr, @@ -299,35 +281,20 @@ pub fn build_append<'ctx, 'this>( // No need to memmove, guaranteed by the fact that if we needed to memmove we'd have gone // through the memmove block instead of reallocating. - let value = - realloc_block.insert_value(context, location, entry.argument(0)?.into(), ptr, 0)?; + let value = realloc_block.insert_value(context, location, entry.arg(0)?, ptr, 0)?; let value = realloc_block.insert_value(context, location, value, new_capacity, 3)?; realloc_block.append_operation(cf::br(append_block, &[value], location)); } { - let ptr = append_block.extract_value( - context, - location, - append_block.argument(0)?.into(), - ptr_ty, - 0, - )?; - let array_end = append_block.extract_value( - context, - location, - append_block.argument(0)?.into(), - len_ty, - 2, - )?; + let ptr = append_block.extract_value(context, location, append_block.arg(0)?, ptr_ty, 0)?; + let array_end = + append_block.extract_value(context, location, append_block.arg(0)?, len_ty, 2)?; - let offset = append_block.append_op_result(arith::extui( - array_end, - IntegerType::new(context, 64).into(), - location, - ))?; - let offset = append_block.append_op_result(arith::muli(offset, elem_stride, location))?; + let offset = + append_block.extui(array_end, IntegerType::new(context, 64).into(), location)?; + let offset = append_block.muli(offset, elem_stride, location)?; let ptr = append_block.append_op_result(llvm::get_element_ptr_dynamic( context, ptr, @@ -337,16 +304,11 @@ pub fn build_append<'ctx, 'this>( location, ))?; - append_block.store(context, location, ptr, entry.argument(1)?.into())?; + append_block.store(context, location, ptr, entry.arg(1)?)?; - let array_len = append_block.append_op_result(arith::addi(array_end, k1, location))?; - let value = append_block.insert_value( - context, - location, - append_block.argument(0)?.into(), - array_len, - 2, - )?; + let array_len = append_block.addi(array_end, k1, location)?; + let value = + append_block.insert_value(context, location, append_block.arg(0)?, array_len, 2)?; append_block.append_operation(helper.br(0, &[value], location)); } @@ -373,7 +335,7 @@ pub fn build_len<'ctx, 'this>( )?; let len_ty = crate::ffi::get_struct_field_type_at(&array_ty, 1); - let array_value = entry.argument(0)?.into(); + let array_value = entry.arg(0)?; let array_start = entry.extract_value(context, location, array_value, len_ty, 1)?; let array_end = entry.extract_value(context, location, array_value, len_ty, 2)?; @@ -389,7 +351,7 @@ pub fn build_len<'ctx, 'this>( entry, location, &info.signature.param_signatures[0].ty, - entry.argument(0)?.into(), + entry.arg(0)?, )?; } _ => {} @@ -413,8 +375,7 @@ pub fn build_get<'ctx, 'this>( metadata.insert(ReallocBindingsMeta::new(context, helper)); } - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let array_ty = registry.build_type( context, @@ -432,20 +393,14 @@ pub fn build_get<'ctx, 'this>( let ptr_ty = crate::ffi::get_struct_field_type_at(&array_ty, 0); let len_ty = crate::ffi::get_struct_field_type_at(&array_ty, 1); - let value = entry.argument(1)?.into(); - let index = entry.argument(2)?.into(); + let value = entry.arg(1)?; + let index = entry.arg(2)?; let array_start = entry.extract_value(context, location, value, len_ty, 1)?; let array_end = entry.extract_value(context, location, value, len_ty, 2)?; let array_len = entry.append_op_result(arith::subi(array_end, array_start, location))?; - let is_valid = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ult, - index, - array_len, - location, - ))?; + let is_valid = entry.cmpi(context, CmpiPredicate::Ult, index, array_len, location)?; let valid_block = helper.append_block(Block::new(&[])); let error_block = helper.append_block(Block::new(&[])); @@ -462,23 +417,15 @@ pub fn build_get<'ctx, 'this>( { let ptr = valid_block.extract_value(context, location, value, ptr_ty, 0)?; - let array_start = valid_block.append_op_result(arith::extui( - array_start, - IntegerType::new(context, 64).into(), - location, - ))?; + let array_start = + valid_block.extui(array_start, IntegerType::new(context, 64).into(), location)?; let index = { - let index = valid_block.append_op_result(arith::extui( - index, - IntegerType::new(context, 64).into(), - location, - ))?; - valid_block.append_op_result(arith::addi(array_start, index, location))? + let index = valid_block.extui(index, IntegerType::new(context, 64).into(), location)?; + valid_block.addi(array_start, index, location)? }; let elem_stride = valid_block.const_int(context, location, elem_stride, 64)?; - let elem_offset = - valid_block.append_op_result(arith::muli(elem_stride, index, location))?; + let elem_offset = valid_block.muli(elem_stride, index, location)?; let elem_ptr = valid_block.gep( context, @@ -509,19 +456,11 @@ pub fn build_get<'ctx, 'this>( match metadata.get::() { Some(drop_overrides_meta) if drop_overrides_meta.is_overriden(&info.ty) => { - let array_end = valid_block.append_op_result(arith::extui( - array_end, - IntegerType::new(context, 64).into(), - location, - ))?; - - let array_start = valid_block.append_op_result(arith::muli( - array_start, - elem_stride, - location, - ))?; let array_end = - valid_block.append_op_result(arith::muli(array_end, elem_stride, location))?; + valid_block.extui(array_end, IntegerType::new(context, 64).into(), location)?; + + let array_start = valid_block.muli(array_start, elem_stride, location)?; + let array_end = valid_block.muli(array_end, elem_stride, location)?; valid_block.append_operation(scf::r#for( array_start, @@ -537,7 +476,7 @@ pub fn build_get<'ctx, 'this>( let value_ptr = block.append_op_result(llvm::get_element_ptr_dynamic( context, ptr, - &[block.argument(0)?.into()], + &[block.arg(0)?], IntegerType::new(context, 8).into(), llvm::r#type::pointer(context, 0), location, @@ -635,18 +574,12 @@ pub fn build_pop_front<'ctx, 'this>( let ptr_ty = crate::ffi::get_struct_field_type_at(&array_ty, 0); let len_ty = crate::ffi::get_struct_field_type_at(&array_ty, 1); - let value = entry.argument(0)?.into(); + let value = entry.arg(0)?; let array_start = entry.extract_value(context, location, value, len_ty, 1)?; let array_end = entry.extract_value(context, location, value, len_ty, 2)?; - let is_empty = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - array_start, - array_end, - location, - ))?; + let is_empty = entry.cmpi(context, CmpiPredicate::Eq, array_start, array_end, location)?; let valid_block = helper.append_block(Block::new(&[])); let empty_block = helper.append_block(Block::new(&[])); @@ -664,13 +597,9 @@ pub fn build_pop_front<'ctx, 'this>( let ptr = valid_block.extract_value(context, location, value, ptr_ty, 0)?; let elem_size = valid_block.const_int(context, location, elem_layout.size(), 64)?; - let elem_offset = valid_block.append_op_result(arith::extui( - array_start, - IntegerType::new(context, 64).into(), - location, - ))?; let elem_offset = - valid_block.append_op_result(arith::muli(elem_offset, elem_size, location))?; + valid_block.extui(array_start, IntegerType::new(context, 64).into(), location)?; + let elem_offset = valid_block.muli(elem_offset, elem_size, location)?; let ptr = valid_block.append_op_result(llvm::get_element_ptr_dynamic( context, ptr, @@ -697,7 +626,7 @@ pub fn build_pop_front<'ctx, 'this>( valid_block.memcpy(context, location, ptr, target_ptr, elem_size); let k1 = valid_block.const_int(context, location, 1, 32)?; - let new_start = valid_block.append_op_result(arith::addi(array_start, k1, location))?; + let new_start = valid_block.addi(array_start, k1, location)?; let value = valid_block.insert_value(context, location, value, new_start, 1)?; valid_block.append_operation(helper.br(0, &[value, target_ptr], location)); @@ -735,18 +664,12 @@ pub fn build_pop_front_consume<'ctx, 'this>( let ptr_ty = crate::ffi::get_struct_field_type_at(&array_ty, 0); let len_ty = crate::ffi::get_struct_field_type_at(&array_ty, 1); - let value = entry.argument(0)?.into(); + let value = entry.arg(0)?; let array_start = entry.extract_value(context, location, value, len_ty, 1)?; let array_end = entry.extract_value(context, location, value, len_ty, 2)?; - let is_empty = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - array_start, - array_end, - location, - ))?; + let is_empty = entry.cmpi(context, CmpiPredicate::Eq, array_start, array_end, location)?; let valid_block = helper.append_block(Block::new(&[])); let empty_block = helper.append_block(Block::new(&[])); @@ -764,13 +687,9 @@ pub fn build_pop_front_consume<'ctx, 'this>( let ptr = valid_block.extract_value(context, location, value, ptr_ty, 0)?; let elem_size = valid_block.const_int(context, location, elem_layout.size(), 64)?; - let elem_offset = valid_block.append_op_result(arith::extui( - array_start, - IntegerType::new(context, 64).into(), - location, - ))?; let elem_offset = - valid_block.append_op_result(arith::muli(elem_offset, elem_size, location))?; + valid_block.extui(array_start, IntegerType::new(context, 64).into(), location)?; + let elem_offset = valid_block.muli(elem_offset, elem_size, location)?; let ptr = valid_block.append_op_result(llvm::get_element_ptr_dynamic( context, ptr, @@ -797,7 +716,7 @@ pub fn build_pop_front_consume<'ctx, 'this>( valid_block.memcpy(context, location, ptr, target_ptr, elem_size); let k1 = valid_block.const_int(context, location, 1, 32)?; - let new_start = valid_block.append_op_result(arith::addi(array_start, k1, location))?; + let new_start = valid_block.addi(array_start, k1, location)?; let value = valid_block.insert_value(context, location, value, new_start, 1)?; valid_block.append_operation(helper.br(0, &[value, target_ptr], location)); @@ -859,17 +778,11 @@ pub fn build_snapshot_pop_back<'ctx, 'this>( let ptr_ty = crate::ffi::get_struct_field_type_at(&array_ty, 0); let len_ty = crate::ffi::get_struct_field_type_at(&array_ty, 1); - let value = entry.argument(0)?.into(); + let value = entry.arg(0)?; let array_start = entry.extract_value(context, location, value, len_ty, 1)?; let array_end = entry.extract_value(context, location, value, len_ty, 2)?; - let is_empty = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - array_start, - array_end, - location, - ))?; + let is_empty = entry.cmpi(context, CmpiPredicate::Eq, array_start, array_end, location)?; let valid_block = helper.append_block(Block::new(&[])); let empty_block = helper.append_block(Block::new(&[])); @@ -890,13 +803,9 @@ pub fn build_snapshot_pop_back<'ctx, 'this>( let ptr = valid_block.extract_value(context, location, value, ptr_ty, 0)?; let elem_size = valid_block.const_int(context, location, elem_layout.size(), 64)?; - let elem_offset = valid_block.append_op_result(arith::extui( - new_end, - IntegerType::new(context, 64).into(), - location, - ))?; let elem_offset = - valid_block.append_op_result(arith::muli(elem_offset, elem_size, location))?; + valid_block.extui(new_end, IntegerType::new(context, 64).into(), location)?; + let elem_offset = valid_block.muli(elem_offset, elem_size, location)?; let ptr = valid_block.append_op_result(llvm::get_element_ptr_dynamic( context, ptr, @@ -944,7 +853,7 @@ pub fn build_snapshot_multi_pop_front<'ctx, 'this>( metadata.insert(ReallocBindingsMeta::new(context, helper)); } - let range_check = entry.argument(0)?.into(); + let range_check = entry.arg(0)?; // Get type information @@ -971,7 +880,7 @@ pub fn build_snapshot_multi_pop_front<'ctx, 'this>( // Get array information - let array = entry.argument(1)?.into(); + let array = entry.arg(1)?; let array_ptr = entry.extract_value(context, location, array, array_ptr_ty, 0)?; let array_start = entry.extract_value(context, location, array, array_start_ty, 1)?; let array_end = entry.extract_value(context, location, array, array_end_ty, 2)?; @@ -983,13 +892,13 @@ pub fn build_snapshot_multi_pop_front<'ctx, 'this>( let array_len = entry.append_op_result(arith::subi(array_end, array_start, location))?; let popped_len_value = entry.const_int(context, location, popped_len, 32)?; - let is_valid = entry.append_op_result(arith::cmpi( + let is_valid = entry.cmpi( context, CmpiPredicate::Uge, array_len, popped_len_value, location, - ))?; + )?; let valid_block = helper.append_block(Block::new(&[])); let invalid_block = helper.append_block(Block::new(&[])); @@ -1040,11 +949,7 @@ pub fn build_snapshot_multi_pop_front<'ctx, 'this>( // Update array start (removing popped elements) let array = { - let new_array_start = valid_block.append_op_result(arith::addi( - array_start, - popped_len_value, - location, - ))?; + let new_array_start = valid_block.addi(array_start, popped_len_value, location)?; valid_block.insert_value(context, location, array, new_array_start, 1)? }; @@ -1070,7 +975,7 @@ pub fn build_snapshot_multi_pop_back<'ctx, 'this>( metadata.insert(ReallocBindingsMeta::new(context, helper)); } - let range_check = entry.argument(0)?.into(); + let range_check = entry.arg(0)?; // Get type information @@ -1097,7 +1002,7 @@ pub fn build_snapshot_multi_pop_back<'ctx, 'this>( // Get array information - let array = entry.argument(1)?.into(); + let array = entry.arg(1)?; let array_ptr = entry.extract_value(context, location, array, array_ptr_ty, 0)?; let array_start = entry.extract_value(context, location, array, array_start_ty, 1)?; let array_end = entry.extract_value(context, location, array, array_end_ty, 2)?; @@ -1109,13 +1014,13 @@ pub fn build_snapshot_multi_pop_back<'ctx, 'this>( let array_len = entry.append_op_result(arith::subi(array_end, array_start, location))?; let popped_len_value = entry.const_int(context, location, popped_len, 32)?; - let is_valid = entry.append_op_result(arith::cmpi( + let is_valid = entry.cmpi( context, CmpiPredicate::Uge, array_len, popped_len_value, location, - ))?; + )?; let valid_block = helper.append_block(Block::new(&[])); let invalid_block = helper.append_block(Block::new(&[])); @@ -1193,38 +1098,30 @@ pub fn build_slice<'ctx, 'this>( metadata: &mut MetadataStorage, info: &SignatureAndTypeConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let len_ty = IntegerType::new(context, 32).into(); let elem_ty = registry.get_type(&info.ty)?; let elem_layout = elem_ty.layout(registry)?; - let array_start = - entry.extract_value(context, location, entry.argument(1)?.into(), len_ty, 1)?; - let array_end = entry.extract_value(context, location, entry.argument(1)?.into(), len_ty, 2)?; + let array_start = entry.extract_value(context, location, entry.arg(1)?, len_ty, 1)?; + let array_end = entry.extract_value(context, location, entry.arg(1)?, len_ty, 2)?; - let slice_start = entry.argument(2)?.into(); - let slice_len = entry.argument(3)?.into(); - let slice_end = entry.append_op_result(arith::addi(slice_start, slice_len, location))?; + let slice_start = entry.arg(2)?; + let slice_len = entry.arg(3)?; + let slice_end = entry.addi(slice_start, slice_len, location)?; - let slice_start = entry.append_op_result(arith::addi(array_start, slice_start, location))?; - let slice_end = entry.append_op_result(arith::addi(array_start, slice_end, location))?; - let lhs_bound = entry.append_op_result(arith::cmpi( + let slice_start = entry.addi(array_start, slice_start, location)?; + let slice_end = entry.addi(array_start, slice_end, location)?; + let lhs_bound = entry.cmpi( context, CmpiPredicate::Uge, slice_start, array_start, location, - ))?; - let rhs_bound = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ule, - slice_end, - array_end, - location, - ))?; + )?; + let rhs_bound = entry.cmpi(context, CmpiPredicate::Ule, slice_end, array_end, location)?; let is_valid = entry.append_op_result(arith::andi(lhs_bound, rhs_bound, location))?; @@ -1243,25 +1140,21 @@ pub fn build_slice<'ctx, 'this>( { let elem_ty = elem_ty.build(context, helper, registry, metadata, &info.ty)?; - let value = entry.argument(1)?.into(); + let value = entry.arg(1)?; let value = slice_block.insert_value(context, location, value, slice_start, 1)?; let value = slice_block.insert_value(context, location, value, slice_end, 2)?; let elem_stride = slice_block.const_int(context, location, elem_layout.pad_to_align().size(), 64)?; let prepare = |value| { - let value = slice_block.append_op_result(arith::extui( - value, - IntegerType::new(context, 64).into(), - location, - ))?; - slice_block.append_op_result(arith::muli(value, elem_stride, location)) + let value = slice_block.extui(value, IntegerType::new(context, 64).into(), location)?; + slice_block.muli(value, elem_stride, location) }; let ptr = slice_block.extract_value( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), 0, )?; @@ -1275,7 +1168,7 @@ pub fn build_slice<'ctx, 'this>( let value_ptr = block.append_op_result(llvm::get_element_ptr_dynamic( context, ptr, - &[block.argument(0)?.into()], + &[block.arg(0)?], IntegerType::new(context, 8).into(), llvm::r#type::pointer(context, 0), location, @@ -1335,7 +1228,7 @@ pub fn build_slice<'ctx, 'this>( error_block, location, &info.signature.param_signatures[1].ty, - entry.argument(1)?.into(), + entry.arg(1)?, )?; error_block.append_operation(helper.br(1, &[range_check], location)); @@ -1365,14 +1258,10 @@ pub fn build_span_from_tuple<'ctx, 'this>( let container: Value = { // load box - entry.load(context, location, entry.argument(0)?.into(), struct_ty)? + entry.load(context, location, entry.arg(0)?, struct_ty)? }; // TODO: Maybe reuse the pointer? - entry.append_operation(ReallocBindingsMeta::free( - context, - entry.argument(0)?.into(), - location, - )?); + entry.append_operation(ReallocBindingsMeta::free(context, entry.arg(0)?, location)?); let fields = struct_type_info.fields().expect("should have fields"); let (field_ty, field_layout) = @@ -1404,10 +1293,8 @@ pub fn build_span_from_tuple<'ctx, 'this>( .append_op_result(ods::llvm::mlir_zero(context, pointer(context, 0), location).into())?; let field_size: Value = entry.const_int(context, location, field_stride, 64)?; - let array_len_value_i64 = - entry.append_op_result(arith::extui(array_len_value, field_size.r#type(), location))?; - let total_size = - entry.append_op_result(arith::muli(field_size, array_len_value_i64, location))?; + let array_len_value_i64 = entry.extui(array_len_value, field_size.r#type(), location)?; + let total_size = entry.muli(field_size, array_len_value_i64, location)?; let ptr = entry.append_op_result(ReallocBindingsMeta::realloc( context, ptr, total_size, location, @@ -1499,14 +1386,14 @@ pub fn build_tuple_from_span<'ctx, 'this>( let array_start = entry.extract_value( context, location, - entry.argument(0)?.into(), + entry.arg(0)?, IntegerType::new(context, 32).into(), 1, )?; let array_end = entry.extract_value( context, location, - entry.argument(0)?.into(), + entry.arg(0)?, IntegerType::new(context, 32).into(), 2, )?; @@ -1529,13 +1416,7 @@ pub fn build_tuple_from_span<'ctx, 'this>( ) }; - let len_matches = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - array_len, - tuple_len, - location, - ))?; + let len_matches = entry.cmpi(context, CmpiPredicate::Eq, array_len, tuple_len, location)?; let block_ok = helper.append_block(Block::new(&[])); let block_err = helper.append_block(Block::new(&[])); @@ -1551,33 +1432,23 @@ pub fn build_tuple_from_span<'ctx, 'this>( { let k0 = block_ok.const_int(context, location, 0, 32)?; - let starts_at_zero = block_ok.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - array_start, - k0, - location, - ))?; + let starts_at_zero = + block_ok.cmpi(context, CmpiPredicate::Eq, array_start, k0, location)?; let array_cap = block_ok.extract_value( context, location, - entry.argument(0)?.into(), + entry.arg(0)?, IntegerType::new(context, 32).into(), 3, )?; - let capacity_matches = block_ok.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - array_cap, - tuple_len, - location, - ))?; + let capacity_matches = + block_ok.cmpi(context, CmpiPredicate::Eq, array_cap, tuple_len, location)?; let array_ptr = block_ok.extract_value( context, location, - entry.argument(0)?.into(), + entry.arg(0)?, llvm::r#type::pointer(context, 0), 0, )?; @@ -1599,13 +1470,9 @@ pub fn build_tuple_from_span<'ctx, 'this>( { let elem_stride = block_clone.const_int(context, location, elem_layout.pad_to_align().size(), 64)?; - let tuple_len = block_clone.append_op_result(arith::extui( - tuple_len, - IntegerType::new(context, 64).into(), - location, - ))?; let tuple_len = - block_clone.append_op_result(arith::muli(tuple_len, elem_stride, location))?; + block_clone.extui(tuple_len, IntegerType::new(context, 64).into(), location)?; + let tuple_len = block_clone.muli(tuple_len, elem_stride, location)?; let box_ptr = block_clone .append_op_result(llvm::zero(llvm::r#type::pointer(context, 0), location))?; @@ -1613,13 +1480,9 @@ pub fn build_tuple_from_span<'ctx, 'this>( context, box_ptr, tuple_len, location, )?)?; - let elem_offset = block_clone.append_op_result(arith::extui( - array_start, - IntegerType::new(context, 64).into(), - location, - ))?; let elem_offset = - block_clone.append_op_result(arith::muli(elem_offset, elem_stride, location))?; + block_clone.extui(array_start, IntegerType::new(context, 64).into(), location)?; + let elem_offset = block_clone.muli(elem_offset, elem_stride, location)?; let elem_ptr = block_clone.append_op_result(llvm::get_element_ptr_dynamic( context, array_ptr, @@ -1670,7 +1533,7 @@ pub fn build_tuple_from_span<'ctx, 'this>( block_err, location, &info.signature.param_signatures[0].ty, - entry.argument(0)?.into(), + entry.arg(0)?, )?; block_err.append_operation(helper.br(1, &[], location)); diff --git a/src/libfuncs/bitwise.rs b/src/libfuncs/bitwise.rs index 3ec9218dd..294640853 100644 --- a/src/libfuncs/bitwise.rs +++ b/src/libfuncs/bitwise.rs @@ -25,11 +25,10 @@ pub fn build<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let bitwise = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let bitwise = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs = entry.argument(1)?.into(); - let rhs = entry.argument(2)?.into(); + let lhs = entry.arg(1)?; + let rhs = entry.arg(2)?; let logical_and = entry.append_op_result(arith::andi(lhs, rhs, location))?; let logical_xor = entry.append_op_result(arith::xori(lhs, rhs, location))?; diff --git a/src/libfuncs/bool.rs b/src/libfuncs/bool.rs index f0b2786a7..da431ad75 100644 --- a/src/libfuncs/bool.rs +++ b/src/libfuncs/bool.rs @@ -100,8 +100,8 @@ fn build_bool_binary<'ctx, 'this>( .trailing_zeros(); let tag_ty = IntegerType::new(context, tag_bits).into(); - let lhs = entry.argument(0)?.into(); - let rhs = entry.argument(1)?.into(); + let lhs = entry.arg(0)?; + let rhs = entry.arg(1)?; let lhs_tag = entry.extract_value(context, location, lhs, tag_ty, 0)?; @@ -149,7 +149,7 @@ pub fn build_bool_not<'ctx, 'this>( .trailing_zeros(); let tag_ty = IntegerType::new(context, tag_bits).into(); - let value = entry.argument(0)?.into(); + let value = entry.arg(0)?; let tag_value = entry.extract_value(context, location, value, tag_ty, 0)?; let const_1 = entry.const_int_from_type(context, location, 1, tag_ty)?; @@ -199,10 +199,10 @@ pub fn build_bool_to_felt252<'ctx, 'this>( .trailing_zeros(); let tag_ty = IntegerType::new(context, tag_bits).into(); - let value = entry.argument(0)?.into(); + let value = entry.arg(0)?; let tag_value = entry.extract_value(context, location, value, tag_ty, 0)?; - let result = entry.append_op_result(arith::extui(tag_value, felt252_ty, location))?; + let result = entry.extui(tag_value, felt252_ty, location)?; entry.append_operation(helper.br(0, &[result], location)); Ok(()) diff --git a/src/libfuncs/bounded_int.rs b/src/libfuncs/bounded_int.rs index 25bf13781..5da4785e0 100644 --- a/src/libfuncs/bounded_int.rs +++ b/src/libfuncs/bounded_int.rs @@ -77,8 +77,8 @@ fn build_add<'ctx, 'this>( _metadata: &mut MetadataStorage, info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let lhs_value = entry.argument(0)?.into(); - let rhs_value = entry.argument(1)?.into(); + let lhs_value = entry.arg(0)?; + let rhs_value = entry.arg(1)?; // Extract the ranges for the operands and the result type. let lhs_ty = registry.get_type(&info.signature.param_signatures[0].ty)?; @@ -126,18 +126,18 @@ fn build_add<'ctx, 'this>( let lhs_value = if compute_range.offset_bit_width() > lhs_width { if lhs_range.lower.sign() != Sign::Minus || lhs_ty.is_bounded_int(registry)? { - entry.append_op_result(arith::extui(lhs_value, compute_ty, location))? + entry.extui(lhs_value, compute_ty, location)? } else { - entry.append_op_result(arith::extsi(lhs_value, compute_ty, location))? + entry.extsi(lhs_value, compute_ty, location)? } } else { lhs_value }; let rhs_value = if compute_range.offset_bit_width() > rhs_width { if rhs_range.lower.sign() != Sign::Minus || rhs_ty.is_bounded_int(registry)? { - entry.append_op_result(arith::extui(rhs_value, compute_ty, location))? + entry.extui(rhs_value, compute_ty, location)? } else { - entry.append_op_result(arith::extsi(rhs_value, compute_ty, location))? + entry.extsi(rhs_value, compute_ty, location)? } } else { rhs_value @@ -151,7 +151,7 @@ fn build_add<'ctx, 'this>( }; let lhs_value = if lhs_offset != BigInt::ZERO { let lhs_offset = entry.const_int_from_type(context, location, lhs_offset, compute_ty)?; - entry.append_op_result(arith::addi(lhs_value, lhs_offset, location))? + entry.addi(lhs_value, lhs_offset, location)? } else { lhs_value }; @@ -163,13 +163,13 @@ fn build_add<'ctx, 'this>( }; let rhs_value = if rhs_offset != BigInt::ZERO { let rhs_offset = entry.const_int_from_type(context, location, rhs_offset, compute_ty)?; - entry.append_op_result(arith::addi(rhs_value, rhs_offset, location))? + entry.addi(rhs_value, rhs_offset, location)? } else { rhs_value }; // Compute the operation. - let res_value = entry.append_op_result(arith::addi(lhs_value, rhs_value, location))?; + let res_value = entry.addi(lhs_value, rhs_value, location)?; // Offset and truncate the result to the output type. let res_offset = &dst_range.lower - &compute_range.lower; @@ -181,11 +181,11 @@ fn build_add<'ctx, 'this>( }; let res_value = if dst_range.offset_bit_width() < compute_range.offset_bit_width() { - entry.append_op_result(arith::trunci( + entry.trunci( res_value, IntegerType::new(context, dst_range.offset_bit_width()).into(), location, - ))? + )? } else { res_value }; @@ -205,8 +205,8 @@ fn build_sub<'ctx, 'this>( _metadata: &mut MetadataStorage, info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let lhs_value = entry.argument(0)?.into(); - let rhs_value = entry.argument(1)?.into(); + let lhs_value = entry.arg(0)?; + let rhs_value = entry.arg(1)?; // Extract the ranges for the operands and the result type. let lhs_ty = registry.get_type(&info.signature.param_signatures[0].ty)?; @@ -254,18 +254,18 @@ fn build_sub<'ctx, 'this>( let lhs_value = if compute_range.offset_bit_width() > lhs_width { if lhs_range.lower.sign() != Sign::Minus || lhs_ty.is_bounded_int(registry)? { - entry.append_op_result(arith::extui(lhs_value, compute_ty, location))? + entry.extui(lhs_value, compute_ty, location)? } else { - entry.append_op_result(arith::extsi(lhs_value, compute_ty, location))? + entry.extsi(lhs_value, compute_ty, location)? } } else { lhs_value }; let rhs_value = if compute_range.offset_bit_width() > rhs_width { if rhs_range.lower.sign() != Sign::Minus || rhs_ty.is_bounded_int(registry)? { - entry.append_op_result(arith::extui(rhs_value, compute_ty, location))? + entry.extui(rhs_value, compute_ty, location)? } else { - entry.append_op_result(arith::extsi(rhs_value, compute_ty, location))? + entry.extsi(rhs_value, compute_ty, location)? } } else { rhs_value @@ -279,7 +279,7 @@ fn build_sub<'ctx, 'this>( }; let lhs_value = if lhs_offset != BigInt::ZERO { let lhs_offset = entry.const_int_from_type(context, location, lhs_offset, compute_ty)?; - entry.append_op_result(arith::addi(lhs_value, lhs_offset, location))? + entry.addi(lhs_value, lhs_offset, location)? } else { lhs_value }; @@ -291,7 +291,7 @@ fn build_sub<'ctx, 'this>( }; let rhs_value = if rhs_offset != BigInt::ZERO { let rhs_offset = entry.const_int_from_type(context, location, rhs_offset, compute_ty)?; - entry.append_op_result(arith::addi(rhs_value, rhs_offset, location))? + entry.addi(rhs_value, rhs_offset, location)? } else { rhs_value }; @@ -309,11 +309,11 @@ fn build_sub<'ctx, 'this>( }; let res_value = if dst_range.offset_bit_width() < compute_range.offset_bit_width() { - entry.append_op_result(arith::trunci( + entry.trunci( res_value, IntegerType::new(context, dst_range.offset_bit_width()).into(), location, - ))? + )? } else { res_value }; @@ -333,8 +333,8 @@ fn build_mul<'ctx, 'this>( _metadata: &mut MetadataStorage, info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let lhs_value = entry.argument(0)?.into(); - let rhs_value = entry.argument(1)?.into(); + let lhs_value = entry.arg(0)?; + let rhs_value = entry.arg(1)?; // Extract the ranges for the operands and the result type. let lhs_ty = registry.get_type(&info.signature.param_signatures[0].ty)?; @@ -383,18 +383,18 @@ fn build_mul<'ctx, 'this>( let lhs_value = if compute_range.zero_based_bit_width() > lhs_width { if lhs_range.lower.sign() != Sign::Minus || lhs_ty.is_bounded_int(registry)? { - entry.append_op_result(arith::extui(lhs_value, compute_ty, location))? + entry.extui(lhs_value, compute_ty, location)? } else { - entry.append_op_result(arith::extsi(lhs_value, compute_ty, location))? + entry.extsi(lhs_value, compute_ty, location)? } } else { lhs_value }; let rhs_value = if compute_range.zero_based_bit_width() > rhs_width { if rhs_range.lower.sign() != Sign::Minus || rhs_ty.is_bounded_int(registry)? { - entry.append_op_result(arith::extui(rhs_value, compute_ty, location))? + entry.extui(rhs_value, compute_ty, location)? } else { - entry.append_op_result(arith::extsi(rhs_value, compute_ty, location))? + entry.extsi(rhs_value, compute_ty, location)? } } else { rhs_value @@ -404,20 +404,20 @@ fn build_mul<'ctx, 'this>( let lhs_value = if lhs_ty.is_bounded_int(registry)? && lhs_range.lower != BigInt::ZERO { let lhs_offset = entry.const_int_from_type(context, location, lhs_range.lower, compute_ty)?; - entry.append_op_result(arith::addi(lhs_value, lhs_offset, location))? + entry.addi(lhs_value, lhs_offset, location)? } else { lhs_value }; let rhs_value = if rhs_ty.is_bounded_int(registry)? && rhs_range.lower != BigInt::ZERO { let rhs_offset = entry.const_int_from_type(context, location, rhs_range.lower, compute_ty)?; - entry.append_op_result(arith::addi(rhs_value, rhs_offset, location))? + entry.addi(rhs_value, rhs_offset, location)? } else { rhs_value }; // Compute the operation. - let res_value = entry.append_op_result(arith::muli(lhs_value, rhs_value, location))?; + let res_value = entry.muli(lhs_value, rhs_value, location)?; // Offset and truncate the result to the output type. let res_offset = (&dst_range.lower).max(&compute_range.lower).clone(); @@ -429,11 +429,11 @@ fn build_mul<'ctx, 'this>( }; let res_value = if dst_range.offset_bit_width() < compute_range.zero_based_bit_width() { - entry.append_op_result(arith::trunci( + entry.trunci( res_value, IntegerType::new(context, dst_range.offset_bit_width()).into(), location, - ))? + )? } else { res_value }; @@ -453,11 +453,10 @@ fn build_divrem<'ctx, 'this>( _metadata: &mut MetadataStorage, info: &BoundedIntDivRemConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs_value = entry.argument(1)?.into(); - let rhs_value = entry.argument(2)?.into(); + let lhs_value = entry.arg(1)?; + let rhs_value = entry.arg(2)?; // Extract the ranges for the operands and the result type. let lhs_ty = registry.get_type(&info.param_signatures()[1].ty)?; @@ -511,18 +510,18 @@ fn build_divrem<'ctx, 'this>( let lhs_value = if compute_range.zero_based_bit_width() > lhs_width { if lhs_range.lower.sign() != Sign::Minus || lhs_ty.is_bounded_int(registry)? { - entry.append_op_result(arith::extui(lhs_value, compute_ty, location))? + entry.extui(lhs_value, compute_ty, location)? } else { - entry.append_op_result(arith::extsi(lhs_value, compute_ty, location))? + entry.extsi(lhs_value, compute_ty, location)? } } else { lhs_value }; let rhs_value = if compute_range.zero_based_bit_width() > rhs_width { if rhs_range.lower.sign() != Sign::Minus || rhs_ty.is_bounded_int(registry)? { - entry.append_op_result(arith::extui(rhs_value, compute_ty, location))? + entry.extui(rhs_value, compute_ty, location)? } else { - entry.append_op_result(arith::extsi(rhs_value, compute_ty, location))? + entry.extsi(rhs_value, compute_ty, location)? } } else { rhs_value @@ -532,14 +531,14 @@ fn build_divrem<'ctx, 'this>( let lhs_value = if lhs_ty.is_bounded_int(registry)? && lhs_range.lower != BigInt::ZERO { let lhs_offset = entry.const_int_from_type(context, location, lhs_range.lower, compute_ty)?; - entry.append_op_result(arith::addi(lhs_value, lhs_offset, location))? + entry.addi(lhs_value, lhs_offset, location)? } else { lhs_value }; let rhs_value = if rhs_ty.is_bounded_int(registry)? && rhs_range.lower != BigInt::ZERO { let rhs_offset = entry.const_int_from_type(context, location, rhs_range.lower, compute_ty)?; - entry.append_op_result(arith::addi(rhs_value, rhs_offset, location))? + entry.addi(rhs_value, rhs_offset, location)? } else { rhs_value }; @@ -566,20 +565,20 @@ fn build_divrem<'ctx, 'this>( }; let div_value = if div_range.offset_bit_width() < compute_range.zero_based_bit_width() { - entry.append_op_result(arith::trunci( + entry.trunci( div_value, IntegerType::new(context, div_range.offset_bit_width()).into(), location, - ))? + )? } else { div_value }; let rem_value = if rem_range.offset_bit_width() < compute_range.zero_based_bit_width() { - entry.append_op_result(arith::trunci( + entry.trunci( rem_value, IntegerType::new(context, rem_range.offset_bit_width()).into(), location, - ))? + )? } else { rem_value }; @@ -598,9 +597,8 @@ fn build_constrain<'ctx, 'this>( _metadata: &mut MetadataStorage, info: &BoundedIntConstrainConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; - let src_value: Value = entry.argument(1)?.into(); + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; + let src_value: Value = entry.arg(1)?; let src_ty = registry.get_type(&info.param_signatures()[1].ty)?; let src_range = src_ty.integer_range(registry)?; @@ -620,7 +618,7 @@ fn build_constrain<'ctx, 'this>( let boundary = entry.const_int_from_type(context, location, info.boundary.clone(), src_value.r#type())?; - let is_lower = entry.append_op_result(arith::cmpi( + let is_lower = entry.cmpi( context, if src_range.lower.sign() == Sign::Minus { CmpiPredicate::Slt @@ -630,7 +628,7 @@ fn build_constrain<'ctx, 'this>( src_value, boundary, location, - ))?; + )?; let lower_block = helper.append_block(Block::new(&[])); let upper_block = helper.append_block(Block::new(&[])); @@ -659,11 +657,11 @@ fn build_constrain<'ctx, 'this>( }; let res_value = if src_width > lower_range.offset_bit_width() { - lower_block.append_op_result(arith::trunci( + lower_block.trunci( res_value, IntegerType::new(context, lower_range.offset_bit_width()).into(), location, - ))? + )? } else { res_value }; @@ -686,11 +684,11 @@ fn build_constrain<'ctx, 'this>( }; let res_value = if src_width > upper_range.offset_bit_width() { - upper_block.append_op_result(arith::trunci( + upper_block.trunci( res_value, IntegerType::new(context, upper_range.offset_bit_width()).into(), location, - ))? + )? } else { res_value }; @@ -711,7 +709,7 @@ fn build_is_zero<'ctx, 'this>( _metadata: &mut MetadataStorage, info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let src_value: Value = entry.argument(0)?.into(); + let src_value: Value = entry.arg(0)?; let src_ty = registry.get_type(&info.signature.param_signatures[0].ty)?; let src_range = src_ty.integer_range(registry)?; @@ -722,13 +720,7 @@ fn build_is_zero<'ctx, 'this>( ); let k0 = entry.const_int_from_type(context, location, 0, src_value.r#type())?; - let src_is_zero = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - src_value, - k0, - location, - ))?; + let src_is_zero = entry.cmpi(context, CmpiPredicate::Eq, src_value, k0, location)?; entry.append_operation(helper.cond_br( context, diff --git a/src/libfuncs/box.rs b/src/libfuncs/box.rs index 37135bfa4..224340959 100644 --- a/src/libfuncs/box.rs +++ b/src/libfuncs/box.rs @@ -2,7 +2,7 @@ //! //! A heap allocated value, which is internally a pointer that can't be null. -use super::LibfuncHelper; +use super::{BlockExt, LibfuncHelper}; use crate::{ error::Result, metadata::{realloc_bindings::ReallocBindingsMeta, MetadataStorage}, @@ -98,7 +98,7 @@ pub fn build_into_box<'ctx, 'this>( entry.append_operation(llvm::store( context, - entry.argument(0)?.into(), + entry.arg(0)?, ptr, location, LoadStoreOptions::new().align(Some(IntegerAttribute::new( @@ -133,7 +133,7 @@ pub fn build_unbox<'ctx, 'this>( let value = entry .append_operation(llvm::load( context, - entry.argument(0)?.into(), + entry.arg(0)?, inner_ty, location, LoadStoreOptions::new().align(Some(IntegerAttribute::new( @@ -144,11 +144,7 @@ pub fn build_unbox<'ctx, 'this>( .result(0)? .into(); - entry.append_operation(ReallocBindingsMeta::free( - context, - entry.argument(0)?.into(), - location, - )?); + entry.append_operation(ReallocBindingsMeta::free(context, entry.arg(0)?, location)?); entry.append_operation(helper.br(0, &[value], location)); Ok(()) diff --git a/src/libfuncs/bytes31.rs b/src/libfuncs/bytes31.rs index 0622fe6be..63e59450a 100644 --- a/src/libfuncs/bytes31.rs +++ b/src/libfuncs/bytes31.rs @@ -97,9 +97,9 @@ pub fn build_to_felt252<'ctx, 'this>( metadata, &info.branch_signatures()[0].vars[0].ty, )?; - let value: Value = entry.argument(0)?.into(); + let value: Value = entry.arg(0)?; - let result = entry.append_op_result(arith::extui(value, felt252_ty, location))?; + let result = entry.extui(value, felt252_ty, location)?; entry.append_operation(helper.br(0, &[result], location)); @@ -117,9 +117,9 @@ pub fn build_from_felt252<'ctx, 'this>( info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let value: Value = entry.argument(1)?.into(); + let value: Value = entry.arg(1)?; let felt252_ty = registry.build_type( context, @@ -145,13 +145,7 @@ pub fn build_from_felt252<'ctx, 'this>( location, ))?; - let is_ule = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ule, - value, - const_max, - location, - ))?; + let is_ule = entry.cmpi(context, CmpiPredicate::Ule, value, const_max, location)?; let block_success = helper.append_block(Block::new(&[])); let block_failure = helper.append_block(Block::new(&[])); @@ -166,7 +160,7 @@ pub fn build_from_felt252<'ctx, 'this>( location, )); - let value = block_success.append_op_result(arith::trunci(value, result_ty, location))?; + let value = block_success.trunci(value, result_ty, location)?; block_success.append_operation(helper.br(0, &[range_check, value], location)); block_failure.append_operation(helper.br(1, &[range_check], location)); diff --git a/src/libfuncs/cast.rs b/src/libfuncs/cast.rs index b4ab03d79..1e8ede5fd 100644 --- a/src/libfuncs/cast.rs +++ b/src/libfuncs/cast.rs @@ -55,9 +55,8 @@ pub fn build_downcast<'ctx, 'this>( _metadata: &mut MetadataStorage, info: &DowncastConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; - let src_value: Value = entry.argument(1)?.into(); + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; + let src_value: Value = entry.arg(1)?; if info.signature.param_signatures[1].ty == info.signature.branch_signatures[0].vars[1].ty { let k0 = entry.const_int(context, location, 0, 1)?; @@ -110,17 +109,17 @@ pub fn build_downcast<'ctx, 'this>( let src_value = if compute_width > src_width { if is_signed && !src_ty.is_bounded_int(registry)? && !src_ty.is_felt252(registry)? { - entry.append_op_result(arith::extsi( + entry.extsi( src_value, IntegerType::new(context, compute_width).into(), location, - ))? + )? } else { - entry.append_op_result(arith::extui( + entry.extui( src_value, IntegerType::new(context, compute_width).into(), location, - ))? + )? } } else { src_value @@ -138,13 +137,8 @@ pub fn build_downcast<'ctx, 'this>( HALF_PRIME.clone(), src_value.r#type(), )?; - let is_negative = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ugt, - src_value, - adj_offset, - location, - ))?; + let is_negative = + entry.cmpi(context, CmpiPredicate::Ugt, src_value, adj_offset, location)?; let k_prime = entry.const_int_from_type(context, location, PRIME.clone(), src_value.r#type())?; @@ -159,7 +153,7 @@ pub fn build_downcast<'ctx, 'this>( src_range.lower.clone(), src_value.r#type(), )?; - entry.append_op_result(arith::addi(src_value, dst_offset, location))? + entry.addi(src_value, dst_offset, location)? } else { src_value }; @@ -178,11 +172,11 @@ pub fn build_downcast<'ctx, 'this>( }; let dst_value = if dst_width < compute_width { - entry.append_op_result(arith::trunci( + entry.trunci( dst_value, IntegerType::new(context, dst_width).into(), location, - ))? + )? } else { dst_value }; @@ -204,7 +198,7 @@ pub fn build_downcast<'ctx, 'this>( dst_range.lower.clone(), src_value.r#type(), )?; - Some(entry.append_op_result(arith::cmpi( + Some(entry.cmpi( context, if !is_signed { CmpiPredicate::Uge @@ -214,7 +208,7 @@ pub fn build_downcast<'ctx, 'this>( src_value, dst_lower, location, - ))?) + )?) } else { None }; @@ -225,7 +219,7 @@ pub fn build_downcast<'ctx, 'this>( dst_range.upper.clone(), src_value.r#type(), )?; - Some(entry.append_op_result(arith::cmpi( + Some(entry.cmpi( context, if !is_signed { CmpiPredicate::Ult @@ -235,7 +229,7 @@ pub fn build_downcast<'ctx, 'this>( src_value, dst_upper, location, - ))?) + )?) } else { None }; @@ -265,11 +259,11 @@ pub fn build_downcast<'ctx, 'this>( }; let dst_value = if dst_width < compute_width { - entry.append_op_result(arith::trunci( + entry.trunci( dst_value, IntegerType::new(context, dst_width).into(), location, - ))? + )? } else { dst_value }; @@ -295,7 +289,7 @@ pub fn build_upcast<'ctx, 'this>( _metadata: &mut MetadataStorage, info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let src_value = entry.argument(0)?.into(); + let src_value = entry.arg(0)?; if info.signature.param_signatures[0].ty == info.signature.branch_signatures[0].vars[0].ty { entry.append_operation(helper.br(0, &[src_value], location)); @@ -345,17 +339,17 @@ pub fn build_upcast<'ctx, 'this>( let dst_value = if dst_width > src_width { if is_signed && !src_ty.is_bounded_int(registry)? { - entry.append_op_result(arith::extsi( + entry.extsi( src_value, IntegerType::new(context, dst_width).into(), location, - ))? + )? } else { - entry.append_op_result(arith::extui( + entry.extui( src_value, IntegerType::new(context, dst_width).into(), location, - ))? + )? } } else { src_value @@ -372,23 +366,17 @@ pub fn build_upcast<'ctx, 'this>( }, dst_value.r#type(), )?; - entry.append_op_result(arith::addi(dst_value, dst_offset, location))? + entry.addi(dst_value, dst_offset, location)? } else { dst_value }; let dst_value = if dst_ty.is_felt252(registry)? && src_range.lower.sign() == Sign::Minus { let k0 = entry.const_int(context, location, 0, 252)?; - let is_negative = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Slt, - dst_value, - k0, - location, - ))?; + let is_negative = entry.cmpi(context, CmpiPredicate::Slt, dst_value, k0, location)?; let k_prime = entry.const_int(context, location, PRIME.clone(), 252)?; - let adj_value = entry.append_op_result(arith::addi(dst_value, k_prime, location))?; + let adj_value = entry.addi(dst_value, k_prime, location)?; entry.append_op_result(arith::select(is_negative, adj_value, dst_value, location))? } else { diff --git a/src/libfuncs/circuit.rs b/src/libfuncs/circuit.rs index 8391b21ef..b7f0ad443 100644 --- a/src/libfuncs/circuit.rs +++ b/src/libfuncs/circuit.rs @@ -108,13 +108,7 @@ fn build_init_circuit_data<'ctx, 'this>( } _ => return Err(SierraAssertError::BadTypeInfo.into()), }; - let rc = increment_builtin_counter_by( - context, - entry, - location, - entry.argument(0)?.into(), - rc_usage, - )?; + let rc = increment_builtin_counter_by(context, entry, location, entry.arg(0)?, rc_usage)?; let k0 = entry.const_int(context, location, 0, 64)?; let accumulator_ty = &info.branch_signatures()[0].vars[1].ty; @@ -153,7 +147,7 @@ fn build_add_input<'ctx, 'this>( let accumulator_ctype = registry.get_type(accumulator_type_id)?; let accumulator_layout = accumulator_ctype.layout(registry)?; - let accumulator: Value = entry.argument(0)?.into(); + let accumulator: Value = entry.arg(0)?; // Get accumulator current length let current_length = entry.extract_value( @@ -166,13 +160,13 @@ fn build_add_input<'ctx, 'this>( // Check if last_insert: current_length == number_of_inputs - 1 let n_inputs_minus_1 = entry.const_int(context, location, n_inputs - 1, 64)?; - let last_insert = entry.append_op_result(arith::cmpi( + let last_insert = entry.cmpi( context, arith::CmpiPredicate::Eq, current_length, n_inputs_minus_1, location, - ))?; + )?; let middle_insert_block = helper.append_block(Block::new(&[])); let last_insert_block = helper.append_block(Block::new(&[])); @@ -190,8 +184,7 @@ fn build_add_input<'ctx, 'this>( { // Calculate next length: next_length = current_length + 1 let k1 = middle_insert_block.const_int(context, location, 1, 64)?; - let next_length = - middle_insert_block.append_op_result(arith::addi(current_length, k1, location))?; + let next_length = middle_insert_block.addi(current_length, k1, location)?; // Insert next_length into accumulator let accumulator = @@ -219,7 +212,7 @@ fn build_add_input<'ctx, 'this>( ))?; // Interpret u384 struct (input) as u384 integer - let u384_struct = entry.argument(1)?.into(); + let u384_struct = entry.arg(1)?; let new_input = u384_struct_to_integer(context, middle_insert_block, location, u384_struct)?; @@ -285,7 +278,7 @@ fn build_add_input<'ctx, 'this>( ); // Interpret u384 struct (input) as u384 integer - let u384_struct = entry.argument(1)?.into(); + let u384_struct = entry.arg(1)?; let new_input = u384_struct_to_integer(context, last_insert_block, location, u384_struct)?; // Get pointer to data end @@ -321,16 +314,10 @@ fn build_try_into_circuit_modulus<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let modulus = u384_struct_to_integer(context, entry, location, entry.argument(0)?.into())?; + let modulus = u384_struct_to_integer(context, entry, location, entry.arg(0)?)?; let k1 = entry.const_int(context, location, 1, 384)?; - let is_valid = entry.append_op_result(arith::cmpi( - context, - arith::CmpiPredicate::Ugt, - modulus, - k1, - location, - ))?; + let is_valid = entry.cmpi(context, arith::CmpiPredicate::Ugt, modulus, k1, location)?; entry.append_operation(helper.cond_br(context, is_valid, [0, 1], [&[modulus], &[]], location)); @@ -375,10 +362,10 @@ fn build_eval<'ctx, 'this>( CoreTypeConcrete::Circuit(CircuitTypeConcrete::Circuit(info)) => &info.circuit_info, _ => return Err(SierraAssertError::BadTypeInfo.into()), }; - let add_mod = entry.argument(0)?.into(); - let mul_mod = entry.argument(1)?.into(); - let circuit_data = entry.argument(3)?.into(); - let circuit_modulus = entry.argument(4)?.into(); + let add_mod = entry.arg(0)?; + let mul_mod = entry.arg(1)?; + let circuit_data = entry.arg(3)?; + let circuit_modulus = entry.arg(4)?; // arguments 5 and 6 are used to build the gate 0 (with constant value 1) // let zero = entry.argument(5)?; @@ -433,12 +420,12 @@ fn build_eval<'ctx, 'this>( { // We only consider mul gates evaluated before failure let mul_mod = { - let mul_mod_usage = err_block.append_op_result(arith::muli( - err_block.argument(0)?.into(), + let mul_mod_usage = err_block.muli( + err_block.arg(0)?, err_block.const_int(context, location, 4, 64)?, location, - ))?; - err_block.append_op_result(arith::addi(mul_mod, mul_mod_usage, location)) + )?; + err_block.addi(mul_mod, mul_mod_usage, location) }?; let partial_type_id = &info.branch_signatures()[1].vars[2].ty; @@ -509,67 +496,56 @@ fn build_gate_evaluation<'ctx, 'this>( // ADD: lhs + rhs = out (Some(lhs_value), Some(rhs_value), None) => { // Extend to avoid overflow - let lhs_value = block.append_op_result(arith::extui( + let lhs_value = block.extui( lhs_value, IntegerType::new(context, 384 + 1).into(), location, - ))?; - let rhs_value = block.append_op_result(arith::extui( + )?; + let rhs_value = block.extui( rhs_value, IntegerType::new(context, 384 + 1).into(), location, - ))?; - let circuit_modulus = block.append_op_result(arith::extui( + )?; + let circuit_modulus = block.extui( circuit_modulus, IntegerType::new(context, 384 + 1).into(), location, - ))?; + )?; // value = (lhs_value + rhs_value) % circuit_modulus - let value = - block.append_op_result(arith::addi(lhs_value, rhs_value, location))?; + let value = block.addi(lhs_value, rhs_value, location)?; let value = block.append_op_result(arith::remui(value, circuit_modulus, location))?; // Truncate back - let value = block.append_op_result(arith::trunci( - value, - IntegerType::new(context, 384).into(), - location, - ))?; + let value = + block.trunci(value, IntegerType::new(context, 384).into(), location)?; values[add_gate_offset.output] = Some(value); } // SUB: lhs = out - rhs (None, Some(rhs_value), Some(output_value)) => { // Extend to avoid overflow - let rhs_value = block.append_op_result(arith::extui( + let rhs_value = block.extui( rhs_value, IntegerType::new(context, 384 + 1).into(), location, - ))?; - let output_value = block.append_op_result(arith::extui( + )?; + let output_value = block.extui( output_value, IntegerType::new(context, 384 + 1).into(), location, - ))?; - let circuit_modulus = block.append_op_result(arith::extui( + )?; + let circuit_modulus = block.extui( circuit_modulus, IntegerType::new(context, 384 + 1).into(), location, - ))?; + )?; // value = (output_value + circuit_modulus - rhs_value) % circuit_modulus - let value = block.append_op_result(arith::addi( - output_value, - circuit_modulus, - location, - ))?; + let value = block.addi(output_value, circuit_modulus, location)?; let value = block.append_op_result(arith::subi(value, rhs_value, location))?; let value = block.append_op_result(arith::remui(value, circuit_modulus, location))?; // Truncate back - let value = block.append_op_result(arith::trunci( - value, - IntegerType::new(context, 384).into(), - location, - ))?; + let value = + block.trunci(value, IntegerType::new(context, 384).into(), location)?; values[add_gate_offset.lhs] = Some(value); } // We can't solve this add gate yet, so we break from the loop @@ -592,47 +568,43 @@ fn build_gate_evaluation<'ctx, 'this>( // MUL: lhs * rhs = out (Some(lhs_value), Some(rhs_value), None) => { // Extend to avoid overflow - let lhs_value = block.append_op_result(arith::extui( + let lhs_value = block.extui( lhs_value, IntegerType::new(context, 384 * 2).into(), location, - ))?; - let rhs_value = block.append_op_result(arith::extui( + )?; + let rhs_value = block.extui( rhs_value, IntegerType::new(context, 384 * 2).into(), location, - ))?; - let circuit_modulus = block.append_op_result(arith::extui( + )?; + let circuit_modulus = block.extui( circuit_modulus, IntegerType::new(context, 384 * 2).into(), location, - ))?; + )?; // value = (lhs_value * rhs_value) % circuit_modulus - let value = - block.append_op_result(arith::muli(lhs_value, rhs_value, location))?; + let value = block.muli(lhs_value, rhs_value, location)?; let value = block.append_op_result(arith::remui(value, circuit_modulus, location))?; // Truncate back - let value = block.append_op_result(arith::trunci( - value, - IntegerType::new(context, 384).into(), - location, - ))?; + let value = + block.trunci(value, IntegerType::new(context, 384).into(), location)?; values[output] = Some(value) } // INV: lhs = 1 / rhs (None, Some(rhs_value), Some(_)) => { // Extend to avoid overflow - let rhs_value = block.append_op_result(arith::extui( + let rhs_value = block.extui( rhs_value, IntegerType::new(context, 384 * 2).into(), location, - ))?; - let circuit_modulus = block.append_op_result(arith::extui( + )?; + let circuit_modulus = block.extui( circuit_modulus, IntegerType::new(context, 384 * 2).into(), location, - ))?; + )?; let integer_type = rhs_value.r#type(); // Apply egcd to find gcd and inverse @@ -644,8 +616,8 @@ fn build_gate_evaluation<'ctx, 'this>( rhs_value, circuit_modulus, )?; - let gcd = egcd_result_block.argument(0)?.into(); - let inverse = egcd_result_block.argument(1)?.into(); + let gcd = egcd_result_block.arg(0)?; + let inverse = egcd_result_block.arg(1)?; block = egcd_result_block; // if the gcd is not 1, then fail (a and b are not coprimes) @@ -656,13 +628,7 @@ fn build_gate_evaluation<'ctx, 'this>( gate_offset_idx, IntegerType::new(context, 64).into(), )?; - let has_inverse = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - gcd, - one, - location, - ))?; + let has_inverse = block.cmpi(context, CmpiPredicate::Eq, gcd, one, location)?; let has_inverse_block = helper.append_block(Block::new(&[])); block.append_operation(cf::cond_br( context, @@ -687,8 +653,7 @@ fn build_gate_evaluation<'ctx, 'this>( )) .result(0)? .into(); - let wrapped_inverse = - block.append_op_result(arith::addi(inverse, circuit_modulus, location))?; + let wrapped_inverse = block.addi(inverse, circuit_modulus, location)?; let inverse = block.append_op_result(arith::select( is_negative, wrapped_inverse, @@ -697,11 +662,8 @@ fn build_gate_evaluation<'ctx, 'this>( ))?; // Truncate back - let inverse = block.append_op_result(arith::trunci( - inverse, - IntegerType::new(context, 384).into(), - location, - ))?; + let inverse = + block.trunci(inverse, IntegerType::new(context, 384).into(), location)?; values[lhs] = Some(inverse); } @@ -737,8 +699,8 @@ fn build_failure_guarantee_verify<'ctx, 'this>( metadata: &mut MetadataStorage, info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let rc = entry.argument(0)?.into(); - let mul_mod = entry.argument(1)?.into(); + let rc = entry.arg(0)?; + let mul_mod = entry.arg(1)?; let rc = increment_builtin_counter_by(context, entry, location, rc, 4)?; let mul_mod = increment_builtin_counter_by(context, entry, location, mul_mod, 4)?; @@ -834,7 +796,7 @@ fn build_get_output<'ctx, 'this>( let output_idx = output_offset_idx - circuit_info.n_inputs - 1; - let outputs = entry.argument(0)?.into(); + let outputs = entry.arg(0)?; let output_integer = entry.extract_value( context, location, @@ -862,40 +824,40 @@ fn u384_struct_to_integer<'a>( ) -> Result> { let u96_type = IntegerType::new(context, 96).into(); - let limb1 = block.append_op_result(arith::extui( + let limb1 = block.extui( block.extract_value(context, location, u384_struct, u96_type, 0)?, IntegerType::new(context, 384).into(), location, - ))?; + )?; let limb2 = { - let limb = block.append_op_result(arith::extui( + let limb = block.extui( block.extract_value(context, location, u384_struct, u96_type, 1)?, IntegerType::new(context, 384).into(), location, - ))?; + )?; let k96 = block.const_int(context, location, 96, 384)?; - block.append_op_result(arith::shli(limb, k96, location))? + block.shli(limb, k96, location)? }; let limb3 = { - let limb = block.append_op_result(arith::extui( + let limb = block.extui( block.extract_value(context, location, u384_struct, u96_type, 2)?, IntegerType::new(context, 384).into(), location, - ))?; + )?; let k192 = block.const_int(context, location, 96 * 2, 384)?; - block.append_op_result(arith::shli(limb, k192, location))? + block.shli(limb, k192, location)? }; let limb4 = { - let limb = block.append_op_result(arith::extui( + let limb = block.extui( block.extract_value(context, location, u384_struct, u96_type, 3)?, IntegerType::new(context, 384).into(), location, - ))?; + )?; let k288 = block.const_int(context, location, 96 * 3, 384)?; - block.append_op_result(arith::shli(limb, k288, location))? + block.shli(limb, k288, location)? }; let value = block.append_op_result(arith::ori(limb1, limb2, location))?; @@ -913,25 +875,21 @@ fn u384_integer_to_struct<'a>( ) -> Result> { let u96_type = IntegerType::new(context, 96).into(); - let limb1 = block.append_op_result(arith::trunci( - integer, - IntegerType::new(context, 96).into(), - location, - ))?; + let limb1 = block.trunci(integer, IntegerType::new(context, 96).into(), location)?; let limb2 = { let k96 = block.const_int(context, location, 96, 384)?; - let limb = block.append_op_result(arith::shrui(integer, k96, location))?; - block.append_op_result(arith::trunci(limb, u96_type, location))? + let limb = block.shrui(integer, k96, location)?; + block.trunci(limb, u96_type, location)? }; let limb3 = { let k192 = block.const_int(context, location, 96 * 2, 384)?; - let limb = block.append_op_result(arith::shrui(integer, k192, location))?; - block.append_op_result(arith::trunci(limb, u96_type, location))? + let limb = block.shrui(integer, k192, location)?; + block.trunci(limb, u96_type, location)? }; let limb4 = { let k288 = block.const_int(context, location, 96 * 3, 384)?; - let limb = block.append_op_result(arith::shrui(integer, k288, location))?; - block.append_op_result(arith::trunci(limb, u96_type, location))? + let limb = block.shrui(integer, k288, location)?; + block.trunci(limb, u96_type, location)? }; let struct_type = llvm::r#type::r#struct( @@ -997,18 +955,18 @@ fn build_euclidean_algorithm<'ctx, 'this>( // -- Loop body -- // Arguments are rem_(i-1), rem, inv_(i-1), inv - let prev_remainder = loop_block.argument(0)?.into(); - let remainder = loop_block.argument(1)?.into(); - let prev_inverse = loop_block.argument(2)?.into(); - let inverse = loop_block.argument(3)?.into(); + let prev_remainder = loop_block.arg(0)?; + let remainder = loop_block.arg(1)?; + let prev_inverse = loop_block.arg(2)?; + let inverse = loop_block.arg(3)?; // First calculate q = rem_(i-1)/rem_i, rounded down let quotient = loop_block.append_op_result(arith::divui(prev_remainder, remainder, location))?; // Then r_(i+1) = r_(i-1) - q * r_i, and inv_(i+1) = inv_(i-1) - q * inv_i - let rem_times_quo = loop_block.append_op_result(arith::muli(remainder, quotient, location))?; - let inv_times_quo = loop_block.append_op_result(arith::muli(inverse, quotient, location))?; + let rem_times_quo = loop_block.muli(remainder, quotient, location)?; + let inv_times_quo = loop_block.muli(inverse, quotient, location)?; let next_remainder = loop_block.append_op_result(arith::subi(prev_remainder, rem_times_quo, location))?; let next_inverse = @@ -1020,13 +978,8 @@ fn build_euclidean_algorithm<'ctx, 'this>( // - inv_i is the bezout coefficient x let zero = loop_block.const_int_from_type(context, location, 0, integer_type)?; - let next_remainder_eq_zero = loop_block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - next_remainder, - zero, - location, - ))?; + let next_remainder_eq_zero = + loop_block.cmpi(context, CmpiPredicate::Eq, next_remainder, zero, location)?; loop_block.append_operation(cf::cond_br( context, next_remainder_eq_zero, diff --git a/src/libfuncs/debug.rs b/src/libfuncs/debug.rs index 1afcd5452..770db996a 100644 --- a/src/libfuncs/debug.rs +++ b/src/libfuncs/debug.rs @@ -61,21 +61,21 @@ pub fn build_print<'ctx>( let values_ptr = entry.extract_value( context, location, - entry.argument(0)?.into(), + entry.arg(0)?, llvm::r#type::pointer(context, 0), 0, )?; let values_start = entry.extract_value( context, location, - entry.argument(0)?.into(), + entry.arg(0)?, IntegerType::new(context, 32).into(), 1, )?; let values_end = entry.extract_value( context, location, - entry.argument(0)?.into(), + entry.arg(0)?, IntegerType::new(context, 32).into(), 2, )?; @@ -87,11 +87,8 @@ pub fn build_print<'ctx>( let values_len = entry.append_op_result(arith::subi(values_end, values_start, location))?; let values_ptr = { - let values_start = entry.append_op_result(arith::extui( - values_start, - IntegerType::new(context, 64).into(), - location, - ))?; + let values_start = + entry.extui(values_start, IntegerType::new(context, 64).into(), location)?; entry.append_op_result(llvm::get_element_ptr_dynamic( context, @@ -112,22 +109,11 @@ pub fn build_print<'ctx>( metadata .get::() .ok_or(Error::MissingMetadata)? - .invoke_override( - context, - entry, - location, - input_ty, - entry.argument(0)?.into(), - )?; + .invoke_override(context, entry, location, input_ty, entry.arg(0)?)?; let k0 = entry.const_int(context, location, 0, 32)?; - let return_code_is_ok = entry.append_op_result(arith::cmpi( - context, - arith::CmpiPredicate::Eq, - return_code, - k0, - location, - ))?; + let return_code_is_ok = + entry.cmpi(context, arith::CmpiPredicate::Eq, return_code, k0, location)?; cf::assert( context, return_code_is_ok, diff --git a/src/libfuncs/drop.rs b/src/libfuncs/drop.rs index 62d62428e..eec53cfce 100644 --- a/src/libfuncs/drop.rs +++ b/src/libfuncs/drop.rs @@ -5,7 +5,7 @@ //! //! However, types like an array need manual dropping. -use super::LibfuncHelper; +use super::{BlockExt, LibfuncHelper}; use crate::{ error::Result, metadata::{drop_overrides::DropOverridesMeta, MetadataStorage}, @@ -47,7 +47,7 @@ pub fn build<'ctx, 'this>( entry, location, &info.signature.param_signatures[0].ty, - entry.argument(0)?.into(), + entry.arg(0)?, )?; } diff --git a/src/libfuncs/dup.rs b/src/libfuncs/dup.rs index 87760c4be..a0f6112b4 100644 --- a/src/libfuncs/dup.rs +++ b/src/libfuncs/dup.rs @@ -5,7 +5,7 @@ //! //! However, types like an array need special handling. -use super::LibfuncHelper; +use super::{BlockExt, LibfuncHelper}; use crate::{ error::Result, metadata::{dup_overrides::DupOverridesMeta, MetadataStorage}, @@ -43,7 +43,7 @@ pub fn build<'ctx, 'this>( entry, location, &info.signature.param_signatures[0].ty, - entry.argument(0)?.into(), + entry.arg(0)?, )?; entry.append_operation(helper.br(0, &[values.0, values.1], location)); diff --git a/src/libfuncs/ec.rs b/src/libfuncs/ec.rs index 2994be1a9..12b8bdfae 100644 --- a/src/libfuncs/ec.rs +++ b/src/libfuncs/ec.rs @@ -83,20 +83,19 @@ pub fn build_is_zero<'ctx, 'this>( let y = entry.extract_value( context, location, - entry.argument(0)?.into(), + entry.arg(0)?, IntegerType::new(context, 252).into(), 1, )?; let k0 = entry.const_int(context, location, 0, 252)?; - let y_is_zero = - entry.append_op_result(arith::cmpi(context, CmpiPredicate::Eq, y, k0, location))?; + let y_is_zero = entry.cmpi(context, CmpiPredicate::Eq, y, k0, location)?; entry.append_operation(helper.cond_br( context, y_is_zero, [0, 1], - [&[], &[entry.argument(0)?.into()]], + [&[], &[entry.arg(0)?]], location, )); Ok(()) @@ -115,7 +114,7 @@ pub fn build_neg<'ctx, 'this>( let y = entry.extract_value( context, location, - entry.argument(0)?.into(), + entry.arg(0)?, IntegerType::new(context, 252).into(), 1, )?; @@ -123,8 +122,7 @@ pub fn build_neg<'ctx, 'this>( let k_prime = entry.const_int(context, location, PRIME.clone(), 252)?; let k0 = entry.const_int(context, location, 0, 252)?; - let y_is_zero = - entry.append_op_result(arith::cmpi(context, CmpiPredicate::Eq, y, k0, location))?; + let y_is_zero = entry.cmpi(context, CmpiPredicate::Eq, y, k0, location)?; let y_neg = entry.append_op_result(arith::subi(k_prime, y, location))?; let y_neg = entry.append_op_result( @@ -134,7 +132,7 @@ pub fn build_neg<'ctx, 'this>( .build()?, )?; - let result = entry.insert_value(context, location, entry.argument(0)?.into(), y_neg, 1)?; + let result = entry.insert_value(context, location, entry.arg(0)?, y_neg, 1)?; entry.append_operation(helper.br(0, &[result], location)); Ok(()) @@ -150,8 +148,7 @@ pub fn build_point_from_x<'ctx, 'this>( metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let ec_point_ty = llvm::r#type::r#struct( context, @@ -170,7 +167,7 @@ pub fn build_point_from_x<'ctx, 'this>( )?; let point = entry.append_op_result(llvm::undef(ec_point_ty, location))?; - let point = entry.insert_value(context, location, point, entry.argument(1)?.into(), 0)?; + let point = entry.insert_value(context, location, point, entry.arg(1)?, 0)?; entry.store(context, location, point_ptr, point)?; let result = metadata @@ -226,8 +223,8 @@ pub fn build_state_add<'ctx, 'this>( get_integer_layout(252).align(), )?; - entry.store(context, location, state_ptr, entry.argument(0)?.into())?; - entry.store(context, location, point_ptr, entry.argument(1)?.into())?; + entry.store(context, location, state_ptr, entry.arg(0)?)?; + entry.store(context, location, point_ptr, entry.arg(1)?)?; metadata .get_mut::() @@ -250,8 +247,7 @@ pub fn build_state_add_mul<'ctx, 'this>( metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let ec_op = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let ec_op = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let felt252_ty = IntegerType::new(context, 252).into(); let ec_state_ty = llvm::r#type::r#struct( @@ -280,9 +276,9 @@ pub fn build_state_add_mul<'ctx, 'this>( get_integer_layout(252).align(), )?; - entry.store(context, location, state_ptr, entry.argument(1)?.into())?; - entry.store(context, location, scalar_ptr, entry.argument(2)?.into())?; - entry.store(context, location, point_ptr, entry.argument(3)?.into())?; + entry.store(context, location, state_ptr, entry.arg(1)?)?; + entry.store(context, location, scalar_ptr, entry.arg(2)?)?; + entry.store(context, location, point_ptr, entry.arg(3)?)?; metadata .get_mut::() @@ -328,7 +324,7 @@ pub fn build_state_finalize<'ctx, 'this>( get_integer_layout(252).align(), )?; - entry.store(context, location, state_ptr, entry.argument(0)?.into())?; + entry.store(context, location, state_ptr, entry.arg(0)?)?; let is_zero = metadata .get_mut::() @@ -409,8 +405,8 @@ pub fn build_try_new<'ctx, 'this>( )?; let point = entry.append_op_result(llvm::undef(ec_point_ty, location))?; - let point = entry.insert_value(context, location, point, entry.argument(0)?.into(), 0)?; - let point = entry.insert_value(context, location, point, entry.argument(1)?.into(), 1)?; + let point = entry.insert_value(context, location, point, entry.arg(0)?, 0)?; + let point = entry.insert_value(context, location, point, entry.arg(1)?, 1)?; entry.store(context, location, point_ptr, point)?; @@ -438,7 +434,7 @@ pub fn build_unwrap_point<'ctx, 'this>( let x = entry.extract_value( context, location, - entry.argument(0)?.into(), + entry.arg(0)?, registry.build_type( context, helper, @@ -452,7 +448,7 @@ pub fn build_unwrap_point<'ctx, 'this>( let y = entry.extract_value( context, location, - entry.argument(0)?.into(), + entry.arg(0)?, registry.build_type( context, helper, diff --git a/src/libfuncs/enum.rs b/src/libfuncs/enum.rs index 150c1d481..fecb73082 100644 --- a/src/libfuncs/enum.rs +++ b/src/libfuncs/enum.rs @@ -89,7 +89,7 @@ pub fn build_init<'ctx, 'this>( location, helper, metadata, - entry.argument(0)?.into(), + entry.arg(0)?, &info.branch_signatures()[0].vars[0].ty, &info.signature.param_signatures[0].ty, info.index, @@ -224,7 +224,7 @@ pub fn build_from_bounded_int<'ctx, 'this>( let tag_bits = info.n_variants.next_power_of_two().trailing_zeros(); let tag_type = IntegerType::new(context, tag_bits); - let mut tag_value: Value = entry.argument(0)?.into(); + let mut tag_value: Value = entry.arg(0)?; match tag_type.width().cmp(&varaint_selector_type.width()) { std::cmp::Ordering::Less => { @@ -280,7 +280,7 @@ pub fn build_match<'ctx, 'this>( entry.append_operation(llvm::unreachable(location)); } 1 => { - entry.append_operation(helper.br(0, &[entry.argument(0)?.into()], location)); + entry.append_operation(helper.br(0, &[entry.arg(0)?], location)); } _ => { let (layout, (tag_ty, _), variant_tys) = crate::types::r#enum::get_type_for_variants( @@ -304,7 +304,7 @@ pub fn build_match<'ctx, 'this>( )?, layout.align(), )?; - entry.store(context, location, stack_ptr, entry.argument(0)?.into())?; + entry.store(context, location, stack_ptr, entry.arg(0)?)?; let tag_val = entry.load(context, location, stack_ptr, tag_ty)?; (Some(stack_ptr), tag_val) @@ -312,7 +312,7 @@ pub fn build_match<'ctx, 'this>( let tag_val = entry .append_operation(llvm::extract_value( context, - entry.argument(0)?.into(), + entry.arg(0)?, DenseI64ArrayAttribute::new(context, &[0]), tag_ty, location, @@ -383,7 +383,7 @@ pub fn build_match<'ctx, 'this>( // - Either it's a C-style enum and all payloads have the same type. // - Or the enum only has a single non-memory-allocated variant. if variant_ids.len() == 1 { - entry.argument(0)?.into() + entry.arg(0)? } else { assert!(registry.get_type(&variant_ids[i])?.is_zst(registry)?); block @@ -438,7 +438,7 @@ pub fn build_snapshot_match<'ctx, 'this>( entry.append_operation(llvm::unreachable(location)); } 1 => { - entry.append_operation(helper.br(0, &[entry.argument(0)?.into()], location)); + entry.append_operation(helper.br(0, &[entry.arg(0)?], location)); } _ => { let (layout, (tag_ty, _), variant_tys) = crate::types::r#enum::get_type_for_variants( @@ -462,13 +462,12 @@ pub fn build_snapshot_match<'ctx, 'this>( )?, layout.align(), )?; - entry.store(context, location, stack_ptr, entry.argument(0)?.into())?; + entry.store(context, location, stack_ptr, entry.arg(0)?)?; let tag_val = entry.load(context, location, stack_ptr, tag_ty)?; (Some(stack_ptr), tag_val) } else { - let tag_val = - entry.extract_value(context, location, entry.argument(0)?.into(), tag_ty, 0)?; + let tag_val = entry.extract_value(context, location, entry.arg(0)?, tag_ty, 0)?; (None, tag_val) }; @@ -526,7 +525,7 @@ pub fn build_snapshot_match<'ctx, 'this>( // - Either it's a C-style enum and all payloads have the same type. // - Or the enum only has a single non-memory-allocated variant. if variant_ids.len() == 1 { - entry.argument(0)?.into() + entry.arg(0)? } else { assert!(registry.get_type(&variant_ids[i])?.is_zst(registry)?); block.append_op_result(llvm::undef(payload_ty, location))? diff --git a/src/libfuncs/felt252.rs b/src/libfuncs/felt252.rs index 7c90ea948..ad8225524 100644 --- a/src/libfuncs/felt252.rs +++ b/src/libfuncs/felt252.rs @@ -76,11 +76,9 @@ pub fn build_binary_operation<'ctx, 'this>( let i512 = IntegerType::new(context, 512).into(); let (op, lhs, rhs) = match info { - Felt252BinaryOperationConcrete::WithVar(operation) => ( - operation.operator, - entry.argument(0)?.into(), - entry.argument(1)?.into(), - ), + Felt252BinaryOperationConcrete::WithVar(operation) => { + (operation.operator, entry.arg(0)?, entry.arg(1)?) + } Felt252BinaryOperationConcrete::WithConst(operation) => { let value = match operation.c.sign() { Sign::Minus => (&operation.c + BigInt::from_biguint(Sign::Minus, PRIME.clone())) @@ -92,25 +90,20 @@ pub fn build_binary_operation<'ctx, 'this>( // TODO: Ensure that the constant is on the correct side of the operation. let rhs = entry.const_int_from_type(context, location, value, felt252_ty)?; - (operation.operator, entry.argument(0)?.into(), rhs) + (operation.operator, entry.arg(0)?, rhs) } }; let result = match op { Felt252BinaryOperator::Add => { - let lhs = entry.append_op_result(arith::extui(lhs, i256, location))?; - let rhs = entry.append_op_result(arith::extui(rhs, i256, location))?; - let result = entry.append_op_result(arith::addi(lhs, rhs, location))?; + let lhs = entry.extui(lhs, i256, location)?; + let rhs = entry.extui(rhs, i256, location)?; + let result = entry.addi(lhs, rhs, location)?; let prime = entry.const_int_from_type(context, location, PRIME.clone(), i256)?; let result_mod = entry.append_op_result(arith::subi(result, prime, location))?; - let is_out_of_range = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Uge, - result, - prime, - location, - ))?; + let is_out_of_range = + entry.cmpi(context, CmpiPredicate::Uge, result, prime, location)?; let result = entry.append_op_result(arith::select( is_out_of_range, @@ -118,22 +111,16 @@ pub fn build_binary_operation<'ctx, 'this>( result, location, ))?; - entry.append_op_result(arith::trunci(result, felt252_ty, location))? + entry.trunci(result, felt252_ty, location)? } Felt252BinaryOperator::Sub => { - let lhs = entry.append_op_result(arith::extui(lhs, i256, location))?; - let rhs = entry.append_op_result(arith::extui(rhs, i256, location))?; + let lhs = entry.extui(lhs, i256, location)?; + let rhs = entry.extui(rhs, i256, location)?; let result = entry.append_op_result(arith::subi(lhs, rhs, location))?; let prime = entry.const_int_from_type(context, location, PRIME.clone(), i256)?; - let result_mod = entry.append_op_result(arith::addi(result, prime, location))?; - let is_out_of_range = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ult, - lhs, - rhs, - location, - ))?; + let result_mod = entry.addi(result, prime, location)?; + let is_out_of_range = entry.cmpi(context, CmpiPredicate::Ult, lhs, rhs, location)?; let result = entry.append_op_result(arith::select( is_out_of_range, @@ -141,22 +128,17 @@ pub fn build_binary_operation<'ctx, 'this>( result, location, ))?; - entry.append_op_result(arith::trunci(result, felt252_ty, location))? + entry.trunci(result, felt252_ty, location)? } Felt252BinaryOperator::Mul => { - let lhs = entry.append_op_result(arith::extui(lhs, i512, location))?; - let rhs = entry.append_op_result(arith::extui(rhs, i512, location))?; - let result = entry.append_op_result(arith::muli(lhs, rhs, location))?; + let lhs = entry.extui(lhs, i512, location)?; + let rhs = entry.extui(rhs, i512, location)?; + let result = entry.muli(lhs, rhs, location)?; let prime = entry.const_int_from_type(context, location, PRIME.clone(), i512)?; let result_mod = entry.append_op_result(arith::remui(result, prime, location))?; - let is_out_of_range = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Uge, - result, - prime, - location, - ))?; + let is_out_of_range = + entry.cmpi(context, CmpiPredicate::Uge, result, prime, location)?; let result = entry.append_op_result(arith::select( is_out_of_range, @@ -164,7 +146,7 @@ pub fn build_binary_operation<'ctx, 'this>( result, location, ))?; - entry.append_op_result(arith::trunci(result, felt252_ty, location))? + entry.trunci(result, felt252_ty, location)? } Felt252BinaryOperator::Div => { // The extended euclidean algorithm calculates the greatest common divisor of two integers, @@ -190,7 +172,7 @@ pub fn build_binary_operation<'ctx, 'this>( // This order is chosen because if we reverse them, then the first iteration will just swap them let prev_remainder = start_block.const_int_from_type(context, location, PRIME.clone(), i512)?; - let remainder = start_block.argument(0)?.into(); + let remainder = start_block.arg(0)?; // Similarly we'll calculate another series which starts 0,1,... and from which we will retrieve the modular inverse of a let prev_inverse = start_block.const_int_from_type(context, location, 0, i512)?; let inverse = start_block.const_int_from_type(context, location, 1, i512)?; @@ -202,19 +184,17 @@ pub fn build_binary_operation<'ctx, 'this>( //---Loop body--- // Arguments are rem_(i-1), rem, inv_(i-1), inv - let prev_remainder = loop_block.argument(0)?.into(); - let remainder = loop_block.argument(1)?.into(); - let prev_inverse = loop_block.argument(2)?.into(); - let inverse = loop_block.argument(3)?.into(); + let prev_remainder = loop_block.arg(0)?; + let remainder = loop_block.arg(1)?; + let prev_inverse = loop_block.arg(2)?; + let inverse = loop_block.arg(3)?; // First calculate q = rem_(i-1)/rem_i, rounded down let quotient = loop_block.append_op_result(arith::divui(prev_remainder, remainder, location))?; // Then r_(i+1) = r_(i-1) - q * r_i, and inv_(i+1) = inv_(i-1) - q * inv_i - let rem_times_quo = - loop_block.append_op_result(arith::muli(remainder, quotient, location))?; - let inv_times_quo = - loop_block.append_op_result(arith::muli(inverse, quotient, location))?; + let rem_times_quo = loop_block.muli(remainder, quotient, location)?; + let inv_times_quo = loop_block.muli(inverse, quotient, location)?; let next_remainder = loop_block.append_op_result(arith::subi( prev_remainder, rem_times_quo, @@ -225,13 +205,8 @@ pub fn build_binary_operation<'ctx, 'this>( // If r_(i+1) is 0, then inv_i is the inverse let zero = loop_block.const_int_from_type(context, location, 0, i512)?; - let next_remainder_eq_zero = loop_block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - next_remainder, - zero, - location, - ))?; + let next_remainder_eq_zero = + loop_block.cmpi(context, CmpiPredicate::Eq, next_remainder, zero, location)?; loop_block.append_operation(cf::cond_br( context, next_remainder_eq_zero, @@ -260,8 +235,7 @@ pub fn build_binary_operation<'ctx, 'this>( // if the inverse is < 0, add PRIME let prime = negative_check_block.const_int_from_type(context, location, PRIME.clone(), i512)?; - let wrapped_inverse = - negative_check_block.append_op_result(arith::addi(inverse, prime, location))?; + let wrapped_inverse = negative_check_block.addi(inverse, prime, location)?; let inverse = negative_check_block.append_op_result(arith::select( is_negative, wrapped_inverse, @@ -276,25 +250,19 @@ pub fn build_binary_operation<'ctx, 'this>( // Div Logic Start // Fetch operands - let lhs = entry.append_op_result(arith::extui(lhs, i512, location))?; - let rhs = entry.append_op_result(arith::extui(rhs, i512, location))?; + let lhs = entry.extui(lhs, i512, location)?; + let rhs = entry.extui(rhs, i512, location)?; // Calculate inverse of rhs, callling the inverse implementation's starting block entry.append_operation(cf::br(start_block, &[rhs], location)); // Fetch the inverse result from the result block - let inverse = inverse_result_block.argument(0)?.into(); + let inverse = inverse_result_block.arg(0)?; // Peform lhs * (1/ rhs) - let result = - inverse_result_block.append_op_result(arith::muli(lhs, inverse, location))?; + let result = inverse_result_block.muli(lhs, inverse, location)?; // Apply modulo and convert result to felt252 let result_mod = inverse_result_block.append_op_result(arith::remui(result, prime, location))?; - let is_out_of_range = inverse_result_block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Uge, - result, - prime, - location, - ))?; + let is_out_of_range = + inverse_result_block.cmpi(context, CmpiPredicate::Uge, result, prime, location)?; let result = inverse_result_block.append_op_result(arith::select( is_out_of_range, @@ -302,8 +270,7 @@ pub fn build_binary_operation<'ctx, 'this>( result, location, ))?; - let result = inverse_result_block - .append_op_result(arith::trunci(result, felt252_ty, location))?; + let result = inverse_result_block.trunci(result, felt252_ty, location)?; inverse_result_block.append_operation(helper.br(0, &[result], location)); return Ok(()); @@ -355,11 +322,10 @@ pub fn build_is_zero<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); + let arg0: Value = entry.arg(0)?; let k0 = entry.const_int_from_type(context, location, 0, arg0.r#type())?; - let condition = - entry.append_op_result(arith::cmpi(context, CmpiPredicate::Eq, arg0, k0, location))?; + let condition = entry.cmpi(context, CmpiPredicate::Eq, arg0, k0, location)?; entry.append_operation(helper.cond_br(context, condition, [0, 1], [&[], &[arg0]], location)); Ok(()) diff --git a/src/libfuncs/felt252_dict.rs b/src/libfuncs/felt252_dict.rs index d1d539e41..4b3f7a67d 100644 --- a/src/libfuncs/felt252_dict.rs +++ b/src/libfuncs/felt252_dict.rs @@ -15,7 +15,6 @@ use cairo_lang_sierra::{ program_registry::ProgramRegistry, }; use melior::{ - dialect::arith, ir::{Block, Location}, Context, }; @@ -49,8 +48,7 @@ pub fn build_new<'ctx, 'this>( metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let segment_arena = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let segment_arena = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let runtime_bindings = metadata .get_mut::() @@ -71,12 +69,10 @@ pub fn build_squash<'ctx, 'this>( metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; - let gas_builtin = entry.argument(1)?.into(); - let segment_arena = - super::increment_builtin_counter(context, entry, location, entry.argument(2)?.into())?; - let dict_ptr = entry.argument(3)?.into(); + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; + let gas_builtin = entry.arg(1)?; + let segment_arena = super::increment_builtin_counter(context, entry, location, entry.arg(2)?)?; + let dict_ptr = entry.arg(3)?; let runtime_bindings = metadata .get_mut::() @@ -87,16 +83,11 @@ pub fn build_squash<'ctx, 'this>( .result(0)? .into(); - let new_gas_builtin = entry.append_op_result(arith::addi(gas_builtin, gas_refund, location))?; + let new_gas_builtin = entry.addi(gas_builtin, gas_refund, location)?; entry.append_operation(helper.br( 0, - &[ - range_check, - new_gas_builtin, - segment_arena, - entry.argument(3)?.into(), - ], + &[range_check, new_gas_builtin, segment_arena, entry.arg(3)?], location, )); diff --git a/src/libfuncs/felt252_dict_entry.rs b/src/libfuncs/felt252_dict_entry.rs index 6b9e4171b..50ac436e8 100644 --- a/src/libfuncs/felt252_dict_entry.rs +++ b/src/libfuncs/felt252_dict_entry.rs @@ -71,8 +71,8 @@ pub fn build_get<'ctx, 'this>( )?; let value_ty = registry.build_type(context, helper, registry, metadata, &info.ty)?; - let dict_ptr = entry.argument(0)?.into(); - let entry_key = entry.argument(1)?.into(); + let dict_ptr = entry.arg(0)?; + let entry_key = entry.arg(1)?; let entry_key_ptr = helper @@ -157,7 +157,7 @@ pub fn build_get<'ctx, 'this>( let entry = block_final.insert_values(context, location, entry, &[dict_ptr, entry_value_ptr_ptr])?; - block_final.append_operation(helper.br(0, &[entry, block_final.argument(0)?.into()], location)); + block_final.append_operation(helper.br(0, &[entry, block_final.arg(0)?], location)); Ok(()) } @@ -182,8 +182,8 @@ pub fn build_finalize<'ctx, 'this>( &info.signature.param_signatures[1].ty, )?; - let dict_entry = entry.argument(0)?.into(); - let entry_value = entry.argument(1)?.into(); + let dict_entry = entry.arg(0)?; + let entry_value = entry.arg(1)?; let dict_ptr = entry.extract_value( context, @@ -265,12 +265,7 @@ pub fn build_finalize<'ctx, 'this>( block_vacant.append_operation(cf::br(block_final, &[value_ptr], location)); } - block_final.store( - context, - location, - block_final.argument(0)?.into(), - entry_value, - )?; + block_final.store(context, location, block_final.arg(0)?, entry_value)?; block_final.append_operation(helper.br(0, &[dict_ptr], location)); Ok(()) diff --git a/src/libfuncs/gas.rs b/src/libfuncs/gas.rs index 687f9b92e..db8a924f8 100644 --- a/src/libfuncs/gas.rs +++ b/src/libfuncs/gas.rs @@ -16,10 +16,7 @@ use cairo_lang_sierra::{ program_registry::ProgramRegistry, }; use melior::{ - dialect::{ - arith::{self, CmpiPredicate}, - ods, - }, + dialect::{arith::CmpiPredicate, ods}, ir::{r#type::IntegerType, Block, Location}, Context, }; @@ -61,14 +58,10 @@ pub fn build_get_available_gas<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let gas = entry.argument(0)?.into(); - let gas_u128 = entry.append_op_result(arith::extui( - gas, - IntegerType::new(context, 128).into(), - location, - ))?; + let gas = entry.arg(0)?; + let gas_u128 = entry.extui(gas, IntegerType::new(context, 128).into(), location)?; // The gas is returned as u128 on the second arg. - entry.append_operation(helper.br(0, &[entry.argument(0)?.into(), gas_u128], location)); + entry.append_operation(helper.br(0, &[entry.arg(0)?, gas_u128], location)); Ok(()) } @@ -82,9 +75,8 @@ pub fn build_withdraw_gas<'ctx, 'this>( metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; - let current_gas = entry.argument(1)?.into(); + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; + let current_gas = entry.arg(1)?; let gas_cost = metadata .get::() @@ -134,19 +126,17 @@ pub fn build_withdraw_gas<'ctx, 'this>( u64_type, )?; let cost_value = entry.load(context, location, builtin_cost_value_ptr, u64_type)?; - let gas_cost_value = - entry.append_op_result(arith::muli(cost_count_value, cost_value, location))?; - total_gas_cost_value = - entry.append_op_result(arith::addi(total_gas_cost_value, gas_cost_value, location))?; + let gas_cost_value = entry.muli(cost_count_value, cost_value, location)?; + total_gas_cost_value = entry.addi(total_gas_cost_value, gas_cost_value, location)?; } - let is_enough = entry.append_op_result(arith::cmpi( + let is_enough = entry.cmpi( context, CmpiPredicate::Uge, current_gas, total_gas_cost_value, location, - ))?; + )?; let resulting_gas = entry.append_op_result( ods::llvm::intr_usub_sat(context, current_gas, total_gas_cost_value, location).into(), @@ -173,10 +163,9 @@ pub fn build_builtin_withdraw_gas<'ctx, 'this>( metadata: &MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; - let current_gas = entry.argument(1)?.into(); - let builtin_ptr = entry.argument(2)?.into(); + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; + let current_gas = entry.arg(1)?; + let builtin_ptr = entry.arg(2)?; let gas_cost = metadata .get::() @@ -215,19 +204,17 @@ pub fn build_builtin_withdraw_gas<'ctx, 'this>( u64_type, )?; let cost_value = entry.load(context, location, builtin_cost_value_ptr, u64_type)?; - let gas_cost_value = - entry.append_op_result(arith::muli(cost_count_value, cost_value, location))?; - total_gas_cost_value = - entry.append_op_result(arith::addi(total_gas_cost_value, gas_cost_value, location))?; + let gas_cost_value = entry.muli(cost_count_value, cost_value, location)?; + total_gas_cost_value = entry.addi(total_gas_cost_value, gas_cost_value, location)?; } - let is_enough = entry.append_op_result(arith::cmpi( + let is_enough = entry.cmpi( context, CmpiPredicate::Uge, current_gas, total_gas_cost_value, location, - ))?; + )?; let resulting_gas = entry.append_op_result( ods::llvm::intr_usub_sat(context, current_gas, total_gas_cost_value, location).into(), diff --git a/src/libfuncs/int_range.rs b/src/libfuncs/int_range.rs index 38e337bd7..f231da6da 100644 --- a/src/libfuncs/int_range.rs +++ b/src/libfuncs/int_range.rs @@ -56,9 +56,9 @@ pub fn build_int_range_try_new<'ctx, 'this>( metadata: &mut MetadataStorage, info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = entry.argument(0)?.into(); - let x = entry.argument(1)?.into(); - let y = entry.argument(2)?.into(); + let range_check = entry.arg(0)?; + let x = entry.arg(1)?; + let y = entry.arg(2)?; let range_ty = registry.build_type( context, helper, @@ -71,9 +71,9 @@ pub fn build_int_range_try_new<'ctx, 'this>( let inner_range = inner.integer_range(registry)?; let is_valid = if inner_range.lower < BigInt::ZERO { - entry.append_op_result(arith::cmpi(context, CmpiPredicate::Sle, x, y, location))? + entry.cmpi(context, CmpiPredicate::Sle, x, y, location)? } else { - entry.append_op_result(arith::cmpi(context, CmpiPredicate::Ule, x, y, location))? + entry.cmpi(context, CmpiPredicate::Ule, x, y, location)? }; let range = @@ -103,7 +103,7 @@ pub fn build_int_range_pop_front<'ctx, 'this>( metadata: &mut MetadataStorage, info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range = entry.argument(0)?.into(); + let range = entry.arg(0)?; let inner_ty = registry.build_type( context, @@ -117,16 +117,16 @@ pub fn build_int_range_pop_front<'ctx, 'this>( let x = entry.extract_value(context, location, range, inner_ty, 0)?; let k1 = entry.const_int_from_type(context, location, 1, inner_ty)?; - let x_p_1 = entry.append_op_result(arith::addi(x, k1, location))?; + let x_p_1 = entry.addi(x, k1, location)?; let y = entry.extract_value(context, location, range, inner_ty, 1)?; // to know if it is signed let inner_range = inner.integer_range(registry)?; let is_valid = if inner_range.lower < BigInt::ZERO { - entry.append_op_result(arith::cmpi(context, CmpiPredicate::Slt, x, y, location))? + entry.cmpi(context, CmpiPredicate::Slt, x, y, location)? } else { - entry.append_op_result(arith::cmpi(context, CmpiPredicate::Ult, x, y, location))? + entry.cmpi(context, CmpiPredicate::Ult, x, y, location)? }; let range = entry.insert_value(context, location, range, x_p_1, 0)?; diff --git a/src/libfuncs/mem.rs b/src/libfuncs/mem.rs index 59f6b97dd..7f1a39485 100644 --- a/src/libfuncs/mem.rs +++ b/src/libfuncs/mem.rs @@ -99,6 +99,6 @@ pub fn build_store_local<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureAndTypeConcreteLibfunc, ) -> Result<()> { - entry.append_operation(helper.br(0, &[entry.argument(1)?.into()], location)); + entry.append_operation(helper.br(0, &[entry.arg(1)?], location)); Ok(()) } diff --git a/src/libfuncs/nullable.rs b/src/libfuncs/nullable.rs index c7eed1b20..79645e6cf 100644 --- a/src/libfuncs/nullable.rs +++ b/src/libfuncs/nullable.rs @@ -80,7 +80,7 @@ fn build_match_nullable<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureAndTypeConcreteLibfunc, ) -> Result<()> { - let arg = entry.argument(0)?.into(); + let arg = entry.arg(0)?; let nullptr = entry .append_op_result(ods::llvm::mlir_zero(context, pointer(context, 0), location).into())?; diff --git a/src/libfuncs/pedersen.rs b/src/libfuncs/pedersen.rs index 5d5e99536..d9ee3d3f8 100644 --- a/src/libfuncs/pedersen.rs +++ b/src/libfuncs/pedersen.rs @@ -17,7 +17,6 @@ use cairo_lang_sierra::{ program_registry::ProgramRegistry, }; use melior::{ - dialect::arith, ir::{r#type::IntegerType, Block, Location}, Context, }; @@ -53,7 +52,7 @@ pub fn build_pedersen<'ctx>( .expect("Runtime library not available."); let pedersen_builtin = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let felt252_ty = registry.build_type( context, @@ -66,8 +65,8 @@ pub fn build_pedersen<'ctx>( let i256_ty = IntegerType::new(context, 256).into(); let layout_i256 = get_integer_layout(256); - let lhs = entry.argument(1)?.into(); - let rhs = entry.argument(2)?.into(); + let lhs = entry.arg(1)?; + let rhs = entry.arg(2)?; // We must extend to i256 because bswap must be an even number of bytes. @@ -81,8 +80,8 @@ pub fn build_pedersen<'ctx>( .init_block() .alloca1(context, location, i256_ty, layout_i256.align())?; - let lhs_i256 = entry.append_op_result(arith::extui(lhs, i256_ty, location))?; - let rhs_i256 = entry.append_op_result(arith::extui(rhs, i256_ty, location))?; + let lhs_i256 = entry.extui(lhs, i256_ty, location)?; + let rhs_i256 = entry.extui(rhs, i256_ty, location)?; entry.store(context, location, lhs_ptr, lhs_i256)?; entry.store(context, location, rhs_ptr, rhs_i256)?; @@ -95,7 +94,7 @@ pub fn build_pedersen<'ctx>( .libfunc_pedersen(context, helper, entry, dst_ptr, lhs_ptr, rhs_ptr, location)?; let result = entry.load(context, location, dst_ptr, i256_ty)?; - let result = entry.append_op_result(arith::trunci(result, felt252_ty, location))?; + let result = entry.trunci(result, felt252_ty, location)?; entry.append_operation(helper.br(0, &[pedersen_builtin, result], location)); Ok(()) diff --git a/src/libfuncs/poseidon.rs b/src/libfuncs/poseidon.rs index bf06587f6..3ad52694f 100644 --- a/src/libfuncs/poseidon.rs +++ b/src/libfuncs/poseidon.rs @@ -17,7 +17,7 @@ use cairo_lang_sierra::{ program_registry::ProgramRegistry, }; use melior::{ - dialect::{arith, ods}, + dialect::ods, ir::{r#type::IntegerType, Block, Location}, Context, }; @@ -53,7 +53,7 @@ pub fn build_hades_permutation<'ctx>( .expect("Runtime library not available."); let poseidon_builtin = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let felt252_ty = registry.build_type( context, @@ -66,9 +66,9 @@ pub fn build_hades_permutation<'ctx>( let i256_ty = IntegerType::new(context, 256).into(); let layout_i256 = get_integer_layout(256); - let op0 = entry.argument(1)?.into(); - let op1 = entry.argument(2)?.into(); - let op2 = entry.argument(3)?.into(); + let op0 = entry.arg(1)?; + let op1 = entry.arg(2)?; + let op2 = entry.arg(3)?; // We must extend to i256 because bswap must be an even number of bytes. @@ -105,9 +105,9 @@ pub fn build_hades_permutation<'ctx>( let op1_i256 = entry.load(context, location, op1_ptr, i256_ty)?; let op2_i256 = entry.load(context, location, op2_ptr, i256_ty)?; - let op0 = entry.append_op_result(arith::trunci(op0_i256, felt252_ty, location))?; - let op1 = entry.append_op_result(arith::trunci(op1_i256, felt252_ty, location))?; - let op2 = entry.append_op_result(arith::trunci(op2_i256, felt252_ty, location))?; + let op0 = entry.trunci(op0_i256, felt252_ty, location)?; + let op1 = entry.trunci(op1_i256, felt252_ty, location)?; + let op2 = entry.trunci(op2_i256, felt252_ty, location)?; entry.append_operation(helper.br(0, &[poseidon_builtin, op0, op1, op2], location)); diff --git a/src/libfuncs/sint128.rs b/src/libfuncs/sint128.rs index 4ce06cbf8..0242913ed 100644 --- a/src/libfuncs/sint128.rs +++ b/src/libfuncs/sint128.rs @@ -97,10 +97,10 @@ pub fn build_operation<'ctx, 'this>( info: &IntOperationConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let op_name = match info.operator { IntOperator::OverflowingAdd => "llvm.intr.sadd.with.overflow", @@ -127,13 +127,7 @@ pub fn build_operation<'ctx, 'this>( // Create a const operation to get the 0 value to compare against let zero_const = entry.const_int_from_type(context, location, 0, values_type)?; // Check if the result is positive - let is_positive = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - op_result, - zero_const, - location, - ))?; + let is_positive = entry.cmpi(context, CmpiPredicate::Sge, op_result, zero_const, location)?; // Check overflow flag let op_overflow = entry.extract_value( @@ -180,8 +174,8 @@ pub fn build_equal<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); - let arg1: Value = entry.argument(1)?.into(); + let arg0: Value = entry.arg(0)?; + let arg1: Value = entry.arg(1)?; let op0 = entry.append_operation(arith::cmpi( context, @@ -211,17 +205,11 @@ pub fn build_is_zero<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); + let arg0: Value = entry.arg(0)?; let const_0 = entry.const_int_from_type(context, location, 0, arg0.r#type())?; - let condition = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - arg0, - const_0, - location, - ))?; + let condition = entry.cmpi(context, CmpiPredicate::Eq, arg0, const_0, location)?; entry.append_operation(helper.cond_br(context, condition, [0, 1], [&[], &[arg0]], location)); @@ -247,20 +235,20 @@ pub fn build_to_felt252<'ctx, 'this>( )?; let prime = entry.const_int_from_type(context, location, PRIME.clone(), felt252_ty)?; - let value: Value = entry.argument(0)?.into(); + let value: Value = entry.arg(0)?; let value_type = value.r#type(); - let is_negative = entry.append_op_result(arith::cmpi( + let is_negative = entry.cmpi( context, arith::CmpiPredicate::Slt, value, entry.const_int_from_type(context, location, 0, value_type)?, location, - ))?; + )?; let value_abs = entry.append_op_result(math::absi(context, value, location).into())?; - let result = entry.append_op_result(arith::extui(value_abs, felt252_ty, location))?; + let result = entry.extui(value_abs, felt252_ty, location)?; let prime_minus_result = entry.append_op_result(arith::subi(prime, result, location))?; @@ -287,9 +275,9 @@ pub fn build_from_felt252<'ctx, 'this>( info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let value: Value = entry.argument(1)?.into(); + let value: Value = entry.arg(1)?; let felt252_ty = registry.build_type( context, @@ -317,13 +305,7 @@ pub fn build_from_felt252<'ctx, 'this>( let half_prime = block.const_int_from_type(context, location, HALF_PRIME.clone(), felt252_ty)?; - let is_felt_neg = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ugt, - value, - half_prime, - location, - ))?; + let is_felt_neg = block.cmpi(context, CmpiPredicate::Ugt, value, half_prime, location)?; let is_neg_block = helper.append_block(Block::new(&[])); let is_not_neg_block = helper.append_block(Block::new(&[])); @@ -346,8 +328,7 @@ pub fn build_from_felt252<'ctx, 'this>( is_neg_block.append_op_result(arith::subi(prime, value, location))?; let kneg1 = is_neg_block.const_int_from_type(context, location, -1, felt252_ty)?; - src_value_is_neg = - is_neg_block.append_op_result(arith::muli(src_value_is_neg, kneg1, location))?; + src_value_is_neg = is_neg_block.muli(src_value_is_neg, kneg1, location)?; is_neg_block.append_operation(cf::br(final_block, &[src_value_is_neg], location)); } @@ -355,24 +336,12 @@ pub fn build_from_felt252<'ctx, 'this>( is_not_neg_block.append_operation(cf::br(final_block, &[value], location)); block = final_block; - block.argument(0)?.into() + block.arg(0)? }; - let is_smaller_eq = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sle, - value, - const_max, - location, - ))?; + let is_smaller_eq = block.cmpi(context, CmpiPredicate::Sle, value, const_max, location)?; - let is_bigger_eq = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - value, - const_min, - location, - ))?; + let is_bigger_eq = block.cmpi(context, CmpiPredicate::Sge, value, const_min, location)?; let is_ok = block.append_op_result(arith::andi(is_smaller_eq, is_bigger_eq, location))?; @@ -389,7 +358,7 @@ pub fn build_from_felt252<'ctx, 'this>( location, )); - let value = block_success.append_op_result(arith::trunci(value, result_ty, location))?; + let value = block_success.trunci(value, result_ty, location)?; block_success.append_operation(helper.br(0, &[range_check, value], location)); block_failure.append_operation(helper.br(1, &[range_check], location)); @@ -407,14 +376,13 @@ pub fn build_diff<'ctx, 'this>( _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; // Check if lhs >= rhs - let is_ge = - entry.append_op_result(arith::cmpi(context, CmpiPredicate::Sge, lhs, rhs, location))?; + let is_ge = entry.cmpi(context, CmpiPredicate::Sge, lhs, rhs, location)?; let result = entry.append_op_result(arith::subi(lhs, rhs, location))?; diff --git a/src/libfuncs/sint16.rs b/src/libfuncs/sint16.rs index 5211e0246..c3b1e312d 100644 --- a/src/libfuncs/sint16.rs +++ b/src/libfuncs/sint16.rs @@ -98,10 +98,10 @@ pub fn build_operation<'ctx, 'this>( info: &IntOperationConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let op_name = match info.operator { IntOperator::OverflowingAdd => "llvm.intr.sadd.with.overflow", @@ -128,13 +128,7 @@ pub fn build_operation<'ctx, 'this>( // Create a const operation to get the 0 value to compare against let zero_const = entry.const_int_from_type(context, location, 0, values_type)?; // Check if the result is positive - let is_positive = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - op_result, - zero_const, - location, - ))?; + let is_positive = entry.cmpi(context, CmpiPredicate::Sge, op_result, zero_const, location)?; // Check overflow flag let op_overflow = entry.extract_value( @@ -181,8 +175,8 @@ pub fn build_equal<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); - let arg1: Value = entry.argument(1)?.into(); + let arg0: Value = entry.arg(0)?; + let arg1: Value = entry.arg(1)?; let op0 = entry.append_operation(arith::cmpi( context, @@ -212,17 +206,11 @@ pub fn build_is_zero<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); + let arg0: Value = entry.arg(0)?; let const_0 = entry.const_int_from_type(context, location, 0, arg0.r#type())?; - let condition = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - arg0, - const_0, - location, - ))?; + let condition = entry.cmpi(context, CmpiPredicate::Eq, arg0, const_0, location)?; entry.append_operation(helper.cond_br(context, condition, [0, 1], [&[], &[arg0]], location)); @@ -246,13 +234,13 @@ pub fn build_widemul<'ctx, 'this>( metadata, &info.output_types()[0][0], )?; - let lhs: Value = entry.argument(0)?.into(); - let rhs: Value = entry.argument(1)?.into(); + let lhs: Value = entry.arg(0)?; + let rhs: Value = entry.arg(1)?; - let lhs = entry.append_op_result(arith::extsi(lhs, target_type, location))?; - let rhs = entry.append_op_result(arith::extsi(rhs, target_type, location))?; + let lhs = entry.extsi(lhs, target_type, location)?; + let rhs = entry.extsi(rhs, target_type, location)?; - let result = entry.append_op_result(arith::muli(lhs, rhs, location))?; + let result = entry.muli(lhs, rhs, location)?; entry.append_operation(helper.br(0, &[result], location)); Ok(()) @@ -277,20 +265,20 @@ pub fn build_to_felt252<'ctx, 'this>( )?; let prime = entry.const_int_from_type(context, location, PRIME.clone(), felt252_ty)?; - let value: Value = entry.argument(0)?.into(); + let value: Value = entry.arg(0)?; let value_type = value.r#type(); - let is_negative = entry.append_op_result(arith::cmpi( + let is_negative = entry.cmpi( context, arith::CmpiPredicate::Slt, value, entry.const_int_from_type(context, location, 0, value_type)?, location, - ))?; + )?; let value_abs = entry.append_op_result(math::absi(context, value, location).into())?; - let result = entry.append_op_result(arith::extui(value_abs, felt252_ty, location))?; + let result = entry.extui(value_abs, felt252_ty, location)?; let prime_minus_result = entry.append_op_result(arith::subi(prime, result, location))?; @@ -317,9 +305,9 @@ pub fn build_from_felt252<'ctx, 'this>( info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let value: Value = entry.argument(1)?.into(); + let value: Value = entry.arg(1)?; let felt252_ty = registry.build_type( context, @@ -347,13 +335,7 @@ pub fn build_from_felt252<'ctx, 'this>( let half_prime = block.const_int_from_type(context, location, HALF_PRIME.clone(), felt252_ty)?; - let is_felt_neg = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ugt, - value, - half_prime, - location, - ))?; + let is_felt_neg = block.cmpi(context, CmpiPredicate::Ugt, value, half_prime, location)?; let is_neg_block = helper.append_block(Block::new(&[])); let is_not_neg_block = helper.append_block(Block::new(&[])); @@ -376,8 +358,7 @@ pub fn build_from_felt252<'ctx, 'this>( is_neg_block.append_op_result(arith::subi(prime, value, location))?; let kneg1 = is_neg_block.const_int_from_type(context, location, -1, felt252_ty)?; - src_value_is_neg = - is_neg_block.append_op_result(arith::muli(src_value_is_neg, kneg1, location))?; + src_value_is_neg = is_neg_block.muli(src_value_is_neg, kneg1, location)?; is_neg_block.append_operation(cf::br(final_block, &[src_value_is_neg], location)); } @@ -385,24 +366,12 @@ pub fn build_from_felt252<'ctx, 'this>( is_not_neg_block.append_operation(cf::br(final_block, &[value], location)); block = final_block; - block.argument(0)?.into() + block.arg(0)? }; - let is_smaller_eq = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sle, - value, - const_max, - location, - ))?; + let is_smaller_eq = block.cmpi(context, CmpiPredicate::Sle, value, const_max, location)?; - let is_bigger_eq = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - value, - const_min, - location, - ))?; + let is_bigger_eq = block.cmpi(context, CmpiPredicate::Sge, value, const_min, location)?; let is_ok = block.append_op_result(arith::andi(is_smaller_eq, is_bigger_eq, location))?; @@ -419,7 +388,7 @@ pub fn build_from_felt252<'ctx, 'this>( location, )); - let value = block_success.append_op_result(arith::trunci(value, result_ty, location))?; + let value = block_success.trunci(value, result_ty, location)?; block_success.append_operation(helper.br(0, &[range_check, value], location)); block_failure.append_operation(helper.br(1, &[range_check], location)); @@ -437,14 +406,13 @@ pub fn build_diff<'ctx, 'this>( _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; // Check if lhs >= rhs - let is_ge = - entry.append_op_result(arith::cmpi(context, CmpiPredicate::Sge, lhs, rhs, location))?; + let is_ge = entry.cmpi(context, CmpiPredicate::Sge, lhs, rhs, location)?; let result = entry.append_op_result(arith::subi(lhs, rhs, location))?; diff --git a/src/libfuncs/sint32.rs b/src/libfuncs/sint32.rs index 3e8bd9139..37d5251e6 100644 --- a/src/libfuncs/sint32.rs +++ b/src/libfuncs/sint32.rs @@ -98,10 +98,10 @@ pub fn build_operation<'ctx, 'this>( info: &IntOperationConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let op_name = match info.operator { IntOperator::OverflowingAdd => "llvm.intr.sadd.with.overflow", @@ -128,13 +128,7 @@ pub fn build_operation<'ctx, 'this>( // Create a const operation to get the 0 value to compare against let zero_const = entry.const_int_from_type(context, location, 0, values_type)?; // Check if the result is positive - let is_positive = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - op_result, - zero_const, - location, - ))?; + let is_positive = entry.cmpi(context, CmpiPredicate::Sge, op_result, zero_const, location)?; // Check overflow flag let op_overflow = entry.extract_value( @@ -180,8 +174,8 @@ pub fn build_equal<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); - let arg1: Value = entry.argument(1)?.into(); + let arg0: Value = entry.arg(0)?; + let arg1: Value = entry.arg(1)?; let op0 = entry.append_operation(arith::cmpi( context, @@ -211,17 +205,11 @@ pub fn build_is_zero<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); + let arg0: Value = entry.arg(0)?; let const_0 = entry.const_int_from_type(context, location, 0, arg0.r#type())?; - let condition = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - arg0, - const_0, - location, - ))?; + let condition = entry.cmpi(context, CmpiPredicate::Eq, arg0, const_0, location)?; entry.append_operation(helper.cond_br(context, condition, [0, 1], [&[], &[arg0]], location)); @@ -245,13 +233,13 @@ pub fn build_widemul<'ctx, 'this>( metadata, &info.output_types()[0][0], )?; - let lhs: Value = entry.argument(0)?.into(); - let rhs: Value = entry.argument(1)?.into(); + let lhs: Value = entry.arg(0)?; + let rhs: Value = entry.arg(1)?; - let lhs = entry.append_op_result(arith::extsi(lhs, target_type, location))?; - let rhs = entry.append_op_result(arith::extsi(rhs, target_type, location))?; + let lhs = entry.extsi(lhs, target_type, location)?; + let rhs = entry.extsi(rhs, target_type, location)?; - let result = entry.append_op_result(arith::muli(lhs, rhs, location))?; + let result = entry.muli(lhs, rhs, location)?; entry.append_operation(helper.br(0, &[result], location)); Ok(()) @@ -276,20 +264,20 @@ pub fn build_to_felt252<'ctx, 'this>( )?; let prime = entry.const_int_from_type(context, location, PRIME.clone(), felt252_ty)?; - let value: Value = entry.argument(0)?.into(); + let value: Value = entry.arg(0)?; let value_type = value.r#type(); - let is_negative = entry.append_op_result(arith::cmpi( + let is_negative = entry.cmpi( context, arith::CmpiPredicate::Slt, value, entry.const_int_from_type(context, location, 0, value_type)?, location, - ))?; + )?; let value_abs = entry.append_op_result(math::absi(context, value, location).into())?; - let result = entry.append_op_result(arith::extui(value_abs, felt252_ty, location))?; + let result = entry.extui(value_abs, felt252_ty, location)?; let prime_minus_result = entry.append_op_result(arith::subi(prime, result, location))?; @@ -316,9 +304,9 @@ pub fn build_from_felt252<'ctx, 'this>( info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let value: Value = entry.argument(1)?.into(); + let value: Value = entry.arg(1)?; let felt252_ty = registry.build_type( context, @@ -346,13 +334,7 @@ pub fn build_from_felt252<'ctx, 'this>( let half_prime = block.const_int_from_type(context, location, HALF_PRIME.clone(), felt252_ty)?; - let is_felt_neg = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ugt, - value, - half_prime, - location, - ))?; + let is_felt_neg = block.cmpi(context, CmpiPredicate::Ugt, value, half_prime, location)?; let is_neg_block = helper.append_block(Block::new(&[])); let is_not_neg_block = helper.append_block(Block::new(&[])); @@ -375,8 +357,7 @@ pub fn build_from_felt252<'ctx, 'this>( is_neg_block.append_op_result(arith::subi(prime, value, location))?; let kneg1 = is_neg_block.const_int_from_type(context, location, -1, felt252_ty)?; - src_value_is_neg = - is_neg_block.append_op_result(arith::muli(src_value_is_neg, kneg1, location))?; + src_value_is_neg = is_neg_block.muli(src_value_is_neg, kneg1, location)?; is_neg_block.append_operation(cf::br(final_block, &[src_value_is_neg], location)); } @@ -384,24 +365,12 @@ pub fn build_from_felt252<'ctx, 'this>( is_not_neg_block.append_operation(cf::br(final_block, &[value], location)); block = final_block; - block.argument(0)?.into() + block.arg(0)? }; - let is_smaller_eq = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sle, - value, - const_max, - location, - ))?; + let is_smaller_eq = block.cmpi(context, CmpiPredicate::Sle, value, const_max, location)?; - let is_bigger_eq = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - value, - const_min, - location, - ))?; + let is_bigger_eq = block.cmpi(context, CmpiPredicate::Sge, value, const_min, location)?; let is_ok = block.append_op_result(arith::andi(is_smaller_eq, is_bigger_eq, location))?; @@ -418,7 +387,7 @@ pub fn build_from_felt252<'ctx, 'this>( location, )); - let value = block_success.append_op_result(arith::trunci(value, result_ty, location))?; + let value = block_success.trunci(value, result_ty, location)?; block_success.append_operation(helper.br(0, &[range_check, value], location)); block_failure.append_operation(helper.br(1, &[range_check], location)); @@ -436,14 +405,13 @@ pub fn build_diff<'ctx, 'this>( _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; // Check if lhs >= rhs - let is_ge = - entry.append_op_result(arith::cmpi(context, CmpiPredicate::Sge, lhs, rhs, location))?; + let is_ge = entry.cmpi(context, CmpiPredicate::Sge, lhs, rhs, location)?; let result = entry.append_op_result(arith::subi(lhs, rhs, location))?; diff --git a/src/libfuncs/sint64.rs b/src/libfuncs/sint64.rs index a7ec6bc6c..e13366fe9 100644 --- a/src/libfuncs/sint64.rs +++ b/src/libfuncs/sint64.rs @@ -98,10 +98,10 @@ pub fn build_operation<'ctx, 'this>( info: &IntOperationConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let op_name = match info.operator { IntOperator::OverflowingAdd => "llvm.intr.sadd.with.overflow", @@ -128,13 +128,7 @@ pub fn build_operation<'ctx, 'this>( // Create a const operation to get the 0 value to compare against let zero_const = entry.const_int_from_type(context, location, 0, values_type)?; // Check if the result is positive - let is_positive = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - op_result, - zero_const, - location, - ))?; + let is_positive = entry.cmpi(context, CmpiPredicate::Sge, op_result, zero_const, location)?; // Check overflow flag let op_overflow = entry.extract_value( @@ -181,8 +175,8 @@ pub fn build_equal<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); - let arg1: Value = entry.argument(1)?.into(); + let arg0: Value = entry.arg(0)?; + let arg1: Value = entry.arg(1)?; let op0 = entry.append_operation(arith::cmpi( context, @@ -212,17 +206,11 @@ pub fn build_is_zero<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); + let arg0: Value = entry.arg(0)?; let const_0 = entry.const_int_from_type(context, location, 0, arg0.r#type())?; - let condition = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - arg0, - const_0, - location, - ))?; + let condition = entry.cmpi(context, CmpiPredicate::Eq, arg0, const_0, location)?; entry.append_operation(helper.cond_br(context, condition, [0, 1], [&[], &[arg0]], location)); @@ -246,13 +234,13 @@ pub fn build_widemul<'ctx, 'this>( metadata, &info.output_types()[0][0], )?; - let lhs: Value = entry.argument(0)?.into(); - let rhs: Value = entry.argument(1)?.into(); + let lhs: Value = entry.arg(0)?; + let rhs: Value = entry.arg(1)?; - let lhs = entry.append_op_result(arith::extsi(lhs, target_type, location))?; - let rhs = entry.append_op_result(arith::extsi(rhs, target_type, location))?; + let lhs = entry.extsi(lhs, target_type, location)?; + let rhs = entry.extsi(rhs, target_type, location)?; - let result = entry.append_op_result(arith::muli(lhs, rhs, location))?; + let result = entry.muli(lhs, rhs, location)?; entry.append_operation(helper.br(0, &[result], location)); Ok(()) @@ -277,20 +265,20 @@ pub fn build_to_felt252<'ctx, 'this>( )?; let prime = entry.const_int_from_type(context, location, PRIME.clone(), felt252_ty)?; - let value: Value = entry.argument(0)?.into(); + let value: Value = entry.arg(0)?; let value_type = value.r#type(); - let is_negative = entry.append_op_result(arith::cmpi( + let is_negative = entry.cmpi( context, arith::CmpiPredicate::Slt, value, entry.const_int_from_type(context, location, 0, value_type)?, location, - ))?; + )?; let value_abs = entry.append_op_result(math::absi(context, value, location).into())?; - let result = entry.append_op_result(arith::extui(value_abs, felt252_ty, location))?; + let result = entry.extui(value_abs, felt252_ty, location)?; let prime_minus_result = entry.append_op_result(arith::subi(prime, result, location))?; @@ -317,9 +305,9 @@ pub fn build_from_felt252<'ctx, 'this>( info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let value: Value = entry.argument(1)?.into(); + let value: Value = entry.arg(1)?; let felt252_ty = registry.build_type( context, @@ -347,13 +335,7 @@ pub fn build_from_felt252<'ctx, 'this>( let half_prime = block.const_int_from_type(context, location, HALF_PRIME.clone(), felt252_ty)?; - let is_felt_neg = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ugt, - value, - half_prime, - location, - ))?; + let is_felt_neg = block.cmpi(context, CmpiPredicate::Ugt, value, half_prime, location)?; let is_neg_block = helper.append_block(Block::new(&[])); let is_not_neg_block = helper.append_block(Block::new(&[])); @@ -376,8 +358,7 @@ pub fn build_from_felt252<'ctx, 'this>( is_neg_block.append_op_result(arith::subi(prime, value, location))?; let kneg1 = is_neg_block.const_int_from_type(context, location, -1, felt252_ty)?; - src_value_is_neg = - is_neg_block.append_op_result(arith::muli(src_value_is_neg, kneg1, location))?; + src_value_is_neg = is_neg_block.muli(src_value_is_neg, kneg1, location)?; is_neg_block.append_operation(cf::br(final_block, &[src_value_is_neg], location)); } @@ -385,24 +366,12 @@ pub fn build_from_felt252<'ctx, 'this>( is_not_neg_block.append_operation(cf::br(final_block, &[value], location)); block = final_block; - block.argument(0)?.into() + block.arg(0)? }; - let is_smaller_eq = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sle, - value, - const_max, - location, - ))?; + let is_smaller_eq = block.cmpi(context, CmpiPredicate::Sle, value, const_max, location)?; - let is_bigger_eq = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - value, - const_min, - location, - ))?; + let is_bigger_eq = block.cmpi(context, CmpiPredicate::Sge, value, const_min, location)?; let is_ok = block.append_op_result(arith::andi(is_smaller_eq, is_bigger_eq, location))?; @@ -419,7 +388,7 @@ pub fn build_from_felt252<'ctx, 'this>( location, )); - let value = block_success.append_op_result(arith::trunci(value, result_ty, location))?; + let value = block_success.trunci(value, result_ty, location)?; block_success.append_operation(helper.br(0, &[range_check, value], location)); block_failure.append_operation(helper.br(1, &[range_check], location)); @@ -437,14 +406,13 @@ pub fn build_diff<'ctx, 'this>( _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; // Check if lhs >= rhs - let is_ge = - entry.append_op_result(arith::cmpi(context, CmpiPredicate::Sge, lhs, rhs, location))?; + let is_ge = entry.cmpi(context, CmpiPredicate::Sge, lhs, rhs, location)?; let result = entry.append_op_result(arith::subi(lhs, rhs, location))?; diff --git a/src/libfuncs/sint8.rs b/src/libfuncs/sint8.rs index 99c3705bc..0c19b6f35 100644 --- a/src/libfuncs/sint8.rs +++ b/src/libfuncs/sint8.rs @@ -98,10 +98,10 @@ pub fn build_operation<'ctx, 'this>( info: &IntOperationConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let op_name = match info.operator { IntOperator::OverflowingAdd => "llvm.intr.sadd.with.overflow", @@ -129,13 +129,7 @@ pub fn build_operation<'ctx, 'this>( let zero_const = entry.const_int_from_type(context, location, 0, values_type)?; // Check if the result is positive - let is_positive = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - op_result, - zero_const, - location, - ))?; + let is_positive = entry.cmpi(context, CmpiPredicate::Sge, op_result, zero_const, location)?; // Check overflow flag let op_overflow = entry.extract_value( @@ -182,8 +176,8 @@ pub fn build_equal<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); - let arg1: Value = entry.argument(1)?.into(); + let arg0: Value = entry.arg(0)?; + let arg1: Value = entry.arg(1)?; let op0 = entry.append_operation(arith::cmpi( context, @@ -213,17 +207,11 @@ pub fn build_is_zero<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); + let arg0: Value = entry.arg(0)?; let const_0 = entry.const_int_from_type(context, location, 0, arg0.r#type())?; - let condition = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - arg0, - const_0, - location, - ))?; + let condition = entry.cmpi(context, CmpiPredicate::Eq, arg0, const_0, location)?; entry.append_operation(helper.cond_br(context, condition, [0, 1], [&[], &[arg0]], location)); @@ -247,13 +235,13 @@ pub fn build_widemul<'ctx, 'this>( metadata, &info.output_types()[0][0], )?; - let lhs: Value = entry.argument(0)?.into(); - let rhs: Value = entry.argument(1)?.into(); + let lhs: Value = entry.arg(0)?; + let rhs: Value = entry.arg(1)?; - let lhs = entry.append_op_result(arith::extsi(lhs, target_type, location))?; - let rhs = entry.append_op_result(arith::extsi(rhs, target_type, location))?; + let lhs = entry.extsi(lhs, target_type, location)?; + let rhs = entry.extsi(rhs, target_type, location)?; - let result = entry.append_op_result(arith::muli(lhs, rhs, location))?; + let result = entry.muli(lhs, rhs, location)?; entry.append_operation(helper.br(0, &[result], location)); Ok(()) @@ -278,20 +266,20 @@ pub fn build_to_felt252<'ctx, 'this>( )?; let prime = entry.const_int_from_type(context, location, PRIME.clone(), felt252_ty)?; - let value: Value = entry.argument(0)?.into(); + let value: Value = entry.arg(0)?; let value_type = value.r#type(); - let is_negative = entry.append_op_result(arith::cmpi( + let is_negative = entry.cmpi( context, arith::CmpiPredicate::Slt, value, entry.const_int_from_type(context, location, 0, value_type)?, location, - ))?; + )?; let value_abs = entry.append_op_result(math::absi(context, value, location).into())?; - let result = entry.append_op_result(arith::extui(value_abs, felt252_ty, location))?; + let result = entry.extui(value_abs, felt252_ty, location)?; let prime_minus_result = entry.append_op_result(arith::subi(prime, result, location))?; @@ -318,9 +306,9 @@ pub fn build_from_felt252<'ctx, 'this>( info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let value: Value = entry.argument(1)?.into(); + let value: Value = entry.arg(1)?; let felt252_ty = registry.build_type( context, @@ -349,13 +337,7 @@ pub fn build_from_felt252<'ctx, 'this>( let half_prime = block.const_int_from_type(context, location, HALF_PRIME.clone(), felt252_ty)?; - let is_felt_neg = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ugt, - value, - half_prime, - location, - ))?; + let is_felt_neg = block.cmpi(context, CmpiPredicate::Ugt, value, half_prime, location)?; let is_neg_block = helper.append_block(Block::new(&[])); let is_not_neg_block = helper.append_block(Block::new(&[])); @@ -378,8 +360,7 @@ pub fn build_from_felt252<'ctx, 'this>( is_neg_block.append_op_result(arith::subi(prime, value, location))?; let kneg1 = is_neg_block.const_int_from_type(context, location, -1, felt252_ty)?; - src_value_is_neg = - is_neg_block.append_op_result(arith::muli(src_value_is_neg, kneg1, location))?; + src_value_is_neg = is_neg_block.muli(src_value_is_neg, kneg1, location)?; is_neg_block.append_operation(cf::br(final_block, &[src_value_is_neg], location)); } @@ -387,24 +368,12 @@ pub fn build_from_felt252<'ctx, 'this>( is_not_neg_block.append_operation(cf::br(final_block, &[value], location)); block = final_block; - block.argument(0)?.into() + block.arg(0)? }; - let is_smaller_eq = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sle, - value, - const_max, - location, - ))?; + let is_smaller_eq = block.cmpi(context, CmpiPredicate::Sle, value, const_max, location)?; - let is_bigger_eq = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - value, - const_min, - location, - ))?; + let is_bigger_eq = block.cmpi(context, CmpiPredicate::Sge, value, const_min, location)?; let is_ok = block.append_op_result(arith::andi(is_smaller_eq, is_bigger_eq, location))?; @@ -421,7 +390,7 @@ pub fn build_from_felt252<'ctx, 'this>( location, )); - let value = block_success.append_op_result(arith::trunci(value, result_ty, location))?; + let value = block_success.trunci(value, result_ty, location)?; block_success.append_operation(helper.br(0, &[range_check, value], location)); block_failure.append_operation(helper.br(1, &[range_check], location)); @@ -439,14 +408,13 @@ pub fn build_diff<'ctx, 'this>( _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; // Check if lhs >= rhs - let is_ge = - entry.append_op_result(arith::cmpi(context, CmpiPredicate::Sge, lhs, rhs, location))?; + let is_ge = entry.cmpi(context, CmpiPredicate::Sge, lhs, rhs, location)?; let result = entry.append_op_result(arith::subi(lhs, rhs, location))?; diff --git a/src/libfuncs/starknet.rs b/src/libfuncs/starknet.rs index 8705848f6..e0239ac45 100644 --- a/src/libfuncs/starknet.rs +++ b/src/libfuncs/starknet.rs @@ -163,7 +163,7 @@ pub fn build_call_contract<'ctx, 'this>( let ptr = entry.load( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), )?; @@ -211,7 +211,7 @@ pub fn build_call_contract<'ctx, 'this>( .alloca1(context, location, gas_ty, gas_layout.align())?; entry.append_operation(llvm::store( context, - entry.argument(0)?.into(), + entry.arg(0)?, gas_builtin_ptr, location, LoadStoreOptions::default(), @@ -221,7 +221,7 @@ pub fn build_call_contract<'ctx, 'this>( let address_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; entry.append_operation(llvm::store( context, - entry.argument(2)?.into(), + entry.arg(2)?, address_arg_ptr, location, LoadStoreOptions::default(), @@ -231,7 +231,7 @@ pub fn build_call_contract<'ctx, 'this>( let entry_point_selector_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; entry.append_operation(llvm::store( context, - entry.argument(3)?.into(), + entry.arg(3)?, entry_point_selector_arg_ptr, location, LoadStoreOptions::default(), @@ -258,18 +258,13 @@ pub fn build_call_contract<'ctx, 'this>( calldata_arg_ty, get_integer_layout(64).align(), )?; - entry.store( - context, - location, - calldata_arg_ptr, - entry.argument(4)?.into(), - )?; + entry.store(context, location, calldata_arg_ptr, entry.arg(4)?)?; // Extract function pointer. let fn_ptr = entry.gep( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, &[GepIndex::Const( StarknetSyscallHandlerCallbacks::<()>::CALL_CONTRACT.try_into()?, )], @@ -352,8 +347,8 @@ pub fn build_call_contract<'ctx, 'this>( result_tag, [1, 0], [ - &[remaining_gas, entry.argument(1)?.into(), payload_err], - &[remaining_gas, entry.argument(1)?.into(), payload_ok], + &[remaining_gas, entry.arg(1)?, payload_err], + &[remaining_gas, entry.arg(1)?, payload_ok], ], location, )); @@ -392,10 +387,9 @@ pub fn build_class_hash_try_from_felt252<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let value = entry.argument(1)?.into(); + let value = entry.arg(1)?; let limit = entry.append_op_result(arith::constant( context, @@ -406,13 +400,7 @@ pub fn build_class_hash_try_from_felt252<'ctx, 'this>( .ok_or(Error::ParseAttributeError)?, location, ))?; - let is_in_range = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ult, - value, - limit, - location, - ))?; + let is_in_range = entry.cmpi(context, CmpiPredicate::Ult, value, limit, location)?; entry.append_operation(helper.cond_br( context, @@ -456,10 +444,9 @@ pub fn build_contract_address_try_from_felt252<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let value = entry.argument(1)?.into(); + let value = entry.arg(1)?; let limit = entry.append_op_result(arith::constant( context, @@ -470,13 +457,7 @@ pub fn build_contract_address_try_from_felt252<'ctx, 'this>( .ok_or(Error::ParseAttributeError)?, location, ))?; - let is_in_range = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ult, - value, - limit, - location, - ))?; + let is_in_range = entry.cmpi(context, CmpiPredicate::Ult, value, limit, location)?; entry.append_operation(helper.cond_br( context, @@ -501,7 +482,7 @@ pub fn build_storage_read<'ctx, 'this>( let ptr = entry.load( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), )?; @@ -547,27 +528,17 @@ pub fn build_storage_read<'ctx, 'this>( helper .init_block() .alloca1(context, location, gas_ty, gas_layout.align())?; - entry.store( - context, - location, - gas_builtin_ptr, - entry.argument(0)?.into(), - )?; + entry.store(context, location, gas_builtin_ptr, entry.arg(0)?)?; // Allocate `address` argument and write the value. let address_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; - entry.store( - context, - location, - address_arg_ptr, - entry.argument(3)?.into(), - )?; + entry.store(context, location, address_arg_ptr, entry.arg(3)?)?; // Extract function pointer. let fn_ptr = entry.gep( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, &[GepIndex::Const( StarknetSyscallHandlerCallbacks::<()>::STORAGE_READ.try_into()?, )], @@ -582,7 +553,7 @@ pub fn build_storage_read<'ctx, 'this>( result_ptr, ptr, gas_builtin_ptr, - entry.argument(2)?.into(), + entry.arg(2)?, address_arg_ptr, ]) .build()?, @@ -644,8 +615,8 @@ pub fn build_storage_read<'ctx, 'this>( result_tag, [1, 0], [ - &[remaining_gas, entry.argument(1)?.into(), payload_err], - &[remaining_gas, entry.argument(1)?.into(), payload_ok], + &[remaining_gas, entry.arg(1)?, payload_err], + &[remaining_gas, entry.arg(1)?, payload_ok], ], location, )); @@ -665,7 +636,7 @@ pub fn build_storage_write<'ctx, 'this>( let ptr = entry.load( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), )?; @@ -715,30 +686,20 @@ pub fn build_storage_write<'ctx, 'this>( helper .init_block() .alloca1(context, location, gas_ty, gas_layout.align())?; - entry.store( - context, - location, - gas_builtin_ptr, - entry.argument(0)?.into(), - )?; + entry.store(context, location, gas_builtin_ptr, entry.arg(0)?)?; // Allocate `address` argument and write the value. let address_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; - entry.store( - context, - location, - address_arg_ptr, - entry.argument(3)?.into(), - )?; + entry.store(context, location, address_arg_ptr, entry.arg(3)?)?; // Allocate `value` argument and write the value. let value_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; - entry.store(context, location, value_arg_ptr, entry.argument(4)?.into())?; + entry.store(context, location, value_arg_ptr, entry.arg(4)?)?; let fn_ptr = entry.gep( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, &[GepIndex::Const( StarknetSyscallHandlerCallbacks::<()>::STORAGE_WRITE.try_into()?, )], @@ -753,7 +714,7 @@ pub fn build_storage_write<'ctx, 'this>( result_ptr, ptr, gas_builtin_ptr, - entry.argument(2)?.into(), + entry.arg(2)?, address_arg_ptr, value_arg_ptr, ]) @@ -816,8 +777,8 @@ pub fn build_storage_write<'ctx, 'this>( result_tag, [1, 0], [ - &[remaining_gas, entry.argument(1)?.into(), payload_err], - &[remaining_gas, entry.argument(1)?.into(), payload_ok], + &[remaining_gas, entry.arg(1)?, payload_err], + &[remaining_gas, entry.arg(1)?, payload_ok], ], location, )); @@ -856,8 +817,7 @@ pub fn build_storage_base_address_from_felt252<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let k_limit = entry.append_op_result(arith::constant( context, @@ -869,19 +829,18 @@ pub fn build_storage_base_address_from_felt252<'ctx, 'this>( location, ))?; - let limited_value = - entry.append_op_result(arith::subi(entry.argument(1)?.into(), k_limit, location))?; + let limited_value = entry.append_op_result(arith::subi(entry.arg(1)?, k_limit, location))?; - let is_within_limit = entry.append_op_result(arith::cmpi( + let is_within_limit = entry.cmpi( context, CmpiPredicate::Ult, - entry.argument(1)?.into(), + entry.arg(1)?, k_limit, location, - ))?; + )?; let value = entry.append_op_result(arith::select( is_within_limit, - entry.argument(1)?.into(), + entry.arg(1)?, limited_value, location, ))?; @@ -899,12 +858,8 @@ pub fn build_storage_address_from_base_and_offset<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let offset = entry.append_op_result(arith::extui( - entry.argument(1)?.into(), - entry.argument(0)?.r#type(), - location, - ))?; - let addr = entry.append_op_result(arith::addi(entry.argument(0)?.into(), offset, location))?; + let offset = entry.extui(entry.arg(1)?, entry.argument(0)?.r#type(), location)?; + let addr = entry.addi(entry.arg(0)?, offset, location)?; entry.append_operation(helper.br(0, &[addr], location)); Ok(()) @@ -919,10 +874,9 @@ pub fn build_storage_address_try_from_felt252<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let value = entry.argument(1)?.into(); + let value = entry.arg(1)?; let limit = entry.append_op_result(arith::constant( context, @@ -933,13 +887,7 @@ pub fn build_storage_address_try_from_felt252<'ctx, 'this>( .ok_or(Error::ParseAttributeError)?, location, ))?; - let is_in_range = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ult, - value, - limit, - location, - ))?; + let is_in_range = entry.cmpi(context, CmpiPredicate::Ult, value, limit, location)?; entry.append_operation(helper.cond_br( context, @@ -964,7 +912,7 @@ pub fn build_emit_event<'ctx, 'this>( let ptr = entry.load( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), )?; @@ -1016,7 +964,7 @@ pub fn build_emit_event<'ctx, 'this>( .alloca1(context, location, gas_ty, gas_layout.align())?; entry.append_operation(llvm::store( context, - entry.argument(0)?.into(), + entry.arg(0)?, gas_builtin_ptr, location, LoadStoreOptions::default(), @@ -1042,7 +990,7 @@ pub fn build_emit_event<'ctx, 'this>( ), get_integer_layout(64).align(), )?; - entry.store(context, location, keys_arg_ptr, entry.argument(2)?.into())?; + entry.store(context, location, keys_arg_ptr, entry.arg(2)?)?; // Allocate `data` argument and write the value. let data_arg_ptr = helper.init_block().alloca1( @@ -1064,12 +1012,12 @@ pub fn build_emit_event<'ctx, 'this>( ), get_integer_layout(64).align(), )?; - entry.store(context, location, data_arg_ptr, entry.argument(3)?.into())?; + entry.store(context, location, data_arg_ptr, entry.arg(3)?)?; let fn_ptr = entry.gep( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, &[GepIndex::Const( StarknetSyscallHandlerCallbacks::<()>::EMIT_EVENT.try_into()?, )], @@ -1146,8 +1094,8 @@ pub fn build_emit_event<'ctx, 'this>( result_tag, [1, 0], [ - &[remaining_gas, entry.argument(1)?.into(), payload_err], - &[remaining_gas, entry.argument(1)?.into(), payload_ok], + &[remaining_gas, entry.arg(1)?, payload_err], + &[remaining_gas, entry.arg(1)?, payload_ok], ], location, )); @@ -1167,7 +1115,7 @@ pub fn build_get_block_hash<'ctx, 'this>( let ptr = entry.load( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), )?; @@ -1215,7 +1163,7 @@ pub fn build_get_block_hash<'ctx, 'this>( .alloca1(context, location, gas_ty, gas_layout.align())?; entry.append_operation(llvm::store( context, - entry.argument(0)?.into(), + entry.arg(0)?, gas_builtin_ptr, location, LoadStoreOptions::default(), @@ -1225,7 +1173,7 @@ pub fn build_get_block_hash<'ctx, 'this>( let fn_ptr = entry.gep( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, &[GepIndex::Const( StarknetSyscallHandlerCallbacks::<()>::GET_BLOCK_HASH.try_into()?, )], @@ -1235,13 +1183,7 @@ pub fn build_get_block_hash<'ctx, 'this>( entry.append_operation( OperationBuilder::new("llvm.call", location) - .add_operands(&[ - fn_ptr, - result_ptr, - ptr, - gas_builtin_ptr, - entry.argument(2)?.into(), - ]) + .add_operands(&[fn_ptr, result_ptr, ptr, gas_builtin_ptr, entry.arg(2)?]) .build()?, ); @@ -1301,8 +1243,8 @@ pub fn build_get_block_hash<'ctx, 'this>( result_tag, [1, 0], [ - &[remaining_gas, entry.argument(1)?.into(), payload_err], - &[remaining_gas, entry.argument(1)?.into(), payload_ok], + &[remaining_gas, entry.arg(1)?, payload_err], + &[remaining_gas, entry.arg(1)?, payload_ok], ], location, )); @@ -1322,7 +1264,7 @@ pub fn build_get_execution_info<'ctx, 'this>( let ptr = entry.load( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), )?; @@ -1368,18 +1310,13 @@ pub fn build_get_execution_info<'ctx, 'this>( helper .init_block() .alloca1(context, location, gas_ty, gas_layout.align())?; - entry.store( - context, - location, - gas_builtin_ptr, - entry.argument(0)?.into(), - )?; + entry.store(context, location, gas_builtin_ptr, entry.arg(0)?)?; // Extract function pointer. let fn_ptr = entry.gep( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, &[GepIndex::Const( StarknetSyscallHandlerCallbacks::<()>::GET_EXECUTION_INFO.try_into()?, )], @@ -1449,8 +1386,8 @@ pub fn build_get_execution_info<'ctx, 'this>( result_tag, [1, 0], [ - &[remaining_gas, entry.argument(1)?.into(), payload_err], - &[remaining_gas, entry.argument(1)?.into(), payload_ok], + &[remaining_gas, entry.arg(1)?, payload_err], + &[remaining_gas, entry.arg(1)?, payload_ok], ], location, )); @@ -1470,7 +1407,7 @@ pub fn build_get_execution_info_v2<'ctx, 'this>( let ptr = entry.load( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), )?; @@ -1516,18 +1453,13 @@ pub fn build_get_execution_info_v2<'ctx, 'this>( helper .init_block() .alloca1(context, location, gas_ty, gas_layout.align())?; - entry.store( - context, - location, - gas_builtin_ptr, - entry.argument(0)?.into(), - )?; + entry.store(context, location, gas_builtin_ptr, entry.arg(0)?)?; // Extract function pointer. let fn_ptr = entry.gep( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, &[GepIndex::Const( StarknetSyscallHandlerCallbacks::<()>::GET_EXECUTION_INFOV2.try_into()?, )], @@ -1597,8 +1529,8 @@ pub fn build_get_execution_info_v2<'ctx, 'this>( result_tag, [1, 0], [ - &[remaining_gas, entry.argument(1)?.into(), payload_err], - &[remaining_gas, entry.argument(1)?.into(), payload_ok], + &[remaining_gas, entry.arg(1)?, payload_err], + &[remaining_gas, entry.arg(1)?, payload_ok], ], location, )); @@ -1618,7 +1550,7 @@ pub fn build_deploy<'ctx, 'this>( let ptr = entry.load( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), )?; @@ -1707,21 +1639,11 @@ pub fn build_deploy<'ctx, 'this>( helper .init_block() .alloca1(context, location, gas_ty, gas_layout.align())?; - entry.store( - context, - location, - gas_builtin_ptr, - entry.argument(0)?.into(), - )?; + entry.store(context, location, gas_builtin_ptr, entry.arg(0)?)?; // Allocate `class_hash` argument and write the value. let class_hash_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; - entry.store( - context, - location, - class_hash_arg_ptr, - entry.argument(2)?.into(), - )?; + entry.store(context, location, class_hash_arg_ptr, entry.arg(2)?)?; // Allocate `entry_point_selector` argument and write the value. let contract_address_salt_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; @@ -1729,7 +1651,7 @@ pub fn build_deploy<'ctx, 'this>( context, location, contract_address_salt_arg_ptr, - entry.argument(3)?.into(), + entry.arg(3)?, )?; // Allocate `calldata` argument and write the value. @@ -1752,17 +1674,12 @@ pub fn build_deploy<'ctx, 'this>( ), get_integer_layout(64).align(), )?; - entry.store( - context, - location, - calldata_arg_ptr, - entry.argument(4)?.into(), - )?; + entry.store(context, location, calldata_arg_ptr, entry.arg(4)?)?; let fn_ptr = entry.gep( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, &[GepIndex::Const( StarknetSyscallHandlerCallbacks::<()>::DEPLOY.try_into()?, )], @@ -1783,7 +1700,7 @@ pub fn build_deploy<'ctx, 'this>( entry .append_operation(llvm::extract_value( context, - entry.argument(5)?.into(), + entry.arg(5)?, DenseI64ArrayAttribute::new(context, &[0]), IntegerType::new(context, 1).into(), location, @@ -1850,10 +1767,10 @@ pub fn build_deploy<'ctx, 'this>( result_tag, [1, 0], [ - &[remaining_gas, entry.argument(1)?.into(), payload_err], + &[remaining_gas, entry.arg(1)?, payload_err], &[ remaining_gas, - entry.argument(1)?.into(), + entry.arg(1)?, entry.extract_value( context, location, @@ -1888,7 +1805,7 @@ pub fn build_keccak<'ctx, 'this>( let ptr = entry.load( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), )?; @@ -1934,12 +1851,7 @@ pub fn build_keccak<'ctx, 'this>( helper .init_block() .alloca1(context, location, gas_ty, gas_layout.align())?; - entry.store( - context, - location, - gas_builtin_ptr, - entry.argument(0)?.into(), - )?; + entry.store(context, location, gas_builtin_ptr, entry.arg(0)?)?; // Allocate `input` argument and write the value. let input_arg_ptr = helper.init_block().alloca1( @@ -1957,12 +1869,12 @@ pub fn build_keccak<'ctx, 'this>( ), get_integer_layout(64).align(), )?; - entry.store(context, location, input_arg_ptr, entry.argument(2)?.into())?; + entry.store(context, location, input_arg_ptr, entry.arg(2)?)?; let fn_ptr = entry.gep( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, &[GepIndex::Const( StarknetSyscallHandlerCallbacks::<()>::KECCAK.try_into()?, )], @@ -2031,8 +1943,8 @@ pub fn build_keccak<'ctx, 'this>( result_tag, [1, 0], [ - &[remaining_gas, entry.argument(1)?.into(), payload_err], - &[remaining_gas, entry.argument(1)?.into(), payload_ok], + &[remaining_gas, entry.arg(1)?, payload_err], + &[remaining_gas, entry.arg(1)?, payload_ok], ], location, )); @@ -2052,7 +1964,7 @@ pub fn build_library_call<'ctx, 'this>( let ptr = entry.load( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), )?; @@ -2098,30 +2010,15 @@ pub fn build_library_call<'ctx, 'this>( helper .init_block() .alloca1(context, location, gas_ty, gas_layout.align())?; - entry.store( - context, - location, - gas_builtin_ptr, - entry.argument(0)?.into(), - )?; + entry.store(context, location, gas_builtin_ptr, entry.arg(0)?)?; // Allocate `class_hash` argument and write the value. let class_hash_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; - entry.store( - context, - location, - class_hash_arg_ptr, - entry.argument(2)?.into(), - )?; + entry.store(context, location, class_hash_arg_ptr, entry.arg(2)?)?; // Allocate `entry_point_selector` argument and write the value. let function_selector_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; - entry.store( - context, - location, - function_selector_arg_ptr, - entry.argument(3)?.into(), - )?; + entry.store(context, location, function_selector_arg_ptr, entry.arg(3)?)?; // Allocate `calldata` argument and write the value. let calldata_arg_ptr = helper.init_block().alloca1( @@ -2143,17 +2040,12 @@ pub fn build_library_call<'ctx, 'this>( ), get_integer_layout(64).align(), )?; - entry.store( - context, - location, - calldata_arg_ptr, - entry.argument(4)?.into(), - )?; + entry.store(context, location, calldata_arg_ptr, entry.arg(4)?)?; let fn_ptr = entry.gep( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, &[GepIndex::Const( StarknetSyscallHandlerCallbacks::<()>::LIBRARY_CALL.try_into()?, )], @@ -2231,8 +2123,8 @@ pub fn build_library_call<'ctx, 'this>( result_tag, [1, 0], [ - &[remaining_gas, entry.argument(1)?.into(), payload_err], - &[remaining_gas, entry.argument(1)?.into(), payload_ok], + &[remaining_gas, entry.arg(1)?, payload_err], + &[remaining_gas, entry.arg(1)?, payload_ok], ], location, )); @@ -2252,7 +2144,7 @@ pub fn build_replace_class<'ctx, 'this>( let ptr = entry.load( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), )?; @@ -2302,26 +2194,16 @@ pub fn build_replace_class<'ctx, 'this>( helper .init_block() .alloca1(context, location, gas_ty, gas_layout.align())?; - entry.store( - context, - location, - gas_builtin_ptr, - entry.argument(0)?.into(), - )?; + entry.store(context, location, gas_builtin_ptr, entry.arg(0)?)?; // Allocate `class_hash` argument and write the value. let class_hash_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; - entry.store( - context, - location, - class_hash_arg_ptr, - entry.argument(2)?.into(), - )?; + entry.store(context, location, class_hash_arg_ptr, entry.arg(2)?)?; let fn_ptr = entry.gep( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, &[GepIndex::Const( StarknetSyscallHandlerCallbacks::<()>::REPLACE_CLASS.try_into()?, )], @@ -2391,8 +2273,8 @@ pub fn build_replace_class<'ctx, 'this>( result_tag, [1, 0], [ - &[remaining_gas, entry.argument(1)?.into(), payload_err], - &[remaining_gas, entry.argument(1)?.into(), payload_ok], + &[remaining_gas, entry.arg(1)?, payload_err], + &[remaining_gas, entry.arg(1)?, payload_ok], ], location, )); @@ -2412,7 +2294,7 @@ pub fn build_send_message_to_l1<'ctx, 'this>( let ptr = entry.load( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), )?; @@ -2462,21 +2344,11 @@ pub fn build_send_message_to_l1<'ctx, 'this>( helper .init_block() .alloca1(context, location, gas_ty, gas_layout.align())?; - entry.store( - context, - location, - gas_builtin_ptr, - entry.argument(0)?.into(), - )?; + entry.store(context, location, gas_builtin_ptr, entry.arg(0)?)?; // Allocate `to_address` argument and write the value. let to_address_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; - entry.store( - context, - location, - to_address_arg_ptr, - entry.argument(2)?.into(), - )?; + entry.store(context, location, to_address_arg_ptr, entry.arg(2)?)?; // Allocate `payload` argument and write the value. let payload_arg_ptr = helper.init_block().alloca1( @@ -2494,17 +2366,12 @@ pub fn build_send_message_to_l1<'ctx, 'this>( ), get_integer_layout(64).align(), )?; - entry.store( - context, - location, - payload_arg_ptr, - entry.argument(3)?.into(), - )?; + entry.store(context, location, payload_arg_ptr, entry.arg(3)?)?; let fn_ptr = entry.gep( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, &[GepIndex::Const( StarknetSyscallHandlerCallbacks::<()>::SEND_MESSAGE_TO_L1.try_into()?, )], @@ -2581,8 +2448,8 @@ pub fn build_send_message_to_l1<'ctx, 'this>( result_tag, [1, 0], [ - &[remaining_gas, entry.argument(1)?.into(), payload_err], - &[remaining_gas, entry.argument(1)?.into(), payload_ok], + &[remaining_gas, entry.arg(1)?, payload_err], + &[remaining_gas, entry.arg(1)?, payload_ok], ], location, )); @@ -2602,7 +2469,7 @@ pub fn build_sha256_process_block_syscall<'ctx, 'this>( let ptr = entry .append_operation(llvm::load( context, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), @@ -2654,19 +2521,19 @@ pub fn build_sha256_process_block_syscall<'ctx, 'this>( .alloca1(context, location, gas_ty, gas_layout.align())?; entry.append_operation(llvm::store( context, - entry.argument(0)?.into(), + entry.arg(0)?, gas_builtin_ptr, location, LoadStoreOptions::default(), )); - let sha256_prev_state_ptr = entry.argument(2)?.into(); - let sha256_current_block_ptr = entry.argument(3)?.into(); + let sha256_prev_state_ptr = entry.arg(2)?; + let sha256_current_block_ptr = entry.arg(3)?; let fn_ptr = entry.gep( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, &[GepIndex::Const( StarknetSyscallHandlerCallbacks::<()>::SHA256_PROCESS_BLOCK.try_into()?, )], @@ -2733,8 +2600,8 @@ pub fn build_sha256_process_block_syscall<'ctx, 'this>( result_tag, [1, 0], [ - &[remaining_gas, entry.argument(1)?.into(), payload_err], - &[remaining_gas, entry.argument(1)?.into(), payload_ok], + &[remaining_gas, entry.arg(1)?, payload_err], + &[remaining_gas, entry.arg(1)?, payload_ok], ], location, )); @@ -2754,7 +2621,7 @@ pub fn build_get_class_hash_at<'ctx, 'this>( let ptr = entry.load( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, llvm::r#type::pointer(context, 0), )?; @@ -2802,7 +2669,7 @@ pub fn build_get_class_hash_at<'ctx, 'this>( .alloca1(context, location, gas_ty, gas_layout.align())?; entry.append_operation(llvm::store( context, - entry.argument(0)?.into(), + entry.arg(0)?, gas_builtin_ptr, location, LoadStoreOptions::default(), @@ -2810,18 +2677,13 @@ pub fn build_get_class_hash_at<'ctx, 'this>( // Allocate `contract_address` argument and write the value. let contract_address_ptr = helper.init_block().alloca_int(context, location, 252)?; - entry.store( - context, - location, - contract_address_ptr, - entry.argument(2)?.into(), - )?; + entry.store(context, location, contract_address_ptr, entry.arg(2)?)?; // Extract function pointer. let fn_ptr = entry.gep( context, location, - entry.argument(1)?.into(), + entry.arg(1)?, &[GepIndex::Const( StarknetSyscallHandlerCallbacks::<()>::GET_CLASS_HASH_AT.try_into()?, )], @@ -2891,8 +2753,8 @@ pub fn build_get_class_hash_at<'ctx, 'this>( result_tag, [1, 0], [ - &[remaining_gas, entry.argument(1)?.into(), payload_err], - &[remaining_gas, entry.argument(1)?.into(), payload_ok], + &[remaining_gas, entry.arg(1)?, payload_err], + &[remaining_gas, entry.arg(1)?, payload_ok], ], location, )); diff --git a/src/libfuncs/struct.rs b/src/libfuncs/struct.rs index 0e9341528..7e30cfb3b 100644 --- a/src/libfuncs/struct.rs +++ b/src/libfuncs/struct.rs @@ -105,7 +105,7 @@ pub fn build_deconstruct<'ctx, 'this>( metadata: &mut MetadataStorage, info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let container = entry.argument(0)?.into(); + let container = entry.arg(0)?; let mut fields = Vec::::with_capacity(info.branch_signatures()[0].vars.len()); for (i, var_info) in info.branch_signatures()[0].vars.iter().enumerate() { diff --git a/src/libfuncs/uint128.rs b/src/libfuncs/uint128.rs index 9f1ca14c0..cb96ca053 100644 --- a/src/libfuncs/uint128.rs +++ b/src/libfuncs/uint128.rs @@ -91,10 +91,9 @@ pub fn build_byte_reverse<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let bitwise = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let bitwise = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let arg1 = entry.argument(1)?.into(); + let arg1 = entry.arg(1)?; let res = entry.append_op_result(ods::llvm::intr_bswap(context, arg1, location).into())?; @@ -139,11 +138,10 @@ pub fn build_divmod<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let result_div = entry.append_op_result(arith::divui(lhs, rhs, location))?; let result_rem = entry.append_op_result(arith::remui(lhs, rhs, location))?; @@ -162,8 +160,8 @@ pub fn build_equal<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); - let arg1: Value = entry.argument(1)?.into(); + let arg0: Value = entry.arg(0)?; + let arg1: Value = entry.arg(1)?; let op0 = entry.append_operation(arith::cmpi( context, @@ -194,35 +192,20 @@ pub fn build_from_felt252<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let value = entry.argument(1)?.into(); + let value = entry.arg(1)?; let k128 = entry.const_int(context, location, 128, 252)?; let bound = BigUint::from(u128::MAX) + 1u32; let k_u128_max_1 = entry.const_int(context, location, bound, 252)?; - let is_wide = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Uge, - value, - k_u128_max_1, - location, - ))?; + let is_wide = entry.cmpi(context, CmpiPredicate::Uge, value, k_u128_max_1, location)?; - let lsb_bits = entry.append_op_result(arith::trunci( - value, - IntegerType::new(context, 128).into(), - location, - ))?; + let lsb_bits = entry.trunci(value, IntegerType::new(context, 128).into(), location)?; - let msb_bits = entry.append_op_result(arith::shrui(value, k128, location))?; - let msb_bits = entry.append_op_result(arith::trunci( - msb_bits, - IntegerType::new(context, 128).into(), - location, - ))?; + let msb_bits = entry.shrui(value, k128, location)?; + let msb_bits = entry.trunci(msb_bits, IntegerType::new(context, 128).into(), location)?; entry.append_operation(helper.cond_br( context, @@ -244,17 +227,11 @@ pub fn build_is_zero<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); + let arg0: Value = entry.arg(0)?; let const_0 = entry.const_int_from_type(context, location, 0, arg0.r#type())?; - let condition = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - arg0, - const_0, - location, - ))?; + let condition = entry.cmpi(context, CmpiPredicate::Eq, arg0, const_0, location)?; entry.append_operation(helper.cond_br(context, condition, [0, 1], [&[], &[arg0]], location)); Ok(()) @@ -271,10 +248,10 @@ pub fn build_operation<'ctx, 'this>( info: &IntOperationConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let op_name = match info.operator { IntOperator::OverflowingAdd => "llvm.intr.uadd.with.overflow", @@ -325,21 +302,14 @@ pub fn build_square_root<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let i64_ty = IntegerType::new(context, 64).into(); let i128_ty = IntegerType::new(context, 128).into(); let k1 = entry.const_int_from_type(context, location, 1, i128_ty)?; - let is_small = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ule, - entry.argument(1)?.into(), - k1, - location, - ))?; + let is_small = entry.cmpi(context, CmpiPredicate::Ule, entry.arg(1)?, k1, location)?; let result = entry.append_op_result(scf::r#if( is_small, @@ -348,7 +318,7 @@ pub fn build_square_root<'ctx, 'this>( let region = Region::new(); let block = region.append_block(Block::new(&[])); - block.append_operation(scf::r#yield(&[entry.argument(1)?.into()], location)); + block.append_operation(scf::r#yield(&[entry.arg(1)?], location)); region }, @@ -362,7 +332,7 @@ pub fn build_square_root<'ctx, 'this>( ods::llvm::intr_ctlz( context, i128_ty, - entry.argument(1)?.into(), + entry.arg(1)?, IntegerAttribute::new(IntegerType::new(context, 1).into(), 1), location, ) @@ -371,7 +341,7 @@ pub fn build_square_root<'ctx, 'this>( let num_bits = block.append_op_result(arith::subi(k128, leading_zeros, location))?; - let shift_amount = block.append_op_result(arith::addi(num_bits, k1, location))?; + let shift_amount = block.addi(num_bits, k1, location)?; let parity_mask = block.const_int_from_type(context, location, -2, i128_ty)?; let shift_amount = @@ -386,32 +356,16 @@ pub fn build_square_root<'ctx, 'this>( let block = region .append_block(Block::new(&[(i128_ty, location), (i128_ty, location)])); - let result = block.append_op_result(arith::shli( - block.argument(0)?.into(), - k1, - location, - ))?; + let result = block.shli(block.arg(0)?, k1, location)?; let large_candidate = block.append_op_result(arith::xori(result, k1, location))?; - let large_candidate_squared = block.append_op_result(arith::muli( - large_candidate, - large_candidate, - location, - ))?; + let large_candidate_squared = + block.muli(large_candidate, large_candidate, location)?; - let threshold = block.append_op_result(arith::shrui( - entry.argument(1)?.into(), - block.argument(1)?.into(), - location, - ))?; - let threshold_is_poison = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - block.argument(1)?.into(), - k128, - location, - ))?; + let threshold = block.shrui(entry.arg(1)?, block.arg(1)?, location)?; + let threshold_is_poison = + block.cmpi(context, CmpiPredicate::Eq, block.arg(1)?, k128, location)?; let threshold = block.append_op_result( OperationBuilder::new("arith.select", location) .add_operands(&[threshold_is_poison, k0, threshold]) @@ -419,13 +373,13 @@ pub fn build_square_root<'ctx, 'this>( .build()?, )?; - let is_in_range = block.append_op_result(arith::cmpi( + let is_in_range = block.cmpi( context, CmpiPredicate::Ule, large_candidate_squared, threshold, location, - ))?; + )?; let result = block.append_op_result( OperationBuilder::new("arith.select", location) @@ -436,19 +390,11 @@ pub fn build_square_root<'ctx, 'this>( let k2 = block.const_int_from_type(context, location, 2, i128_ty)?; - let shift_amount = block.append_op_result(arith::subi( - block.argument(1)?.into(), - k2, - location, - ))?; + let shift_amount = + block.append_op_result(arith::subi(block.arg(1)?, k2, location))?; - let should_continue = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - shift_amount, - k0, - location, - ))?; + let should_continue = + block.cmpi(context, CmpiPredicate::Sge, shift_amount, k0, location)?; block.append_operation(scf::condition( should_continue, &[result, shift_amount], @@ -463,7 +409,7 @@ pub fn build_square_root<'ctx, 'this>( .append_block(Block::new(&[(i128_ty, location), (i128_ty, location)])); block.append_operation(scf::r#yield( - &[block.argument(0)?.into(), block.argument(1)?.into()], + &[block.arg(0)?, block.argument(1)?.into()], location, )); @@ -479,7 +425,7 @@ pub fn build_square_root<'ctx, 'this>( location, ))?; - let result = entry.append_op_result(arith::trunci(result, i64_ty, location))?; + let result = entry.trunci(result, i64_ty, location)?; entry.append_operation(helper.br(0, &[range_check, result], location)); Ok(()) @@ -496,7 +442,7 @@ pub fn build_to_felt252<'ctx, 'this>( _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let op = entry.append_operation(arith::extui( - entry.argument(0)?.into(), + entry.arg(0)?, IntegerType::new(context, 252).into(), location, )); @@ -515,8 +461,8 @@ pub fn build_guarantee_mul<'ctx, 'this>( metadata: &mut MetadataStorage, info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let lhs: Value = entry.argument(0)?.into(); - let rhs: Value = entry.argument(1)?.into(); + let lhs: Value = entry.arg(0)?; + let rhs: Value = entry.arg(1)?; let origin_type = lhs.r#type(); @@ -529,15 +475,15 @@ pub fn build_guarantee_mul<'ctx, 'this>( &info.output_types()[0][2], )?; - let lhs = entry.append_op_result(arith::extui(lhs, target_type, location))?; - let rhs = entry.append_op_result(arith::extui(rhs, target_type, location))?; - let result = entry.append_op_result(arith::muli(lhs, rhs, location))?; - let result_lo = entry.append_op_result(arith::trunci(result, origin_type, location))?; + let lhs = entry.extui(lhs, target_type, location)?; + let rhs = entry.extui(rhs, target_type, location)?; + let result = entry.muli(lhs, rhs, location)?; + let result_lo = entry.trunci(result, origin_type, location)?; let const_128 = entry.const_int_from_type(context, location, 128, target_type)?; - let result_hi = entry.append_op_result(arith::shrui(result, const_128, location))?; - let result_hi = entry.append_op_result(arith::trunci(result_hi, origin_type, location))?; + let result_hi = entry.shrui(result, const_128, location)?; + let result_hi = entry.trunci(result_hi, origin_type, location)?; let guarantee = entry.append_op_result(llvm::undef(guarantee_type, location))?; @@ -555,8 +501,7 @@ pub fn build_guarantee_verify<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; entry.append_operation(helper.br(0, &[range_check], location)); Ok(()) diff --git a/src/libfuncs/uint16.rs b/src/libfuncs/uint16.rs index 480cc8a51..02aeae934 100644 --- a/src/libfuncs/uint16.rs +++ b/src/libfuncs/uint16.rs @@ -108,10 +108,10 @@ pub fn build_operation<'ctx, 'this>( info: &IntOperationConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let op_name = match info.operator { IntOperator::OverflowingAdd => "llvm.intr.uadd.with.overflow", @@ -162,24 +162,12 @@ pub fn build_equal<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); - let arg1: Value = entry.argument(1)?.into(); + let arg0: Value = entry.arg(0)?; + let arg1: Value = entry.arg(1)?; - let op0 = entry.append_operation(arith::cmpi( - context, - CmpiPredicate::Eq, - arg0, - arg1, - location, - )); + let cond = entry.cmpi(context, CmpiPredicate::Eq, arg0, arg1, location)?; - entry.append_operation(helper.cond_br( - context, - op0.result(0)?.into(), - [1, 0], - [&[]; 2], - location, - )); + entry.append_operation(helper.cond_br(context, cond, [1, 0], [&[]; 2], location)); Ok(()) } @@ -193,7 +181,7 @@ pub fn build_is_zero<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); + let arg0: Value = entry.arg(0)?; let const_0 = entry.append_op_result(arith::constant( context, @@ -201,13 +189,7 @@ pub fn build_is_zero<'ctx, 'this>( location, ))?; - let condition = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - arg0, - const_0, - location, - ))?; + let condition = entry.cmpi(context, CmpiPredicate::Eq, arg0, const_0, location)?; entry.append_operation(helper.cond_br(context, condition, [0, 1], [&[], &[arg0]], location)); @@ -223,11 +205,10 @@ pub fn build_divmod<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let result_div = entry.append_op_result(arith::divui(lhs, rhs, location))?; let result_rem = entry.append_op_result(arith::remui(lhs, rhs, location))?; @@ -253,12 +234,12 @@ pub fn build_widemul<'ctx, 'this>( metadata, &info.output_types()[0][0], )?; - let lhs: Value = entry.argument(0)?.into(); - let rhs: Value = entry.argument(1)?.into(); + let lhs: Value = entry.arg(0)?; + let rhs: Value = entry.arg(1)?; - let lhs = entry.append_op_result(arith::extui(lhs, target_type, location))?; - let rhs = entry.append_op_result(arith::extui(rhs, target_type, location))?; - let result = entry.append_op_result(arith::muli(lhs, rhs, location))?; + let lhs = entry.extui(lhs, target_type, location)?; + let rhs = entry.extui(rhs, target_type, location)?; + let result = entry.muli(lhs, rhs, location)?; entry.append_operation(helper.br(0, &[result], location)); Ok(()) @@ -281,9 +262,9 @@ pub fn build_to_felt252<'ctx, 'this>( metadata, &info.branch_signatures()[0].vars[0].ty, )?; - let value: Value = entry.argument(0)?.into(); + let value: Value = entry.arg(0)?; - let result = entry.append_op_result(arith::extui(value, felt252_ty, location))?; + let result = entry.extui(value, felt252_ty, location)?; entry.append_operation(helper.br(0, &[result], location)); @@ -300,8 +281,7 @@ pub fn build_square_root<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let i8_ty = IntegerType::new(context, 8).into(); let i16_ty = IntegerType::new(context, 16).into(); @@ -312,13 +292,7 @@ pub fn build_square_root<'ctx, 'this>( location, ))?; - let is_small = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ule, - entry.argument(1)?.into(), - k1, - location, - ))?; + let is_small = entry.cmpi(context, CmpiPredicate::Ule, entry.arg(1)?, k1, location)?; let result = entry.append_op_result(scf::r#if( is_small, @@ -327,7 +301,7 @@ pub fn build_square_root<'ctx, 'this>( let region = Region::new(); let block = region.append_block(Block::new(&[])); - block.append_operation(scf::r#yield(&[entry.argument(1)?.into()], location)); + block.append_operation(scf::r#yield(&[entry.arg(1)?], location)); region }, @@ -345,7 +319,7 @@ pub fn build_square_root<'ctx, 'this>( ods::llvm::intr_ctlz( context, i16_ty, - entry.argument(1)?.into(), + entry.arg(1)?, IntegerAttribute::new(IntegerType::new(context, 1).into(), 1), location, ) @@ -354,7 +328,7 @@ pub fn build_square_root<'ctx, 'this>( let num_bits = block.append_op_result(arith::subi(k16, leading_zeros, location))?; - let shift_amount = block.append_op_result(arith::addi(num_bits, k1, location))?; + let shift_amount = block.addi(num_bits, k1, location)?; let parity_mask = block.append_op_result(arith::constant( context, @@ -377,32 +351,16 @@ pub fn build_square_root<'ctx, 'this>( let block = region.append_block(Block::new(&[(i16_ty, location), (i16_ty, location)])); - let result = block.append_op_result(arith::shli( - block.argument(0)?.into(), - k1, - location, - ))?; + let result = block.shli(block.arg(0)?, k1, location)?; let large_candidate = block.append_op_result(arith::xori(result, k1, location))?; - let large_candidate_squared = block.append_op_result(arith::muli( - large_candidate, - large_candidate, - location, - ))?; + let large_candidate_squared = + block.muli(large_candidate, large_candidate, location)?; - let threshold = block.append_op_result(arith::shrui( - entry.argument(1)?.into(), - block.argument(1)?.into(), - location, - ))?; - let threshold_is_poison = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - block.argument(1)?.into(), - k16, - location, - ))?; + let threshold = block.shrui(entry.arg(1)?, block.arg(1)?, location)?; + let threshold_is_poison = + block.cmpi(context, CmpiPredicate::Eq, block.arg(1)?, k16, location)?; let threshold = block.append_op_result( OperationBuilder::new("arith.select", location) .add_operands(&[threshold_is_poison, k0, threshold]) @@ -410,13 +368,13 @@ pub fn build_square_root<'ctx, 'this>( .build()?, )?; - let is_in_range = block.append_op_result(arith::cmpi( + let is_in_range = block.cmpi( context, CmpiPredicate::Ule, large_candidate_squared, threshold, location, - ))?; + )?; let result = block.append_op_result( OperationBuilder::new("arith.select", location) @@ -431,19 +389,11 @@ pub fn build_square_root<'ctx, 'this>( location, ))?; - let shift_amount = block.append_op_result(arith::subi( - block.argument(1)?.into(), - k2, - location, - ))?; + let shift_amount = + block.append_op_result(arith::subi(block.arg(1)?, k2, location))?; - let should_continue = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - shift_amount, - k0, - location, - ))?; + let should_continue = + block.cmpi(context, CmpiPredicate::Sge, shift_amount, k0, location)?; block.append_operation(scf::condition( should_continue, &[result, shift_amount], @@ -458,7 +408,7 @@ pub fn build_square_root<'ctx, 'this>( region.append_block(Block::new(&[(i16_ty, location), (i16_ty, location)])); block.append_operation(scf::r#yield( - &[block.argument(0)?.into(), block.argument(1)?.into()], + &[block.arg(0)?, block.argument(1)?.into()], location, )); @@ -474,7 +424,7 @@ pub fn build_square_root<'ctx, 'this>( location, ))?; - let result = entry.append_op_result(arith::trunci(result, i8_ty, location))?; + let result = entry.trunci(result, i8_ty, location)?; entry.append_operation(helper.br(0, &[range_check, result], location)); Ok(()) @@ -491,9 +441,9 @@ pub fn build_from_felt252<'ctx, 'this>( info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let value: Value = entry.argument(1)?.into(); + let value: Value = entry.arg(1)?; let felt252_ty = registry.build_type( context, @@ -517,13 +467,7 @@ pub fn build_from_felt252<'ctx, 'this>( location, ))?; - let is_ule = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ule, - value, - const_max, - location, - ))?; + let is_ule = entry.cmpi(context, CmpiPredicate::Ule, value, const_max, location)?; let block_success = helper.append_block(Block::new(&[])); let block_failure = helper.append_block(Block::new(&[])); @@ -538,7 +482,7 @@ pub fn build_from_felt252<'ctx, 'this>( location, )); - let value = block_success.append_op_result(arith::trunci(value, result_ty, location))?; + let value = block_success.trunci(value, result_ty, location)?; block_success.append_operation(helper.br(0, &[range_check, value], location)); block_failure.append_operation(helper.br(1, &[range_check], location)); diff --git a/src/libfuncs/uint256.rs b/src/libfuncs/uint256.rs index a1f2fd64f..dacaf29b5 100644 --- a/src/libfuncs/uint256.rs +++ b/src/libfuncs/uint256.rs @@ -1,6 +1,6 @@ //! # `u256`-related libfuncs -use super::LibfuncHelper; +use super::{BlockExt, LibfuncHelper}; use crate::{error::Result, metadata::MetadataStorage, utils::ProgramRegistryExt}; use cairo_lang_sierra::{ extensions::{ @@ -61,8 +61,7 @@ pub fn build_divmod<'ctx, 'this>( metadata: &mut MetadataStorage, info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let i128_ty = IntegerType::new(context, 128).into(); let i256_ty = IntegerType::new(context, 256).into(); @@ -75,8 +74,8 @@ pub fn build_divmod<'ctx, 'this>( &info.output_types()[0][3], )?; - let lhs_struct: Value = entry.argument(1)?.into(); - let rhs_struct: Value = entry.argument(2)?.into(); + let lhs_struct: Value = entry.arg(1)?; + let rhs_struct: Value = entry.arg(2)?; let lhs_lo = entry .append_operation(llvm::extract_value( @@ -276,7 +275,7 @@ pub fn build_is_zero<'ctx, 'this>( ) -> Result<()> { let i128_ty = IntegerType::new(context, 128).into(); - let val_struct = entry.argument(0)?.into(); + let val_struct = entry.arg(0)?; let val_lo = entry .append_operation(llvm::extract_value( context, @@ -352,13 +351,12 @@ pub fn build_square_root<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let i128_ty = IntegerType::new(context, 128).into(); let i256_ty = IntegerType::new(context, 256).into(); - let arg_struct = entry.argument(1)?.into(); + let arg_struct = entry.arg(1)?; let arg_lo = entry .append_operation(llvm::extract_value( context, @@ -509,11 +507,7 @@ pub fn build_square_root<'ctx, 'this>( ])); let result = block - .append_operation(arith::shli( - block.argument(0)?.into(), - k1, - location, - )) + .append_operation(arith::shli(block.arg(0)?, k1, location)) .result(0)? .into(); let large_candidate = block @@ -531,18 +525,14 @@ pub fn build_square_root<'ctx, 'this>( .into(); let threshold = block - .append_operation(arith::shrui( - arg_value, - block.argument(1)?.into(), - location, - )) + .append_operation(arith::shrui(arg_value, block.arg(1)?, location)) .result(0)? .into(); let threshold_is_poison = block .append_operation(arith::cmpi( context, CmpiPredicate::Eq, - block.argument(1)?.into(), + block.arg(1)?, k128, location, )) @@ -589,11 +579,7 @@ pub fn build_square_root<'ctx, 'this>( .into(); let shift_amount = block - .append_operation(arith::subi( - block.argument(1)?.into(), - k2, - location, - )) + .append_operation(arith::subi(block.arg(1)?, k2, location)) .result(0)? .into(); @@ -623,7 +609,7 @@ pub fn build_square_root<'ctx, 'this>( ])); block.append_operation(scf::r#yield( - &[block.argument(0)?.into(), block.argument(1)?.into()], + &[block.arg(0)?, block.argument(1)?.into()], location, )); @@ -665,7 +651,7 @@ pub fn build_u256_guarantee_inv_mod_n<'ctx, 'this>( let i128_ty = IntegerType::new(context, 128).into(); let i256_ty = IntegerType::new(context, 256).into(); - let lhs_struct = entry.argument(1)?.into(); + let lhs_struct = entry.arg(1)?; let lhs_lo = entry .append_operation(llvm::extract_value( context, @@ -687,7 +673,7 @@ pub fn build_u256_guarantee_inv_mod_n<'ctx, 'this>( .result(0)? .into(); - let rhs_struct = entry.argument(2)?.into(); + let rhs_struct = entry.arg(2)?; let rhs_lo = entry .append_operation(llvm::extract_value( context, @@ -783,29 +769,25 @@ pub fn build_u256_guarantee_inv_mod_n<'ctx, 'this>( ])); let q = block - .append_operation(arith::divui( - block.argument(1)?.into(), - block.argument(0)?.into(), - location, - )) + .append_operation(arith::divui(block.arg(1)?, block.arg(0)?, location)) .result(0)? .into(); let q_c = block - .append_operation(arith::muli(q, block.argument(0)?.into(), location)) + .append_operation(arith::muli(q, block.arg(0)?, location)) .result(0)? .into(); let c = block - .append_operation(arith::subi(block.argument(1)?.into(), q_c, location)) + .append_operation(arith::subi(block.arg(1)?, q_c, location)) .result(0)? .into(); let q_uc = block - .append_operation(arith::muli(q, block.argument(2)?.into(), location)) + .append_operation(arith::muli(q, block.arg(2)?, location)) .result(0)? .into(); let u_c = block - .append_operation(arith::subi(block.argument(3)?.into(), q_uc, location)) + .append_operation(arith::subi(block.arg(3)?, q_uc, location)) .result(0)? .into(); @@ -815,7 +797,7 @@ pub fn build_u256_guarantee_inv_mod_n<'ctx, 'this>( .into(); block.append_operation(scf::condition( should_continue, - &[c, block.argument(0)?.into(), u_c, block.argument(2)?.into()], + &[c, block.arg(0)?, u_c, block.argument(2)?.into()], location, )); @@ -831,12 +813,7 @@ pub fn build_u256_guarantee_inv_mod_n<'ctx, 'this>( ])); block.append_operation(scf::r#yield( - &[ - block.argument(0)?.into(), - block.argument(1)?.into(), - block.argument(2)?.into(), - block.argument(3)?.into(), - ], + &[block.arg(0)?, block.arg(1)?, block.arg(2)?, block.arg(3)?], location, )); @@ -967,7 +944,7 @@ pub fn build_u256_guarantee_inv_mod_n<'ctx, 'this>( [0, 1], [ &[ - entry.argument(0)?.into(), + entry.arg(0)?, result_inv, guarantee, guarantee, @@ -978,7 +955,7 @@ pub fn build_u256_guarantee_inv_mod_n<'ctx, 'this>( guarantee, guarantee, ], - &[entry.argument(0)?.into(), guarantee, guarantee], + &[entry.arg(0)?, guarantee, guarantee], ], location, )); diff --git a/src/libfuncs/uint32.rs b/src/libfuncs/uint32.rs index 01e54e11f..d7c3c5ade 100644 --- a/src/libfuncs/uint32.rs +++ b/src/libfuncs/uint32.rs @@ -108,10 +108,10 @@ pub fn build_operation<'ctx, 'this>( info: &IntOperationConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let op_name = match info.operator { IntOperator::OverflowingAdd => "llvm.intr.uadd.with.overflow", @@ -162,8 +162,8 @@ pub fn build_equal<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); - let arg1: Value = entry.argument(1)?.into(); + let arg0: Value = entry.arg(0)?; + let arg1: Value = entry.arg(1)?; let op0 = entry.append_operation(arith::cmpi( context, @@ -193,17 +193,11 @@ pub fn build_is_zero<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); + let arg0: Value = entry.arg(0)?; let const_0 = entry.const_int_from_type(context, location, 0, arg0.r#type())?; - let condition = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - arg0, - const_0, - location, - ))?; + let condition = entry.cmpi(context, CmpiPredicate::Eq, arg0, const_0, location)?; entry.append_operation(helper.cond_br(context, condition, [0, 1], [&[], &[arg0]], location)); @@ -219,11 +213,10 @@ pub fn build_divmod<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let result_div = entry.append_op_result(arith::divui(lhs, rhs, location))?; let result_rem = entry.append_op_result(arith::remui(lhs, rhs, location))?; @@ -249,12 +242,12 @@ pub fn build_widemul<'ctx, 'this>( metadata, &info.output_types()[0][0], )?; - let lhs: Value = entry.argument(0)?.into(); - let rhs: Value = entry.argument(1)?.into(); + let lhs: Value = entry.arg(0)?; + let rhs: Value = entry.arg(1)?; - let lhs = entry.append_op_result(arith::extui(lhs, target_type, location))?; - let rhs = entry.append_op_result(arith::extui(rhs, target_type, location))?; - let result = entry.append_op_result(arith::muli(lhs, rhs, location))?; + let lhs = entry.extui(lhs, target_type, location)?; + let rhs = entry.extui(rhs, target_type, location)?; + let result = entry.muli(lhs, rhs, location)?; entry.append_operation(helper.br(0, &[result], location)); Ok(()) @@ -277,9 +270,9 @@ pub fn build_to_felt252<'ctx, 'this>( metadata, &info.branch_signatures()[0].vars[0].ty, )?; - let value: Value = entry.argument(0)?.into(); + let value: Value = entry.arg(0)?; - let result = entry.append_op_result(arith::extui(value, felt252_ty, location))?; + let result = entry.extui(value, felt252_ty, location)?; entry.append_operation(helper.br(0, &[result], location)); @@ -296,21 +289,14 @@ pub fn build_square_root<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let i16_ty = IntegerType::new(context, 16).into(); let i32_ty = IntegerType::new(context, 32).into(); let k1 = entry.const_int_from_type(context, location, 1, i32_ty)?; - let is_small = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ule, - entry.argument(1)?.into(), - k1, - location, - ))?; + let is_small = entry.cmpi(context, CmpiPredicate::Ule, entry.arg(1)?, k1, location)?; let result = entry.append_op_result(scf::r#if( is_small, @@ -319,7 +305,7 @@ pub fn build_square_root<'ctx, 'this>( let region = Region::new(); let block = region.append_block(Block::new(&[])); - block.append_operation(scf::r#yield(&[entry.argument(1)?.into()], location)); + block.append_operation(scf::r#yield(&[entry.arg(1)?], location)); region }, @@ -333,7 +319,7 @@ pub fn build_square_root<'ctx, 'this>( ods::llvm::intr_ctlz( context, i32_ty, - entry.argument(1)?.into(), + entry.arg(1)?, IntegerAttribute::new(IntegerType::new(context, 1).into(), 1), location, ) @@ -342,7 +328,7 @@ pub fn build_square_root<'ctx, 'this>( let num_bits = block.append_op_result(arith::subi(k32, leading_zeros, location))?; - let shift_amount = block.append_op_result(arith::addi(num_bits, k1, location))?; + let shift_amount = block.addi(num_bits, k1, location)?; let parity_mask = block.const_int_from_type(context, location, -2, i32_ty)?; let shift_amount = @@ -357,32 +343,16 @@ pub fn build_square_root<'ctx, 'this>( let block = region.append_block(Block::new(&[(i32_ty, location), (i32_ty, location)])); - let result = block.append_op_result(arith::shli( - block.argument(0)?.into(), - k1, - location, - ))?; + let result = block.shli(block.arg(0)?, k1, location)?; let large_candidate = block.append_op_result(arith::xori(result, k1, location))?; - let large_candidate_squared = block.append_op_result(arith::muli( - large_candidate, - large_candidate, - location, - ))?; + let large_candidate_squared = + block.muli(large_candidate, large_candidate, location)?; - let threshold = block.append_op_result(arith::shrui( - entry.argument(1)?.into(), - block.argument(1)?.into(), - location, - ))?; - let threshold_is_poison = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - block.argument(1)?.into(), - k32, - location, - ))?; + let threshold = block.shrui(entry.arg(1)?, block.arg(1)?, location)?; + let threshold_is_poison = + block.cmpi(context, CmpiPredicate::Eq, block.arg(1)?, k32, location)?; let threshold = block.append_op_result( OperationBuilder::new("arith.select", location) .add_operands(&[threshold_is_poison, k0, threshold]) @@ -390,13 +360,13 @@ pub fn build_square_root<'ctx, 'this>( .build()?, )?; - let is_in_range = block.append_op_result(arith::cmpi( + let is_in_range = block.cmpi( context, CmpiPredicate::Ule, large_candidate_squared, threshold, location, - ))?; + )?; let result = block.append_op_result( OperationBuilder::new("arith.select", location) @@ -407,19 +377,11 @@ pub fn build_square_root<'ctx, 'this>( let k2 = block.const_int_from_type(context, location, 2, i32_ty)?; - let shift_amount = block.append_op_result(arith::subi( - block.argument(1)?.into(), - k2, - location, - ))?; + let shift_amount = + block.append_op_result(arith::subi(block.arg(1)?, k2, location))?; - let should_continue = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - shift_amount, - k0, - location, - ))?; + let should_continue = + block.cmpi(context, CmpiPredicate::Sge, shift_amount, k0, location)?; block.append_operation(scf::condition( should_continue, &[result, shift_amount], @@ -434,7 +396,7 @@ pub fn build_square_root<'ctx, 'this>( region.append_block(Block::new(&[(i32_ty, location), (i32_ty, location)])); block.append_operation(scf::r#yield( - &[block.argument(0)?.into(), block.argument(1)?.into()], + &[block.arg(0)?, block.argument(1)?.into()], location, )); @@ -450,7 +412,7 @@ pub fn build_square_root<'ctx, 'this>( location, ))?; - let result = entry.append_op_result(arith::trunci(result, i16_ty, location))?; + let result = entry.trunci(result, i16_ty, location)?; entry.append_operation(helper.br(0, &[range_check, result], location)); Ok(()) @@ -467,9 +429,9 @@ pub fn build_from_felt252<'ctx, 'this>( info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let value: Value = entry.argument(1)?.into(); + let value: Value = entry.arg(1)?; let felt252_ty = registry.build_type( context, @@ -488,13 +450,7 @@ pub fn build_from_felt252<'ctx, 'this>( let const_max = entry.const_int_from_type(context, location, u32::MAX, felt252_ty)?; - let is_ule = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ule, - value, - const_max, - location, - ))?; + let is_ule = entry.cmpi(context, CmpiPredicate::Ule, value, const_max, location)?; let block_success = helper.append_block(Block::new(&[])); let block_failure = helper.append_block(Block::new(&[])); @@ -509,7 +465,7 @@ pub fn build_from_felt252<'ctx, 'this>( location, )); - let value = block_success.append_op_result(arith::trunci(value, result_ty, location))?; + let value = block_success.trunci(value, result_ty, location)?; block_success.append_operation(helper.br(0, &[range_check, value], location)); block_failure.append_operation(helper.br(1, &[range_check], location)); diff --git a/src/libfuncs/uint512.rs b/src/libfuncs/uint512.rs index 565c6ee40..287a9a14b 100644 --- a/src/libfuncs/uint512.rs +++ b/src/libfuncs/uint512.rs @@ -48,8 +48,7 @@ pub fn build_divmod_u256<'ctx, 'this>( metadata: &mut MetadataStorage, info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let i128_ty = IntegerType::new(context, 128).into(); let i512_ty = IntegerType::new(context, 512).into(); @@ -62,8 +61,8 @@ pub fn build_divmod_u256<'ctx, 'this>( &info.output_types()[0][3], )?; - let lhs_struct: Value = entry.argument(1)?.into(); - let rhs_struct: Value = entry.argument(2)?.into(); + let lhs_struct: Value = entry.arg(1)?; + let rhs_struct: Value = entry.arg(2)?; let dividend = ( entry.extract_value(context, location, lhs_struct, i128_ty, 0)?, @@ -77,14 +76,14 @@ pub fn build_divmod_u256<'ctx, 'this>( ); let dividend = ( - entry.append_op_result(arith::extui(dividend.0, i512_ty, location))?, - entry.append_op_result(arith::extui(dividend.1, i512_ty, location))?, - entry.append_op_result(arith::extui(dividend.2, i512_ty, location))?, - entry.append_op_result(arith::extui(dividend.3, i512_ty, location))?, + entry.extui(dividend.0, i512_ty, location)?, + entry.extui(dividend.1, i512_ty, location)?, + entry.extui(dividend.2, i512_ty, location)?, + entry.extui(dividend.3, i512_ty, location)?, ); let divisor = ( - entry.append_op_result(arith::extui(divisor.0, i512_ty, location))?, - entry.append_op_result(arith::extui(divisor.1, i512_ty, location))?, + entry.extui(divisor.0, i512_ty, location)?, + entry.extui(divisor.1, i512_ty, location)?, ); let k128 = entry.const_int_from_type(context, location, 128, i512_ty)?; @@ -93,14 +92,11 @@ pub fn build_divmod_u256<'ctx, 'this>( let dividend = ( dividend.0, - entry.append_op_result(arith::shli(dividend.1, k128, location))?, - entry.append_op_result(arith::shli(dividend.2, k256, location))?, - entry.append_op_result(arith::shli(dividend.3, k384, location))?, - ); - let divisor = ( - divisor.0, - entry.append_op_result(arith::shli(divisor.1, k128, location))?, + entry.shli(dividend.1, k128, location)?, + entry.shli(dividend.2, k256, location)?, + entry.shli(dividend.3, k384, location)?, ); + let divisor = (divisor.0, entry.shli(divisor.1, k128, location)?); let dividend = { let lhs_01 = entry.append_op_result(arith::ori(dividend.0, dividend.1, location))?; @@ -114,31 +110,15 @@ pub fn build_divmod_u256<'ctx, 'this>( let result_rem = entry.append_op_result(arith::remui(dividend, divisor, location))?; let result_div = ( - entry.append_op_result(arith::trunci(result_div, i128_ty, location))?, - entry.append_op_result(arith::trunci( - entry.append_op_result(arith::shrui(result_div, k128, location))?, - i128_ty, - location, - ))?, - entry.append_op_result(arith::trunci( - entry.append_op_result(arith::shrui(result_div, k256, location))?, - i128_ty, - location, - ))?, - entry.append_op_result(arith::trunci( - entry.append_op_result(arith::shrui(result_div, k384, location))?, - i128_ty, - location, - ))?, + entry.trunci(result_div, i128_ty, location)?, + entry.trunci(entry.shrui(result_div, k128, location)?, i128_ty, location)?, + entry.trunci(entry.shrui(result_div, k256, location)?, i128_ty, location)?, + entry.trunci(entry.shrui(result_div, k384, location)?, i128_ty, location)?, ); let result_rem = ( - entry.append_op_result(arith::trunci(result_rem, i128_ty, location))?, - entry.append_op_result(arith::trunci( - entry.append_op_result(arith::shrui(result_rem, k128, location))?, - i128_ty, - location, - ))?, + entry.trunci(result_rem, i128_ty, location)?, + entry.trunci(entry.shrui(result_rem, k128, location)?, i128_ty, location)?, ); let result_div_val = entry.append_op_result(llvm::undef( diff --git a/src/libfuncs/uint64.rs b/src/libfuncs/uint64.rs index fbe29f9a5..4d95445a5 100644 --- a/src/libfuncs/uint64.rs +++ b/src/libfuncs/uint64.rs @@ -108,10 +108,10 @@ pub fn build_operation<'ctx, 'this>( info: &IntOperationConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let op_name = match info.operator { IntOperator::OverflowingAdd => "llvm.intr.uadd.with.overflow", @@ -162,24 +162,12 @@ pub fn build_equal<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); - let arg1: Value = entry.argument(1)?.into(); + let arg0: Value = entry.arg(0)?; + let arg1: Value = entry.arg(1)?; - let op0 = entry.append_operation(arith::cmpi( - context, - CmpiPredicate::Eq, - arg0, - arg1, - location, - )); + let cond = entry.cmpi(context, CmpiPredicate::Eq, arg0, arg1, location)?; - entry.append_operation(helper.cond_br( - context, - op0.result(0)?.into(), - [1, 0], - [&[]; 2], - location, - )); + entry.append_operation(helper.cond_br(context, cond, [1, 0], [&[]; 2], location)); Ok(()) } @@ -193,17 +181,11 @@ pub fn build_is_zero<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); + let arg0: Value = entry.arg(0)?; let const_0 = entry.const_int_from_type(context, location, 0, arg0.r#type())?; - let condition = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - arg0, - const_0, - location, - ))?; + let condition = entry.cmpi(context, CmpiPredicate::Eq, arg0, const_0, location)?; entry.append_operation(helper.cond_br(context, condition, [0, 1], [&[], &[arg0]], location)); @@ -219,11 +201,10 @@ pub fn build_divmod<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let result_div = entry.append_op_result(arith::divui(lhs, rhs, location))?; let result_rem = entry.append_op_result(arith::remui(lhs, rhs, location))?; @@ -249,12 +230,12 @@ pub fn build_widemul<'ctx, 'this>( metadata, &info.output_types()[0][0], )?; - let lhs: Value = entry.argument(0)?.into(); - let rhs: Value = entry.argument(1)?.into(); + let lhs: Value = entry.arg(0)?; + let rhs: Value = entry.arg(1)?; - let lhs = entry.append_op_result(arith::extui(lhs, target_type, location))?; - let rhs = entry.append_op_result(arith::extui(rhs, target_type, location))?; - let result = entry.append_op_result(arith::muli(lhs, rhs, location))?; + let lhs = entry.extui(lhs, target_type, location)?; + let rhs = entry.extui(rhs, target_type, location)?; + let result = entry.muli(lhs, rhs, location)?; entry.append_operation(helper.br(0, &[result], location)); Ok(()) @@ -277,9 +258,9 @@ pub fn build_to_felt252<'ctx, 'this>( metadata, &info.branch_signatures()[0].vars[0].ty, )?; - let value: Value = entry.argument(0)?.into(); + let value: Value = entry.arg(0)?; - let result = entry.append_op_result(arith::extui(value, felt252_ty, location))?; + let result = entry.extui(value, felt252_ty, location)?; entry.append_operation(helper.br(0, &[result], location)); @@ -296,21 +277,14 @@ pub fn build_square_root<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let i32_ty = IntegerType::new(context, 32).into(); let i64_ty = IntegerType::new(context, 64).into(); let k1 = entry.const_int_from_type(context, location, 1, i64_ty)?; - let is_small = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ule, - entry.argument(1)?.into(), - k1, - location, - ))?; + let is_small = entry.cmpi(context, CmpiPredicate::Ule, entry.arg(1)?, k1, location)?; let result = entry.append_op_result(scf::r#if( is_small, @@ -319,7 +293,7 @@ pub fn build_square_root<'ctx, 'this>( let region = Region::new(); let block = region.append_block(Block::new(&[])); - block.append_operation(scf::r#yield(&[entry.argument(1)?.into()], location)); + block.append_operation(scf::r#yield(&[entry.arg(1)?], location)); region }, @@ -333,7 +307,7 @@ pub fn build_square_root<'ctx, 'this>( ods::llvm::intr_ctlz( context, i64_ty, - entry.argument(1)?.into(), + entry.arg(1)?, IntegerAttribute::new(IntegerType::new(context, 1).into(), 1), location, ) @@ -342,7 +316,7 @@ pub fn build_square_root<'ctx, 'this>( let num_bits = block.append_op_result(arith::subi(k64, leading_zeros, location))?; - let shift_amount = block.append_op_result(arith::addi(num_bits, k1, location))?; + let shift_amount = block.addi(num_bits, k1, location)?; let parity_mask = block.const_int_from_type(context, location, -2, i64_ty)?; let shift_amount = @@ -357,32 +331,16 @@ pub fn build_square_root<'ctx, 'this>( let block = region.append_block(Block::new(&[(i64_ty, location), (i64_ty, location)])); - let result = block.append_op_result(arith::shli( - block.argument(0)?.into(), - k1, - location, - ))?; + let result = block.shli(block.arg(0)?, k1, location)?; let large_candidate = block.append_op_result(arith::xori(result, k1, location))?; - let large_candidate_squared = block.append_op_result(arith::muli( - large_candidate, - large_candidate, - location, - ))?; + let large_candidate_squared = + block.muli(large_candidate, large_candidate, location)?; - let threshold = block.append_op_result(arith::shrui( - entry.argument(1)?.into(), - block.argument(1)?.into(), - location, - ))?; - let threshold_is_poison = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - block.argument(1)?.into(), - k64, - location, - ))?; + let threshold = block.shrui(entry.arg(1)?, block.arg(1)?, location)?; + let threshold_is_poison = + block.cmpi(context, CmpiPredicate::Eq, block.arg(1)?, k64, location)?; let threshold = block.append_op_result( OperationBuilder::new("arith.select", location) .add_operands(&[threshold_is_poison, k0, threshold]) @@ -390,13 +348,13 @@ pub fn build_square_root<'ctx, 'this>( .build()?, )?; - let is_in_range = block.append_op_result(arith::cmpi( + let is_in_range = block.cmpi( context, CmpiPredicate::Ule, large_candidate_squared, threshold, location, - ))?; + )?; let result = block.append_op_result( OperationBuilder::new("arith.select", location) @@ -407,19 +365,11 @@ pub fn build_square_root<'ctx, 'this>( let k2 = block.const_int_from_type(context, location, 2, i64_ty)?; - let shift_amount = block.append_op_result(arith::subi( - block.argument(1)?.into(), - k2, - location, - ))?; + let shift_amount = + block.append_op_result(arith::subi(block.arg(1)?, k2, location))?; - let should_continue = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - shift_amount, - k0, - location, - ))?; + let should_continue = + block.cmpi(context, CmpiPredicate::Sge, shift_amount, k0, location)?; block.append_operation(scf::condition( should_continue, &[result, shift_amount], @@ -433,10 +383,7 @@ pub fn build_square_root<'ctx, 'this>( let block = region.append_block(Block::new(&[(i64_ty, location), (i64_ty, location)])); - block.append_operation(scf::r#yield( - &[block.argument(0)?.into(), block.argument(1)?.into()], - location, - )); + block.append_operation(scf::r#yield(&[block.arg(0)?, block.arg(1)?], location)); region }, @@ -450,7 +397,7 @@ pub fn build_square_root<'ctx, 'this>( location, ))?; - let result = entry.append_op_result(arith::trunci(result, i32_ty, location))?; + let result = entry.trunci(result, i32_ty, location)?; entry.append_operation(helper.br(0, &[range_check, result], location)); Ok(()) @@ -467,9 +414,9 @@ pub fn build_from_felt252<'ctx, 'this>( info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let value: Value = entry.argument(1)?.into(); + let value: Value = entry.arg(1)?; let felt252_ty = registry.build_type( context, @@ -488,13 +435,7 @@ pub fn build_from_felt252<'ctx, 'this>( let const_max = entry.const_int_from_type(context, location, u64::MAX, felt252_ty)?; - let is_ule = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ule, - value, - const_max, - location, - ))?; + let is_ule = entry.cmpi(context, CmpiPredicate::Ule, value, const_max, location)?; let block_success = helper.append_block(Block::new(&[])); let block_failure = helper.append_block(Block::new(&[])); @@ -509,7 +450,7 @@ pub fn build_from_felt252<'ctx, 'this>( location, )); - let value = block_success.append_op_result(arith::trunci(value, result_ty, location))?; + let value = block_success.trunci(value, result_ty, location)?; block_success.append_operation(helper.br(0, &[range_check, value], location)); block_failure.append_operation(helper.br(1, &[range_check], location)); diff --git a/src/libfuncs/uint8.rs b/src/libfuncs/uint8.rs index 9b91ce2bd..eb0cd7fbe 100644 --- a/src/libfuncs/uint8.rs +++ b/src/libfuncs/uint8.rs @@ -108,10 +108,10 @@ pub fn build_operation<'ctx, 'this>( info: &IntOperationConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let op_name = match info.operator { IntOperator::OverflowingAdd => "llvm.intr.uadd.with.overflow", @@ -162,8 +162,8 @@ pub fn build_equal<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); - let arg1: Value = entry.argument(1)?.into(); + let arg0: Value = entry.arg(0)?; + let arg1: Value = entry.arg(1)?; let op0 = entry.append_operation(arith::cmpi( context, @@ -193,17 +193,11 @@ pub fn build_is_zero<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let arg0: Value = entry.argument(0)?.into(); + let arg0: Value = entry.arg(0)?; let const_0 = entry.const_int_from_type(context, location, 0, arg0.r#type())?; - let condition = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - arg0, - const_0, - location, - ))?; + let condition = entry.cmpi(context, CmpiPredicate::Eq, arg0, const_0, location)?; entry.append_operation(helper.cond_br(context, condition, [0, 1], [&[], &[arg0]], location)); @@ -219,11 +213,10 @@ pub fn build_divmod<'ctx, 'this>( helper: &LibfuncHelper<'ctx, 'this>, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let lhs: Value = entry.argument(1)?.into(); - let rhs: Value = entry.argument(2)?.into(); + let lhs: Value = entry.arg(1)?; + let rhs: Value = entry.arg(2)?; let result_div = entry.append_op_result(arith::divui(lhs, rhs, location))?; let result_rem = entry.append_op_result(arith::remui(lhs, rhs, location))?; @@ -249,12 +242,12 @@ pub fn build_widemul<'ctx, 'this>( metadata, &info.output_types()[0][0], )?; - let lhs: Value = entry.argument(0)?.into(); - let rhs: Value = entry.argument(1)?.into(); + let lhs: Value = entry.arg(0)?; + let rhs: Value = entry.arg(1)?; - let lhs = entry.append_op_result(arith::extui(lhs, target_type, location))?; - let rhs = entry.append_op_result(arith::extui(rhs, target_type, location))?; - let result = entry.append_op_result(arith::muli(lhs, rhs, location))?; + let lhs = entry.extui(lhs, target_type, location)?; + let rhs = entry.extui(rhs, target_type, location)?; + let result = entry.muli(lhs, rhs, location)?; entry.append_operation(helper.br(0, &[result], location)); Ok(()) @@ -277,9 +270,9 @@ pub fn build_to_felt252<'ctx, 'this>( metadata, &info.branch_signatures()[0].vars[0].ty, )?; - let value: Value = entry.argument(0)?.into(); + let value: Value = entry.arg(0)?; - let result = entry.append_op_result(arith::extui(value, felt252_ty, location))?; + let result = entry.extui(value, felt252_ty, location)?; entry.append_operation(helper.br(0, &[result], location)); @@ -296,20 +289,13 @@ pub fn build_square_root<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let range_check = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + let range_check = super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; let i8_ty = IntegerType::new(context, 8).into(); let k1 = entry.const_int_from_type(context, location, 1, i8_ty)?; - let is_small = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ule, - entry.argument(1)?.into(), - k1, - location, - ))?; + let is_small = entry.cmpi(context, CmpiPredicate::Ule, entry.arg(1)?, k1, location)?; let result = entry.append_op_result(scf::r#if( is_small, @@ -318,7 +304,7 @@ pub fn build_square_root<'ctx, 'this>( let region = Region::new(); let block = region.append_block(Block::new(&[])); - block.append_operation(scf::r#yield(&[entry.argument(1)?.into()], location)); + block.append_operation(scf::r#yield(&[entry.arg(1)?], location)); region }, @@ -332,7 +318,7 @@ pub fn build_square_root<'ctx, 'this>( ods::llvm::intr_ctlz( context, i8_ty, - entry.argument(1)?.into(), + entry.arg(1)?, IntegerAttribute::new(IntegerType::new(context, 1).into(), 1), location, ) @@ -341,7 +327,7 @@ pub fn build_square_root<'ctx, 'this>( let num_bits = block.append_op_result(arith::subi(k8, leading_zeros, location))?; - let shift_amount = block.append_op_result(arith::addi(num_bits, k1, location))?; + let shift_amount = block.addi(num_bits, k1, location)?; let parity_mask = block.const_int_from_type(context, location, -2, i8_ty)?; let shift_amount = @@ -356,32 +342,16 @@ pub fn build_square_root<'ctx, 'this>( let block = region.append_block(Block::new(&[(i8_ty, location), (i8_ty, location)])); - let result = block.append_op_result(arith::shli( - block.argument(0)?.into(), - k1, - location, - ))?; + let result = block.shli(block.arg(0)?, k1, location)?; let large_candidate = block.append_op_result(arith::xori(result, k1, location))?; - let large_candidate_squared = block.append_op_result(arith::muli( - large_candidate, - large_candidate, - location, - ))?; + let large_candidate_squared = + block.muli(large_candidate, large_candidate, location)?; - let threshold = block.append_op_result(arith::shrui( - entry.argument(1)?.into(), - block.argument(1)?.into(), - location, - ))?; - let threshold_is_poison = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - block.argument(1)?.into(), - k8, - location, - ))?; + let threshold = block.shrui(entry.arg(1)?, block.arg(1)?, location)?; + let threshold_is_poison = + block.cmpi(context, CmpiPredicate::Eq, block.arg(1)?, k8, location)?; let threshold = block.append_op_result( OperationBuilder::new("arith.select", location) .add_operands(&[threshold_is_poison, k0, threshold]) @@ -389,13 +359,13 @@ pub fn build_square_root<'ctx, 'this>( .build()?, )?; - let is_in_range = block.append_op_result(arith::cmpi( + let is_in_range = block.cmpi( context, CmpiPredicate::Ule, large_candidate_squared, threshold, location, - ))?; + )?; let result = block.append_op_result( OperationBuilder::new("arith.select", location) @@ -406,19 +376,11 @@ pub fn build_square_root<'ctx, 'this>( let k2 = block.const_int_from_type(context, location, 2, i8_ty)?; - let shift_amount = block.append_op_result(arith::subi( - block.argument(1)?.into(), - k2, - location, - ))?; + let shift_amount = + block.append_op_result(arith::subi(block.arg(1)?, k2, location))?; - let should_continue = block.append_op_result(arith::cmpi( - context, - CmpiPredicate::Sge, - shift_amount, - k0, - location, - ))?; + let should_continue = + block.cmpi(context, CmpiPredicate::Sge, shift_amount, k0, location)?; block.append_operation(scf::condition( should_continue, &[result, shift_amount], @@ -433,7 +395,7 @@ pub fn build_square_root<'ctx, 'this>( region.append_block(Block::new(&[(i8_ty, location), (i8_ty, location)])); block.append_operation(scf::r#yield( - &[block.argument(0)?.into(), block.argument(1)?.into()], + &[block.arg(0)?, block.argument(1)?.into()], location, )); @@ -464,9 +426,9 @@ pub fn build_from_felt252<'ctx, 'this>( info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { let range_check: Value = - super::increment_builtin_counter(context, entry, location, entry.argument(0)?.into())?; + super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?; - let value: Value = entry.argument(1)?.into(); + let value: Value = entry.arg(1)?; let felt252_ty = registry.build_type( context, @@ -485,13 +447,7 @@ pub fn build_from_felt252<'ctx, 'this>( let const_max = entry.const_int_from_type(context, location, u8::MAX, felt252_ty)?; - let is_ule = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Ule, - value, - const_max, - location, - ))?; + let is_ule = entry.cmpi(context, CmpiPredicate::Ule, value, const_max, location)?; let block_success = helper.append_block(Block::new(&[])); let block_failure = helper.append_block(Block::new(&[])); @@ -506,7 +462,7 @@ pub fn build_from_felt252<'ctx, 'this>( location, )); - let value = block_success.append_op_result(arith::trunci(value, result_ty, location))?; + let value = block_success.trunci(value, result_ty, location)?; block_success.append_operation(helper.br(0, &[range_check, value], location)); block_failure.append_operation(helper.br(1, &[range_check], location)); diff --git a/src/metadata/debug_utils.rs b/src/metadata/debug_utils.rs index 68cc54dac..6b9771171 100644 --- a/src/metadata/debug_utils.rs +++ b/src/metadata/debug_utils.rs @@ -44,7 +44,7 @@ //! info: &SignatureAndTypeConcreteLibfunc, //! ) -> Result<()> //! { -//! let array_val = entry.argument(0)?.into(); +//! let array_val = entry.arg(0)?; //! let elem_ty = registry.build_type(context, helper, registry, metadata, &info.ty)?; //! //! #[cfg(feature = "with-debug-utils")] diff --git a/src/types/array.rs b/src/types/array.rs index 7aa2e21f4..3a7fd0d3a 100644 --- a/src/types/array.rs +++ b/src/types/array.rs @@ -110,7 +110,7 @@ fn build_dup<'ctx>( let region = Region::new(); let entry = region.append_block(Block::new(&[(value_ty, location)])); - let src_value = entry.argument(0)?.into(); + let src_value = entry.arg(0)?; let value_ptr = entry.extract_value( context, location, @@ -136,13 +136,7 @@ fn build_dup<'ctx>( let value_len = entry.append_op_result(arith::subi(value_end, value_start, location))?; let k0 = entry.const_int(context, location, 0, 32)?; - let value_is_empty = entry.append_op_result(arith::cmpi( - context, - CmpiPredicate::Eq, - value_len, - k0, - location, - ))?; + let value_is_empty = entry.cmpi(context, CmpiPredicate::Eq, value_len, k0, location)?; let null_ptr = entry.append_op_result(llvm::zero(llvm::r#type::pointer(context, 0), location))?; @@ -164,13 +158,10 @@ fn build_dup<'ctx>( let elem_stride = block_realloc.const_int(context, location, elem_stride, 64)?; let dst_value_len = { - let value_len = block_realloc.append_op_result(arith::extui( - value_len, - IntegerType::new(context, 64).into(), - location, - ))?; + let value_len = + block_realloc.extui(value_len, IntegerType::new(context, 64).into(), location)?; - block_realloc.append_op_result(arith::muli(value_len, elem_stride, location))? + block_realloc.muli(value_len, elem_stride, location)? }; let dst_value_ptr = { block_realloc.append_op_result(ReallocBindingsMeta::realloc( @@ -182,14 +173,10 @@ fn build_dup<'ctx>( }; let src_value_ptr = { - let value_offset = block_realloc.append_op_result(arith::extui( - value_start, - IntegerType::new(context, 64).into(), - location, - ))?; + let value_offset = + block_realloc.extui(value_start, IntegerType::new(context, 64).into(), location)?; - let src_value_offset = - block_realloc.append_op_result(arith::muli(value_offset, elem_stride, location))?; + let src_value_offset = block_realloc.muli(value_offset, elem_stride, location)?; block_realloc.gep( context, location, @@ -213,7 +200,7 @@ fn build_dup<'ctx>( location, )])); - let idx = block.argument(0)?.into(); + let idx = block.arg(0)?; let src_value_ptr = block.gep( context, @@ -266,7 +253,7 @@ fn build_dup<'ctx>( context, location, dst_value, - &[block_finish.argument(0)?.into(), k0, value_len, value_len], + &[block_finish.arg(0)?, k0, value_len, value_len], )?; block_finish.append_operation(func::r#return(&[src_value, dst_value], location)); @@ -295,7 +282,7 @@ fn build_drop<'ctx>( let region = Region::new(); let entry = region.append_block(Block::new(&[(value_ty, location)])); - let src_value = entry.argument(0)?.into(); + let src_value = entry.arg(0)?; let value_ptr = entry.extract_value( context, location, @@ -321,22 +308,14 @@ fn build_drop<'ctx>( 2, )?; - let value_start = entry.append_op_result(arith::extui( - value_start, - IntegerType::new(context, 64).into(), - location, - ))?; - let value_end = entry.append_op_result(arith::extui( - value_end, - IntegerType::new(context, 64).into(), - location, - ))?; + let value_start = + entry.extui(value_start, IntegerType::new(context, 64).into(), location)?; + let value_end = + entry.extui(value_end, IntegerType::new(context, 64).into(), location)?; let elem_stride = entry.const_int(context, location, elem_stride, 64)?; - let offset_start = - entry.append_op_result(arith::muli(value_start, elem_stride, location))?; - let offset_end = - entry.append_op_result(arith::muli(value_end, elem_stride, location))?; + let offset_start = entry.muli(value_start, elem_stride, location)?; + let offset_end = entry.muli(value_end, elem_stride, location)?; entry.append_operation(scf::r#for( offset_start, @@ -349,7 +328,7 @@ fn build_drop<'ctx>( location, )])); - let elem_offset = block.argument(0)?.into(); + let elem_offset = block.arg(0)?; let elem_ptr = block.append_op_result(llvm::get_element_ptr_dynamic( context, value_ptr, diff --git a/src/types/box.rs b/src/types/box.rs index 17f994d08..35cdaa551 100644 --- a/src/types/box.rs +++ b/src/types/box.rs @@ -100,7 +100,7 @@ fn build_dup<'ctx>( entry.append_op_result(llvm::zero(llvm::r#type::pointer(context, 0), location))?; let inner_len_val = entry.const_int(context, location, inner_len, 64)?; - let src_value = entry.argument(0)?.into(); + let src_value = entry.arg(0)?; let dst_value = entry.append_op_result(ReallocBindingsMeta::realloc( context, null_ptr, @@ -152,7 +152,7 @@ fn build_drop<'ctx>( let region = Region::new(); let entry = region.append_block(Block::new(&[(llvm::r#type::pointer(context, 0), location)])); - let value = entry.argument(0)?.into(); + let value = entry.arg(0)?; match metadata.get::() { Some(drop_override_meta) if drop_override_meta.is_overriden(&info.ty) => { let value = entry.load(context, location, value, inner_ty)?; diff --git a/src/types/enum.rs b/src/types/enum.rs index 2d10070fe..27819c829 100644 --- a/src/types/enum.rs +++ b/src/types/enum.rs @@ -574,19 +574,13 @@ fn build_dup<'ctx>( let values = metadata .get::() .ok_or(Error::MissingMetadata)? - .invoke_override( - context, - &entry, - location, - &info.variants[0], - entry.argument(0)?.into(), - )?; + .invoke_override(context, &entry, location, &info.variants[0], entry.arg(0)?)?; entry.append_operation(func::r#return(&[values.0, values.1], location)); } _ => { let ptr = entry.alloca1(context, location, self_ty, layout.align())?; - entry.store(context, location, ptr, entry.argument(0)?.into())?; + entry.store(context, location, ptr, entry.arg(0)?)?; let mut variant_blocks = HashMap::new(); for (variant_id, variant_ty) in info @@ -678,19 +672,13 @@ fn build_drop<'ctx>( metadata .get::() .ok_or(Error::MissingMetadata)? - .invoke_override( - context, - &entry, - location, - &info.variants[0], - entry.argument(0)?.into(), - )?; + .invoke_override(context, &entry, location, &info.variants[0], entry.arg(0)?)?; entry.append_operation(func::r#return(&[], location)); } _ => { let ptr = entry.alloca1(context, location, self_ty, layout.align())?; - entry.store(context, location, ptr, entry.argument(0)?.into())?; + entry.store(context, location, ptr, entry.arg(0)?)?; let mut variant_blocks = HashMap::new(); for (variant_id, variant_ty) in info diff --git a/src/types/felt252_dict.rs b/src/types/felt252_dict.rs index e29df04db..66a75b3f5 100644 --- a/src/types/felt252_dict.rs +++ b/src/types/felt252_dict.rs @@ -102,7 +102,7 @@ fn build_dup<'ctx>( entry.append_op_result(llvm::zero(llvm::r#type::pointer(context, 0), location))?; let inner_len = entry.const_int(context, location, inner_len, 64)?; - let old_ptr = entry.argument(0)?.into(); + let old_ptr = entry.arg(0)?; let new_ptr = entry.append_op_result(ReallocBindingsMeta::realloc( context, null_ptr, inner_len, location, )?)?; @@ -155,7 +155,7 @@ fn build_dup<'ctx>( )?; // The following unwrap is unreachable because the registration logic will always insert it. - let value0 = entry.argument(0)?.into(); + let value0 = entry.arg(0)?; let value1 = metadata .get_mut::() .ok_or(Error::MissingMetadata)? @@ -186,7 +186,7 @@ fn build_drop<'ctx>( let entry = region.append_block(Block::new(&[(llvm::r#type::pointer(context, 0), location)])); - let value = entry.load(context, location, entry.argument(0)?.into(), inner_ty)?; + let value = entry.load(context, location, entry.arg(0)?, inner_ty)?; drop_overrides_meta.invoke_override(context, &entry, location, &info.ty, value)?; entry.append_operation(llvm::r#return(None, location)); @@ -242,14 +242,7 @@ fn build_drop<'ctx>( let runtime_bindings_meta = metadata .get_mut::() .ok_or(Error::MissingMetadata)?; - runtime_bindings_meta.dict_drop( - context, - module, - &entry, - entry.argument(0)?.into(), - drop_fn, - location, - )?; + runtime_bindings_meta.dict_drop(context, module, &entry, entry.arg(0)?, drop_fn, location)?; entry.append_operation(func::r#return(&[], location)); Ok(region) diff --git a/src/types/nullable.rs b/src/types/nullable.rs index beec95559..47dc93de5 100644 --- a/src/types/nullable.rs +++ b/src/types/nullable.rs @@ -97,7 +97,7 @@ fn build_dup<'ctx>( entry.append_op_result(llvm::zero(llvm::r#type::pointer(context, 0), location))?; let inner_len_val = entry.const_int(context, location, inner_len, 64)?; - let src_value = entry.argument(0)?.into(); + let src_value = entry.arg(0)?; let src_is_null = entry.append_op_result( ods::llvm::icmp( context, @@ -165,10 +165,7 @@ fn build_dup<'ctx>( block_realloc.append_operation(cf::br(&block_finish, &[dst_value], location)); } - block_finish.append_operation(func::r#return( - &[src_value, block_finish.argument(0)?.into()], - location, - )); + block_finish.append_operation(func::r#return(&[src_value, block_finish.arg(0)?], location)); Ok(region) } @@ -192,7 +189,7 @@ fn build_drop<'ctx>( let null_ptr = entry.append_op_result(llvm::zero(llvm::r#type::pointer(context, 0), location))?; - let value = entry.argument(0)?.into(); + let value = entry.arg(0)?; let is_null = entry.append_op_result( ods::llvm::icmp( context, diff --git a/src/types/snapshot.rs b/src/types/snapshot.rs index 644b006d5..46f9b9b7a 100644 --- a/src/types/snapshot.rs +++ b/src/types/snapshot.rs @@ -12,7 +12,7 @@ //! pub struct Snapshot(pub T); //! ``` -use super::{TypeBuilder, WithSelf}; +use super::{BlockExt, TypeBuilder, WithSelf}; use crate::{ error::{Error, Result}, metadata::{ @@ -117,13 +117,7 @@ fn build_dup<'ctx>( let values = metadata .get::() .ok_or(Error::MissingMetadata)? - .invoke_override( - context, - &entry, - location, - &info.ty, - entry.argument(0)?.into(), - )?; + .invoke_override(context, &entry, location, &info.ty, entry.arg(0)?)?; entry.append_operation(func::r#return(&[values.0, values.1], location)); Ok(region) @@ -147,13 +141,7 @@ fn build_drop<'ctx>( metadata .get::() .ok_or(Error::MissingMetadata)? - .invoke_override( - context, - &entry, - location, - &info.ty, - entry.argument(0)?.into(), - )?; + .invoke_override(context, &entry, location, &info.ty, entry.arg(0)?)?; entry.append_operation(func::r#return(&[], location)); Ok(region) diff --git a/src/types/starknet.rs b/src/types/starknet.rs index 032a9f004..90ceab278 100644 --- a/src/types/starknet.rs +++ b/src/types/starknet.rs @@ -221,7 +221,7 @@ pub fn build_sha256_state_handle<'ctx>( ods::llvm::intr_memcpy_inline( context, new_ptr, - block.argument(0)?.into(), + block.arg(0)?, IntegerAttribute::new(IntegerType::new(context, 64).into(), 32), IntegerAttribute::new(IntegerType::new(context, 1).into(), 0), location, @@ -229,10 +229,7 @@ pub fn build_sha256_state_handle<'ctx>( .into(), ); - block.append_operation(func::r#return( - &[block.argument(0)?.into(), new_ptr], - location, - )); + block.append_operation(func::r#return(&[block.arg(0)?, new_ptr], location)); Ok(Some(region)) })?; DropOverridesMeta::register_with(context, module, registry, metadata, info.self_ty(), |_| { @@ -240,11 +237,7 @@ pub fn build_sha256_state_handle<'ctx>( let block = region.append_block(Block::new(&[(llvm::r#type::pointer(context, 0), location)])); - block.append_operation(ReallocBindingsMeta::free( - context, - block.argument(0)?.into(), - location, - )?); + block.append_operation(ReallocBindingsMeta::free(context, block.arg(0)?, location)?); block.append_operation(func::r#return(&[], location)); Ok(Some(region)) diff --git a/src/types/struct.rs b/src/types/struct.rs index 1b89d9dd1..0f30d1a7c 100644 --- a/src/types/struct.rs +++ b/src/types/struct.rs @@ -140,7 +140,7 @@ fn build_dup<'ctx>( let region = Region::new(); let entry = region.append_block(Block::new(&[(self_ty, location)])); - let mut src_value = entry.argument(0)?.into(); + let mut src_value = entry.arg(0)?; let mut dst_value = entry.append_op_result(llvm::undef(self_ty, location))?; for (idx, member_id) in info.members.iter().enumerate() { @@ -175,7 +175,7 @@ fn build_drop<'ctx>( let region = Region::new(); let entry = region.append_block(Block::new(&[(self_ty, location)])); - let value = entry.argument(0)?.into(); + let value = entry.arg(0)?; for (idx, member_id) in info.members.iter().enumerate() { let member_ty = registry.build_type(context, module, registry, metadata, member_id)?; let member_val = entry.extract_value(context, location, value, member_ty, idx)?; diff --git a/src/utils/block_ext.rs b/src/utils/block_ext.rs index cecced8b3..d6f7b1b48 100644 --- a/src/utils/block_ext.rs +++ b/src/utils/block_ext.rs @@ -2,7 +2,11 @@ use crate::{error::Error, utils::get_integer_layout}; use melior::{ - dialect::{llvm::r#type::pointer, ods}, + dialect::{ + arith::{self, CmpiPredicate}, + llvm::r#type::pointer, + ods, + }, ir::{ attribute::{ DenseI32ArrayAttribute, DenseI64ArrayAttribute, IntegerAttribute, TypeAttribute, @@ -24,6 +28,67 @@ pub enum GepIndex<'c, 'a> { } pub trait BlockExt<'ctx> { + fn arg(&self, idx: usize) -> Result, Error>; + + /// Creates an arith.cmpi operation. + fn cmpi( + &self, + context: &'ctx Context, + pred: CmpiPredicate, + lhs: Value<'ctx, '_>, + rhs: Value<'ctx, '_>, + location: Location<'ctx>, + ) -> Result, Error>; + + fn extui( + &self, + lhs: Value<'ctx, '_>, + target_type: Type<'ctx>, + location: Location<'ctx>, + ) -> Result, Error>; + + fn extsi( + &self, + lhs: Value<'ctx, '_>, + target_type: Type<'ctx>, + location: Location<'ctx>, + ) -> Result, Error>; + + fn trunci( + &self, + lhs: Value<'ctx, '_>, + target_type: Type<'ctx>, + location: Location<'ctx>, + ) -> Result, Error>; + + fn shrui( + &self, + lhs: Value<'ctx, '_>, + rhs: Value<'ctx, '_>, + location: Location<'ctx>, + ) -> Result, Error>; + + fn shli( + &self, + lhs: Value<'ctx, '_>, + rhs: Value<'ctx, '_>, + location: Location<'ctx>, + ) -> Result, Error>; + + fn addi( + &self, + lhs: Value<'ctx, '_>, + rhs: Value<'ctx, '_>, + location: Location<'ctx>, + ) -> Result, Error>; + + fn muli( + &self, + lhs: Value<'ctx, '_>, + rhs: Value<'ctx, '_>, + location: Location<'ctx>, + ) -> Result, Error>; + /// Appends the operation and returns the first result. fn append_op_result(&self, operation: Operation<'ctx>) -> Result, Error>; @@ -165,6 +230,93 @@ pub trait BlockExt<'ctx> { } impl<'ctx> BlockExt<'ctx> for Block<'ctx> { + #[inline] + fn arg(&self, idx: usize) -> Result, Error> { + Ok(self.argument(idx)?.into()) + } + + #[inline] + fn cmpi( + &self, + context: &'ctx Context, + pred: CmpiPredicate, + lhs: Value<'ctx, '_>, + rhs: Value<'ctx, '_>, + location: Location<'ctx>, + ) -> Result, Error> { + self.append_op_result(arith::cmpi(context, pred, lhs, rhs, location)) + } + + #[inline] + fn extsi( + &self, + lhs: Value<'ctx, '_>, + target_type: Type<'ctx>, + location: Location<'ctx>, + ) -> Result, Error> { + self.append_op_result(arith::extsi(lhs, target_type, location)) + } + + #[inline] + fn extui( + &self, + lhs: Value<'ctx, '_>, + target_type: Type<'ctx>, + location: Location<'ctx>, + ) -> Result, Error> { + self.append_op_result(arith::extui(lhs, target_type, location)) + } + + #[inline] + fn trunci( + &self, + lhs: Value<'ctx, '_>, + target_type: Type<'ctx>, + location: Location<'ctx>, + ) -> Result, Error> { + self.append_op_result(arith::trunci(lhs, target_type, location)) + } + + #[inline] + fn shli( + &self, + lhs: Value<'ctx, '_>, + rhs: Value<'ctx, '_>, + location: Location<'ctx>, + ) -> Result, Error> { + self.append_op_result(arith::shli(lhs, rhs, location)) + } + + #[inline] + fn shrui( + &self, + lhs: Value<'ctx, '_>, + rhs: Value<'ctx, '_>, + location: Location<'ctx>, + ) -> Result, Error> { + self.append_op_result(arith::shrui(lhs, rhs, location)) + } + + #[inline] + fn addi( + &self, + lhs: Value<'ctx, '_>, + rhs: Value<'ctx, '_>, + location: Location<'ctx>, + ) -> Result, Error> { + self.append_op_result(arith::addi(lhs, rhs, location)) + } + + #[inline] + fn muli( + &self, + lhs: Value<'ctx, '_>, + rhs: Value<'ctx, '_>, + location: Location<'ctx>, + ) -> Result, Error> { + self.append_op_result(arith::muli(lhs, rhs, location)) + } + #[inline] fn const_int( &self,