Repository navigation
[JSC] DFG backwards propagation should not treat non-integer constants as within power of two (cherry-pick WebKit/WebKit#76149) - #778
Conversation
…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):
|
Status: ready for review. How this was reproduced (Release 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);
}
|
|
Preview build of 1a4965a: |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (2)
Included review availability: This review used your included allowance. Your plan provides up to 5 included reviews per hour; 1 remain after this review. WalkthroughThe 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. ChangesConstant range validation
Priority: ➖ Normal Merge Risk: ⚪ Minimal · up to No actionable issue remains in the reviewed change; it is mergeable after normal checks. 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
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 Comment |
|
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)); }
The same holds for |
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
(x + y + 0.5) | 0whenx + yleaves 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 markx + yas 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).| 0truncates toward zero: an added fraction then rounds the other way.JSTests/stress/backwards-propagation-add-with-non-integer-constant.js(from the upstream commit) fails onmainin all 15 JIT modes and passes in all 16.Background
| 0) can be computed with unchecked Int32 arithmetic.Downsides
(a + b + 0.5) | 0gets an overflow check ona + b: 320 to 352 bytes of DFG code, ns a call within noise (6.1, main 5.9 and 7.0).Notes
(x / y + 0.5) | 0and((x + y) / 2 + 0.5) | 0over Int32 operands: onmainthe 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 | 0at 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) | 0counts as a use as a number.(x + y + 0.5) | 0320 to 352,(0.5 + (x + y)) | 0320 to 352,(x - y + 0.5) | 0352,(x + y - 0.5) | 0352,(x * 3 + 0.5) | 0320,(-x + 0.5) | 0320,((x | 0) + 0.5) | 0320,(x + 0.5) | 0352,(x + y + 0.5) >>> 0352,(x + y + 1) | 0256,(x + y) | 0256,(x + y + 4294967296) | 0320,x + y + 0.5288,Math.floor(x + y + 0.5)352.run-javascriptcore-testsfiltered 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.