Note: There are a few cases of duplicates in user autocompletion which are being worked on.

Add SIMD support for JavaScript

NEW
Unassigned

Status

()

Core
JavaScript Engine
4 years ago
4 months ago

People

(Reporter: Moh Haghighat, Unassigned)

Tracking

(Depends on: 5 bugs, Blocks: 1 bug, {feature})

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: [platform-rel-Games])

Attachments

(2 attachments, 1 obsolete attachment)

(Reporter)

Description

4 years ago
The purpose of this entry is discussing addition of SIMD support for JavaScript, tracking the progress towards that goal, and the related performance measurements. Comments, suggestions, and constructive critiques are all welcome.

Cheers!
-moh
dupe of bug 644389?

Comment 2

4 years ago
We have a prototype of SIMD vaddf and vmulf running in SpiderMonkey. The purpose of this prototype is to establish a base line and measure some estimates on performance gains we can obtain using SIMD for certain typed array operations. 

A native SIMD object that currently has two static methods, vaddf and vmulf, is added to SpiderMonkey. The operations are currently implemented as runtime calls in C/C++ using intrinsics. The implementation does not currently deal with the alignment of the data and also assumes that the array sizes are multiples of 4. Of course, we’ll take care of these as we make progress.

As the base, we use the following JavaScript code for float32 typed array addition:

// SIMD_JS.vaddf implementation
SIMD_JS.vaddf = function (dst, op1, op2) {
    for (var i = 0, n = dst.length; i < n; ++i) {
        dst[i] = op1[i] + op2[i];
    }
}

We compare the performance of the SIMD version against the above code. To make a fair comparison of the execution time of the SIMD version and the reference, we pre-allocate the destination array so we actually measure only the cost of the math operations and not the array allocation. Note that this is just for prototyping purposes and the final version could be in functional form,  that is vaddf would return the result. The new JavaScript SIMD capability is currently used as:

// SIMD.vaddf implementation
SIMD.vaddf (dst, op1, op2);

The JS test file we used to obtain our numbers as well as the patch to SpiderMonkey that creates the SIMD object and implements vaddf and vmulf are attached. Here are the performance results we measure on MacOS X running on Intel Ivy Bridge using SM JS shell with the patch applied to revision 137197:

+-------+------------+---------------+---------+
| aSize | SIMD.vaddf | SIMD_JS.vaddf | speedup |
| words | in ms      | in ms         |         |
|       |            |               |         |
+-------+------------+---------------+---------+
| 4     | 83         | 58            | 0.70    |
+-------+------------+---------------+---------+
| 8     | 75         | 65            | 0.87    |
+-------+------------+---------------+---------+
| 16    | 79         | 95            | 1.20    |
+-------+------------+---------------+---------+
| 32    | 81         | 138           | 1.70    |
+-------+------------+---------------+---------+
| 64    | 88         | 226           | 2.57    |
+-------+------------+---------------+---------+
| 128   | 104        | 398           | 3.83    |
+-------+------------+---------------+---------+
| 256   | 128        | 738           | 5.77    |
+-------+------------+---------------+---------+
| 512   | 167        | 1429          | 8.56    |
+-------+------------+---------------+---------+
| 1024  | 241        | 2801          | 11.62   |
+-------+------------+---------------+---------+
| 2048  | 395        | 5543          | 14.03   |
+-------+------------+---------------+---------+
| 4096  | 928        | 11040         | 11.90   |
+-------+------------+---------------+---------+
| 9192  | 2020       | 24621         | 12.19   |
+-------+------------+---------------+---------+

+-------+------------+---------------+---------+
| aSize | SIMD.vmulf | SIMD_JS.vmulf | speedup |
| words | in ms      | in ms         |         |
|       |            |               |         |
+-------+------------+---------------+---------+
| 4     | 83         | 62            | 0.75    |
+-------+------------+---------------+---------+
| 8     | 84         | 71            | 0.85    |
+-------+------------+---------------+---------+
| 16    | 85         | 102           | 1.20    |
+-------+------------+---------------+---------+
| 32    | 90         | 145           | 1.61    |
+-------+------------+---------------+---------+
| 64    | 94         | 231           | 2.46    |
+-------+------------+---------------+---------+
| 128   | 102        | 403           | 3.95    |
+-------+------------+---------------+---------+
| 256   | 127        | 743           | 5.85    |
+-------+------------+---------------+---------+
| 512   | 166        | 1428          | 8.60    |
+-------+------------+---------------+---------+
| 1024  | 240        | 2812          | 11.72   |
+-------+------------+---------------+---------+
| 2048  | 393        | 5547          | 14.11   |
+-------+------------+---------------+---------+
| 4096  | 938        | 11024         | 11.75   |
+-------+------------+---------------+---------+
| 9192  | 2039       | 24657         | 12.09   |
+-------+------------+---------------+---------+

As can be seen from the data points, the potential gains can be significant for large arrays.

Our next step is to make the JIT generate the SIMD instructions directly as opposed to calling functions that are implemented in intrinsics. Later on, we will take care of other aspects of the SIMD object  including proper handling of alignment and different array sizes.

Comment 3

4 years ago
Created attachment 776063 [details] [diff] [review]
Prototype Patch for SIMD.vaddf and SIMD.vmulf functions

Comment 4

4 years ago
Created attachment 776064 [details]
Test .js file for prototype patch for SIMD.vaddf and SIMD.vmulf
See John McCutchan's

http://wiki.ecmascript.org/doku.php?id=strawman:simd_number

and

https://github.com/johnmccutchan/ecmascript_simd

See also

https://github.com/jussi-kalliokoski/node-dsp

/be

Comment 6

4 years ago
Created attachment 778189 [details]
Test.js file for prototype patch for SIMD.vaddf and SIMD.vmulf

Original test file was for wrong version, re-attaching the right one.
Attachment #776064 - Attachment is obsolete: true

Updated

4 years ago
Depends on: 904913
Keywords: feature
Attachment #778189 - Attachment mime type: application/octet-stream → text/plain
Attachment #776063 - Attachment is patch: true
Comment on attachment 776063 [details] [diff] [review]
Prototype Patch for SIMD.vaddf and SIMD.vmulf functions

Review of attachment 776063 [details] [diff] [review]:
-----------------------------------------------------------------

::: js/src/jsSIMD.h
@@ +20,5 @@
> +
> +namespace js {
> +
> +extern JSBool
> +js_vaddf4(JSContext *cx, unsigned argc, Value *vp);

style nit: No need for a double prefix (js::js_…). js_ is the C namespace scheme.
Depends on: 992267
(Assignee)

Updated

3 years ago
Assignee: general → nobody
Depends on: 1064540

Updated

3 years ago
Blocks: 1107589
Depends on: 1132894
Depends on: 1123383
Depends on: 1160971
Depends on: 1110656
Depends on: 1112167
Depends on: 1183522
Duplicate of this bug: 644389
Depends on: 1204604
Depends on: 1233111
Depends on: 1201934
Depends on: 1240791
Depends on: 1244889
Depends on: 1252270
Depends on: 1256945
Whiteboard: [platform-rel-Games]
platform-rel: --- → ?
Depends on: 1313512
platform-rel: ? → ---
You need to log in before you can comment on or make changes to this bug.