From c4512d9c09fd16a937653a5cd836600456ee375c Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 9 Jan 2025 08:55:25 -0600 Subject: [PATCH] chore: Remove resolve_is_unconstrained pass (#7004) --- compiler/noirc_evaluator/src/ssa.rs | 1 - compiler/noirc_evaluator/src/ssa/ir/dfg.rs | 1 + .../src/ssa/ir/instruction/call.rs | 5 +- .../src/ssa/opt/constant_folding.rs | 16 +++--- compiler/noirc_evaluator/src/ssa/opt/mod.rs | 1 - .../src/ssa/opt/resolve_is_unconstrained.rs | 55 ------------------- 6 files changed, 12 insertions(+), 67 deletions(-) delete mode 100644 compiler/noirc_evaluator/src/ssa/opt/resolve_is_unconstrained.rs diff --git a/compiler/noirc_evaluator/src/ssa.rs b/compiler/noirc_evaluator/src/ssa.rs index 02425618c3d..206fe8d9084 100644 --- a/compiler/noirc_evaluator/src/ssa.rs +++ b/compiler/noirc_evaluator/src/ssa.rs @@ -153,7 +153,6 @@ fn optimize_all(builder: SsaBuilder, options: &SsaEvaluatorOptions) -> Result SimplifyResult::None, - Intrinsic::IsUnconstrained => SimplifyResult::None, + Intrinsic::IsUnconstrained => { + let result = dfg.runtime().is_brillig().into(); + SimplifyResult::SimplifiedTo(dfg.make_constant(result, NumericType::bool())) + } Intrinsic::DerivePedersenGenerators => { if let Some(Type::Array(_, len)) = return_type.clone() { simplify_derive_generators(dfg, arguments, len, block, call_stack) diff --git a/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs b/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs index 8dca867fc6d..db249f3bc3a 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs @@ -1557,7 +1557,6 @@ mod test { // After EnableSideEffectsIf removal: brillig(inline) fn main f0 { b0(v0: Field, v1: Field, v2: u1): - v4 = call is_unconstrained() -> u1 v7 = call to_be_radix(v0, u32 256) -> [u8; 1] // `a.to_be_radix(256)`; inc_rc v7 v8 = call to_be_radix(v0, u32 256) -> [u8; 1] // duplicate load of `a` @@ -1574,14 +1573,13 @@ mod test { let expected = " brillig(inline) fn main f0 { b0(v0: Field, v1: Field, v2: u1): - v4 = call is_unconstrained() -> u1 - v7 = call to_be_radix(v0, u32 256) -> [u8; 1] - inc_rc v7 - inc_rc v7 - v8 = cast v2 as Field - v9 = mul v0, v8 - v10 = call to_be_radix(v9, u32 256) -> [u8; 1] - inc_rc v10 + v5 = call to_be_radix(v0, u32 256) -> [u8; 1] + inc_rc v5 + inc_rc v5 + v6 = cast v2 as Field + v7 = mul v0, v6 + v8 = call to_be_radix(v7, u32 256) -> [u8; 1] + inc_rc v8 enable_side_effects v2 return } diff --git a/compiler/noirc_evaluator/src/ssa/opt/mod.rs b/compiler/noirc_evaluator/src/ssa/opt/mod.rs index 58dbc25e869..1105e15c30e 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/mod.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/mod.rs @@ -21,7 +21,6 @@ mod remove_bit_shifts; mod remove_enable_side_effects; mod remove_if_else; mod remove_unreachable; -mod resolve_is_unconstrained; mod simplify_cfg; mod unrolling; diff --git a/compiler/noirc_evaluator/src/ssa/opt/resolve_is_unconstrained.rs b/compiler/noirc_evaluator/src/ssa/opt/resolve_is_unconstrained.rs deleted file mode 100644 index 3ac7535a1c4..00000000000 --- a/compiler/noirc_evaluator/src/ssa/opt/resolve_is_unconstrained.rs +++ /dev/null @@ -1,55 +0,0 @@ -use crate::ssa::{ - ir::{ - function::{Function, RuntimeType}, - instruction::{Instruction, Intrinsic}, - types::NumericType, - value::Value, - }, - ssa_gen::Ssa, -}; -use fxhash::FxHashSet as HashSet; - -impl Ssa { - /// An SSA pass to find any calls to `Intrinsic::IsUnconstrained` and replacing any uses of the result of the intrinsic - /// with the resolved boolean value. - /// Note that this pass must run after the pass that does runtime separation, since in SSA generation an ACIR function can end up targeting brillig. - #[tracing::instrument(level = "trace", skip(self))] - pub(crate) fn resolve_is_unconstrained(mut self) -> Self { - for func in self.functions.values_mut() { - func.replace_is_unconstrained_result(); - } - self - } -} - -impl Function { - pub(crate) fn replace_is_unconstrained_result(&mut self) { - let mut is_unconstrained_calls = HashSet::default(); - // Collect all calls to is_unconstrained - for block_id in self.reachable_blocks() { - for &instruction_id in self.dfg[block_id].instructions() { - let target_func = match &self.dfg[instruction_id] { - Instruction::Call { func, .. } => *func, - _ => continue, - }; - - if let Value::Intrinsic(Intrinsic::IsUnconstrained) = &self.dfg[target_func] { - is_unconstrained_calls.insert(instruction_id); - } - } - } - - let is_unconstrained = matches!(self.runtime(), RuntimeType::Brillig(_)).into(); - let is_within_unconstrained = self.dfg.make_constant(is_unconstrained, NumericType::bool()); - for instruction_id in is_unconstrained_calls { - let call_returns = self.dfg.instruction_results(instruction_id); - let original_return_id = call_returns[0]; - - // Replace all uses of the original return value with the constant - self.dfg.set_value_from_id(original_return_id, is_within_unconstrained); - - // Now remove the original instruction - self.dfg.remove_instruction(instruction_id); - } - } -}