From 8259f7825d19c6f8cd9953e684909566010c7272 Mon Sep 17 00:00:00 2001 From: Matt D'Souza Date: Fri, 6 Dec 2024 16:07:26 +0100 Subject: [PATCH] Compiler intrinsics for new Bytecode DSL APIs should be optional --- .../TruffleGraphBuilderPlugins.java | 140 ++++++++++-------- .../TruffleInvocationPlugins.java | 13 +- 2 files changed, 87 insertions(+), 66 deletions(-) diff --git a/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/substitutions/TruffleGraphBuilderPlugins.java b/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/substitutions/TruffleGraphBuilderPlugins.java index 4d630932e245..e1241a74112f 100644 --- a/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/substitutions/TruffleGraphBuilderPlugins.java +++ b/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/substitutions/TruffleGraphBuilderPlugins.java @@ -590,7 +590,7 @@ public static void registerFrameWithoutBoxingPlugins(InvocationPlugins plugins, registerFrameAccessors(r, types, JavaKind.Byte); int accessTag = types.FrameSlotKind_javaKindToTagIndex.get(JavaKind.Object); - registerGet(r, JavaKind.Object, accessTag, "unsafeUncheckedGet", JavaKind.Object.name()); + registerGet(r, JavaKind.Object, accessTag, "unsafeUncheckedGet" + JavaKind.Object.name(), true); registerOSRFrameTransferMethods(r); @@ -621,11 +621,10 @@ private static void registerFrameAccessors(Registration r, KnownTruffleTypes typ int accessTag = types.FrameSlotKind_javaKindToTagIndex.get(accessKind); String nameSuffix = accessKind.name(); boolean isPrimitiveAccess = accessKind.isPrimitive(); - String[] indexedGetPrefixes = new String[]{"get", "unsafeGet", "expect", "unsafeExpect"}; - for (String prefix : indexedGetPrefixes) { - registerGet(r, accessKind, accessTag, prefix, nameSuffix); + registerGet(r, accessKind, accessTag, "get" + nameSuffix, false); + for (String prefix : new String[]{"unsafeGet", "expect", "unsafeExpect"}) { + registerGet(r, accessKind, accessTag, prefix + nameSuffix, true); } - r.register(new RequiredInvocationPlugin("get" + nameSuffix + "Static", Receiver.class, int.class) { @Override public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver frameNode, ValueNode frameSlotNode) { @@ -639,20 +638,8 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec } }); - String[] indexedSetPrefixes = new String[]{"set", "unsafeSet"}; - for (String prefix : indexedSetPrefixes) { - r.register(new RequiredInvocationPlugin(prefix + nameSuffix, Receiver.class, int.class, getJavaClass(accessKind)) { - @Override - public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver frameNode, ValueNode frameSlotNode, ValueNode value) { - int frameSlotIndex = maybeGetConstantNumberedFrameSlotIndex(frameNode, frameSlotNode); - if (frameSlotIndex >= 0) { - b.add(new VirtualFrameSetNode(frameNode, frameSlotIndex, accessTag, value, VirtualFrameAccessType.Indexed, VirtualFrameAccessFlags.NON_STATIC_UPDATE)); - return true; - } - return false; - } - }); - } + registerSet(r, accessKind, accessTag, "set" + nameSuffix, false); + registerSet(r, accessKind, accessTag, "unsafeSet" + nameSuffix, true); r.register(new RequiredInvocationPlugin("set" + nameSuffix + "Static", Receiver.class, int.class, getJavaClass(accessKind)) { @Override public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver frameNode, ValueNode frameSlotNode, ValueNode value) { @@ -678,8 +665,8 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec }); } - private static void registerGet(Registration r, JavaKind accessKind, int accessTag, String prefix, String nameSuffix) { - r.register(new RequiredInvocationPlugin(prefix + nameSuffix, Receiver.class, int.class) { + private static void registerGet(Registration r, JavaKind accessKind, int accessTag, String name, boolean optional) { + r.register(new InvocationPlugin(name, Receiver.class, int.class) { @Override public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver frameNode, ValueNode frameSlotNode) { int frameSlotIndex = maybeGetConstantNumberedFrameSlotIndex(frameNode, frameSlotNode); @@ -689,6 +676,70 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec } return false; } + + @Override + public boolean isOptional() { + return optional; + } + }); + } + + private static void registerSet(Registration r, JavaKind accessKind, int accessTag, String name, boolean optional) { + r.register(new InvocationPlugin(name, Receiver.class, int.class, getJavaClass(accessKind)) { + @Override + public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver frameNode, ValueNode frameSlotNode, ValueNode value) { + int frameSlotIndex = maybeGetConstantNumberedFrameSlotIndex(frameNode, frameSlotNode); + if (frameSlotIndex >= 0) { + b.add(new VirtualFrameSetNode(frameNode, frameSlotIndex, accessTag, value, VirtualFrameAccessType.Indexed, VirtualFrameAccessFlags.NON_STATIC_UPDATE)); + return true; + } + return false; + } + + @Override + public boolean isOptional() { + return optional; + } + }); + } + + private static void registerCopy(Registration r, String name, boolean optional) { + r.register(new InvocationPlugin(name, Receiver.class, int.class, int.class) { + @Override + public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode frameSlot1, ValueNode frameSlot2) { + int frameSlot1Index = maybeGetConstantNumberedFrameSlotIndex(receiver, frameSlot1); + int frameSlot2Index = maybeGetConstantNumberedFrameSlotIndex(receiver, frameSlot2); + if (frameSlot1Index >= 0 && frameSlot2Index >= 0) { + b.add(new VirtualFrameCopyNode(receiver, frameSlot1Index, frameSlot2Index, VirtualFrameAccessType.Indexed, VirtualFrameAccessFlags.NON_STATIC_UPDATE)); + return true; + } + return false; + } + + @Override + public boolean isOptional() { + return optional; + } + }); + } + + private static void registerClear(Registration r, String name, int illegalTag, boolean optional) { + r.register(new InvocationPlugin(name, Receiver.class, int.class) { + @Override + public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode frameSlot) { + int frameSlotIndex = maybeGetConstantNumberedFrameSlotIndex(receiver, frameSlot); + if (frameSlotIndex >= 0) { + b.add(new VirtualFrameClearNode(receiver, frameSlotIndex, illegalTag, VirtualFrameAccessType.Indexed, + VirtualFrameAccessFlags.NON_STATIC_UPDATE)); + return true; + } + return false; + } + + @Override + public boolean isOptional() { + return optional; + } }); } @@ -814,34 +865,10 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec } }); - for (String prefix : new String[]{"", "unsafe"}) { - r.register(new RequiredInvocationPlugin(buildCamelCaseName(prefix, "copy"), Receiver.class, int.class, int.class) { - @Override - public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode frameSlot1, ValueNode frameSlot2) { - int frameSlot1Index = maybeGetConstantNumberedFrameSlotIndex(receiver, frameSlot1); - int frameSlot2Index = maybeGetConstantNumberedFrameSlotIndex(receiver, frameSlot2); - if (frameSlot1Index >= 0 && frameSlot2Index >= 0) { - b.add(new VirtualFrameCopyNode(receiver, frameSlot1Index, frameSlot2Index, VirtualFrameAccessType.Indexed, VirtualFrameAccessFlags.NON_STATIC_UPDATE)); - return true; - } - return false; - } - }); - - r.register(new RequiredInvocationPlugin(buildCamelCaseName(prefix, "clear"), Receiver.class, int.class) { - @Override - public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode frameSlot) { - int frameSlotIndex = maybeGetConstantNumberedFrameSlotIndex(receiver, frameSlot); - if (frameSlotIndex >= 0) { - b.add(new VirtualFrameClearNode(receiver, frameSlotIndex, illegalTag, VirtualFrameAccessType.Indexed, - VirtualFrameAccessFlags.NON_STATIC_UPDATE)); - return true; - } - return false; - } - }); - - } + registerCopy(r, "copy", false); + registerCopy(r, "unsafeCopy", true); + registerClear(r, "clear", illegalTag, false); + registerClear(r, "unsafeClear", illegalTag, true); r.register(new RequiredInvocationPlugin("clearPrimitiveStatic", Receiver.class, int.class) { @Override @@ -968,21 +995,6 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec }); } - private static String buildCamelCaseName(String prefix, String name) { - if (prefix.isEmpty()) { - return name; - } else { - return prefix + firstLetterUpperCase(name); - } - } - - private static String firstLetterUpperCase(String name) { - if (name == null || name.isEmpty()) { - return name; - } - return Character.toUpperCase(name.charAt(0)) + name.substring(1, name.length()); - } - public static void registerNodePlugins(InvocationPlugins plugins, KnownTruffleTypes types, MetaAccessProvider metaAccess, boolean canDelayIntrinsification, ConstantReflectionProvider constantReflection) { Registration r = new Registration(plugins, new ResolvedJavaSymbol(types.Node)); diff --git a/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/substitutions/TruffleInvocationPlugins.java b/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/substitutions/TruffleInvocationPlugins.java index 74cfba77807d..fc315a6593ad 100644 --- a/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/substitutions/TruffleInvocationPlugins.java +++ b/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/truffle/substitutions/TruffleInvocationPlugins.java @@ -90,8 +90,7 @@ private static void registerBytecodePlugins(InvocationPlugins plugins, Replaceme plugins.registerIntrinsificationPredicate(t -> t.getName().equals("Lcom/oracle/truffle/api/bytecode/BytecodeDSLUncheckedAccess;")); InvocationPlugins.Registration r = new InvocationPlugins.Registration(plugins, "com.oracle.truffle.api.bytecode.BytecodeDSLUncheckedAccess", replacements); - r.register(new InlineOnlyInvocationPlugin("uncheckedCast", Receiver.class, Object.class, - Class.class) { + r.register(new InvocationPlugin("uncheckedCast", Receiver.class, Object.class, Class.class) { @Override public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode object, ValueNode clazz) { @@ -111,6 +110,16 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec return true; } } + + @Override + public boolean inlineOnly() { + return true; + } + + @Override + public boolean isOptional() { + return true; + } }); }