-
Notifications
You must be signed in to change notification settings - Fork 11
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
Function inlining #463
Merged
Merged
Function inlining #463
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0f8fba9
Function inlining in Rust
kirstenmg 5757cab
Restore previous context adding
kirstenmg 6aa8cc7
Replace context of children in rust subst
kirstenmg f525152
Deduplicate function inlining; rust code is too slow but egglog runs …
kirstenmg e663dd6
Try not replacing context; improves speed
kirstenmg 790115d
Fix slow function inlining by actually caching in subst and not repla…
kirstenmg d5234a5
Clean up lib.rs
kirstenmg 7da21b5
Add snapshots
kirstenmg be37526
Clean up code
kirstenmg 7b0a23e
Update snapshots due to rebase
kirstenmg 2dcecfb
Split get_calls_and_subst
kirstenmg 2a0e4e6
Fix iteration bug in function inlining
kirstenmg a532714
Make egglog generation for func inlining fast by not hashing RcExprs
kirstenmg b0a8109
Make egglog faster for func inlining by sharing intermediate cache
kirstenmg eb7ca9d
Remove unneeded var_count
kirstenmg e3212e9
Share more caches
kirstenmg dea1395
Decrease function inlining to 2 iterations
kirstenmg 161ba11
Factor out function inlining config
kirstenmg 4f3245b
Clean up code; even fewer intermediates generated
kirstenmg 83379ba
Refactor, remove silly method
kirstenmg 4fb73f9
Merge with main
kirstenmg 43b5e05
Refactor to work with context on leaves
kirstenmg 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) const FUNCTION_INLINING_ITERATIONS: usize = 2; |
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 |
---|---|---|
@@ -1 +1,91 @@ | ||
use std::{ | ||
collections::{HashMap, HashSet}, | ||
rc::Rc, | ||
vec, | ||
}; | ||
|
||
use crate::schema::{Expr, RcExpr, TreeProgram}; | ||
|
||
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord)] | ||
pub struct CallBody { | ||
pub call: RcExpr, | ||
pub body: RcExpr, | ||
} | ||
|
||
// Gets a set of all the calls in the program | ||
fn get_calls(expr: &RcExpr) -> Vec<RcExpr> { | ||
// Get calls from children | ||
let mut calls = if !expr.children_exprs().is_empty() { | ||
expr.children_exprs() | ||
.iter() | ||
.flat_map(get_calls) | ||
.collect::<Vec<_>>() | ||
} else { | ||
Vec::new() | ||
}; | ||
|
||
// Add to set if this is a call | ||
if let Expr::Call(_, _) = expr.as_ref() { | ||
calls.push(expr.clone()); | ||
} | ||
|
||
calls | ||
} | ||
|
||
// Pairs a call with its equivalent inlined body, using the passed-in function -> body map | ||
// to look up the body | ||
fn subst_call(call: &RcExpr, func_to_body: &HashMap<String, &RcExpr>) -> CallBody { | ||
if let Expr::Call(func_name, args) = call.as_ref() { | ||
CallBody { | ||
call: call.clone(), | ||
body: Expr::subst(args, func_to_body[func_name]), | ||
} | ||
} else { | ||
panic!("Tried to substitute non-calls.") | ||
} | ||
} | ||
|
||
// Generates a list of (call, body) pairs (in a CallBody) that can be unioned | ||
pub fn function_inlining_pairs(program: &TreeProgram, iterations: usize) -> Vec<CallBody> { | ||
let mut all_funcs = vec![program.entry.clone()]; | ||
all_funcs.extend(program.functions.clone()); | ||
|
||
// Make func name -> body map | ||
let func_name_to_body = all_funcs | ||
.iter() | ||
.map(|func| { | ||
( | ||
func.func_name().expect("Func has name"), | ||
func.func_body().expect("Func has body"), | ||
) | ||
}) | ||
.collect::<HashMap<String, &RcExpr>>(); | ||
|
||
// Inline once | ||
// Keep track of all calls we've seen so far to avoid duplication | ||
let mut prev_calls: HashSet<*const Expr> = HashSet::new(); | ||
let mut prev_inlining = all_funcs | ||
.iter() | ||
.flat_map(get_calls) | ||
// Deduplicate calls before substitution | ||
.filter(|call| prev_calls.insert(Rc::as_ptr(call))) | ||
// We cannot hash RcExprs because it is too slow | ||
.map(|call| subst_call(&call, &func_name_to_body)) | ||
.collect::<Vec<_>>(); | ||
|
||
let mut all_inlining = prev_inlining.clone(); | ||
|
||
// Repeat! Get calls and subst for each new substituted body. | ||
for _ in 1..iterations { | ||
let next_inlining = prev_inlining | ||
.iter() | ||
.flat_map(|cb| get_calls(&cb.body)) | ||
.filter(|call| prev_calls.insert(Rc::as_ptr(call))) | ||
.map(|call| subst_call(&call, &func_name_to_body)) | ||
.collect::<Vec<_>>(); | ||
all_inlining.extend(next_inlining.clone()); | ||
prev_inlining = next_inlining; | ||
} | ||
|
||
all_inlining | ||
} |
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
Oops, something went wrong.
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.
This has a pretty confusing name- I think you should inline this function so it doesn't exist