Skip to content

Commit

Permalink
feat: remove predicate from sort intrinsic function (#4228)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This PR removes the predicate from `AcirContext.sort()`. I'm not sure
why this exists as we should always be able to perform a sorting on an
array of values. We also list `Intrinsic::Sort` as having no side
effects so it doesn't make sense for us to be using the side effects
predicate here.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
TomAFrench authored Feb 2, 2024
1 parent 4467ea9 commit d646243
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
18 changes: 4 additions & 14 deletions compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,6 @@ impl AcirContext {
&mut self,
inputs: Vec<AcirVar>,
bit_size: u32,
predicate: AcirVar,
) -> Result<Vec<AcirVar>, RuntimeError> {
let len = inputs.len();
// Convert the inputs into expressions
Expand All @@ -1666,25 +1665,16 @@ impl AcirContext {
self.acir_ir.permutation(&inputs_expr, &output_expr)?;

// Enforce the outputs to be sorted
let true_var = self.add_constant(true);
for i in 0..(outputs_var.len() - 1) {
self.less_than_constrain(outputs_var[i], outputs_var[i + 1], bit_size, predicate)?;
let less_than_next_element =
self.more_than_eq_var(outputs_var[i + 1], outputs_var[i], bit_size)?;
self.assert_eq_var(less_than_next_element, true_var, None)?;
}

Ok(outputs_var)
}

/// Constrain lhs to be less than rhs
fn less_than_constrain(
&mut self,
lhs: AcirVar,
rhs: AcirVar,
bit_size: u32,
predicate: AcirVar,
) -> Result<(), RuntimeError> {
let lhs_less_than_rhs = self.more_than_eq_var(rhs, lhs, bit_size)?;
self.maybe_eq_predicate(lhs_less_than_rhs, predicate)
}

/// Returns a Variable that is constrained to be the result of reading
/// from the memory `block_id` at the given `index`.
pub(crate) fn read_from_memory(
Expand Down
5 changes: 1 addition & 4 deletions compiler/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1623,10 +1623,7 @@ impl Context {
}
}
// Generate the sorted output variables
let out_vars = self
.acir_context
.sort(input_vars, bit_size, self.current_side_effects_enabled_var)
.expect("Could not sort");
let out_vars = self.acir_context.sort(input_vars, bit_size)?;

Ok(self.convert_vars_to_values(out_vars, dfg, result_ids))
}
Expand Down

0 comments on commit d646243

Please sign in to comment.