diff --git a/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify/call.rs b/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify/call.rs index 848d27f4bc6..4526e06421b 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify/call.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/dfg/simplify/call.rs @@ -699,6 +699,14 @@ fn array_is_constant(dfg: &DataFlowGraph, values: &im::Vector) -> bool values.iter().all(|value| dfg.get_numeric_constant(*value).is_some()) } +/// Replaces a call to `derive_pedersen_generators` with the results of the computation. +/// +/// It only works if the arguments to the call are both constants, which means that the +/// function which contains this call needs to be inlined into its caller, where the +/// arguments are known. This is taken care of by the `#[no_predicates]` attribute, +/// which forces inlining after flattening. +/// +/// This intrinsic must not reach Brillig-gen. fn simplify_derive_generators( dfg: &mut DataFlowGraph, arguments: &[ValueId], diff --git a/compiler/noirc_frontend/src/monomorphization/ast.rs b/compiler/noirc_frontend/src/monomorphization/ast.rs index 7aaafadd557..cb83501bd55 100644 --- a/compiler/noirc_frontend/src/monomorphization/ast.rs +++ b/compiler/noirc_frontend/src/monomorphization/ast.rs @@ -462,6 +462,28 @@ impl InlineType { InlineType::NoPredicates => false, } } + + /// Produce an `InlineType` which we can use with an unconstrained version of a function. + pub fn into_unconstrained(self) -> Self { + match self { + InlineType::Inline | InlineType::InlineAlways => self, + InlineType::Fold => { + // The #[fold] attribute is about creating separate ACIR circuits for proving, + // not relevant in Brillig. Leaving it violates some expectations that each + // will become its own entry point. + Self::default() + } + InlineType::NoPredicates => { + // The #[no_predicates] are guaranteed to be inlined after flattening, + // which is needed for some of the programs even in Brillig, otherwise + // some intrinsics can survive until Brillig-gen that weren't supposed to. + // We can keep these, or try inlining more aggressively, since we don't + // have to wait until after flattening in Brillig, but InlineAlways + // resulted in some Brillig bytecode size regressions. + self + } + } + } } impl Display for InlineType { diff --git a/compiler/noirc_frontend/src/monomorphization/mod.rs b/compiler/noirc_frontend/src/monomorphization/mod.rs index 7698c9af529..a15a65bfa21 100644 --- a/compiler/noirc_frontend/src/monomorphization/mod.rs +++ b/compiler/noirc_frontend/src/monomorphization/mod.rs @@ -403,7 +403,10 @@ impl<'interner> Monomorphizer<'interner> { let unconstrained = self.in_unconstrained_function; let attributes = self.interner.function_attributes(&f); - let inline_type = InlineType::from(attributes); + let mut inline_type = InlineType::from(attributes); + if unconstrained { + inline_type = inline_type.into_unconstrained(); + } let parameters = self.parameters(&meta.parameters)?; let body = self.expr(body_expr_id)?; diff --git a/test_programs/compile_success_no_bug/regression_10153/Nargo.toml b/test_programs/compile_success_no_bug/regression_10153/Nargo.toml new file mode 100644 index 00000000000..d532f83ad5d --- /dev/null +++ b/test_programs/compile_success_no_bug/regression_10153/Nargo.toml @@ -0,0 +1,6 @@ +[package] +name = "regression_10153" +type = "bin" +authors = [""] + +[dependencies] \ No newline at end of file diff --git a/test_programs/compile_success_no_bug/regression_10153/src/main.nr b/test_programs/compile_success_no_bug/regression_10153/src/main.nr new file mode 100644 index 00000000000..70659cf98a6 --- /dev/null +++ b/test_programs/compile_success_no_bug/regression_10153/src/main.nr @@ -0,0 +1,17 @@ +pub fn main() -> pub Field { + let _f = foo; // function pointer with #[fold] + + // Safety: test + unsafe { + bar() + } +} + +#[fold] +fn foo() -> Field { + 0 +} + +unconstrained fn bar() -> Field { + foo() // #[fold] called from unconstrained +} diff --git a/tooling/nargo_cli/tests/snapshots/compile_success_no_bug/regression_10153/execute__tests__expanded.snap b/tooling/nargo_cli/tests/snapshots/compile_success_no_bug/regression_10153/execute__tests__expanded.snap new file mode 100644 index 00000000000..8f8ecd38f2e --- /dev/null +++ b/tooling/nargo_cli/tests/snapshots/compile_success_no_bug/regression_10153/execute__tests__expanded.snap @@ -0,0 +1,20 @@ +--- +source: tooling/nargo_cli/tests/execute.rs +expression: expanded_code +--- +pub fn main() -> pub Field { + let _f: fn() -> Field = foo; + // Safety: comment added by `nargo expand` + unsafe { + bar() + } +} + +#[fold] +fn foo() -> Field { + 0_Field +} + +unconstrained fn bar() -> Field { + foo() +}