You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Is mul128 going to be as fast as widening multiplication? #11
Some background on why I think it will be useful: Widening multiplication 64 x 64 -> 128 is the main bottleneck in all algorithms related to elliptic curve-based cryptography. That's because elliptic curves are defined over finite fields of size (typically) ~256 bits.
256-bit elements are represented as [64 64 64 64], and multiplication (and modular reduction) of two such elements is a central algorithm which always ends up being the critical bottleneck in EC cryptography.
When multiplying two 64-bit limbs, we need to get both halves of the result, i.e. an i128.
The missing Wasm support of this operation so far has led to significant slowdown compared to native when naively compiling standard algorithms. (See e.g. some documentation of that in this write-up).
So, this proposal is promising to make Wasm implementation of EC-based crypto both simpler and faster.
My question is regarding the choice to expose i64.mul128 : [128 128] -> 128 instead of i64.mul_wide : [64 64] -> 128. This choice is discussed in the proposal under Alternative: Widening multiplication.
Quoting:
The i64.mul_wide_u instruction can be encoded as i64.mul128 with constant 0 values for the upper bits and i64.mul_wide_s can be encoded similarly with a right-shift of the low bits. This means that i64.mul128 should be sufficient for expressing the use cases of i64.mul_wide_{s,u}.
So, I wanted to confirm that emulating mul_wide with inputs to mul128 of the form [64 0], [64 0] is going to be compiled into instructions with no slow-down compared to a direct mul_wide instruction? (I'm not very familiar with compilers so please school me if this is obvious)
Clearly, for EC cryptography, only mul_wide is needed and not mul128 so having maximal speed for mul_wide-type use cases is important.
which modulo register moves and frame setup/teardown (none of that's relevant when this is all inlined elsewhere which these operators would be) is the same instructions.
So at the lowest level I can say that at least for Wasmtime the answer to this question is "yes the same instructions are generated". Now that's a pretty low-level approach though and doesn't necessarily prove that this is as fast as i64.mul_wide_{s,u}. For that a benchmark is probably best.
We've previously collected a simple benchmark which is cryptography-related but I believe it's RSA instead of EC. Despite that though today it's 627% slower than native at the same benchmark. With this proposal (i64.mul128) it's 78% slower than native.
Looking at profiles the generated instructions for multiplication look to be similar and I think the remaining slowdown is elsewhere but I'm having a tough time pinpointing what the remaining slowdown is. (I don't think it's mul-related though).
Perhaps the best wway to answer your question though is do you have some code you have benchmarked in the past or would like to see benchmarked? I'd love to have more data to help inform what the best instructions for this proposal are.
Some numbers in #6 (comment) surprised me where mul128 was slower than mul_wide_u. Upon investigating I think I'd like to shift this proposal to using i64.mul_wide_{u,s} instead of i64.mul128.
The basic problem was that i64.mul128 was selected as an instruction by LLVM even if the upper half of the result wasn't actually used. I believe that this all gets optimized away in native backends but such optimizations weren't in the wasm backend. This wasn't a problem with i64.mul_wide_{u,s} in the wasm backend though because I think that "fit better" with what LLVM already supports and it would naturally skip the i64.mul_wide_{u,s} if the high bits weren't necessary.
This is then additionally coupled with the fact that to achieve performance parity I've had to implement a number of pattern-matching optimizations such as:
For i64.mul128 if both operands are zero-extended from 64-bits then behave like i64.mul_wide_u
For i64.mul128 if both operands are sign-extended from 64-bits then behave like i64.mul_wide_s
If the upper bits of one operand is a constant 0 then you can constant fold some of the component operations away because multiplication by zero is zero.
I tried to implement an optimization in the LLVM lowering to use i64.mul instead of i64.mul128 when the upper bits weren't actually used, but while it worked for simple cases it didn't work in the benchmark I was looking at.
Overall this makes me lean in the direction of i64.mul128 is requiring too much work to retain parity with i64.mul_wide_{u,s}. At this point the main motivation for i64.mul128 was that it performed similarly and "felt cleaner" with the other 128-bit operations. The "performed similarly" point is now being proven wrong and I don't personally think the "feels cleaner" point continues to justify its existence.
Given all that I will likely make a PR to switch to i64.mul_wide_{u,s} instead of i64.mul128.
Activity
alexcrichton commentedon Aug 22, 2024
Hello! At a super low level I can confirm that this is true. For example this Rust code:
generates this on x86_64 today:
with a prototype version of this proposal it generates the wasm (edited to not return results through memory):
which Wasmtime then generates:
which modulo register moves and frame setup/teardown (none of that's relevant when this is all inlined elsewhere which these operators would be) is the same instructions.
So at the lowest level I can say that at least for Wasmtime the answer to this question is "yes the same instructions are generated". Now that's a pretty low-level approach though and doesn't necessarily prove that this is as fast as
i64.mul_wide_{s,u}. For that a benchmark is probably best.We've previously collected a simple benchmark which is cryptography-related but I believe it's RSA instead of EC. Despite that though today it's 627% slower than native at the same benchmark. With this proposal (
i64.mul128) it's 78% slower than native.Looking at profiles the generated instructions for multiplication look to be similar and I think the remaining slowdown is elsewhere but I'm having a tough time pinpointing what the remaining slowdown is. (I don't think it's mul-related though).
Perhaps the best wway to answer your question though is do you have some code you have benchmarked in the past or would like to see benchmarked? I'd love to have more data to help inform what the best instructions for this proposal are.
mitschabaude commentedon Aug 22, 2024
Awesome thanks, that's very promising!
I don't have a relevant benchmark at my fingertips but will come back with one when I get to it.
It will probably just be multiplying field elements or adding curve points with arkworks which is a pretty widely used library in my industry
alexcrichton commentedon Sep 4, 2024
Some numbers in #6 (comment) surprised me where mul128 was slower than mul_wide_u. Upon investigating I think I'd like to shift this proposal to using
i64.mul_wide_{u,s}instead ofi64.mul128.The basic problem was that
i64.mul128was selected as an instruction by LLVM even if the upper half of the result wasn't actually used. I believe that this all gets optimized away in native backends but such optimizations weren't in the wasm backend. This wasn't a problem withi64.mul_wide_{u,s}in the wasm backend though because I think that "fit better" with what LLVM already supports and it would naturally skip thei64.mul_wide_{u,s}if the high bits weren't necessary.This is then additionally coupled with the fact that to achieve performance parity I've had to implement a number of pattern-matching optimizations such as:
i64.mul128if both operands are zero-extended from 64-bits then behave likei64.mul_wide_ui64.mul128if both operands are sign-extended from 64-bits then behave likei64.mul_wide_sI tried to implement an optimization in the LLVM lowering to use
i64.mulinstead ofi64.mul128when the upper bits weren't actually used, but while it worked for simple cases it didn't work in the benchmark I was looking at.Overall this makes me lean in the direction of
i64.mul128is requiring too much work to retain parity withi64.mul_wide_{u,s}. At this point the main motivation fori64.mul128was that it performed similarly and "felt cleaner" with the other 128-bit operations. The "performed similarly" point is now being proven wrong and I don't personally think the "feels cleaner" point continues to justify its existence.Given all that I will likely make a PR to switch to
i64.mul_wide_{u,s}instead ofi64.mul128.Remove `i64.mul128`, add `i64.mul_wide_{s,u}`
alexcrichton commentedon Sep 5, 2024
I've opened #13 to replace
i64.mul128withi64.mul_wide_{s,u}, some explanation of what those instructions are, and additionally some extra rationale.