Skip to content

Conversation

@bjorng
Copy link
Contributor

@bjorng bjorng commented Feb 7, 2022

Building on #5316 and #5664, this pull request annotates more arithmetic instructions with type information to allow the JIT to generate better code.

With this PR, the compiler and the JIT can now optimize a tuple used as a lookup table such as in the following example (similar to real code in the base64 module):

foo(N0) ->
    N1 = N0 band 3,
    N = N1 + 1,
    element(N, {zero,one,two,three}).

Because N is known to be an integer in the range 1 through 4, the JIT will omit all type and range checks and generate inlined code to fetch the Nth element of the tuple.

While at it, also add test cases for addition and multiplication.
The ssa_opt_float pass rewrites arithmetic instructions to
special float operations. During the rewrite, the `arg_types`
annotation was inherited in a random way. Be sure to assign
correct `arg_types` annotation for each new float instruction
created.
Optimize the use of a tuple as lookup table, such as in the following
example:

    element(N, {10, 11, 12, 13, 14, 15})
@bjorng bjorng added team:VM Assigned to OTP team VM enhancement testing currently being tested, tag is used by OTP internal CI labels Feb 7, 2022
@bjorng bjorng added this to the OTP-25.0 milestone Feb 7, 2022
@bjorng bjorng self-assigned this Feb 7, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Feb 7, 2022

CT Test Results

       4 files     358 suites   44m 6s ⏱️
1 965 tests 1 912 ✔️ 53 💤 0
5 454 runs  5 383 ✔️ 71 💤 0

Results for commit 46323c0.

♻️ 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

@bjorng bjorng requested review from garazdawi and jhogberg February 7, 2022 09:17
The main type analysis and optimizer sub pass iterates until a
fixpoint is reached. To ensure convergence, this sub pass is limited
in how it calculates ranges. It is only allowed to calculate a range
for operations that retain or shrink the ranges of their
operands. Examples of such operations are `band`, `bsr`, and `rem`. It
is not possible to calculate a range for operations such as `+' and
`bsl` that expand the ranges of their arguments.

This commit adds the `opt_range` sub pass that is run a single time
after type analysis pass. It will start from the types found in
annotations and propagate them forward through arithmetic instructions.

Here is an example:

    foo(N0) ->
        N1 = N0 band 3,
        N = N1 + 1,        % N1 is in 0..3
        element(N,         % N is in 1..4
            {zero,one,two,three}).

The range for `N1` was found by the main type pass. The range for
`N` was found by `opt_ranges`. Given a range for `N`, the JIT
will omit all range and type test for this call to `element/2`.
@bjorng bjorng force-pushed the bjorn/type-based-optimizations branch from becb62f to 9be154d Compare February 7, 2022 13:40
@bjorng bjorng force-pushed the bjorn/type-based-optimizations branch from 9be154d to 46323c0 Compare February 7, 2022 14:46
@bjorng bjorng force-pushed the bjorn/type-based-optimizations branch from 46323c0 to 382d84f Compare February 8, 2022 11:54
@bjorng bjorng merged commit d5713bf into erlang:master Feb 8, 2022
@bjorng bjorng deleted the bjorn/type-based-optimizations branch February 8, 2022 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement 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.

3 participants