MikeInnes / WebAssembly.jl

Other
83 stars 11 forks source link

Optimisation: Stack values #14

Closed sjorn3 closed 6 years ago

sjorn3 commented 6 years ago

A rather simple method for moving values to the stack if possible. All it does is check if the stack height doesn't change between a set and get, and if it doesn't then these can be safely removed. Unless the local is used again later, then the set can be removed and the get replaced with a tee (set+get). This handily cleans up the slightly annoying/common set_local get_local pairs.

Due to the way blocks work with the stack, essentially creating a new stack on entry, this can only be done with instructions at the same height. Blocks can return a single value which would allow applying this once on the way out of a block, but I've ignored that for the time being, and it might be best to do it separately. The restriction of 1 return from a block might be lifted in the future, which would make it easier to make use of.

Here is an example:

function addTwo(x, y)
  res = 10+x
  t = 10+x
  r = 431*y
  p = t*r
  q = p+2
  return res
end
f = (func $#addTwo_Int64_Int64 (param i64) (param i64) (result i64)
  (local i64) (local i64) (local i64) (local i64) (local i64)
  (i64.const 10)
  (get_local 0)
  (i64.add)
  (set_local 2)
  (i64.const 10)
  (get_local 0)
  (i64.add)
  (set_local 3)
  (i64.const 431)
  (get_local 1)
  (i64.mul)
  (set_local 4)
  (get_local 3)
  (get_local 4)
  (i64.mul)
  (set_local 5)
  (get_local 2)
  (return))

For the sake of cleaning up the registers I've passed it to allocate_registers, but all the structural changes are due to stack_locals. In this example all calls to set_local are removed (apart from the call to drop which is hiding an unused set_local).

(func $#addTwo_Int64_Int64 (param i64) (param i64) (result i64)
  (i64.const 10)
  (get_local 0)
  (i64.add)
  (i64.const 10)
  (get_local 0)
  (i64.add)
  (i64.const 431)
  (get_local 1)
  (i64.mul)
  (i64.mul)
  (drop)
  (return))

I plan to implement drop removal next which would reduce this particular example to the first 3 instructions.

sjorn3 commented 6 years ago

Contained in #19