Skip to content

Commit

Permalink
chore: pull out constant brillig inliner (#10291)
Browse files Browse the repository at this point in the history
Please read [contributing guidelines](CONTRIBUTING.md) and remove this
line.

---------

Co-authored-by: Ary Borenszweig <asterite@gmail.com>
  • Loading branch information
TomAFrench and asterite authored Nov 29, 2024
1 parent ba335bd commit 0577c1a
Show file tree
Hide file tree
Showing 4 changed files with 594 additions and 55 deletions.
17 changes: 17 additions & 0 deletions noir/noir-repo/compiler/noirc_evaluator/src/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ pub(crate) fn optimize_into_acir(
ssa.to_brillig(options.enable_brillig_logging)
});

let ssa_gen_span = span!(Level::TRACE, "ssa_generation");
let ssa_gen_span_guard = ssa_gen_span.enter();

let ssa = SsaBuilder {
ssa,
print_ssa_passes: options.enable_ssa_logging,
print_codegen_timings: options.print_codegen_timings,
}
.run_pass(
|ssa| ssa.fold_constants_with_brillig(&brillig),
"After Constant Folding with Brillig:",
)
.run_pass(Ssa::dead_instruction_elimination, "After Dead Instruction Elimination:")
.finish();

drop(ssa_gen_span_guard);

let artifacts = time("SSA to ACIR", options.print_codegen_timings, || {
ssa.into_acir(&brillig, options.expression_width)
})?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ pub(super) fn simplify_call(
_ => return SimplifyResult::None,
};

let return_type = ctrl_typevars.and_then(|return_types| return_types.first().cloned());

let constant_args: Option<Vec<_>> =
arguments.iter().map(|value_id| dfg.get_numeric_constant(*value_id)).collect();

match intrinsic {
let simplified_result = match intrinsic {
Intrinsic::ToBits(endian) => {
// TODO: simplify to a range constraint if `limb_count == 1`
if let (Some(constant_args), Some(return_type)) =
(constant_args, ctrl_typevars.map(|return_types| return_types.first().cloned()))
{
if let (Some(constant_args), Some(return_type)) = (constant_args, return_type.clone()) {
let field = constant_args[0];
let limb_count = if let Some(Type::Array(_, array_len)) = return_type {
let limb_count = if let Type::Array(_, array_len) = return_type {
array_len as u32
} else {
unreachable!("ICE: Intrinsic::ToRadix return type must be array")
Expand All @@ -67,12 +67,10 @@ pub(super) fn simplify_call(
}
Intrinsic::ToRadix(endian) => {
// TODO: simplify to a range constraint if `limb_count == 1`
if let (Some(constant_args), Some(return_type)) =
(constant_args, ctrl_typevars.map(|return_types| return_types.first().cloned()))
{
if let (Some(constant_args), Some(return_type)) = (constant_args, return_type.clone()) {
let field = constant_args[0];
let radix = constant_args[1].to_u128() as u32;
let limb_count = if let Some(Type::Array(_, array_len)) = return_type {
let limb_count = if let Type::Array(_, array_len) = return_type {
array_len as u32
} else {
unreachable!("ICE: Intrinsic::ToRadix return type must be array")
Expand Down Expand Up @@ -330,7 +328,7 @@ pub(super) fn simplify_call(
}
Intrinsic::FromField => {
let incoming_type = Type::field();
let target_type = ctrl_typevars.unwrap().remove(0);
let target_type = return_type.clone().unwrap();

let truncate = Instruction::Truncate {
value: arguments[0],
Expand All @@ -352,8 +350,8 @@ pub(super) fn simplify_call(
Intrinsic::AsWitness => SimplifyResult::None,
Intrinsic::IsUnconstrained => SimplifyResult::None,
Intrinsic::DerivePedersenGenerators => {
if let Some(Type::Array(_, len)) = ctrl_typevars.unwrap().first() {
simplify_derive_generators(dfg, arguments, *len as u32, block, call_stack)
if let Some(Type::Array(_, len)) = return_type.clone() {
simplify_derive_generators(dfg, arguments, len as u32, block, call_stack)
} else {
unreachable!("Derive Pedersen Generators must return an array");
}
Expand All @@ -370,7 +368,19 @@ pub(super) fn simplify_call(
}
Intrinsic::ArrayRefCount => SimplifyResult::None,
Intrinsic::SliceRefCount => SimplifyResult::None,
};

if let (Some(expected_types), SimplifyResult::SimplifiedTo(result)) =
(return_type, &simplified_result)
{
assert_eq!(
dfg.type_of_value(*result),
expected_types,
"Simplification should not alter return type"
);
}

simplified_result
}

/// Slices have a tuple structure (slice length, slice contents) to enable logic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub(super) fn simplify_ec_add(

let result_x = dfg.make_constant(result_x, Type::field());
let result_y = dfg.make_constant(result_y, Type::field());
let result_is_infinity = dfg.make_constant(result_is_infinity, Type::bool());
let result_is_infinity = dfg.make_constant(result_is_infinity, Type::field());

let typ = Type::Array(Arc::new(vec![Type::field()]), 3);

Expand Down Expand Up @@ -107,7 +107,7 @@ pub(super) fn simplify_msm(

let result_x = dfg.make_constant(result_x, Type::field());
let result_y = dfg.make_constant(result_y, Type::field());
let result_is_infinity = dfg.make_constant(result_is_infinity, Type::bool());
let result_is_infinity = dfg.make_constant(result_is_infinity, Type::field());

let elements = im::vector![result_x, result_y, result_is_infinity];
let typ = Type::Array(Arc::new(vec![Type::field()]), 3);
Expand Down
Loading

0 comments on commit 0577c1a

Please sign in to comment.