Open Robbepop opened 7 months ago
Having https://github.com/paritytech/wasmi/issues/924 implemented makes this optimization simpler to implement since we no longer have to differentiate between i32.add r c
and i32.sub r c
variants of the above instruction sequences.
Wasm producers such as LLVM often generate Wasm code with a so-called shadow stack in order to handle parameters and return values that did not fit the Wasm function parameter and result passing model.
Common sequences include:
and
as well as the counterpart
Where
$n
denotes a positivei32
integer literal and$v
denotes a local variable index. Currently Wasmi (register) translates this to roughly the following bytecode:However, with special treatment in the Wasmi translator and introduction of new Wasmi bytecode instructions we could easily translate the entire sequences above to a single instruction.
global.0.i32.sub_assign_imm
andglobal.0.i32.add_assign_imm
we can use the fact that sequences withi32.add
always have a negative constant immediate value and thus convert thei32.add
into ani32.sub
using a negated constant.It is to be expected that this optimization yields good results because:
spidermonkey.wat
orwestend.wat
(Polkadot runtime) we observed thousands of occurrences of the above Wasm instruction sequences.