src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot.test/src/org/graalvm/compiler/hotspot/test/GraalOSRTestBase.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot.test/src/org/graalvm/compiler/hotspot/test

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot.test/src/org/graalvm/compiler/hotspot/test/GraalOSRTestBase.java

Print this page




   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 org.graalvm.compiler.hotspot.test;
  25 
  26 import org.junit.Assert;
  27 
  28 import org.graalvm.compiler.bytecode.Bytecode;

  29 import org.graalvm.compiler.bytecode.BytecodeStream;
  30 import org.graalvm.compiler.bytecode.ResolvedJavaMethodBytecode;
  31 import org.graalvm.compiler.core.target.Backend;
  32 import org.graalvm.compiler.core.test.GraalCompilerTest;
  33 import org.graalvm.compiler.debug.GraalError;

  34 import org.graalvm.compiler.hotspot.CompilationTask;
  35 import org.graalvm.compiler.hotspot.HotSpotGraalCompiler;
  36 import org.graalvm.compiler.java.BciBlockMapping;
  37 import org.graalvm.compiler.java.BciBlockMapping.BciBlock;
  38 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;


  39 
  40 import jdk.vm.ci.code.Architecture;
  41 import jdk.vm.ci.hotspot.HotSpotCompilationRequest;
  42 import jdk.vm.ci.hotspot.HotSpotCompilationRequestResult;
  43 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  44 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
  45 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  46 import jdk.vm.ci.meta.ResolvedJavaMethod;
  47 
  48 public abstract class GraalOSRTestBase extends GraalCompilerTest {
  49 
  50     protected void testOSR(String methodName) {




  51         ResolvedJavaMethod method = getResolvedJavaMethod(methodName);
  52         testOSR(method);
  53     }
  54 
  55     protected void testOSR(ResolvedJavaMethod method) {
  56         // invalidate any existing compiled code
  57         method.reprofile();
  58         compileOSR(method);
  59         Result result = executeExpected(method, null);
  60         checkResult(result);
  61     }
  62 
  63     private static void compile(ResolvedJavaMethod method, int bci) {
  64         HotSpotJVMCIRuntimeProvider runtime = HotSpotJVMCIRuntime.runtime();
  65         long jvmciEnv = 0L;
  66         HotSpotCompilationRequest request = new HotSpotCompilationRequest((HotSpotResolvedJavaMethod) method, bci, jvmciEnv);
  67         HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) runtime.getCompiler();
  68         CompilationTask task = new CompilationTask(runtime, compiler, request, true, true);
  69         HotSpotCompilationRequestResult result = task.runCompilation();
  70         if (result.getFailure() != null) {
  71             throw new GraalError(result.getFailureMessage());
  72         }
  73     }
  74 
  75     /**
  76      * Returns the target BCI of the first bytecode backedge. This is where HotSpot triggers
  77      * on-stack-replacement in case the backedge counter overflows.
  78      */
  79     private static int getBackedgeBCI(ResolvedJavaMethod method) {
  80         Bytecode code = new ResolvedJavaMethodBytecode(method);
  81         BytecodeStream stream = new BytecodeStream(code.getCode());
  82         BciBlockMapping bciBlockMapping = BciBlockMapping.create(stream, code);
  83         assert bciBlockMapping.getLoopCount() == 1 : "Expected exactly one loop " + method;
  84 
  85         for (BciBlock block : bciBlockMapping.getBlocks()) {

  86             int bci = block.startBci;
  87             for (BciBlock succ : block.getSuccessors()) {

  88                 int succBci = succ.startBci;
  89                 if (succBci < bci) {
  90                     // back edge
  91                     return succBci;
  92                 }
  93             }
  94         }




  95         return -1;
  96     }
  97 
  98     private static void checkResult(Result result) {
  99         Assert.assertNull("Unexpected exception", result.exception);
 100         Assert.assertNotNull(result.returnValue);
 101         Assert.assertTrue(result.returnValue instanceof ReturnValue);
 102         Assert.assertEquals(ReturnValue.SUCCESS, result.returnValue);
 103     }
 104 
 105     private void compileOSR(ResolvedJavaMethod method) {


 106         int bci = getBackedgeBCI(method);
 107         assert bci != -1;
 108         // ensure eager resolving
 109         parseEager(method, AllowAssumptions.YES);
 110         compile(method, bci);
 111     }
 112 
 113     protected enum ReturnValue {
 114         SUCCESS,
 115         FAILURE

 116     }
 117 
 118     public GraalOSRTestBase() {
 119         super();
 120     }
 121 
 122     public GraalOSRTestBase(Class<? extends Architecture> arch) {
 123         super(arch);
 124     }
 125 
 126     public GraalOSRTestBase(Backend backend) {
 127         super(backend);
 128     }
 129 
 130 }


   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 org.graalvm.compiler.hotspot.test;
  25 
  26 import java.util.Arrays;
  27 
  28 import org.graalvm.compiler.bytecode.Bytecode;
  29 import org.graalvm.compiler.bytecode.BytecodeDisassembler;
  30 import org.graalvm.compiler.bytecode.BytecodeStream;
  31 import org.graalvm.compiler.bytecode.ResolvedJavaMethodBytecode;
  32 import org.graalvm.compiler.core.target.Backend;
  33 import org.graalvm.compiler.core.test.GraalCompilerTest;
  34 import org.graalvm.compiler.debug.GraalError;
  35 import org.graalvm.compiler.debug.TTY;
  36 import org.graalvm.compiler.hotspot.CompilationTask;
  37 import org.graalvm.compiler.hotspot.HotSpotGraalCompiler;
  38 import org.graalvm.compiler.java.BciBlockMapping;
  39 import org.graalvm.compiler.java.BciBlockMapping.BciBlock;
  40 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  41 import org.graalvm.compiler.options.OptionValues;
  42 import org.junit.Assert;
  43 
  44 import jdk.vm.ci.code.Architecture;
  45 import jdk.vm.ci.hotspot.HotSpotCompilationRequest;
  46 import jdk.vm.ci.hotspot.HotSpotCompilationRequestResult;
  47 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  48 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
  49 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  50 import jdk.vm.ci.meta.ResolvedJavaMethod;
  51 
  52 public abstract class GraalOSRTestBase extends GraalCompilerTest {
  53 
  54     protected void testOSR(OptionValues options, String methodName) {
  55         testOSR(options, methodName, null);
  56     }
  57 
  58     protected void testOSR(OptionValues options, String methodName, Object receiver, Object... args) {
  59         ResolvedJavaMethod method = getResolvedJavaMethod(methodName);
  60         testOSR(options, method, receiver, args);
  61     }
  62 
  63     protected void testOSR(OptionValues options, ResolvedJavaMethod method, Object receiver, Object... args) {
  64         // invalidate any existing compiled code
  65         method.reprofile();
  66         compileOSR(options, method);
  67         Result result = executeExpected(method, receiver, args);
  68         checkResult(result);
  69     }
  70 
  71     protected static void compile(OptionValues options, ResolvedJavaMethod method, int bci) {
  72         HotSpotJVMCIRuntimeProvider runtime = HotSpotJVMCIRuntime.runtime();
  73         long jvmciEnv = 0L;
  74         HotSpotCompilationRequest request = new HotSpotCompilationRequest((HotSpotResolvedJavaMethod) method, bci, jvmciEnv);
  75         HotSpotGraalCompiler compiler = (HotSpotGraalCompiler) runtime.getCompiler();
  76         CompilationTask task = new CompilationTask(runtime, compiler, request, true, true, options);
  77         HotSpotCompilationRequestResult result = task.runCompilation();
  78         if (result.getFailure() != null) {
  79             throw new GraalError(result.getFailureMessage());
  80         }
  81     }
  82 
  83     /**
  84      * Returns the target BCI of the first bytecode backedge. This is where HotSpot triggers
  85      * on-stack-replacement in case the backedge counter overflows.
  86      */
  87     private static int getBackedgeBCI(ResolvedJavaMethod method) {
  88         Bytecode code = new ResolvedJavaMethodBytecode(method);
  89         BytecodeStream stream = new BytecodeStream(code.getCode());
  90         BciBlockMapping bciBlockMapping = BciBlockMapping.create(stream, code, getInitialOptions());

  91 
  92         for (BciBlock block : bciBlockMapping.getBlocks()) {
  93             if (block.startBci != -1) {
  94                 int bci = block.startBci;
  95                 for (BciBlock succ : block.getSuccessors()) {
  96                     if (succ.startBci != -1) {
  97                         int succBci = succ.startBci;
  98                         if (succBci < bci) {
  99                             // back edge
 100                             return succBci;
 101                         }
 102                     }
 103                 }
 104             }
 105         }
 106         TTY.println("Cannot find loop back edge with bytecode loops at:%s", Arrays.toString(bciBlockMapping.getLoopHeaders()));
 107         TTY.println(new BytecodeDisassembler().disassemble(code));
 108         return -1;
 109     }
 110 
 111     private static void checkResult(Result result) {
 112         Assert.assertNull("Unexpected exception", result.exception);
 113         Assert.assertNotNull(result.returnValue);
 114         Assert.assertTrue(result.returnValue instanceof ReturnValue);
 115         Assert.assertEquals(ReturnValue.SUCCESS, result.returnValue);
 116     }
 117 
 118     private void compileOSR(OptionValues options, ResolvedJavaMethod method) {
 119         // ensure eager resolving
 120         parseEager(method, AllowAssumptions.YES, options);
 121         int bci = getBackedgeBCI(method);
 122         assert bci != -1;
 123         // ensure eager resolving
 124         parseEager(method, AllowAssumptions.YES, options);
 125         compile(options, method, bci);
 126     }
 127 
 128     protected enum ReturnValue {
 129         SUCCESS,
 130         FAILURE,
 131         SIDE
 132     }
 133 
 134     public GraalOSRTestBase() {
 135         super();
 136     }
 137 
 138     public GraalOSRTestBase(Class<? extends Architecture> arch) {
 139         super(arch);
 140     }
 141 
 142     public GraalOSRTestBase(Backend backend) {
 143         super(backend);
 144     }
 145 
 146 }
src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot.test/src/org/graalvm/compiler/hotspot/test/GraalOSRTestBase.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File