Skip to content

8379503: Intern constant expression of type String#978

Closed
mabbay wants to merge 28 commits intoopenjdk:code-reflectionfrom
mabbay:intern-string-constant-expr
Closed

8379503: Intern constant expression of type String#978
mabbay wants to merge 28 commits intoopenjdk:code-reflectionfrom
mabbay:intern-string-constant-expr

Conversation

@mabbay
Copy link
Copy Markdown
Member

@mabbay mabbay commented Mar 24, 2026

Implement constant folding in the model. An Operation that model a constant expression is replaced by a ConstantOp that has the value of the expression.
This transformation eliminate the need to explicitly intern constant expression of type String in the model, as these expressions are replaced by ConstantOps with String literals as values, which are interned naturally.



Progress

  • Change must not contain extraneous whitespace

Issue

  • JDK-8379503: Intern constant expression of type String (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 978

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/babylon/pull/978.diff

Using Webrev

Link to Webrev Comment

@mabbay mabbay requested a review from PaulSandoz March 24, 2026 21:42
@mabbay mabbay self-assigned this Mar 24, 2026
@bridgekeeper
Copy link
Copy Markdown

bridgekeeper Bot commented Mar 24, 2026

👋 Welcome back mabbay! A progress list of the required criteria for merging this PR into code-reflection 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
Copy Markdown

openjdk Bot commented Mar 24, 2026

@mabbay 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:

8379503: Intern constant expression of type String

Reviewed-by: psandoz, mcimadamore

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 2 new commits pushed to the code-reflection branch:

  • 774621b: [hat] Roll back tensors from the main branch
  • e12e6dc: Updates to specification and API

Please see this link for an up-to-date comparison between the source branch of this pull request and the code-reflection 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 code-reflection branch, type /integrate in a new comment.

@mabbay mabbay changed the title Intern constant expression of type String 8379503: Intern constant expression of type String Mar 24, 2026
@mabbay mabbay changed the title 8379503: Intern constant expression of type String Implement constant folding in the model Mar 30, 2026
Copy link
Copy Markdown
Member

@PaulSandoz PaulSandoz left a comment

Choose a reason for hiding this comment

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

The reuse of the evaluator is nice.

Using NonConstantExpression works well internally for evaluating one expression, but could be a performance issue when called many times when used for transformation. We could revisit later, at the expense of more complexity with value | error union result.

Comment thread test/jdk/jdk/incubator/code/TestEvaluation.java Outdated
Comment thread src/jdk.incubator.code/share/classes/jdk/incubator/code/dialect/java/JavaOp.java Outdated
Comment thread src/jdk.incubator.code/share/classes/jdk/incubator/code/CodeTransformer.java Outdated
Comment thread src/jdk.incubator.code/share/classes/jdk/incubator/code/dialect/java/JavaOp.java Outdated
@mabbay
Copy link
Copy Markdown
Member Author

mabbay commented Apr 13, 2026

The reuse of the evaluator is nice.

Using NonConstantExpression works well internally for evaluating one expression, but could be a performance issue when called many times when used for transformation. We could revisit later, at the expense of more complexity with value | error union result.

I logged an issue for this.

# Conflicts:
#	src/jdk.incubator.code/share/classes/jdk/incubator/code/dialect/java/JavaOp.java
@mabbay
Copy link
Copy Markdown
Member Author

mabbay commented Apr 14, 2026

/template append

@openjdk
Copy link
Copy Markdown

openjdk Bot commented Apr 14, 2026

@mabbay The pull request template has been appended to the pull request body

@mabbay mabbay marked this pull request as ready for review April 23, 2026 15:50
@openjdk openjdk Bot added ready Pull request is ready to be integrated rfr Pull request is ready for review labels Apr 23, 2026
@mlbridge
Copy link
Copy Markdown

mlbridge Bot commented Apr 23, 2026

Webrevs

@mabbay mabbay changed the title Implement constant folding in the model 8379503: Intern constant expression of type String Apr 23, 2026
}
Object v;
if ((field.getModifiers() & Modifier.STATIC) != 0) {
// @@@ why using field.get fails ?
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

did you try field::get(null) ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, but it throws IllegalAccessException

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We discussed this offline -- this failure is due to the fact that Field::get uses caller sensitive methods -- so access checks are relative to the place where Field::get is called -- in this case jdk.incubator.code.dialect.java. When using VarHandle however, access checks are defined in terms of a user-provided lookup object -- which makes this work more reliably. I'd suggest dropping the comment and maybe say that we need to use VH here.

throw new NonConstantExpression();
}
Object v;
if ((field.getModifiers() & Modifier.STATIC) != 0) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can't you check for STATIC in the above -- e.g. when you also check for FINAL ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The first checks are for constant variables (as per JLS). The second check is due to the limitation we have in the model, as we can't get the value of an instance field.

@mcimadamore
Copy link
Copy Markdown
Collaborator

The code seems to track JLS 15.29 quite well. Of course there's some limitations in the code model -- e.g. when it comes to variables, we don't know which variables were declared as "final" in the source code, or which also had an initializer. Maybe javac could leave an extra attribute there for a "constant field/var load" -- but not sure whether doing so would help the general issue.

@mabbay
Copy link
Copy Markdown
Member Author

mabbay commented Apr 27, 2026

The code seems to track JLS 15.29 quite well. Of course there's some limitations in the code model -- e.g. when it comes to variables, we don't know which variables were declared as "final" in the source code, or which also had an initializer. Maybe javac could leave an extra attribute there for a "constant field/var load" -- but not sure whether doing so would help the general issue.

We need a general solution to cover user built model

Copy link
Copy Markdown
Member

@PaulSandoz PaulSandoz left a comment

Choose a reason for hiding this comment

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

Very good.

Let's follow up in another PR with common patterns for constructing and using code transformers.

@mabbay
Copy link
Copy Markdown
Member Author

mabbay commented Apr 28, 2026

/integrate

@openjdk
Copy link
Copy Markdown

openjdk Bot commented Apr 28, 2026

Going to push as commit a7dc835.
Since your change was applied there have been 2 commits pushed to the code-reflection branch:

  • 774621b: [hat] Roll back tensors from the main branch
  • e12e6dc: Updates to specification and API

Your commit was automatically rebased without conflicts.

@openjdk openjdk Bot added the integrated Pull request has been integrated label Apr 28, 2026
@openjdk openjdk Bot closed this Apr 28, 2026
@openjdk openjdk Bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Apr 28, 2026
@openjdk
Copy link
Copy Markdown

openjdk Bot commented Apr 28, 2026

@mabbay Pushed as commit a7dc835.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

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

Labels

integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

3 participants