Skip to content

Commit

Permalink
Merge 8540b55 into fd836cb
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Nov 15, 2023
2 parents fd836cb + 8540b55 commit 54ff1c2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/ssa/ir/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub(crate) fn display_instruction(
)
}
Instruction::RangeCheck { value, max_bit_size, .. } => {
write!(f, "range_check {} to {} bits", show(*value), *max_bit_size,)
writeln!(f, "range_check {} to {} bits", show(*value), *max_bit_size,)
}
}
}
16 changes: 16 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,22 @@ impl<'f> Context<'f> {
self.remember_store(address, value);
Instruction::Store { address, value }
}
Instruction::RangeCheck { value, max_bit_size, assert_message } => {
// Replace value with `value * predicate` to zero out value when predicate is inactive.

// Condition needs to be cast to argument type in order to multiply them together.
let argument_type = self.inserter.function.dfg.type_of_value(value);
let casted_condition = self.insert_instruction(
Instruction::Cast(condition, argument_type),
call_stack.clone(),
);

let value = self.insert_instruction(
Instruction::binary(BinaryOp::Mul, value, casted_condition),
call_stack.clone(),
);
Instruction::RangeCheck { value, max_bit_size, assert_message }
}
other => other,
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "conditional_underflow"
type = "bin"
authors = [""]

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "4"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Regression test for https://github.com/noir-lang/noir/issues/3493
fn main(x: u4) {
if x == 10 {
x + 15;
}
if x == 9 {
x << 3;
}
if x == 8 {
x * 3;
}
if x == 7 {
x - 8;
}
}

0 comments on commit 54ff1c2

Please sign in to comment.