Skip to content

Commit fdb4350

Browse files
author
Daniel Lundén
committed
8324345: Stack overflow during C2 compilation when splitting memory phi
Reviewed-by: thartmann, kvn
1 parent f2ba2eb commit fdb4350

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed

src/hotspot/share/opto/escape.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3879,7 +3879,7 @@ PhiNode *ConnectionGraph::create_split_phi(PhiNode *orig_phi, int alias_idx, Gro
38793879
// Return a new version of Memory Phi "orig_phi" with the inputs having the
38803880
// specified alias index.
38813881
//
3882-
PhiNode *ConnectionGraph::split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist) {
3882+
PhiNode *ConnectionGraph::split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, uint rec_depth) {
38833883
assert(alias_idx != Compile::AliasIdxBot, "can't split out bottom memory");
38843884
Compile *C = _compile;
38853885
PhaseGVN* igvn = _igvn;
@@ -3895,7 +3895,7 @@ PhiNode *ConnectionGraph::split_memory_phi(PhiNode *orig_phi, int alias_idx, Gro
38953895
bool finished = false;
38963896
while(!finished) {
38973897
while (idx < phi->req()) {
3898-
Node *mem = find_inst_mem(phi->in(idx), alias_idx, orig_phi_worklist);
3898+
Node *mem = find_inst_mem(phi->in(idx), alias_idx, orig_phi_worklist, rec_depth + 1);
38993899
if (mem != nullptr && mem->is_Phi()) {
39003900
PhiNode *newphi = create_split_phi(mem->as_Phi(), alias_idx, orig_phi_worklist, new_phi_created);
39013901
if (new_phi_created) {
@@ -4037,7 +4037,12 @@ void ConnectionGraph::move_inst_mem(Node* n, GrowableArray<PhiNode *> &orig_phi
40374037
// Search memory chain of "mem" to find a MemNode whose address
40384038
// is the specified alias index.
40394039
//
4040-
Node* ConnectionGraph::find_inst_mem(Node *orig_mem, int alias_idx, GrowableArray<PhiNode *> &orig_phis) {
4040+
#define FIND_INST_MEM_RECURSION_DEPTH_LIMIT 1000
4041+
Node* ConnectionGraph::find_inst_mem(Node *orig_mem, int alias_idx, GrowableArray<PhiNode *> &orig_phis, uint rec_depth) {
4042+
if (rec_depth > FIND_INST_MEM_RECURSION_DEPTH_LIMIT) {
4043+
_compile->record_failure(_invocation > 0 ? C2Compiler::retry_no_iterative_escape_analysis() : C2Compiler::retry_no_escape_analysis());
4044+
return nullptr;
4045+
}
40414046
if (orig_mem == nullptr) {
40424047
return orig_mem;
40434048
}
@@ -4111,7 +4116,7 @@ Node* ConnectionGraph::find_inst_mem(Node *orig_mem, int alias_idx, GrowableArra
41114116
if (result == mmem->base_memory()) {
41124117
// Didn't find instance memory, search through general slice recursively.
41134118
result = mmem->memory_at(C->get_general_index(alias_idx));
4114-
result = find_inst_mem(result, alias_idx, orig_phis);
4119+
result = find_inst_mem(result, alias_idx, orig_phis, rec_depth + 1);
41154120
if (C->failing()) {
41164121
return nullptr;
41174122
}
@@ -4179,7 +4184,7 @@ Node* ConnectionGraph::find_inst_mem(Node *orig_mem, int alias_idx, GrowableArra
41794184
orig_phis.append_if_missing(mphi);
41804185
} else if (C->get_alias_index(t) != alias_idx) {
41814186
// Create a new Phi with the specified alias index type.
4182-
result = split_memory_phi(mphi, alias_idx, orig_phis);
4187+
result = split_memory_phi(mphi, alias_idx, orig_phis, rec_depth + 1);
41834188
}
41844189
}
41854190
// the result is either MemNode, PhiNode, InitializeNode.

src/hotspot/share/opto/escape.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -549,10 +549,10 @@ class ConnectionGraph: public ArenaObj {
549549
bool split_AddP(Node *addp, Node *base);
550550

551551
PhiNode *create_split_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, bool &new_created);
552-
PhiNode *split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist);
552+
PhiNode *split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, uint rec_depth);
553553

554554
void move_inst_mem(Node* n, GrowableArray<PhiNode *> &orig_phis);
555-
Node* find_inst_mem(Node* mem, int alias_idx,GrowableArray<PhiNode *> &orig_phi_worklist);
555+
Node* find_inst_mem(Node* mem, int alias_idx,GrowableArray<PhiNode *> &orig_phi_worklist, uint rec_depth = 0);
556556
Node* step_through_mergemem(MergeMemNode *mmem, int alias_idx, const TypeOopPtr *toop);
557557

558558
Node_Array _node_map; // used for bookkeeping during type splitting
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2024, 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 8324345
27+
* @summary Ensure that ConnectionGraph::find_inst_mem does not cause a stack
28+
* overflow.
29+
*
30+
* @run main/othervm -Xcomp -XX:CompileThreshold=10 -XX:-TieredCompilation
31+
* -XX:CompileCommand=CompileOnly,javax.swing.plaf.basic.BasicLookAndFeel::initComponentDefaults
32+
* -XX:CompileCommand=MemLimit,*.*,0
33+
* compiler.escapeAnalysis.TestFindInstMemRecursion
34+
*
35+
*/
36+
37+
package compiler.escapeAnalysis;
38+
39+
import javax.swing.*;
40+
import javax.swing.plaf.metal.*;
41+
42+
public class TestFindInstMemRecursion {
43+
public static void main(String[] args) throws Exception {
44+
LookAndFeel lookAndFeel = new MetalLookAndFeel();
45+
for (int i = 0; i < 20; ++i) {
46+
UIManager.setLookAndFeel(lookAndFeel);
47+
}
48+
}
49+
}

test/hotspot/jtreg/compiler/vectorapi/VectorReplicateLongSpecialImmTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @library /test/lib
3737
* @requires os.arch == "aarch64"
3838
* @modules jdk.incubator.vector
39-
* @run testng/othervm -XX:UseSVE=0 -XX:-TieredCompilation -XX:CompileThreshold=100 compiler.vectorapi.VectorReplicateLongSpecialImmTest
39+
* @run testng/othervm -XX:UseSVE=0 -XX:-TieredCompilation -XX:CompileThreshold=100 -XX:+IgnoreUnrecognizedVMOptions -XX:CompileCommand=MemLimit,*.*,0 compiler.vectorapi.VectorReplicateLongSpecialImmTest
4040
*/
4141
public class VectorReplicateLongSpecialImmTest {
4242

0 commit comments

Comments
 (0)