From 8deea1b3d98cf16b772e42014b04bc0d83afddca Mon Sep 17 00:00:00 2001 From: guipublic Date: Mon, 9 Dec 2024 09:55:18 +0000 Subject: [PATCH 1/6] allow empty loop headers --- compiler/noirc_evaluator/src/ssa/opt/unrolling.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compiler/noirc_evaluator/src/ssa/opt/unrolling.rs b/compiler/noirc_evaluator/src/ssa/opt/unrolling.rs index 22daba1de45..806085743d4 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/unrolling.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/unrolling.rs @@ -302,6 +302,11 @@ impl Loop { pub(super) fn get_const_upper_bound(&self, function: &Function) -> Option { 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. + return None; + } assert_eq!( instructions.len(), 1, From 2b56f1067c1cd632c6efd6ef7ba6526e74674f9d Mon Sep 17 00:00:00 2001 From: guipublic Date: Mon, 9 Dec 2024 13:36:51 +0000 Subject: [PATCH 2/6] add regression test --- .../execution_success/regression/src/main.nr | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test_programs/execution_success/regression/src/main.nr b/test_programs/execution_success/regression/src/main.nr index 809fdbe4b28..b7dd54b241d 100644 --- a/test_programs/execution_success/regression/src/main.nr +++ b/test_programs/execution_success/regression/src/main.nr @@ -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 + unsafe { sub_array_extended_into_empty(); } +} + + +pub fn sub_array_extended( + 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), []); } From f3f65d5e7f32778c37dc4078f2cbb13a42824921 Mon Sep 17 00:00:00 2001 From: jfecher Date: Mon, 9 Dec 2024 08:03:31 -0600 Subject: [PATCH 3/6] Update test_programs/execution_success/regression/src/main.nr Co-authored-by: Akosh Farkash --- test_programs/execution_success/regression/src/main.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_programs/execution_success/regression/src/main.nr b/test_programs/execution_success/regression/src/main.nr index b7dd54b241d..a02b74ae5a5 100644 --- a/test_programs/execution_success/regression/src/main.nr +++ b/test_programs/execution_success/regression/src/main.nr @@ -139,7 +139,7 @@ fn main(x: [u8; 5], z: Field, u: i16, v: i16) { assert(u % -11 == u % (v + 2)); assert(-u % -11 == -u % (v + 2)); - // Issue 6736 + // Issue 6734 unsafe { sub_array_extended_into_empty(); } } From de23e6f3826c3dd5d162fd677fddcd35a1e93957 Mon Sep 17 00:00:00 2001 From: guipublic Date: Mon, 9 Dec 2024 14:25:57 +0000 Subject: [PATCH 4/6] format --- test_programs/execution_success/regression/src/main.nr | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test_programs/execution_success/regression/src/main.nr b/test_programs/execution_success/regression/src/main.nr index a02b74ae5a5..00f98735b25 100644 --- a/test_programs/execution_success/regression/src/main.nr +++ b/test_programs/execution_success/regression/src/main.nr @@ -140,10 +140,11 @@ fn main(x: [u8; 5], z: Field, u: i16, v: i16) { assert(-u % -11 == -u % (v + 2)); // Issue 6734 - unsafe { sub_array_extended_into_empty(); } + unsafe { + sub_array_extended_into_empty(); + } } - pub fn sub_array_extended( src: [Field; SRC_LEN], offset: u32, From 28ad3f2b48eca967de2076cf8af081771d2d9f9d Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 9 Dec 2024 16:52:15 +0000 Subject: [PATCH 5/6] . --- .../execution_success/regression/src/main.nr | 28 ------------------- .../regression_6734/Nargo.toml | 6 ++++ .../regression_6734/src/main.nr | 24 ++++++++++++++++ 3 files changed, 30 insertions(+), 28 deletions(-) create mode 100644 test_programs/execution_success/regression_6734/Nargo.toml create mode 100644 test_programs/execution_success/regression_6734/src/main.nr diff --git a/test_programs/execution_success/regression/src/main.nr b/test_programs/execution_success/regression/src/main.nr index 00f98735b25..809fdbe4b28 100644 --- a/test_programs/execution_success/regression/src/main.nr +++ b/test_programs/execution_success/regression/src/main.nr @@ -138,32 +138,4 @@ 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 6734 - unsafe { - sub_array_extended_into_empty(); - } -} - -pub fn sub_array_extended( - 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), []); } diff --git a/test_programs/execution_success/regression_6734/Nargo.toml b/test_programs/execution_success/regression_6734/Nargo.toml new file mode 100644 index 00000000000..347c08d2996 --- /dev/null +++ b/test_programs/execution_success/regression_6734/Nargo.toml @@ -0,0 +1,6 @@ +[package] +name = "regression_6736" +type = "bin" +authors = [""] + +[dependencies] diff --git a/test_programs/execution_success/regression_6734/src/main.nr b/test_programs/execution_success/regression_6734/src/main.nr new file mode 100644 index 00000000000..87cac64aaa1 --- /dev/null +++ b/test_programs/execution_success/regression_6734/src/main.nr @@ -0,0 +1,24 @@ +// https://github.com/noir-lang/noir/issues/6734 + +pub fn sub_array_extended( + 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 main() { + assert_eq(sub_array_extended([], 0), []); +} From 177a6589868bcc820cd3c67c13c29c35f088c684 Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 9 Dec 2024 17:06:30 +0000 Subject: [PATCH 6/6] . --- test_programs/execution_success/regression_6734/Nargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_programs/execution_success/regression_6734/Nargo.toml b/test_programs/execution_success/regression_6734/Nargo.toml index 347c08d2996..4c757bab454 100644 --- a/test_programs/execution_success/regression_6734/Nargo.toml +++ b/test_programs/execution_success/regression_6734/Nargo.toml @@ -1,5 +1,5 @@ [package] -name = "regression_6736" +name = "regression_6734" type = "bin" authors = [""]