Skip to content

Strengthen value range analysis in the compiler - #11038

Merged
bjorng merged 5 commits into
erlang:masterfrom
bjorng:bjorn/compiler/better-ranges/GH-8157/OTP-19831
May 20, 2026
Merged

Strengthen value range analysis in the compiler#11038
bjorng merged 5 commits into
erlang:masterfrom
bjorng:bjorn/compiler/better-ranges/GH-8157/OTP-19831

Conversation

@bjorng

@bjorng bjorng commented Apr 19, 2026

Copy link
Copy Markdown
Contributor

This pull request improves the value range analysis. It ensures that the original example in #8157 is properly optimized, and it also optimizes the fact function added by me to that issue. Also, the expression C bxor (B bor (bnot D)) (part of an MD5 calculation) will now have a range.

See the commit messages for more details.

This PR is meant for OTP 30, since we usually don't add new optimizations after RC1.

Resolves #8157

@bjorng bjorng added this to the 30.0 milestone Apr 19, 2026
@bjorng
bjorng requested review from jhogberg and lucioleKi April 19, 2026 06:16
@bjorng bjorng self-assigned this Apr 19, 2026
@bjorng bjorng added the team:VM Assigned to OTP team VM label Apr 19, 2026
@github-actions

github-actions Bot commented Apr 19, 2026

Copy link
Copy Markdown
Contributor

CT Test Results

    2 files    335 suites   9m 14s ⏱️
  874 tests   868 ✅ 6 💤 0 ❌
5 721 runs  5 715 ✅ 6 💤 0 ❌

Results for commit 88e4098.

♻️ This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

lucioleKi
lucioleKi previously approved these changes Apr 20, 2026
@bjorng
bjorng force-pushed the bjorn/compiler/better-ranges/GH-8157/OTP-19831 branch 4 times, most recently from 905050f to 714c7b4 Compare April 27, 2026 04:57
@bjorng
bjorng force-pushed the bjorn/compiler/better-ranges/GH-8157/OTP-19831 branch from 714c7b4 to 3b3c942 Compare April 29, 2026 08:35
bjorng added 2 commits May 13, 2026 12:45
The signatures pass is run once, doing a type analysis of all functions
in the module to compute argument types and return types for all functions.

This pass must be conservative regarding the range for certain
operators. Consider this example:

    fact(N) when is_integer(N, 0, 10_000) ->
        fact(N, 1).

    fact(0, P) -> P;
    fact(N, P) -> fact(N - 1, P * N).

To ensure convergence, the signatures pass will compute the range of
the result of `N - 1` to be '-inf' to 9999, and `P * N` to be 1 to
'+inf'.

This commit improves the signatures pass by attempting to use a
tighter range for the result of the `+`, `-`, `*`, and
`bnot` operators. If the value range analysis converges, the tighter
range is kept; otherwise, the more conservative range is used.

For the example, the range for `N` will now be 0 to 10000.

Solves erlang#8157
The `opt_ranges` subpass is no longer needed after the improvements
made to the signatures pass in the previous commit.
@bjorng
bjorng force-pushed the bjorn/compiler/better-ranges/GH-8157/OTP-19831 branch from 3b3c942 to ec77c9e Compare May 13, 2026 10:52
@bjorng bjorng added the testing currently being tested, tag is used by OTP internal CI label May 13, 2026
@bjorng
bjorng requested a review from lucioleKi May 19, 2026 07:42
Handle negative bounds for `bor` and `bxor`, and simplify handling of
`band`.

Also simplify all logical operators by canonicalizing the input
ranges: converting `any` to `{'-inf','+inf'} and replacing too large
integers with infinity. This reduces the number of special cases.

Thanks to Nelson Vides for the idea to replace large integers
with infinity.
@bjorng
bjorng force-pushed the bjorn/compiler/better-ranges/GH-8157/OTP-19831 branch from ec77c9e to eddb727 Compare May 19, 2026 11:50
Comment thread lib/compiler/src/beam_bounds.erl Outdated
inf_bnot(D), inf_bnot(C)),
{inf_bnot(Max),inf_bnot(Min)}.

-else.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you mean to make a separate commit in this PR or later?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Forgot that. I will do it in this PR.

@bjorng
bjorng force-pushed the bjorn/compiler/better-ranges/GH-8157/OTP-19831 branch from eddb727 to 88e4098 Compare May 20, 2026 08:23
@bjorng
bjorng requested a review from lucioleKi May 20, 2026 08:24
@bjorng
bjorng merged commit f261915 into erlang:master May 20, 2026
31 checks passed
@bjorng
bjorng deleted the bjorn/compiler/better-ranges/GH-8157/OTP-19831 branch May 20, 2026 10:40
bjorng added a commit to bjorng/otp that referenced this pull request Aug 13, 2026
The bounds for the `rem` operator is supposed to reach a fixpoint if
the result is fed back to one of its operands, but for some values of
its operands it didn't. This matters much more after erlang#11038.

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

Labels

team:VM Assigned to OTP team VM testing currently being tested, tag is used by OTP internal CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiler: Missed optimisation due to incorrect inferred type for pure integer subtraction

2 participants