forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a call-graph in the inliner (FuelLabs#3186)
This reduces the time spent in the inliner, in compiling the [`amm` library ](https://github.com/sway-libs/amm) from ~120s to ~5s. Code sizes before/after on [sway-applications](https://github.com/FuelLabs/sway-applications): ----------------------------------- benchmark | before | after ----------------|----------|-------- dao-voting | 20568 | 20496 escrow | 72740 | 72668 fundraiser | 31344 | 31440 multisig-wallet | 6592 | 7000 NFT | 23200 | 23200 oracle | 1924 | 1924 While this change doesn't touch the inlining heuristics, but only the order in which inlining of functions is undertaken, we have a heuristic that checks the size of the function being inlined. **That** depends on the order in which we inline, and hence the final set of inlining decisions aren't identical (though largely similar). Overall, I think the compile time benefits outweigh any differences in the inlining decisions.
- Loading branch information
1 parent
aff832c
commit 51987a9
Showing
5 changed files
with
105 additions
and
27 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
pub mod call_graph; | ||
pub use call_graph::*; | ||
pub mod dominator; | ||
pub use dominator::*; |
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,54 @@ | ||
/// Build call graphs for the program being compiled. | ||
/// If a function F1 calls function F2, then the call | ||
/// graph has an edge F1->F2. | ||
use crate::{Context, Function, Instruction, ValueDatum}; | ||
|
||
use rustc_hash::{FxHashMap, FxHashSet}; | ||
|
||
pub type CallGraph = FxHashMap<Function, FxHashSet<Function>>; | ||
|
||
/// Build call graph considering all providing functions. | ||
pub fn build_call_graph(ctx: &Context, functions: &[Function]) -> CallGraph { | ||
let mut res = CallGraph::default(); | ||
for function in functions { | ||
let entry = res.entry(*function); | ||
let entry = entry.or_insert_with(FxHashSet::default); | ||
for (_, inst) in function.instruction_iter(ctx) { | ||
if let ValueDatum::Instruction(Instruction::Call(callee, _)) = ctx.values[inst.0].value | ||
{ | ||
entry.insert(callee); | ||
} | ||
} | ||
} | ||
res | ||
} | ||
|
||
/// Given a call graph, return reverse topological sort | ||
/// (post order traversal), i.e., If A calls B, then B | ||
/// occurs before A in the returned Vec. | ||
pub fn callee_first_order(ctx: &Context, cg: &CallGraph) -> Vec<Function> { | ||
let mut res = Vec::new(); | ||
|
||
let mut visited = FxHashSet::<Function>::default(); | ||
fn post_order_visitor( | ||
ctx: &Context, | ||
cg: &CallGraph, | ||
visited: &mut FxHashSet<Function>, | ||
res: &mut Vec<Function>, | ||
node: Function, | ||
) { | ||
if visited.contains(&node) { | ||
return; | ||
} | ||
visited.insert(node); | ||
for callee in &cg[&node] { | ||
post_order_visitor(ctx, cg, visited, res, *callee); | ||
} | ||
res.push(node); | ||
} | ||
for node in cg.keys() { | ||
post_order_visitor(ctx, cg, &mut visited, &mut res, *node); | ||
} | ||
|
||
res | ||
} |
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