From ba12cba6e5c8d31a5d5610f9f0768a6b89db7c73 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Sat, 12 Apr 2025 08:15:23 -0300 Subject: [PATCH] chore: parse IfElse in SSA parser --- .../noirc_evaluator/src/ssa/parser/ast.rs | 7 +++++++ .../src/ssa/parser/into_ssa.rs | 16 ++++++++++++++++ .../noirc_evaluator/src/ssa/parser/mod.rs | 19 +++++++++++++++++++ .../noirc_evaluator/src/ssa/parser/tests.rs | 12 ++++++++++++ .../noirc_evaluator/src/ssa/parser/token.rs | 3 +++ 5 files changed, 57 insertions(+) diff --git a/compiler/noirc_evaluator/src/ssa/parser/ast.rs b/compiler/noirc_evaluator/src/ssa/parser/ast.rs index 10ffe8a6be3..504ab0ff107 100644 --- a/compiler/noirc_evaluator/src/ssa/parser/ast.rs +++ b/compiler/noirc_evaluator/src/ssa/parser/ast.rs @@ -121,6 +121,13 @@ pub(crate) enum ParsedInstruction { EnableSideEffectsIf { condition: ParsedValue, }, + IfElse { + target: Identifier, + then_condition: ParsedValue, + then_value: ParsedValue, + else_condition: ParsedValue, + else_value: ParsedValue, + }, IncrementRc { value: ParsedValue, }, diff --git a/compiler/noirc_evaluator/src/ssa/parser/into_ssa.rs b/compiler/noirc_evaluator/src/ssa/parser/into_ssa.rs index 8420cd272ad..d794aa235d1 100644 --- a/compiler/noirc_evaluator/src/ssa/parser/into_ssa.rs +++ b/compiler/noirc_evaluator/src/ssa/parser/into_ssa.rs @@ -297,6 +297,22 @@ impl Translator { let condition = self.translate_value(condition)?; self.builder.insert_enable_side_effects_if(condition); } + ParsedInstruction::IfElse { + target, + then_condition, + then_value, + else_condition, + else_value, + } => { + let then_condition = self.translate_value(then_condition)?; + let then_value = self.translate_value(then_value)?; + let else_condition = self.translate_value(else_condition)?; + let else_value = self.translate_value(else_value)?; + let instruction = + Instruction::IfElse { then_condition, then_value, else_condition, else_value }; + let value_id = self.builder.insert_instruction(instruction, None).first(); + self.define_variable(target, value_id)?; + } ParsedInstruction::IncrementRc { value } => { let value = self.translate_value(value)?; self.builder.increment_array_reference_count(value); diff --git a/compiler/noirc_evaluator/src/ssa/parser/mod.rs b/compiler/noirc_evaluator/src/ssa/parser/mod.rs index 89cbb99313f..ad3d72d24eb 100644 --- a/compiler/noirc_evaluator/src/ssa/parser/mod.rs +++ b/compiler/noirc_evaluator/src/ssa/parser/mod.rs @@ -551,6 +551,25 @@ impl<'a> Parser<'a> { return Ok(ParsedInstruction::Truncate { target, value, bit_size, max_bit_size }); } + if self.eat_keyword(Keyword::If)? { + let then_condition = self.parse_value_or_error()?; + self.eat_or_error(Token::Keyword(Keyword::Then))?; + let then_value = self.parse_value_or_error()?; + self.eat_or_error(Token::Keyword(Keyword::Else))?; + self.eat_or_error(Token::LeftParen)?; + self.eat_or_error(Token::Keyword(Keyword::If))?; + let else_condition = self.parse_value_or_error()?; + self.eat_or_error(Token::RightParen)?; + let else_value = self.parse_value_or_error()?; + return Ok(ParsedInstruction::IfElse { + target, + then_condition, + then_value, + else_condition, + else_value, + }); + } + if let Some(op) = self.eat_binary_op()? { let lhs = self.parse_value_or_error()?; self.eat_or_error(Token::Comma)?; diff --git a/compiler/noirc_evaluator/src/ssa/parser/tests.rs b/compiler/noirc_evaluator/src/ssa/parser/tests.rs index 864f2ab77e5..ff83b150f73 100644 --- a/compiler/noirc_evaluator/src/ssa/parser/tests.rs +++ b/compiler/noirc_evaluator/src/ssa/parser/tests.rs @@ -610,3 +610,15 @@ fn parses_purity() { "; assert_ssa_roundtrip(src); } + +#[test] +fn test_parses_if_else() { + let src = " + acir(inline) fn main f0 { + b0(v0: u1, v1: u1): + v4 = if v0 then Field 1 else (if v1) Field 2 + return v4 + } + "; + assert_ssa_roundtrip(src); +} diff --git a/compiler/noirc_evaluator/src/ssa/parser/token.rs b/compiler/noirc_evaluator/src/ssa/parser/token.rs index abd834c8cc3..0c4b60cc3ea 100644 --- a/compiler/noirc_evaluator/src/ssa/parser/token.rs +++ b/compiler/noirc_evaluator/src/ssa/parser/token.rs @@ -139,6 +139,7 @@ pub(crate) enum Keyword { Fold, Fn, Function, + If, Impure, IncRc, Index, @@ -196,6 +197,7 @@ impl Keyword { "else" => Keyword::Else, "enable_side_effects" => Keyword::EnableSideEffects, "eq" => Keyword::Eq, + "if" => Keyword::If, "impure" => Keyword::Impure, "inline" => Keyword::Inline, "inline_always" => Keyword::InlineAlways, @@ -267,6 +269,7 @@ impl Display for Keyword { Keyword::Fold => write!(f, "fold"), Keyword::Fn => write!(f, "fn"), Keyword::Function => write!(f, "function"), + Keyword::If => write!(f, "if"), Keyword::Impure => write!(f, "impure"), Keyword::IncRc => write!(f, "inc_rc"), Keyword::Index => write!(f, "index"),