36 import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
37 import org.graalvm.compiler.nodes.spi.Virtualizable;
38 import org.graalvm.compiler.phases.VerifyPhase;
39 import org.graalvm.compiler.phases.tiers.PhaseContext;
40
41 import jdk.vm.ci.meta.ResolvedJavaMethod;
42 import jdk.vm.ci.meta.ResolvedJavaType;
43
44 /**
45 * Verifies that node types implementing the {@link Virtualizable} interface use it correctly.
46 * Implementors of {@link Virtualizable#virtualize(org.graalvm.compiler.nodes.spi.VirtualizerTool)}
47 * must not apply effects on their {@link Graph graph} that cannot be easily undone.
48 */
49 public class VerifyVirtualizableUsage extends VerifyPhase<PhaseContext> {
50 @Override
51 public boolean checkContract() {
52 return false;
53 }
54
55 @Override
56 protected boolean verify(StructuredGraph graph, PhaseContext context) {
57 final ResolvedJavaType graphType = context.getMetaAccess().lookupJavaType(Graph.class);
58 final ResolvedJavaType virtualizableType = context.getMetaAccess().lookupJavaType(Virtualizable.class);
59 final ResolvedJavaType constantNodeType = context.getMetaAccess().lookupJavaType(ConstantNode.class);
60 if (virtualizableType.isAssignableFrom(graph.method().getDeclaringClass()) && graph.method().getName().equals("virtualize")) {
61 for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
62 int bci = t.invoke().bci();
63 ResolvedJavaMethod callee = t.targetMethod();
64 String calleeName = callee.getName();
65 if (callee.getDeclaringClass().equals(graphType)) {
66 if (calleeName.equals("add") || calleeName.equals("addWithoutUnique") || calleeName.equals("addOrUnique") || calleeName.equals("addWithoutUniqueWithInputs") ||
67 calleeName.equals("addOrUniqueWithInputs")) {
68 verifyVirtualizableEffectArguments(constantNodeType, graph.method(), callee, bci, t.arguments(), 1);
69 }
70 }
71 }
72 }
73 return true;
74 }
75
76 private static void verifyVirtualizableEffectArguments(ResolvedJavaType constantNodeType, ResolvedJavaMethod caller, ResolvedJavaMethod callee, int bciCaller,
77 NodeInputList<? extends Node> arguments, int startIdx) {
78 /*
79 * Virtualizable.virtualize should never apply effects on the graph during the execution of
80 * the call as the handling of loops during pea might be speculative and does not hold. We
81 * should only allow nodes changing the graph that do no harm like constants.
82 */
83 int i = 0;
84 for (Node arg : arguments) {
85 if (i >= startIdx) {
86 Stamp argStamp = ((ValueNode) arg).stamp(NodeView.DEFAULT);
87 if (argStamp instanceof ObjectStamp) {
88 ObjectStamp objectStamp = (ObjectStamp) argStamp;
89 ResolvedJavaType argStampType = objectStamp.type();
90 if (!(argStampType.equals(constantNodeType))) {
91 StackTraceElement e = caller.asStackTraceElement(bciCaller);
92 throw new VerificationError("%s:Parameter %d in call to %s (which has effects on the graph) is not a " +
93 "constant and thus not safe to apply during speculative virtualization.", e, i, callee.format("%H.%n(%p)"));
|
36 import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
37 import org.graalvm.compiler.nodes.spi.Virtualizable;
38 import org.graalvm.compiler.phases.VerifyPhase;
39 import org.graalvm.compiler.phases.tiers.PhaseContext;
40
41 import jdk.vm.ci.meta.ResolvedJavaMethod;
42 import jdk.vm.ci.meta.ResolvedJavaType;
43
44 /**
45 * Verifies that node types implementing the {@link Virtualizable} interface use it correctly.
46 * Implementors of {@link Virtualizable#virtualize(org.graalvm.compiler.nodes.spi.VirtualizerTool)}
47 * must not apply effects on their {@link Graph graph} that cannot be easily undone.
48 */
49 public class VerifyVirtualizableUsage extends VerifyPhase<PhaseContext> {
50 @Override
51 public boolean checkContract() {
52 return false;
53 }
54
55 @Override
56 protected void verify(StructuredGraph graph, PhaseContext context) {
57 final ResolvedJavaType graphType = context.getMetaAccess().lookupJavaType(Graph.class);
58 final ResolvedJavaType virtualizableType = context.getMetaAccess().lookupJavaType(Virtualizable.class);
59 final ResolvedJavaType constantNodeType = context.getMetaAccess().lookupJavaType(ConstantNode.class);
60 if (virtualizableType.isAssignableFrom(graph.method().getDeclaringClass()) && graph.method().getName().equals("virtualize")) {
61 for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
62 int bci = t.invoke().bci();
63 ResolvedJavaMethod callee = t.targetMethod();
64 String calleeName = callee.getName();
65 if (callee.getDeclaringClass().equals(graphType)) {
66 if (calleeName.equals("add") || calleeName.equals("addWithoutUnique") || calleeName.equals("addOrUnique") || calleeName.equals("addWithoutUniqueWithInputs") ||
67 calleeName.equals("addOrUniqueWithInputs")) {
68 verifyVirtualizableEffectArguments(constantNodeType, graph.method(), callee, bci, t.arguments(), 1);
69 }
70 }
71 }
72 }
73 }
74
75 private static void verifyVirtualizableEffectArguments(ResolvedJavaType constantNodeType, ResolvedJavaMethod caller, ResolvedJavaMethod callee, int bciCaller,
76 NodeInputList<? extends Node> arguments, int startIdx) {
77 /*
78 * Virtualizable.virtualize should never apply effects on the graph during the execution of
79 * the call as the handling of loops during pea might be speculative and does not hold. We
80 * should only allow nodes changing the graph that do no harm like constants.
81 */
82 int i = 0;
83 for (Node arg : arguments) {
84 if (i >= startIdx) {
85 Stamp argStamp = ((ValueNode) arg).stamp(NodeView.DEFAULT);
86 if (argStamp instanceof ObjectStamp) {
87 ObjectStamp objectStamp = (ObjectStamp) argStamp;
88 ResolvedJavaType argStampType = objectStamp.type();
89 if (!(argStampType.equals(constantNodeType))) {
90 StackTraceElement e = caller.asStackTraceElement(bciCaller);
91 throw new VerificationError("%s:Parameter %d in call to %s (which has effects on the graph) is not a " +
92 "constant and thus not safe to apply during speculative virtualization.", e, i, callee.format("%H.%n(%p)"));
|