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

8312332: C2: Refactor SWPointer out from SuperWord #15013

Closed
wants to merge 4 commits into from

Conversation

pfustc
Copy link
Member

@pfustc pfustc commented Jul 25, 2023

As discussed in JDK-8308994, we should first do some refactoring work before proceeding with the new post loop vectorization. In this patch, we have done the following.

  1. We have created new C2 source files vectorization.[cpp|hpp] for shared logics and utilities for C2's auto-vectorization. So far we have moved class SWPointer and VectorElementSizeStats here from superword.[cpp|hpp].

  2. We have decoupled SWPointer from class SuperWord and renamed it to VPointer as it will be used by vectorizers other than SuperWord. The original class SWPointer and its inner class Tracer both have a _slp field initialized in their constructors. In this patch, we have replaced them by other fields and re-written the constructors for the same functionality. Original SWPointer::invariant() calls function SuperWord::find_pre_loop_end() for loop invariant checks. To help decoupling, we moved function find_pre_loop_end() to class CountedLoopNode. As function SWPointer::Tracer::invariant_1() is tightly coupled with SuperWord but only prints some debug messages, we temporarily removed it in this patch. We will consider adding it back after later refactoring of SuperWord so we added a TODO at its call site in this patch.

  3. We have a lot of memory phi node checks in loop optimizations. So we added a utility function is_memory_phi() in node.hpp.

Tested tier1~3 on x86 and AArch64. Also manually verified that option VectorizeDebug in compiler directives still works well.


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-8312332: C2: Refactor SWPointer out from SuperWord (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 15013

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

Using diff file

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

Webrev

Link to Webrev Comment

As discussed in JDK-8308994, we should first do some refactoring work
before proceeding with the new post loop vectorization. In this patch,
we have done the following.

1) We have created new C2 source files `vectorization.[cpp|hpp]` for
   shared logics and utilities for C2's auto-vectorization. So far we
   have moved class `SWPointer` and `VectorElementSizeStats` here from
   `superword.[cpp|hpp]`.

2) We have decoupled `SWPointer` from class `SuperWord` and renamed it
   to `VPointer` as it will be used by vectorizers other than SuperWord.
   The original class `SWPointer` and its inner class `Tracer` both have
   a `_slp` field initialized in their constructors. In this patch, we
   have replaced them by other fields and re-written the constructors
   for the same functionality. Original `SWPointer::invariant()` calls
   function `SuperWord::find_pre_loop_end()` for loop invariant checks.
   To help decoupling, we moved function `find_pre_loop_end()` to class
   `CountedLoopNode`. As function `SWPointer::Tracer::invariant_1()` is
   tightly coupled with `SuperWord` but only prints some debug messages,
   we temporarily removed it in this patch. We will consider adding it
   back after later refactoring of `SuperWord` so we added a `TODO` at
   its call site in this patch.

3) We have a lot of memory phi node checks in loop optimizations. So we
   added a utility function `is_memory_phi()` in `node.hpp`.

Tested tier1~3 on x86 and AArch64. Also manually verified that option
`VectorizeDebug` in compiler directives still works well.
@bridgekeeper
Copy link

bridgekeeper bot commented Jul 25, 2023

👋 Welcome back pli! 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 Jul 25, 2023
@openjdk
Copy link

openjdk bot commented Jul 25, 2023

@pfustc 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 Jul 25, 2023
@mlbridge
Copy link

mlbridge bot commented Jul 25, 2023

Webrevs

Copy link
Contributor

@eme64 eme64 left a comment

Choose a reason for hiding this comment

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

@pfustc Thanks a lot for moving this code, and especially for untangling it from SuperWord. This will be helpful for many future vectorization projects.

I left a few comments, but otherwise this looks straight-forward and good to me.


