fix(defunctionalization): ACIR variant in Brillig #9728
Merged
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.
Description
Problem*
Resolves errors in #9412.
As per the #9484 description we duplicate both constrained and unconstrained versions of closures. Later when the function is called we extract the correct version to call based off of the caller runtime. However, this means we may still have closure variants with differing runtimes for which defunctionalization will attempt to call.
Summary*
#9412 attempted to work around variants with differing runtimes by adding an extra always failing constrain that are attemping to call a function with a different runtime.
For example this is what was done in #9412 (where f3 is an ACIR function):
However, this breaks when we get to inlining as we will attempt to inline f3.
We can achieve the same effect by simply not skipping all functions with a differing runtime. All conditionals and the final dispatch constrain will only reference the function IDs for the variants with matching runtimes. Thus, if the frontend generates bad code and attempts to call the ACIR variant, it will be caught by the
constrain
in the final dispatch. You can see the tests in this PR for a more in depth example. Theapply
function above will now be the following:I decided to perform the filtering during
create_apply_functions
so that we can add an extra check for whether we ended up with no variants after filtering against ACIR runtimes. This means we have a frontend bug for closure runtime duplication.Additional Context
Another option would be to keep what was done in #9412 but mark ACIR -> Brillig calls as never being allowed to be inlined. However, this ACIR being called from Brillig would then be a well-formed SSA and it would have to be protected by constrain instruction as done for the
apply
method.Documentation*
Check one:
PR Checklist*
cargo fmt
on default settings.