Repository navigation
[12.x] Fold compare against 0 only where JS operators agree - #9401
Open
illusionalsagacity wants to merge 4 commits into
Open
illusionalsagacity wants to merge 4 commits into
illusionalsagacity wants to merge 4 commits into
Conversation
`compare(a, b) == 0`, `< 0` and `> 0` are folded to `a === b`, `a < b` and `a > b` for any type, not only ints, so structural comparison of records, tuples, arrays, options and NaN gives the wrong answer. Cause: compiler/backend/js_exp_make.ml:1539 (int_comp matches any runtime "compare") Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
int_comp rewrote any runtime call named "compare" compared with 0 into a direct JS comparison (compare(a, b) == 0 -> a === b, < 0 -> a < b, ...). The pattern ignored the runtime module, so Primitive_object.compare (structural) and Primitive_float.compare (NaN ordering) were folded too, giving wrong results for records, tuples, arrays, options and NaN. Only fold the compare of Primitive_int, Primitive_bool, Primitive_string and Primitive_bigint, whose results always agree with the JS operators. Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
int_comp folds a comparison of two int constants at compile time. It reads the left constant as unsigned and the right one as signed, so when the fold of compare(a, b) <op> 0 recursed into it with a negative left constant, that constant became a large positive number: compare(-1, -1) == 0 folded to false and compare(-5, 3) > 0 to true. Plain comparisons such as -1 == -1 are folded earlier and were not affected. The unsigned read cannot simply be dropped here as on master: is_out compares (e >>> 0) > range through int_comp and relies on it for a constant e (gpr_3877_test). So the compare fold now compares two int constants as signed itself, and the general case is left alone. Also correct two comments: the one on compare_agrees_with_js_operators now says where the folded and the runtime result can differ (values JS cannot order), and the one in j.ml no longer says that the module of a runtime function can be ignored when matching on its name. Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #9398 (item 1). Backport of the master fix to
maintenance/12.x; the bug is present in 12.3.1.Problem
Js_exp_make.int_comprewrites a runtimecomparecall that is compared with0into a direct JS comparison:compare(a, b) == 0becomesa === b,< 0becomesa < b, and so on. The pattern matched any runtime function namedcomparewithout looking at its module, so the structuralPrimitive_object.compareandPrimitive_float.comparewere folded as well:Before:
equal({x: 1}, {x: 1})returnsfalse,less([10], [9])returnstruebecause JS compares the arrays as strings, andnanEqual(nan, nan)returnsfalsealthoughcompare(nan, nan)is0.After:
Fix
The fold now applies only when the call is the
compareofPrimitive_int,Primitive_bool,Primitive_stringorPrimitive_bigint. Each of those is a total order that agrees with the JS<,===and>operators, so their output is unchanged.Primitive_float.compare(NaN) andPrimitive_object.compare(structural) are left as runtime calls.Restricting the fold to ints alone would also fix the bug; I kept the other three because the rewrite is exact for them. Happy to narrow it if you prefer.
Negative int constants
A review of this change found a second bug in the same fold.
int_compfolds a comparison of two int constants, reading the left one as unsigned and the right one as signed. When thecomparefold recursed into it with a negative left constant, the result was wrong:On 12.x the unsigned read is still needed:
is_outcompares(e >>> 0) > rangethroughint_compand relies on it wheneis a constant (gpr_3877_testfails without it). So here thecomparefold compares two int constants as signed itself and the general case is unchanged. On master, whereis_outis gone, the unsigned read is removed instead.Tests
tests/tests/src/compare_fold_test.rescompares records, tuples, arrays, options and NaN through an@inline(never) id, and checks that int, string, bool and bigint still fold, that negative int constants fold to the right result, and that ordinary floats compare correctly through the runtime. Without the compiler change its first three tests fail.Two existing outputs change:
test_list.mjs: the polymorphicmem,assoc,mem_assocandremove_assocnow callPrimitive_object.compare(...) === 0instead of===. They were affected by the same bug.caml_compare_bigint_test.mjs: that test types its values asfloat, so itscompareis the float one and is no longer folded.The fix was written by Claude and reviewed by me.