bool VPointer::invariant(Node* n) const {
NOT_PRODUCT(Tracer::Depth dd;)
// TODO: Add more trace output for invariant check after later refactoring
Copy link
Contributor

Choose a reason for hiding this comment

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

We generally don't like TODOs in the code. Best is to just drop it in the code and file an RFE if you think it is really important.

When did this even trace anything?
_slp->_lpt->is_member(_slp->_phase->get_loop(n_c)) != (int)_slp->in_bb(n)

Do you think this tracing is relevant enough?

Copy link
Contributor

Choose a reason for hiding this comment

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

If it should never happen: can we add an assert somewhere instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know when this can happen. This was added in jdk9 without a test case. If this can happen, it should be in some real corner cases. But definitely, the trace message is not important and it's safe to drop that. TODO is removed in my latest commit.

if (pre_loop_end != nullptr) {
Node* n_c = phase()->get_ctrl(n);
return phase()->is_dominator(n_c, pre_loop_end->loopnode());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is pre_loop_end != nullptr possible here? Before your patch we always found _slp->pre_loop_head().
I'm just worried that if we do not find it, then we still return is_not_member, but n is still located in the space between pre and post loop.
What do you think about this?

And: would it make sense to cache the pre_loop_head in the VPointer?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I have found that pre_loop_end could be null when we construct VPointer in SuperWord::output() - see the code in superword.cpp (L2574, L2580). It's null because CountedLoopNode::is_canonical_loop_entry() returns null at this time. Before my patch, it cannot be null as we cached _pre_loop_end in the SuperWord class. To address your concern, my latest commit moves the cache of _pre_loop_end from SuperWord to CountedLoopNode. Some more asserts are also added to make sure it's used for main loops only. (Caching it in VPointer doesn't help).

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain a bit more why CountedLoopNode::is_canonical_loop_entry() returned null at the time, and why you did move the caching?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the comment before function CountedLoopNode::is_canonical_loop_entry() explains that. I quote it below.

// Check the shape of the graph at the loop entry. In some cases,
// the shape of the graph does not match the shape outlined below.
// That is caused by the Opaque1 node "protecting" the shape of
// the graph being removed by, for example, the IGVN performed
// in PhaseIdealLoop::build_and_optimize().
//
// After the Opaque1 node has been removed, optimizations (e.g., split-if,
// loop unswitching, and IGVN, or a combination of them) can freely change
// the graph's shape. As a result, the graph shape outlined below cannot
// be guaranteed anymore.
Node* CountedLoopNode::is_canonical_loop_entry() {

Moving the caching is just to address your above concern that we still return is_not_member when pre_loop_end is null. Before my patch, it cannot be null as we cached _pre_loop_end in the SuperWord class. But after decoupling, we cannot access the cache in the SuperWord class anymore.

#endif
_nstack(nstack), _analyze_only(analyze_only), _stack_idx(0)
#ifndef PRODUCT
, _tracer((phase->C->directive()->VectorizeDebugOption & 2) > 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

You should also refactor the accessors for VectorizeDebugOption. I would move it from SuperWord to vectorization.hpp/cpp somehow. We should only do the "masking" & 2 in one single place.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, but doing this requires moving more handles including phase to vectorization.hpp/cpp. Perhaps we need to create a new class there first. I have created another task (https://bugs.openjdk.org/browse/JDK-8315361) and would like to do more refactoring later.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, we can do that refactoring later.

int VPointer::Tracer::_depth = 0;
#endif

VPointer::VPointer(MemNode* mem, PhaseIdealLoop* phase, IdealLoopTree* lpt,
Copy link
Contributor

Choose a reason for hiding this comment

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

You could also call it LPointer or LoopPointer. VPointer sounds like VectorPointer - but it is not a pointer of a vector but a scalar memop. That could be confusing. But you could also argue it is a VectorizationPointer, and hence VPointer.

Copy link
Member Author

Choose a reason for hiding this comment

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

My idea is keeping the name as short as possible. I think it should be VectorizationPointer so VPointer. In my opinion, LoopPointer also sounds like a pointer to a loop but it isn't. I'd like to have more suggestions from other reviewers about the name.

BTW: VPointer is also used for vector memop in SuperWord::output().

Copy link
Contributor

Choose a reason for hiding this comment

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

No worries, I can live with VPointer.

@pfustc pfustc requested a review from eme64 September 4, 2023 01:25
@eme64
Copy link
Contributor

eme64 commented Sep 4, 2023

Hi @pfustc sorry for the delay. I am still out of the office, and I have not yet had time to look at it. It will be one of the first things I'll have a look at when I get back. Looking forward to this refactoring :)

FYI: @TobiHartmann just launched testing for the 3rd commit (web rev 02)
Update: no related test failures.

Copy link
Contributor

@eme64 eme64 left a comment

Choose a reason for hiding this comment

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

Ok, this now looks good. I only have some questions about the caching of _pre_loop_end.
Thanks for doing this work, and sorry for the delay with the review.

// The eventual count of vectorizable packs in slp
int _slp_vector_pack_count;
// Cached CountedLoopEndNode of pre loop for main loops
CountedLoopEndNode* _pre_loop_end;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it safe to cached this node? What happens if the node were ever to be replaced?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, we need to access it via pre_loop_end() which has asserts that check the consistency. Ok.

Copy link
Contributor

Choose a reason for hiding this comment

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

We just have to make sure that this field is never accessed directly. What happens if people start using it elsewhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

The field is already private. What else do you think we can do to further protect it?

int VPointer::Tracer::_depth = 0;
#endif

VPointer::VPointer(MemNode* mem, PhaseIdealLoop* phase, IdealLoopTree* lpt,
Copy link
Contributor

Choose a reason for hiding this comment

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

No worries, I can live with VPointer.

#endif
_nstack(nstack), _analyze_only(analyze_only), _stack_idx(0)
#ifndef PRODUCT
, _tracer((phase->C->directive()->VectorizeDebugOption & 2) > 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, we can do that refactoring later.

if (pre_loop_end != nullptr) {
Node* n_c = phase()->get_ctrl(n);
return phase()->is_dominator(n_c, pre_loop_end->loopnode());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain a bit more why CountedLoopNode::is_canonical_loop_entry() returned null at the time, and why you did move the caching?

Copy link
Contributor

@eme64 eme64 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 the responses. LGTM. Thanks for the nice refactoring work!

@openjdk
Copy link

openjdk bot commented Sep 11, 2023

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

8312332: C2: Refactor SWPointer out from SuperWord

Reviewed-by: epeter, kvn

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

  • 5cea53d: 8315810: Reimplement sun.reflect.ReflectionFactory::newConstructorForSerialization with method handles
  • eb1f67b: 8315937: Enable parallelism in vmTestbase/nsk/stress/numeric tests
  • a57b9da: 8316242: Opensource SwingGraphics manual test
  • 3abe798: 8316115: Parallel: Fix -Wconversion warnings around NUMA node ID
  • 83dca62: 8313638: Add test for dump of resolved references
  • cfa8901: 8315338: RISC-V: Change flags for stable extensions to non-experimental
  • edd454b: 8315766: Parallelize gc/stress/TestStressIHOPMultiThread.java test
  • de9b971: 8315933: Serial: Remove empty Generation::ensure_parsability
  • b48dbf6: 8316181: Move the fast locking implementation out of the .ad files
  • 8f4dfc4: 8316164: Opensource JMenuBar manual test
  • ... and 228 more: https://git.openjdk.org/jdk/compare/12de9b0225363377e9a76729b11698221d4f29f2...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 Pull request is ready to be integrated label Sep 11, 2023
@pfustc
Copy link
Member Author

pfustc commented Sep 12, 2023

Thanks @eme64. May I have another review from someone else?

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.

In general it looks good. I just want that you add more description.

And I have question. Part of code is under "#ifndef PRODUCT". Did you build "optimized" VM to make sure it works?

#include "opto/loopnode.hpp"

// ----------------------------------VPointer----------------------------------
// Information about an address for dependence checking and vector alignment
Copy link
Contributor

Choose a reason for hiding this comment

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

I am fine with using VPointer name but I think you need to add more description here to avoid confusion.

May be add a general description what is in this file: "for shared logics and utilities for C2's auto-vectorization".

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

#include "opto/node.hpp"
#include "opto/loopnode.hpp"

// ----------------------------------VPointer----------------------------------
Copy link
Contributor

Choose a reason for hiding this comment

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

We stop using such lines - there was cleaning RFE to remove them.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

@pfustc
Copy link
Member Author

pfustc commented Sep 13, 2023

In general it looks good. I just want that you add more description.

And I have question. Part of code is under "#ifndef PRODUCT". Did you build "optimized" VM to make sure it works?

@vnkozlov, I just checked with CompilerDirectives and got the same tracing output between "fastdebug" build and "optimized" build, and no tracing output is seen for "release" build.

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.

Good. Thank you for checking optimized build.

@pfustc
Copy link
Member Author

pfustc commented Sep 15, 2023

/integrate

@openjdk
Copy link

openjdk bot commented Sep 15, 2023

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

  • b55e418: 8301991: Convert l10n properties resource bundles to UTF-8 native
  • d475f61: 8315731: Open source several Swing Text related tests
  • 4415261: 8315683: Parallelize java/util/concurrent/tck/JSR166TestCase.java
  • b2e9106: 8313452: Improve Classfile API attributes handling safety
  • 2a6fb9c: 8316147: Remove serviceability/sa/TestJhsdbJstackMixed.java from -Xcomp problem list
  • ca747f0: 8315678: Classfile API ConstantPool::entryCount and ConstantPool::entryByIndex methods are confusing
  • 6d47fc6: 8313258: RuntimeInvisibleTypeAnnotationsAttribute.annotations() API Index out of Bound error
  • c7d306c: 8315541: Classfile API TypeAnnotation.TargetInfo factory methods accept null labels
  • c11f835: 8315824: Open source several Swing Text/HTML related tests
  • 5cea53d: 8315810: Reimplement sun.reflect.ReflectionFactory::newConstructorForSerialization with method handles
  • ... and 237 more: https://git.openjdk.org/jdk/compare/12de9b0225363377e9a76729b11698221d4f29f2...master

Your commit was automatically rebased without conflicts.

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

openjdk bot commented Sep 15, 2023

@pfustc Pushed as commit 96781ba.

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

@pfustc pfustc deleted the swpointer branch September 15, 2023 01:29
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
3 participants