From fe430ad121464d731402ef4add6d57dadffe723e Mon Sep 17 00:00:00 2001 From: Vladimir Ivanov Date: Tue, 14 Jul 2026 19:14:06 +0000 Subject: [PATCH] 8387940: C2: Stress allocation elimination failures Reviewed-by: chagedorn, kvn, qamai --- src/hotspot/share/opto/c2_globals.hpp | 8 +++ src/hotspot/share/opto/compile.cpp | 3 +- src/hotspot/share/opto/macro.cpp | 26 ++++--- .../compiler/arguments/TestStressOptions.java | 6 +- .../StressEliminateAllocationsIRTest.java | 67 +++++++++++++++++++ 5 files changed, 100 insertions(+), 10 deletions(-) create mode 100644 test/hotspot/jtreg/compiler/escapeAnalysis/StressEliminateAllocationsIRTest.java diff --git a/src/hotspot/share/opto/c2_globals.hpp b/src/hotspot/share/opto/c2_globals.hpp index cbe149fd01a..9ff88e8c310 100644 --- a/src/hotspot/share/opto/c2_globals.hpp +++ b/src/hotspot/share/opto/c2_globals.hpp @@ -601,6 +601,14 @@ "Number of fields in instance limit for scalar replacement") \ range(0, max_jint) \ \ + product(bool, StressEliminateAllocations, false, DIAGNOSTIC, \ + "Randomly fail allocation elimination attempts") \ + \ + product(uint, StressEliminateAllocationsMean, 20, DIAGNOSTIC, \ + "The expected number of elimination checks made until " \ + "a random failure.") \ + range(1, max_juint) \ + \ product(bool, OptimizePtrCompare, true, \ "Use escape analysis to optimize pointers compare") \ \ diff --git a/src/hotspot/share/opto/compile.cpp b/src/hotspot/share/opto/compile.cpp index 0c7083ed723..93d8e4c425d 100644 --- a/src/hotspot/share/opto/compile.cpp +++ b/src/hotspot/share/opto/compile.cpp @@ -748,7 +748,8 @@ Compile::Compile(ciEnv* ci_env, ciMethod* target, int osr_bci, if (StressLCM || StressGCM || StressIGVN || StressCCP || StressIncrementalInlining || StressMacroExpansion || StressMacroElimination || StressUnstableIfTraps || - StressBailout || StressLoopPeeling || StressCountedLoop) { + StressBailout || StressLoopPeeling || StressCountedLoop || + StressEliminateAllocations) { initialize_stress_seed(directive); } diff --git a/src/hotspot/share/opto/macro.cpp b/src/hotspot/share/opto/macro.cpp index 0ee073e8b06..a4d03970fcf 100644 --- a/src/hotspot/share/opto/macro.cpp +++ b/src/hotspot/share/opto/macro.cpp @@ -931,7 +931,9 @@ SafePointScalarObjectNode* PhaseMacroExpand::create_scalarized_object_descriptio // We weren't able to find a value for this field, // give up on eliminating this allocation. - if (field_val == nullptr) { + bool force_scalarization_failure = StressEliminateAllocations && + (C->random() % StressEliminateAllocationsMean == 0); + if (field_val == nullptr || force_scalarization_failure) { uint last = sfpt->req() - 1; for (int k = 0; k < j; k++) { sfpt->del_req(last--); @@ -940,13 +942,21 @@ SafePointScalarObjectNode* PhaseMacroExpand::create_scalarized_object_descriptio #ifndef PRODUCT if (PrintEliminateAllocations) { - if (field != nullptr) { - tty->print("=== At SafePoint node %d can't find value of field: ", sfpt->_idx); - field->print(); - int field_idx = C->get_alias_index(field_addr_type); - tty->print(" (alias_idx=%d)", field_idx); - } else { // Array's element - tty->print("=== At SafePoint node %d can't find value of array element [%d]", sfpt->_idx, j); + tty->print("=== At SafePoint node %d ", sfpt->_idx); + if (field_val == nullptr) { + tty->print_raw("can't find value of "); + + if (field != nullptr) { + tty->print_raw("field: "); + field->print(); + int field_idx = C->get_alias_index(field_addr_type); + tty->print(" (alias_idx=%d)", field_idx); + } else { // Array's element + tty->print("array element [%d]", j); + } + } else { + assert(force_scalarization_failure, "sanity"); + tty->print_raw("forcibly abort elimination"); } tty->print(", which prevents elimination of: "); if (res == nullptr) diff --git a/test/hotspot/jtreg/compiler/arguments/TestStressOptions.java b/test/hotspot/jtreg/compiler/arguments/TestStressOptions.java index 534ec9d2d97..99cf06110d6 100644 --- a/test/hotspot/jtreg/compiler/arguments/TestStressOptions.java +++ b/test/hotspot/jtreg/compiler/arguments/TestStressOptions.java @@ -24,7 +24,7 @@ /* * @test * @key stress randomness - * @bug 8252219 8256535 8317349 8319879 8335334 8325478 + * @bug 8252219 8256535 8317349 8319879 8335334 8325478 8387940 * @requires vm.compiler2.enabled * @summary Tests that different combinations of stress options and * -XX:StressSeed=N are accepted. @@ -60,6 +60,10 @@ * compiler.arguments.TestStressOptions * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+StressMacroElimination -XX:StressSeed=42 * compiler.arguments.TestStressOptions + * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+StressEliminateAllocations + * compiler.arguments.TestStressOptions + * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+StressEliminateAllocations -XX:StressSeed=42 + * compiler.arguments.TestStressOptions */ package compiler.arguments; diff --git a/test/hotspot/jtreg/compiler/escapeAnalysis/StressEliminateAllocationsIRTest.java b/test/hotspot/jtreg/compiler/escapeAnalysis/StressEliminateAllocationsIRTest.java new file mode 100644 index 00000000000..b97847b9b28 --- /dev/null +++ b/test/hotspot/jtreg/compiler/escapeAnalysis/StressEliminateAllocationsIRTest.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2026, 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 8387940 + * @requires vm.compiler2.enabled + * @summary C2: Stress allocation elimination failures + * + * @library /test/lib / + * @run driver ${test.main.class} + */ + +package compiler.escapeAnalysis; + +import compiler.lib.ir_framework.*; + +public class StressEliminateAllocationsIRTest { + public static void main(String[] args) { + TestFramework.runWithFlags("-XX:+UnlockDiagnosticVMOptions", + "-XX:+StressEliminateAllocations", + "-XX:StressEliminateAllocationsMean=1"); + } + + static class A { + final int i; + A(int i) { + this.i = i; + } + } + + @Test + @IR(counts = {IRNode.ALLOC, "1"}) + @Arguments(values = Argument.NUMBER_42) + private static int test(int i) { + // Even though the object is scalar replaceable, + // allocation elimination unconditionally fails in stress mode. + A a = new A(i); + + dontInline(); + + return a.i; + } + + @DontInline + private static void dontInline() {} +}