changeset 59242:3b693618d084

8233032: assert(in_bb(n)) failed: must be Summary: Find first and last memory state of a load pack without relying on bb indices. Reviewed-by: roland, kvn, thartmann Contributed-by: Roland Westrelin <rwestrel@redhat.com>, Christian Hagedorn <christian.hagedorn@oracle.com>
author chagedorn
date Tue, 10 Dec 2019 09:28:38 +0100
parents 9ea6521df290
children a4fb32538898
files src/hotspot/share/opto/superword.cpp test/hotspot/jtreg/compiler/loopopts/superword/CoLocatePack.java
diffstat 2 files changed, 115 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/hotspot/share/opto/superword.cpp	Mon Dec 09 21:38:12 2019 -0800
+++ b/src/hotspot/share/opto/superword.cpp	Tue Dec 10 09:28:38 2019 +0100
@@ -2255,30 +2255,38 @@
     // we use the memory state of the last load. However, if any load could
     // not be moved down due to the dependence constraint, we use the memory
     // state of the first load.
-    Node* first_mem = pk->at(0)->in(MemNode::Memory);
-    Node* last_mem = first_mem;
-    for (uint i = 1; i < pk->size(); i++) {
-      Node* ld = pk->at(i);
-      Node* mem = ld->in(MemNode::Memory);
-      assert(in_bb(first_mem) || in_bb(mem) || mem == first_mem, "2 different memory state from outside the loop?");
-      if (in_bb(mem)) {
-        if (in_bb(first_mem) && bb_idx(mem) < bb_idx(first_mem)) {
-          first_mem = mem;
-        }
-        if (!in_bb(last_mem) || bb_idx(mem) > bb_idx(last_mem)) {
-          last_mem = mem;
+    Node* last_mem  = pk->at(0)->in(MemNode::Memory);
+    Node* first_mem = last_mem;
+    // Walk the memory graph from the current first load until the
+    // start of the loop and check if nodes on the way are memory
+    // edges of loads in the pack. The last one we encounter is the
+    // first load.
+    for (Node* current = first_mem; in_bb(current); current = current->is_Phi() ? current->in(LoopNode::EntryControl) : current->in(MemNode::Memory)) {
+     assert(current->is_Mem() || (current->is_Phi() && current->in(0) == bb()), "unexpected memory");
+     for (uint i = 1; i < pk->size(); i++) {
+        Node* ld = pk->at(i);
+        if (ld->in(MemNode::Memory) == current) {
+          first_mem = current;
+          break;
         }
       }
     }
+    // Find the last load by going over the pack again and walking
+    // the memory graph from the loads of the pack to the memory of
+    // the first load. If we encounter the memory of the current last
+    // load, then we started from further down in the memory graph and
+    // the load we started from is the last load. Check for dependence
+    // constraints in that loop as well.
     bool schedule_last = true;
     for (uint i = 0; i < pk->size(); i++) {
       Node* ld = pk->at(i);
-      for (Node* current = last_mem; current != ld->in(MemNode::Memory);
-           current=current->in(MemNode::Memory)) {
-        assert(current != first_mem, "corrupted memory graph");
-        if(current->is_Mem() && !independent(current, ld)){
+      for (Node* current = ld->in(MemNode::Memory); current != first_mem; current = current->in(MemNode::Memory)) {
+        assert(current->is_Mem() && in_bb(current), "unexpected memory");
+        if (current->in(MemNode::Memory) == last_mem) {
+          last_mem = ld->in(MemNode::Memory);
+        }
+        if (!independent(current, ld)) {
           schedule_last = false; // a later store depends on this load
-          break;
         }
       }
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/compiler/loopopts/superword/CoLocatePack.java	Tue Dec 10 09:28:38 2019 +0100
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2019, 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 8233032
+ * @summary Tests SuperWord::co_locate_pack() involving a load pack that relies on a sandwiched and moved StoreF node.
+ *
+ * @run main/othervm -Xbatch -XX:+IgnoreUnrecognizedVMOptions -XX:UseAVX=1
+ *      -XX:CompileCommand=compileonly,compiler.loopopts.superword.CoLocatePack::test
+ *      compiler.loopopts.superword.CoLocatePack
+ */
+package compiler.loopopts.superword;
+
+public class CoLocatePack {
+
+    public static long lFld = 10;
+    public static float fFld = 11.2f;
+    public int iFld = 12;
+
+    public void test() {
+        int iArr[] = new int[200];
+        float fArr[] = new float[200];
+
+        /*
+         * The IR for this loop contains the following StoreF chain after unrolling once:
+         * StoreF 1 -> StoreF 2 -> StoreF 3 -> StoreF 4 -> StoreF 5 -> StoreF 6
+         *
+         * The superword algorithm creates a pack for [ StoreF 2 and 5 ] and one for [ StoreF 3 and 6 ]
+         * (The pack [ StoreF 1 and 4 ] is filtered out). As a result, StoreF 3 and 4 are sandwiched between
+         * StoreF 2 and 5. SuperWord::co_locate_pack() will move both after StoreF 5 to remove any dependencies
+         * within the pack:
+         * StoreF 1 -> [ StoreF 2 -> StoreF 5 ] -> StoreF 3 -> StoreF 4 -> StoreF 6
+         *
+         * Afterwards, StoreF 4 is moved before StoreF 3 to remove any dependency within [ StoreF 3 -> StoreF 6 ]
+         * The resulting chain looks like this:
+         * StoreF 1 -> [ StoreF 2 -> StoreF 5 ] -> StoreF 4 -> [ StoreF 3 -> StoreF 6 ]
+         *
+         * When later processing a load pack depending on StoreF 4 and 5, the first and last memory state of the load pack are
+         * determined by using the bb indices. However, those were not updated before when moving nodes around and
+         * bb_idx(4) < bb_idx(5) still holds even though they swapped positions in the IR. Therefore, it wrongly uses the memory
+         * state of the first load (StoreF 5) in the pack as the last memory state. As a result, the graph walk always starts
+         * following the input of StoreF 5 (which should actually be StoreF 4) and will move beyond a loop phi as the stop
+         * condition is never met for a node having another memory state than the first one of the load pack. Eventually a
+         * bb index for a node outside of the loop is read resulting in an assertion failure.
+         *
+         * The fix uses a different approach to find the first and last memory state of a load pack without depending on bb indices.
+         */
+        for (int i = 5; i < 169; i++) {
+            fArr[i + 1] += ((long)(fFld) | 1); // StoreF 1/4
+            iFld += lFld;
+            fArr[i - 1] -= 20; // StoreF 2/5
+            fFld += i;
+            fArr[i + 1] -= -117; // StoreF 3/6
+
+            int j = 10;
+            do {
+            } while (--j > 0);
+
+            iArr[i] += 11;
+        }
+    }
+
+    public static void main(String[] strArr) {
+        CoLocatePack _instance = new CoLocatePack();
+        for (int i = 0; i < 1000; i++ ) {
+            _instance.test();
+        }
+    }
+}