Bug 186177

Summary: [BigInt] Add support to BigInt into ValueAdd
Product: WebKit Reporter: Caio Lima <ticaiolima>
Component: JavaScriptCoreAssignee: Caio Lima <ticaiolima>
Status: RESOLVED FIXED    
Severity: Normal CC: caitp, cdumez, commit-queue, dominik.infuehr, ews-watchlist, guijemont, keith_miller, littledan, mark.lam, msaboff, rniwa, saam, webkit-bug-importer, xan.lopez, ysuzuki
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Bug Depends on:    
Bug Blocks: 186173, 186176    
Attachments:
Description Flags
WIP - RFC Patch
none
WIP - RFC Patch
none
Patch
none
Benchmarks
none
Patch
none
Patch
none
Patch
none
Benchmarks
none
Patch none

Description Caio Lima 2018-05-31 19:12:25 PDT
...
Comment 1 Caio Lima 2018-10-02 10:57:04 PDT
Created attachment 351418 [details]
WIP - RFC Patch
Comment 2 Caio Lima 2018-10-02 10:58:02 PDT
Comment on attachment 351418 [details]
WIP - RFC Patch

Patch not ready yet.
Comment 3 Caio Lima 2018-10-02 11:01:01 PDT
@dinfuehr, @caitp, @xan and @guijemont could you take a look in this Patch before sending to review?
Comment 4 Caio Lima 2018-10-06 06:07:39 PDT
Created attachment 351721 [details]
WIP - RFC Patch
Comment 5 Caio Lima 2018-10-09 13:49:13 PDT
Created attachment 351908 [details]
Patch
Comment 6 Caio Lima 2018-10-09 15:48:15 PDT
Created attachment 351921 [details]
Benchmarks

These changes are perf neutral.
Comment 7 Caio Lima 2018-10-12 08:44:23 PDT
Ping Review
Comment 8 Caio Lima 2018-10-15 09:24:08 PDT
Ping Review
Comment 9 Caio Lima 2018-10-17 04:53:36 PDT
Ping Review
Comment 10 Yusuke Suzuki 2018-10-17 05:48:01 PDT
Comment on attachment 351908 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=351908&action=review

I think our ArithProfile for `+` needs additional changes.

> Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h:594
> +        ASSERT(node->binaryUseKind() == UntypedUse || node->binaryUseKind() == BigIntUse);

Use DFG_ASSERT instead.

> Source/JavaScriptCore/dfg/DFGPredictionPropagationPhase.cpp:203
>                      changed |= mergePrediction(SpecInt32Only);
>                      if (node->mayHaveDoubleResult())
>                          changed |= mergePrediction(SpecBytecodeDouble);

Let's consider the polymorphic `+` site. It takes Number and BigInt. It can be like, `function add(a, b) { return a + b; }` and it is used in various places as a callback. Some functions pass Number, and some pass BigInt.
In this case, ArithProfile says "Result includes non number". And this prediction says "String | Number". But it is not correct in this case. It should say "Number | BigInt".
So, at least for `+`, I think ArithProfile needs to record additional result type information.
Comment 11 Caio Lima 2018-10-17 06:21:37 PDT
Comment on attachment 351908 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=351908&action=review

Thx for the review!

>> Source/JavaScriptCore/dfg/DFGPredictionPropagationPhase.cpp:203
>>                          changed |= mergePrediction(SpecBytecodeDouble);
> 
> Let's consider the polymorphic `+` site. It takes Number and BigInt. It can be like, `function add(a, b) { return a + b; }` and it is used in various places as a callback. Some functions pass Number, and some pass BigInt.
> In this case, ArithProfile says "Result includes non number". And this prediction says "String | Number". But it is not correct in this case. It should say "Number | BigInt".
> So, at least for `+`, I think ArithProfile needs to record additional result type information.

Oops. Sorry I missed that. Nice Catch.
Comment 12 Caio Lima 2018-10-17 14:23:25 PDT
Created attachment 352640 [details]
Patch
Comment 13 Caio Lima 2018-10-17 15:01:12 PDT
Created attachment 352648 [details]
Patch
Comment 14 EWS Watchlist 2018-10-17 18:22:22 PDT
Comment on attachment 352648 [details]
Patch

Attachment 352648 [details] did not pass jsc-ews (mac):
Output: https://webkit-queues.webkit.org/results/9645190

New failing tests:
stress/big-int-negate-jit.js.big-int-enabled
apiTests
Comment 15 Yusuke Suzuki 2018-10-18 06:38:39 PDT
Comment on attachment 352648 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=352648&action=review

