Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDK-8273914: Indy string concat changes order of operations #5844

Closed
wants to merge 6 commits into from

Conversation

cushon
Copy link
Contributor

@cushon cushon commented Oct 6, 2021

This change makes string concatenation call toString on the arguments eagerly, to preserve the correct evaluation order of the arguments and the calls to toString.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed
  • Change requires a CSR request to be approved

Issues

  • JDK-8273914: Indy string concat changes order of operations
  • JDK-8274863: Indy string concat changes order of operations (CSR)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/5844/head:pull/5844
$ git checkout pull/5844

Update a local copy of the PR:
$ git checkout pull/5844
$ git pull https://git.openjdk.java.net/jdk pull/5844/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 5844

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/5844.diff

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented Oct 6, 2021

👋 Welcome back cushon! 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 openjdk bot added the rfr label Oct 6, 2021
@openjdk
Copy link

@openjdk openjdk bot commented Oct 6, 2021

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

  • compiler
  • 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.

@openjdk openjdk bot added hotspot-runtime compiler labels Oct 6, 2021
@mlbridge
Copy link

@mlbridge mlbridge bot commented Oct 6, 2021

Webrevs

* Returns true if the argument should be converted to a string eagerly, to preserve
* possible side-effects.
*/
protected boolean shouldConvertToStringEagerly(Type argType) {
Copy link
Member

@forax forax Oct 6, 2021

Choose a reason for hiding this comment

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

For me the implementation should be neither a primitive type nor a final class of java.lang. I think that at least the wrappers should not be converted eagerly

Copy link
Contributor Author

@cushon cushon Oct 6, 2021

Choose a reason for hiding this comment

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

@forax can you expand on the suggestion here?

The current implementation is not eagerly converting boxes for primitives types, which wrappers should not be converted eagerly?

Also note that one of the motivating examples was StringBuilder, which is a final class in java.lang. It's not just about toString() not having side-effects, it's also about insulting the operands from each others's side effects (e.g. myStringBuilder.append("foo") + myStringBuilder.append("bar"))

Copy link
Member

@forax forax Oct 7, 2021

Choose a reason for hiding this comment

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

The current implementation is not eagerly converting boxes for primitives types, which wrappers should not be converted eagerly?

I was just thinking that not calling Boolean or Double.toString() explicitly was Ok

myStringBuilder.append("foo") + myStringBuilder.append("bar")

Aaaah, so only primitives + their boxes should not be evaluated eagerly.

@jddarcy
Copy link
Member

@jddarcy jddarcy commented Oct 6, 2021

/csr needed

@jddarcy
Copy link
Member

@jddarcy jddarcy commented Oct 6, 2021

Please file a CSR for this issues to discuss the behavioral changes, even if to fix a bug.

@openjdk openjdk bot added the csr label Oct 6, 2021
@openjdk
Copy link

@openjdk openjdk bot commented Oct 6, 2021

@jddarcy has indicated that a compatibility and specification (CSR) request is needed for this pull request.
@cushon please create a CSR request for issue JDK-8273914. This pull request cannot be integrated until the CSR request is approved.

@cushon cushon changed the title JDK-8273914: Indy string concat changes order of operations. JDK-8273914: Indy string concat changes order of operations Oct 6, 2021
@cushon
Copy link
Contributor Author

@cushon cushon commented Oct 6, 2021

@jddarcy thanks, I have opened a CSR: https://bugs.openjdk.java.net/browse/JDK-8274863

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Can you explain the changes made in relation to the runtime test:

test/hotspot/jtreg/runtime/modules/AccessCheck/MethodAccessReadTwice.java

please. The connection to this fix is not at all apparent, nor what the conversion to jasm is achieving.

Thanks,
David

Copy link
Contributor

@shipilev shipilev left a comment

Thank you for fixing this. It looks nominally fine, but I would defer to javac experts to approve this. Minor testing suggestions below.

* @test
* @bug 8273914
* @summary Indy string concat changes order of operations
*
Copy link
Contributor

@shipilev shipilev Oct 7, 2021

Choose a reason for hiding this comment

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

Here and later: please add the test block with -XDstringConcat=inline here as well, so that test would verify that every javac strategy produces the same result?

Copy link
Contributor Author

@cushon cushon Oct 7, 2021

Choose a reason for hiding this comment

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

Done, thanks

Copy link
Contributor Author

@cushon cushon Oct 7, 2021

Choose a reason for hiding this comment

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

(The WellKnownTypes test still only exercises the indy strategies, because it's testing logic that only exists in those strategies.)

Copy link
Contributor

@shipilev shipilev Oct 12, 2021

Choose a reason for hiding this comment

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

But still, we probably want to confirm that inline strategy yields the same answer in WellKnownTypes? That's my thinking why to test inline: that is a baseline.

Copy link
Contributor Author

@cushon cushon Oct 12, 2021

Choose a reason for hiding this comment

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

I agree in general about using inline as a baseline, but WellKnownTypes doesn't currently check the output of string concatenation, it checks the signature of the invokedynamic it find in the test case.

What do you have in mind for inline here? Should I update the test to assert on the output of string concatenation (and tolerate not being able to find an invokedynamic, which could make the coverage for the indy strategies more fragile)? Or add a different test with similar inputs, and check the output of string concatenation on primitives and boxes?

I think it might make sense to rely on other tests to ensure inline and the indy strategies produce the same results, and use WellKnownTypes as a test just for shouldConvertToStringEagerly, which is only used by the indy strategies.

Copy link
Contributor

@shipilev shipilev Oct 14, 2021

Choose a reason for hiding this comment

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

Ah, I missed that WellKnownTypes does not verify concat output. Yes, that resolves my concern, we don't need to handle inline there. Maybe rename WellKnownTypes to WellKnownTypeSignatures or something?

But yes, I think adding another test that verifies the "special" input types produce consistent results across all strategies would be useful.

Copy link
Contributor Author

@cushon cushon Oct 14, 2021

Choose a reason for hiding this comment

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

Sure, thanks--I renamed WellKnownTypes to WellKnownTypeSignatures, and added a new WellKnownTypes that tests the actual concat behaviour on primitives for all of the strategies.

Copy link
Contributor

@shipilev shipilev Oct 15, 2021

Choose a reason for hiding this comment

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

Thank you, the last change resolves my comments.

@cushon
Copy link
Contributor Author

@cushon cushon commented Oct 7, 2021

@dholmes-ora

Can you explain the changes made in relation to the runtime test:

test/hotspot/jtreg/runtime/modules/AccessCheck/MethodAccessReadTwice.java

please. The connection to this fix is not at all apparent, nor what the conversion to jasm is achieving.

Thanks, good question:

That test is for JDK-8174954, which checks for an expected IllegalAccessError when the parameter type of an invokedynamic is inaccessible. It's assuming that given "" + param, javac generates an invokedynamic that uses the specific type of param. This change make javac eagerly convert param to a String before passing it to the invokedynamic call, which avoids the accessibility issue the test is trying to exercise.

Using jasm preserves the existing bytecode (including the signature of the invokedynamic) that the test is expecting.

@mlbridge
Copy link

@mlbridge mlbridge bot commented Oct 8, 2021

Mailing list message from David Holmes on hotspot-runtime-dev:

On 8/10/2021 6:02 am, Liam Miller-Cushon wrote:

On Thu, 7 Oct 2021 01:20:04 GMT, David Holmes <dholmes at openjdk.org> wrote:

Liam Miller-Cushon has updated the pull request incrementally with two additional commits since the last revision:

- Also test evaluation order for 'inline'
- Remove sharpestAccessible logic, since types are now passed as strings

Can you explain the changes made in relation to the runtime test:

test/hotspot/jtreg/runtime/modules/AccessCheck/MethodAccessReadTwice.java

please. The connection to this fix is not at all apparent, nor what the conversion to jasm is achieving.

Thanks,
David

@dholmes-ora

Can you explain the changes made in relation to the runtime test:

test/hotspot/jtreg/runtime/modules/AccessCheck/MethodAccessReadTwice.java

please. The connection to this fix is not at all apparent, nor what the conversion to jasm is achieving.

Thanks, good question:

That test is for [JDK-8174954](https://bugs.openjdk.java.net/browse/JDK-8174954), which checks for an expected `IllegalAccessError` when the parameter type of an invokedynamic is inaccessible. It's assuming that given `"" + param`, javac generates an invokedynamic that uses the specific type of `param`. This change make javac eagerly convert `param` to a `String` before passing it to the `invokedynamic` call, which avoids the accessibility issue the test is trying to exercise.

Using `jasm` preserves the existing bytecode (including the signature of the `invokedynamic`) that the test is expecting.

Okay, can you please add a comment to the jasm file that explains that.
It is useful to know what Java code a jasm file represents and where/why
it differs from how that Java code would compile.

Thanks,
David
-----

@cushon
Copy link
Contributor Author

@cushon cushon commented Oct 8, 2021

Okay, can you please add a comment to the jasm file that explains that.

Done

@shipilev
Copy link
Contributor

@shipilev shipilev commented Oct 12, 2021

@lahodaj, want to review the compiler change?

@lahodaj
Copy link
Contributor

@lahodaj lahodaj commented Oct 12, 2021

How about code like:

        StringBuilder builder2 = new StringBuilder("foo");
        Object oo = builder2;
        oo += "" + builder2.append("bar"); 

Should that be covered as well? From looking at the patch (not really trying it), it does not seem to be covered?

@shipilev
Copy link
Contributor

@shipilev shipilev commented Oct 12, 2021

        StringBuilder builder2 = new StringBuilder("foo");
        Object oo = builder2;
        oo += "" + builder2.append("bar"); 

Should that be covered as well? From looking at the patch (not really trying it), it does not seem to be covered?

Tried it, still not correct:

$ cat Concat.java 
public class Concat {
	public static void main(String... args) {
	        StringBuilder builder2 = new StringBuilder("foo");
	        Object oo = builder2;
        	oo += "" + builder2.append("bar");
		System.out.println(oo);
	}
}

$ build/linux-x86_64-server-fastdebug/images/jdk/bin/javac Concat.java
$ build/linux-x86_64-server-fastdebug/images/jdk/bin/java Concat
foobarfoobar

I believe if (shouldConvertToStringEagerly(argType)) branch should be handled for first argument as well, i.e. code should be shaped as:

if (!first || generateFirstArg) {
    genExpr(arg, arg.type).load();
}
if (shouldConvertToStringEagerly(argType)) {
    gen.callMethod(pos, syms.stringType, names.valueOf, List.of(syms.objectType), true);
    argType = syms.stringType;
}
dynamicArgs.add(argType);

This produces the correct result:

$ build/linux-x86_64-server-fastdebug/images/jdk/bin/java Concat
foofoobar

@cushon, could you do this change and add a relevant test case?

@cushon
Copy link
Contributor Author

@cushon cushon commented Oct 12, 2021

        StringBuilder builder2 = new StringBuilder("foo");
        Object oo = builder2;
        oo += "" + builder2.append("bar"); 

Should that be covered as well? From looking at the patch (not really trying it), it does not seem to be covered?

Tried it, still not correct:

I believe if (shouldConvertToStringEagerly(argType)) branch should be handled for first argument as well, i.e. code should be shaped as:

@cushon, could you do this change and add a relevant test case?

Fixed, thanks!

and add more functional test coverage for string concat of well-known
types, covering all strategies.
Copy link
Contributor

@shipilev shipilev left a comment

Current change passes tier{1,2,3,4} for me. Still, I think compiler folks have to approve the javac change. @lahodaj, could you do it, or ask somebody else?

@cushon
Copy link
Contributor Author

@cushon cushon commented Oct 18, 2021

@shipilev this is also blocked on the CSR, I think the next step is for it to be 'reviewed by at least one engineer familiar with that technology area', is that something you'd be able to help with? https://bugs.openjdk.java.net/browse/JDK-8274863

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented Nov 15, 2021

@cushon This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@cushon
Copy link
Contributor Author

@cushon cushon commented Nov 15, 2021

This pull request has been inactive for more than 4 weeks

The CSR is still in progress

@openjdk openjdk bot removed the csr label Dec 7, 2021
@cushon
Copy link
Contributor Author

@cushon cushon commented Dec 7, 2021

The CSR has been approved for 19, this is ready for review pending integration after the JDK 18 branch is cut.

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented Jan 5, 2022

@cushon This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@cushon
Copy link
Contributor Author

@cushon cushon commented Jan 7, 2022

@lahodaj would you be willing to review for the compiler group?

Copy link
Contributor

@vicente-romero-oracle vicente-romero-oracle left a comment

the compiler changes look good to me

@openjdk
Copy link

@openjdk openjdk bot commented Jan 7, 2022

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

8273914: Indy string concat changes order of operations

Reviewed-by: vromero, jlahoda

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 1099 new commits pushed to the master branch:

  • 4243f4c: 8279540: Shenandoah: Should only clear CLD::_claim_strong mark for strong CLD iterations
  • 62d03c2: 8279547: [vectorapi] Enable vector cast tests after JDK-8278948
  • 6a42fba: 8279258: Auto-vectorization enhancement for two-dimensional array operations
  • 8d0f385: 8279520: SPNEGO has not passed channel binding info into the underlying mechanism
  • b3dbfc6: 4884570: StreamPrintService.isAttributeValueSupported does not work properly for SheetCollate
  • 8703f14: 8273322: Enhance macro logic optimization for masked logic operations.
  • bc12381: 8279505: Update documentation for RETRY_COUNT and REPEAT_COUNT
  • 2dbb936: 8279339: (ch) Input/Output streams returned by Channels factory methods don't support concurrent read/write ops
  • 456bd1e: 8211004: javac is complaining about non-denotable types and refusing to generate the class file
  • 844dfb3: Merge
  • ... and 1089 more: https://git.openjdk.java.net/jdk/compare/c10de3538b47c182d7bfeb02f348fac9b2ad0641...master

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 the ready label Jan 7, 2022
lahodaj
lahodaj approved these changes Jan 7, 2022
Copy link
Contributor

@lahodaj lahodaj left a comment

Looks reasonable.

@cushon
Copy link
Contributor Author

@cushon cushon commented Jan 11, 2022

/integrate

@openjdk
Copy link

@openjdk openjdk bot commented Jan 11, 2022

Going to push as commit cfee451.
Since your change was applied there have been 1143 commits pushed to the master branch:

  • c3d0a94: 8279833: Loop optimization issue in String.encodeUTF8_UTF16
  • 9e02447: 8279834: Alpine Linux fails to build when --with-source-date enabled
  • 08e14c6: 8278207: G1: Tighten verification in G1ResetSkipCompactingClosure
  • c08b2ac: 8225093: Special property jdk.boot.class.path.append should not default to empty string
  • 4c52eb3: 8279669: test/jdk/com/sun/jdi/TestScaffold.java uses wrong condition
  • d46410c: 8279785: JFR: 'jfr configure' should show default values
  • 2bbeae3: 8279668: x86: AVX2 versions of vpxor should be asserted
  • 3121898: 8279703: G1: Remove unused force_not_compacted local in G1CalculatePointersClosure::do_heap_region
  • bf7bcaa: 8277748: Obsolete the MinInliningThreshold flag in JDK 19
  • 126328c: 8279560: AArch64: generate_compare_long_string_same_encoding and LARGE_LOOP_PREFETCH alignment
  • ... and 1133 more: https://git.openjdk.java.net/jdk/compare/c10de3538b47c182d7bfeb02f348fac9b2ad0641...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated label Jan 11, 2022
@openjdk openjdk bot closed this Jan 11, 2022
@openjdk openjdk bot removed ready rfr labels Jan 11, 2022
@openjdk
Copy link

@openjdk openjdk bot commented Jan 11, 2022

@cushon Pushed as commit cfee451.

💡 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
compiler hotspot-runtime integrated
7 participants