Skip to content

Commit b922e90

Browse files
committed
intrinsified PyTuple_Size/GetSlice
1 parent 96fa6f2 commit b922e90

File tree

2 files changed

+123
-23
lines changed

2 files changed

+123
-23
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextTupleBuiltins.java

Lines changed: 123 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,44 @@
4141
package com.oracle.graal.python.builtins.modules.cext;
4242

4343
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.IndexError;
44-
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.SystemError;
4544
import java.util.List;
4645
import com.oracle.graal.python.builtins.Builtin;
4746
import com.oracle.graal.python.builtins.CoreFunctions;
4847
import com.oracle.graal.python.builtins.Python3Core;
48+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
49+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.SystemError;
4950
import com.oracle.graal.python.builtins.PythonBuiltins;
51+
import com.oracle.graal.python.builtins.modules.BuiltinConstructors.StrNode;
52+
import com.oracle.graal.python.builtins.objects.PNone;
5053
import com.oracle.graal.python.builtins.objects.cext.capi.CApiGuards;
5154
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.AsPythonObjectNode;
5255
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.AsPythonObjectStealingNode;
56+
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.GetNativeNullNode;
5357
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.PCallCapiFunction;
58+
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.PRaiseNativeNode;
5459
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.TransformExceptionToNativeNode;
5560
import com.oracle.graal.python.builtins.objects.cext.capi.NativeCAPISymbol;
5661
import com.oracle.graal.python.builtins.objects.cext.capi.PythonNativeWrapper;
5762
import com.oracle.graal.python.builtins.objects.common.IndexNodes.NormalizeIndexNode;
5863
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
64+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.LenNode;
65+
import com.oracle.graal.python.builtins.objects.ints.PInt;
5966
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
67+
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins.GetItemNode;
6068
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
6169
import com.oracle.graal.python.nodes.ErrorMessages;
70+
import static com.oracle.graal.python.nodes.ErrorMessages.BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P;
71+
import static com.oracle.graal.python.nodes.ErrorMessages.NATIVE_S_SUBTYPES_NOT_IMPLEMENTED;
6272
import com.oracle.graal.python.nodes.PGuards;
6373
import com.oracle.graal.python.nodes.PRaiseNode;
74+
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
6475
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6576
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
6677
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
6778
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
79+
import com.oracle.graal.python.nodes.object.GetClassNode;
80+
import com.oracle.graal.python.nodes.subscript.SliceLiteralNode;
81+
import com.oracle.graal.python.nodes.truffle.PythonTypes;
6882
import com.oracle.graal.python.runtime.exception.PException;
6983
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
7084
import com.oracle.truffle.api.dsl.Cached;
@@ -73,17 +87,17 @@
7387
import com.oracle.truffle.api.dsl.ImportStatic;
7488
import com.oracle.truffle.api.dsl.NodeFactory;
7589
import com.oracle.truffle.api.dsl.Specialization;
90+
import com.oracle.truffle.api.dsl.TypeSystemReference;
7691
import com.oracle.truffle.api.frame.VirtualFrame;
92+
import com.oracle.truffle.api.profiles.BranchProfile;
7793

78-
@CoreFunctions(defineModule = PythonCextTupleBuiltins.PYTHON_CEXT_TUPLE)
94+
@CoreFunctions(extendsModule = PythonCextBuiltins.PYTHON_CEXT)
7995
@GenerateNodeFactory
8096
public class PythonCextTupleBuiltins extends PythonBuiltins {
8197

82-
public static final String PYTHON_CEXT_TUPLE = "python_cext_tuple";
83-
8498
@Override
8599
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
86-
return PythonCextBuiltinsFactory.getFactories();
100+
return PythonCextTupleBuiltinsFactory.getFactories();
87101
}
88102

