Skip to content

[JSC] DFG backwards propagation should not treat non-integer constants as within power of two (cherry-pick WebKit/WebKit#76149) - #778

Open
robobun wants to merge 1 commit into
mainfrom
robobun/7d53e19b/cherry-pick-webkit-76149
Open

robobun wants to merge 1 commit into
mainfrom
robobun/7d53e19b/cherry-pick-webkit-76149

Conversation

@robobun

@robobun robobun commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

Cherry-pick of WebKit#76149 (commit d823bc684f29, https://bugs.webkit.org/show_bug.cgi?id=326632), which is open upstream and not reviewed yet. It applies cleanly. Author and message are unchanged.

Problem

  • DFG and FTL code returns a wrong value for (x + y + 0.5) | 0 when x + y leaves the Int32 range: round(2000000000, 2000000000) gives -294967295, not -294967296.
  • isWithinPowerOfTwoForConstant() (DFGBackwardsPropagationPhase.cpp:159) accepts a constant that is not an integer. Then the outer add does not mark x + y as used as a number, so it becomes an unchecked Int32 add that wraps.

Fix

  • isWithinPowerOfTwoForConstant() accepts integers only (the one line of the upstream commit).
  • Correct because a wrapped sum can have the other sign, and | 0 truncates toward zero: an added fraction then rounds the other way.
  • Verified: JSTests/stress/backwards-propagation-add-with-non-integer-constant.js (from the upstream commit) fails on main in all 15 JIT modes and passes in all 16.

Background

  • BackwardsPropagationPhase records how each value is used. A value that is only truncated (| 0) can be computed with unchecked Int32 arithmetic.
  • An add with a small integer constant passes that freedom on to its other operand.

Downsides

  • (a + b + 0.5) | 0 gets an overflow check on a + b: 320 to 352 bytes of DFG code, ns a call within noise (6.1, main 5.9 and 7.0).
  • Of 16 functions that add a constant, 12 keep their DFG code size, 2 are this add, and 2 are divisions that now settle.
Notes
  • (x / y + 0.5) | 0 and ((x + y) / 2 + 0.5) | 0 over Int32 operands: on main the DFG compiles them again and again (7 compiles in 20,000 calls), because the division is checked but counts as "not used as a number". With this change the division is a double division from the first compile. A repro of 21 such shapes goes from 16 to 13 that never settle. A separate pull request handles the other 13.
  • ++v | 0 at INT32_MAX is a follow-up on top of this change (a separate pull request). An unchecked Inc is only correct once (++v + 0.5) | 0 counts as a use as a number.
  • The 16 functions (Int32 operands, 20,000 calls, no concurrent JIT, DFG code bytes on main and with the change): (x + y + 0.5) | 0 320 to 352, (0.5 + (x + y)) | 0 320 to 352, (x - y + 0.5) | 0 352, (x + y - 0.5) | 0 352, (x * 3 + 0.5) | 0 320, (-x + 0.5) | 0 320, ((x | 0) + 0.5) | 0 320, (x + 0.5) | 0 352, (x + y + 0.5) >>> 0 352, (x + y + 1) | 0 256, (x + y) | 0 256, (x + y + 4294967296) | 0 320, x + y + 0.5 288, Math.floor(x + y + 0.5) 352.
  • Also ran: run-javascriptcore-tests filtered to the tests whose names contain add, inc, dec, int32, overflow, negative-zero, round or trunc, on a build that has this change together with the two pull requests named above. 7,590 runs, 0 failures.

…s as within power of two

https://bugs.webkit.org/show_bug.cgi?id=326632

Reviewed by NOBODY (OOPS!).

When one operand of an addition is a constant whose absolute value is less
than 2^32, BackwardsPropagationPhase does not propagate NodeBytecodeUsesAsNumber
to the other operand, so the other operand can become an unchecked int32
addition. This relies on ToInt32(v + k * 2^32) == ToInt32(v), which only holds
when v is an integer. isWithinPowerOfTwoForConstant accepted non-integer
constants too, so DFG and FTL returned a result that is off by one when the
inner addition overflows:

    function round(x, y) { return (x + y + 0.5) | 0; }
    round(2000000000, 2000000000); // -294967295, should be -294967296

This patch makes isWithinPowerOfTwoForConstant accept integers only.

Test: JSTests/stress/backwards-propagation-add-with-non-integer-constant.js

* JSTests/stress/backwards-propagation-add-with-non-integer-constant.js: Added.
(shouldBe):
(addThenAddHalf):
(addThenAddNegativeHalf):
(subThenAddHalf):
(halfOnLeft):
(unsignedRightShift):
(bitAnd):
(threeOperands):
(negate):
(mulThenAddHalf):
(largeNegativeConstant):
(largePositiveConstant):
* Source/JavaScriptCore/dfg/DFGBackwardsPropagationPhase.cpp:
(JSC::DFG::BackwardsPropagationPhase::isWithinPowerOfTwoForConstant):
@robobun

robobun commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author

Status: ready for review.

How this was reproduced (Release jsc, main 7012d42e55)

function round(x, y) { return (x + y + 0.5) | 0; }
noInline(round);
for (let i = 0; i < 20000; ++i) {
    round(1, 2);
    let r = round(2000000000, 2000000000);
    if (r !== -294967296)
        throw new Error("call " + i + ": " + r);
}
  • On main this throws call 1514: -294967295 (the call number changes from run to run). It is the first call after the DFG compiled round. With this change it does not throw.
  • JSTests/stress/backwards-propagation-add-with-non-integer-constant.js (from the upstream commit) throws bad value: -294967295, expected: -294967296 on main in the 15 JIT modes of run-jsc-stress-tests. It passes in all 16 modes with this change.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review completed

Nothing new to post: everything this review found is already covered by existing comments on this pull request or didn't merit a separate one.

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown

Preview build of 1a4965a: autobuild-preview-pr-778-1a4965a8

@coderabbitai

coderabbitai Bot commented Oct 8, 2026

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Essentials
  • Run ID: 7e74fd1c-5918-4527-a342-395298d40620
📥 Commits

Reviewing files that changed from the base of the PR and between 7012d42 and 1a4965a.

📒 Files selected for processing (2)
  • JSTests/stress/backwards-propagation-add-with-non-integer-constant.js
  • Source/JavaScriptCore/dfg/DFGBackwardsPropagationPhase.cpp

Included review availability: This review used your included allowance. Your plan provides up to 5 included reviews per hour; 1 remain after this review.


Walkthrough

The DFG constant range check now requires values to be integers within the existing exclusive bounds. A new stress test checks arithmetic cases with fractional and large constants across repeated inputs.

Changes

Constant range validation

Layer / File(s) Summary
Require integer constants and test arithmetic results
Source/JavaScriptCore/dfg/DFGBackwardsPropagationPhase.cpp, JSTests/stress/backwards-propagation-add-with-non-integer-constant.js
The constant range check rejects fractional values. The stress test checks ten non-inlined arithmetic cases with fractional and large constants, including small and overflow-scale inputs.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to 1a496

No actionable issue remains in the reviewed change; it is mergeable after normal checks.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the DFG backwards-propagation fix for non-integer constants and accurately reflects the main change.
Description check ✅ Passed The description explains the bug, identifies the fix, links the Bugzilla issue, lists validation results, and provides relevant performance details. It does not use the exact template format or includ…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Git: CodeRabbit could not clone the repository, so clone-backed analysis was skipped and this review may be incomplete. Verify repository clone access, such as SSH credentials, before requesting another full review. If clone access is intentionally unavailable, use path_filters to narrow the review scope.


Comment @coderabbitai help to get the list of available commands.

@robobun

robobun commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator Author

This PR needs #792 under it.

The change here stops a recompile loop that, by accident, keeps some inlined sorts on the slow path. Without #792, those sorts then return a corrupted Int32 array:

function f(v, w, x, y) { return [v, w, x, y].sort((a, b) => ((a / 4 + 0.5) | 0) - ((b / 4 + 0.5) | 0)); }
build wrong results in 20,000 calls DFG compiles of f
main 7012d42 0 7
this branch (1a4965a) 19,901 1
this branch with the commit of #792 0 1

The same holds for / 3 in place of / 4. The defect itself is in the inlined sort and is on main today for other comparators. #792 has the details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants