You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Certain instructions used in loops may be unnecessarily executed on each iteration of the loop. This can happen if the instruction only depends on other values used outside the loop. For example, the program:
The downside of this optimization is that if we apply this for blocks in general we could move instructions out of rarely-executed else blocks and into the happy path.
Another benefit of this optimization is that it allows more instructions to be deduplicated by constant folding since moving instructions to their dependency's common ancestor is much more permissive than checking if the first usage of an instruction dominates each other usage. For example, if we have the program if foo { ... }; if bar { ... }; if baz { ... }, each if body could be duplicated but none dominate each other since it is possible the else branch could have been taken each time. If we hoist to before the first if however, we'd deduplicate the blocks at the cost of executing extra instructions in the else case.
Since this could be a tradeoff in guessing how many times certain blocks may be executed, we could implement an alternate version of this change to start out with: only hoist if the instruction is used multiple times. That way, we could still execute unnecessary instructions but we'd at least guarantee to optimize out (deduplicate) 1 instance of the same instruction.
Workaround
None
Workaround Description
No response
Additional Context
No response
Project Impact
None
Blocker Context
No response
Would you like to submit a PR for this Issue?
None
Support Needs
No response
The text was updated successfully, but these errors were encountered:
jfecher
changed the title
Add SSA optimization pass to hoist instructions out of loops
Add SSA optimization pass to hoist instructions out of blocks to deduplicate them or hoist them out of loops
Nov 14, 2024
Problem
Certain instructions used in loops may be unnecessarily executed on each iteration of the loop. This can happen if the instruction only depends on other values used outside the loop. For example, the program:
Currently produces the SSA:
Happy Case
We can hoist instructions like
v6
out of their blocks to their earliest ancestor to get the SSA:The downside of this optimization is that if we apply this for blocks in general we could move instructions out of rarely-executed else blocks and into the happy path.
Another benefit of this optimization is that it allows more instructions to be deduplicated by constant folding since moving instructions to their dependency's common ancestor is much more permissive than checking if the first usage of an instruction dominates each other usage. For example, if we have the program
if foo { ... }; if bar { ... }; if baz { ... }
, each if body could be duplicated but none dominate each other since it is possible the else branch could have been taken each time. If we hoist to before the first if however, we'd deduplicate the blocks at the cost of executing extra instructions in the else case.Since this could be a tradeoff in guessing how many times certain blocks may be executed, we could implement an alternate version of this change to start out with: only hoist if the instruction is used multiple times. That way, we could still execute unnecessary instructions but we'd at least guarantee to optimize out (deduplicate) 1 instance of the same instruction.
Workaround
None
Workaround Description
No response
Additional Context
No response
Project Impact
None
Blocker Context
No response
Would you like to submit a PR for this Issue?
None
Support Needs
No response
The text was updated successfully, but these errors were encountered: