src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.replacements.test/src/org/graalvm/compiler/replacements/test/MethodSubstitutionTest.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.replacements.test/src/org/graalvm/compiler/replacements/test

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.replacements.test/src/org/graalvm/compiler/replacements/test/MethodSubstitutionTest.java

Print this page




   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 package org.graalvm.compiler.replacements.test;
  24 
  25 import java.lang.reflect.InvocationTargetException;
  26 
  27 import org.graalvm.compiler.api.replacements.MethodSubstitution;
  28 import org.graalvm.compiler.core.test.GraalCompilerTest;
  29 import org.graalvm.compiler.debug.Debug;
  30 import org.graalvm.compiler.debug.Debug.Scope;
  31 import org.graalvm.compiler.graph.Node;
  32 import org.graalvm.compiler.nodes.Invoke;
  33 import org.graalvm.compiler.nodes.StructuredGraph;
  34 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  35 import org.graalvm.compiler.nodes.spi.LoweringTool;
  36 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  37 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
  38 import org.graalvm.compiler.phases.common.LoweringPhase;
  39 import org.graalvm.compiler.phases.common.inlining.InliningPhase;
  40 import org.graalvm.compiler.phases.tiers.HighTierContext;
  41 import org.graalvm.compiler.replacements.nodes.MacroNode;
  42 
  43 import jdk.vm.ci.code.InstalledCode;
  44 import jdk.vm.ci.code.InvalidInstalledCodeException;
  45 import jdk.vm.ci.meta.ResolvedJavaMethod;
  46 
  47 /**
  48  * Tests if {@link MethodSubstitution}s are inlined correctly. Most test cases only assert that
  49  * there are no remaining invocations in the graph. This is sufficient if the method that is being
  50  * substituted is a native method. For Java methods, additional checks are necessary.
  51  */
  52 public abstract class MethodSubstitutionTest extends GraalCompilerTest {
  53 
  54     @SuppressWarnings("try")
  55     protected StructuredGraph testGraph(final String snippet) {
  56         try (Scope s = Debug.scope("MethodSubstitutionTest", getResolvedJavaMethod(snippet))) {
  57             StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);

  58             HighTierContext context = getDefaultHighTierContext();
  59             Debug.dump(Debug.BASIC_LEVEL, graph, "Graph");
  60             new InliningPhase(new CanonicalizerPhase()).apply(graph, context);
  61             Debug.dump(Debug.BASIC_LEVEL, graph, "Graph");
  62             new CanonicalizerPhase().apply(graph, context);
  63             new DeadCodeEliminationPhase().apply(graph);
  64             // Try to ensure any macro nodes are lowered to expose any resulting invokes
  65             if (graph.getNodes().filter(MacroNode.class).isNotEmpty()) {
  66                 new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
  67             }
  68             if (graph.getNodes().filter(MacroNode.class).isNotEmpty()) {
  69                 new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, context);
  70             }
  71             assertNotInGraph(graph, MacroNode.class);
  72             assertNotInGraph(graph, Invoke.class);
  73             return graph;
  74         } catch (Throwable e) {
  75             throw Debug.handle(e);
  76         }
  77     }
  78 
  79     protected static StructuredGraph assertNotInGraph(StructuredGraph graph, Class<?> clazz) {
  80         for (Node node : graph.getNodes()) {
  81             if (clazz.isInstance(node)) {
  82                 fail(node.toString());
  83             }
  84         }
  85         return graph;
  86     }
  87 
  88     protected void testSubstitution(String testMethodName, Class<?> intrinsicClass, Class<?> holder, String methodName, Class<?>[] parameterTypes, boolean optional, Object[] args1, Object[] args2) {
  89         ResolvedJavaMethod realMethod = getResolvedJavaMethod(holder, methodName, parameterTypes);
  90         ResolvedJavaMethod testMethod = getResolvedJavaMethod(testMethodName);
  91         StructuredGraph graph = testGraph(testMethodName);
  92 
  93         // Check to see if the resulting graph contains the expected node
  94         StructuredGraph replacement = getReplacements().getSubstitution(realMethod, -1);
  95         if (replacement == null && !optional) {




   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 package org.graalvm.compiler.replacements.test;
  24 
  25 import java.lang.reflect.InvocationTargetException;
  26 
  27 import org.graalvm.compiler.api.replacements.MethodSubstitution;
  28 import org.graalvm.compiler.core.test.GraalCompilerTest;
  29 import org.graalvm.compiler.debug.DebugContext;

  30 import org.graalvm.compiler.graph.Node;
  31 import org.graalvm.compiler.nodes.Invoke;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  34 import org.graalvm.compiler.nodes.spi.LoweringTool;
  35 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  36 import org.graalvm.compiler.phases.common.DeadCodeEliminationPhase;
  37 import org.graalvm.compiler.phases.common.LoweringPhase;
  38 import org.graalvm.compiler.phases.common.inlining.InliningPhase;
  39 import org.graalvm.compiler.phases.tiers.HighTierContext;
  40 import org.graalvm.compiler.replacements.nodes.MacroNode;
  41 
  42 import jdk.vm.ci.code.InstalledCode;
  43 import jdk.vm.ci.code.InvalidInstalledCodeException;
  44 import jdk.vm.ci.meta.ResolvedJavaMethod;
  45 
  46 /**
  47  * Tests if {@link MethodSubstitution}s are inlined correctly. Most test cases only assert that
  48  * there are no remaining invocations in the graph. This is sufficient if the method that is being
  49  * substituted is a native method. For Java methods, additional checks are necessary.
  50  */
  51 public abstract class MethodSubstitutionTest extends GraalCompilerTest {
  52 
  53     @SuppressWarnings("try")
  54     protected StructuredGraph testGraph(final String snippet) {
  55         DebugContext debug = getDebugContext();
  56         try (DebugContext.Scope s = debug.scope("MethodSubstitutionTest", getResolvedJavaMethod(snippet))) {
  57             StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES, debug);
  58             HighTierContext context = getDefaultHighTierContext();
  59             debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
  60             new InliningPhase(new CanonicalizerPhase()).apply(graph, context);
  61             debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
  62             new CanonicalizerPhase().apply(graph, context);
  63             new DeadCodeEliminationPhase().apply(graph);
  64             // Try to ensure any macro nodes are lowered to expose any resulting invokes
  65             if (graph.getNodes().filter(MacroNode.class).isNotEmpty()) {
  66                 new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
  67             }
  68             if (graph.getNodes().filter(MacroNode.class).isNotEmpty()) {
  69                 new LoweringPhase(new CanonicalizerPhase(), LoweringTool.StandardLoweringStage.MID_TIER).apply(graph, context);
  70             }
  71             assertNotInGraph(graph, MacroNode.class);
  72             assertNotInGraph(graph, Invoke.class);
  73             return graph;
  74         } catch (Throwable e) {
  75             throw debug.handle(e);
  76         }
  77     }
  78 
  79     protected static StructuredGraph assertNotInGraph(StructuredGraph graph, Class<?> clazz) {
  80         for (Node node : graph.getNodes()) {
  81             if (clazz.isInstance(node)) {
  82                 fail(node.toString());
  83             }
  84         }
  85         return graph;
  86     }
  87 
  88     protected void testSubstitution(String testMethodName, Class<?> intrinsicClass, Class<?> holder, String methodName, Class<?>[] parameterTypes, boolean optional, Object[] args1, Object[] args2) {
  89         ResolvedJavaMethod realMethod = getResolvedJavaMethod(holder, methodName, parameterTypes);
  90         ResolvedJavaMethod testMethod = getResolvedJavaMethod(testMethodName);
  91         StructuredGraph graph = testGraph(testMethodName);
  92 
  93         // Check to see if the resulting graph contains the expected node
  94         StructuredGraph replacement = getReplacements().getSubstitution(realMethod, -1);
  95         if (replacement == null && !optional) {


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