WebAssembly / binaryen

Optimizer and compiler/toolchain library for WebAssembly
Apache License 2.0
7.42k stars 734 forks source link

Remove dynCall thunks from wasm-emscripten #1466

Open NWilson opened 6 years ago

NWilson commented 6 years ago

I've been looking at how Emscripten post-processes the output of LLD in the current Wasm work. It looks like wasm-emscripten generates dynCall thunks for every function signature in the table? I can see why you want to do this - to allow callback-style programming.

But don't the Web APIs give you what you want already?

You'd need to do something like this:

var table = module.exports['__indirect_table'];
// Can then generate these on the fly in JS:
function dynCall_ii(fn, arg) {
  return table.get(fn)(arg);
}

I can think of three reasons perhaps why you don't do this already - but all of them are surmountable.

  1. Currently LLD doesn't export the indirect table! But that's easily fixed, you just have to ask.
  2. Are you worried about speed? I'd be surprised if doing table.get(fn) is really all that much slower than going via your dynCall thunk. It might be marginally quicker - did you test the two approaches?
  3. Are there startup ordering issues? Do you have to be able to do a dynCall from within startup code? But surely you can't get a handle to the dynCall stubs until after instantiation has completed, at which point the table should be available too.
kripken commented 6 years ago

I think this is an interesting idea, see previous discussion on the emscripten repo at https://github.com/kripken/emscripten/issues/6155

Overall, the main issue is with point 1: currently the table is neither imported nor exported, and that turns out to be good for VM optimizations as well as toolchain ones, see last comment in that link.