Closed Bug 934174 Opened 11 years ago Closed 11 years ago

Optimize the interpreter's int32 add

Categories

(Core :: JavaScript Engine, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla28

People

(Reporter: sunfish, Assigned: sunfish)

Details

(Whiteboard: [qa-])

Attachments

(1 file)

Attached patch interp-add.patchSplinter Review
This patch optimizes the int32 + int32 path to do an int32 add, to avoid going through floating-point in the common case.
Attachment #826365 - Flags: review?(luke)
Comment on attachment 826365 [details] [diff] [review]
interp-add.patch

Makes sense.
Attachment #826365 - Flags: review?(luke) → review+
As long as you're optimizing interp add and we have SafeAdd to abstract the grossness, you might consider trying alternative overflow tests:
  http://hg.mozilla.org/mozilla-central/rev/9c869e64ee26#l89.2964
I suppose it depends on how smart the compiler is about 64-bit int addition (my experience looking at GCC output a few years ago left me unimpressed).  (Yes, that code also has the signed-overflow UB you just reported on SafeAdd; uint32_t should work.)

Alternatively, I keep expecting there to be some compiler builtin
  bool __add_with_overflow(int32 lhs,int32 rhs,int32 *result)
that returns whether the addition overflowed so that we could take advantage of the overflow bit that the compiler already sets, but every time I go looking I can't find one.  Do you know of one?
s/the compiler already sets/the processor already sets/
(In reply to Luke Wagner [:luke] from comment #2)
> As long as you're optimizing interp add and we have SafeAdd to abstract the
> grossness, you might consider trying alternative overflow tests:
>   http://hg.mozilla.org/mozilla-central/rev/9c869e64ee26#l89.2964
> I suppose it depends on how smart the compiler is about 64-bit int addition
> (my experience looking at GCC output a few years ago left me unimpressed). 

Do you remember what sort of problems you saw?

> Alternatively, I keep expecting there to be some compiler builtin
>   bool __add_with_overflow(int32 lhs,int32 rhs,int32 *result)
> that returns whether the addition overflowed so that we could take advantage
> of the overflow bit that the compiler already sets, but every time I go
> looking I can't find one.  Do you know of one?

clang might have it; I know they have one for multiplication.  GCC does not.
(In reply to Nathan Froyd (:froydnj) from comment #4)
> Do you remember what sort of problems you saw?

IIRC, it was on x86 (x64 was fine, as you might hope :) and I just noticed a ton of what seemed like unnecessary spilling as the int64 values got moved around.
Also, and this might have been an issue with structs, not int64, but after the move to 64-bit js::Value (which was/is a 64-bit struct), we found more than a few perf regressions where the fix was to transform a loop of the form:

  for (int i = 0; i < N; i++)
    dst[i] = src[i]

into

  for (Value *dstp = dst, *srcp = src, *srcend = src + N; srcp != srcend; srcp++, dstp++)
    *dstp = *srcp;

because either the struct or 64-bit-ness was throwing off all the normal optimizations that usually allow us to write the former w/o loss of performance.
Yeah, clang does have builtin functions for these. And yeah, that thing with the xors looks like a pretty good alternative. Next time I take another look at interpreter performance, I'll take a look, though I don't know when that'll be.
https://hg.mozilla.org/mozilla-central/rev/d0cad525a6b4
Status: NEW → RESOLVED
Closed: 11 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla28
Whiteboard: [qa-]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: