From d559e88080cf1cc851f6e6c340be6e11e9efedfc Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Fri, 8 Oct 2021 17:59:09 +0100 Subject: [PATCH 01/33] Non functional Truffle implementation of "all" builtin - test Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 19 +++++++++++++++++++ graalpython/lib-graalpython/functions.py | 8 -------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 70696c081e..2c59cbaf75 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -30,6 +30,7 @@ import static com.oracle.graal.python.builtins.objects.PNone.NO_VALUE; import static com.oracle.graal.python.builtins.objects.PNotImplemented.NOT_IMPLEMENTED; import static com.oracle.graal.python.nodes.BuiltinNames.ABS; +import static com.oracle.graal.python.nodes.BuiltinNames.ALL; import static com.oracle.graal.python.nodes.BuiltinNames.ASCII; import static com.oracle.graal.python.nodes.BuiltinNames.BIN; import static com.oracle.graal.python.nodes.BuiltinNames.BREAKPOINT; @@ -1279,6 +1280,24 @@ public int len(VirtualFrame frame, Object obj, } } + @Builtin(name = ALL, minNumOfPositionalArgs = 1) + @GenerateNodeFactory + public abstract static class AllNode extends PythonUnaryBuiltinNode { + @Specialization + static boolean doList(PList object) { + System.out.println("list_all"); + return true; + } + + @Specialization + static boolean doObject(VirtualFrame frame, Object object) { + System.out.println("obj_all"); + return true; + } + } + + + public abstract static class MinMaxNode extends PythonBuiltinNode { @CompilationFinal private boolean seenNonBoolean = false; diff --git a/graalpython/lib-graalpython/functions.py b/graalpython/lib-graalpython/functions.py index 1a18a43564..f70c3b9d8b 100644 --- a/graalpython/lib-graalpython/functions.py +++ b/graalpython/lib-graalpython/functions.py @@ -51,14 +51,6 @@ def any(iterable): return False -@__graalpython__.builtin -def all(iterable): - for i in iterable: - if not i: - return False - return True - - from sys import _getframe as __getframe__ From 334be4c34c73e2a8a256f3862eb4a87bc682249f Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Mon, 11 Oct 2021 11:30:30 +0100 Subject: [PATCH 02/33] all() and any() builtin functions implemented in GraalPython. Only for lists and no error messages/correct behavior for invalid inputs Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 36 ++++++++++++++++--- graalpython/lib-graalpython/functions.py | 12 +++---- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 2c59cbaf75..00f294e5fb 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -31,6 +31,7 @@ import static com.oracle.graal.python.builtins.objects.PNotImplemented.NOT_IMPLEMENTED; import static com.oracle.graal.python.nodes.BuiltinNames.ABS; import static com.oracle.graal.python.nodes.BuiltinNames.ALL; +import static com.oracle.graal.python.nodes.BuiltinNames.ANY; import static com.oracle.graal.python.nodes.BuiltinNames.ASCII; import static com.oracle.graal.python.nodes.BuiltinNames.BIN; import static com.oracle.graal.python.nodes.BuiltinNames.BREAKPOINT; @@ -135,6 +136,7 @@ import com.oracle.graal.python.lib.PyObjectGetAttr; import com.oracle.graal.python.lib.PyObjectGetIter; import com.oracle.graal.python.lib.PyObjectHashNode; +import com.oracle.graal.python.lib.PyObjectIsTrueNode; import com.oracle.graal.python.lib.PyObjectLookupAttr; import com.oracle.graal.python.lib.PyObjectReprAsObjectNode; import com.oracle.graal.python.lib.PyObjectSizeNode; @@ -1284,19 +1286,45 @@ public int len(VirtualFrame frame, Object obj, @GenerateNodeFactory public abstract static class AllNode extends PythonUnaryBuiltinNode { @Specialization - static boolean doList(PList object) { - System.out.println("list_all"); + static boolean doList(VirtualFrame frame, + PList list, + @Cached PyObjectIsTrueNode isTrue) { + Object[] internalArray = list.getSequenceStorage().getInternalArray(); + for (int i = 0; i < list.getSequenceStorage().length(); i++) + if (!isTrue.execute(frame, internalArray[i])) + return false; return true; } @Specialization static boolean doObject(VirtualFrame frame, Object object) { - System.out.println("obj_all"); - return true; +// System.out.println("obj_all"); +// throw raise(TypeError, ErrorMessages.OBJ_NOT_ITERABLE); + return true; } } + @Builtin(name = ANY, minNumOfPositionalArgs = 1) + @GenerateNodeFactory + public abstract static class AnyNode extends PythonUnaryBuiltinNode { + @Specialization + static boolean doList(VirtualFrame frame, + PList list, + @Cached PyObjectIsTrueNode isTrue) { + Object[] internalArray = list.getSequenceStorage().getInternalArray(); + for (int i = 0; i < list.getSequenceStorage().length(); i++) + if (isTrue.execute(frame, internalArray[i])) + return true; + return false; + } + @Specialization + static boolean doObject(VirtualFrame frame, Object object) { +// System.out.println("obj_all"); +// throw raise(TypeError, ErrorMessages.OBJ_NOT_ITERABLE); + return true; + } + } public abstract static class MinMaxNode extends PythonBuiltinNode { diff --git a/graalpython/lib-graalpython/functions.py b/graalpython/lib-graalpython/functions.py index f70c3b9d8b..8c95d56910 100644 --- a/graalpython/lib-graalpython/functions.py +++ b/graalpython/lib-graalpython/functions.py @@ -43,12 +43,12 @@ def hasattr(obj, key): return getattr(obj, key, default) is not default -@__graalpython__.builtin -def any(iterable): - for i in iterable: - if i: - return True - return False +# @__graalpython__.builtin +# def any(iterable): +# for i in iterable: +# if i: +# return True +# return False from sys import _getframe as __getframe__ From 2bdbfa60e268884d4bee3bd8b0a5d59beb3d0d2a Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Mon, 11 Oct 2021 12:48:36 +0100 Subject: [PATCH 03/33] Temporarily removed any(), all() implementation now functional on dicts/sets/tuples as well Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 44 ++++++++----------- graalpython/lib-graalpython/functions.py | 12 ++--- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 00f294e5fb..a6bd1d5c89 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -104,7 +104,9 @@ import com.oracle.graal.python.builtins.objects.code.PCode; import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage; import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes; +import com.oracle.graal.python.builtins.objects.common.HashingStorage; import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary; +import com.oracle.graal.python.builtins.objects.common.PHashingCollection; import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetObjectArrayNode; import com.oracle.graal.python.builtins.objects.common.SequenceNodesFactory.GetObjectArrayNodeGen; import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes; @@ -207,6 +209,7 @@ import com.oracle.graal.python.runtime.exception.PException; import com.oracle.graal.python.runtime.exception.PythonErrorType; import com.oracle.graal.python.runtime.object.PythonObjectFactory; +import com.oracle.graal.python.runtime.sequence.PSequence; import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage; import com.oracle.graal.python.util.CharsetMapping; import com.oracle.graal.python.util.PythonUtils; @@ -1287,42 +1290,33 @@ public int len(VirtualFrame frame, Object obj, public abstract static class AllNode extends PythonUnaryBuiltinNode { @Specialization static boolean doList(VirtualFrame frame, - PList list, + PSequence seq, @Cached PyObjectIsTrueNode isTrue) { - Object[] internalArray = list.getSequenceStorage().getInternalArray(); - for (int i = 0; i < list.getSequenceStorage().length(); i++) + System.out.println("seq_all"); + Object[] internalArray = seq.getSequenceStorage().getInternalArray(); + for (int i = 0; i < seq.getSequenceStorage().length(); i++) if (!isTrue.execute(frame, internalArray[i])) return false; return true; } - @Specialization - static boolean doObject(VirtualFrame frame, Object object) { -// System.out.println("obj_all"); -// throw raise(TypeError, ErrorMessages.OBJ_NOT_ITERABLE); - return true; - } - } - - @Builtin(name = ANY, minNumOfPositionalArgs = 1) - @GenerateNodeFactory - public abstract static class AnyNode extends PythonUnaryBuiltinNode { @Specialization - static boolean doList(VirtualFrame frame, - PList list, + static boolean doHashColl(VirtualFrame frame, + PHashingCollection hc, @Cached PyObjectIsTrueNode isTrue) { - Object[] internalArray = list.getSequenceStorage().getInternalArray(); - for (int i = 0; i < list.getSequenceStorage().length(); i++) - if (isTrue.execute(frame, internalArray[i])) - return true; - return false; + System.out.println("hash_all"); + + for (HashingStorage.DictEntry entry: hc.entries()) + if (!isTrue.execute(frame, entry.key)) + return false; + return true; } - @Specialization - static boolean doObject(VirtualFrame frame, Object object) { -// System.out.println("obj_all"); + @Specialization + static boolean doObject(VirtualFrame frame, Object object) { + System.out.println("obj_all :" + object.getClass()); // throw raise(TypeError, ErrorMessages.OBJ_NOT_ITERABLE); - return true; + return true; // should be a void type instead } } diff --git a/graalpython/lib-graalpython/functions.py b/graalpython/lib-graalpython/functions.py index 8c95d56910..f70c3b9d8b 100644 --- a/graalpython/lib-graalpython/functions.py +++ b/graalpython/lib-graalpython/functions.py @@ -43,12 +43,12 @@ def hasattr(obj, key): return getattr(obj, key, default) is not default -# @__graalpython__.builtin -# def any(iterable): -# for i in iterable: -# if i: -# return True -# return False +@__graalpython__.builtin +def any(iterable): + for i in iterable: + if i: + return True + return False from sys import _getframe as __getframe__ From d63e8b54db28cdb03f7fd316b4855c1234a35f97 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Mon, 11 Oct 2021 17:53:33 +0100 Subject: [PATCH 04/33] all() now throws correctly if given a non-iterable object Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index a6bd1d5c89..3bec9bb0c0 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -173,6 +173,7 @@ import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode; import com.oracle.graal.python.nodes.call.special.LookupAndCallTernaryNode; import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode; +import com.oracle.graal.python.nodes.call.special.LookupSpecialMethodSlotNode; import com.oracle.graal.python.nodes.classes.IsSubtypeNode; import com.oracle.graal.python.nodes.control.GetNextNode; import com.oracle.graal.python.nodes.expression.BinaryArithmetic; @@ -1289,7 +1290,7 @@ public int len(VirtualFrame frame, Object obj, @GenerateNodeFactory public abstract static class AllNode extends PythonUnaryBuiltinNode { @Specialization - static boolean doList(VirtualFrame frame, + static boolean doSeq(VirtualFrame frame, PSequence seq, @Cached PyObjectIsTrueNode isTrue) { System.out.println("seq_all"); @@ -1313,10 +1314,19 @@ static boolean doHashColl(VirtualFrame frame, } @Specialization - static boolean doObject(VirtualFrame frame, Object object) { + static boolean doObject(VirtualFrame frame, + Object object, + @Cached GetClassNode getClassNode, + @Cached(parameters = "Iter") LookupSpecialMethodSlotNode lookupLen, + @Cached PRaiseNode raiseNode) { System.out.println("obj_all :" + object.getClass()); -// throw raise(TypeError, ErrorMessages.OBJ_NOT_ITERABLE); - return true; // should be a void type instead + + Object lenDescr = lookupLen.execute(frame, getClassNode.execute(object), object); + if (lenDescr == PNone.NO_VALUE) + throw raiseNode.raise(TypeError, ErrorMessages.OBJ_NOT_ITERABLE, object); + + // TODO should iterate through the object instead + return true; } } From 0b3c21c720f26a4aa420bc1de32aafc1d80792c3 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Tue, 12 Oct 2021 11:37:16 +0100 Subject: [PATCH 05/33] Functional all() implementation for every iterable (famous last words) Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 3bec9bb0c0..d56695626d 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -1291,8 +1291,8 @@ public int len(VirtualFrame frame, Object obj, public abstract static class AllNode extends PythonUnaryBuiltinNode { @Specialization static boolean doSeq(VirtualFrame frame, - PSequence seq, - @Cached PyObjectIsTrueNode isTrue) { + PSequence seq, + @Cached PyObjectIsTrueNode isTrue) { System.out.println("seq_all"); Object[] internalArray = seq.getSequenceStorage().getInternalArray(); for (int i = 0; i < seq.getSequenceStorage().length(); i++) @@ -1303,8 +1303,8 @@ static boolean doSeq(VirtualFrame frame, @Specialization static boolean doHashColl(VirtualFrame frame, - PHashingCollection hc, - @Cached PyObjectIsTrueNode isTrue) { + PHashingCollection hc, + @Cached PyObjectIsTrueNode isTrue) { System.out.println("hash_all"); for (HashingStorage.DictEntry entry: hc.entries()) @@ -1316,18 +1316,35 @@ static boolean doHashColl(VirtualFrame frame, @Specialization static boolean doObject(VirtualFrame frame, Object object, - @Cached GetClassNode getClassNode, - @Cached(parameters = "Iter") LookupSpecialMethodSlotNode lookupLen, - @Cached PRaiseNode raiseNode) { + @Cached PyObjectGetIter getIter, + @Cached("createNextCall()") LookupAndCallUnaryNode callNode, + @Cached IsBuiltinClassProfile errorProfile, + @Cached PyObjectIsTrueNode isTrue) { System.out.println("obj_all :" + object.getClass()); - Object lenDescr = lookupLen.execute(frame, getClassNode.execute(object), object); - if (lenDescr == PNone.NO_VALUE) - throw raiseNode.raise(TypeError, ErrorMessages.OBJ_NOT_ITERABLE, object); + Object iterator = getIter.execute(frame, object); + while (true) { + try { + Object next = callNode.executeObject(frame, iterator); + if (!isTrue.execute(frame, next)) + return false; + } catch (PException e) { + e.expectStopIteration(errorProfile); + break; + } + } - // TODO should iterate through the object instead return true; } + + protected LookupAndCallUnaryNode createNextCall() { + return LookupAndCallUnaryNode.create(__NEXT__, () -> new LookupAndCallUnaryNode.NoAttributeHandler() { + @Override + public Object execute(Object iterator) { + throw raise(TypeError, ErrorMessages.OBJ_ISNT_ITERATOR, iterator); + } + }); + } } public abstract static class MinMaxNode extends PythonBuiltinNode { From a82ba4f2a347ec01b18ae8e787977ef569d356d7 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Tue, 12 Oct 2021 12:17:35 +0100 Subject: [PATCH 06/33] Added any() builtin, using very similar logic to all() Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 155 +++++++++++------- graalpython/lib-graalpython/functions.py | 8 - 2 files changed, 94 insertions(+), 69 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index d56695626d..90998ca555 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -293,6 +293,100 @@ public Object absObject(VirtualFrame frame, Object object, } } + @Builtin(name = ALL, minNumOfPositionalArgs = 1) + @GenerateNodeFactory + public abstract static class AllNode extends PythonUnaryBuiltinNode { + @Specialization + static boolean doSeq(VirtualFrame frame, + PSequence seq, + @Cached PyObjectIsTrueNode isTrue) { + Object[] internalArray = seq.getSequenceStorage().getInternalArray(); + for (int i = 0; i < seq.getSequenceStorage().length(); i++) + if (!isTrue.execute(frame, internalArray[i])) + return false; + return true; + } + + @Specialization + static boolean doHashColl(VirtualFrame frame, + PHashingCollection hc, + @Cached PyObjectIsTrueNode isTrue) { + for (HashingStorage.DictEntry entry: hc.entries()) + if (!isTrue.execute(frame, entry.key)) + return false; + return true; + } + + @Specialization + static boolean doObject(VirtualFrame frame, + Object object, + @Cached PyObjectGetIter getIter, + @Cached("create(__NEXT__)") LookupAndCallUnaryNode callNode, + @Cached IsBuiltinClassProfile errorProfile, + @Cached PyObjectIsTrueNode isTrue) { + Object iterator = getIter.execute(frame, object); + while (true) { + try { + Object next = callNode.executeObject(frame, iterator); + if (!isTrue.execute(frame, next)) + return false; + } catch (PException e) { + e.expectStopIteration(errorProfile); + break; + } + } + + return true; + } + } + + @Builtin(name = ANY, minNumOfPositionalArgs = 1) + @GenerateNodeFactory + public abstract static class AnyNode extends PythonUnaryBuiltinNode { + @Specialization + static boolean doSeq(VirtualFrame frame, + PSequence seq, + @Cached PyObjectIsTrueNode isTrue) { + Object[] internalArray = seq.getSequenceStorage().getInternalArray(); + for (int i = 0; i < seq.getSequenceStorage().length(); i++) + if (isTrue.execute(frame, internalArray[i])) + return true; + return false; + } + + @Specialization + static boolean doHashColl(VirtualFrame frame, + PHashingCollection hc, + @Cached PyObjectIsTrueNode isTrue) { + for (HashingStorage.DictEntry entry: hc.entries()) + if (isTrue.execute(frame, entry.key)) + return true; + return false; + } + + @Specialization + static boolean doObject(VirtualFrame frame, + Object object, + @Cached PyObjectGetIter getIter, + @Cached("create(__NEXT__)") LookupAndCallUnaryNode callNode, + @Cached IsBuiltinClassProfile errorProfile, + @Cached PyObjectIsTrueNode isTrue) { + Object iterator = getIter.execute(frame, object); + while (true) { + try { + Object next = callNode.executeObject(frame, iterator); + if (isTrue.execute(frame, next)) + return true; + } catch (PException e) { + e.expectStopIteration(errorProfile); + break; + } + } + + return false; + } + } + // bin(object) @Builtin(name = BIN, minNumOfPositionalArgs = 1) @TypeSystemReference(PythonArithmeticTypes.class) @@ -1286,67 +1380,6 @@ public int len(VirtualFrame frame, Object obj, } } - @Builtin(name = ALL, minNumOfPositionalArgs = 1) - @GenerateNodeFactory - public abstract static class AllNode extends PythonUnaryBuiltinNode { - @Specialization - static boolean doSeq(VirtualFrame frame, - PSequence seq, - @Cached PyObjectIsTrueNode isTrue) { - System.out.println("seq_all"); - Object[] internalArray = seq.getSequenceStorage().getInternalArray(); - for (int i = 0; i < seq.getSequenceStorage().length(); i++) - if (!isTrue.execute(frame, internalArray[i])) - return false; - return true; - } - - @Specialization - static boolean doHashColl(VirtualFrame frame, - PHashingCollection hc, - @Cached PyObjectIsTrueNode isTrue) { - System.out.println("hash_all"); - - for (HashingStorage.DictEntry entry: hc.entries()) - if (!isTrue.execute(frame, entry.key)) - return false; - return true; - } - - @Specialization - static boolean doObject(VirtualFrame frame, - Object object, - @Cached PyObjectGetIter getIter, - @Cached("createNextCall()") LookupAndCallUnaryNode callNode, - @Cached IsBuiltinClassProfile errorProfile, - @Cached PyObjectIsTrueNode isTrue) { - System.out.println("obj_all :" + object.getClass()); - - Object iterator = getIter.execute(frame, object); - while (true) { - try { - Object next = callNode.executeObject(frame, iterator); - if (!isTrue.execute(frame, next)) - return false; - } catch (PException e) { - e.expectStopIteration(errorProfile); - break; - } - } - - return true; - } - - protected LookupAndCallUnaryNode createNextCall() { - return LookupAndCallUnaryNode.create(__NEXT__, () -> new LookupAndCallUnaryNode.NoAttributeHandler() { - @Override - public Object execute(Object iterator) { - throw raise(TypeError, ErrorMessages.OBJ_ISNT_ITERATOR, iterator); - } - }); - } - } - public abstract static class MinMaxNode extends PythonBuiltinNode { @CompilationFinal private boolean seenNonBoolean = false; diff --git a/graalpython/lib-graalpython/functions.py b/graalpython/lib-graalpython/functions.py index f70c3b9d8b..6462f07368 100644 --- a/graalpython/lib-graalpython/functions.py +++ b/graalpython/lib-graalpython/functions.py @@ -43,14 +43,6 @@ def hasattr(obj, key): return getattr(obj, key, default) is not default -@__graalpython__.builtin -def any(iterable): - for i in iterable: - if i: - return True - return False - - from sys import _getframe as __getframe__ From 73ac20d4976ca407a0d84d337e2401a37d9fbb63 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Tue, 12 Oct 2021 16:35:38 +0100 Subject: [PATCH 07/33] Specializations on lists/sets/tuples/dicts for all/any Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 125 +++++++++++++----- 1 file changed, 91 insertions(+), 34 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 90998ca555..f3ce9d04be 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -123,6 +123,9 @@ import com.oracle.graal.python.builtins.objects.module.PythonModule; import com.oracle.graal.python.builtins.objects.object.ObjectNodes; import com.oracle.graal.python.builtins.objects.object.PythonObject; +import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary; +import com.oracle.graal.python.builtins.objects.set.PBaseSet; +import com.oracle.graal.python.builtins.objects.set.PSet; import com.oracle.graal.python.builtins.objects.str.PString; import com.oracle.graal.python.builtins.objects.tuple.PTuple; import com.oracle.graal.python.builtins.objects.type.SpecialMethodSlot; @@ -297,24 +300,31 @@ public Object absObject(VirtualFrame frame, Object object, @GenerateNodeFactory public abstract static class AllNode extends PythonUnaryBuiltinNode { @Specialization - static boolean doSeq(VirtualFrame frame, - PSequence seq, - @Cached PyObjectIsTrueNode isTrue) { - Object[] internalArray = seq.getSequenceStorage().getInternalArray(); - for (int i = 0; i < seq.getSequenceStorage().length(); i++) - if (!isTrue.execute(frame, internalArray[i])) - return false; - return true; + static boolean doList(VirtualFrame frame, + PList list, + @Cached PyObjectIsTrueNode isTrueNode) { + return checkSequenceStorage(list, frame, isTrueNode); } @Specialization - static boolean doHashColl(VirtualFrame frame, - PHashingCollection hc, - @Cached PyObjectIsTrueNode isTrue) { - for (HashingStorage.DictEntry entry: hc.entries()) - if (!isTrue.execute(frame, entry.key)) - return false; - return true; + static boolean doTuple(VirtualFrame frame, + PTuple tuple, + @Cached PyObjectIsTrueNode isTrueNode) { + return checkSequenceStorage(tuple, frame, isTrueNode); + } + + @Specialization + static boolean doDict(VirtualFrame frame, + PDict hc, + @Cached PyObjectIsTrueNode isTrueNode) { + return checkHashEntries(hc, frame, isTrueNode); + } + + @Specialization + static boolean doSet(VirtualFrame frame, + PSet hc, + @Cached PyObjectIsTrueNode isTrueNode) { + return checkHashEntries(hc, frame, isTrueNode); } @Specialization @@ -323,12 +333,12 @@ static boolean doObject(VirtualFrame frame, @Cached PyObjectGetIter getIter, @Cached("create(__NEXT__)") LookupAndCallUnaryNode callNode, @Cached IsBuiltinClassProfile errorProfile, - @Cached PyObjectIsTrueNode isTrue) { + @Cached PyObjectIsTrueNode isTrueNode) { Object iterator = getIter.execute(frame, object); while (true) { try { Object next = callNode.executeObject(frame, iterator); - if (!isTrue.execute(frame, next)) + if (!isTrueNode.execute(frame, next)) return false; } catch (PException e) { e.expectStopIteration(errorProfile); @@ -338,30 +348,57 @@ static boolean doObject(VirtualFrame frame, return true; } + + static boolean checkSequenceStorage(PSequence seq, + VirtualFrame frame, + PyObjectIsTrueNode isTrueNode) { + SequenceStorage sequenceStorage = seq.getSequenceStorage(); + Object[] internalArray = sequenceStorage.getInternalArray(); + for (int i = 0; i < sequenceStorage.length(); i++) + if (!isTrueNode.execute(frame, internalArray[i])) + return false; + return true; + } + + static boolean checkHashEntries(PHashingCollection hashingCollection, + VirtualFrame frame, + PyObjectIsTrueNode isTrueNode) { + for (HashingStorage.DictEntry entry: hashingCollection.entries()) + if (!isTrueNode.execute(frame, entry.key)) + return false; + return true; + } } @Builtin(name = ANY, minNumOfPositionalArgs = 1) @GenerateNodeFactory public abstract static class AnyNode extends PythonUnaryBuiltinNode { @Specialization - static boolean doSeq(VirtualFrame frame, - PSequence seq, - @Cached PyObjectIsTrueNode isTrue) { - Object[] internalArray = seq.getSequenceStorage().getInternalArray(); - for (int i = 0; i < seq.getSequenceStorage().length(); i++) - if (isTrue.execute(frame, internalArray[i])) - return true; - return false; + static boolean doList(VirtualFrame frame, + PList list, + @Cached PyObjectIsTrueNode isTrueNode) { + return checkSequenceStorage(list, frame, isTrueNode); } @Specialization - static boolean doHashColl(VirtualFrame frame, - PHashingCollection hc, - @Cached PyObjectIsTrueNode isTrue) { - for (HashingStorage.DictEntry entry: hc.entries()) - if (isTrue.execute(frame, entry.key)) - return true; - return false; + static boolean doTuple(VirtualFrame frame, + PTuple tuple, + @Cached PyObjectIsTrueNode isTrueNode) { + return checkSequenceStorage(tuple, frame, isTrueNode); + } + + @Specialization + static boolean doDict(VirtualFrame frame, + PDict hc, + @Cached PyObjectIsTrueNode isTrueNode) { + return checkHashEntries(hc, frame, isTrueNode); + } + + @Specialization + static boolean doSet(VirtualFrame frame, + PSet hc, + @Cached PyObjectIsTrueNode isTrueNode) { + return checkHashEntries(hc, frame, isTrueNode); } @Specialization @@ -370,12 +407,12 @@ static boolean doObject(VirtualFrame frame, @Cached PyObjectGetIter getIter, @Cached("create(__NEXT__)") LookupAndCallUnaryNode callNode, @Cached IsBuiltinClassProfile errorProfile, - @Cached PyObjectIsTrueNode isTrue) { + @Cached PyObjectIsTrueNode isTrueNode) { Object iterator = getIter.execute(frame, object); while (true) { try { Object next = callNode.executeObject(frame, iterator); - if (isTrue.execute(frame, next)) + if (isTrueNode.execute(frame, next)) return true; } catch (PException e) { e.expectStopIteration(errorProfile); @@ -385,6 +422,26 @@ static boolean doObject(VirtualFrame frame, return false; } + + static boolean checkSequenceStorage(PSequence seq, + VirtualFrame frame, + PyObjectIsTrueNode isTrueNode) { + SequenceStorage sequenceStorage = seq.getSequenceStorage(); + Object[] internalArray = sequenceStorage.getInternalArray(); + for (int i = 0; i < sequenceStorage.length(); i++) + if (isTrueNode.execute(frame, internalArray[i])) + return true; + return false; + } + + static boolean checkHashEntries(PHashingCollection hashingCollection, + VirtualFrame frame, + PyObjectIsTrueNode isTrueNode) { + for (HashingStorage.DictEntry entry: hashingCollection.entries()) + if (isTrueNode.execute(frame, entry.key)) + return true; + return false; + } } // bin(object) From 3df7af0e5c75859be7027b051c561310dee77627 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Wed, 13 Oct 2021 17:10:50 +0100 Subject: [PATCH 08/33] Simply returns True when all() is given a dict Signed-off-by: Octave Larose --- .../oracle/graal/python/builtins/modules/BuiltinFunctions.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index f3ce9d04be..d758491f21 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -317,7 +317,7 @@ static boolean doTuple(VirtualFrame frame, static boolean doDict(VirtualFrame frame, PDict hc, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashEntries(hc, frame, isTrueNode); + return true; } @Specialization From 70d07cbfa709569c732caa8836df6c255dc0e0fa Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Mon, 18 Oct 2021 11:54:01 +0100 Subject: [PATCH 09/33] Fixed dict behaviour (now checks keys instead of entries) sign: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index d758491f21..db78c6bae2 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -317,7 +317,7 @@ static boolean doTuple(VirtualFrame frame, static boolean doDict(VirtualFrame frame, PDict hc, @Cached PyObjectIsTrueNode isTrueNode) { - return true; + return checkHashKeys(hc, frame, isTrueNode); } @Specialization @@ -360,6 +360,15 @@ static boolean checkSequenceStorage(PSequence seq, return true; } + static boolean checkHashKeys(PHashingCollection hashingCollection, + VirtualFrame frame, + PyObjectIsTrueNode isTrueNode) { + for (Object key: hashingCollection.keys()) + if (!isTrueNode.execute(frame, key)) + return false; + return true; + } + static boolean checkHashEntries(PHashingCollection hashingCollection, VirtualFrame frame, PyObjectIsTrueNode isTrueNode) { @@ -391,7 +400,7 @@ static boolean doTuple(VirtualFrame frame, static boolean doDict(VirtualFrame frame, PDict hc, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashEntries(hc, frame, isTrueNode); + return checkHashKeys(hc, frame, isTrueNode); } @Specialization @@ -434,6 +443,15 @@ static boolean checkSequenceStorage(PSequence seq, return false; } + static boolean checkHashKeys(PHashingCollection hashingCollection, + VirtualFrame frame, + PyObjectIsTrueNode isTrueNode) { + for (Object key: hashingCollection.keys()) + if (!isTrueNode.execute(frame, key)) + return false; + return true; + } + static boolean checkHashEntries(PHashingCollection hashingCollection, VirtualFrame frame, PyObjectIsTrueNode isTrueNode) { From cd5bd7679ad03d00e7de4644bf103cf11f3edc2d Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Mon, 18 Oct 2021 12:07:18 +0100 Subject: [PATCH 10/33] Using GetNextNode + removed warnings sign: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 48 ++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index db78c6bae2..6fe72588a9 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -124,7 +124,6 @@ import com.oracle.graal.python.builtins.objects.object.ObjectNodes; import com.oracle.graal.python.builtins.objects.object.PythonObject; import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary; -import com.oracle.graal.python.builtins.objects.set.PBaseSet; import com.oracle.graal.python.builtins.objects.set.PSet; import com.oracle.graal.python.builtins.objects.str.PString; import com.oracle.graal.python.builtins.objects.tuple.PTuple; @@ -176,7 +175,6 @@ import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode; import com.oracle.graal.python.nodes.call.special.LookupAndCallTernaryNode; import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode; -import com.oracle.graal.python.nodes.call.special.LookupSpecialMethodSlotNode; import com.oracle.graal.python.nodes.classes.IsSubtypeNode; import com.oracle.graal.python.nodes.control.GetNextNode; import com.oracle.graal.python.nodes.expression.BinaryArithmetic; @@ -331,15 +329,16 @@ static boolean doSet(VirtualFrame frame, static boolean doObject(VirtualFrame frame, Object object, @Cached PyObjectGetIter getIter, - @Cached("create(__NEXT__)") LookupAndCallUnaryNode callNode, + @Cached GetNextNode nextNode, @Cached IsBuiltinClassProfile errorProfile, @Cached PyObjectIsTrueNode isTrueNode) { Object iterator = getIter.execute(frame, object); while (true) { try { - Object next = callNode.executeObject(frame, iterator); - if (!isTrueNode.execute(frame, next)) + Object next = nextNode.execute(frame, iterator); + if (!isTrueNode.execute(frame, next)) { return false; + } } catch (PException e) { e.expectStopIteration(errorProfile); break; @@ -354,27 +353,33 @@ static boolean checkSequenceStorage(PSequence seq, PyObjectIsTrueNode isTrueNode) { SequenceStorage sequenceStorage = seq.getSequenceStorage(); Object[] internalArray = sequenceStorage.getInternalArray(); - for (int i = 0; i < sequenceStorage.length(); i++) - if (!isTrueNode.execute(frame, internalArray[i])) + for (int i = 0; i < sequenceStorage.length(); i++) { + if (!isTrueNode.execute(frame, internalArray[i])) { return false; + } + } return true; } static boolean checkHashKeys(PHashingCollection hashingCollection, VirtualFrame frame, PyObjectIsTrueNode isTrueNode) { - for (Object key: hashingCollection.keys()) - if (!isTrueNode.execute(frame, key)) + for (Object key: hashingCollection.keys()) { + if (!isTrueNode.execute(frame, key)) { return false; + } + } return true; } static boolean checkHashEntries(PHashingCollection hashingCollection, VirtualFrame frame, PyObjectIsTrueNode isTrueNode) { - for (HashingStorage.DictEntry entry: hashingCollection.entries()) - if (!isTrueNode.execute(frame, entry.key)) + for (HashingStorage.DictEntry entry: hashingCollection.entries()) { + if (!isTrueNode.execute(frame, entry.key)) { return false; + } + } return true; } } @@ -421,8 +426,9 @@ static boolean doObject(VirtualFrame frame, while (true) { try { Object next = callNode.executeObject(frame, iterator); - if (isTrueNode.execute(frame, next)) + if (isTrueNode.execute(frame, next)) { return true; + } } catch (PException e) { e.expectStopIteration(errorProfile); break; @@ -437,27 +443,33 @@ static boolean checkSequenceStorage(PSequence seq, PyObjectIsTrueNode isTrueNode) { SequenceStorage sequenceStorage = seq.getSequenceStorage(); Object[] internalArray = sequenceStorage.getInternalArray(); - for (int i = 0; i < sequenceStorage.length(); i++) - if (isTrueNode.execute(frame, internalArray[i])) + for (int i = 0; i < sequenceStorage.length(); i++) { + if (isTrueNode.execute(frame, internalArray[i])) { return true; + } + } return false; } static boolean checkHashKeys(PHashingCollection hashingCollection, VirtualFrame frame, PyObjectIsTrueNode isTrueNode) { - for (Object key: hashingCollection.keys()) - if (!isTrueNode.execute(frame, key)) + for (Object key: hashingCollection.keys()) { + if (!isTrueNode.execute(frame, key)) { return false; + } + } return true; } static boolean checkHashEntries(PHashingCollection hashingCollection, VirtualFrame frame, PyObjectIsTrueNode isTrueNode) { - for (HashingStorage.DictEntry entry: hashingCollection.entries()) - if (isTrueNode.execute(frame, entry.key)) + for (HashingStorage.DictEntry entry: hashingCollection.entries()) { + if (isTrueNode.execute(frame, entry.key)) { return true; + } + } return false; } } From d2a29a3edb1eaaa51bbc430f46a2d0d761143cf6 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Mon, 18 Oct 2021 17:26:34 +0100 Subject: [PATCH 11/33] Added parent class to AllNode/AnyNode + fixed a bug related to Specialization sign: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 247 +++++++++--------- 1 file changed, 128 insertions(+), 119 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 6fe72588a9..eecba20258 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -124,7 +124,7 @@ import com.oracle.graal.python.builtins.objects.object.ObjectNodes; import com.oracle.graal.python.builtins.objects.object.PythonObject; import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary; -import com.oracle.graal.python.builtins.objects.set.PSet; +import com.oracle.graal.python.builtins.objects.set.PBaseSet; import com.oracle.graal.python.builtins.objects.str.PString; import com.oracle.graal.python.builtins.objects.tuple.PTuple; import com.oracle.graal.python.builtins.objects.type.SpecialMethodSlot; @@ -294,44 +294,117 @@ public Object absObject(VirtualFrame frame, Object object, } } + public abstract static class AllOrAnyNode extends PythonUnaryBuiltinNode { + enum NodeType { ALL, ANY }; + + protected NodeType nodeType; + + protected boolean checkSequenceStorage(PSequence seq, + VirtualFrame frame, + PyObjectIsTrueNode isTrueNode, + NodeType nodeType) { + SequenceStorage sequenceStorage = seq.getSequenceStorage(); + Object[] internalArray = sequenceStorage.getInternalArray(); + for (int i = 0; i < sequenceStorage.length(); i++) { + switch (nodeType) { + case ALL: + if (!isTrueNode.execute(frame, internalArray[i])) { + return false; + } + case ANY: + if (isTrueNode.execute(frame, internalArray[i])) { + return true; + } + } + } + + return nodeType == NodeType.ALL; + } + + protected boolean checkHashKeys(PHashingCollection hashingCollection, + VirtualFrame frame, + PyObjectIsTrueNode isTrueNode, + NodeType nodeType) { + for (Object key: hashingCollection.keys()) { + switch (nodeType) { + case ALL: + if (!isTrueNode.execute(frame, key)) { + return false; + } + case ANY: + if (isTrueNode.execute(frame, key)) { + return true; + } + } + } + + return nodeType == NodeType.ALL; + } + + protected boolean checkHashEntries(PHashingCollection hashingCollection, + VirtualFrame frame, + PyObjectIsTrueNode isTrueNode, + NodeType nodeType) { + for (HashingStorage.DictEntry entry: hashingCollection.entries()) { + switch (nodeType) { + case ALL: + if (!isTrueNode.execute(frame, entry.key)) { + return false; + } + case ANY: + if (isTrueNode.execute(frame, entry.key)) { + return true; + } + } + } + return nodeType == NodeType.ALL; + } + } + @Builtin(name = ALL, minNumOfPositionalArgs = 1) @GenerateNodeFactory - public abstract static class AllNode extends PythonUnaryBuiltinNode { - @Specialization - static boolean doList(VirtualFrame frame, - PList list, - @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(list, frame, isTrueNode); + public abstract static class AllNode extends AllOrAnyNode { + private final NodeType nodeType = NodeType.ALL; + + @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") + public boolean doList(VirtualFrame frame, + PList object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached PyObjectIsTrueNode isTrueNode) { + return checkSequenceStorage(object, frame, isTrueNode, nodeType); } - @Specialization - static boolean doTuple(VirtualFrame frame, - PTuple tuple, + @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") + public boolean doTuple(VirtualFrame frame, + PTuple object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(tuple, frame, isTrueNode); + return checkSequenceStorage(object, frame, isTrueNode, nodeType); } - @Specialization - static boolean doDict(VirtualFrame frame, - PDict hc, + @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") + public boolean doDict(VirtualFrame frame, + PDict object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(hc, frame, isTrueNode); + return checkHashKeys(object, frame, isTrueNode, nodeType); } - @Specialization - static boolean doSet(VirtualFrame frame, - PSet hc, + @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") + public boolean doSet(VirtualFrame frame, + PBaseSet object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashEntries(hc, frame, isTrueNode); + return checkHashEntries(object, frame, isTrueNode, nodeType); } @Specialization - static boolean doObject(VirtualFrame frame, - Object object, - @Cached PyObjectGetIter getIter, - @Cached GetNextNode nextNode, - @Cached IsBuiltinClassProfile errorProfile, - @Cached PyObjectIsTrueNode isTrueNode) { + boolean doObject(VirtualFrame frame, + Object object, + @Cached PyObjectGetIter getIter, + @Cached GetNextNode nextNode, + @Cached IsBuiltinClassProfile errorProfile, + @Cached PyObjectIsTrueNode isTrueNode) { Object iterator = getIter.execute(frame, object); while (true) { try { @@ -347,85 +420,56 @@ static boolean doObject(VirtualFrame frame, return true; } - - static boolean checkSequenceStorage(PSequence seq, - VirtualFrame frame, - PyObjectIsTrueNode isTrueNode) { - SequenceStorage sequenceStorage = seq.getSequenceStorage(); - Object[] internalArray = sequenceStorage.getInternalArray(); - for (int i = 0; i < sequenceStorage.length(); i++) { - if (!isTrueNode.execute(frame, internalArray[i])) { - return false; - } - } - return true; - } - - static boolean checkHashKeys(PHashingCollection hashingCollection, - VirtualFrame frame, - PyObjectIsTrueNode isTrueNode) { - for (Object key: hashingCollection.keys()) { - if (!isTrueNode.execute(frame, key)) { - return false; - } - } - return true; - } - - static boolean checkHashEntries(PHashingCollection hashingCollection, - VirtualFrame frame, - PyObjectIsTrueNode isTrueNode) { - for (HashingStorage.DictEntry entry: hashingCollection.entries()) { - if (!isTrueNode.execute(frame, entry.key)) { - return false; - } - } - return true; - } } @Builtin(name = ANY, minNumOfPositionalArgs = 1) @GenerateNodeFactory - public abstract static class AnyNode extends PythonUnaryBuiltinNode { - @Specialization - static boolean doList(VirtualFrame frame, - PList list, + public abstract static class AnyNode extends AllOrAnyNode { + private final NodeType nodeType = NodeType.ANY; + + @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") + public boolean doList(VirtualFrame frame, + PList object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(list, frame, isTrueNode); + return checkSequenceStorage(object, frame, isTrueNode, nodeType); } - @Specialization - static boolean doTuple(VirtualFrame frame, - PTuple tuple, + @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") + public boolean doTuple(VirtualFrame frame, + PTuple object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(tuple, frame, isTrueNode); + return checkSequenceStorage(object, frame, isTrueNode, nodeType); } - @Specialization - static boolean doDict(VirtualFrame frame, - PDict hc, + @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") + public boolean doDict(VirtualFrame frame, + PDict object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(hc, frame, isTrueNode); + return checkHashKeys(object, frame, isTrueNode, nodeType); } - @Specialization - static boolean doSet(VirtualFrame frame, - PSet hc, + @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") + public boolean doSet(VirtualFrame frame, + PBaseSet object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashEntries(hc, frame, isTrueNode); + return checkHashEntries(object, frame, isTrueNode, nodeType); } @Specialization - static boolean doObject(VirtualFrame frame, - Object object, - @Cached PyObjectGetIter getIter, - @Cached("create(__NEXT__)") LookupAndCallUnaryNode callNode, - @Cached IsBuiltinClassProfile errorProfile, - @Cached PyObjectIsTrueNode isTrueNode) { + boolean doObject(VirtualFrame frame, + Object object, + @Cached PyObjectGetIter getIter, + @Cached GetNextNode nextNode, + @Cached IsBuiltinClassProfile errorProfile, + @Cached PyObjectIsTrueNode isTrueNode) { Object iterator = getIter.execute(frame, object); while (true) { try { - Object next = callNode.executeObject(frame, iterator); + Object next = nextNode.execute(frame, iterator); if (isTrueNode.execute(frame, next)) { return true; } @@ -437,41 +481,6 @@ static boolean doObject(VirtualFrame frame, return false; } - - static boolean checkSequenceStorage(PSequence seq, - VirtualFrame frame, - PyObjectIsTrueNode isTrueNode) { - SequenceStorage sequenceStorage = seq.getSequenceStorage(); - Object[] internalArray = sequenceStorage.getInternalArray(); - for (int i = 0; i < sequenceStorage.length(); i++) { - if (isTrueNode.execute(frame, internalArray[i])) { - return true; - } - } - return false; - } - - static boolean checkHashKeys(PHashingCollection hashingCollection, - VirtualFrame frame, - PyObjectIsTrueNode isTrueNode) { - for (Object key: hashingCollection.keys()) { - if (!isTrueNode.execute(frame, key)) { - return false; - } - } - return true; - } - - static boolean checkHashEntries(PHashingCollection hashingCollection, - VirtualFrame frame, - PyObjectIsTrueNode isTrueNode) { - for (HashingStorage.DictEntry entry: hashingCollection.entries()) { - if (isTrueNode.execute(frame, entry.key)) { - return true; - } - } - return false; - } } // bin(object) From 24c18fa3ffbf9a6a5802b46118d0b01916b9372e Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Thu, 21 Oct 2021 10:52:51 +0100 Subject: [PATCH 12/33] Removed fall-through bug + removed checkHashEntries() sign: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 27 +++++-------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index eecba20258..585be53fdd 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -311,10 +311,12 @@ protected boolean checkSequenceStorage(PSequence seq, if (!isTrueNode.execute(frame, internalArray[i])) { return false; } + break; case ANY: if (isTrueNode.execute(frame, internalArray[i])) { return true; } + break; } } @@ -331,34 +333,17 @@ protected boolean checkHashKeys(PHashingCollection hashingCollection, if (!isTrueNode.execute(frame, key)) { return false; } + break; case ANY: if (isTrueNode.execute(frame, key)) { return true; } + break; } } return nodeType == NodeType.ALL; } - - protected boolean checkHashEntries(PHashingCollection hashingCollection, - VirtualFrame frame, - PyObjectIsTrueNode isTrueNode, - NodeType nodeType) { - for (HashingStorage.DictEntry entry: hashingCollection.entries()) { - switch (nodeType) { - case ALL: - if (!isTrueNode.execute(frame, entry.key)) { - return false; - } - case ANY: - if (isTrueNode.execute(frame, entry.key)) { - return true; - } - } - } - return nodeType == NodeType.ALL; - } } @Builtin(name = ALL, minNumOfPositionalArgs = 1) @@ -395,7 +380,7 @@ public boolean doSet(VirtualFrame frame, PBaseSet object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashEntries(object, frame, isTrueNode, nodeType); + return checkHashKeys(object, frame, isTrueNode, nodeType); } @Specialization @@ -456,7 +441,7 @@ public boolean doSet(VirtualFrame frame, PBaseSet object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashEntries(object, frame, isTrueNode, nodeType); + return checkHashKeys(object, frame, isTrueNode, nodeType); } @Specialization From a5ab86906f99d4a63618f9684e3559efc1e22b5a Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Thu, 21 Oct 2021 15:54:36 +0100 Subject: [PATCH 13/33] Using HashingStorageLibrary instead of PHashingCollection Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 585be53fdd..ca5af16514 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -80,6 +80,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.locks.Condition; import com.oracle.graal.python.PythonFileDetector; import com.oracle.graal.python.PythonLanguage; @@ -323,11 +324,12 @@ protected boolean checkSequenceStorage(PSequence seq, return nodeType == NodeType.ALL; } - protected boolean checkHashKeys(PHashingCollection hashingCollection, + protected boolean checkHashKeys(HashingStorage hashingStorage, VirtualFrame frame, PyObjectIsTrueNode isTrueNode, + HashingStorageLibrary hlib, NodeType nodeType) { - for (Object key: hashingCollection.keys()) { + for (Object key: hlib.keys(hashingStorage)) { switch (nodeType) { case ALL: if (!isTrueNode.execute(frame, key)) { @@ -371,16 +373,18 @@ public boolean doTuple(VirtualFrame frame, public boolean doDict(VirtualFrame frame, PDict object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object, frame, isTrueNode, nodeType); + return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doSet(VirtualFrame frame, PBaseSet object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object, frame, isTrueNode, nodeType); + return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, nodeType); } @Specialization @@ -432,16 +436,18 @@ public boolean doTuple(VirtualFrame frame, public boolean doDict(VirtualFrame frame, PDict object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object, frame, isTrueNode, nodeType); + return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doSet(VirtualFrame frame, PBaseSet object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object, frame, isTrueNode, nodeType); + return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, nodeType); } @Specialization From a432076b6af1e9190cd2051f9fd4e7cf647ade5e Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Thu, 21 Oct 2021 17:45:21 +0100 Subject: [PATCH 14/33] Optimization attempt by specializing on SequenceStorage subtypes Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 115 ++++++++++++++---- 1 file changed, 90 insertions(+), 25 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index ca5af16514..8f8512c1c0 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -212,7 +212,8 @@ import com.oracle.graal.python.runtime.exception.PException; import com.oracle.graal.python.runtime.exception.PythonErrorType; import com.oracle.graal.python.runtime.object.PythonObjectFactory; -import com.oracle.graal.python.runtime.sequence.PSequence; +import com.oracle.graal.python.runtime.sequence.storage.BoolSequenceStorage; +import com.oracle.graal.python.runtime.sequence.storage.IntSequenceStorage; import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage; import com.oracle.graal.python.util.CharsetMapping; import com.oracle.graal.python.util.PythonUtils; @@ -292,36 +293,100 @@ public Object absObject(VirtualFrame frame, Object object, throw raise(TypeError, ErrorMessages.BAD_OPERAND_FOR, "", "abs()", object); } return result; - } + } } public abstract static class AllOrAnyNode extends PythonUnaryBuiltinNode { - enum NodeType { ALL, ANY }; + enum NodeType { ALL, ANY } protected NodeType nodeType; - protected boolean checkSequenceStorage(PSequence seq, + @GenerateNodeFactory + public abstract static class SequenceStorageIterationNode { + public static boolean checkBooleanSequenceStorage(BoolSequenceStorage sequenceStorage, + VirtualFrame frame, + PyObjectIsTrueNode isTrueNode, + NodeType nodeType) { + boolean[] internalArray = sequenceStorage.getInternalBoolArray(); + + for (int i = 0; i < sequenceStorage.length(); i++) { + switch (nodeType) { + case ALL: + if (!isTrueNode.execute(frame, internalArray[i])) { + return false; + } + break; + case ANY: + if (isTrueNode.execute(frame, internalArray[i])) { + return true; + } + break; + } + } + + return nodeType == NodeType.ALL; + } + + public static boolean checkIntSequenceStorage(IntSequenceStorage sequenceStorage, + VirtualFrame frame, + PyObjectIsTrueNode isTrueNode, + NodeType nodeType) { + int[] internalArray = sequenceStorage.getInternalIntArray(); + + for (int i = 0; i < sequenceStorage.length(); i++) { + switch (nodeType) { + case ALL: + if (!isTrueNode.execute(frame, internalArray[i])) { + return false; + } + break; + case ANY: + if (isTrueNode.execute(frame, internalArray[i])) { + return true; + } + break; + } + } + + return nodeType == NodeType.ALL; + } + + public static boolean checkGenericSequenceStorage(SequenceStorage sequenceStorage, + VirtualFrame frame, + PyObjectIsTrueNode isTrueNode, + NodeType nodeType) { + Object[] internalArray = sequenceStorage.getInternalArray(); + for (int i = 0; i < sequenceStorage.length(); i++) { + switch (nodeType) { + case ALL: + if (!isTrueNode.execute(frame, internalArray[i])) { + return false; + } + break; + case ANY: + if (isTrueNode.execute(frame, internalArray[i])) { + return true; + } + break; + } + } + + return nodeType == NodeType.ALL; + } + + } + + protected boolean checkSequenceStorage(SequenceStorage seq, VirtualFrame frame, PyObjectIsTrueNode isTrueNode, NodeType nodeType) { - SequenceStorage sequenceStorage = seq.getSequenceStorage(); - Object[] internalArray = sequenceStorage.getInternalArray(); - for (int i = 0; i < sequenceStorage.length(); i++) { - switch (nodeType) { - case ALL: - if (!isTrueNode.execute(frame, internalArray[i])) { - return false; - } - break; - case ANY: - if (isTrueNode.execute(frame, internalArray[i])) { - return true; - } - break; - } + if (seq instanceof IntSequenceStorage) { + return SequenceStorageIterationNode.checkIntSequenceStorage((IntSequenceStorage) seq, frame, isTrueNode, nodeType); + } else if (seq instanceof BoolSequenceStorage) { + return SequenceStorageIterationNode.checkBooleanSequenceStorage((BoolSequenceStorage) seq, frame, isTrueNode, nodeType); + } else { + return SequenceStorageIterationNode.checkGenericSequenceStorage(seq, frame, isTrueNode, nodeType); } - - return nodeType == NodeType.ALL; } protected boolean checkHashKeys(HashingStorage hashingStorage, @@ -358,7 +423,7 @@ public boolean doList(VirtualFrame frame, PList object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(object, frame, isTrueNode, nodeType); + return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") @@ -366,7 +431,7 @@ public boolean doTuple(VirtualFrame frame, PTuple object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(object, frame, isTrueNode, nodeType); + return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") @@ -421,7 +486,7 @@ public boolean doList(VirtualFrame frame, PList object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(object, frame, isTrueNode, nodeType); + return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") @@ -429,7 +494,7 @@ public boolean doTuple(VirtualFrame frame, PTuple object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(object, frame, isTrueNode, nodeType); + return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") From ac14bc859267181166b2bb2d0950ab4b1dcc35bd Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Thu, 21 Oct 2021 18:10:36 +0100 Subject: [PATCH 15/33] Utilizing LenNode for generic SequenceStorage objects Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 8f8512c1c0..8ef860aca4 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -354,9 +354,10 @@ public static boolean checkIntSequenceStorage(IntSequenceStorage sequenceStorage public static boolean checkGenericSequenceStorage(SequenceStorage sequenceStorage, VirtualFrame frame, PyObjectIsTrueNode isTrueNode, + SequenceStorageNodes.LenNode lenNode, NodeType nodeType) { Object[] internalArray = sequenceStorage.getInternalArray(); - for (int i = 0; i < sequenceStorage.length(); i++) { + for (int i = 0; i < lenNode.execute(sequenceStorage); i++) { switch (nodeType) { case ALL: if (!isTrueNode.execute(frame, internalArray[i])) { @@ -379,13 +380,14 @@ public static boolean checkGenericSequenceStorage(SequenceStorage sequenceStorag protected boolean checkSequenceStorage(SequenceStorage seq, VirtualFrame frame, PyObjectIsTrueNode isTrueNode, + SequenceStorageNodes.LenNode lenNode, NodeType nodeType) { if (seq instanceof IntSequenceStorage) { return SequenceStorageIterationNode.checkIntSequenceStorage((IntSequenceStorage) seq, frame, isTrueNode, nodeType); } else if (seq instanceof BoolSequenceStorage) { return SequenceStorageIterationNode.checkBooleanSequenceStorage((BoolSequenceStorage) seq, frame, isTrueNode, nodeType); } else { - return SequenceStorageIterationNode.checkGenericSequenceStorage(seq, frame, isTrueNode, nodeType); + return SequenceStorageIterationNode.checkGenericSequenceStorage(seq, frame, isTrueNode, lenNode, nodeType); } } @@ -422,16 +424,18 @@ public abstract static class AllNode extends AllOrAnyNode { public boolean doList(VirtualFrame frame, PList object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached SequenceStorageNodes.LenNode lenNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, nodeType); + return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, lenNode, nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doTuple(VirtualFrame frame, PTuple object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached SequenceStorageNodes.LenNode lenNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, nodeType); + return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, lenNode, nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") @@ -485,16 +489,18 @@ public abstract static class AnyNode extends AllOrAnyNode { public boolean doList(VirtualFrame frame, PList object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached SequenceStorageNodes.LenNode lenNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, nodeType); + return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, lenNode, nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doTuple(VirtualFrame frame, PTuple object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached SequenceStorageNodes.LenNode lenNode, @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, nodeType); + return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, lenNode, nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") From 8b6104acc838a69b5145584de2b043798269c875 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Fri, 22 Oct 2021 12:04:26 +0100 Subject: [PATCH 16/33] Attempts to cache PyObjectIsTrueNode and LenNode, reduces code duplication via shouldStopIteration() Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 141 +++++++----------- 1 file changed, 56 insertions(+), 85 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 8ef860aca4..214f09dde5 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -80,7 +80,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.locks.Condition; import com.oracle.graal.python.PythonFileDetector; import com.oracle.graal.python.PythonLanguage; @@ -107,7 +106,6 @@ import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes; import com.oracle.graal.python.builtins.objects.common.HashingStorage; import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary; -import com.oracle.graal.python.builtins.objects.common.PHashingCollection; import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetObjectArrayNode; import com.oracle.graal.python.builtins.objects.common.SequenceNodesFactory.GetObjectArrayNodeGen; import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes; @@ -296,98 +294,84 @@ public Object absObject(VirtualFrame frame, Object object, } } + /** + * Base class for AllNode and AnyNode, as their logic and behaviors are very similar. + */ public abstract static class AllOrAnyNode extends PythonUnaryBuiltinNode { enum NodeType { ALL, ANY } protected NodeType nodeType; + /** + * Specializes on primitive storage types based on SequenceStorage. + */ @GenerateNodeFactory - public abstract static class SequenceStorageIterationNode { - public static boolean checkBooleanSequenceStorage(BoolSequenceStorage sequenceStorage, - VirtualFrame frame, - PyObjectIsTrueNode isTrueNode, - NodeType nodeType) { + public abstract static class AllOrAnySequenceStorageNode extends PythonUnaryBuiltinNode { + @Child private static PyObjectIsTrueNode isTrueNode = PyObjectIsTrueNode.create(); + @Child private static SequenceStorageNodes.LenNode lenNode = SequenceStorageNodes.LenNode.create(); + + public static boolean checkBool(BoolSequenceStorage sequenceStorage, + VirtualFrame frame, + NodeType nodeType) { boolean[] internalArray = sequenceStorage.getInternalBoolArray(); for (int i = 0; i < sequenceStorage.length(); i++) { - switch (nodeType) { - case ALL: - if (!isTrueNode.execute(frame, internalArray[i])) { - return false; - } - break; - case ANY: - if (isTrueNode.execute(frame, internalArray[i])) { - return true; - } - break; + if (shouldStopIteration(frame, internalArray[i], nodeType)) { + return nodeType != NodeType.ALL; } } return nodeType == NodeType.ALL; } - public static boolean checkIntSequenceStorage(IntSequenceStorage sequenceStorage, - VirtualFrame frame, - PyObjectIsTrueNode isTrueNode, - NodeType nodeType) { + public static boolean checkInt(IntSequenceStorage sequenceStorage, + VirtualFrame frame, + NodeType nodeType) { int[] internalArray = sequenceStorage.getInternalIntArray(); for (int i = 0; i < sequenceStorage.length(); i++) { - switch (nodeType) { - case ALL: - if (!isTrueNode.execute(frame, internalArray[i])) { - return false; - } - break; - case ANY: - if (isTrueNode.execute(frame, internalArray[i])) { - return true; - } - break; + if (shouldStopIteration(frame, internalArray[i], nodeType)) { + return nodeType != NodeType.ALL; } } return nodeType == NodeType.ALL; } - public static boolean checkGenericSequenceStorage(SequenceStorage sequenceStorage, - VirtualFrame frame, - PyObjectIsTrueNode isTrueNode, - SequenceStorageNodes.LenNode lenNode, - NodeType nodeType) { + public static boolean checkGeneric(SequenceStorage sequenceStorage, + VirtualFrame frame, + NodeType nodeType) { Object[] internalArray = sequenceStorage.getInternalArray(); for (int i = 0; i < lenNode.execute(sequenceStorage); i++) { - switch (nodeType) { - case ALL: - if (!isTrueNode.execute(frame, internalArray[i])) { - return false; - } - break; - case ANY: - if (isTrueNode.execute(frame, internalArray[i])) { - return true; - } - break; + if (shouldStopIteration(frame, internalArray[i], nodeType)) { + return nodeType != NodeType.ALL; } } return nodeType == NodeType.ALL; } + private static boolean shouldStopIteration(VirtualFrame frame, + Object obj, + NodeType nodeType) { + if (nodeType == NodeType.ALL) { + return !isTrueNode.execute(frame, obj); + } else { // ANY case + return isTrueNode.execute(frame, obj); + } + } + } protected boolean checkSequenceStorage(SequenceStorage seq, VirtualFrame frame, - PyObjectIsTrueNode isTrueNode, - SequenceStorageNodes.LenNode lenNode, NodeType nodeType) { if (seq instanceof IntSequenceStorage) { - return SequenceStorageIterationNode.checkIntSequenceStorage((IntSequenceStorage) seq, frame, isTrueNode, nodeType); + return AllOrAnySequenceStorageNode.checkInt((IntSequenceStorage) seq, frame, nodeType); } else if (seq instanceof BoolSequenceStorage) { - return SequenceStorageIterationNode.checkBooleanSequenceStorage((BoolSequenceStorage) seq, frame, isTrueNode, nodeType); + return AllOrAnySequenceStorageNode.checkBool((BoolSequenceStorage) seq, frame, nodeType); } else { - return SequenceStorageIterationNode.checkGenericSequenceStorage(seq, frame, isTrueNode, lenNode, nodeType); + return AllOrAnySequenceStorageNode.checkGeneric(seq, frame, nodeType); } } @@ -397,17 +381,12 @@ protected boolean checkHashKeys(HashingStorage hashingStorage, HashingStorageLibrary hlib, NodeType nodeType) { for (Object key: hlib.keys(hashingStorage)) { - switch (nodeType) { - case ALL: - if (!isTrueNode.execute(frame, key)) { - return false; - } - break; - case ANY: - if (isTrueNode.execute(frame, key)) { - return true; - } - break; + if (nodeType == NodeType.ALL) { + if (!isTrueNode.execute(frame, key)) { + return false; + } + } else if (isTrueNode.execute(frame, key)) { // ANY case + return true; } } @@ -423,19 +402,15 @@ public abstract static class AllNode extends AllOrAnyNode { @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doList(VirtualFrame frame, PList object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached SequenceStorageNodes.LenNode lenNode, - @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, lenNode, nodeType); + @SuppressWarnings("unused") @Cached GetClassNode getClassNode) { + return checkSequenceStorage(object.getSequenceStorage(), frame, this.nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doTuple(VirtualFrame frame, PTuple object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached SequenceStorageNodes.LenNode lenNode, - @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, lenNode, nodeType); + @SuppressWarnings("unused") @Cached GetClassNode getClassNode) { + return checkSequenceStorage(object.getSequenceStorage(), frame, this.nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") @@ -444,7 +419,7 @@ public boolean doDict(VirtualFrame frame, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, nodeType); + return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, this.nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") @@ -453,7 +428,7 @@ public boolean doSet(VirtualFrame frame, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, nodeType); + return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, this.nodeType); } @Specialization @@ -488,19 +463,15 @@ public abstract static class AnyNode extends AllOrAnyNode { @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doList(VirtualFrame frame, PList object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached SequenceStorageNodes.LenNode lenNode, - @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, lenNode, nodeType); + @SuppressWarnings("unused") @Cached GetClassNode getClassNode) { + return checkSequenceStorage(object.getSequenceStorage(), frame, this.nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doTuple(VirtualFrame frame, PTuple object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached SequenceStorageNodes.LenNode lenNode, - @Cached PyObjectIsTrueNode isTrueNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, isTrueNode, lenNode, nodeType); + @SuppressWarnings("unused") @Cached GetClassNode getClassNode) { + return checkSequenceStorage(object.getSequenceStorage(), frame, this.nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") @@ -509,7 +480,7 @@ public boolean doDict(VirtualFrame frame, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, nodeType); + return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, this.nodeType); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") @@ -518,7 +489,7 @@ public boolean doSet(VirtualFrame frame, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, nodeType); + return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, this.nodeType); } @Specialization From 4a8c67f274280f5983fb359263cae1c58c207a56 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Fri, 22 Oct 2021 15:10:01 +0100 Subject: [PATCH 17/33] Removed nodeType field Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 214f09dde5..ac9c209c0a 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -300,8 +300,6 @@ public Object absObject(VirtualFrame frame, Object object, public abstract static class AllOrAnyNode extends PythonUnaryBuiltinNode { enum NodeType { ALL, ANY } - protected NodeType nodeType; - /** * Specializes on primitive storage types based on SequenceStorage. */ @@ -397,20 +395,19 @@ protected boolean checkHashKeys(HashingStorage hashingStorage, @Builtin(name = ALL, minNumOfPositionalArgs = 1) @GenerateNodeFactory public abstract static class AllNode extends AllOrAnyNode { - private final NodeType nodeType = NodeType.ALL; @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doList(VirtualFrame frame, PList object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, this.nodeType); + return checkSequenceStorage(object.getSequenceStorage(), frame, NodeType.ALL); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doTuple(VirtualFrame frame, PTuple object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, this.nodeType); + return checkSequenceStorage(object.getSequenceStorage(), frame, NodeType.ALL); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") @@ -419,7 +416,7 @@ public boolean doDict(VirtualFrame frame, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, this.nodeType); + return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, NodeType.ALL); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") @@ -428,7 +425,7 @@ public boolean doSet(VirtualFrame frame, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, this.nodeType); + return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, NodeType.ALL); } @Specialization @@ -458,20 +455,19 @@ boolean doObject(VirtualFrame frame, @Builtin(name = ANY, minNumOfPositionalArgs = 1) @GenerateNodeFactory public abstract static class AnyNode extends AllOrAnyNode { - private final NodeType nodeType = NodeType.ANY; - + @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doList(VirtualFrame frame, PList object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, this.nodeType); + return checkSequenceStorage(object.getSequenceStorage(), frame, NodeType.ANY); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doTuple(VirtualFrame frame, PTuple object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, this.nodeType); + return checkSequenceStorage(object.getSequenceStorage(), frame, NodeType.ANY); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") @@ -480,7 +476,7 @@ public boolean doDict(VirtualFrame frame, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, this.nodeType); + return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, NodeType.ANY); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") @@ -489,7 +485,7 @@ public boolean doSet(VirtualFrame frame, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, this.nodeType); + return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, NodeType.ANY); } @Specialization From 3107467cc76da1deeaa1f5e0eea4568879505bcb Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Thu, 28 Oct 2021 14:24:03 +0100 Subject: [PATCH 18/33] Removed shouldStopIteration function by "inlining" it Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index ac9c209c0a..fd172795e6 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -314,8 +314,10 @@ public static boolean checkBool(BoolSequenceStorage sequenceStorage, boolean[] internalArray = sequenceStorage.getInternalBoolArray(); for (int i = 0; i < sequenceStorage.length(); i++) { - if (shouldStopIteration(frame, internalArray[i], nodeType)) { - return nodeType != NodeType.ALL; + if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { + return false; + } else if (isTrueNode.execute(frame, internalArray[i])) { // ANY case + return true; } } @@ -328,8 +330,10 @@ public static boolean checkInt(IntSequenceStorage sequenceStorage, int[] internalArray = sequenceStorage.getInternalIntArray(); for (int i = 0; i < sequenceStorage.length(); i++) { - if (shouldStopIteration(frame, internalArray[i], nodeType)) { - return nodeType != NodeType.ALL; + if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { + return false; + } else if (isTrueNode.execute(frame, internalArray[i])) { // ANY case + return true; } } @@ -341,24 +345,15 @@ public static boolean checkGeneric(SequenceStorage sequenceStorage, NodeType nodeType) { Object[] internalArray = sequenceStorage.getInternalArray(); for (int i = 0; i < lenNode.execute(sequenceStorage); i++) { - if (shouldStopIteration(frame, internalArray[i], nodeType)) { - return nodeType != NodeType.ALL; + if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { + return false; + } else if (isTrueNode.execute(frame, internalArray[i])) { // ANY case + return true; } } return nodeType == NodeType.ALL; } - - private static boolean shouldStopIteration(VirtualFrame frame, - Object obj, - NodeType nodeType) { - if (nodeType == NodeType.ALL) { - return !isTrueNode.execute(frame, obj); - } else { // ANY case - return isTrueNode.execute(frame, obj); - } - } - } protected boolean checkSequenceStorage(SequenceStorage seq, From fb93d4cba7b50423316452b5524fadbb02681db0 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Thu, 28 Oct 2021 14:38:23 +0100 Subject: [PATCH 19/33] AllOrAnySequenceStorageNode: Removed factory generation annotation + changed parent node class Signed-off-by: Octave Larose --- .../graal/python/builtins/modules/BuiltinFunctions.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index fd172795e6..dbf1ffc2b1 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -150,6 +150,7 @@ import com.oracle.graal.python.nodes.ErrorMessages; import com.oracle.graal.python.nodes.GraalPythonTranslationErrorNode; import com.oracle.graal.python.nodes.PGuards; +import com.oracle.graal.python.nodes.PNodeWithContext; import com.oracle.graal.python.nodes.PNodeWithRaise; import com.oracle.graal.python.nodes.PRaiseNode; import com.oracle.graal.python.nodes.PRootNode; @@ -303,8 +304,7 @@ enum NodeType { ALL, ANY } /** * Specializes on primitive storage types based on SequenceStorage. */ - @GenerateNodeFactory - public abstract static class AllOrAnySequenceStorageNode extends PythonUnaryBuiltinNode { + public abstract static class AllOrAnySequenceStorageNode extends PNodeWithContext { @Child private static PyObjectIsTrueNode isTrueNode = PyObjectIsTrueNode.create(); @Child private static SequenceStorageNodes.LenNode lenNode = SequenceStorageNodes.LenNode.create(); From 784b19e810804e7630877045154d5846318f3997 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Thu, 28 Oct 2021 17:18:58 +0100 Subject: [PATCH 20/33] AllOrAnyNode: added child PyObjectIsTrueNode attribute Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 154 ++++++++---------- 1 file changed, 71 insertions(+), 83 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index dbf1ffc2b1..9f683806fd 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -296,84 +296,72 @@ public Object absObject(VirtualFrame frame, Object object, } /** - * Base class for AllNode and AnyNode, as their logic and behaviors are very similar. + * Common class for all() and any() operations, as their logic and behaviors are very similar. */ - public abstract static class AllOrAnyNode extends PythonUnaryBuiltinNode { + public abstract static class AllOrAnyNode extends PythonBuiltinBaseNode { enum NodeType { ALL, ANY } - /** - * Specializes on primitive storage types based on SequenceStorage. - */ - public abstract static class AllOrAnySequenceStorageNode extends PNodeWithContext { - @Child private static PyObjectIsTrueNode isTrueNode = PyObjectIsTrueNode.create(); - @Child private static SequenceStorageNodes.LenNode lenNode = SequenceStorageNodes.LenNode.create(); + @Child private PyObjectIsTrueNode isTrueNode = PyObjectIsTrueNode.create(); - public static boolean checkBool(BoolSequenceStorage sequenceStorage, - VirtualFrame frame, - NodeType nodeType) { - boolean[] internalArray = sequenceStorage.getInternalBoolArray(); + abstract public boolean execute(Frame frame, Object storageObj, NodeType nodeType); - for (int i = 0; i < sequenceStorage.length(); i++) { - if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { - return false; - } else if (isTrueNode.execute(frame, internalArray[i])) { // ANY case - return true; - } + @Specialization + boolean doBoolSequence(VirtualFrame frame, + BoolSequenceStorage sequenceStorage, + NodeType nodeType) { + boolean[] internalArray = sequenceStorage.getInternalBoolArray(); + + for (int i = 0; i < sequenceStorage.length(); i++) { + if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { + return false; + } else if (isTrueNode.execute(frame, internalArray[i])) { // ANY case + return true; } - - return nodeType == NodeType.ALL; } - public static boolean checkInt(IntSequenceStorage sequenceStorage, - VirtualFrame frame, - NodeType nodeType) { - int[] internalArray = sequenceStorage.getInternalIntArray(); + return nodeType == NodeType.ALL; + } - for (int i = 0; i < sequenceStorage.length(); i++) { - if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { - return false; - } else if (isTrueNode.execute(frame, internalArray[i])) { // ANY case - return true; - } + @Specialization + boolean doIntSequence(VirtualFrame frame, + IntSequenceStorage sequenceStorage, + NodeType nodeType) { + int[] internalArray = sequenceStorage.getInternalIntArray(); + + for (int i = 0; i < sequenceStorage.length(); i++) { + if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { + return false; + } else if (isTrueNode.execute(frame, internalArray[i])) { // ANY case + return true; } - - return nodeType == NodeType.ALL; } - public static boolean checkGeneric(SequenceStorage sequenceStorage, - VirtualFrame frame, - NodeType nodeType) { - Object[] internalArray = sequenceStorage.getInternalArray(); - for (int i = 0; i < lenNode.execute(sequenceStorage); i++) { - if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { - return false; - } else if (isTrueNode.execute(frame, internalArray[i])) { // ANY case - return true; - } - } - - return nodeType == NodeType.ALL; - } + return nodeType == NodeType.ALL; } - protected boolean checkSequenceStorage(SequenceStorage seq, - VirtualFrame frame, - NodeType nodeType) { - if (seq instanceof IntSequenceStorage) { - return AllOrAnySequenceStorageNode.checkInt((IntSequenceStorage) seq, frame, nodeType); - } else if (seq instanceof BoolSequenceStorage) { - return AllOrAnySequenceStorageNode.checkBool((BoolSequenceStorage) seq, frame, nodeType); - } else { - return AllOrAnySequenceStorageNode.checkGeneric(seq, frame, nodeType); + @Specialization + boolean doGenericSequence(VirtualFrame frame, + SequenceStorage sequenceStorage, + NodeType nodeType, + @Cached SequenceStorageNodes.LenNode lenNode) { + Object[] internalArray = sequenceStorage.getInternalArray(); + for (int i = 0; i < lenNode.execute(sequenceStorage); i++) { + if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { + return false; + } else if (isTrueNode.execute(frame, internalArray[i])) { // ANY case + return true; + } } + + return nodeType == NodeType.ALL; } - protected boolean checkHashKeys(HashingStorage hashingStorage, - VirtualFrame frame, - PyObjectIsTrueNode isTrueNode, - HashingStorageLibrary hlib, - NodeType nodeType) { - for (Object key: hlib.keys(hashingStorage)) { + @Specialization(limit = "3") + protected boolean doHashStorage(VirtualFrame frame, + HashingStorage hashingStorage, + NodeType nodeType, + @CachedLibrary("hashingStorage") HashingStorageLibrary hlib) { + for (Object key : hlib.keys(hashingStorage)) { if (nodeType == NodeType.ALL) { if (!isTrueNode.execute(frame, key)) { return false; @@ -389,38 +377,38 @@ protected boolean checkHashKeys(HashingStorage hashingStorage, @Builtin(name = ALL, minNumOfPositionalArgs = 1) @GenerateNodeFactory - public abstract static class AllNode extends AllOrAnyNode { + public abstract static class AllNode extends PythonUnaryBuiltinNode { @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doList(VirtualFrame frame, PList object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, NodeType.ALL); + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached AllOrAnyNode allOrAnyNode) { + return allOrAnyNode.execute(frame, object.getSequenceStorage(), AllOrAnyNode.NodeType.ALL); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doTuple(VirtualFrame frame, PTuple object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, NodeType.ALL); + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached AllOrAnyNode allOrAnyNode) { + return allOrAnyNode.execute(frame, object.getSequenceStorage(), AllOrAnyNode.NodeType.ALL); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doDict(VirtualFrame frame, PDict object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, - @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, NodeType.ALL); + @Cached AllOrAnyNode allOrAnyNode) { + return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ALL); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doSet(VirtualFrame frame, PBaseSet object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, - @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, NodeType.ALL); + @Cached AllOrAnyNode allOrAnyNode) { + return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ALL); } @Specialization @@ -449,38 +437,38 @@ boolean doObject(VirtualFrame frame, @Builtin(name = ANY, minNumOfPositionalArgs = 1) @GenerateNodeFactory - public abstract static class AnyNode extends AllOrAnyNode { + public abstract static class AnyNode extends PythonUnaryBuiltinNode { @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doList(VirtualFrame frame, PList object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, NodeType.ANY); + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached AllOrAnyNode allOrAnyNode) { + return allOrAnyNode.execute(frame, object.getSequenceStorage(), AllOrAnyNode.NodeType.ANY); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doTuple(VirtualFrame frame, PTuple object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode) { - return checkSequenceStorage(object.getSequenceStorage(), frame, NodeType.ANY); + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached AllOrAnyNode allOrAnyNode) { + return allOrAnyNode.execute(frame, object.getSequenceStorage(), AllOrAnyNode.NodeType.ANY); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doDict(VirtualFrame frame, PDict object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, - @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, NodeType.ANY); + @Cached AllOrAnyNode allOrAnyNode) { + return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ANY); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") public boolean doSet(VirtualFrame frame, PBaseSet object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @CachedLibrary("object.getDictStorage()") HashingStorageLibrary hlib, - @Cached PyObjectIsTrueNode isTrueNode) { - return checkHashKeys(object.getDictStorage(), frame, isTrueNode, hlib, NodeType.ANY); + @Cached AllOrAnyNode allOrAnyNode) { + return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ANY); } @Specialization From 1828e55280440d73af355845274db595a6d99131 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Fri, 29 Oct 2021 14:27:14 +0100 Subject: [PATCH 21/33] Removed a broken import and an unused one in BuiltinFunctions Signed-off-by: Octave Larose --- .../oracle/graal/python/builtins/modules/BuiltinFunctions.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 9f683806fd..8d98bf7bf3 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -122,7 +122,6 @@ import com.oracle.graal.python.builtins.objects.module.PythonModule; import com.oracle.graal.python.builtins.objects.object.ObjectNodes; import com.oracle.graal.python.builtins.objects.object.PythonObject; -import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary; import com.oracle.graal.python.builtins.objects.set.PBaseSet; import com.oracle.graal.python.builtins.objects.str.PString; import com.oracle.graal.python.builtins.objects.tuple.PTuple; @@ -150,7 +149,6 @@ import com.oracle.graal.python.nodes.ErrorMessages; import com.oracle.graal.python.nodes.GraalPythonTranslationErrorNode; import com.oracle.graal.python.nodes.PGuards; -import com.oracle.graal.python.nodes.PNodeWithContext; import com.oracle.graal.python.nodes.PNodeWithRaise; import com.oracle.graal.python.nodes.PRaiseNode; import com.oracle.graal.python.nodes.PRootNode; From 13a3b265a4d16f8239db385ba0bbf5ac3700aa2c Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Mon, 1 Nov 2021 10:29:39 +0000 Subject: [PATCH 22/33] Removed unnecessary public qualifiers + changed AllOrAnyNode's parent class to PNodeWithContext Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 8d98bf7bf3..e92807efae 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -149,6 +149,7 @@ import com.oracle.graal.python.nodes.ErrorMessages; import com.oracle.graal.python.nodes.GraalPythonTranslationErrorNode; import com.oracle.graal.python.nodes.PGuards; +import com.oracle.graal.python.nodes.PNodeWithContext; import com.oracle.graal.python.nodes.PNodeWithRaise; import com.oracle.graal.python.nodes.PRaiseNode; import com.oracle.graal.python.nodes.PRootNode; @@ -296,7 +297,7 @@ public Object absObject(VirtualFrame frame, Object object, /** * Common class for all() and any() operations, as their logic and behaviors are very similar. */ - public abstract static class AllOrAnyNode extends PythonBuiltinBaseNode { + public abstract static class AllOrAnyNode extends PNodeWithContext { enum NodeType { ALL, ANY } @Child private PyObjectIsTrueNode isTrueNode = PyObjectIsTrueNode.create(); @@ -378,7 +379,7 @@ protected boolean doHashStorage(VirtualFrame frame, public abstract static class AllNode extends PythonUnaryBuiltinNode { @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - public boolean doList(VirtualFrame frame, + boolean doList(VirtualFrame frame, PList object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached AllOrAnyNode allOrAnyNode) { @@ -386,7 +387,7 @@ public boolean doList(VirtualFrame frame, } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - public boolean doTuple(VirtualFrame frame, + boolean doTuple(VirtualFrame frame, PTuple object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached AllOrAnyNode allOrAnyNode) { @@ -394,7 +395,7 @@ public boolean doTuple(VirtualFrame frame, } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - public boolean doDict(VirtualFrame frame, + boolean doDict(VirtualFrame frame, PDict object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached AllOrAnyNode allOrAnyNode) { @@ -402,7 +403,7 @@ public boolean doDict(VirtualFrame frame, } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - public boolean doSet(VirtualFrame frame, + boolean doSet(VirtualFrame frame, PBaseSet object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached AllOrAnyNode allOrAnyNode) { @@ -438,7 +439,7 @@ boolean doObject(VirtualFrame frame, public abstract static class AnyNode extends PythonUnaryBuiltinNode { @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - public boolean doList(VirtualFrame frame, + boolean doList(VirtualFrame frame, PList object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached AllOrAnyNode allOrAnyNode) { @@ -446,7 +447,7 @@ public boolean doList(VirtualFrame frame, } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - public boolean doTuple(VirtualFrame frame, + boolean doTuple(VirtualFrame frame, PTuple object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached AllOrAnyNode allOrAnyNode) { @@ -454,7 +455,7 @@ public boolean doTuple(VirtualFrame frame, } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - public boolean doDict(VirtualFrame frame, + boolean doDict(VirtualFrame frame, PDict object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached AllOrAnyNode allOrAnyNode) { @@ -462,7 +463,7 @@ public boolean doDict(VirtualFrame frame, } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - public boolean doSet(VirtualFrame frame, + boolean doSet(VirtualFrame frame, PBaseSet object, @SuppressWarnings("unused") @Cached GetClassNode getClassNode, @Cached AllOrAnyNode allOrAnyNode) { From 80059f8547ddc4b17dc18b9d4e9206f453f84414 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Mon, 1 Nov 2021 10:38:22 +0000 Subject: [PATCH 23/33] mx checkstyle (had left a public keyword behind accidentally) + code formatting Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index e92807efae..e71ab1933e 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -298,11 +298,11 @@ public Object absObject(VirtualFrame frame, Object object, * Common class for all() and any() operations, as their logic and behaviors are very similar. */ public abstract static class AllOrAnyNode extends PNodeWithContext { - enum NodeType { ALL, ANY } + enum NodeType {ALL, ANY} @Child private PyObjectIsTrueNode isTrueNode = PyObjectIsTrueNode.create(); - abstract public boolean execute(Frame frame, Object storageObj, NodeType nodeType); + abstract boolean execute(Frame frame, Object storageObj, NodeType nodeType); @Specialization boolean doBoolSequence(VirtualFrame frame, @@ -359,7 +359,7 @@ boolean doGenericSequence(VirtualFrame frame, protected boolean doHashStorage(VirtualFrame frame, HashingStorage hashingStorage, NodeType nodeType, - @CachedLibrary("hashingStorage") HashingStorageLibrary hlib) { + @CachedLibrary("hashingStorage") HashingStorageLibrary hlib) { for (Object key : hlib.keys(hashingStorage)) { if (nodeType == NodeType.ALL) { if (!isTrueNode.execute(frame, key)) { @@ -380,33 +380,33 @@ public abstract static class AllNode extends PythonUnaryBuiltinNode { @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doList(VirtualFrame frame, - PList object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + PList object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getSequenceStorage(), AllOrAnyNode.NodeType.ALL); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doTuple(VirtualFrame frame, - PTuple object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + PTuple object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getSequenceStorage(), AllOrAnyNode.NodeType.ALL); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doDict(VirtualFrame frame, - PDict object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + PDict object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ALL); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doSet(VirtualFrame frame, - PBaseSet object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + PBaseSet object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ALL); } @@ -437,36 +437,36 @@ boolean doObject(VirtualFrame frame, @Builtin(name = ANY, minNumOfPositionalArgs = 1) @GenerateNodeFactory public abstract static class AnyNode extends PythonUnaryBuiltinNode { - + @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doList(VirtualFrame frame, - PList object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + PList object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getSequenceStorage(), AllOrAnyNode.NodeType.ANY); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doTuple(VirtualFrame frame, - PTuple object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + PTuple object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getSequenceStorage(), AllOrAnyNode.NodeType.ANY); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doDict(VirtualFrame frame, - PDict object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + PDict object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ANY); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doSet(VirtualFrame frame, - PBaseSet object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + PBaseSet object, + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, + @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ANY); } From 65bbacf151cfe74b998096b69315801e65775819 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Mon, 1 Nov 2021 10:44:55 +0000 Subject: [PATCH 24/33] Shared annotation for AllOrAnyNode/GetClassNode instances Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index e71ab1933e..d11a11d710 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -381,32 +381,32 @@ public abstract static class AllNode extends PythonUnaryBuiltinNode { @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doList(VirtualFrame frame, PList object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, + @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getSequenceStorage(), AllOrAnyNode.NodeType.ALL); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doTuple(VirtualFrame frame, PTuple object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, + @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getSequenceStorage(), AllOrAnyNode.NodeType.ALL); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doDict(VirtualFrame frame, PDict object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, + @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ALL); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doSet(VirtualFrame frame, PBaseSet object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, + @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ALL); } @@ -441,32 +441,32 @@ public abstract static class AnyNode extends PythonUnaryBuiltinNode { @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doList(VirtualFrame frame, PList object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, + @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getSequenceStorage(), AllOrAnyNode.NodeType.ANY); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doTuple(VirtualFrame frame, PTuple object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, + @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getSequenceStorage(), AllOrAnyNode.NodeType.ANY); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doDict(VirtualFrame frame, PDict object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, + @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ANY); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doSet(VirtualFrame frame, PBaseSet object, - @SuppressWarnings("unused") @Cached GetClassNode getClassNode, - @Cached AllOrAnyNode allOrAnyNode) { + @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, + @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ANY); } From 0fb74598b5a37b23eceadf8ad204fcfc77fbfefd Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Wed, 3 Nov 2021 10:43:13 +0000 Subject: [PATCH 25/33] Fixed all builtin behavior Signed-off-by: Octave Larose --- .../graal/python/builtins/modules/BuiltinFunctions.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index d11a11d710..af1e64bc37 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -313,7 +313,7 @@ boolean doBoolSequence(VirtualFrame frame, for (int i = 0; i < sequenceStorage.length(); i++) { if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { return false; - } else if (isTrueNode.execute(frame, internalArray[i])) { // ANY case + } else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) { return true; } } @@ -330,7 +330,7 @@ boolean doIntSequence(VirtualFrame frame, for (int i = 0; i < sequenceStorage.length(); i++) { if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { return false; - } else if (isTrueNode.execute(frame, internalArray[i])) { // ANY case + } else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) { return true; } } @@ -347,7 +347,7 @@ boolean doGenericSequence(VirtualFrame frame, for (int i = 0; i < lenNode.execute(sequenceStorage); i++) { if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { return false; - } else if (isTrueNode.execute(frame, internalArray[i])) { // ANY case + } else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) { return true; } } @@ -365,7 +365,7 @@ protected boolean doHashStorage(VirtualFrame frame, if (!isTrueNode.execute(frame, key)) { return false; } - } else if (isTrueNode.execute(frame, key)) { // ANY case + } else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, key)) { return true; } } From 16b54b63f2593d631869b85716bfe24a4204a3e7 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Wed, 3 Nov 2021 13:38:25 +0000 Subject: [PATCH 26/33] Ran Eclipse formatter for BuiltinFunctions changes Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 83 ++++++++++--------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index af1e64bc37..ca1eabad64 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -291,14 +291,17 @@ public Object absObject(VirtualFrame frame, Object object, throw raise(TypeError, ErrorMessages.BAD_OPERAND_FOR, "", "abs()", object); } return result; - } + } } /** * Common class for all() and any() operations, as their logic and behaviors are very similar. */ public abstract static class AllOrAnyNode extends PNodeWithContext { - enum NodeType {ALL, ANY} + enum NodeType { + ALL, + ANY + } @Child private PyObjectIsTrueNode isTrueNode = PyObjectIsTrueNode.create(); @@ -306,8 +309,8 @@ enum NodeType {ALL, ANY} @Specialization boolean doBoolSequence(VirtualFrame frame, - BoolSequenceStorage sequenceStorage, - NodeType nodeType) { + BoolSequenceStorage sequenceStorage, + NodeType nodeType) { boolean[] internalArray = sequenceStorage.getInternalBoolArray(); for (int i = 0; i < sequenceStorage.length(); i++) { @@ -323,8 +326,8 @@ boolean doBoolSequence(VirtualFrame frame, @Specialization boolean doIntSequence(VirtualFrame frame, - IntSequenceStorage sequenceStorage, - NodeType nodeType) { + IntSequenceStorage sequenceStorage, + NodeType nodeType) { int[] internalArray = sequenceStorage.getInternalIntArray(); for (int i = 0; i < sequenceStorage.length(); i++) { @@ -340,9 +343,9 @@ boolean doIntSequence(VirtualFrame frame, @Specialization boolean doGenericSequence(VirtualFrame frame, - SequenceStorage sequenceStorage, - NodeType nodeType, - @Cached SequenceStorageNodes.LenNode lenNode) { + SequenceStorage sequenceStorage, + NodeType nodeType, + @Cached SequenceStorageNodes.LenNode lenNode) { Object[] internalArray = sequenceStorage.getInternalArray(); for (int i = 0; i < lenNode.execute(sequenceStorage); i++) { if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { @@ -357,9 +360,9 @@ boolean doGenericSequence(VirtualFrame frame, @Specialization(limit = "3") protected boolean doHashStorage(VirtualFrame frame, - HashingStorage hashingStorage, - NodeType nodeType, - @CachedLibrary("hashingStorage") HashingStorageLibrary hlib) { + HashingStorage hashingStorage, + NodeType nodeType, + @CachedLibrary("hashingStorage") HashingStorageLibrary hlib) { for (Object key : hlib.keys(hashingStorage)) { if (nodeType == NodeType.ALL) { if (!isTrueNode.execute(frame, key)) { @@ -380,9 +383,9 @@ public abstract static class AllNode extends PythonUnaryBuiltinNode { @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doList(VirtualFrame frame, - PList object, - @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, - @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { + PList object, + @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, + @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getSequenceStorage(), AllOrAnyNode.NodeType.ALL); } @@ -396,27 +399,27 @@ boolean doTuple(VirtualFrame frame, @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doDict(VirtualFrame frame, - PDict object, - @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, - @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { + PDict object, + @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, + @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ALL); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doSet(VirtualFrame frame, - PBaseSet object, - @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, - @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { + PBaseSet object, + @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, + @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ALL); } @Specialization boolean doObject(VirtualFrame frame, - Object object, - @Cached PyObjectGetIter getIter, - @Cached GetNextNode nextNode, - @Cached IsBuiltinClassProfile errorProfile, - @Cached PyObjectIsTrueNode isTrueNode) { + Object object, + @Cached PyObjectGetIter getIter, + @Cached GetNextNode nextNode, + @Cached IsBuiltinClassProfile errorProfile, + @Cached PyObjectIsTrueNode isTrueNode) { Object iterator = getIter.execute(frame, object); while (true) { try { @@ -440,9 +443,9 @@ public abstract static class AnyNode extends PythonUnaryBuiltinNode { @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doList(VirtualFrame frame, - PList object, - @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, - @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { + PList object, + @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, + @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getSequenceStorage(), AllOrAnyNode.NodeType.ANY); } @@ -456,27 +459,27 @@ boolean doTuple(VirtualFrame frame, @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doDict(VirtualFrame frame, - PDict object, - @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, - @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { + PDict object, + @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, + @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ANY); } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") boolean doSet(VirtualFrame frame, - PBaseSet object, - @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, - @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { + PBaseSet object, + @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, + @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ANY); } @Specialization boolean doObject(VirtualFrame frame, - Object object, - @Cached PyObjectGetIter getIter, - @Cached GetNextNode nextNode, - @Cached IsBuiltinClassProfile errorProfile, - @Cached PyObjectIsTrueNode isTrueNode) { + Object object, + @Cached PyObjectGetIter getIter, + @Cached GetNextNode nextNode, + @Cached IsBuiltinClassProfile errorProfile, + @Cached PyObjectIsTrueNode isTrueNode) { Object iterator = getIter.execute(frame, object); while (true) { try { From aa7cde41b1632b0d95ac147c6c17b9c30173f77f Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Mon, 8 Nov 2021 10:00:31 +0000 Subject: [PATCH 27/33] Removed public identifier on AllOrAnyNode + added static keyword to every all/any specialization method Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index ca1eabad64..41b662c917 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -297,7 +297,7 @@ public Object absObject(VirtualFrame frame, Object object, /** * Common class for all() and any() operations, as their logic and behaviors are very similar. */ - public abstract static class AllOrAnyNode extends PNodeWithContext { + abstract static class AllOrAnyNode extends PNodeWithContext { enum NodeType { ALL, ANY @@ -382,7 +382,7 @@ protected boolean doHashStorage(VirtualFrame frame, public abstract static class AllNode extends PythonUnaryBuiltinNode { @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - boolean doList(VirtualFrame frame, + static boolean doList(VirtualFrame frame, PList object, @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { @@ -390,7 +390,7 @@ boolean doList(VirtualFrame frame, } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - boolean doTuple(VirtualFrame frame, + static boolean doTuple(VirtualFrame frame, PTuple object, @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { @@ -398,7 +398,7 @@ boolean doTuple(VirtualFrame frame, } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - boolean doDict(VirtualFrame frame, + static boolean doDict(VirtualFrame frame, PDict object, @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { @@ -406,7 +406,7 @@ boolean doDict(VirtualFrame frame, } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - boolean doSet(VirtualFrame frame, + static boolean doSet(VirtualFrame frame, PBaseSet object, @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { @@ -414,7 +414,7 @@ boolean doSet(VirtualFrame frame, } @Specialization - boolean doObject(VirtualFrame frame, + static boolean doObject(VirtualFrame frame, Object object, @Cached PyObjectGetIter getIter, @Cached GetNextNode nextNode, @@ -442,7 +442,7 @@ boolean doObject(VirtualFrame frame, public abstract static class AnyNode extends PythonUnaryBuiltinNode { @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - boolean doList(VirtualFrame frame, + static boolean doList(VirtualFrame frame, PList object, @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { @@ -450,7 +450,7 @@ boolean doList(VirtualFrame frame, } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - boolean doTuple(VirtualFrame frame, + static boolean doTuple(VirtualFrame frame, PTuple object, @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { @@ -458,7 +458,7 @@ boolean doTuple(VirtualFrame frame, } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - boolean doDict(VirtualFrame frame, + static boolean doDict(VirtualFrame frame, PDict object, @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { @@ -466,7 +466,7 @@ boolean doDict(VirtualFrame frame, } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - boolean doSet(VirtualFrame frame, + static boolean doSet(VirtualFrame frame, PBaseSet object, @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { @@ -474,7 +474,7 @@ boolean doSet(VirtualFrame frame, } @Specialization - boolean doObject(VirtualFrame frame, + static boolean doObject(VirtualFrame frame, Object object, @Cached PyObjectGetIter getIter, @Cached GetNextNode nextNode, From 5dd75dc38b3079b45054aaf054fac3a79a3038bf Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Mon, 8 Nov 2021 10:09:41 +0000 Subject: [PATCH 28/33] Merged doSet and doDict specialization methods Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 25 ++++--------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 41b662c917..b78c8b4f3e 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -106,6 +106,7 @@ import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes; import com.oracle.graal.python.builtins.objects.common.HashingStorage; import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary; +import com.oracle.graal.python.builtins.objects.common.PHashingCollection; import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetObjectArrayNode; import com.oracle.graal.python.builtins.objects.common.SequenceNodesFactory.GetObjectArrayNodeGen; import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes; @@ -398,16 +399,8 @@ static boolean doTuple(VirtualFrame frame, } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - static boolean doDict(VirtualFrame frame, - PDict object, - @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, - @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { - return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ALL); - } - - @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - static boolean doSet(VirtualFrame frame, - PBaseSet object, + static boolean doHashColl(VirtualFrame frame, + PHashingCollection object, @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ALL); @@ -458,16 +451,8 @@ static boolean doTuple(VirtualFrame frame, } @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - static boolean doDict(VirtualFrame frame, - PDict object, - @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, - @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { - return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ANY); - } - - @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") - static boolean doSet(VirtualFrame frame, - PBaseSet object, + static boolean doHashColl(VirtualFrame frame, + PHashingCollection object, @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ANY); From de15e383c17ea3dc29ba77bc99abd9e49d3fca60 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Mon, 8 Nov 2021 15:53:44 +0000 Subject: [PATCH 29/33] Loop profiling for all/any related methods Signed-off-by: Octave Larose --- .../builtins/modules/BuiltinFunctions.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index b78c8b4f3e..3d1227f695 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -241,11 +241,13 @@ import com.oracle.truffle.api.library.CachedLibrary; import com.oracle.truffle.api.nodes.ExplodeLoop; import com.oracle.truffle.api.nodes.ExplodeLoop.LoopExplosionKind; +import com.oracle.truffle.api.nodes.LoopNode; import com.oracle.truffle.api.nodes.Node; import com.oracle.truffle.api.nodes.RootNode; import com.oracle.truffle.api.nodes.UnexpectedResultException; import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.api.profiles.ConditionProfile; +import com.oracle.truffle.api.profiles.LoopConditionProfile; import com.oracle.truffle.api.source.Source; import com.oracle.truffle.api.utilities.TriState; @@ -306,6 +308,8 @@ enum NodeType { @Child private PyObjectIsTrueNode isTrueNode = PyObjectIsTrueNode.create(); + final private LoopConditionProfile loopConditionProfile = LoopConditionProfile.create(); + abstract boolean execute(Frame frame, Object storageObj, NodeType nodeType); @Specialization @@ -313,13 +317,15 @@ boolean doBoolSequence(VirtualFrame frame, BoolSequenceStorage sequenceStorage, NodeType nodeType) { boolean[] internalArray = sequenceStorage.getInternalBoolArray(); + int i = 0; - for (int i = 0; i < sequenceStorage.length(); i++) { + while (loopConditionProfile.profile(i < sequenceStorage.length())) { if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { return false; } else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) { return true; } + i++; } return nodeType == NodeType.ALL; @@ -330,8 +336,9 @@ boolean doIntSequence(VirtualFrame frame, IntSequenceStorage sequenceStorage, NodeType nodeType) { int[] internalArray = sequenceStorage.getInternalIntArray(); + int i = 0; - for (int i = 0; i < sequenceStorage.length(); i++) { + while (loopConditionProfile.profile(i < sequenceStorage.length())) { if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { return false; } else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) { @@ -348,7 +355,10 @@ boolean doGenericSequence(VirtualFrame frame, NodeType nodeType, @Cached SequenceStorageNodes.LenNode lenNode) { Object[] internalArray = sequenceStorage.getInternalArray(); - for (int i = 0; i < lenNode.execute(sequenceStorage); i++) { + int i = 0; + int seqLength = lenNode.execute(sequenceStorage); + + while (loopConditionProfile.profile(i < seqLength)) { if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { return false; } else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) { @@ -365,6 +375,7 @@ protected boolean doHashStorage(VirtualFrame frame, NodeType nodeType, @CachedLibrary("hashingStorage") HashingStorageLibrary hlib) { for (Object key : hlib.keys(hashingStorage)) { + LoopNode.reportLoopCount(this, 1); if (nodeType == NodeType.ALL) { if (!isTrueNode.execute(frame, key)) { return false; @@ -407,7 +418,7 @@ static boolean doHashColl(VirtualFrame frame, } @Specialization - static boolean doObject(VirtualFrame frame, + boolean doObject(VirtualFrame frame, Object object, @Cached PyObjectGetIter getIter, @Cached GetNextNode nextNode, @@ -416,6 +427,7 @@ static boolean doObject(VirtualFrame frame, Object iterator = getIter.execute(frame, object); while (true) { try { + LoopNode.reportLoopCount(this, 1); Object next = nextNode.execute(frame, iterator); if (!isTrueNode.execute(frame, next)) { return false; @@ -452,14 +464,14 @@ static boolean doTuple(VirtualFrame frame, @Specialization(guards = "cannotBeOverridden(object, getClassNode)", limit = "1") static boolean doHashColl(VirtualFrame frame, - PHashingCollection object, + PHashingCollection object, @SuppressWarnings("unused") @Shared("getClassNode") @Cached GetClassNode getClassNode, @Shared("allOrAnyNode") @Cached AllOrAnyNode allOrAnyNode) { return allOrAnyNode.execute(frame, object.getDictStorage(), AllOrAnyNode.NodeType.ANY); } @Specialization - static boolean doObject(VirtualFrame frame, + boolean doObject(VirtualFrame frame, Object object, @Cached PyObjectGetIter getIter, @Cached GetNextNode nextNode, @@ -468,6 +480,7 @@ static boolean doObject(VirtualFrame frame, Object iterator = getIter.execute(frame, object); while (true) { try { + LoopNode.reportLoopCount(this, 1); Object next = nextNode.execute(frame, iterator); if (isTrueNode.execute(frame, next)) { return true; From 522e6ed2cf1c9892e840081f283512fe0a7b8efb Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Wed, 10 Nov 2021 09:58:12 +0000 Subject: [PATCH 30/33] Fixed infinite loops + profiling for HashingStorage Signed-off-by: Octave Larose --- .../python/builtins/modules/BuiltinFunctions.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 3d1227f695..7d318d47bf 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -317,9 +317,8 @@ boolean doBoolSequence(VirtualFrame frame, BoolSequenceStorage sequenceStorage, NodeType nodeType) { boolean[] internalArray = sequenceStorage.getInternalBoolArray(); - int i = 0; - while (loopConditionProfile.profile(i < sequenceStorage.length())) { + for (int i = 0; loopConditionProfile.profile(i < sequenceStorage.length()); i++) { if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { return false; } else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) { @@ -336,9 +335,8 @@ boolean doIntSequence(VirtualFrame frame, IntSequenceStorage sequenceStorage, NodeType nodeType) { int[] internalArray = sequenceStorage.getInternalIntArray(); - int i = 0; - while (loopConditionProfile.profile(i < sequenceStorage.length())) { + for (int i = 0; loopConditionProfile.profile(i < sequenceStorage.length()); i++) { if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { return false; } else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) { @@ -355,10 +353,9 @@ boolean doGenericSequence(VirtualFrame frame, NodeType nodeType, @Cached SequenceStorageNodes.LenNode lenNode) { Object[] internalArray = sequenceStorage.getInternalArray(); - int i = 0; int seqLength = lenNode.execute(sequenceStorage); - while (loopConditionProfile.profile(i < seqLength)) { + for (int i = 0; loopConditionProfile.profile(i < seqLength); i++) { if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { return false; } else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) { @@ -374,8 +371,10 @@ protected boolean doHashStorage(VirtualFrame frame, HashingStorage hashingStorage, NodeType nodeType, @CachedLibrary("hashingStorage") HashingStorageLibrary hlib) { - for (Object key : hlib.keys(hashingStorage)) { - LoopNode.reportLoopCount(this, 1); + HashingStorageLibrary.HashingStorageIterator keysIter = hlib.keys(hashingStorage).iterator(); + + for (int i = 0; loopConditionProfile.profile(i < hlib.length(hashingStorage)); i++) { + Object key = keysIter.next(); if (nodeType == NodeType.ALL) { if (!isTrueNode.execute(frame, key)) { return false; From bceb24ae18450e3d89d57f9fa472051cfa12c6a7 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Wed, 10 Nov 2021 10:01:39 +0000 Subject: [PATCH 31/33] Fixed loop profiling for doObject in all/any implementations Signed-off-by: Octave Larose --- .../python/builtins/modules/BuiltinFunctions.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index 7d318d47bf..b864c3e158 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -424,16 +424,20 @@ boolean doObject(VirtualFrame frame, @Cached IsBuiltinClassProfile errorProfile, @Cached PyObjectIsTrueNode isTrueNode) { Object iterator = getIter.execute(frame, object); + int nbrIter = 0; + while (true) { try { - LoopNode.reportLoopCount(this, 1); Object next = nextNode.execute(frame, iterator); + nbrIter++; if (!isTrueNode.execute(frame, next)) { return false; } } catch (PException e) { e.expectStopIteration(errorProfile); break; + } finally { + LoopNode.reportLoopCount(this, nbrIter); } } @@ -477,16 +481,20 @@ boolean doObject(VirtualFrame frame, @Cached IsBuiltinClassProfile errorProfile, @Cached PyObjectIsTrueNode isTrueNode) { Object iterator = getIter.execute(frame, object); + int nbrIter = 0; + while (true) { try { - LoopNode.reportLoopCount(this, 1); Object next = nextNode.execute(frame, iterator); + nbrIter++; if (isTrueNode.execute(frame, next)) { return true; } } catch (PException e) { e.expectStopIteration(errorProfile); break; + } finally { + LoopNode.reportLoopCount(this, nbrIter); } } From 66661b485497d2fcb6122315149e2b4e34afbd4c Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Wed, 10 Nov 2021 17:01:16 +0000 Subject: [PATCH 32/33] loopConditionProfile.profileCounted() calls instead of just profile() Signed-off-by: Octave Larose --- .../python/builtins/modules/BuiltinFunctions.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index b864c3e158..fe4e68f64a 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -317,8 +317,10 @@ boolean doBoolSequence(VirtualFrame frame, BoolSequenceStorage sequenceStorage, NodeType nodeType) { boolean[] internalArray = sequenceStorage.getInternalBoolArray(); + int seqLength = sequenceStorage.length(); - for (int i = 0; loopConditionProfile.profile(i < sequenceStorage.length()); i++) { + loopConditionProfile.profileCounted(seqLength); + for (int i = 0; loopConditionProfile.inject(i < seqLength); i++) { if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { return false; } else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) { @@ -335,8 +337,10 @@ boolean doIntSequence(VirtualFrame frame, IntSequenceStorage sequenceStorage, NodeType nodeType) { int[] internalArray = sequenceStorage.getInternalIntArray(); + int seqLength = sequenceStorage.length(); - for (int i = 0; loopConditionProfile.profile(i < sequenceStorage.length()); i++) { + loopConditionProfile.profileCounted(seqLength); + for (int i = 0; loopConditionProfile.inject(i < seqLength); i++) { if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { return false; } else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) { @@ -355,7 +359,8 @@ boolean doGenericSequence(VirtualFrame frame, Object[] internalArray = sequenceStorage.getInternalArray(); int seqLength = lenNode.execute(sequenceStorage); - for (int i = 0; loopConditionProfile.profile(i < seqLength); i++) { + loopConditionProfile.profileCounted(seqLength); + for (int i = 0; loopConditionProfile.inject(i < seqLength); i++) { if (nodeType == NodeType.ALL && !isTrueNode.execute(frame, internalArray[i])) { return false; } else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) { @@ -372,8 +377,10 @@ protected boolean doHashStorage(VirtualFrame frame, NodeType nodeType, @CachedLibrary("hashingStorage") HashingStorageLibrary hlib) { HashingStorageLibrary.HashingStorageIterator keysIter = hlib.keys(hashingStorage).iterator(); + int seqLength = hlib.length(hashingStorage); - for (int i = 0; loopConditionProfile.profile(i < hlib.length(hashingStorage)); i++) { + loopConditionProfile.profileCounted(seqLength); + for (int i = 0; loopConditionProfile.inject(i < seqLength); i++) { Object key = keysIter.next(); if (nodeType == NodeType.ALL) { if (!isTrueNode.execute(frame, key)) { From fa07d3072a26e9206dffb0ee3de30c619fe85c73 Mon Sep 17 00:00:00 2001 From: Octave Larose Date: Fri, 12 Nov 2021 10:57:07 +0000 Subject: [PATCH 33/33] Fixed doBoolSequence() skipping some iterable elements Signed-off-by: Octave Larose --- .../oracle/graal/python/builtins/modules/BuiltinFunctions.java | 1 - 1 file changed, 1 deletion(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java index fe4e68f64a..9de90f5ede 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java @@ -326,7 +326,6 @@ boolean doBoolSequence(VirtualFrame frame, } else if (nodeType == NodeType.ANY && isTrueNode.execute(frame, internalArray[i])) { return true; } - i++; } return nodeType == NodeType.ALL;