Closed Bug 894105 Opened 11 years ago Closed 6 years ago

Add SIMD support for JavaScript

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED WONTFIX

People

(Reporter: mohammad.r.haghighat, Unassigned)

References

(Depends on 1 open bug)

Details

(Keywords: feature, Whiteboard: [platform-rel-Games])

Attachments

(2 files, 1 obsolete file)

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
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.
Original test file was for wrong version, re-attaching the right one.
Attachment #776064 - Attachment is obsolete: true
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.
Assignee: general → nobody
Depends on: 1064540
Depends on: 1132894
Whiteboard: [platform-rel-Games]
platform-rel: --- → ?
platform-rel: ? → ---
SIMD.js is being removed (bug 1416723). Instead, we'll likely implement the wasm SIMD proposal at some point in the future.
Status: NEW → RESOLVED
Closed: 6 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: