Skip to content

Conversation

iklam
Copy link
Member

@iklam iklam commented Sep 20, 2025

This is an alternative to #27024. Thanks to @ashu-mehra for the suggestion.

Background:

The AOT Assembly Phase is in essence a small Java program that executes a limited set of Java bytecodes. This program bootstraps the module system, loads classes, and performs certain ahead-of-time optimizations such as resolving invokedynamic call sites.

As a side effect of Java program execution, a small set of Java classes are initialized in the Assembly Phase.

Since JDK-8360163, if a class X is annotated with @AOTSafeClassInitializer and is initialized in the Assembly Phase, then X will be stored in the AOT cache in the "initialized" state. When the AOT cache is used in the Production Run, X::<clinit> will not be executed, and the static variables of X will be available upon JVM bootstrap.

Problem:

The Assembly Phase doesn't touch many classes that may benefit from @AOTSafeClassInitializer. For example, jdk.internal.math.MathUtils::<clinit> creates a few large tables. Caching MathUtils in the "initialized" state will improve start-up time. However, since no bytecodes executed by the Assembly Phase use MathUtils. it will not be initialized.

Fix:

If a class X has the @AOTSafeClassInitializer annotation and was initialized in the AOT Training Run, the JVM will proactively initialize X in the Assembly Phase. This will ensure that X will be cached in the "initialized" state.

As a proof of concept, @AOTSafeClassInitializer is added to MathUtils. @AOTSafeClassInitializer will be added to more classes in future RFEs.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8368174: Proactive initialization of @AOTSafeClassInitializer classes (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/27402/head:pull/27402
$ git checkout pull/27402

Update a local copy of the PR:
$ git checkout pull/27402
$ git pull https://git.openjdk.org/jdk.git pull/27402/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 27402

View PR using the GUI difftool:
$ git pr show -t 27402

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/27402.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 20, 2025

👋 Welcome back iklam! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Sep 20, 2025

@iklam This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8368174: Proactive initialization of @AOTSafeClassInitializer classes

Reviewed-by: adinn

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 36 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added hotspot-runtime hotspot-runtime-dev@openjdk.org core-libs core-libs-dev@openjdk.org labels Sep 20, 2025
@openjdk
Copy link

openjdk bot commented Sep 20, 2025

@iklam The following labels will be automatically applied to this pull request:

  • core-libs
  • hotspot-runtime

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@iklam iklam marked this pull request as ready for review September 21, 2025 00:12
@openjdk openjdk bot added the rfr Pull request is ready for review label Sep 21, 2025
@mlbridge
Copy link

mlbridge bot commented Sep 21, 2025

Webrevs

return supertype->has_aot_safe_initializer();
});
} else {
// @AOTRuntimeSetup only meaningful in @AOTClassInitializer
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// @AOTRuntimeSetup only meaningful in @AOTClassInitializer
// @AOTRuntimeSetup only meaningful in @AOTSafeClassInitializer

// constant pool entries that were resolved during the training run.
FinalImageRecipes::apply_recipes(CHECK);

// Because the AOT assembly phase does not run the exact code as in the
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Because the AOT assembly phase does not run the exact code as in the
// Because the AOT assembly phase does not run the exact same code as in the

Comment on lines +44 to +45
/// This is usually the result of performing AOT optimizations for the
/// `java.lang.invoke` package.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// This is usually the result of performing AOT optimizations for the
/// `java.lang.invoke` package.
/// At present this is usually the result of performing AOT optimizations for
/// the `java.lang.invoke` package but the set of classes which may be
/// pre-initialized via this annotation is not restricted to just that case.

Copy link
Contributor

@adinn adinn left a comment

Choose a reason for hiding this comment

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

This is much simpler and clearer than the previous version making it easier for non-Leyden devs to understand what they are buying into when they use the annotation. Nice work @ashu-mehra and @iklam.
I made a few suggestions to clarify comments which you are free to adopt or drop as you see fit. Otherwise looks good to go.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Sep 22, 2025
check_aot_annotations(ik);

if (!ik->is_initialized() && !ik->is_being_initialized()) {
if (ik->has_aot_safe_initializer()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Now that we are forcing initialization of classes annotated with AOTSafeClassInitializer, is it still possible that a class is not initialized but has_aot_safe_initializer() is true?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's possible in some rare circumstances. I.e, in the assembly phase, we take a path that was not cover in the training run (e.g. some sort of advanced JLI linkage). We could load a class X without initializing it. This could happen if X is used only for instanceof checking, or if it was loaded during verification of other classes.

Copy link
Member

@liach liach left a comment

Choose a reason for hiding this comment

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

Looks good otherwise.

Copy link
Member

Choose a reason for hiding this comment

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

Redundant change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-libs core-libs-dev@openjdk.org hotspot-runtime hotspot-runtime-dev@openjdk.org ready Pull request is ready to be integrated rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

4 participants