Skip to content

8357530: C2 SuperWord: Diagnostic flag AutoVectorizationOverrideProfitability #25387

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

Closed

Conversation

eme64
Copy link
Contributor

@eme64 eme64 commented May 22, 2025

I'm adding a diagnostic flag AutoVectorizationOverrideProfitability. The goal is that with it, we can systematically benchmark our Auto Vectorization profitability heuristics. In all cases, we run Auto Vectorization, including packing.

  • 0: abort vectorization, as if it was not profitable.
  • 1: default, use profitability heuristics to determine if we should vectorize.
  • 2: always vectorize when possible, even if profitability heuristic would say that it is not profitable.

In the future, we may change our heuristics. We may for example introduce a cost model JDK-8340093. But at any rate, we need this flag, so that we can override these profitability heuristics, even if just for benchmarking.

I did not yet go through all of SuperWord to check if there may be other decisions that could go under this flag. If we find any later, we can still add them.

Below, I'm showing how it helps to benchmark the some reduction cases we have been working on.

And if you want a small test to experiement with, I have one at the end for you.

Note to reviewer: This patch should not make any behavioral difference, i.e. with the default AutoVectorizationOverrideProfitability=1 the behavior should be as before this patch.


Use-Case: investigate Reduction Heuristics

A while back, I have written a comprehensive benchmark for Reductions #21032. I saw that some cases might possibly be profitable, but we have disabled vectorization because of a heuristic.

This heuristic was added a long time ago. The observation at the time was that simple add and mul reductions were not profitable.

But in the meantime, I have added an improvement, where we move int/long reductions out of the loop. We can do that because int/long reductions can be reordered. See #13056 . We cannot do that with float/double reductions, because there we must keep the strict order of reductions. Otherwise we risk wrong rounding results.

Since then, we have had multiple reports that simple reductions are not vectorized, and I am working on it:
https://bugs.openjdk.org/browse/JDK-8307516

Running the reduction benchmarks from #21032 (please have a look at it now, the results below are only going to be more complicated!), like this:

make test TEST="micro:vm.compiler.VectorReduction2.WithSuperword" CONF=linux-x64 TEST_VM_OPTS="-XX:+UnlockDiagnosticVMOptions -XX:AutoVectorizationOverrideProfitability=2"

I ran the experiments on my x64 / AVX512 machine, and a aarch64 / neon machine.
For each I ran with SuperWord disabled (no), and with SuperWord and AutoVectorizationOverrideProfitability set to 1 (default), 0 (abort vectorization), and 2 (force vectorization).

image

image

The orange heuristic tags show where the heuristic makes a difference - in this case we prevent vectorization even though it is would be faster. This is evidence that we need to update the heuristic.

Interestingly, forcing vectorization in the strict cases did not lead to any performance drop.

It seems that forced vectorization is only problematic in one case: longMulSimple on aarch64. I need to investigate. Generally, we do vectorize (if forced - they are 2-element vectors after all) at least some of the long cases (hand checked longAddSimple), but it seems it is just not very fast, no idea why. The problematic longMulSimple does also vectorize (if forced only), but it is consistently slow. The confusing part: longMulDotProduct should be even slower. But a quick investigation showed that we actually do not vectorize it, the packing algorithm gets confused about which multiplications to pack. I suspect that generally 2-element multiplication reduction is very slow on neon / arch64. We will have to be careful about that when we change the heuristic. It is edge cases like these that make me nervous, and are the reason why I have not changed these heuristics sooner.

I would also have to investigate the impact on a few more platforms, especially on AVX and AVX2.

With x64 and byte/char/short, we never vectorize. Still, enabling SuperWord changes the level of unrolling, and it seems in some cases SuperWord enabled leads to over-unrolling, hence you see some slowdowns in some cases. We should investigate that as well.

For now it is clear: this flag would be helpful for improving performance heuristics.


Example for the Flag

I played around with an example like this:

java -XX:CompileCommand=compileonly,Test::test2 -XX:CompileCommand=TraceAutoVectorization,Test::test*,ALL -Xbatch -XX:AutoVectorizationOverrideProfitability=0 -XX:MaxVectorSize=64 Test.java

public class Test {
    public static int[] a = new int[10_000];

    public static void main(String[] args) {
        for (int i = 0; i < a.length; i++) {
            a[i] = (int)i;
        }

        for (int i = 0; i < 10_000; i++) {
            test1();
            test2(a, a);
	}
        System.out.println("sum: " + test1());
    }

    public static int test1() {
	int sum = 0;
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
	}
        return sum;
    }

    public static void test2(int[] a, int[] b) {
        for (int i = 0; i < a.length; i++) {
            a[i] = b[i];
        }
    }
}

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-8357530: C2 SuperWord: Diagnostic flag AutoVectorizationOverrideProfitability (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 25387

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented May 22, 2025

👋 Welcome back epeter! 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 May 22, 2025

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

8357530: C2 SuperWord: Diagnostic flag AutoVectorizationOverrideProfitability

Reviewed-by: thartmann, 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 48 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 changed the title JDK-8357530 8357530: C2 SuperWord: Diagnostic flag AutoVectorizationOverrideProfitability May 22, 2025
@openjdk
Copy link

openjdk bot commented May 22, 2025

@eme64 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-8357530-SuperWordOverrideProfitability
git fetch https://git.openjdk.org/jdk.git 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 May 22, 2025
@openjdk
Copy link

openjdk bot commented May 22, 2025

@eme64 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 removed the merge-conflict Pull request has merge conflict with target branch label May 22, 2025
@eme64 eme64 marked this pull request as ready for review May 23, 2025 13:22
@openjdk openjdk bot added the rfr Pull request is ready for review label May 23, 2025
@mlbridge
Copy link

mlbridge bot commented May 23, 2025

Webrevs

@eme64
Copy link
Contributor Author

eme64 commented May 23, 2025

@galderz You may be interested in these results ;)

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.

This looks fine. One suggestion I have for separate RFE is to use UL for such outputs.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 23, 2025
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 otherwise.

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label May 26, 2025
@eme64
Copy link
Contributor Author

eme64 commented May 26, 2025

This looks fine. One suggestion I have for separate RFE is to use UL for such outputs.

@vnkozlov Thanks for the approval! About your suggestion:
I suppose that means I would be refactoring all TraceSuperWord/TraceAutoVectorization to use UL. Is there now a good way to do the CompileCommand method-level filtering with UL? Because TraceAutoVectorization uses method-based filtering.

@TobiHartmann I applied all your suggestions :)

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 26, 2025
@eme64
Copy link
Contributor Author

eme64 commented May 26, 2025

@TobiHartmann @vnkozlov Thanks for the reviews!

/integrate

@openjdk
Copy link

openjdk bot commented May 26, 2025

Going to push as commit e8eff4d.
Since your change was applied there have been 53 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 May 26, 2025
@openjdk openjdk bot closed this May 26, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels May 26, 2025
@openjdk
Copy link

openjdk bot commented May 26, 2025

@eme64 Pushed as commit e8eff4d.

💡 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 [email protected] integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

3 participants