From 02a37c71daefd5504b3ae568dfd5a9bfeb108f68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20D=C3=B6rbandt?= Date: Tue, 24 Nov 2026 16:22:01 +0100 Subject: [PATCH 1/5] Avoid casts in global access paths --- .../builtins/objects/function/PArguments.java | 10 +++++++++- .../bytecode_dsl/PBytecodeDSLRootNode.java | 6 +++--- .../python/nodes/frame/DeleteGlobalNode.java | 10 +++++++--- .../nodes/frame/ReadGlobalOrBuiltinNode.java | 17 +++++++++++------ .../python/nodes/frame/WriteGlobalNode.java | 12 ++++++++---- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PArguments.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PArguments.java index 5bead17104..fdc3789d8a 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PArguments.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PArguments.java @@ -128,13 +128,21 @@ public static void setGlobals(Object[] arguments, PythonObject globals) { } public static PythonObject getGlobals(Object[] arguments) { - return (PythonObject) arguments[INDEX_GLOBALS_ARGUMENT]; + return (PythonObject) getGlobalsNoCast(arguments); } public static PythonObject getGlobals(Frame frame) { return getGlobals(frame.getArguments()); } + public static Object getGlobalsNoCast(Object[] arguments) { + return arguments[INDEX_GLOBALS_ARGUMENT]; + } + + public static Object getGlobalsNoCast(Frame frame) { + return getGlobalsNoCast(frame.getArguments()); + } + public static PythonObject getGlobalsSafe(Frame frame) { if (frame.getArguments()[INDEX_GLOBALS_ARGUMENT] instanceof PythonObject) { return getGlobals(frame); diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java index 65174414f9..6db2c8f1cb 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java @@ -1649,7 +1649,7 @@ public static void performLocals(VirtualFrame frame, TruffleString name, @Specialization(guards = "!hasLocals(frame)") public static void performGlobals(VirtualFrame frame, TruffleString name, @Cached DeleteGlobalNode deleteNode) { - deleteNode.executeWithGlobals(frame, PArguments.getGlobals(frame), name); + deleteNode.executeWithGlobals(frame, PArguments.getGlobalsNoCast(frame), name); } public static boolean hasLocals(VirtualFrame frame) { @@ -2800,7 +2800,7 @@ public static final class DeleteGlobal { @Specialization public static void perform(VirtualFrame frame, TruffleString name, @Cached DeleteGlobalNode deleteNode) { - deleteNode.executeWithGlobals(frame, PArguments.getGlobals(frame), name); + deleteNode.executeWithGlobals(frame, PArguments.getGlobalsNoCast(frame), name); } } @@ -3687,7 +3687,7 @@ public static final class Import { @InliningCutoff public static Object doImport(VirtualFrame frame, TruffleString name, TruffleString[] fromList, int level, @Cached ImportNode node) { - return node.execute(frame, name, PArguments.getGlobals(frame), fromList, level); + return node.execute(frame, name, PArguments.getGlobalsNoCast(frame), fromList, level); } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/DeleteGlobalNode.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/DeleteGlobalNode.java index c08e61a025..adaa7c5db3 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/DeleteGlobalNode.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/DeleteGlobalNode.java @@ -60,6 +60,10 @@ @GenerateUncached @GenerateInline(false) // footprint reduction 52 -> 36 public abstract class DeleteGlobalNode extends PNodeWithContext { + static PDict getDictOrNull(Object globals) { + return globals instanceof PDict dict ? dict : null; + } + @NeverDefault public static DeleteGlobalNode create() { return DeleteGlobalNodeGen.create(); @@ -72,10 +76,10 @@ public final void executeWithGlobals(VirtualFrame frame, Object globals, Truffle public abstract void executeWithGlobalsImpl(VirtualFrame frame, Object globals, TruffleString attributeId); - @Specialization(guards = {"isSingleContext()", "globals == cachedGlobals"}, limit = "1") - static void deleteDictCached(VirtualFrame frame, @SuppressWarnings("unused") PDict globals, TruffleString attributeId, + @Specialization(guards = {"isSingleContext()", "cachedGlobals != null", "isSameObject(globals, cachedGlobals)"}, limit = "1") + static void deleteDictCached(VirtualFrame frame, @SuppressWarnings("unused") Object globals, TruffleString attributeId, @Bind Node inliningTarget, - @Cached(value = "globals", weak = true) PDict cachedGlobals, + @Cached(value = "getDictOrNull(globals)", weak = true) PDict cachedGlobals, @Shared("delItem") @Cached PyObjectDelItem deleteNode, @Shared @Cached PRaiseNode raiseNode) { try { diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/ReadGlobalOrBuiltinNode.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/ReadGlobalOrBuiltinNode.java index 361fbe676d..667ce71f48 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/ReadGlobalOrBuiltinNode.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/ReadGlobalOrBuiltinNode.java @@ -111,7 +111,7 @@ public static PythonModule getGlobalsOwner(VirtualFrame frame) { } public static Shape getGlobalsStorageShape(VirtualFrame frame) { - Object obj = PArguments.getGlobals(frame); + Object obj = PArguments.getGlobalsNoCast(frame); if (obj instanceof PDict dict && dict.getDictStorage() instanceof DynamicObjectStorage dom) { return dom.getStore().getShape(); } @@ -119,7 +119,7 @@ public static Shape getGlobalsStorageShape(VirtualFrame frame) { } public static Shape getGlobalsStorageShapeIfPropMissing(VirtualFrame frame, TruffleString name) { - Object obj = PArguments.getGlobals(frame); + Object obj = PArguments.getGlobalsNoCast(frame); if (obj instanceof PDict dict && dict.getDictStorage() instanceof DynamicObjectStorage dom) { Shape shape = dom.getStore().getShape(); if (!shape.hasProperty(name)) { @@ -133,7 +133,7 @@ public static Shape getGlobalsStorageShapeIfPropMissing(VirtualFrame frame, Truf @Specialization(guards = { /* static: */ "isSingleContext(inliningTarget)", "globalsOwner != null", "globalsShape != null", // /* static: */ "!hasMaterializedDict(globalsShape)", "builtinGetter != null", // - /* dynamic: */ "getGlobals(frame) == cachedGlobals", "globalsShape == getGlobalsOwnerShape(globalsOwner)", // + /* dynamic: */ "getGlobalsNoCast(frame) == cachedGlobals", "globalsShape == getGlobalsOwnerShape(globalsOwner)", // /* dynamic: */ "getterAccepts(builtinGetter, builtins)", "!isNoValue(result)"}, // excludeForUncached = true, limit = "1") public static Object readBuiltinFastPath(VirtualFrame frame, TruffleString attributeId, @@ -171,7 +171,7 @@ public static Object readBuiltinFromStorage(VirtualFrame frame, TruffleString at } public static Object readFastFromGlobalStore(VirtualFrame frame, TruffleString name, ReadAttributeFromPythonObjectNode readNode) { - Object obj = PArguments.getGlobals(frame); + Object obj = PArguments.getGlobalsNoCast(frame); if (obj instanceof PDict dict && dict.getDictStorage() instanceof DynamicObjectStorage dom) { return readNode.execute(dom.getStore(), name, PNone.NO_VALUE); } @@ -189,6 +189,11 @@ public static Object getGlobals(VirtualFrame frame) { return PArguments.getGlobals(frame); } + @NonIdempotent + public static Object getGlobalsNoCast(VirtualFrame frame) { + return PArguments.getGlobalsNoCast(frame); + } + @NonIdempotent public static Shape getGlobalsOwnerShape(PythonModule module) { return module.getShape(); @@ -202,7 +207,7 @@ public static boolean getterAccepts(PropertyGetter getter, PythonModule module) @ForceQuickening @Specialization(guards = { /* static: */ "isSingleContext(inliningTarget)", "globalsOwner != null", "!hasMaterializedDict(globalsShape)", "getter != null", // - /* dynamic: */ "getGlobals(frame) == cachedGlobals", "getterAccepts(getter, globalsOwner)", "!isNoValue(result)"}, // + /* dynamic: */ "getGlobalsNoCast(frame) == cachedGlobals", "getterAccepts(getter, globalsOwner)", "!isNoValue(result)"}, // replaces = "readBuiltinFromStorage", excludeForUncached = true, limit = "1") public static Object readGlobalFastPath(VirtualFrame frame, TruffleString attributeId, @Bind Node inliningTarget, @@ -236,7 +241,7 @@ public static Object readGlobalOrBuiltinGeneric(VirtualFrame frame, TruffleStrin @Shared("readFromBuiltinsNode") @Cached ReadBuiltinNode readFromBuiltinsNode, @Exclusive @Cached InlinedBranchProfile wasReadFromModule, @Cached PyObjectGetItemOrNull getItemNode) { - PythonObject globalsObj = PArguments.getGlobals(frame); + Object globalsObj = PArguments.getGlobalsNoCast(frame); if (!(globalsObj instanceof PDict globals)) { throw raiseSystemError(inliningTarget); } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/WriteGlobalNode.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/WriteGlobalNode.java index 6ef428c580..d19f948ea4 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/WriteGlobalNode.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/WriteGlobalNode.java @@ -61,6 +61,10 @@ @GenerateUncached @GenerateInline(false) // footprint reduction 60 -> 42 public abstract class WriteGlobalNode extends PNodeWithContext { + static PDict getDictOrNull(Object globals) { + return globals instanceof PDict dict ? dict : null; + } + public static WriteGlobalNode getUncached() { return WriteGlobalNodeGen.getUncached(); } @@ -72,7 +76,7 @@ public static WriteGlobalNode create() { public final void executeObject(VirtualFrame frame, TruffleString name, Object value) { CompilerAsserts.partialEvaluationConstant(name); - executeObjectWithGlobals(frame, PArguments.getGlobals(frame), name, value); + executeObjectWithGlobals(frame, PArguments.getGlobalsNoCast(frame), name, value); } public final void write(Frame frame, Object globals, TruffleString name, Object value) { @@ -82,10 +86,10 @@ public final void write(Frame frame, Object globals, TruffleString name, Object public abstract void executeObjectWithGlobals(VirtualFrame frame, Object globals, TruffleString name, Object value); - @Specialization(guards = {"isSingleContext()", "globals == cachedGlobals", "isBuiltinDict(cachedGlobals)"}, limit = "1") - void writeDictObjectCached(VirtualFrame frame, @SuppressWarnings("unused") PDict globals, TruffleString attributeId, Object value, + @Specialization(guards = {"isSingleContext()", "cachedGlobals != null", "isSameObject(globals, cachedGlobals)", "isBuiltinDict(cachedGlobals)"}, limit = "1") + void writeDictObjectCached(VirtualFrame frame, Object globals, TruffleString attributeId, Object value, @Bind Node inliningTarget, - @Cached(value = "globals", weak = true) PDict cachedGlobals, + @Cached(value = "getDictOrNull(globals)", weak = true) PDict cachedGlobals, @Shared("setItemDict") @Cached HashingCollectionNodes.SetItemNode storeNode) { storeNode.execute(frame, inliningTarget, cachedGlobals, attributeId, value); } From 49a971104493396484983c78c9e1f04d3e53270e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20D=C3=B6rbandt?= Date: Tue, 24 Nov 2026 16:22:06 +0100 Subject: [PATCH 2/5] Avoid casts in sequence storage guards --- .../builtins/objects/common/SequenceStorageNodes.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java index e5f6c45b92..25f4190f89 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java @@ -1730,7 +1730,7 @@ abstract static class SetStorageSliceNode extends Node { public abstract void execute(SequenceStorage s, SliceInfo info, SequenceStorage iterable, boolean canGeneralize); - @Specialization(limit = "MAX_BASIC_STORAGES", guards = {"self.getClass() == cachedClass", "self.getClass() == sequence.getClass()", "replacesWholeSequence(cachedClass, self, info)"}) + @Specialization(limit = "MAX_BASIC_STORAGES", guards = {"self.getClass() == cachedClass", "self.getClass() == sequence.getClass()", "replacesWholeSequence(self, info)"}) static void doWholeSequence(ArrayBasedSequenceStorage self, @SuppressWarnings("unused") SliceInfo info, ArrayBasedSequenceStorage sequence, @SuppressWarnings("unused") boolean canGeneralize, @Cached("self.getClass()") Class cachedClass) { ArrayBasedSequenceStorage selfProfiled = cachedClass.cast(self); @@ -1878,8 +1878,8 @@ static void singleStep(SequenceStorage self, int lo, int hi, SequenceStorage dat } } - protected static boolean replacesWholeSequence(Class cachedClass, ArrayBasedSequenceStorage s, SliceInfo info) { - return info.start == 0 && info.step == 1 && info.stop == cachedClass.cast(s).length(); + protected static boolean replacesWholeSequence(ArrayBasedSequenceStorage s, SliceInfo info) { + return info.start == 0 && info.step == 1 && info.stop == s.length(); } } From 3f0fc5c829f17acc525186c482de781c217fc511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20D=C3=B6rbandt?= Date: Tue, 24 Nov 2026 16:22:12 +0100 Subject: [PATCH 3/5] Avoid casts in method call guards --- .../python/nodes/call/special/AbstractCallMethodNode.java | 7 ++++++- .../python/nodes/call/special/CallBinaryMethodNode.java | 2 +- .../nodes/call/special/CallQuaternaryMethodNode.java | 2 +- .../python/nodes/call/special/CallTernaryMethodNode.java | 2 +- .../python/nodes/call/special/CallUnaryMethodNode.java | 2 +- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/AbstractCallMethodNode.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/AbstractCallMethodNode.java index 400bbc7db0..29b25b6227 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/AbstractCallMethodNode.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/AbstractCallMethodNode.java @@ -169,7 +169,12 @@ public static boolean isSingleContext() { @Idempotent public static PFunction getPFunction(PMethod method) { - return method.getFunction() instanceof PFunction function ? function : null; + return getPFunctionNoCast(method) instanceof PFunction function ? function : null; + } + + @Idempotent + public static Object getPFunctionNoCast(PMethod method) { + return method.getFunction(); } public static Object callUnaryBuiltin(VirtualFrame frame, PythonBuiltinBaseNode builtin, Object arg1) { diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallBinaryMethodNode.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallBinaryMethodNode.java index 214695878d..a60e4b831b 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallBinaryMethodNode.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallBinaryMethodNode.java @@ -148,7 +148,7 @@ public static Object callPFunction(VirtualFrame frame, @SuppressWarnings("unused return invokeNode.execute(frame, inliningTarget, callNode, cachedFunc, args); } - @Specialization(guards = {"cachedFunc != null", "isSingleContext()", "getPFunction(func) == cachedFunc", "hasSimpleSignature(cachedFunc, 3)"}, excludeForUncached = true, // + @Specialization(guards = {"cachedFunc != null", "isSingleContext()", "isSameObject(getPFunctionNoCast(func), cachedFunc)", "hasSimpleSignature(cachedFunc, 3)"}, excludeForUncached = true, // assumptions = "cachedFunc.getCodeStableAssumption()", limit = "getCallSiteInlineCacheMaxDepth()") public static Object callPMethod(VirtualFrame frame, PMethod func, Object arg1, Object arg2, @Bind Node inliningTarget, diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallQuaternaryMethodNode.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallQuaternaryMethodNode.java index 5084982ab3..202b712c9b 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallQuaternaryMethodNode.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallQuaternaryMethodNode.java @@ -134,7 +134,7 @@ public static Object callPFunction(VirtualFrame frame, @SuppressWarnings("unused return invokeNode.execute(frame, inliningTarget, callNode, cachedFunc, args); } - @Specialization(guards = {"cachedFunc != null", "isSingleContext()", "getPFunction(func) == cachedFunc", "hasSimpleSignature(cachedFunc, 5)"}, excludeForUncached = true, // + @Specialization(guards = {"cachedFunc != null", "isSingleContext()", "isSameObject(getPFunctionNoCast(func), cachedFunc)", "hasSimpleSignature(cachedFunc, 5)"}, excludeForUncached = true, // assumptions = "cachedFunc.getCodeStableAssumption()", limit = "getCallSiteInlineCacheMaxDepth()") public static Object callPMethod(VirtualFrame frame, PMethod func, Object arg1, Object arg2, Object arg3, Object arg4, @Bind Node inliningTarget, diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallTernaryMethodNode.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallTernaryMethodNode.java index 49953c0e23..0b2121a281 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallTernaryMethodNode.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallTernaryMethodNode.java @@ -149,7 +149,7 @@ public static Object callPFunction(VirtualFrame frame, @SuppressWarnings("unused return invokeNode.execute(frame, inliningTarget, callNode, cachedFunc, args); } - @Specialization(guards = {"cachedFunc != null", "isSingleContext()", "getPFunction(func) == cachedFunc", "hasSimpleSignature(cachedFunc, 4)"}, excludeForUncached = true, // + @Specialization(guards = {"cachedFunc != null", "isSingleContext()", "isSameObject(getPFunctionNoCast(func), cachedFunc)", "hasSimpleSignature(cachedFunc, 4)"}, excludeForUncached = true, // assumptions = "cachedFunc.getCodeStableAssumption()", limit = "getCallSiteInlineCacheMaxDepth()") public static Object callPMethod(VirtualFrame frame, PMethod func, Object arg1, Object arg2, Object arg3, @Bind Node inliningTarget, diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallUnaryMethodNode.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallUnaryMethodNode.java index c89a8125c5..351de4ebb3 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallUnaryMethodNode.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallUnaryMethodNode.java @@ -154,7 +154,7 @@ public static Object callPFunction(VirtualFrame frame, @SuppressWarnings("unused return invokeNode.execute(frame, inliningTarget, callNode, cachedFunc, args); } - @Specialization(guards = {"cachedFunc != null", "isSingleContext()", "getPFunction(func) == cachedFunc", "hasSimpleSignature(cachedFunc, 2)"}, excludeForUncached = true, // + @Specialization(guards = {"cachedFunc != null", "isSingleContext()", "isSameObject(getPFunctionNoCast(func), cachedFunc)", "hasSimpleSignature(cachedFunc, 2)"}, excludeForUncached = true, // assumptions = "cachedFunc.getCodeStableAssumption()", limit = "getCallSiteInlineCacheMaxDepth()") public static Object callPMethod(VirtualFrame frame, PMethod func, Object arg, @Bind Node inliningTarget, From 80e6bd125d01b692044386634b29375e4f97d6c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20D=C3=B6rbandt?= Date: Tue, 24 Nov 2026 16:22:26 +0100 Subject: [PATCH 4/5] Avoid casts in builtin type attribute caches --- .../bytecode_dsl/PBytecodeDSLRootNode.java | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java index 6db2c8f1cb..4486d51116 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java @@ -2379,6 +2379,10 @@ public static boolean isBuiltinTypeType(Shape cachedShape) { return cachedShape.getDynamicType() == PythonBuiltinClassType.PythonClass; } + static PythonManagedClass getManagedClassOrNull(Object receiver) { + return receiver instanceof PythonManagedClass klass ? klass : null; + } + public static Assumption getTypeStableAssumptionForReceiver(PythonManagedClass klass) { return klass instanceof PythonClass pc ? pc.getTypeStableAssumption() : null; } @@ -2390,15 +2394,15 @@ public static boolean canCacheBuiltinTypeMultiContext(Object value, PythonManage } @Specialization(guards = { - "isSingleContext()", "!isNoValue(result)", // + "isSingleContext()", "cachedReceiver != null", "!isNoValue(result)", // "!hasMaterializedDict(cachedShape)", "isBuiltinTypeType(cachedShape)", // - /* dynamic checks: */ "cachedReceiver == receiver"}, // + /* dynamic checks: */ "isSameObject(receiver, cachedReceiver)"}, // rewriteOn = {UnexpectedResultException.class, InvalidAssumptionException.class}, limit = "3") - static int doTypeInt(@SuppressWarnings("unused") VirtualFrame frame, TruffleString key, PythonManagedClass receiver, + static int doTypeInt(@SuppressWarnings("unused") VirtualFrame frame, TruffleString key, Object receiver, @Bind Node inliningTarget, - @Cached("receiver.getShape()") Shape cachedShape, - @Cached("loadCacheableTypeAttr(receiver, key)") Object result, - @Cached("receiver") PythonManagedClass cachedReceiver, + @Cached("getManagedClassOrNull(receiver)") PythonManagedClass cachedReceiver, + @Cached("cachedReceiver.getShape()") Shape cachedShape, + @Cached("loadCacheableTypeAttr(cachedReceiver, key)") Object result, @Cached("getTypeStableAssumptionForReceiver(cachedReceiver)") Assumption typeStableAssumption) throws UnexpectedResultException, InvalidAssumptionException { assert !shouldInternConstant(key) || isInterned(key); checkAssumption(typeStableAssumption); @@ -2410,15 +2414,15 @@ static int doTypeInt(@SuppressWarnings("unused") VirtualFrame frame, TruffleStri @ForceQuickening @Specialization(guards = { - "isSingleContext()", "!isNoValue(result)", // + "isSingleContext()", "cachedReceiver != null", "!isNoValue(result)", // "!hasMaterializedDict(cachedShape)", "isBuiltinTypeType(cachedShape)", // - /* dynamic checks: */ "cachedReceiver == receiver"}, // + /* dynamic checks: */ "isSameObject(receiver, cachedReceiver)"}, // rewriteOn = InvalidAssumptionException.class, replaces = "doTypeInt", limit = "3") - static Object doType(@SuppressWarnings("unused") VirtualFrame frame, TruffleString key, PythonManagedClass receiver, + static Object doType(@SuppressWarnings("unused") VirtualFrame frame, TruffleString key, Object receiver, @Bind Node inliningTarget, - @Cached("receiver.getShape()") Shape cachedShape, - @Cached("loadCacheableTypeAttr(receiver, key)") Object result, - @Cached("receiver") PythonManagedClass cachedReceiver, + @Cached("getManagedClassOrNull(receiver)") PythonManagedClass cachedReceiver, + @Cached("cachedReceiver.getShape()") Shape cachedShape, + @Cached("loadCacheableTypeAttr(cachedReceiver, key)") Object result, @Cached("getTypeStableAssumptionForReceiver(cachedReceiver)") Assumption typeStableAssumption) throws InvalidAssumptionException { assert !shouldInternConstant(key) || isInterned(key); checkAssumption(typeStableAssumption); @@ -2427,15 +2431,15 @@ static Object doType(@SuppressWarnings("unused") VirtualFrame frame, TruffleStri @ForceQuickening @Specialization(guards = { - "canCacheBuiltinTypeMultiContext(result, cachedReceiver)", "!isNoValue(result)", // + "cachedReceiver != null", "canCacheBuiltinTypeMultiContext(result, cachedReceiver)", "!isNoValue(result)", // "!hasMaterializedDict(cachedShape)", "isBuiltinTypeType(cachedShape)", // - /* dynamic checks: */ "cachedReceiver == receiver"}, // + /* dynamic checks: */ "isSameObject(receiver, cachedReceiver)"}, // limit = "3", excludeForUncached = true) - static Object doBuiltinTypeMultiContext(@SuppressWarnings("unused") VirtualFrame frame, TruffleString key, PythonManagedClass receiver, + static Object doBuiltinTypeMultiContext(@SuppressWarnings("unused") VirtualFrame frame, TruffleString key, Object receiver, @Bind Node inliningTarget, - @Cached("receiver.getShape()") Shape cachedShape, - @Cached("loadCacheableTypeAttr(receiver, key)") Object result, - @Cached(value = "receiver", weak = true) PythonManagedClass cachedReceiver) { + @Cached(value = "getManagedClassOrNull(receiver)", weak = true) PythonManagedClass cachedReceiver, + @Cached("cachedReceiver.getShape()") Shape cachedShape, + @Cached("loadCacheableTypeAttr(cachedReceiver, key)") Object result) { assert !shouldInternConstant(key) || isInterned(key); return result; } @@ -2452,15 +2456,15 @@ public static Assumption getTypeStableAssumptionForValue(Object descrValue) { @ForceQuickening @Specialization(guards = { - "isSingleContext()", "!isNoValue(result)", // + "isSingleContext()", "cachedReceiver != null", "!isNoValue(result)", // "!hasMaterializedDict(cachedShape)", "isBuiltinTypeType(cachedShape)", // - /* dynamic checks: */ "cachedReceiver == receiver"}, // + /* dynamic checks: */ "isSameObject(receiver, cachedReceiver)"}, // limit = "3", excludeForUncached = true, rewriteOn = InvalidAssumptionException.class) - static Object doTypeMutableNonDescriptor(@SuppressWarnings("unused") VirtualFrame frame, TruffleString key, PythonManagedClass receiver, + static Object doTypeMutableNonDescriptor(@SuppressWarnings("unused") VirtualFrame frame, TruffleString key, Object receiver, @Bind Node inliningTarget, - @Cached("receiver.getShape()") Shape cachedShape, - @Cached("loadCacheableTypeAttr(receiver, key, true)") Object result, - @Cached("receiver") PythonManagedClass cachedReceiver, + @Cached("getManagedClassOrNull(receiver)") PythonManagedClass cachedReceiver, + @Cached("cachedReceiver.getShape()") Shape cachedShape, + @Cached("loadCacheableTypeAttr(cachedReceiver, key, true)") Object result, @Cached("getTypeStableAssumptionForValue(result)") Assumption valueTypeStableAssumption, @Cached("getTypeStableAssumptionForReceiver(cachedReceiver)") Assumption typeStableAssumption) throws InvalidAssumptionException { assert !shouldInternConstant(key) || isInterned(key); From 93ade1c318c45572918b4ce0db6c2f22534f520c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20D=C3=B6rbandt?= Date: Tue, 24 Nov 2026 16:22:33 +0100 Subject: [PATCH 5/5] Avoid casts in exception state paths --- .../builtins/objects/function/PArguments.java | 12 +++++++++-- .../bytecode_dsl/PBytecodeDSLRootNode.java | 8 ++++---- .../python/runtime/ExecutionContext.java | 20 +++++++++---------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PArguments.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PArguments.java index fdc3789d8a..b8ab2bdfd7 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PArguments.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PArguments.java @@ -184,7 +184,11 @@ public static AbstractTruffleException getException(Object[] arguments) { } public static AbstractTruffleException getException(Frame frame) { - return (AbstractTruffleException) getExceptionUnchecked(frame.getArguments()); + return (AbstractTruffleException) getExceptionUnchecked(frame); + } + + public static Object getExceptionUnchecked(Frame frame) { + return getExceptionUnchecked(frame.getArguments()); } public static Object getExceptionUnchecked(Object[] arguments) { @@ -197,7 +201,11 @@ public static boolean hasException(Object[] arguments) { } public static void setException(Frame frame, AbstractTruffleException exc) { - setException(frame.getArguments(), exc); + setExceptionUnchecked(frame, exc); + } + + public static void setExceptionUnchecked(Frame frame, Object exc) { + setExceptionUnchecked(frame.getArguments(), exc); } public static void setException(Object[] arguments, AbstractTruffleException exc) { diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java index 4486d51116..68b0a0e286 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java @@ -4300,7 +4300,7 @@ public static void doExceptional(VirtualFrame frame, @Cached GetClassNode getClass, @Cached ExceptionNodes.GetTracebackNode getTraceback, @Cached PyObjectIsTrueNode isTrue) { - AbstractTruffleException savedExcState = PArguments.getException(frame); + Object savedExcState = PArguments.getExceptionUnchecked(frame); try { Object pythonException = exception; if (exception instanceof PException pException) { @@ -4322,7 +4322,7 @@ public static void doExceptional(VirtualFrame frame, } } } finally { - PArguments.setException(frame, savedExcState); + PArguments.setExceptionUnchecked(frame, savedExcState); } } } @@ -4546,7 +4546,7 @@ public static Object doObject(VirtualFrame frame, LocalAccessor currentGenerator // saved exception was fetched from the caller, so what we do is that we override it // to the new caller passed exception state (possibly null meaning we need to fetch // it via stack-walking if needed). - AbstractTruffleException callerExceptionState = PArguments.getException(frame.getArguments()); + Object callerExceptionState = PArguments.getExceptionUnchecked(frame.getArguments()); savedException.setObject(bytecode, frame, callerExceptionState); } @@ -4557,7 +4557,7 @@ public static Object doObject(VirtualFrame frame, LocalAccessor currentGenerator if (!currentGeneratorException.isCleared(bytecode, frame)) { Object currentGenEx = currentGeneratorException.getObject(bytecode, frame); if (currentGenEx != null) { - PArguments.setException(frame, (PException) currentGenEx); + PArguments.setExceptionUnchecked(frame, currentGenEx); } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/ExecutionContext.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/ExecutionContext.java index d172b91259..66837ab760 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/ExecutionContext.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/ExecutionContext.java @@ -298,9 +298,9 @@ protected abstract static class PassExceptionStateNode extends Node { @Specialization(guards = {"!needsExceptionState"}) protected static void dontPassExceptionState(VirtualFrame frame, Node inliningTarget, Object[] callArguments, boolean needsExceptionState, @Cached InlinedConditionProfile hasNoException) { - AbstractTruffleException curExc = PArguments.getException(frame); + Object curExc = PArguments.getExceptionUnchecked(frame); if (hasNoException.profile(inliningTarget, curExc == PException.NO_EXCEPTION)) { - PArguments.setException(callArguments, curExc); + PArguments.setException(callArguments, PException.NO_EXCEPTION); } } @@ -322,7 +322,7 @@ final void executeCutoff(VirtualFrame frame, Node inliningTarget, Object[] callA protected abstract void execute(VirtualFrame frame, Node inliningTarget, Object[] callArguments); - @Specialization(guards = {"isPythonFrame(frame)", "getException(frame) == null"}) + @Specialization(guards = {"isPythonFrame(frame)", "getExceptionUnchecked(frame) == null"}) @InliningCutoff protected static void passExceptionStateFromStackWalk(VirtualFrame frame, Node inliningTarget, Object[] callArguments) { AbstractTruffleException fromStackWalk = GetCaughtExceptionNode.fullStackWalk(); @@ -338,8 +338,8 @@ protected static void passExceptionStateFromStackWalk(VirtualFrame frame, Node i @Specialization(guards = {"isPythonFrame(frame)", "curExc != null"}, replaces = "passExceptionStateFromStackWalk") @InliningCutoff protected static void passGivenExceptionState(VirtualFrame frame, Node inliningTarget, Object[] callArguments, - @Bind("getException(frame)") AbstractTruffleException curExc) { - PArguments.setException(callArguments, curExc); + @Bind("getExceptionUnchecked(frame)") Object curExc) { + PArguments.setExceptionUnchecked(callArguments, curExc); } @ReportPolymorphism.Megamorphic @@ -347,7 +347,7 @@ protected static void passGivenExceptionState(VirtualFrame frame, Node inliningT @InliningCutoff protected static void passExceptionStateFromFrameOrStack(VirtualFrame frame, Node inliningTarget, Object[] callArguments, @Cached InlinedCountingConditionProfile needsStackWalk) { - AbstractTruffleException curExc = PArguments.getException(frame); + Object curExc = PArguments.getExceptionUnchecked(frame); if (needsStackWalk.profile(inliningTarget, curExc == null)) { passExceptionStateFromStackWalk(frame, inliningTarget, callArguments); } else { @@ -371,10 +371,10 @@ private static final class Uncached extends PassExceptionStateNode implements Un @Override protected void execute(VirtualFrame frame, Node inliningTarget, Object[] callArguments, boolean needsExceptionState) { if (PArguments.isPythonFrame(frame)) { - AbstractTruffleException exception = PArguments.getException(frame); + Object exception = PArguments.getExceptionUnchecked(frame); // Always pass the exception when we have it if (exception != null) { - PArguments.setException(callArguments, exception); + PArguments.setExceptionUnchecked(callArguments, exception); } else if (needsExceptionState) { PassExceptionStateInternalNode.passExceptionStateFromStackWalk(frame, null, callArguments); } @@ -700,7 +700,7 @@ private static Object enterWithPythonFrame(VirtualFrame frame, IndirectCallDataB CompilerAsserts.partialEvaluationConstant(defaultReturn); if (callerFlags == 0) { AbstractTruffleException curExc = pythonThreadState.getCaughtException(); - AbstractTruffleException exceptionState = PArguments.getException(frame); + Object exceptionState = PArguments.getExceptionUnchecked(frame); if (exceptionState != curExc) { // the thread state has exception info inconsistent with the current frame's. we // need to force lower frames to walk the stack. @@ -800,7 +800,7 @@ public static Object enter(PythonThreadState threadState, Object[] pArguments) { // If someone set the exception in the arguments explicitly, we do not override it. This // is used in top level code, async handlers, etc., where we want to avoid pointless // stack-walking - if (PArguments.getException(pArguments) == null) { + if (PArguments.getExceptionUnchecked(pArguments) == null) { AbstractTruffleException curExc = threadState.getCaughtException(); if (curExc != null) { threadState.setCaughtException(null);