Skip to content

Commit 69341be

Browse files
committed
8261147: C2: Node is wrongly marked as reduction resulting in a wrong execution due to wrong vector instructions
Reviewed-by: roland Backport-of: f791fdf
1 parent feee8ef commit 69341be

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

src/hotspot/share/opto/loopopts.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,10 +2041,14 @@ void PhaseIdealLoop::clone_loop( IdealLoopTree *loop, Node_List &old_new, int dd
20412041

20422042
// Step 1: Clone the loop body. Make the old->new mapping.
20432043
uint i;
2044-
for( i = 0; i < loop->_body.size(); i++ ) {
2045-
Node *old = loop->_body.at(i);
2046-
Node *nnn = old->clone();
2047-
old_new.map( old->_idx, nnn );
2044+
for (i = 0; i < loop->_body.size(); i++) {
2045+
Node* old = loop->_body.at(i);
2046+
Node* nnn = old->clone();
2047+
old_new.map(old->_idx, nnn);
2048+
if (old->is_reduction()) {
2049+
// Reduction flag is not copied by default. Copy it here when cloning the entire loop body.
2050+
nnn->add_flag(Node::Flag_is_reduction);
2051+
}
20482052
if (C->do_vector_loop()) {
20492053
cm.verify_insert_and_clone(old, nnn, cm.clone_idx());
20502054
}

src/hotspot/share/opto/node.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,10 @@ Node *Node::clone() const {
503503
C->add_macro_node(n);
504504
if (is_expensive())
505505
C->add_expensive_node(n);
506+
if (n->is_reduction()) {
507+
// Do not copy reduction information. This must be explicitly set by the calling code.
508+
n->remove_flag(Node::Flag_is_reduction);
509+
}
506510
BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
507511
bs->register_potential_barrier_node(n);
508512
// If the cloned node is a range check dependent CastII, add it to the list.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its 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+
/**
25+
* @test
26+
* @bug 8261147
27+
* @summary Cloned node in AddNode::Ideal is no longer a reduction but is still marked as such leading to wrong vectorization.
28+
* @run main/othervm -Xcomp -XX:CompileCommand=compileonly,compiler.loopopts.superword.TestWronglyMarkedReduction::*
29+
* compiler.loopopts.superword.TestWronglyMarkedReduction
30+
*/
31+
package compiler.loopopts.superword;
32+
33+
public class TestWronglyMarkedReduction {
34+
public static long b = 0;
35+
36+
public static void main(String[] p) {
37+
TestWronglyMarkedReduction u = new TestWronglyMarkedReduction();
38+
for (int i = 0; i < 1000; i++) {
39+
b = 0;
40+
test();
41+
}
42+
}
43+
44+
public static void test() {
45+
long r[] = new long[20];
46+
for (int q = 0; q < 12; ++q) {
47+
for (int i = 1; i < 6; ++i) {
48+
r[i + 1] += b;
49+
}
50+
b += 2;
51+
}
52+
check(r);
53+
}
54+
55+
public static void check(long[] a) {
56+
for (int j = 0; j < 20; j++) {
57+
if (j >= 2 && j <= 6) {
58+
if (a[j] != 132) {
59+
throw new RuntimeException("expected 132 at index " + j + " but got " + a[j]);
60+
}
61+
} else if (a[j] != 0) {
62+
throw new RuntimeException("expected 0 at index " + j + " but got " + a[j]);
63+
}
64+
}
65+
}
66+
}
67+

0 commit comments

Comments
 (0)