Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.

Commit f791fdf

Browse files
committed
8261147: C2: Node is wrongly marked as reduction resulting in a wrong execution due to wrong vector instructions
Reviewed-by: thartmann, kvn
1 parent 1196b35 commit f791fdf

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
@@ -2203,10 +2203,14 @@ void PhaseIdealLoop::clone_loop( IdealLoopTree *loop, Node_List &old_new, int dd
22032203

22042204
// Step 1: Clone the loop body. Make the old->new mapping.
22052205
uint i;
2206-
for( i = 0; i < loop->_body.size(); i++ ) {
2207-
Node *old = loop->_body.at(i);
2208-
Node *nnn = old->clone();
2209-
old_new.map( old->_idx, nnn );
2206+
for (i = 0; i < loop->_body.size(); i++) {
2207+
Node* old = loop->_body.at(i);
2208+
Node* nnn = old->clone();
2209+
old_new.map(old->_idx, nnn);
2210+
if (old->is_reduction()) {
2211+
// Reduction flag is not copied by default. Copy it here when cloning the entire loop body.
2212+
nnn->add_flag(Node::Flag_is_reduction);
2213+
}
22102214
if (C->do_vector_loop()) {
22112215
cm.verify_insert_and_clone(old, nnn, cm.clone_idx());
22122216
}

src/hotspot/share/opto/node.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,10 @@ Node *Node::clone() const {
528528
// If it is applicable, it will happen anyway when the cloned node is registered with IGVN.
529529
n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn);
530530
}
531+
if (n->is_reduction()) {
532+
// Do not copy reduction information. This must be explicitly set by the calling code.
533+
n->remove_flag(Node::Flag_is_reduction);
534+
}
531535
BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
532536
bs->register_potential_barrier_node(n);
533537

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)