Looks nice. But since I have found several issues, I would like to have another round of review.

> Source/JavaScriptCore/bytecode/ArithProfile.cpp:41
>      CCallHelpers::Jump isInt32 = jit.branchIfInt32(regs, mode);

`done.append(jit.branchIfInt32(regs, mode))` seems simple.

> Source/JavaScriptCore/bytecode/ArithProfile.cpp:47
> +    CCallHelpers::Jump notBigInt = jit.branchIfNotBigInt(regs.payloadGPR());

It assumes the regs are JSCell, but it seems flaky. Putting branchIfNotCell before this is better.
At that time, please pass `mode` to branchIfNotCell since we may not have tag register.

> Source/JavaScriptCore/bytecode/ArithProfile.h:274
>      void emitSetNonNumber(CCallHelpers&) const;

I think renaming this term "NonNumber" in ArithProfile, DFG::Node flags etc. to "NonNumeric" is better since Numeric can include BigInt.

> Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp:926
> +                        node->mergeFlags(NodeMayHaveBigIntResult);

I think ValueNegate and ArithNegate needs this flag too.

> Source/JavaScriptCore/dfg/DFGNodeFlags.h:75
> +#define NodeMayHaveBigIntResult         0x100000

I think this flag should not be sparse: this should be cooperating with the other flags like NodeMayHaveNonNumberResult, NodeMayHaveDoubleResult etc.
The value of this flag should be close to NodeMayHaveNonNumberResult. And NodeMayHaveNonIntResult should be updated since BigInt is NonInt result.

> Source/JavaScriptCore/dfg/DFGPredictionPropagationPhase.cpp:274
>                          // FIXME: We should add support to BigInt into speculatio
>                          // https://bugs.webkit.org/show_bug.cgi?id=182470

This can be removed.
Comment 16 Caio Lima 2018-10-21 09:16:09 PDT
Created attachment 352873 [details]
Patch
Comment 17 Caio Lima 2018-10-21 09:19:52 PDT
Comment on attachment 352648 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=352648&action=review

Thx for the review!

>> Source/JavaScriptCore/bytecode/ArithProfile.cpp:47
>> +    CCallHelpers::Jump notBigInt = jit.branchIfNotBigInt(regs.payloadGPR());
> 
> It assumes the regs are JSCell, but it seems flaky. Putting branchIfNotCell before this is better.
> At that time, please pass `mode` to branchIfNotCell since we may not have tag register.

Done. BTW, I'm trying to create a test for such case, but I'm stuck to find a case where this code is emitted. Do you know when we emit such code? I tried OSR code generated due BadType and Overflow and neither one triggers the generation of this code.

>> Source/JavaScriptCore/bytecode/ArithProfile.h:274
>>      void emitSetNonNumber(CCallHelpers&) const;
> 
> I think renaming this term "NonNumber" in ArithProfile, DFG::Node flags etc. to "NonNumeric" is better since Numeric can include BigInt.

I agree. Done.

>> Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp:926
>> +                        node->mergeFlags(NodeMayHaveBigIntResult);
> 
> I think ValueNegate and ArithNegate needs this flag too.

Done.

>> Source/JavaScriptCore/dfg/DFGNodeFlags.h:75
>> +#define NodeMayHaveBigIntResult         0x100000
> 
> I think this flag should not be sparse: this should be cooperating with the other flags like NodeMayHaveNonNumberResult, NodeMayHaveDoubleResult etc.
> The value of this flag should be close to NodeMayHaveNonNumberResult. And NodeMayHaveNonIntResult should be updated since BigInt is NonInt result.

Makes sense. I updated it.
Comment 18 Caio Lima 2018-10-21 11:58:36 PDT
Created attachment 352874 [details]
Benchmarks

The performance is perf neutral according to results.
Comment 19 Caio Lima 2018-11-01 10:47:59 PDT
Created attachment 353619 [details]
Patch
Comment 20 Keith Miller 2018-11-07 12:26:02 PST
Comment on attachment 353619 [details]
Patch

r=me.
Comment 21 Caio Lima 2018-11-07 17:34:49 PST
@Yusuke and  @Keith thank you very much for the review!
Comment 22 WebKit Commit Bot 2018-11-07 17:47:34 PST
Comment on attachment 353619 [details]
Patch

Clearing flags on attachment: 353619

Committed r237972: <https://trac.webkit.org/changeset/237972>
Comment 23 WebKit Commit Bot 2018-11-07 17:47:36 PST
All reviewed patches have been landed.  Closing bug.
Comment 24 Radar WebKit Bug Importer 2018-11-07 17:48:21 PST
<rdar://problem/45896441>