Skip to content

Commit f9bcef4

Browse files
author
Xiaohong Gong
committed
8351627: C2 AArch64 ROR/ROL: assert((1 << ((T>>1)+3)) > shift) failed: Invalid Shift value
Reviewed-by: chagedorn, epeter, jbhateja, adinn
1 parent 17dc30c commit f9bcef4

File tree

2 files changed

+304
-1
lines changed

2 files changed

+304
-1
lines changed

src/hotspot/share/opto/vectornode.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,7 @@ Node* VectorNode::degenerate_vector_rotate(Node* src, Node* cnt, bool is_rotate_
16701670
// Constant shift.
16711671
int shift = cnt_type->get_con() & shift_mask;
16721672
shiftRCnt = phase->intcon(shift);
1673-
shiftLCnt = phase->intcon(shift_mask + 1 - shift);
1673+
shiftLCnt = phase->intcon((shift_mask + 1 - shift) & shift_mask);
16741674
} else if (cnt->Opcode() == Op_Replicate) {
16751675
// Scalar variable shift, handle replicates generated by auto vectorizer.
16761676
cnt = cnt->in(1);
@@ -1686,6 +1686,7 @@ Node* VectorNode::degenerate_vector_rotate(Node* src, Node* cnt, bool is_rotate_
16861686
}
16871687
shiftRCnt = phase->transform(new AndINode(cnt, phase->intcon(shift_mask)));
16881688
shiftLCnt = phase->transform(new SubINode(phase->intcon(shift_mask + 1), shiftRCnt));
1689+
shiftLCnt = phase->transform(new AndINode(shiftLCnt, phase->intcon(shift_mask)));
16891690
} else {
16901691
// Variable vector rotate count.
16911692
assert(Matcher::supports_vector_variable_shifts(), "");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
/*
2+
* Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package compiler.vectorapi;
25+
26+
import compiler.lib.generators.*;
27+
import jdk.incubator.vector.IntVector;
28+
import jdk.incubator.vector.ShortVector;
29+
import jdk.incubator.vector.VectorOperators;
30+
import jdk.incubator.vector.VectorSpecies;
31+
import jdk.test.lib.Asserts;
32+
33+
/*
34+
* @test
35+
* @bug 8351627
36+
* @summary C2 AArch64 ROR/ROL: assert((1 << ((T>>1)+3)) > shift) failed: Invalid Shift value
37+
* @modules jdk.incubator.vector
38+
* @library /test/lib /
39+
* @run main/othervm -XX:-TieredCompilation compiler.vectorapi.TestRotateWithZero
40+
*/
41+
public class TestRotateWithZero {
42+
private static final int INVOCATIONS = 10000;
43+
private static final int LENGTH = 2048;
44+
private static final Generators random = Generators.G;
45+
private static final VectorSpecies<Integer> I_SPECIES = IntVector.SPECIES_PREFERRED;
46+
private static final VectorSpecies<Short> S_SPECIES = ShortVector.SPECIES_PREFERRED;
47+
48+
private static int[] arr1;
49+
private static int[] arr2;
50+
private static int[] res;
51+
private static short[] sarr1;
52+
private static short[] sarr2;
53+
private static short[] sres;
54+
55+
static {
56+
arr1 = new int[LENGTH];
57+
arr2 = new int[LENGTH];
58+
res = new int[LENGTH];
59+
sarr1 = new short[LENGTH];
60+
sarr2 = new short[LENGTH];
61+
sres = new short[LENGTH];
62+
63+
random.fill(random.ints(), arr1);
64+
Generator<Integer> shortGen = random.uniformInts(Short.MIN_VALUE, Short.MAX_VALUE);
65+
for (int i = 0; i < LENGTH; i++) {
66+
sarr1[i] = shortGen.next().shortValue();
67+
sarr2[i] = (short)0;
68+
arr2[i] = 0;
69+
}
70+
}
71+
72+
private static void rotateRightWithZero() {
73+
for (int i = 0; i < LENGTH; i += I_SPECIES.length()) {
74+
IntVector v = IntVector.fromArray(I_SPECIES, arr1, i);
75+
v.lanewise(VectorOperators.ROR, 0).intoArray(res, i);
76+
}
77+
}
78+
79+
private static void rotateLeftWithZero() {
80+
for (int i = 0; i < LENGTH; i += I_SPECIES.length()) {
81+
IntVector v = IntVector.fromArray(I_SPECIES, arr1, i);
82+
v.lanewise(VectorOperators.ROL, 0).intoArray(res, i);
83+
}
84+
}
85+
86+
private static void rotateRightWithZeroConst() {
87+
IntVector vzero = IntVector.zero(I_SPECIES);
88+
for (int i = 0; i < LENGTH; i += I_SPECIES.length()) {
89+
IntVector v = IntVector.fromArray(I_SPECIES, arr1, i);
90+
v.lanewise(VectorOperators.ROR, vzero).intoArray(res, i);
91+
}
92+
}
93+
94+
private static void rotateLeftWithZeroConst() {
95+
IntVector vzero = IntVector.zero(I_SPECIES);
96+
for (int i = 0; i < LENGTH; i += I_SPECIES.length()) {
97+
IntVector v = IntVector.fromArray(I_SPECIES, arr1, i);
98+
v.lanewise(VectorOperators.ROL, vzero).intoArray(res, i);
99+
}
100+
}
101+
102+
private static void rotateRightWithZeroArr() {
103+
IntVector vzero = IntVector.fromArray(I_SPECIES, arr2, 0);
104+
for (int i = 0; i < LENGTH; i += I_SPECIES.length()) {
105+
IntVector v = IntVector.fromArray(I_SPECIES, arr1, i);
106+
v.lanewise(VectorOperators.ROR, vzero).intoArray(res, i);
107+
}
108+
}
109+
110+
private static void rotateLeftWithZeroArr() {
111+
IntVector vzero = IntVector.fromArray(I_SPECIES, arr2, 0);
112+
for (int i = 0; i < LENGTH; i += I_SPECIES.length()) {
113+
IntVector v = IntVector.fromArray(I_SPECIES, arr1, i);
114+
v.lanewise(VectorOperators.ROL, vzero).intoArray(res, i);
115+
}
116+
}
117+
118+
private static void rotateRightWithZeroVar() {
119+
for (int i = 0; i < LENGTH; i += I_SPECIES.length()) {
120+
IntVector v = IntVector.fromArray(I_SPECIES, arr1, i);
121+
v.lanewise(VectorOperators.ROR, arr2[i]).intoArray(res, i);
122+
}
123+
}
124+
125+
private static void rotateLeftWithZeroVar() {
126+
for (int i = 0; i < LENGTH; i += I_SPECIES.length()) {
127+
IntVector v = IntVector.fromArray(I_SPECIES, arr1, i);
128+
v.lanewise(VectorOperators.ROL, arr2[i]).intoArray(res, i);
129+
}
130+
}
131+
132+
private static void rotateRightWithZero_subword() {
133+
for (int i = 0; i < LENGTH; i += S_SPECIES.length()) {
134+
ShortVector v = ShortVector.fromArray(S_SPECIES, sarr1, i);
135+
v.lanewise(VectorOperators.ROR, 0).intoArray(sres, i);
136+
}
137+
}
138+
139+
private static void rotateLeftWithZero_subword() {
140+
for (int i = 0; i < LENGTH; i += S_SPECIES.length()) {
141+
ShortVector v = ShortVector.fromArray(S_SPECIES, sarr1, i);
142+
v.lanewise(VectorOperators.ROL, 0).intoArray(sres, i);
143+
}
144+
}
145+
146+
private static void rotateRightWithZeroConst_subword() {
147+
ShortVector vzero = ShortVector.zero(S_SPECIES);
148+
for (int i = 0; i < LENGTH; i += S_SPECIES.length()) {
149+
ShortVector v = ShortVector.fromArray(S_SPECIES, sarr1, i);
150+
v.lanewise(VectorOperators.ROR, vzero).intoArray(sres, i);
151+
}
152+
}
153+
154+
private static void rotateLeftWithZeroConst_subword() {
155+
ShortVector vzero = ShortVector.zero(S_SPECIES);
156+
for (int i = 0; i < LENGTH; i += S_SPECIES.length()) {
157+
ShortVector v = ShortVector.fromArray(S_SPECIES, sarr1, i);
158+
v.lanewise(VectorOperators.ROL, vzero).intoArray(sres, i);
159+
}
160+
}
161+
162+
private static void rotateRightWithZeroArr_subword() {
163+
for (int i = 0; i < LENGTH; i += S_SPECIES.length()) {
164+
ShortVector v1 = ShortVector.fromArray(S_SPECIES, sarr1, i);
165+
ShortVector v2 = ShortVector.fromArray(S_SPECIES, sarr2, i);
166+
v1.lanewise(VectorOperators.ROR, v2).intoArray(sres, i);
167+
}
168+
}
169+
170+
private static void rotateLeftWithZeroArr_subword() {
171+
for (int i = 0; i < LENGTH; i += S_SPECIES.length()) {
172+
ShortVector v1 = ShortVector.fromArray(S_SPECIES, sarr1, i);
173+
ShortVector v2 = ShortVector.fromArray(S_SPECIES, sarr2, i);
174+
v1.lanewise(VectorOperators.ROL, v2).intoArray(sres, i);
175+
}
176+
}
177+
178+
private static void rotateRightWithZeroVar_subword() {
179+
for (int i = 0; i < LENGTH; i += S_SPECIES.length()) {
180+
ShortVector v = ShortVector.fromArray(S_SPECIES, sarr1, i);
181+
v.lanewise(VectorOperators.ROR, sarr2[i]).intoArray(sres, i);
182+
}
183+
}
184+
185+
private static void rotateLeftWithZeroVar_subword() {
186+
for (int i = 0; i < LENGTH; i += S_SPECIES.length()) {
187+
ShortVector v = ShortVector.fromArray(S_SPECIES, sarr1, i);
188+
v.lanewise(VectorOperators.ROL, sarr2[i]).intoArray(sres, i);
189+
}
190+
}
191+
192+
private static void checkResults(int[] ref, int[] res) {
193+
for (int i = 0; i < LENGTH; i++) {
194+
Asserts.assertEquals(ref[i], res[i]);
195+
}
196+
}
197+
198+
private static void checkResults(short[] ref, short[] res) {
199+
for (int i = 0; i < LENGTH; i++) {
200+
Asserts.assertEquals(ref[i], res[i]);
201+
}
202+
}
203+
204+
private static void test() {
205+
// Test rotate with a immediate 0
206+
for (int i = 0; i < INVOCATIONS; i++) {
207+
rotateRightWithZero();
208+
}
209+
checkResults(arr1, res);
210+
211+
for (int i = 0; i < INVOCATIONS; i++) {
212+
rotateLeftWithZero();
213+
}
214+
checkResults(arr1, res);
215+
216+
// Test rotate with a constant vector with all zeros
217+
for (int i = 0; i < INVOCATIONS; i++) {
218+
rotateRightWithZeroConst();
219+
}
220+
checkResults(arr1, res);
221+
222+
for (int i = 0; i < INVOCATIONS; i++) {
223+
rotateLeftWithZeroConst();
224+
}
225+
checkResults(arr1, res);
226+
227+
// Test rotate with a vector loaded from the memory
228+
// filled with zeros
229+
for (int i = 0; i < INVOCATIONS; i++) {
230+
rotateRightWithZeroArr();
231+
}
232+
checkResults(arr1, res);
233+
234+
for (int i = 0; i < INVOCATIONS; i++) {
235+
rotateLeftWithZeroArr();
236+
}
237+
checkResults(arr1, res);
238+
239+
// Test rotate with a variable assigned with zero.
240+
for (int i = 0; i < INVOCATIONS; i++) {
241+
rotateRightWithZeroVar();
242+
}
243+
checkResults(arr1, res);
244+
245+
for (int i = 0; i < INVOCATIONS; i++) {
246+
rotateLeftWithZeroVar();
247+
}
248+
checkResults(arr1, res);
249+
}
250+
251+
private static void test_subword() {
252+
// Test rotate with a immediate 0
253+
for (int i = 0; i < INVOCATIONS; i++) {
254+
rotateRightWithZero_subword();
255+
}
256+
checkResults(sarr1, sres);
257+
258+
for (int i = 0; i < INVOCATIONS; i++) {
259+
rotateLeftWithZero_subword();
260+
}
261+
checkResults(sarr1, sres);
262+
263+
// Test rotate with a constant vector with all zeros
264+
for (int i = 0; i < INVOCATIONS; i++) {
265+
rotateRightWithZeroConst_subword();
266+
}
267+
checkResults(sarr1, sres);
268+
269+
for (int i = 0; i < INVOCATIONS; i++) {
270+
rotateLeftWithZeroConst_subword();
271+
}
272+
checkResults(sarr1, sres);
273+
274+
// Test rotate with a vector loaded from the memory
275+
// filled with zeros
276+
for (int i = 0; i < INVOCATIONS; i++) {
277+
rotateRightWithZeroArr_subword();
278+
}
279+
checkResults(sarr1, sres);
280+
281+
for (int i = 0; i < INVOCATIONS; i++) {
282+
rotateLeftWithZeroArr_subword();
283+
}
284+
checkResults(sarr1, sres);
285+
286+
// Test rotate with a variable assigned with zero.
287+
for (int i = 0; i < INVOCATIONS; i++) {
288+
rotateRightWithZeroVar_subword();
289+
}
290+
checkResults(sarr1, sres);
291+
292+
for (int i = 0; i < INVOCATIONS; i++) {
293+
rotateLeftWithZeroVar_subword();
294+
}
295+
checkResults(sarr1, sres);
296+
}
297+
298+
public static void main(String[] args) {
299+
test();
300+
test_subword();
301+
}
302+
}

0 commit comments

Comments
 (0)