Skip to content

Commit

Permalink
chore: find set of instructions which allow tests to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Aug 29, 2023
1 parent e45dfd6 commit 4973d34
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
25 changes: 25 additions & 0 deletions crates/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,31 @@ impl Instruction {
matches!(self.result_type(), InstructionResultType::Unknown)
}

/// Pure `Instructions` are instructions which have no side-effects and results are a function of the inputs only,
/// i.e. there are no interactions with memory.
///
/// Pure instructions can be replaced with the results of another pure instruction with the same inputs.
pub(crate) fn is_pure(&self, dfg: &DataFlowGraph) -> bool {
use Instruction::*;

match self {
Binary(_) | Cast(_, _) | Not(_) | ArrayGet { .. } | ArraySet { .. } => true,

// Unclear why this instruction causes problems.
Truncate { .. } => false,

// These either have side-effects or interact with memory
Constrain(_, _) | EnableSideEffects { .. } | Allocate | Load { .. } | Store { .. } => {
false
}

Call { func, .. } => match dfg[*func] {
Value::Intrinsic(intrinsic) => !intrinsic.has_side_effects(),
_ => false,
},
}
}

pub(crate) fn has_side_effects(&self, dfg: &DataFlowGraph) -> bool {
use Instruction::*;

Expand Down
4 changes: 1 addition & 3 deletions crates/noirc_evaluator/src/ssa/opt/constant_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ impl Context {

// If the instruction doesn't have side-effects, cache the results so we can reuse them if
// the same instruction appears again later in the block.
if !instruction.has_side_effects(&function.dfg)
&& !matches!(instruction, Instruction::Allocate)
{
if instruction.is_pure(&function.dfg) {
instruction_result_cache.insert(instruction, new_results.clone());
}
for (old_result, new_result) in old_results.iter().zip(new_results) {
Expand Down

0 comments on commit 4973d34

Please sign in to comment.