Skip to content

Commit cb86e70

Browse files
committed
fix zkDSL compiler crash (edge case when the end value of the non-unrolled loop index is defined by a const malloc)
1 parent 3ceaf1b commit cb86e70

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

crates/lean_compiler/src/a_simplify_lang.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,20 @@ fn simplify_lines(
814814

815815
let start_simplified =
816816
simplify_expr(start, &mut res, counters, array_manager, const_malloc, const_arrays);
817-
let end_simplified = simplify_expr(end, &mut res, counters, array_manager, const_malloc, const_arrays);
817+
let mut end_simplified =
818+
simplify_expr(end, &mut res, counters, array_manager, const_malloc, const_arrays);
819+
if let SimpleExpr::ConstMallocAccess { malloc_label, offset } = end_simplified.clone() {
820+
// we use an auxilary variable to store the end value (const malloc inside non-unrolled loops does not work)
821+
let aux_end_var = format!("@aux_end_{}", counters.aux_vars);
822+
counters.aux_vars += 1;
823+
res.push(SimpleLine::Assignment {
824+
var: aux_end_var.clone().into(),
825+
operation: HighLevelOperation::Add,
826+
arg0: SimpleExpr::ConstMallocAccess { malloc_label, offset },
827+
arg1: SimpleExpr::zero(),
828+
});
829+
end_simplified = SimpleExpr::Var(aux_end_var);
830+
}
818831

819832
for (simplified, original) in [
820833
(start_simplified.clone(), start.clone()),

crates/lean_compiler/tests/test_compiler.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,3 +766,21 @@ fn test_const_array() {
766766
"#;
767767
compile_and_run(program.to_string(), (&[], &[]), DEFAULT_NO_VEC_RUNTIME_MEMORY, false);
768768
}
769+
770+
#[test]
771+
fn test_const_malloc_end_iterator_loop() {
772+
let program = r#"
773+
fn main() {
774+
x = malloc(2);
775+
x[0] = 3;
776+
x[1] = 5;
777+
for i in 0..2 unroll {
778+
for j in 0..x[i] {
779+
print(i, j);
780+
}
781+
}
782+
return;
783+
}
784+
"#;
785+
compile_and_run(program.to_string(), (&[], &[]), DEFAULT_NO_VEC_RUNTIME_MEMORY, false);
786+
}

0 commit comments

Comments
 (0)