Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow empty loop headers #6736

Merged
merged 8 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/unrolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@
pub(super) fn get_const_upper_bound(&self, function: &Function) -> Option<FieldElement> {
let block = &function.dfg[self.header];
let instructions = block.instructions();
if instructions.is_empty() {
// If the loop condition is constant time, the loop header will be
// simplified to a simple jump.
aakoshh marked this conversation as resolved.
Show resolved Hide resolved
return None;
}
assert_eq!(
instructions.len(),
1,
Expand Down Expand Up @@ -1023,7 +1028,7 @@
use super::{is_new_size_ok, BoilerplateStats, Loops};

/// Tries to unroll all loops in each SSA function once, calling the `Function` directly,
/// bypassing the iterative loop done by the SSA which does further optimisations.

Check warning on line 1031 in compiler/noirc_evaluator/src/ssa/opt/unrolling.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (optimisations)
///
/// If any loop cannot be unrolled, it is left as-is or in a partially unrolled state.
fn try_unroll_loops(mut ssa: Ssa) -> (Ssa, Vec<RuntimeError>) {
Expand Down
27 changes: 27 additions & 0 deletions test_programs/execution_success/regression/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,31 @@ fn main(x: [u8; 5], z: Field, u: i16, v: i16) {
assert(-u % -11 == -4);
assert(u % -11 == u % (v + 2));
assert(-u % -11 == -u % (v + 2));

// Issue 6736
jfecher marked this conversation as resolved.
Show resolved Hide resolved
unsafe { sub_array_extended_into_empty(); }
}


pub fn sub_array_extended<let SRC_LEN: u32, let DST_LEN: u32>(
src: [Field; SRC_LEN],
offset: u32,
) -> [Field; DST_LEN] {
let available_elements_to_copy = SRC_LEN - offset;
let elements_to_copy = if DST_LEN > available_elements_to_copy {
available_elements_to_copy
} else {
DST_LEN
};

let mut dst: [Field; DST_LEN] = std::mem::zeroed();
for i in 0..elements_to_copy {
dst[i] = src[i + offset];
}

dst
}

unconstrained fn sub_array_extended_into_empty() {
assert_eq(sub_array_extended([], 0), []);
}
Loading