Skip to content

Improve code generation for the JIT - #7713

Merged
bjorng merged 25 commits into
erlang:masterfrom
bjorng:bjorn/jit/misc-improvements
Oct 13, 2023
Merged

bjorng merged 25 commits into
erlang:masterfrom
bjorng:bjorn/jit/misc-improvements

Conversation

@bjorng

@bjorng bjorng commented Oct 4, 2023

Copy link
Copy Markdown
Contributor

This pull request introduces many small improvements to the code generation for the JIT.

@bjorng bjorng added team:VM Assigned to OTP team VM enhancement testing currently being tested, tag is used by OTP internal CI labels Oct 4, 2023
@bjorng bjorng added this to the OTP-27.0 milestone Oct 4, 2023
@bjorng
bjorng requested a review from jhogberg October 4, 2023 05:16
@bjorng bjorng self-assigned this Oct 4, 2023
@github-actions

github-actions Bot commented Oct 4, 2023 •

Copy link
Copy Markdown
Contributor

CT Test Results

       3 files     140 suites   1h 0m 27s ⏱️
1 565 tests 1 512 ✔️ 52 💤 1 ❌
2 073 runs  2 000 ✔️ 72 💤 1 ❌

For more details on these failures, see this check.

Results for commit 7b28b97.

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

a.mov(dst_ptr, RET);
break;
}
for (j = i + 1; j < size && args[i] == args[j]; j++) {

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.

std::adjacent_find (inverted)?

Comment thread erts/emulator/beam/jit/arm/beam_asm.hpp Resolved
Comment thread erts/emulator/beam/jit/arm/beam_asm.hpp Resolved
@bjorng
bjorng force-pushed the bjorn/jit/misc-improvements branch from 3ae08fd to a3c4d54 Compare October 4, 2023 10:52
bjorng added 21 commits October 13, 2023 05:46
No BIFs have more than four arguments, so there is no need to
save and restore {x,4} and {x,5}.
When `try_end` with `deallocate` are combined the clearing of the
catch on the stack can be omitted. That eliminates one instruction
for x86_64 (9 bytes) and two instructions for AArch64 (8 bytes).

Also combine `try_end` followed by `move` and `deallocate` for AArch64.

These instruction combinations are used more than 800 times in the OTP
code base.
With the ancient introduction of literals, this test case no longer
builds anything, but simply moves a literal into a BEAM register:

    {move,{literal,[{<<>>,1}, . . ., {<<>>,1}]},{x,0}}.
The combination of `move`, `deallocate`, and `return` occurs about
1600 times in the OTP sources.

By combining those three BEAM instructions into one, the first three
native instructions of the combined instruction:

    ldr x25, [x20]
    add x20, x20, 8
    ldr x30, [x20], 8

can be combined to:

    ldp x25, x30, [x20], 16
Whenever possible, use SSE/AVX to copy two registers at once to the
fun environment.
We used to only combine `is_tuple` and `test_arity` instructions
having the same failure labels.
While at it, for AArch64 reduce tuple construction by one instruction
when the destination register is also a source register.
Improve reuse of registers by recognizing more neutral instructions
that will not force the cache to be discarded and by updating the
cache when reading from memory.

For the x86_64 JIT, with this commit 115697 loads of BEAM registers
are avoided, compared to 88786 loads before this commit.

For the AArch64 JIT, with this commit 14938 loads of BEAM registers
are avoided, compared to 14422 loads before this commit.
Inline the code for `element/2` when the first argument (the position)
is not a literal but the second argument is known to be a tuple. That
typically requires 13 native instructions.
There are more than 4700 occurrences of the following instruction
sequence in the OTP code base:

    ldr x25, [x20], 8
    ldr x30, [x20], 8

It can be simplified like so:

    ldp x25, x30, [x20], 16
* Replace the instructions `store_two_xregs` and `move_two_yregs` with
  the more general `store_two_values`, which also supports storing
  any value into Y registers.

* Replace an `init_yreg` instruction that initializes a single
  register with a `move` instruction. That give more opportunities for
  optimizations, such as combining it with another `move` instruction or
  with a `trim` instruction.
Because of the limited reach of branch instructions, stubs that
forward branches need to be emitted at regular intervals. Each stubs
section is always preceded by a `b` instruction skipping the section.

Frquently, this `b` instruction is unreachable (for example, if the
preceding instruction is a `ret` instruction).

By keeping track of whether the current position is unreachable,
unnecessary `b` instructions can be omitted. We can also emit stubs
sections somewhat earlier than strictly necessary if the current
position is unreachable. That will reduce the number of `b`
instructions that will need to be executed.
@bjorng
bjorng force-pushed the bjorn/jit/misc-improvements branch from 7b28b97 to 57bc32d Compare October 13, 2023 04:14
@bjorng
bjorng merged commit dc41c93 into erlang:master Oct 13, 2023
@bjorng
bjorng deleted the bjorn/jit/misc-improvements branch October 13, 2023 04:51
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.

2 participants