RFR: 8302652: [SuperWord] Reduction should happen after loop, when possible [v5]

Emanuel Peter epeter at openjdk.org
Fri May 12 07:05:49 UTC 2023


On Fri, 12 May 2023 06:48:20 GMT, Emanuel Peter <epeter at openjdk.org> wrote:

>> I agree, the assert is not very necessary, but I'd rather have an assert more in there and figure out what cases I missed when the fuzzer eventually finds a case. But if it is wished I can also just remove that assert.
>> 
>> I wrote this `Test.java`:
>> 
>> class Test {
>>     static final int RANGE = 1024;
>>     static final int ITER  = 10_000;
>> 
>>     static void init(int[] data) {
>>         for (int i = 0; i < RANGE; i++) {
>>             data[i] = i + 1;
>>         }
>>     }
>> 
>>     static int test(int[] data, int sum) {
>>         int x = 0;
>>         for (int i = 0; i < RANGE; i++) {
>>             sum += 11 * data[i];
>>             x = sum & i; // what happens with this AndI ?
>>         }
>>         return sum + x;
>>     }
>> 
>>     public static void main(String[] args) {
>>         int[] data = new int[RANGE];
>>         init(data);
>>         for (int i = 0; i < ITER; i++) {
>>             test(data, i);
>>         }
>>     }
>> }
>> 
>> And ran it like this, with my patch:
>> 
>> ./java -Xbatch -XX:CompileCommand=compileonly,Test::test  -XX:+TraceNewVectors -XX:+TraceSuperWord Test.java
>> 
>> 
>> Everything vectorized as usual. But what happens with the `AndI`? It actually drops outside the loop. Its left input is the `AddReductionVI`, and the right input is `(Phi #tripcount) + 63` (the last `i` thus already drops outside the loop).
>
> Note: If I have uses of the reduction in each iteration, then we already refuse to vectorize the reduction, as in this case:
> 
>     static int test(int[] data, int sum) {
>         int x = 0;
>         for (int i = 0; i < RANGE; i++) {
>             sum += 11 * data[i];
>             x += sum & i;  // vector use of sum prevents vectorization of sum's reduction-vectorization -> whole chain not vectorized
>         }
>         return sum + x;
>     }

My conclusion, given my best understanding: eigher we have a use of the `sum` in all iterations, which prevents vectorization of the reduction. Or we only have a use of the last iteration, and it drops out of the loop already.

So if there is such an odd example, I'd rather we run into an assert in debug and look at it again. Maybe it would be perfectly legal, or maybe it reveals a bug here or elsewhere in the reduction code.

@sviswa7 what do you think?

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/13056#discussion_r1191981425


More information about the hotspot-compiler-dev mailing list