forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move all cold code to the end of the function
Fixes rust-lang#836 Benchmark #1: simple-raytracer/raytracer_cg_clif Time (mean ± σ): 9.250 s ± 0.056 s [User: 9.213 s, System: 0.015 s] Range (min … max): 9.151 s … 9.348 s 20 runs Benchmark #2: simple-raytracer/raytracer_cg_clif_cold_separated Time (mean ± σ): 9.179 s ± 0.101 s [User: 9.141 s, System: 0.016 s] Range (min … max): 9.070 s … 9.473 s 20 runs Summary 'simple-raytracer/raytracer_cg_clif_cold_separated' ran 1.01 ± 0.01 times faster than 'simple-raytracer/raytracer_cg_clif'
- Loading branch information
Showing
7 changed files
with
56 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//! This optimization moves cold code to the end of the function. | ||
//! | ||
//! Some code is executed much less often than other code. For example panicking or the | ||
//! landingpads for unwinding. By moving this cold code to the end of the function the average | ||
//! amount of jumps is reduced and the code locality is improved. | ||
//! | ||
//! # Undefined behaviour | ||
//! | ||
//! This optimization doesn't assume anything that isn't already assumed by Cranelift itself. | ||
use crate::prelude::*; | ||
|
||
pub fn optimize_function(ctx: &mut Context, cold_ebbs: &EntitySet<Ebb>) { | ||
// FIXME Move the ebb in place instead of remove and append once | ||
// bytecodealliance/cranelift#1339 is implemented. | ||
|
||
let mut ebb_insts = HashMap::new(); | ||
for ebb in cold_ebbs.keys().filter(|&ebb| cold_ebbs.contains(ebb)) { | ||
let insts = ctx.func.layout.ebb_insts(ebb).collect::<Vec<_>>(); | ||
for &inst in &insts { | ||
ctx.func.layout.remove_inst(inst); | ||
} | ||
ebb_insts.insert(ebb, insts); | ||
ctx.func.layout.remove_ebb(ebb); | ||
} | ||
|
||
// And then append them at the back again. | ||
for ebb in cold_ebbs.keys().filter(|&ebb| cold_ebbs.contains(ebb)) { | ||
ctx.func.layout.append_ebb(ebb); | ||
for inst in ebb_insts.remove(&ebb).unwrap() { | ||
ctx.func.layout.append_inst(inst, ebb); | ||
} | ||
} | ||
} |
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