89103
@Override
@@ -93,7 +107,7 @@ public void initialize(Python3Core core) {
93107

94108
@Builtin(name = "PyTuple_New", minNumOfPositionalArgs = 1)
95109
@GenerateNodeFactory
96-
abstract static class PyTuple_New extends PythonUnaryBuiltinNode {
110+
abstract static class PyTupleNew extends PythonUnaryBuiltinNode {
97111
@Specialization
98112
PTuple doGeneric(VirtualFrame frame, Object size,
99113
@Cached PyNumberAsSizeNode asSizeNode) {
@@ -104,7 +118,7 @@ PTuple doGeneric(VirtualFrame frame, Object size,
104118
@Builtin(name = "PyTuple_SetItem", minNumOfPositionalArgs = 3)
105119
@GenerateNodeFactory
106120
@ImportStatic(CApiGuards.class)
107-
abstract static class PyTuple_SetItem extends PythonTernaryBuiltinNode {
121+
abstract static class PyTupleSetItem extends PythonTernaryBuiltinNode {
108122
@Specialization
109123
static int doManaged(VirtualFrame frame, PythonNativeWrapper selfWrapper, Object position, Object elementWrapper,
110124
@Cached AsPythonObjectNode selfAsPythonObjectNode,
@@ -143,7 +157,7 @@ protected static SequenceStorageNodes.SetItemNode createSetItem() {
143157

144158
@Builtin(name = "PyTuple_GetItem", minNumOfPositionalArgs = 2)
145159
@GenerateNodeFactory
146-
abstract static class PyTuple_GetItem extends PythonBinaryBuiltinNode {
160+
abstract static class PyTupleGetItem extends PythonBinaryBuiltinNode {
147161

148162
@Specialization
149163
Object doPTuple(VirtualFrame frame, PTuple tuple, long key,
@@ -163,4 +177,105 @@ Object doPTuple(Object tuple, @SuppressWarnings("unused") Object key) {
163177
throw raise(SystemError, ErrorMessages.BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P, tuple, tuple);
164178
}
165179
}
180+
181+
@Builtin(name = "PyTuple_Size", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
182+
@GenerateNodeFactory
183+
public abstract static class PyTupleSizeNode extends PythonUnaryBuiltinNode {
184+
@Specialization
185+
public int size(PTuple tuple,
186+
@Cached LenNode lenNode,
187+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
188+
try {
189+
return lenNode.execute(tuple.getSequenceStorage());
190+
} catch (PException e) {
191+
transformExceptionToNativeNode.execute(e);
192+
return -1;
193+
}
194+
}
195+
196+
@Specialization(guards = {"!isPTuple(obj)", "isTupleSubtype(frame, obj, getClassNode, isSubtypeNode)"})
197+
public Object sizeNative(VirtualFrame frame, @SuppressWarnings("unused") Object obj,
198+
@SuppressWarnings("unused") @Cached GetClassNode getClassNode,
199+
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode,
200+
@Cached PRaiseNativeNode raiseNativeNode) {
201+
return raiseNativeNode.raiseInt(frame, -1, PythonBuiltinClassType.NotImplementedError, NATIVE_S_SUBTYPES_NOT_IMPLEMENTED, "tuple");
202+
}
203+
204+
@SuppressWarnings("unused")
205+
@Specialization(guards = {"!isPTuple(obj)", "!isTupleSubtype(frame, obj, getClassNode, isSubtypeNode)"})
206+
public Object size(VirtualFrame frame, Object obj,
207+
@SuppressWarnings("unused") @Cached GetClassNode getClassNode,
208+
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode,
209+
@Cached StrNode strNode,
210+
@Cached PRaiseNativeNode raiseNativeNode) {
211+
return raiseNativeNode.raiseInt(frame, -1, SystemError, BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P, strNode.executeWith(frame, obj), obj);
212+
}
213+
214+
protected boolean isTupleSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) {
215+
return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PTuple);
216+
}
217+
}
218+
219+
@Builtin(name = "PyTuple_GetSlice", minNumOfPositionalArgs = 3)
220+
@TypeSystemReference(PythonTypes.class)
221+
@GenerateNodeFactory
222+
abstract static class PyTupleGetSliceNode extends PythonTernaryBuiltinNode {
223+
224+
@Specialization
225+
Object getSlice(VirtualFrame frame, PTuple tuple, long iLow, long iHigh,
226+
@Cached GetItemNode getItemNode,
227+
@Cached SliceLiteralNode sliceNode,
228+
@Cached BranchProfile isIntRangeProfile,
229+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
230+
@Cached GetNativeNullNode getNativeNullNode) {
231+
try {
232+
if (PInt.isIntRange(iLow) && PInt.isIntRange(iHigh)) {
233+
isIntRangeProfile.enter();
234+
return getItemNode.execute(frame, tuple, sliceNode.execute(frame, (int) iLow, (int) iHigh, PNone.NONE));
235+
}
236+
return getItemNode.execute(frame, tuple, sliceNode.execute(frame, iLow, iHigh, PNone.NONE));
237+
} catch (PException e) {
238+
transformExceptionToNativeNode.execute(e);
239+
return getNativeNullNode.execute();
240+
}
241+
}
242+
243+
@Specialization
244+
Object getSlice(VirtualFrame frame, PTuple tuple, Object iLow, Object iHigh,
245+
@Cached GetItemNode getItemNode,
246+
@Cached SliceLiteralNode sliceNode,
247+
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
248+
@Cached GetNativeNullNode getNativeNullNode) {
249+
try {
250+
return getItemNode.execute(frame, tuple, sliceNode.execute(frame, iLow, iHigh, PNone.NONE));
251+
} catch (PException e) {
252+
transformExceptionToNativeNode.execute(e);
253+
return getNativeNullNode.execute();
254+
}
255+
}
256+
257+
@Specialization(guards = {"!isPTuple(obj)", "isTupleSubtype(frame, obj, getClassNode, isSubtypeNode)"})
258+
public Object getSliceNative(VirtualFrame frame, @SuppressWarnings("unused") Object obj, @SuppressWarnings("unused") Object iLow, @SuppressWarnings("unused") Object iHigh,
259+
@SuppressWarnings("unused") @Cached GetClassNode getClassNode,
260+
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode,
261+
@Cached PRaiseNativeNode raiseNativeNode,
262+
@Cached GetNativeNullNode getNativeNullNode) {
263+
return raiseNativeNode.raise(frame, getNativeNullNode.execute(), PythonBuiltinClassType.NotImplementedError, NATIVE_S_SUBTYPES_NOT_IMPLEMENTED, "list");
264+
}
265+
266+
@Specialization(guards = {"!isPTuple(obj)", "!isTupleSubtype(frame, obj, getClassNode, isSubtypeNode)"})
267+
public Object getSlice(VirtualFrame frame, Object obj, @SuppressWarnings("unused") Object iLow, @SuppressWarnings("unused") Object iHigh,
268+
@SuppressWarnings("unused") @Cached GetClassNode getClassNode,
269+
@SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode,
270+
@Cached StrNode strNode,
271+
@Cached PRaiseNativeNode raiseNativeNode,
272+
@Cached GetNativeNullNode getNativeNullNode) {
273+
return raiseNativeNode.raise(frame, getNativeNullNode.execute(), SystemError, BAD_ARG_TO_INTERNAL_FUNC_WAS_S_P, strNode.executeWith(frame, obj), obj);
274+
}
275+
276+
protected boolean isTupleSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) {
277+
return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PTuple);
278+
}
279+
}
280+
166281
}

graalpython/lib-graalpython/python_cext.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,6 @@ def PyObject_Str(o):
116116
def PyObject_Repr(o):
117117
return repr(o)
118118

119-
120-
@may_raise(-1)
121-
def PyTuple_Size(t):
122-
if not isinstance(t, tuple):
123-
__bad_internal_call(None, None, t)
124-
return len(t)
125-
126-
127-
@may_raise
128-
def PyTuple_GetSlice(t, start, end):
129-
if not isinstance(t, tuple):
130-
__bad_internal_call(None, None, t)
131-
return t[start:end]
132-
133-
134119
@may_raise
135120
def dict_from_list(lst):
136121
if len(lst) % 2 != 0:

0 commit comments

Comments
 (0)