File: TestUnorderedReductionPartialVectorization.java

package info (click to toggle)
openjdk-25 25~32ea-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 825,280 kB
  • sloc: java: 5,584,902; cpp: 1,333,941; xml: 1,321,242; ansic: 487,993; asm: 404,003; objc: 21,088; sh: 15,102; javascript: 13,265; python: 8,319; makefile: 2,515; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (118 lines) | stat: -rw-r--r-- 5,414 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @bug JDK-8310130
 * @summary Special test cases for PhaseIdealLoop::move_unordered_reduction_out_of_loop
 *          Here a case with partial vectorization of the reduction.
 * @library /test/lib /
 * @run driver compiler.loopopts.superword.TestUnorderedReductionPartialVectorization
 */

package compiler.loopopts.superword;

import compiler.lib.ir_framework.*;

public class TestUnorderedReductionPartialVectorization {
    static final int RANGE = 1024;
    static final int ITER  = 10;

    public static void main(String[] args) {
        TestFramework.runWithFlags("-XX:-UseCompactObjectHeaders", "-XX:-AlignVector");
        TestFramework.runWithFlags("-XX:-UseCompactObjectHeaders", "-XX:+AlignVector");
        TestFramework.runWithFlags("-XX:+UseCompactObjectHeaders", "-XX:-AlignVector");
        TestFramework.runWithFlags("-XX:+UseCompactObjectHeaders", "-XX:+AlignVector");
    }

    @Run(test = {"test1"})
    @Warmup(0)
    public void runTests() throws Exception {
        int[] data = new int[RANGE];

        init(data);
        for (int i = 0; i < ITER; i++) {
            long r1 = test1(data, i);
            long r2 = ref1(data, i);
            if (r1 != r2) {
                throw new RuntimeException("Wrong result test1: " + r1 + " != " + r2);
            }
        }
    }

    @Test
    @IR(counts = {IRNode.LOAD_VECTOR_I,   IRNode.VECTOR_SIZE + "min(max_int, max_long)", "> 0",
                  IRNode.VECTOR_CAST_I2L, IRNode.VECTOR_SIZE + "min(max_int, max_long)", "> 0",
                  IRNode.OR_REDUCTION_V,                                                 "> 0",},
        applyIfOr = {"AlignVector", "false", "UseCompactObjectHeaders", "false"},
        applyIfPlatform = {"64-bit", "true"},
        applyIfCPUFeatureOr = {"avx2", "true", "rvv", "true"})
    static long test1(int[] data, long sum) {
        for (int i = 0; i < data.length; i+=2) {
            // Mixing int and long ops means we only end up allowing half of the int
            // loads in one pack, and we have two int packs. The first pack has one
            // of the pairs missing because of the store, which creates a dependency.
            // The first pack is rejected and left as scalar, the second pack succeeds
            // with vectorization. That means we have a mixed scalar/vector reduction
            // chain. This way it is possible that a vector-reduction has a scalar
            // reduction as input, which is neigher a phi nor a vector reduction.
            // In such a case, we must bail out of the optimization in
            // PhaseIdealLoop::move_unordered_reduction_out_of_loop
            int v = data[i]; // int read
            data[0] = 0;     // ruin the first pack
            sum |= v;        // long reduction (and implicit cast from int to long)

            // This example used to rely on that reductions were ignored in SuperWord::unrolling_analysis,
            // and hence the largest data type in the loop was the ints. This would then unroll the doubles
            // for twice the vector length, and this resulted in us having twice as many packs. Because of
            // the store "data[0] = 0", the first packs were destroyed, since they do not have power of 2
            // size.
            // Now, we no longer ignore reductions, and now we unroll half as much before SuperWord. This
            // means we would only get one pack per operation, and that one would get ruined, and we have
            // no vectorization. We now ensure there are again 2 packs per operation with a 2x hand unroll.
            int v2 = data[i + 1];
            sum |= v2;

            // With AlignVector, we need 8-byte alignment of vector loads/stores.
            // UseCompactObjectHeaders=false                 UseCompactObjectHeaders=true
            // adr = base + 16 + 8*i  ->  always             adr = base + 12 + 8*i  ->  never
            // -> vectorize                                  -> no vectorization
        }
        return sum;
    }

    static long ref1(int[] data, long sum) {
        for (int i = 0; i < data.length; i++) {
            int v = data[i];
            data[0] = 0;
            sum |= v;
        }
        return sum;
    }

    static void init(int[] data) {
        for (int i = 0; i < RANGE; i++) {
            data[i] = i + 1;
        }
    }
}