# HG changeset patch # User Doug Simon # Date 1385069040 -3600 # Node ID d3fc35a433769be1ed663dfde6535748756e9a5a # Parent dd941feb26cb731f8127d8aadc99a649d208b96b HSAIL: addressed issues in JUnit tests Contributed-by: Tom Deneau diff -r dd941feb26cb -r d3fc35a43376 graal/com.oracle.graal.compiler.hsail.test.infra/src/com/oracle/graal/compiler/hsail/test/infra/GraalKernelTester.java --- a/graal/com.oracle.graal.compiler.hsail.test.infra/src/com/oracle/graal/compiler/hsail/test/infra/GraalKernelTester.java Thu Nov 21 22:10:13 2013 +0100 +++ b/graal/com.oracle.graal.compiler.hsail.test.infra/src/com/oracle/graal/compiler/hsail/test/infra/GraalKernelTester.java Thu Nov 21 22:24:00 2013 +0100 @@ -85,54 +85,19 @@ } @Override - protected void dispatchLambdaMethodKernelOkra(int range, MyIntConsumer consumer) { - HSAILCompilationResult hcr = HSAILCompilationResult.getCompiledLambda(consumer.getClass()); + protected void dispatchKernelOkra(int range, Object... args) { + HSAILCompilationResult hcr = HSAILCompilationResult.getHSAILCompilationResult(testMethod); HotSpotNmethod code = (HotSpotNmethod) hcr.getInstalledCode(); - logger.info("To determine parameters to pass to hsail kernel, we will examine " + consumer.getClass()); - Field[] fields = consumer.getClass().getDeclaredFields(); - Object[] args = new Object[fields.length]; - int argIndex = 0; - for (Field f : fields) { - logger.info("... " + f); - args[argIndex++] = getFieldFromObject(f, consumer); - } - if (code != null) { try { - // No return value from HSAIL kernels code.executeParallel(range, 0, 0, args); } catch (InvalidInstalledCodeException e) { Debug.log("WARNING:Invalid installed code: " + e); e.printStackTrace(); } - } - } - - @Override - protected void dispatchMethodKernelOkra(int range, Object... args) { - Object[] fixedArgs = fixArgTypes(args); - - HSAILCompilationResult hcr = HSAILCompilationResult.getHSAILCompilationResult(testMethod); - HotSpotNmethod code = (HotSpotNmethod) hcr.getInstalledCode(); - - if (code != null) { - try { - if (Modifier.isStatic(testMethod.getModifiers())) { - code.executeParallel(range, 0, 0, fixedArgs); - } else { - // If it is a non-static method we have to push "this" as the first argument. - Object[] newFixedArgs = new Object[fixedArgs.length + 1]; - System.arraycopy(fixedArgs, 0, newFixedArgs, 1, fixedArgs.length); - newFixedArgs[0] = this; - code.executeParallel(range, 0, 0, newFixedArgs); - } - } catch (InvalidInstalledCodeException e) { - Debug.log("WARNING:Invalid installed code: " + e); - e.printStackTrace(); - } } else { - super.dispatchMethodKernelOkra(range, args); + super.dispatchKernelOkra(range, args); } } diff -r dd941feb26cb -r d3fc35a43376 graal/com.oracle.graal.compiler.hsail.test.infra/src/com/oracle/graal/compiler/hsail/test/infra/KernelTester.java --- a/graal/com.oracle.graal.compiler.hsail.test.infra/src/com/oracle/graal/compiler/hsail/test/infra/KernelTester.java Thu Nov 21 22:10:13 2013 +0100 +++ b/graal/com.oracle.graal.compiler.hsail.test.infra/src/com/oracle/graal/compiler/hsail/test/infra/KernelTester.java Thu Nov 21 22:24:00 2013 +0100 @@ -367,6 +367,20 @@ } /** + * The "array stream" version of {@link #dispatchMethodKernel(int, Object...)}. + */ + public void dispatchMethodKernel(Object[] ary, Object... args) { + if (testMethod == null) { + setTestMethod(getTestMethodName(), this.getClass()); + } + if (dispatchMode == DispatchMode.SEQ) { + dispatchMethodKernelSeq(ary, args); + } else if (dispatchMode == DispatchMode.OKRA) { + dispatchMethodKernelOkra(ary, args); + } + } + + /** * This dispatchLambdaMethodKernel dispatches the lambda version of a kernel where the "kernel" * is for the lambda method itself (like lambda$0). */ @@ -530,7 +544,11 @@ } } - private void dispatchKernelOkra(int range, Object... args) { + /** + * Dispatches an okra kernel over a given range using JNI. Protected so that it can be + * overridden in {@link GraalKernelTester} which will dispatch without JNI. + */ + protected void dispatchKernelOkra(int range, Object... args) { if (okraKernel == null) { createOkraKernel(); } @@ -544,6 +562,7 @@ okraKernel.dispatchWithArgs(args); } + // int stream version private void dispatchMethodKernelSeq(int range, Object... args) { Object[] invokeArgs = new Object[args.length + 1]; // Need space on the end for the gid parameter. @@ -556,32 +575,46 @@ } for (int rangeIndex = 0; rangeIndex < range; rangeIndex++) { invokeArgs[gidArgIndex] = rangeIndex; - try { - testMethod.invoke(this, invokeArgs); - } catch (IllegalAccessException e) { - fail("could not invoke " + testMethod + ", make sure it is public"); - } catch (IllegalArgumentException e) { - fail("wrong arguments invoking " + testMethod + ", check number and type of args passed to dispatchMethodKernel"); - } catch (InvocationTargetException e) { - Throwable cause = e.getCause(); - /** - * We will ignore ArrayIndexOutOfBoundsException because the graal okra target - * doesn't really handle it yet (basically returns early if it sees one). - */ - if (cause instanceof ArrayIndexOutOfBoundsException) { - logger.severe("ignoring ArrayIndexOutOfBoundsException for index " + rangeIndex); - } else { - // Other exceptions. - String errstr = testMethod + " threw an exception on gid=" + rangeIndex + ", exception was " + cause; - fail(errstr); - } - } catch (Exception e) { - fail("Unknown exception " + e + " invoking " + testMethod); + invokeMethodKernelSeq(invokeArgs, rangeIndex); + } + } + + // array stream version + private void dispatchMethodKernelSeq(Object[] ary, Object... args) { + Object[] invokeArgs = new Object[args.length + 1]; + // Need space on the end for the final obj parameter. + System.arraycopy(args, 0, invokeArgs, 0, args.length); + int objArgIndex = invokeArgs.length - 1; + if (logLevel.intValue() <= Level.FINE.intValue()) { + for (Object arg : args) { + logger.fine(arg.toString()); } } + int range = ary.length; + for (int rangeIndex = 0; rangeIndex < range; rangeIndex++) { + invokeArgs[objArgIndex] = ary[rangeIndex]; + invokeMethodKernelSeq(invokeArgs, rangeIndex); + } } - protected void dispatchMethodKernelOkra(int range, Object... args) { + private void invokeMethodKernelSeq(Object[] invokeArgs, int rangeIndex) { + try { + testMethod.invoke(this, invokeArgs); + } catch (IllegalAccessException e) { + fail("could not invoke " + testMethod + ", make sure it is public"); + } catch (IllegalArgumentException e) { + fail("wrong arguments invoking " + testMethod + ", check number and type of args passed to dispatchMethodKernel"); + } catch (InvocationTargetException e) { + Throwable cause = e.getCause(); + String errstr = testMethod + " threw an exception on gid=" + rangeIndex + ", exception was " + cause; + fail(errstr); + } catch (Exception e) { + fail("Unknown exception " + e + " invoking " + testMethod); + } + } + + // int stream version + private void dispatchMethodKernelOkra(int range, Object... args) { Object[] fixedArgs = fixArgTypes(args); if (Modifier.isStatic(testMethod.getModifiers())) { dispatchKernelOkra(range, fixedArgs); @@ -594,6 +627,26 @@ } } + // array stream version + private void dispatchMethodKernelOkra(Object[] ary, Object... args) { + // add the ary itself as the last arg in the passed parameter list + Object[] argsWithAry = new Object[args.length + 1]; + System.arraycopy(args, 0, argsWithAry, 0, args.length); + argsWithAry[argsWithAry.length - 1] = ary; + + Object[] fixedArgs = fixArgTypes(argsWithAry); + int range = ary.length; + if (Modifier.isStatic(testMethod.getModifiers())) { + dispatchKernelOkra(range, fixedArgs); + } else { + // If it is a non-static method we have to push "this" as the first argument. + Object[] newFixedArgs = new Object[fixedArgs.length + 1]; + System.arraycopy(fixedArgs, 0, newFixedArgs, 1, fixedArgs.length); + newFixedArgs[0] = this; + dispatchKernelOkra(range, newFixedArgs); + } + } + /** * For primitive arg parameters, make sure arg types are cast to whatever the testMethod * signature says they should be. @@ -659,12 +712,13 @@ private void dispatchLambdaMethodKernelOkra(Object[] ary, MyObjConsumer consumer) { logger.info("To determine parameters to pass to hsail kernel, we will examine " + consumer.getClass()); Field[] fields = consumer.getClass().getDeclaredFields(); - Object[] args = new Object[fields.length]; + Object[] args = new Object[fields.length + 1]; // + 1 because we also pass the array int argIndex = 0; for (Field f : fields) { logger.info("... " + f); args[argIndex++] = getFieldFromObject(f, consumer); } + args[argIndex] = ary; dispatchKernelOkra(ary.length, args); } @@ -682,8 +736,9 @@ private void dispatchLambdaKernelOkra(Object[] ary, MyObjConsumer consumer) { // The "wrapper" method always has only one arg consisting of the consumer. - Object[] args = new Object[1]; + Object[] args = new Object[2]; args[0] = consumer; + args[1] = ary; dispatchKernelOkra(ary.length, args); } diff -r dd941feb26cb -r d3fc35a43376 graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/BasicHSAILTest.java --- a/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/BasicHSAILTest.java Thu Nov 21 22:10:13 2013 +0100 +++ b/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/BasicHSAILTest.java Thu Nov 21 22:24:00 2013 +0100 @@ -25,6 +25,7 @@ import org.junit.*; import com.oracle.graal.compiler.test.*; +import com.oracle.graal.debug.*; import com.oracle.graal.hotspot.hsail.*; import com.oracle.graal.hsail.*; import com.oracle.graal.nodes.*; @@ -333,7 +334,7 @@ private void test(String snippet) { StructuredGraph graph = parse(snippet); HSAILCompilationResult compResult = HSAILCompilationResult.getHSAILCompilationResult(graph); - compResult.dumpCompilationResult(); + TTY.println("code generated for " + snippet + ":\n" + compResult.getHSAILCode()); } public static void nBodySpill(float[] inxyz, float[] outxyz, float[] invxyz, float[] outvxyz, int gid) { diff -r dd941feb26cb -r d3fc35a43376 graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/Vec3.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/Vec3.java Thu Nov 21 22:24:00 2013 +0100 @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2009, 2012, 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. + */ + +package com.oracle.graal.compiler.hsail.test; + +/** + * A simple 3 element Vector object used in some junit tests. + */ +public class Vec3 { + + public Vec3(float x, float y, float z) { + this.x = x; + this.y = y; + this.z = z; + } + + public float x; + public float y; + public float z; + + public static Vec3 add(Vec3 a, Vec3 b) { + return new Vec3(a.x + b.x, a.y + b.y, a.z + b.z); + } + + @Override + public boolean equals(Object other) { + if (!(other instanceof Vec3)) { + return false; + } + Vec3 oth = (Vec3) other; + return (oth.x == x && oth.y == y && oth.z == z); + } + + @Override + public String toString() { + return ("Vec3[" + x + ", " + y + ", " + z + "]"); + } + + @Override + public int hashCode() { + return (int) (x + y + z); + } + +} diff -r dd941feb26cb -r d3fc35a43376 graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/Vec3ObjStreamFloatCaptureTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/Vec3ObjStreamFloatCaptureTest.java Thu Nov 21 22:24:00 2013 +0100 @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2009, 2012, 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. + */ + +package com.oracle.graal.compiler.hsail.test; + +import com.oracle.graal.compiler.hsail.test.infra.GraalKernelTester; +import org.junit.Test; + +/** + * Tests codegen for a java 7 style object array stream kernel, one float capture. + */ +public class Vec3ObjStreamFloatCaptureTest extends GraalKernelTester { + + static final int NUM = 20; + + @Result public Vec3[] inArray = new Vec3[NUM]; + + void setupArrays() { + for (int i = 0; i < NUM; i++) { + inArray[i] = new Vec3(i, i + 1, -1); + } + } + + /** + * The "kernel" method we will be testing. For Array Stream, an object from the array will be + * the last parameter + */ + public void run(float adjustment, Vec3 vec3) { + vec3.z = vec3.x + vec3.y - adjustment; + } + + @Override + public void runTest() { + setupArrays(); + dispatchMethodKernel(inArray, 0.5f); + } + + @Test + public void test() { + testGeneratedHsail(); + } + +} diff -r dd941feb26cb -r d3fc35a43376 graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/Vec3ObjStreamIntCaptureTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/Vec3ObjStreamIntCaptureTest.java Thu Nov 21 22:24:00 2013 +0100 @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2009, 2012, 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. + */ + +package com.oracle.graal.compiler.hsail.test; + +import com.oracle.graal.compiler.hsail.test.infra.GraalKernelTester; +import org.junit.Test; + +/** + * Tests codegen for a java 7 style object array stream kernel, one int capture. + */ +public class Vec3ObjStreamIntCaptureTest extends GraalKernelTester { + + static final int NUM = 20; + + @Result public Vec3[] inArray = new Vec3[NUM]; + + void setupArrays() { + for (int i = 0; i < NUM; i++) { + inArray[i] = new Vec3(i, i + 1, -1); + } + } + + /** + * The "kernel" method we will be testing. For Array Stream, an object from the array will be + * the last parameter + */ + public void run(int adjustment, Vec3 vec3) { + vec3.z = vec3.x + vec3.y - adjustment; + } + + @Override + public void runTest() { + setupArrays(); + dispatchMethodKernel(inArray, 7); + } + + @Test + public void test() { + testGeneratedHsail(); + } + +} diff -r dd941feb26cb -r d3fc35a43376 graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/Vec3ObjStreamIntFloatCaptureTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/Vec3ObjStreamIntFloatCaptureTest.java Thu Nov 21 22:24:00 2013 +0100 @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2009, 2012, 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. + */ + +package com.oracle.graal.compiler.hsail.test; + +import com.oracle.graal.compiler.hsail.test.infra.GraalKernelTester; +import org.junit.Test; + +/** + * Tests codegen for a java 7 style object array stream kernel, with one int and one float capture. + */ +public class Vec3ObjStreamIntFloatCaptureTest extends GraalKernelTester { + + static final int NUM = 20; + + @Result public Vec3[] inArray = new Vec3[NUM]; + + void setupArrays() { + for (int i = 0; i < NUM; i++) { + inArray[i] = new Vec3(i, i + 1, -1); + } + } + + /** + * The "kernel" method we will be testing. For Array Stream, an object from the array will be + * the last parameter + */ + public void run(int adjustment, float multiplier, Vec3 vec3) { + vec3.z = (vec3.x + vec3.y - adjustment) * multiplier; + } + + @Override + public void runTest() { + setupArrays(); + dispatchMethodKernel(inArray, 7, 0.5f); + } + + @Test + public void test() { + testGeneratedHsail(); + } + +} diff -r dd941feb26cb -r d3fc35a43376 graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/Vec3ObjStreamObjCaptureTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/Vec3ObjStreamObjCaptureTest.java Thu Nov 21 22:24:00 2013 +0100 @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2009, 2012, 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. + */ + +package com.oracle.graal.compiler.hsail.test; + +import com.oracle.graal.compiler.hsail.test.infra.GraalKernelTester; +import org.junit.Test; + +/** + * Tests codegen for a java 7 style object array stream kernel, one object capture. + */ +public class Vec3ObjStreamObjCaptureTest extends GraalKernelTester { + + static final int NUM = 20; + + @Result public Vec3[] inArray = new Vec3[NUM]; + + void setupArrays() { + for (int i = 0; i < NUM; i++) { + inArray[i] = new Vec3(i, i + 1, -1); + } + } + + /** + * The "kernel" method we will be testing. For Array Stream, an object from the array will be + * the last parameter + */ + public void run(Vec3 basevec, Vec3 vec3) { + vec3.z = vec3.x + vec3.y - basevec.z; + } + + @Override + public void runTest() { + setupArrays(); + dispatchMethodKernel(inArray, new Vec3(1, 2, 3)); + } + + @Test + public void test() { + testGeneratedHsail(); + } + +} diff -r dd941feb26cb -r d3fc35a43376 graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/Vec3ObjStreamObjFieldTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/Vec3ObjStreamObjFieldTest.java Thu Nov 21 22:24:00 2013 +0100 @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2009, 2012, 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. + */ + +package com.oracle.graal.compiler.hsail.test; + +import com.oracle.graal.compiler.hsail.test.infra.GraalKernelTester; +import org.junit.Test; + +/** + * Tests codegen for a java 7 style object array stream kernel, no captures. Instance method which + * accesses an object field + */ +public class Vec3ObjStreamObjFieldTest extends GraalKernelTester { + + static final int NUM = 20; + + @Result public Vec3[] inArray = new Vec3[NUM]; + Vec3 basevec = new Vec3(1, 2, 3); + + void setupArrays() { + for (int i = 0; i < NUM; i++) { + inArray[i] = new Vec3(i, i + 1, -1); + } + } + + /** + * The "kernel" method we will be testing. For Array Stream, an object from the array will be + * the last parameter + */ + public void run(Vec3 vec3) { + vec3.z = vec3.x + vec3.y - basevec.z; + } + + @Override + public void runTest() { + setupArrays(); + dispatchMethodKernel(inArray); + } + + @Test + public void test() { + testGeneratedHsail(); + } + +} diff -r dd941feb26cb -r d3fc35a43376 graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/Vec3ObjStreamTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/Vec3ObjStreamTest.java Thu Nov 21 22:24:00 2013 +0100 @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2009, 2012, 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. + */ + +package com.oracle.graal.compiler.hsail.test; + +import org.junit.*; + +import com.oracle.graal.compiler.hsail.test.infra.*; + +/** + * Tests codegen for a java 7 style object array stream kernel, no captures. + */ +public class Vec3ObjStreamTest extends GraalKernelTester { + + static final int NUM = 20; + + @Result public Vec3[] inArray = new Vec3[NUM]; + + void setupArrays() { + for (int i = 0; i < NUM; i++) { + inArray[i] = new Vec3(i, i + 1, -1); + } + } + + /** + * The "kernel" method we will be testing. For Array Stream, an object from the array will be + * the last parameter + */ + public void run(Vec3 vec3) { + vec3.z = vec3.x + vec3.y; + } + + @Override + public void runTest() { + setupArrays(); + dispatchMethodKernel(inArray); + } + + @Test + public void test() { + testGeneratedHsail(); + } + +} diff -r dd941feb26cb -r d3fc35a43376 graal/com.oracle.graal.hotspot.hsail/src/com/oracle/graal/hotspot/hsail/HSAILCompilationResult.java --- a/graal/com.oracle.graal.hotspot.hsail/src/com/oracle/graal/hotspot/hsail/HSAILCompilationResult.java Thu Nov 21 22:10:13 2013 +0100 +++ b/graal/com.oracle.graal.hotspot.hsail/src/com/oracle/graal/hotspot/hsail/HSAILCompilationResult.java Thu Nov 21 22:24:00 2013 +0100 @@ -26,8 +26,6 @@ import static com.oracle.graal.hotspot.HotSpotGraalRuntime.*; import java.lang.reflect.*; -import java.util.logging.*; - import com.oracle.graal.api.code.*; import com.oracle.graal.api.code.CallingConvention.Type; import com.oracle.graal.api.meta.*; @@ -71,30 +69,6 @@ return installedCode; } - private static final String propPkgName = HSAILCompilationResult.class.getPackage().getName(); - private static Level logLevel; - private static ConsoleHandler consoleHandler; - public static Logger logger; - static { - logger = Logger.getLogger(propPkgName); - logLevel = Level.FINE; - // This block configures the logger with handler and formatter. - consoleHandler = new ConsoleHandler(); - logger.addHandler(consoleHandler); - logger.setUseParentHandlers(false); - SimpleFormatter formatter = new SimpleFormatter() { - - @SuppressWarnings("sync-override") - @Override - public String format(LogRecord record) { - return (record.getMessage() + "\n"); - } - }; - consoleHandler.setFormatter(formatter); - logger.setLevel(logLevel); - consoleHandler.setLevel(logLevel); - } - static final HSAILHotSpotBackend backend; static { // Look for installed HSAIL backend @@ -185,9 +159,6 @@ } // Now that we have the target lambda, compile it. HSAILCompilationResult hsailCompResult = HSAILCompilationResult.getHSAILCompilationResult(lambdaMethod); - if (hsailCompResult != null) { - hsailCompResult.dumpCompilationResult(); - } return hsailCompResult; } @@ -224,9 +195,9 @@ } catch (GraalInternalError e) { String partialCode = backend.getPartialCodeString(); if (partialCode != null && !partialCode.equals("")) { - logger.fine("-------------------\nPartial Code Generation:\n--------------------"); - logger.fine(partialCode); - logger.fine("-------------------\nEnd of Partial Code Generation\n--------------------"); + Debug.log("-------------------\nPartial Code Generation:\n--------------------"); + Debug.log(partialCode); + Debug.log("-------------------\nEnd of Partial Code Generation\n--------------------"); } throw e; } @@ -251,9 +222,4 @@ return new String(getTargetCode(), 0, getTargetCodeSize()); } - public void dumpCompilationResult() { - logger.fine("targetCodeSize=" + getTargetCodeSize()); - logger.fine(getHSAILCode()); - } - } diff -r dd941feb26cb -r d3fc35a43376 graal/com.oracle.graal.hotspot.hsail/src/com/oracle/graal/hotspot/hsail/HSAILHotSpotBackend.java --- a/graal/com.oracle.graal.hotspot.hsail/src/com/oracle/graal/hotspot/hsail/HSAILHotSpotBackend.java Thu Nov 21 22:10:13 2013 +0100 +++ b/graal/com.oracle.graal.hotspot.hsail/src/com/oracle/graal/hotspot/hsail/HSAILHotSpotBackend.java Thu Nov 21 22:24:00 2013 +0100 @@ -56,7 +56,6 @@ paramTypeMap.put("HotSpotResolvedPrimitiveType", "f32"); paramTypeMap.put("HotSpotResolvedPrimitiveType", "f64"); paramTypeMap.put("HotSpotResolvedPrimitiveType", "s64"); - } @Override @@ -128,13 +127,16 @@ codeBuffer = tasm.asm.codeBuffer; codeBuffer.emitString0("version 0:95: $full : $large;"); codeBuffer.emitString(""); + Signature signature = method.getSignature(); int sigParamCount = signature.getParameterCount(false); // We're subtracting 1 because we're not making the final gid as a parameter. + int nonConstantParamCount = sigParamCount - 1; boolean isStatic = (Modifier.isStatic(method.getModifiers())); // Determine if this is an object lambda. boolean isObjectLambda = true; + if (signature.getParameterType(nonConstantParamCount, null).getKind() == Kind.Int) { isObjectLambda = false; } else { @@ -152,8 +154,8 @@ JavaType[] paramtypes = new JavaType[totalParamCount]; String[] paramNames = new String[totalParamCount]; int pidx = 0; + MetaAccessProvider metaAccess = getProviders().getMetaAccess(); for (int i = 0; i < totalParamCount; i++) { - MetaAccessProvider metaAccess = getProviders().getMetaAccess(); if (i == 0 && !isStatic) { paramtypes[i] = metaAccess.lookupJavaType(Object.class); paramNames[i] = "%_this"; @@ -168,19 +170,26 @@ } } } + codeBuffer.emitString0("// " + (isStatic ? "static" : "instance") + " method " + method); codeBuffer.emitString(""); codeBuffer.emitString0("kernel &run ("); codeBuffer.emitString(""); + FrameMap frameMap = tasm.frameMap; RegisterConfig regConfig = frameMap.registerConfig; // Build list of param types which does include the gid (for cc register mapping query). JavaType[] ccParamTypes = new JavaType[nonConstantParamCount + 1]; // Include the gid. System.arraycopy(paramtypes, 0, ccParamTypes, 0, nonConstantParamCount); - // Last entry comes from the signature. - ccParamTypes[ccParamTypes.length - 1] = signature.getParameterType(sigParamCount - 1, null); + + // Last entry is always int (its register gets used in the workitemabsid instruction) + // this is true even for object stream labmdas + if (sigParamCount > 0) { + ccParamTypes[ccParamTypes.length - 1] = metaAccess.lookupJavaType(int.class); + } CallingConvention cc = regConfig.getCallingConvention(JavaCallee, null, ccParamTypes, getTarget(), false); + /** * Compute the hsail size mappings up to but not including the last non-constant parameter * (which is the gid). @@ -196,6 +205,7 @@ // Emit the kernel function parameters. for (int i = 0; i < totalParamCount; i++) { String str = "kernarg_" + paramHsailSizes[i] + " " + paramNames[i]; + if (i != totalParamCount - 1) { str += ","; } @@ -229,19 +239,45 @@ codeBuffer.emitString(spillsegTemplate); // Emit object array load prologue here. if (isObjectLambda) { - final int arrayElementsOffset = 24; + boolean useCompressedOops = getRuntime().getConfig().useCompressedOops; + final int arrayElementsOffset = HotSpotGraalRuntime.getArrayBaseOffset(Kind.Object); String iterationObjArgReg = HSAIL.mapRegister(cc.getArgument(nonConstantParamCount - 1)); - String tmpReg = workItemReg.replace("s", "d"); // "$d1"; + // iterationObjArgReg will be the highest $d register in use (it is the last parameter) + // so tempReg can be the next higher $d register + String tmpReg = "$d" + (asRegister(cc.getArgument(nonConstantParamCount - 1)).encoding() + 1); // Convert gid to long. codeBuffer.emitString("cvt_u64_s32 " + tmpReg + ", " + workItemReg + "; // Convert gid to long"); - // Adjust index for sizeof ref. - codeBuffer.emitString("mul_u64 " + tmpReg + ", " + tmpReg + ", " + 8 + "; // Adjust index for sizeof ref"); + // Adjust index for sizeof ref. Where to pull this size from? + codeBuffer.emitString("mul_u64 " + tmpReg + ", " + tmpReg + ", " + (useCompressedOops ? 4 : 8) + "; // Adjust index for sizeof ref"); // Adjust for actual data start. codeBuffer.emitString("add_u64 " + tmpReg + ", " + tmpReg + ", " + arrayElementsOffset + "; // Adjust for actual elements data start"); // Add to array ref ptr. codeBuffer.emitString("add_u64 " + tmpReg + ", " + tmpReg + ", " + iterationObjArgReg + "; // Add to array ref ptr"); // Load the object into the parameter reg. - codeBuffer.emitString("ld_global_u64 " + iterationObjArgReg + ", " + "[" + tmpReg + "]" + "; // Load from array element into parameter reg"); + if (useCompressedOops) { + + // Load u32 into the d 64 reg since it will become an object address + codeBuffer.emitString("ld_global_u32 " + tmpReg + ", " + "[" + tmpReg + "]" + "; // Load compressed ptr from array"); + + long narrowOopBase = getRuntime().getConfig().narrowOopBase; + long narrowOopShift = getRuntime().getConfig().narrowOopShift; + + if (narrowOopBase == 0 && narrowOopShift == 0) { + // No more calculation to do, mov to target register + codeBuffer.emitString("mov_b64 " + iterationObjArgReg + ", " + tmpReg + "; // no shift or base addition"); + } else { + if (narrowOopBase == 0) { + codeBuffer.emitString("shl_u64 " + iterationObjArgReg + ", " + tmpReg + ", " + narrowOopShift + "; // do narrowOopShift"); + } else if (narrowOopShift == 0) { + codeBuffer.emitString("add_u64 " + iterationObjArgReg + ", " + tmpReg + ", " + narrowOopBase + "; // add narrowOopBase"); + } else { + codeBuffer.emitString("mad_u64 " + iterationObjArgReg + ", " + tmpReg + ", " + (1 << narrowOopShift) + ", " + narrowOopBase + "; // shift and add narrowOopBase"); + } + } + + } else { + codeBuffer.emitString("ld_global_u64 " + iterationObjArgReg + ", " + "[" + tmpReg + "]" + "; // Load from array element into parameter reg"); + } } // Prologue done, Emit code for the LIR. lirGen.lir.emitCode(tasm);