Skip to content

Multi-value support? #1633

Closed
Closed

Description

The WebAssembly multi-value proposal has already been apart of the Wasm finished-proposals for some time, yet I can't find any mention of how to use it in ASC, although Binaryen supports it, and AS is supposed to allow low-level control over the output Wasm, if I'm not mistaken.

I had edited a simple Fibonnaci function implementation, into the following:

const SQRT5 = 2.23606797749979;
const C1 = 0.48121182505960347;
const C2 = -0.48121182505960336;

export function fib(n: f64): f64 {
  if (n <= 0) {
    return 0;
  }

  const temp = f64x2.mul(f64x2(C1, C2), f64x2.splat(n));

  const a = Math.exp( f64x2.extract_lane(temp, 0) );
  const b = Math.exp( f64x2.extract_lane(temp, 1) );

  return Math.round(
    (i32(n) & 1 ? a + b : a - b) * (1.0 / SQRT5)
  );
}

Regarding optimization, I checked the wat and noticed that the compiler was emitting two f64 locals, when it could've left the values on the stack, something more like the following:

local.get temp
f64x2.extract_lane 0
call Math.exp
local.get temp
f64x2.extract_lane 1
call Math.exp
local.get n
i32/f64
i32.const 1
i32.and
if (param f64 f64) (result f64)
    f64.add
else
    f64.sub
end
f64.const 1.0
global.get SQRT5
f64.div
f64.mul
call Math.round
return 

which should provide superior codegen in runtime VMs, while yielding size benefits.

There has been mention of waiting for Multi-value support for multiple things already, such as adding nullable types, among other things.

Is multi-value support is the plans already?

Activity

  1. MaxGraey commented on Jan 25, 2021

    @MaxGraey
    Member

    Is multi-value support is the plans already?

    Yes. Will be used for tuples and nullable primitive types like i32 | null. But currently this not implemented

    https://github.com/AssemblyScript/assemblyscript/wiki/Status-and-Roadmap

  2. MaxGraey commented on Jan 25, 2021

    @MaxGraey
    Member

    Tuples themselves don't need to use the multi-value proposal, they could be implemented more like how C variardic functions are

    tuples can't only pass as arguments but also should return as result as multiple values. C / C++ use shadow stack for that but Wasm hasn't access to shadow stack, so it usually emulate. But multi-values much more efficient. See this for comparision:
    https://godbolt.org/z/f9OIcq

  3. github-actions commented on Jun 2, 2021

    @github-actions

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!

  4. munrocket commented on Jan 19, 2022

    @munrocket

    I am currently working on AssemblyScript WebAPI bindgen (.idl -> externref) and interested in multi-value support. According to my old benchmark it was major performance pitfall in AssemblyScript (#997, #1560). Also it seems that it fit to idl-dictionaries because it is literally struct/tuple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions