From afa1cb24e6a144f52ebdeebd4f1a26803698eab3 Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Fri, 17 Jan 2025 10:26:59 +0000 Subject: [PATCH 1/3] Update test with simplified return --- compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs b/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs index 1dd5fcd6b0..d7fb52264d 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs @@ -440,9 +440,7 @@ mod tests { b1(): constrain v0 == Field 3 v8 = call f3(v1) -> u32 - jmp b2(v8) - b2(v2: u32): - jmp b4(v2) + jmp b4(v8) b3(): v10 = call f2(v1) -> u32 jmp b4(v10) From bddb7de22a66063715a7d5c7f2bda51d4fed640b Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Fri, 17 Jan 2025 01:33:42 +0000 Subject: [PATCH 2/3] Refactor defunctionalization to not create intermediate blocks before return --- .../noirc_evaluator/src/ssa/opt/defunctionalize.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs b/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs index d7fb52264d..535ad7c291 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs @@ -343,22 +343,21 @@ fn create_apply_function( }) } -/// Crates a return block, if no previous return exists, it will create a final return -/// Else, it will create a bypass return block that points to the previous return block +/// If no previous return target exists, it will create a final return, +/// otherwise returns the existing return block to jump to. fn build_return_block( builder: &mut FunctionBuilder, previous_block: BasicBlockId, passed_types: &[Type], target: Option, ) -> BasicBlockId { + if let Some(return_block) = target { + return return_block; + } let return_block = builder.insert_block(); builder.switch_to_block(return_block); - let params = vecmap(passed_types, |typ| builder.add_block_parameter(return_block, typ.clone())); - match target { - None => builder.terminate_with_return(params), - Some(target) => builder.terminate_with_jmp(target, params), - } + builder.terminate_with_return(params); builder.switch_to_block(previous_block); return_block } From 4e3ea96602c9d9e70c96e19c56ee7c1290d8f12f Mon Sep 17 00:00:00 2001 From: Akosh Farkash Date: Fri, 17 Jan 2025 13:43:10 +0000 Subject: [PATCH 3/3] Update expected SSA --- .../src/ssa/opt/defunctionalize.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs b/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs index 535ad7c291..754e550af9 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs @@ -434,17 +434,17 @@ mod tests { } brillig(inline) fn apply f4 { b0(v0: Field, v1: u32): - v5 = eq v0, Field 2 - jmpif v5 then: b3, else: b1 + v4 = eq v0, Field 2 + jmpif v4 then: b2, else: b1 b1(): constrain v0 == Field 3 - v8 = call f3(v1) -> u32 - jmp b4(v8) - b3(): - v10 = call f2(v1) -> u32 - jmp b4(v10) - b4(v3: u32): - return v3 + v7 = call f3(v1) -> u32 + jmp b3(v7) + b2(): + v9 = call f2(v1) -> u32 + jmp b3(v9) + b3(v2: u32): + return v2 } "; assert_normalized_ssa_equals(ssa, expected);