|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | +package compiler.loopopts.superword; |
| 25 | + |
| 26 | +import compiler.lib.ir_framework.*; |
| 27 | + |
| 28 | +/* |
| 29 | + * @test |
| 30 | + * @bug 8350756 |
| 31 | + * @summary Test case where the multiversion fast_loop disappears, and we should |
| 32 | + * constant fold the multiversion_if, to remove the slow_loop. |
| 33 | + * @library /test/lib / |
| 34 | + * @run driver compiler.loopopts.superword.TestMultiversionRemoveUselessSlowLoop |
| 35 | + */ |
| 36 | + |
| 37 | +public class TestMultiversionRemoveUselessSlowLoop { |
| 38 | + |
| 39 | + public static void main(String[] args) { |
| 40 | + TestFramework framework = new TestFramework(TestMultiversionRemoveUselessSlowLoop.class); |
| 41 | + // No traps means we cannot use the predicates version for SuperWord / AutoVectorization, |
| 42 | + // and instead use multiversioning directly. |
| 43 | + framework.addFlags("-XX:-TieredCompilation", "-XX:PerMethodTrapLimit=0"); |
| 44 | + framework.setDefaultWarmup(0); // simulates Xcomp |
| 45 | + framework.start(); |
| 46 | + } |
| 47 | + |
| 48 | + public static final int SIZE = 20; |
| 49 | + public static final int[] a = new int[SIZE]; |
| 50 | + public static final int[] b = new int[SIZE]; |
| 51 | + public static final int SIZE2 = 10_000; |
| 52 | + public static final int[] a2 = new int[SIZE2]; |
| 53 | + public static final int[] b2 = new int[SIZE2]; |
| 54 | + |
| 55 | + @Test |
| 56 | + @IR(counts = {"pre .* multiversion_fast", "= 2", // regular pre-main-post for both loops |
| 57 | + "main .* multiversion_fast", "= 2", |
| 58 | + "post .* multiversion_fast", "= 2", |
| 59 | + "multiversion_delayed_slow", "= 2", // both have the delayed slow_loop |
| 60 | + "multiversion", "= 8", // nothing unexpected |
| 61 | + IRNode.OPAQUE_MULTIVERSIONING, "= 2"}, // Both multiversion_if are still here |
| 62 | + applyIfPlatform = {"64-bit", "true"}, |
| 63 | + applyIfCPUFeatureOr = {"sse4.1", "true", "asimd", "true"}, |
| 64 | + phase = CompilePhase.PHASEIDEALLOOP1) |
| 65 | + @IR(counts = {"pre .* multiversion_fast", "= 2", |
| 66 | + "main .* multiversion_fast", "= 1", // The first main loop is fully unrolled |
| 67 | + "post .* multiversion_fast", "= 3", // the second loop is vectorized, and has a vectorized post loop |
| 68 | + "multiversion_delayed_slow", "= 1", // As a consequence of the first main loop being removed, we constant fold the multiversion_if |
| 69 | + "multiversion", "= 7", // nothing unexpected |
| 70 | + IRNode.OPAQUE_MULTIVERSIONING, "= 1"}, // The multiversion_if of the first loop was constant folded, because the main loop disappeared. |
| 71 | + applyIfPlatform = {"64-bit", "true"}, |
| 72 | + applyIfCPUFeatureOr = {"sse4.1", "true", "asimd", "true"}, |
| 73 | + phase = CompilePhase.PHASEIDEALLOOP_ITERATIONS) |
| 74 | + @IR(counts = {"pre .* multiversion_fast.*", ">= 1", // In some cases, the pre loop of the first loop also disappears because it only has a single iteration |
| 75 | + "pre .* multiversion_fast.*", "<= 2", // but not in other cases the pre loop of the first loop remains. |
| 76 | + "main .* multiversion_fast", "= 1", |
| 77 | + "post .* multiversion_fast", "= 3", |
| 78 | + "multiversion_delayed_slow", "= 0", // The second loop's multiversion_if was also not used, so it is constant folded after loop opts. |
| 79 | + "multiversion", ">= 5", // nothing unexpected |
| 80 | + "multiversion", "<= 6", // nothing unexpected |
| 81 | + IRNode.OPAQUE_MULTIVERSIONING, "= 0"}, // After loop-opts, we also constant fold the multiversion_if of the second loop, as it is unused. |
| 82 | + applyIfPlatform = {"64-bit", "true"}, |
| 83 | + applyIfCPUFeatureOr = {"sse4.1", "true", "asimd", "true"}, |
| 84 | + phase = CompilePhase.PRINT_IDEAL) |
| 85 | + public static void testIR() { |
| 86 | + // This loop is short, and the multiversion_fast main loop eventuall is fully unrolled. |
| 87 | + for (int i = 0; i < SIZE; i++) { |
| 88 | + a[i] = b[i]; |
| 89 | + } |
| 90 | + // We take this second loop with a larger limit so that loop opts keeps going once the loop |
| 91 | + // above is fully optimized. It also gives us a reference where the main loop of the |
| 92 | + // multiverion fast_loop does not disappear. |
| 93 | + for (int i = 0; i < SIZE2; i++) { |
| 94 | + a2[i] = b2[i]; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + static long instanceCount; |
| 99 | + static int iFld; |
| 100 | + static int iFld1; |
| 101 | + |
| 102 | + // The inner loop is Multiversioned, then PreMainPost and Unroll. |
| 103 | + // Eventually, both the fast and slow loops (pre main and post) disappear, |
| 104 | + // and leave us with a simple if-diamond using the multiversion_if. |
| 105 | + // |
| 106 | + // Verification code in PhaseIdealLoop::conditional_move finds this diamond |
| 107 | + // and expects a Bool but gets an OpaqueMultiversioning instead. |
| 108 | + // |
| 109 | + // If we let the multiversion_if constant fold soon after the main fast loop |
| 110 | + // disappears, then this issue does not occur any more. |
| 111 | + @Test |
| 112 | + public static void testCrash() { |
| 113 | + boolean b2 = true; |
| 114 | + for (int i = 0; i < 1000; i++) { |
| 115 | + for (int i21 = 82; i21 > 9; --i21) { |
| 116 | + if (b2) |
| 117 | + break; |
| 118 | + iFld1 = iFld; |
| 119 | + b2 = true; |
| 120 | + } |
| 121 | + instanceCount = iFld1; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments