-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Try to turn acir lambdas into brillig #7279
Draft
aakoshh
wants to merge
6
commits into
master
Choose a base branch
from
7247-fix-defunct-lambdas
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+234
−26
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8a3d5a8
Try to turn acir lambdas into brillig
aakoshh 6ce0a8c
Add comment to undo the change around the unsafe block
aakoshh ce8feff
Set unconstrained on the lambda type to match the lambda function
aakoshh 5de1f6b
Remove prints
aakoshh eff9d08
Remove unconstrained from array sort lambdas
aakoshh 5ecf048
Add comments to test
aakoshh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -500,7 +500,16 @@ impl<'interner> Monomorphizer<'interner> { | |
}, | ||
HirExpression::Literal(HirLiteral::Unit) => ast::Expression::Block(vec![]), | ||
HirExpression::Block(block) => self.block(block.statements)?, | ||
HirExpression::Unsafe(block) => self.block(block.statements)?, | ||
HirExpression::Unsafe(block) => { | ||
// TODO: Undo this change; not everything in an `unsafe` block is unconstrained, it just | ||
// means we can call unconstrained functions. But it was a convenient way to change the | ||
// the way lambdas made in an `unsafe` block are compiled and make corresponding tests pass. | ||
let was_in_unconstrained_function = self.in_unconstrained_function; | ||
self.in_unconstrained_function = true; | ||
let res = self.block(block.statements); | ||
self.in_unconstrained_function = was_in_unconstrained_function; | ||
Comment on lines
+508
to
+510
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like we're treating the contents of an |
||
res? | ||
} | ||
|
||
HirExpression::Prefix(prefix) => { | ||
let rhs = self.expr(prefix.rhs)?; | ||
|
@@ -1822,15 +1831,16 @@ impl<'interner> Monomorphizer<'interner> { | |
inline_type: InlineType::default(), | ||
func_sig: FunctionSignature::default(), | ||
}; | ||
self.push_function(id, function); | ||
|
||
let typ = ast::Type::Function( | ||
parameter_types, | ||
Box::new(ret_type), | ||
Box::new(ast::Type::Unit), | ||
false, | ||
function.unconstrained, | ||
); | ||
|
||
self.push_function(id, function); | ||
|
||
let name = lambda_name.to_owned(); | ||
Ok(ast::Expression::Ident(ast::Ident { | ||
definition: Definition::Function(id), | ||
|
@@ -1928,11 +1938,15 @@ impl<'interner> Monomorphizer<'interner> { | |
let body = self.expr(lambda.body)?; | ||
self.lambda_envs_stack.pop(); | ||
|
||
// TODO: Ideally a lambda would inherit the runtime of its caller, but it could be passed as a | ||
// variable (by function ID) to a function that uses it both as constrained and unconstrained. | ||
let is_unconstrained = self.in_unconstrained_function; | ||
|
||
let lambda_fn_typ: ast::Type = ast::Type::Function( | ||
parameter_types, | ||
Box::new(ret_type), | ||
Box::new(env_typ.clone()), | ||
false, | ||
is_unconstrained, | ||
); | ||
let lambda_fn = ast::Expression::Ident(ast::Ident { | ||
definition: Definition::Function(id), | ||
|
@@ -1951,10 +1965,11 @@ impl<'interner> Monomorphizer<'interner> { | |
parameters, | ||
body, | ||
return_type, | ||
unconstrained: self.in_unconstrained_function, | ||
unconstrained: is_unconstrained, | ||
inline_type: InlineType::default(), | ||
func_sig: FunctionSignature::default(), | ||
}; | ||
|
||
self.push_function(id, function); | ||
|
||
let lambda_value = | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "regression_7247" | ||
version = "0.1.0" | ||
type = "bin" | ||
authors = [""] | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
a = 5 | ||
b = 500 | ||
c = 5000 |
67 changes: 67 additions & 0 deletions
67
test_programs/execution_success/regression_7247/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
fn main( | ||
a: u32, | ||
b: u32, | ||
c: u32, | ||
) { | ||
/// Safety: Test | ||
let vb = unsafe { via_brillig(a) }; | ||
let va = via_acir(a); | ||
assert_eq(vb, b); | ||
assert_eq(va, b); | ||
|
||
/// Safety: Test | ||
let bat = unsafe { brillig_apply_twice(a, times_ten) }; | ||
assert_eq(bat, b, "should be a*100"); | ||
|
||
let mat = mixed_apply_thrice(a, times_ten); | ||
assert_eq(mat, c, "should be a*1000"); | ||
} | ||
|
||
// The lambdas created within should be constrained. | ||
fn via_acir(a: u32) -> u32 { | ||
times100(a) | ||
} | ||
|
||
// The lambdas created within this should be unconstrained, | ||
unconstrained fn via_brillig(a: u32) -> u32 { | ||
times100(a) | ||
} | ||
|
||
// Example of a higher order function that will be transformed | ||
// to call lambdas via `apply` functions. Those should only call | ||
// one version of the `|x| x * 10` lambda, not both `acir` and `brillig`. | ||
fn apply_twice(a: u32, f: fn(u32) -> u32) -> u32 { | ||
f(f(a)) | ||
} | ||
|
||
// Example of an unconstrained higher order function to which we pass | ||
// a lambda defined in constrained code; we want it to call a `brillig` lambda. | ||
unconstrained fn brillig_apply_twice(a: u32, f: fn(u32) -> u32) -> u32 { | ||
f(f(a)) | ||
} | ||
|
||
// Example of a constrained function that gets a constrained lambda, | ||
// which it passes on to unconstrained code and also calls it locally, | ||
// so the lambda is gets is called from both kinds of environments. | ||
fn mixed_apply_thrice(a: u32, f: fn(u32) -> u32) -> u32 { | ||
/// Safety: Test | ||
let aa = unsafe { brillig_apply_twice(a, f) }; | ||
let aaa = f(aa); | ||
aaa | ||
} | ||
|
||
// Example of creating a lambda and passing it on. Whether it's constrained or unconstrained | ||
// depends on whether we call it from `via_acir` or `via_brillig`. | ||
fn times100(a: u32) -> u32 { | ||
apply_twice(a, |x| x * 10) | ||
} | ||
|
||
// Example of a constrained function we pass to both constrained and unconstrained by name. | ||
fn times_ten(x: u32) -> u32 { | ||
// Arbitrary `if` statement to trigger `EnableSideEffect` during flattening. | ||
if x > 100000 { | ||
x * 10 - 1 | ||
} else { | ||
x * 10 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this change we end up in a situation where if the lambda is created in constrained code, and subsequently is passed to unconstrained code, then this list is empty and the compiler crashes further down.