Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ssa): Perform dead instruction elimination on intrinsic functions #2276

Merged
merged 10 commits into from
Aug 15, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "intrinsic_die"
type = "bin"
authors = [""]
compiler_version = "0.6.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use dep::std;

// This test checks that we perform dead-instruction-elimination on intrinsic functions.

fn main(x: Field) {
let bytes = x.to_be_bytes(32);

let hash = std::hash::pedersen([x]);
}
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 21 additions & 0 deletions crates/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ impl std::fmt::Display for Intrinsic {
}

impl Intrinsic {
/// Returns whether the `Intrinsic` has side effects.
///
/// If there are no side effects then the `Intrinsic` can be removed if the result is unused.
pub(crate) fn has_side_effects(&self) -> bool {
match self {
Intrinsic::Println => true,

Intrinsic::Sort
| Intrinsic::ArrayLen
| Intrinsic::SlicePushBack
| Intrinsic::SlicePushFront
| Intrinsic::SlicePopBack
| Intrinsic::SlicePopFront
| Intrinsic::SliceInsert
| Intrinsic::SliceRemove
| Intrinsic::ToBits(_)
| Intrinsic::ToRadix(_)
| Intrinsic::BlackBox(_) => false,
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Lookup an Intrinsic by name and return it if found.
/// If there is no such intrinsic by that name, None is returned.
pub(crate) fn lookup(name: &str) -> Option<Intrinsic> {
Expand Down
26 changes: 20 additions & 6 deletions crates/noirc_evaluator/src/ssa/opt/die.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,26 @@ impl Context {

let instruction = &function.dfg[instruction_id];

// These instruction types cannot be removed
if matches!(
instruction,
Constrain(_) | Call { .. } | Store { .. } | EnableSideEffects { .. }
) {
return false;
match instruction {
// These instruction types can be safely removed
Binary(_)
| Cast(_, _)
| Not(_)
| Truncate { .. }
| Allocate
| Load { .. }
| ArrayGet { .. }
| ArraySet { .. } => (),

// These instruction types cannot be removed
Constrain(_) | Store { .. } | EnableSideEffects { .. } => return false,
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved

Call { func, .. } => match function.dfg[*func] {
Value::Intrinsic(intrinsic) if !intrinsic.has_side_effects() => {
// Intrinsics without side-effects can be safely removed.
}
_ => return false,
jfecher marked this conversation as resolved.
Show resolved Hide resolved
},
}

let results = function.dfg.instruction_results(instruction_id);
Expand Down