diff --git a/src/libfuncs/starknet/secp256.rs b/src/libfuncs/starknet/secp256.rs index 28ec86dc6..d13560523 100644 --- a/src/libfuncs/starknet/secp256.rs +++ b/src/libfuncs/starknet/secp256.rs @@ -15,15 +15,10 @@ use cairo_lang_sierra::{ program_registry::ProgramRegistry, }; use melior::{ - dialect::{ - arith, - llvm::{self, LoadStoreOptions}, - }, + dialect::llvm::{self, LoadStoreOptions}, ir::{ - attribute::{DenseI32ArrayAttribute, IntegerAttribute, TypeAttribute}, - operation::OperationBuilder, - r#type::IntegerType, - Block, Identifier, Location, + attribute::DenseI32ArrayAttribute, operation::OperationBuilder, r#type::IntegerType, Block, + Location, }, Context, }; @@ -323,50 +318,22 @@ pub fn build_k1_add<'ctx, 'this>( ], )?; - let k1 = helper - .init_block() - .append_operation(arith::constant( + let result_ptr = helper.init_block().alloca1( + context, + location, + llvm::r#type::r#struct( context, - IntegerAttribute::new(IntegerType::new(context, 64).into(), 1).into(), - location, - )) - .result(0)? - .into(); - let result_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[ - ( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - ), - ( - Identifier::new(context, "elem_type"), - TypeAttribute::new(llvm::r#type::r#struct( - context, - &[ - result_tag_ty, - llvm::r#type::array( - IntegerType::new(context, 8).into(), - (result_layout.size() - 1).try_into()?, - ), - ], - false, - )) - .into(), - ), - ]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(context, 0)]) - .build()?, - ) - .result(0)? - .into(); + &[ + result_tag_ty, + llvm::r#type::array( + IntegerType::new(context, 8).into(), + (result_layout.size() - 1).try_into()?, + ), + ], + false, + ), + result_layout.align(), + )?; // Allocate space and write the current gas. let gas_builtin_ptr = helper.init_block().alloca1( @@ -564,50 +531,22 @@ pub fn build_k1_mul<'ctx, 'this>( ], )?; - let k1 = helper - .init_block() - .append_operation(arith::constant( + let result_ptr = helper.init_block().alloca1( + context, + location, + llvm::r#type::r#struct( context, - IntegerAttribute::new(IntegerType::new(context, 64).into(), 1).into(), - location, - )) - .result(0)? - .into(); - let result_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[ - ( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - ), - ( - Identifier::new(context, "elem_type"), - TypeAttribute::new(llvm::r#type::r#struct( - context, - &[ - result_tag_ty, - llvm::r#type::array( - IntegerType::new(context, 8).into(), - (result_layout.size() - 1).try_into()?, - ), - ], - false, - )) - .into(), - ), - ]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(context, 0)]) - .build()?, - ) - .result(0)? - .into(); + &[ + result_tag_ty, + llvm::r#type::array( + IntegerType::new(context, 8).into(), + (result_layout.size() - 1).try_into()?, + ), + ], + false, + ), + result_layout.align(), + )?; // Allocate space and write the current gas. let gas_builtin_ptr = helper.init_block().alloca1( @@ -805,50 +744,22 @@ pub fn build_k1_get_point_from_x<'ctx, 'this>( ], )?; - let k1 = helper - .init_block() - .append_operation(arith::constant( + let result_ptr = helper.init_block().alloca1( + context, + location, + llvm::r#type::r#struct( context, - IntegerAttribute::new(IntegerType::new(context, 64).into(), 1).into(), - location, - )) - .result(0)? - .into(); - let result_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[ - ( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - ), - ( - Identifier::new(context, "elem_type"), - TypeAttribute::new(llvm::r#type::r#struct( - context, - &[ - result_tag_ty, - llvm::r#type::array( - IntegerType::new(context, 8).into(), - (result_layout.size() - 1).try_into()?, - ), - ], - false, - )) - .into(), - ), - ]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(context, 0)]) - .build()?, - ) - .result(0)? - .into(); + &[ + result_tag_ty, + llvm::r#type::array( + IntegerType::new(context, 8).into(), + (result_layout.size() - 1).try_into()?, + ), + ], + false, + ), + result_layout.align(), + )?; // Allocate space and write the current gas. let gas_builtin_ptr = helper.init_block().alloca1( @@ -1067,50 +978,22 @@ pub fn build_k1_get_xy<'ctx, 'this>( ) }; - let k1 = helper - .init_block() - .append_operation(arith::constant( + let result_ptr = helper.init_block().alloca1( + context, + location, + llvm::r#type::r#struct( context, - IntegerAttribute::new(IntegerType::new(context, 64).into(), 1).into(), - location, - )) - .result(0)? - .into(); - let result_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[ - ( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - ), - ( - Identifier::new(context, "elem_type"), - TypeAttribute::new(llvm::r#type::r#struct( - context, - &[ - result_tag_ty, - llvm::r#type::array( - IntegerType::new(context, 8).into(), - (result_layout.size() - 1).try_into()?, - ), - ], - false, - )) - .into(), - ), - ]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(context, 0)]) - .build()?, - ) - .result(0)? - .into(); + &[ + result_tag_ty, + llvm::r#type::array( + IntegerType::new(context, 8).into(), + (result_layout.size() - 1).try_into()?, + ), + ], + false, + ), + result_layout.align(), + )?; // Allocate space and write the current gas. let gas_builtin_ptr = helper.init_block().alloca1( @@ -1318,50 +1201,22 @@ pub fn build_r1_new<'ctx, 'this>( ], )?; - let k1 = helper - .init_block() - .append_operation(arith::constant( + let result_ptr = helper.init_block().alloca1( + context, + location, + llvm::r#type::r#struct( context, - IntegerAttribute::new(IntegerType::new(context, 64).into(), 1).into(), - location, - )) - .result(0)? - .into(); - let result_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[ - ( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - ), - ( - Identifier::new(context, "elem_type"), - TypeAttribute::new(llvm::r#type::r#struct( - context, - &[ - result_tag_ty, - llvm::r#type::array( - IntegerType::new(context, 8).into(), - (result_layout.size() - 1).try_into()?, - ), - ], - false, - )) - .into(), - ), - ]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(context, 0)]) - .build()?, - ) - .result(0)? - .into(); + &[ + result_tag_ty, + llvm::r#type::array( + IntegerType::new(context, 8).into(), + (result_layout.size() - 1).try_into()?, + ), + ], + false, + ), + result_layout.align(), + )?; // Allocate space and write the current gas. let gas_builtin_ptr = helper.init_block().alloca1( @@ -1559,50 +1414,22 @@ pub fn build_r1_add<'ctx, 'this>( ], )?; - let k1 = helper - .init_block() - .append_operation(arith::constant( + let result_ptr = helper.init_block().alloca1( + context, + location, + llvm::r#type::r#struct( context, - IntegerAttribute::new(IntegerType::new(context, 64).into(), 1).into(), - location, - )) - .result(0)? - .into(); - let result_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[ - ( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - ), - ( - Identifier::new(context, "elem_type"), - TypeAttribute::new(llvm::r#type::r#struct( - context, - &[ - result_tag_ty, - llvm::r#type::array( - IntegerType::new(context, 8).into(), - (result_layout.size() - 1).try_into()?, - ), - ], - false, - )) - .into(), - ), - ]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(context, 0)]) - .build()?, - ) - .result(0)? - .into(); + &[ + result_tag_ty, + llvm::r#type::array( + IntegerType::new(context, 8).into(), + (result_layout.size() - 1).try_into()?, + ), + ], + false, + ), + result_layout.align(), + )?; // Allocate space and write the current gas. let gas_builtin_ptr = helper.init_block().alloca1( @@ -1800,50 +1627,22 @@ pub fn build_r1_mul<'ctx, 'this>( ], )?; - let k1 = helper - .init_block() - .append_operation(arith::constant( + let result_ptr = helper.init_block().alloca1( + context, + location, + llvm::r#type::r#struct( context, - IntegerAttribute::new(IntegerType::new(context, 64).into(), 1).into(), - location, - )) - .result(0)? - .into(); - let result_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[ - ( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - ), - ( - Identifier::new(context, "elem_type"), - TypeAttribute::new(llvm::r#type::r#struct( - context, - &[ - result_tag_ty, - llvm::r#type::array( - IntegerType::new(context, 8).into(), - (result_layout.size() - 1).try_into()?, - ), - ], - false, - )) - .into(), - ), - ]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(context, 0)]) - .build()?, - ) - .result(0)? - .into(); + &[ + result_tag_ty, + llvm::r#type::array( + IntegerType::new(context, 8).into(), + (result_layout.size() - 1).try_into()?, + ), + ], + false, + ), + result_layout.align(), + )?; // Allocate space and write the current gas. let gas_builtin_ptr = helper.init_block().alloca1( @@ -2044,50 +1843,22 @@ pub fn build_r1_get_point_from_x<'ctx, 'this>( ], )?; - let k1 = helper - .init_block() - .append_operation(arith::constant( + let result_ptr = helper.init_block().alloca1( + context, + location, + llvm::r#type::r#struct( context, - IntegerAttribute::new(IntegerType::new(context, 64).into(), 1).into(), - location, - )) - .result(0)? - .into(); - let result_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[ - ( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - ), - ( - Identifier::new(context, "elem_type"), - TypeAttribute::new(llvm::r#type::r#struct( - context, - &[ - result_tag_ty, - llvm::r#type::array( - IntegerType::new(context, 8).into(), - (result_layout.size() - 1).try_into()?, - ), - ], - false, - )) - .into(), - ), - ]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(context, 0)]) - .build()?, - ) - .result(0)? - .into(); + &[ + result_tag_ty, + llvm::r#type::array( + IntegerType::new(context, 8).into(), + (result_layout.size() - 1).try_into()?, + ), + ], + false, + ), + result_layout.align(), + )?; // Allocate space and write the current gas. let gas_builtin_ptr = helper.init_block().alloca1( @@ -2307,50 +2078,22 @@ pub fn build_r1_get_xy<'ctx, 'this>( ) }; - let k1 = helper - .init_block() - .append_operation(arith::constant( + let result_ptr = helper.init_block().alloca1( + context, + location, + llvm::r#type::r#struct( context, - IntegerAttribute::new(IntegerType::new(context, 64).into(), 1).into(), - location, - )) - .result(0)? - .into(); - let result_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[ - ( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - ), - ( - Identifier::new(context, "elem_type"), - TypeAttribute::new(llvm::r#type::r#struct( - context, - &[ - result_tag_ty, - llvm::r#type::array( - IntegerType::new(context, 8).into(), - (result_layout.size() - 1).try_into()?, - ), - ], - false, - )) - .into(), - ), - ]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(context, 0)]) - .build()?, - ) - .result(0)? - .into(); + &[ + result_tag_ty, + llvm::r#type::array( + IntegerType::new(context, 8).into(), + (result_layout.size() - 1).try_into()?, + ), + ], + false, + ), + result_layout.align(), + )?; // Allocate space and write the current gas. let gas_builtin_ptr = helper.init_block().alloca1(