/* * Copyright (c) 2016, 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. */ // TODO add bugid and summary /* * @test * @library /testlibrary /test/lib /compiler/whitebox / * @build compiler.valhalla.valuetypes.ValueTypeTestBench * @run main ClassFileInstaller sun.hotspot.WhiteBox * @run main ClassFileInstaller jdk.test.lib.Platform * @run main/othervm -noverify -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI * compiler.valhalla.valuetypes.ValueTypeTestBench */ package compiler.valhalla.valuetypes; import compiler.whitebox.CompilerWhiteBoxTest; import jdk.internal.misc.Unsafe; import jdk.test.lib.Asserts; import jdk.test.lib.Platform; import jdk.test.lib.ProcessTools; import jdk.test.lib.OutputAnalyzer; import jdk.test.lib.Utils; import sun.hotspot.WhiteBox; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Hashtable; import java.util.regex.Matcher; import java.util.regex.Pattern; // Test value type __ByValue final class MyValue { final int x; final long y; final double z; private MyValue(int x, long y, double z) { this.x = x; this.y = y; this.z = z; } @DontInline public static MyValue createDontInline(int x, long y, double z) { return __Make MyValue(x, y, z); } @ForceInline public static MyValue createInline(int x, long y, double z) { return __Make MyValue(x, y, z); } @DontInline public String toStringDontInline() { return "MyValue: x=" + x + " y=" + y + " z=" + z; } @ForceInline public String toStringInline() { return "MyValue: x=" + x + " y=" + y + " z=" + z; } } public class ValueTypeTestBench { // Print ideal graph after execution of each test private static final boolean PRINT_GRAPH = true; // ========== Test definitions ========== // Receive value type through call to interpreter @Test(failOn = ALLOC + STORE) public double test1() { MyValue v = MyValue.createDontInline(rI, rL, rD); return v.x + v.y + v.z; } @DontCompile public void test1_verifier(boolean warmup) { double result = test1(); Asserts.assertEQ(result, rI + rL + rD); } // Receive value type from interpreter via parameter @Test(failOn = ALLOC + STORE) public double test2(MyValue v) { return v.x + v.y + v.z; } @DontCompile public void test2_verifier(boolean warmup) { MyValue v = MyValue.createDontInline(rI, rL, rD); double result = test2(v); Asserts.assertEQ(result, rI + rL + rD); } // Return incoming value type without accessing fields @Test(failOn = ALLOC + LOAD + STORE) public MyValue test3(MyValue v) { return v; } @DontCompile public void test3_verifier(boolean warmup) { MyValue v1 = MyValue.createDontInline(rI, rL, rD); MyValue v2 = test3(v1); Asserts.assertEQ(v1.x, v2.x); Asserts.assertEQ(v1.y, v2.y); Asserts.assertEQ(v1.z, v2.z); } // Create a value type in compiled code and only use fields. // Allocation should go away because value type does not escape. @Test(failOn = ALLOC + LOAD + STORE) public double test4() { MyValue v = MyValue.createInline(rI, rL, rD); return v.x + v.y + v.z; } @DontCompile public void test4_verifier(boolean warmup) { double result = test4(); Asserts.assertEQ(result, rI + rL + rD); } // Create a value type in compiled code and pass it to // an inlined compiled method via a call. @Test(failOn = ALLOC + LOAD + STORE) public double test5() { MyValue v = MyValue.createInline(rI, rL, rD); return test5Inline(v); } @ForceInline public double test5Inline(MyValue v) { return v.x + v.y + v.z; } @DontCompile public void test5_verifier(boolean warmup) { double result = test5(); Asserts.assertEQ(result, rI + rL + rD); } // Create a value type in compiled code and pass it to // the interpreter via a call. @Test(match = {ALLOC}, matchCount = {1}, failOn = LOAD) public double test6() { MyValue v = MyValue.createInline(rI, rL, rD); // Pass to interpreter return sumValue(v); } @DontCompile public void test6_verifier(boolean warmup) { double result = test6(); Asserts.assertEQ(result, rI + rL + rD); } // Create a value type in compiled code and pass it to // the interpreter by returning. @Test(match = {ALLOC}, matchCount = {1}, failOn = LOAD) public MyValue test7(int x, long y, double z) { return MyValue.createInline(x, y, z); } @DontCompile public void test7_verifier(boolean warmup) { MyValue v = test7(rI, rL, rD); double result = v.x + v.y + v.z; Asserts.assertEQ(result, rI + rL + rD); } // Merge value types created from two branches @Test(failOn = ALLOC + STORE) public double test8(boolean b) { MyValue v; if (b) { v = MyValue.createInline(rI, rL, rD); } else { v = MyValue.createDontInline(rI + 1, rL + 1, rD + 1); } return v.x + v.y + v.z; } @DontCompile public void test8_verifier(boolean warmup) { Asserts.assertEQ(test8(true), rI + rL + rD); Asserts.assertEQ(test8(false), rI + 1 + rL + 1 + ((double)rD + 1)); } // Merge value types created from two branches @Test(match = {ALLOC, STORE}, matchCount = {1, 3}, failOn = LOAD) public MyValue test9(boolean b) { MyValue v; if (b) { // Value type is not allocated v = MyValue.createInline(rI, rL, rD); } else { // Value type is allocated by the callee v = MyValue.createDontInline(rI + 1, rL + 1, rD + 1); } // Need to allocate value type if 'b' is true double sum = sumValue(v); if (b) { v = MyValue.createDontInline(rI, rL, sum); } else { v = MyValue.createDontInline(rI, rL, sum + 1); } // Don't need to allocate value type because both branches allocate return v; } @DontCompile public void test9_verifier(boolean warmup) { MyValue v = test9(true); Asserts.assertEQ(v.x, rI); Asserts.assertEQ(v.y, rL); Asserts.assertEQ(v.z, rI + rL + rD); v = test9(false); Asserts.assertEQ(v.x, rI); Asserts.assertEQ(v.y, rL); Asserts.assertEQ(v.z, rI + rL + ((double)rD + 1)); } // Merge value types created in a loop (not inlined) @Test(failOn = ALLOC + STORE) public double test10(int x, long y, double z) { MyValue v = MyValue.createDontInline(x, y, z); for (int i = 0; i < 10; ++i) { v = MyValue.createDontInline(v.x + 1, v.y + 1, v.z + 1); } return v.x + v.y + v.z; } @DontCompile public void test10_verifier(boolean warmup) { double result = test10(rI, rL, rD); Asserts.assertEQ(result, rI + rL + 20 + ((double)rD + 10)); } // Merge value types created in a loop (inlined) @Test(failOn = ALLOC + LOAD + STORE + LOOP) public double test11(int x, long y, double z) { MyValue v = MyValue.createInline(x, y, z); for (int i = 0; i < 10; ++i) { v = MyValue.createInline(v.x + 1, v.y + 1, v.z + 1); } return v.x + v.y + v.z; } @DontCompile public void test11_verifier(boolean warmup) { double result = test11(rI, rL, rD); Asserts.assertEQ(result, rI + rL + 20 + ((double)rD + 10)); } // Test loop with uncommon trap referencing a value type @Test(match = {TRAP, SCOBJ}, matchCount = {1, 1}, failOn = ALLOC + LOAD + STORE) public double test12(boolean b) { MyValue v = MyValue.createInline(rI, rL, rD); double result = 42; for (int i = 0; i < 1000; ++i) { if (b) { result += v.x; } else { // Uncommon trap referencing v. We delegate allocation to the // interpreter by adding a SafePointScalarObjectNode. result = sumValue(v); } } return result; } @DontCompile public void test12_verifier(boolean warmup) { double result = test12(warmup); Asserts.assertEQ(result, warmup ? 42 + (1000*(double)rI) : (rI + rL + rD)); } // Test loop with uncommon trap referencing a value type @Test(match = {TRAP, LOAD}, matchCount = {1, 1}, failOn = ALLOC + STORE + SCOBJ) public double test13(boolean b) { MyValue v = MyValue.createDontInline(rI, rL, rD); double result = 42; for (int i = 0; i < 1000; ++i) { if (b) { result += v.x; } else { // Uncommon trap referencing v. Should not allocate // but just pass the existing oop to the uncommon trap. result = sumValue(v); } } return result; } @DontCompile public void test13_verifier(boolean warmup) { double result = test13(warmup); Asserts.assertEQ(result, warmup ? 42 + (1000*(double)rI) : (rI + rL + rD)); } // Create a value type in a non-inlined method and then call a // non-inlined method on that value type. @Test(failOn = (ALLOC + STORE)) public String test14() { MyValue v = MyValue.createDontInline(32, 64L, 128.0); String s = v.toStringDontInline(); return s; } @DontCompile public void test14_verifier(boolean b) { String s = test14(); System.out.println("Result is: " + s); } // Create a value type in an inlined method and then call a // non-inlined method on that value type. @Test(match = {ALLOC}, matchCount = {1}) public String test15() { MyValue v = MyValue.createInline(65, 129L, 257.0); String s = v.toStringDontInline(); return s; } @DontCompile public void test15_verifier(boolean b) { String s = test15(); System.out.println("Result is: " + s); } // Create a value type in a non-inlined method and then call an // inlined method on that value type. Allocations are due to building // String objects and not due to allocating value types. @Test(match = {ALLOC}, matchCount = {2}) public String test16() { MyValue v = MyValue.createDontInline(130, 258L, 514.0); String s = v.toStringInline(); return s; } @DontCompile public void test16_verifier(boolean b) { String s = test16(); System.out.println("Result is: " + s); } // Create a value type in an inlined method and then call an // inlined method on that value type. @Test(match = {ALLOC}, matchCount = {2}) public String test17() { MyValue v = MyValue.createInline(259, 515L, 1027.0); String s = v.toStringInline(); return s; } @DontCompile public void test17_verifier(boolean b) { String s = test17(); System.out.println("Result is: " + s); } // ========== Helper methods ========== @DontCompile public double sumValue(MyValue v) { return v.x + v.y + v.z; } // ========== Test infrastructure ========== private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox(); private static final int COMP_LEVEL_ANY = -1; private static final int COMP_LEVEL_FULL_OPTIMIZATION = 4; private static final Hashtable tests = new Hashtable(); private static final int WARMUP = 10; // Regular expressions used to match nodes in the PrintIdeal output private static final String START = "(\\d+\\t(.*"; private static final String MID = ".*)+\\t===.*"; private static final String END = ")|"; private static final String ALLOC = START + "CallStaticJava" + MID + "_new_instance_Java" + END; private static final String LOAD = START + "Load" + MID + "valuetype\\*" + END; private static final String STORE = START + "Store" + MID + "valuetype\\*" + END; private static final String LOOP = START + "Loop" + MID + "" + END; private static final String TRAP = START + "CallStaticJava" + MID + "uncommon_trap" + END; // TODO: match field values of scalar replaced object private static final String SCOBJ = "(.*# ScObj.*" + END; // Random test values private static final int rI = Utils.getRandomInstance().nextInt(); private static final long rL = Utils.getRandomInstance().nextLong(); private static final double rD = Utils.getRandomInstance().nextDouble(); static { // Gather all test methods and put them in Hashtable for (Method m : ValueTypeTestBench.class.getDeclaredMethods()) { if (m.isAnnotationPresent(Test.class)) { tests.put("ValueTypeTestBench::" + m.getName(), m); } } } public static void main(String[] args) throws Throwable { if (args.length == 0) { // Run tests in own process and verify output OutputAnalyzer oa = ProcessTools.executeTestJvm("-noverify", "-XX:+UnlockDiagnosticVMOptions", "-Xbootclasspath/a:.", "-XX:+WhiteBoxAPI", "-XX:-TieredCompilation", "-XX:-BackgroundCompilation", "-XX:-UseOnStackReplacement", "-XX:CompileCommand=quiet", "-XX:+PrintCompilation", "-XX:+PrintIdeal", "-XX:+PrintOptoAssembly", "-XX:CompileCommand=compileonly,compiler.valhalla.valuetypes.ValueTypeTestBench::*", "-XX:CompileCommand=compileonly,compiler.valhalla.valuetypes.MyValue::*", ValueTypeTestBench.class.getName(), "run"); String output = oa.getOutput(); oa.shouldHaveExitValue(0); parseOutput(output); } else { // Execute tests ValueTypeTestBench bench = new ValueTypeTestBench(); bench.run(); } } public static void parseOutput(String output) throws Exception { String split = "b compiler.valhalla.valuetypes."; String[] compilations = output.split(split); // Print header System.out.println(compilations[0]); // Iterate over compilation output for (String graph : compilations) { String[] lines = graph.split("\\n"); String testName = lines[0].split(" ")[0]; Method test = tests.get(testName); if (test == null) { // Skip helper methods continue; } if (PRINT_GRAPH) { System.out.println("\nGraph for " + graph); } // Parse graph using regular expressions to determine if it contains forbidden nodes Test anno = test.getAnnotation(Test.class); String regexFail = anno.failOn(); if (!regexFail.isEmpty()) { Pattern pattern = Pattern.compile(regexFail.substring(0, regexFail.length()-1)); Matcher matcher = pattern.matcher(graph); boolean fail = false; while (matcher.find()) { System.out.println("Graph for '" + testName + "' contains forbidden node:"); System.out.println(matcher.group()); fail = true; } Asserts.assertFalse(fail, "Graph for '" + testName + "' contains forbidden nodes"); } String[] regexMatch = anno.match(); int[] matchCount = anno.matchCount(); for (int i = 0; i < regexMatch.length; ++i) { Pattern pattern = Pattern.compile(regexMatch[i].substring(0, regexMatch[i].length()-1)); Matcher matcher = pattern.matcher(graph); int count = 0; String nodes = ""; while (matcher.find()) { count++; nodes += matcher.group() + "\n"; } if (matchCount[i] != count) { System.out.println("Graph for '" + testName + "' contains different number of match nodes:"); System.out.println(nodes); } Asserts.assertEQ(matchCount[i], count, "Graph for '" + testName + "' contains different number of match nodes"); } tests.remove(testName); System.out.println(testName + " passed"); } // Check if all tests were compiled if (tests.size() != 0) { for (String name : tests.keySet()) { System.out.println("Test '" + name + "' not compiled!"); } throw new RuntimeException("Not all tests were compiled"); } } public void setup(Method[] methods) { for (Method m : methods) { if (m.isAnnotationPresent(Test.class)) { // Don't inline tests WHITE_BOX.testSetDontInlineMethod(m, true); } if (m.isAnnotationPresent(DontCompile.class)) { WHITE_BOX.makeMethodNotCompilable(m, COMP_LEVEL_ANY, true); WHITE_BOX.makeMethodNotCompilable(m, COMP_LEVEL_ANY, false); } if (m.isAnnotationPresent(ForceInline.class)) { WHITE_BOX.testSetForceInlineMethod(m, true); } else if (m.isAnnotationPresent(DontInline.class)) { WHITE_BOX.testSetDontInlineMethod(m, true); } } } public void run() throws Exception { System.out.format("rI = %d, rL = %d, rD = %f\n", rI, rL, rD); setup(this.getClass().getDeclaredMethods()); setup(MyValue.class.getDeclaredMethods()); // Execute tests for (Method test : tests.values()) { Method verifier = getClass().getDeclaredMethod(test.getName() + "_verifier", boolean.class); // Warmup using verifier method for (int i = 0; i < WARMUP; ++i) { verifier.invoke(this, true); } // Trigger compilation WHITE_BOX.enqueueMethodForCompilation(test, COMP_LEVEL_FULL_OPTIMIZATION); Asserts.assertTrue(WHITE_BOX.isMethodCompiled(test, false)); // Check result verifier.invoke(this, false); } } } // Mark method as test @Retention(RetentionPolicy.RUNTIME) @interface Test { // Regular expression used to match forbidden IR nodes // in the C2 IR emitted for this test. String failOn() default ""; // Regular expressions used to match and count IR nodes. String[] match() default { }; int[] matchCount() default { }; } // Force method inlining during compilation @Retention(RetentionPolicy.RUNTIME) @interface ForceInline { } // Prevent method inlining during compilation @Retention(RetentionPolicy.RUNTIME) @interface DontInline { } // Prevent method compilation @Retention(RetentionPolicy.RUNTIME) @interface DontCompile { }