diff --git a/src/compiler.rs b/src/compiler.rs index c72a7e349..aadf5314e 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -538,6 +538,7 @@ fn compile_func( context, registry, module, + &pre_entry_block, block, location, metadata, @@ -724,6 +725,7 @@ fn compile_func( context, registry, module, + &pre_entry_block, block, location, metadata, diff --git a/src/libfuncs/felt252_dict.rs b/src/libfuncs/felt252_dict.rs index 3b8a258af..02e0a2b29 100644 --- a/src/libfuncs/felt252_dict.rs +++ b/src/libfuncs/felt252_dict.rs @@ -109,9 +109,14 @@ pub fn build_squash<'ctx, 'this>( .to_native_assert_error("runtime library should be available")?; let range_check_ptr = - entry.alloca1(context, location, IntegerType::new(context, 64).into(), 0)?; + helper + .init_block() + .alloca1(context, location, IntegerType::new(context, 64).into(), 0)?; entry.store(context, location, range_check_ptr, range_check)?; - let gas_ptr = entry.alloca1(context, location, IntegerType::new(context, 64).into(), 0)?; + let gas_ptr = + helper + .init_block() + .alloca1(context, location, IntegerType::new(context, 64).into(), 0)?; entry.store(context, location, gas_ptr, gas)?; runtime_bindings.dict_squash( diff --git a/src/libfuncs/qm31.rs b/src/libfuncs/qm31.rs index 81a92e458..dedc08be3 100644 --- a/src/libfuncs/qm31.rs +++ b/src/libfuncs/qm31.rs @@ -433,8 +433,14 @@ pub fn build_binary_op<'ctx, 'this>( let lhs = entry.arg(0)?; let rhs = entry.arg(1)?; - let lhs_ptr = entry.alloca1(context, location, qm31_ty, get_integer_layout(31).align())?; - let rhs_ptr = entry.alloca1(context, location, qm31_ty, get_integer_layout(31).align())?; + let lhs_ptr = + helper + .init_block() + .alloca1(context, location, qm31_ty, get_integer_layout(31).align())?; + let rhs_ptr = + helper + .init_block() + .alloca1(context, location, qm31_ty, get_integer_layout(31).align())?; entry.store(context, location, lhs_ptr, lhs)?; entry.store(context, location, rhs_ptr, rhs)?; diff --git a/src/libfuncs/squashed_dict.rs b/src/libfuncs/squashed_dict.rs index 7bffff573..799c75d33 100644 --- a/src/libfuncs/squashed_dict.rs +++ b/src/libfuncs/squashed_dict.rs @@ -78,7 +78,10 @@ pub fn build_into_entries<'ctx, 'this>( &info.branch_signatures()[0].vars[0].ty, )?; // Alloc the necessary memory - let array_ptr = entry.alloca1(context, location, array_ty, array_layout.align())?; + let array_ptr = + helper + .init_block() + .alloca1(context, location, array_ty, array_layout.align())?; // Runtime function that creates the array with its content metadata diff --git a/src/metadata/runtime_bindings.rs b/src/metadata/runtime_bindings.rs index 742fe9780..10e030b8d 100644 --- a/src/metadata/runtime_bindings.rs +++ b/src/metadata/runtime_bindings.rs @@ -730,7 +730,7 @@ impl RuntimeBindingsMeta { pub fn libfunc_qm31_bin_op<'c, 'a>( &mut self, context: &'c Context, - module: &Module, + helper: &LibfuncHelper<'c, 'a>, block: &'a Block<'c>, lhs_ptr: Value<'c, '_>, rhs_ptr: Value<'c, '_>, @@ -741,20 +741,25 @@ impl RuntimeBindingsMeta { 'c: 'a, { let qm31_ty = llvm::r#type::array(IntegerType::new(context, 31).into(), 4); - let res_ptr = block.alloca1(context, location, qm31_ty, get_integer_layout(31).align())?; + let res_ptr = helper.init_block().alloca1( + context, + location, + qm31_ty, + get_integer_layout(31).align(), + )?; let function = match op { QM31BinaryOperator::Add => { - self.build_function(context, module, block, location, RuntimeBinding::QM31Add)? + self.build_function(context, helper, block, location, RuntimeBinding::QM31Add)? } QM31BinaryOperator::Sub => { - self.build_function(context, module, block, location, RuntimeBinding::QM31Sub)? + self.build_function(context, helper, block, location, RuntimeBinding::QM31Sub)? } QM31BinaryOperator::Mul => { - self.build_function(context, module, block, location, RuntimeBinding::QM31Mul)? + self.build_function(context, helper, block, location, RuntimeBinding::QM31Mul)? } QM31BinaryOperator::Div => { - self.build_function(context, module, block, location, RuntimeBinding::QM31Div)? + self.build_function(context, helper, block, location, RuntimeBinding::QM31Div)? } }; diff --git a/src/utils/trace_dump.rs b/src/utils/trace_dump.rs index 2b39f0ff0..f349a0cc2 100644 --- a/src/utils/trace_dump.rs +++ b/src/utils/trace_dump.rs @@ -74,7 +74,7 @@ use cairo_lang_sierra::{ use cairo_lang_utils::ordered_hash_map::OrderedHashMap; use melior::{ helpers::LlvmBlockExt, - ir::{BlockRef, Location, Module, Value, ValueLike}, + ir::{Block, BlockRef, Location, Module, Value, ValueLike}, Context, }; @@ -88,6 +88,7 @@ pub fn build_state_snapshot( context: &Context, registry: &ProgramRegistry, module: &Module, + init_block: &Block, block: &BlockRef, location: Location, metadata: &mut MetadataStorage, @@ -103,7 +104,9 @@ pub fn build_state_snapshot( let layout = value_type.layout(registry).unwrap(); - let value_ptr = block + // Allocas must live in the init block so that they are not executed on every loop + // iteration, which would grow the stack unboundedly. + let value_ptr = init_block .alloca1(context, location, value.r#type(), layout.align()) .unwrap(); block.store(context, location, value_ptr, *value).unwrap();