Skip to content

Conversation

@rwestrel
Copy link
Contributor

@rwestrel rwestrel commented Mar 9, 2023

In the test case testByteLong1 (that's extracted from a memory
segment micro benchmark), the address of the store is initially:

(AddP#204 base#195 base#195 (AddL#164 (ConvI2L#158 (CastII#157 (LshiftI#107 iv#101))) invar#163))

(#numbers are node numbers to help the discussion).

iv#101 is the Phi of a counted loop. invar#163 is the
baseOffset load.

To eliminate the range check, the loop is transformed into a loop nest
and as a consequence the address above becomes:

(AddP#204 base#195 base#195 (AddL#164 (ConvI2L#158 (CastII#157 (LShiftI#107 (AddI#326 invar#308 iv#321)))) invar#163))

invar#308 is some expression from a Phi of the outer loop.

That AddP is transformed multiple times to push the invariants out of loop:

(AddP#568 base#195 (AddP#556 base#195 base#195 invar#163) (ConvI2L#158 (CastII#157 (AddI#566 (LShiftI#565 iv#321) invar#577))))

then:

(AddP#568 base#195 (AddP#847 (AddP#556 base#195 base#195 invar#163) (AddL#838 (ConvI2L#793 (LShiftL#760 iv#767)) (ConvI2L#818 (CastII#779 invar#577)))))

and finally:

(AddP#568 base#195 (AddP#949 base#195 (AddP#855 base#195 (AddP#556 base#195 base#195 invar#163) (ConvI2L#818 (CastII#809 invar#577))) (ConvI2L#938 (LShiftI#896 iv#908))))

AddP#855 is out of the inner loop.

This doesn't vectorize because:

  • there are 2 invariants in the address expression but superword only
    support one (tracked by _invar in SWPointer)

  • there are more levels of AddP (4) than superword supports (3)

To fix that, I propose to no longer track the address elements in
_invar, _negate_invar and _invar_scale but instead to have a
single _invar which is an expression built by superword as it
follows chains of addP nodes. I kept the previous _invar,
_negate_invar and _invar_scale as debugging and use them to check
that what vectorized with the previous scheme still does.

I also propose lifting the restriction on 3 levels of AddP entirely.


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-8300257: C2: vectorization fails on some simple Memory Segment loops

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 12942

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Mar 9, 2023

👋 Welcome back roland! 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 Pull request is ready for review label Mar 9, 2023
@openjdk
Copy link

openjdk bot commented Mar 9, 2023

@rwestrel The following label will be automatically applied to this pull request:

  • hotspot-compiler

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

@openjdk openjdk bot added the hotspot-compiler hotspot-compiler-dev@openjdk.org label Mar 9, 2023
@mlbridge
Copy link

mlbridge bot commented Mar 9, 2023

Webrevs

@vnkozlov
Copy link
Contributor

vnkozlov commented Mar 9, 2023

So the final expression should be seen by superword as next since AddP#855 is invariant for inner loop:

(AddP#568 base#195 (AddP#949 base#195 (invar#855) (ConvI2L#938 (LShiftI#896 iv#908))))

First, I think you messed up with (). AddP node should have 3 inputs: base, address and offset. I don't see offset for AddP#568.
Second, if superword can mark AddP#855 as invariant (no need to parse it) your address expression become simple.

@rwestrel
Copy link
Contributor Author

rwestrel commented Mar 9, 2023

So the final expression should be seen by superword as next since AddP#855 is invariant for inner loop:

(AddP#568 base#195 (AddP#949 base#195 (invar#855) (ConvI2L#938 (LShiftI#896 iv#908))))

First, I think you messed up with (). AddP node should have 3 inputs: base, address and offset. I don't see offset for AddP#568. Second, if superword can mark AddP#855 as invariant (no need to parse it) your address expression become simple.

I left the constant part of the address out of the expressions. So that was on purpose but confusing, sorry.
The current logic only sets the invariant to the result of an integer operation and otherwise follows through the AddP and checks that all of them have the same base. So If we wanted to use the AddP as invariant, we would need to change the logic so it stops as the first AddP that's loop invariant, we would likely not check that all AddP nodes have the same base and we would need to cast the AddP to an integer to compute the loop alignment.
Do you think that's better that what I'm proposing? I don't really have a strong opinion. Your suggestion is likely a smaller change.

@vnkozlov
Copy link
Contributor

vnkozlov commented Mar 9, 2023

So we need to check all AddP to find base offset. No short cuts then :(
I agree with your proposal with invariants. Last time I touched this code it was one of pain points that we have only one _invar. Any improvements to that is welcome.
I will start testing while looking on changes.

Copy link
Contributor

@vnkozlov vnkozlov left a comment

Choose a reason for hiding this comment

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

My tier1-4,xcomp and stress testing passed.
I looked and changes and they seems fine.
May be we need to run performance testing too.

@openjdk
Copy link

openjdk bot commented Mar 9, 2023

@rwestrel this pull request can not be integrated into master due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout JDK-8300257
git fetch https://git.openjdk.org/jdk master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push

@openjdk openjdk bot added the merge-conflict Pull request has merge conflict with target branch label Mar 9, 2023
@openjdk
Copy link

openjdk bot commented Mar 14, 2023

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

8300257: C2: vectorization fails on some simple Memory Segment loops

Reviewed-by: kvn, thartmann

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 33 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 ready Pull request is ready to be integrated and removed merge-conflict Pull request has merge conflict with target branch labels Mar 14, 2023
Copy link
Member

@TobiHartmann TobiHartmann 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 to me. I'll run some perf testing and report back.

if (n->in(2)->is_Con() && invariant(n->in(1))) {
_negate_invar = negate;
_invar = n->in(1);
maybe_add_to_invar(maybe_negate_invar(negate, n->in(1)));
Copy link
Member

Choose a reason for hiding this comment

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

It feels like maybe_negate_invar should be moved into maybe_add_to_invar and be controlled by a negate argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. I applied your suggestions and made that change too.

@TobiHartmann
Copy link
Member

Performance and correctness testing looks good.

rwestrel and others added 5 commits March 17, 2023 17:43
Co-authored-by: Tobias Hartmann <tobias.hartmann@oracle.com>
…nvar.java

Co-authored-by: Tobias Hartmann <tobias.hartmann@oracle.com>
…nvar.java

Co-authored-by: Tobias Hartmann <tobias.hartmann@oracle.com>
Copy link
Member

@TobiHartmann TobiHartmann left a comment

Choose a reason for hiding this comment

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

Thanks for making these changes. SWPointer::maybe_negate_invar could now be removed as it has only one user but I'm also fine with leaving it as is.

@rwestrel
Copy link
Contributor Author

rwestrel commented Apr 7, 2023

Thanks for making these changes. SWPointer::maybe_negate_invar could now be removed as it has only one user but I'm also fine with leaving it as is.

Thanks for looking at this again.
Because it has a single use? The method is fairly simple but non trivial so I'll leave it as a separate method.

@rwestrel
Copy link
Contributor Author

rwestrel commented Apr 7, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Apr 7, 2023

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

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Apr 7, 2023
@openjdk openjdk bot closed this Apr 7, 2023
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Apr 7, 2023
@openjdk
Copy link

openjdk bot commented Apr 7, 2023

@rwestrel Pushed as commit 6b2a86a.

💡 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

hotspot-compiler hotspot-compiler-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

3 participants