Repository navigation
Avoid trimming spill ranges into deeper loops #272
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
Open
wraitii
wants to merge
1
commit into
bytecodealliance:main
Choose a base branch
from
wraitii:trim-loop-spill
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−0
Open
Changes from all commits
Commits
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| //! Values live across a call after a loop should spill at the call, not on | ||
| //! every iteration. Branch arguments are implicit uses that trimming must | ||
| //! account for when deciding where to end a register allocation. | ||
| use regalloc2::*; | ||
|
|
||
| struct LoopThenCall { | ||
| operands: Vec<Vec<Operand>>, | ||
| ranges: Vec<InstRange>, | ||
| preds: Vec<Vec<Block>>, | ||
| succs: Vec<Vec<Block>>, | ||
| params: Vec<Vec<VReg>>, | ||
| args: Vec<Vec<Vec<VReg>>>, | ||
| call: usize, | ||
| } | ||
|
|
||
| impl Function for LoopThenCall { | ||
| fn num_insts(&self) -> usize { | ||
| self.operands.len() | ||
| } | ||
| fn num_blocks(&self) -> usize { | ||
| self.ranges.len() | ||
| } | ||
| fn entry_block(&self) -> Block { | ||
| Block::new(0) | ||
| } | ||
| fn block_insns(&self, b: Block) -> InstRange { | ||
| self.ranges[b.index()] | ||
| } | ||
| fn block_preds(&self, b: Block) -> &[Block] { | ||
| &self.preds[b.index()] | ||
| } | ||
| fn block_succs(&self, b: Block) -> &[Block] { | ||
| &self.succs[b.index()] | ||
| } | ||
| fn block_params(&self, b: Block) -> &[VReg] { | ||
| &self.params[b.index()] | ||
| } | ||
| fn branch_blockparams(&self, b: Block, _: Inst, s: usize) -> &[VReg] { | ||
| &self.args[b.index()][s] | ||
| } | ||
| fn is_ret(&self, i: Inst) -> bool { | ||
| i.index() == self.num_insts() - 1 | ||
| } | ||
| fn is_branch(&self, i: Inst) -> bool { | ||
| !self.is_ret(i) && self.ranges.iter().any(|r| r.last() == i) | ||
| } | ||
| fn inst_operands(&self, i: Inst) -> &[Operand] { | ||
| &self.operands[i.index()] | ||
| } | ||
| fn inst_clobbers(&self, i: Inst) -> PRegSet { | ||
| if i.index() == self.call { | ||
| [PReg::new(0, RegClass::Float), PReg::new(1, RegClass::Float)] | ||
| .iter() | ||
| .copied() | ||
| .collect() | ||
| } else { | ||
| PRegSet::empty() | ||
| } | ||
| } | ||
| fn num_vregs(&self) -> usize { | ||
| 3 | ||
| } | ||
| fn spillslot_size(&self, _: RegClass) -> usize { | ||
| 1 | ||
| } | ||
| } | ||
|
|
||
| fn check_loop(threshold: bool) { | ||
| let v = |i| VReg::new(i, RegClass::Float); | ||
| let b = Block::new; | ||
| let r = |a, z| InstRange::new(Inst::new(a), Inst::new(z)); | ||
| let def = |i| Operand::reg_def(v(i)); | ||
| let use_ = |i| Operand::reg_use(v(i)); | ||
| let ret = Operand::reg_fixed_use( | ||
| v(if threshold { 2 } else { 1 }), | ||
| PReg::new(0, RegClass::Float), | ||
| ); | ||
| // Both layouts have entry -> header, a backedge block, and an exit | ||
| // containing a call and return. The threshold form updates in the header; | ||
| // the counted form updates only on the backedge. | ||
| let func = if threshold { | ||
| LoopThenCall { | ||
| operands: vec![ | ||
| vec![def(0)], | ||
| vec![], | ||
| vec![use_(1), def(2)], | ||
| vec![use_(2)], | ||
| vec![], | ||
| vec![], | ||
| vec![], | ||
| vec![ret], | ||
| ], | ||
| ranges: vec![r(0, 2), r(2, 5), r(5, 6), r(6, 8)], | ||
| preds: vec![vec![], vec![b(0), b(2)], vec![b(1)], vec![b(1)]], | ||
| succs: vec![vec![b(1)], vec![b(2), b(3)], vec![b(1)], vec![]], | ||
| params: vec![vec![], vec![v(1)], vec![], vec![]], | ||
| args: vec![ | ||
| vec![vec![v(0)]], | ||
| vec![vec![], vec![]], | ||
| vec![vec![v(2)]], | ||
| vec![], | ||
| ], | ||
| call: 6, | ||
| } | ||
| } else { | ||
| LoopThenCall { | ||
| operands: vec![ | ||
| vec![def(0)], | ||
| vec![], | ||
| vec![], | ||
| vec![use_(1), def(2)], | ||
| vec![], | ||
| vec![], | ||
| vec![ret], | ||
| ], | ||
| ranges: vec![r(0, 2), r(2, 3), r(3, 5), r(5, 7)], | ||
| preds: vec![vec![], vec![b(0), b(2)], vec![b(1)], vec![b(1)]], | ||
| succs: vec![vec![b(1)], vec![b(2), b(3)], vec![b(1)], vec![]], | ||
| params: vec![vec![], vec![v(1)], vec![], vec![]], | ||
| args: vec![ | ||
| vec![vec![v(0)]], | ||
| vec![vec![], vec![]], | ||
| vec![vec![v(2)]], | ||
| vec![], | ||
| ], | ||
| call: 5, | ||
| } | ||
| }; | ||
| let env = MachineEnv { | ||
| preferred_regs_by_class: [ | ||
| PRegSet::empty(), | ||
| [PReg::new(0, RegClass::Float), PReg::new(1, RegClass::Float)] | ||
| .iter() | ||
| .copied() | ||
| .collect(), | ||
| PRegSet::empty(), | ||
| ], | ||
| non_preferred_regs_by_class: [PRegSet::empty(); 3], | ||
| scratch_by_class: [None; 3], | ||
| fixed_stack_slots: vec![], | ||
| }; | ||
| let output = run( | ||
| &func, | ||
| &env, | ||
| &RegallocOptions { | ||
| validate_ssa: true, | ||
| ..RegallocOptions::default() | ||
| }, | ||
| ) | ||
| .unwrap(); | ||
| let mut checker = checker::Checker::new(&func, &env); | ||
| checker.prepare(&output); | ||
| checker.run().unwrap(); | ||
| let mut stores = 0; | ||
| let mut loads = 0; | ||
| for (point, edit) in &output.edits { | ||
| let &Edit::Move { from, to } = edit; | ||
| if from.is_stack() || to.is_stack() { | ||
| assert!( | ||
| point.inst().index() >= func.call, | ||
| "stack traffic in loop: {:?}: {:?}", | ||
| point, | ||
| edit | ||
| ); | ||
| stores += usize::from(to.is_stack()); | ||
| loads += usize::from(from.is_stack()); | ||
| } | ||
| } | ||
| assert_eq!((stores, loads), (1, 1)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn counted_loop_then_call() { | ||
| check_loop(false); | ||
| } | ||
|
|
||
| #[test] | ||
| fn threshold_loop_then_call() { | ||
| check_loop(true); | ||
| } | ||
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.
We don't have a test infrastructure like this intentionally -- this is a ton of boilerplate and also unnecessarily bakes in heuristic answers that may change in the future. Could you delete this test? We generally rely on perf tests to know if/when things regress, and aggressive fuzzing with the checker to know that we preserve correctness, otherwise let the heuristics do what they will.
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.
mh yeah in hindsight, should probably have figured that out. I'll drop the test.