Skip to content

Optimizer miscompilations: 31 reproductions ranked by impact #9398

Open

Description

@illusionalsagacity

After #9317 and #9385 I had Claude read through the optimizer passes (Lambda and JS IR, pattern-match compilation, constant folding) looking for more cases where the optimized output disagrees with the unoptimized meaning of the program. Claude did the audit, wrote the reproductions and drafted this issue; I reviewed the results. This is the list of what could be reproduced, ranked by how likely an ordinary program is to hit each one and how silently it fails. I'd like to discuss which of these you consider bugs and in what order they are worth fixing; I'm happy to split out individual issues and send fixes.

Reproductions

Every entry has a failing test on a branch of my fork, one commit per bug:

https://github.com/illusionalsagacity/rescript-compiler/commits/optimizer-failing-tests

  • Based on master at 643e9c0. Each commit adds tests/tests/src/<name>_test.res and its generated .mjs; the commit message names the cause.
  • Most tests compare a constant expression the compiler can fold with the same expression passed through an @inline(never) identity function.
  • Each failing assertion has a // Actual: X, expected: Y comment above it.
  • Together the 31 files give 47 failing assertions.

Each entry says whether the same test also fails on maintenance/12.x. "Not testable" means the test uses syntax or a type that 12.x doesn't have.

Updates

23 November

Already tracked

I checked this list against the existing issues (all issue titles, keyword searches of issue and PR text, and the open PRs). Three items are already known:

I found no existing report for the other 28. Where an older issue is related, the entry links it under "Existing reports".

Tier 1: ordinary code, wrong result or crash

1. compare(a, b) == 0 (also < 0, > 0) is folded to a === b for every type, not only ints

  • How you'd hit it: Comparing records, tuples, arrays or options with compare
  • Effect: Equal values compare unequal; ordering is wrong. Silent.
  • 12.x: also fails
  • Test: compare_fold_test.res
  • Cause: js_exp_make.ml:1539 (int_comp)

2. A local binding shadows the root of a dotted @val external

3. A tagged template call whose result is unused is dropped

  • How you'd hit it: tag`...`->ignore, ignore(tag`...`) or let _ = tag`...`
  • Effect: The call never happens. Silent.
  • 12.x: not testable
  • Test: tagged_template_effect_test.res
  • Cause: js_analyzer.ml:150

4. Record rest rewrites later uses of the source record to the destructured locals

  • How you'd hit it: Calling a method on, or writing to, a record after let {a: _, ...rest} = r
  • Effect: The method loses this; the write goes to a local copy
  • 12.x: not applicable (new feature on master)
  • Test: record_rest_source_test.res
  • Cause: js_pass_record_rest.ml:32

Tier 2: ordinary code in a narrower shape, silent

5. Int * and ** lose low bits at runtime above 2^53, while the constant fold is exact

6. A function or functor body that is copied more than once runs the branch of another copy

  • How you'd hit it: An @inline function with an or-pattern switch or an | exception case, called twice; or a functor whose body has such a switch, applied twice (no @inline needed)
  • Effect: Wrong branch taken
  • 12.x: passes for the shapes I tried (regression on master)
  • Update: wider than first reported, which said it needs @inline. The functor form makes this tier 1.
  • Test: inline_static_exit_test.res
  • Cause: lam_bounded_vars.ml:96, lam_pass_exits.ml

7. The upper bound of a for loop is read again on every iteration

8. Float addition and subtraction of int literals wrap at int32

  • How you'd hit it: Float.fromInt of literals whose sum leaves the int range
  • Effect: Result off by 2^32
  • 12.x: also fails
  • Test: float_add_fold_test.res
  • Cause: js_exp_make.ml:1610

9. Record spread copies the base after evaluating the overrides

10. Closures created in a for loop share the loop variable

  • How you'd hit it: The loop bound is a statement-level try/switch
  • Effect: Every closure sees the final value
  • 12.x: also fails
  • Test: for_loop_closure_test.res
  • Cause: lam_compile.ml:1457

11. A dense int switch loses its default arm

  • How you'd hit it: A match with an unreachable case (already a warning)
  • Effect: undefined or the wrong arm
  • 12.x: not testable
  • Test: dense_switch_default_test.res
  • Cause: matching.ml:1893 (fail_rec)

12. A self tail call evaluates its arguments right to left

Tier 3: edge values or unusual code

All of these also fail on 12.x.

13. Float.fromInt(-1) == Float.fromInt(-1) folds to false

14. Float.toInt of an out-of-range constant saturates instead of wrapping, and depends on the build machine

15. A shift by 32 or more, or by a negative amount, folds to 0; the runtime result differs

16. >>> by a constant 32 or more gives a result outside the int range

17. @as(3000000000) is wrapped to a negative number, so no case matches

18. Int remainder can give -0

19. A unit argument of an external is evaluated before the arguments to its left

20. In target().a = { ...; v } the block runs before target()

21. isNullable / typeof of an array literal drops the side effects of its elements

22. A mutable field read is stale after a guard mutates it (arguably unspecified, but two shapes of the same match disagree)

23. An unused structural comparison of functions is removed, so the exception is never thrown

24. for i in a to x - 1 runs zero times when x - 1 wraps

25. typeof of a bigint literal folds to "number"

26. -0n == 0n folds to false

Tier 4: needs an unsafe cast, or only visible from JavaScript

All of these also fail on 12.x.

27. A switch on a constant string ignores merged arms

  • Why it ranks low: Only reached through a %identity string-to-variant cast; without the cast the constant folds earlier
  • Test: constant_string_switch_test.res
  • Cause: js_stmt_make.ml:132

28. Truthiness of "", 0.0, 0n and Some(None) is folded as true

  • Why it ranks low: Needs a %identity cast to bool
  • Test: truthiness_fold_test.res
  • Cause: lambda.ml:1230, js_exp_make.ml:1351

29. if a && b { true } else { false } is reduced to a && b

30. let rec values of a variant type lack TAG

31. Variants with constructors named None/Some ignore @as

Found but without a test

  • An unused dynamic import() is dropped. Importing a module only to preload it is a real pattern, so I'd put this in tier 2.
  • while true {} is deleted as pure. Tier 3.
  • The compiler hits an assert false on a switch over a constant string that matches no case. It needs the same cast as item 27, so tier 4.

Open questions

  • Item 5 (int multiplication): the runtime form seems to come from Change %mulint primitive behavior #7358. If the imprecision above 2^53 is intended, the constant fold should probably match it; otherwise the runtime should use Math.imul. Which is the intended behavior?
  • Item 11 (dense switch): the only reproduction found has an unreachable case. If one exists without a warning it belongs in tier 1.
  • Item 22 (mutable field after a guard): is the read order specified?
  • Tier 4: are results behind %identity casts in scope at all?

I'd suggest starting with items 1 and 2, since they affect released 12.x and need no unusual code, then item 6 as a recent regression on master that needs no annotation in its functor form.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions