-
Notifications
You must be signed in to change notification settings - Fork 388
chore: Separate unconstrained functions during monomorphization #6894
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
08732ed
Separate brillig fns during monomorphization
jfecher 094a831
Use new flag to set function unconstrained
jfecher 459b300
Fix force-brillig flag
jfecher a597f6a
Remove case
jfecher 16d4b5e
Fix frontend tests
jfecher f1eccde
Remove printlns
jfecher d0a06f1
Merge branch 'master' into jf/monomorphize-unconstrained
jfecher 07078f8
Add check
jfecher 0f1f17f
fmt
jfecher 73ce9b6
Fix issues with remove unused functions pass
jfecher ac10ed0
Make lambdas unconstrained too to fix regression
jfecher 48022e8
Remove println
jfecher b5a151b
Finally find and fix bug
jfecher 54ca620
Remove clone_fn
jfecher a34c46b
Avoid issuing inc & dec_rc outside of unconstrained code
jfecher 8c973ea
Fix evaluator tests
jfecher f8eb0da
Merge branch 'master' into jf/monomorphize-unconstrained
TomAFrench ed9da6e
Merge branch 'master' into jf/monomorphize-unconstrained
TomAFrench a3db9cc
Add exception for the debugger
jfecher 409bca8
Merge branch 'jf/monomorphize-unconstrained' of https://github.com/no…
jfecher 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
80 changes: 80 additions & 0 deletions
80
compiler/noirc_evaluator/src/ssa/opt/remove_unreachable.rs
This file contains hidden or 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,80 @@ | ||
| use std::collections::BTreeSet; | ||
|
|
||
| use fxhash::FxHashSet as HashSet; | ||
|
|
||
| use crate::ssa::{ | ||
| ir::{ | ||
| function::{Function, FunctionId}, | ||
| instruction::Instruction, | ||
| value::Value, | ||
| }, | ||
| ssa_gen::Ssa, | ||
| }; | ||
|
|
||
| impl Ssa { | ||
| /// Removes any unreachable functions from the code. These can result from | ||
| /// optimizations making existing functions unreachable, e.g. `if false { foo() }`, | ||
| /// or even from monomorphizing an unconstrained version of a constrained function | ||
| /// where the original constrained version ends up never being used. | ||
| pub(crate) fn remove_unreachable_functions(mut self) -> Self { | ||
| let mut used_functions = HashSet::default(); | ||
|
|
||
| for function_id in self.functions.keys() { | ||
| if self.is_entry_point(*function_id) { | ||
| collect_reachable_functions(&self, *function_id, &mut used_functions); | ||
| } | ||
| } | ||
|
|
||
| self.functions.retain(|id, _| used_functions.contains(id)); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| fn collect_reachable_functions( | ||
| ssa: &Ssa, | ||
| current_func_id: FunctionId, | ||
| reachable_functions: &mut HashSet<FunctionId>, | ||
| ) { | ||
| if reachable_functions.contains(¤t_func_id) { | ||
| return; | ||
| } | ||
| reachable_functions.insert(current_func_id); | ||
|
|
||
| // If the debugger is used, its possible for function inlining | ||
| // to remove functions that the debugger still references | ||
| let Some(func) = ssa.functions.get(¤t_func_id) else { | ||
| return; | ||
| }; | ||
|
|
||
| let used_functions = used_functions(func); | ||
|
|
||
| for called_func_id in used_functions.iter() { | ||
| collect_reachable_functions(ssa, *called_func_id, reachable_functions); | ||
| } | ||
| } | ||
|
|
||
| fn used_functions(func: &Function) -> BTreeSet<FunctionId> { | ||
| let mut used_function_ids = BTreeSet::default(); | ||
|
|
||
| let mut find_functions = |value| { | ||
| if let Value::Function(function) = func.dfg[func.dfg.resolve(value)] { | ||
| used_function_ids.insert(function); | ||
| } | ||
| }; | ||
|
|
||
| for block_id in func.reachable_blocks() { | ||
| let block = &func.dfg[block_id]; | ||
|
|
||
| for instruction_id in block.instructions() { | ||
| let instruction = &func.dfg[*instruction_id]; | ||
|
|
||
| if matches!(instruction, Instruction::Store { .. } | Instruction::Call { .. }) { | ||
aakoshh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| instruction.for_each_value(&mut find_functions); | ||
| } | ||
| } | ||
|
|
||
| block.unwrap_terminator().for_each_value(&mut find_functions); | ||
| } | ||
|
|
||
| used_function_ids | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.