Skip to content

Commit 779e013

Browse files
authored
chore: remove equality operation on boolean constraints against constants (#5919)
… # Description ## Problem\* Resolves <!-- Link to GitHub Issue --> ## Summary\* We're currently performing a `x == 1` equality and then constraining the result to be `1` when dealing with boolean values in brillig. This PR updates the codegen to just act on `x` directly. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** 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.
1 parent 1737b65 commit 779e013

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs

+35-9
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,40 @@ impl<'block> BrilligBlock<'block> {
255255
self.convert_ssa_binary(binary, dfg, result_var);
256256
}
257257
Instruction::Constrain(lhs, rhs, assert_message) => {
258-
let condition = SingleAddrVariable {
259-
address: self.brillig_context.allocate_register(),
260-
bit_size: 1,
258+
let (condition, deallocate) = match (
259+
dfg.get_numeric_constant_with_type(*lhs),
260+
dfg.get_numeric_constant_with_type(*rhs),
261+
) {
262+
// If the constraint is of the form `x == u1 1` then we can simply constrain `x` directly
263+
(
264+
Some((constant, Type::Numeric(NumericType::Unsigned { bit_size: 1 }))),
265+
None,
266+
) if constant == FieldElement::one() => {
267+
(self.convert_ssa_single_addr_value(*rhs, dfg), false)
268+
}
269+
(
270+
None,
271+
Some((constant, Type::Numeric(NumericType::Unsigned { bit_size: 1 }))),
272+
) if constant == FieldElement::one() => {
273+
(self.convert_ssa_single_addr_value(*lhs, dfg), false)
274+
}
275+
276+
// Otherwise we need to perform the equality explicitly.
277+
_ => {
278+
let condition = SingleAddrVariable {
279+
address: self.brillig_context.allocate_register(),
280+
bit_size: 1,
281+
};
282+
self.convert_ssa_binary(
283+
&Binary { lhs: *lhs, rhs: *rhs, operator: BinaryOp::Eq },
284+
dfg,
285+
condition,
286+
);
287+
288+
(condition, true)
289+
}
261290
};
262291

263-
self.convert_ssa_binary(
264-
&Binary { lhs: *lhs, rhs: *rhs, operator: BinaryOp::Eq },
265-
dfg,
266-
condition,
267-
);
268292
match assert_message {
269293
Some(ConstrainError::Dynamic(selector, values)) => {
270294
let payload_values =
@@ -287,7 +311,9 @@ impl<'block> BrilligBlock<'block> {
287311
self.brillig_context.codegen_constrain(condition, None);
288312
}
289313
}
290-
self.brillig_context.deallocate_single_addr(condition);
314+
if deallocate {
315+
self.brillig_context.deallocate_single_addr(condition);
316+
}
291317
}
292318
Instruction::Allocate => {
293319
let result_value = dfg.instruction_results(instruction_id)[0];

0 commit comments

Comments
 (0)