Skip to content

Tweak kwargs logic to improve JIT inlining#8095

Merged
headius merged 4 commits intojruby:masterfrom
headius:inlinable_kwargs_jit
Feb 9, 2024
Merged

Tweak kwargs logic to improve JIT inlining#8095
headius merged 4 commits intojruby:masterfrom
headius:inlinable_kwargs_jit

Conversation

@headius
Copy link
Member

@headius headius commented Feb 8, 2024

A series of commits to make keyword argument logic more inlinable in JVM JIT compilers.

This method was often too big for JDK to inline, and performed
more boolean checks than necessary for the common paths. This
patch splits it up so it will be simpler and more inlinable.

* Split ruby2 kwarg logic into a separate path. It can change
  after method definition, and potentially after JIT, but it would
  be strange and buggy to not set it immediately. It can't be
  unset, so we assume if it is true at JIT time it will be true
  always.
* Fetch and compare relevant state only as-needed, rather than all
  ahead of time. This avoids extra fetches, branches, and
  comparisons that won't be used along common paths. Primarily
  this means deferring the bit checks of callInfo and the cast of
  the last argument to a RubyHash until needed.
@headius headius added this to the JRuby 9.4.6.0 milestone Feb 8, 2024
Most JVM JITs can trivially inline simple static methods, and
these calls are made frequently, so we flip them to static
versions.
@headius headius force-pushed the inlinable_kwargs_jit branch from 4769248 to f1472ff Compare February 8, 2024 20:18
* Split out ruby2_keywords path
* Create a simplified path for non-ruby2_keywords, non-rest, non-
  keyword receivers (most common no-op varargs case)
* Refactor full receiveKeywords for simplicity by inlining utility
  methods and reducing boolean logic as much as possible.

This commit loses some of the original comments during development
due to the difficulty of tracking them during refactoring, but new
comments have been strategically added to aid readability of the
new version.

The most simplified case described above should inline better, and
the complex path is at least reduced and perhaps easier to
optimize.
The ruby2_keywords method is used to switch a method or block to
use Ruby 2.x keyword argument behavior, based on a normal Hash
passed as the last argument. When this behavior changed for Ruby
3.0, ruby2_keywords was provided as a way to indicate the old
behavior is required for a given scope.

Recent optimizations to kwarg handling in JRuby assume that by the
time JIT has happened, the ruby2_keywords bit has already been set
or will never be set, so we can avoid checking it on each call.
This works fine for normal situations, but under the following
circumstance (found only in CRuby tests, so far), JIT may happen
before the call to ruby2_keywords, and the compiled code uses the
wrong (Ruby 3) logic:

* Scope 1 (a method or block) contains the definition of scope 2
  (another method or block).
* Scope 1 also calls ruby2_keywords to alter the kwarg behavior of
  scope 2.
* Scope 1 gets compiled immediately (no interpretation), which
  forces scope 2 to compile immediately *before* ruby2_keywords
  can be called.
* As a result scope 2 is pinned to Ruby 3 kwarg behavior and
  because immediately-compiled child scopes do not have a way to
  deopt back to interpreter, the ruby2_keywords behavior is never
  reflected.

This situation is extremely rare; only a few unusual cases can
trigger it:

* Whole-script AOT compilation, because the methods get compiled
  before executing code that calls ruby2_keywords.
* JIT with threshold=0 and method or block bodies that contain
  child scopes intended to use ruby2_keywords, as described above
  and found in CRuby tests.
* Code that defines methods, allows them to be called enough times
  for JIT, and some time after JIT attempts to set ruby2_keywords.

Because of the unusual circumstances that can cause problems, this
patch disables compilation for any scope that contains calls to
ruby2_keywords, which solves the first two scenarios. The third
scenario is not fixed by this, but is by far the most rare and
least concern of the three; if the method is being called before
ruby2_keywords is calls, then by definition it needs to work
properly with Ruby 3 kwarg logic, so the failure to switch to Ruby
2 kwarg logic should not be apparent.

A future update to JRuby may improve our ability to "back off"
from jitted code and recompile if ruby2_keywords is called.
@headius headius merged commit 709f4fe into jruby:master Feb 9, 2024
@headius headius deleted the inlinable_kwargs_jit branch February 9, 2024 19:04
headius added a commit to headius/jruby that referenced this pull request Apr 23, 2024
In jruby#8095 (jruby/jruby@121c60fb) I optimized the JIT to
statically compile in the logic for ruby2_keywords at the time of
JIT. Unfortunately there turned out to be too many cases where
this flag can change at runtime, leading to bugs like jruby#8119
where a previously-jitted scope gets flipped to ruby2_keywords
later on.

This patch partially reverts that optimization and resumes passing
in the live ruby2_keywords value from the scope. We can revisit
this in the future when it is possible to throw out and re-compile
scopes that get this flag set later on.

Fixes jruby#8119
headius added a commit that referenced this pull request Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant