Optimize register preservation finalization #808
Labels
optimization
An performance optimization issue.
register-machine
A work item for the register-machine engine.
In the new
wasmi
register-machine translation we have to preserve values oflocal.get x
registers if they are on the stack while an incominglocal.set x
orlocal.tee x
overwrites the value oflocal x
. In order to preserve the value of thelocal x
we have to perform a register preservation which allocates a register at the highest index in descending order.When translation of
wasmi
IR is finalized we then have to offset all those preservation register indices by the difference between the maximum non-preserved register index and the minimum preserved register index. Conservatively this operation needs to iterate over all generatedwasmi
IR instructions and for each check all their registers and potentially offset them if they are a preservation register. This might inspect tons ofwasmi
IR instructions that are not having any preservation registers.This is very costly!
Current Solution and Proposal
In order to offset the costs a bit we currently check at which instruction index we first see a register preservation if any and only start from there which can reduce the amount of iteration by a bit. However, a much better improvement was to only ever touch those instruction that actually need to be adjusted.
This information can probably be inferred because we know whenever the stack pops off such a preserved register during translation. We could notify the register preservation finalization routine that this event occurred to track down the instructions that actually are required to be adjusted.
Difficulties
The main difficulty with this optimization is to be sure that no instruction that needs adjustment is missed out this way. Inspecting instructions that are not required to be adjusted is never a problem disregarding performance.
An additional complexity is that some instructions (e.g. call instructions) span multiple
wasmi
instruction words and therefore none of those instruction words belonging to a parent instruction that needs to be adjusted must be missed out.Iteration over all instruction to adjust their registers if needed:
Where we know that we just popped a preserved register from the stack:
The text was updated successfully, but these errors were encountered: