Skip to content

Commit

Permalink
8272574: C2: assert(false) failed: Bad graph detected in build_loop_late
Browse files Browse the repository at this point in the history
Backport-of: 16c3ad1ff4d9a0e21f15656c73a96a6c143c811a
  • Loading branch information
TobiHartmann committed Sep 29, 2021
1 parent da77acb commit 2b393f9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
21 changes: 18 additions & 3 deletions src/hotspot/share/opto/loopPredicate.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2021, 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
Expand Down Expand Up @@ -712,7 +712,7 @@ class Invariance : public StackObj {
// Returns true if the predicate of iff is in "scale*iv + offset u< load_range(ptr)" format
// Note: this function is particularly designed for loop predication. We require load_range
// and offset to be loop invariant computed on the fly by "invar"
bool IdealLoopTree::is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invariance& invar) const {
bool IdealLoopTree::is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invariance& invar DEBUG_ONLY(COMMA ProjNode *predicate_proj)) const {
if (!is_loop_exit(iff)) {
return false;
}
Expand Down Expand Up @@ -753,6 +753,21 @@ bool IdealLoopTree::is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invari
if (offset && !invar.is_invariant(offset)) { // offset must be invariant
return false;
}
#ifdef ASSERT
if (offset && phase->has_ctrl(offset)) {
Node* offset_ctrl = phase->get_ctrl(offset);
if (phase->get_loop(predicate_proj) == phase->get_loop(offset_ctrl) &&
phase->is_dominator(predicate_proj, offset_ctrl)) {
// If the control of offset is loop predication promoted by previous pass,
// then it will lead to cyclic dependency.
// Previously promoted loop predication is in the same loop of predication
// point.
// This situation can occur when pinning nodes too conservatively - can we do better?
assert(false, "cyclic dependency prevents range check elimination, idx: offset %d, offset_ctrl %d, predicate_proj %d",
offset->_idx, offset_ctrl->_idx, predicate_proj->_idx);
}
}
#endif
return true;
}

Expand Down Expand Up @@ -1232,7 +1247,7 @@ bool PhaseIdealLoop::loop_predication_impl_helper(IdealLoopTree *loop, ProjNode*
loop->dump_head();
}
#endif
} else if (cl != NULL && loop->is_range_check_if(iff, this, invar)) {
} else if (cl != NULL && loop->is_range_check_if(iff, this, invar DEBUG_ONLY(COMMA predicate_proj))) {
// Range check for counted loops
const Node* cmp = bol->in(1)->as_Cmp();
Node* idx = cmp->in(1);
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/opto/loopnode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ class IdealLoopTree : public ResourceObj {
bool policy_range_check( PhaseIdealLoop *phase ) const;

// Return TRUE if "iff" is a range check.
bool is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invariance& invar) const;
bool is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invariance& invar DEBUG_ONLY(COMMA ProjNode *predicate_proj)) const;

// Estimate the number of nodes required when cloning a loop (body).
uint est_loop_clone_sz(uint factor) const;
Expand Down
9 changes: 8 additions & 1 deletion src/hotspot/share/opto/memnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,14 @@ Node *LoadNode::split_through_phi(PhaseGVN *phase) {
the_clone = x; // Remember for possible deletion.
// Alter data node to use pre-phi inputs
if (this->in(0) == region) {
x->set_req(0, in);
if (mem->is_Phi() && (mem->in(0) == region) && mem->in(i)->in(0) != NULL &&
MemNode::all_controls_dominate(address, region)) {
// Enable other optimizations such as loop predication which does not work
// if we directly pin the node to node `in`
x->set_req(0, mem->in(i)->in(0)); // Use same control as memory
} else {
x->set_req(0, in);
}
} else {
x->set_req(0, NULL);
}
Expand Down
69 changes: 69 additions & 0 deletions test/hotspot/jtreg/compiler/loopopts/TestLoopPredicateDep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. 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 8272574
* @summary Crashes in PhaseIdealLoop::build_loop_late_post_work
* @requires vm.compiler2.enabled
*
* @run main TestLoopPredicateDep
*
*/

public class TestLoopPredicateDep {
public static void getPermutations(byte[] inputArray, byte[][] outputArray) {
int[] indexes = new int[]{0, 2};

for (int a = 0; a < a + 16; a++) {
int oneIdx = indexes[0]++;
for (int b = a + 1; b < inputArray.length; b++) {
int twoIdx = indexes[1]++;
outputArray[twoIdx][0] = inputArray[a];
outputArray[twoIdx][1] = inputArray[b];
}
}
}

public static void main(String[] args) {
final byte[] inputArray = new byte[]{0, 1};
final byte[][] outputArray = new byte[3][2];

for (int i = 0; i < 10; ++i) {
Thread t = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 1000000; i++) {
getPermutations(inputArray, outputArray);
}
}
});
t.setDaemon(true);
t.start();
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
}

}

1 comment on commit 2b393f9

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.