asterius icon indicating copy to clipboard operation
asterius copied to clipboard

Avoid using global `__asterius_pc` in the Cmm trampoline

Open TerrorJack opened this issue 5 years ago • 0 comments

Is your feature request related to a problem? Please describe. Our current solution to tail calls in Cmm can be summarized as:

  • All Cmm functions have [] -> [] signature, and Cmm calls become return_call/return_call_indirect in wasm at compile time
  • When the tail calls feature flag is on, return_call/return_call_indirect are translated per se
  • When the tail calls feature flag is off, we store the call destination address in __asterius_pc and return to the outer stgRun trampoline loop.

It is possible to avoid __asterius_pc usage and eliminate a pair of memory load/store in each Cmm function invocation. This may improve Cmm code runtime performance, and we shall investigate this possibility after #640 lands.

Describe the solution you'd like

  • [ ] Each Cmm function should now have [] -> i64 signature, returning the call destination address immediately.
  • [ ] The stgRun trampoline loop should use a wasm local to replace __asterius_pc.
  • [ ] Reserve a special wasm local for each Cmm function. Each Cmm function's code becomes a block containing two expressions: the original CFG, which stores the destination into that local, and a GetLocal which fetches that local as the function return value. This works around a restriction about the relooper: each basic block must have the [] -> [] stack type, not consuming or producing anything on the wasm value stack.

TerrorJack avatar May 16 '20 14:05 TerrorJack