Skip to content

Commit a109825

Browse files
committed
CPython compatible slots infrastructure
* Implemented slots: nb_bool, sq_length, and mp_length, tp_getattr(o), tp_descr_get, tp_descr_set * Skeletons: tp_richcompare, tp_init * CPython compatible slots inheritance (fixup_slot_dispatchers, add_operators) * Skeleton of native HPy slots, but for now HPy slots pretend to be magic methods that are added to the type after it is created * Simple slots 'fuzzer' in scripts directory that compares CPython/GraalPy output * Check slots code conventions and consistency of PBCT slots with the @corefunctions * We want the PBCT slots to be initialized in the static ctor to simply bake them into the native image even without pre-initialized context. This also shifts the responsibility of synchronization to the VM. * Handles incompatible slot assignment (myType.tp_descrget = otherType.tp_getattro) * make TpSlotWrapper subclasses more lenient when checking arguments count
1 parent 5914356 commit a109825

File tree

152 files changed

+8062
-1694
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+8062
-1694
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.annotations;
42+
43+
import java.lang.annotation.ElementType;
44+
import java.lang.annotation.Repeatable;
45+
import java.lang.annotation.Retention;
46+
import java.lang.annotation.RetentionPolicy;
47+
import java.lang.annotation.Target;
48+
49+
@Retention(RetentionPolicy.RUNTIME)
50+
@Target(ElementType.TYPE)
51+
@Repeatable(Slot.Slots.class)
52+
public @interface Slot {
53+
SlotKind value();
54+
55+
/**
56+
* The slot node either needs frame for execution (e.g., may call Python code) and/or is complex
57+
* enough that indirect call to partially evaluated code is preferred over uncached execution
58+
* without frame and without setting up indirect call context.
59+
*
60+
* The slot call nodes AST inline slot nodes, but if the inline cache overflows or in the
61+
* uncached case, they need to either call uncached slot node or do indirect call if
62+
* {@code isComplex} is {@code true}.
63+
*/
64+
boolean isComplex() default false;
65+
66+
@Retention(RetentionPolicy.RUNTIME)
67+
@Target(ElementType.TYPE)
68+
@interface Slots {
69+
Slot[] value();
70+
}
71+
72+
/**
73+
* Some slots do not have fixed signature. This annotation must be used for such slots, the
74+
* semantics of its fields is the same as of the field with the same name in the
75+
* {@code @Builtin} annotation.
76+
*/
77+
@Retention(RetentionPolicy.RUNTIME)
78+
@interface SlotSignature {
79+
int minNumOfPositionalArgs() default 0;
80+
81+
boolean takesVarArgs() default false;
82+
83+
boolean takesVarKeywordArgs() default false;
84+
85+
String[] parameterNames() default {};
86+
87+
boolean needsFrame() default false;
88+
89+
boolean alwaysNeedsCallerFrame() default false;
90+
91+
String raiseErrorName() default "";
92+
}
93+
94+
enum SlotKind {
95+
nb_bool,
96+
sq_length,
97+
mp_length,
98+
tp_descr_get,
99+
tp_get_attro,
100+
tp_descr_set
101+
}
102+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
# Copyright (c) 2017, 2021, Oracle and/or its affiliates.
40+
# Copyright (c) 2013, Regents of the University of California
41+
#
42+
# All rights reserved.
43+
#
44+
# Redistribution and use in source and binary forms, with or without modification, are
45+
# permitted provided that the following conditions are met:
46+
#
47+
# 1. Redistributions of source code must retain the above copyright notice, this list of
48+
# conditions and the following disclaimer.
49+
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of
50+
# conditions and the following disclaimer in the documentation and/or other materials provided
51+
# with the distribution.
52+
#
53+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
54+
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
55+
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
56+
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
57+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
58+
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
59+
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
60+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61+
# OF THE POSSIBILITY OF SUCH DAMAGE.
62+
# micro benchmark: simple for range loop
63+
64+
# This benchmark hits the generic path in PyObjectIsTrueNode, but it is monomorphic in what gets called there
65+
# On top of that this exercises the fast-paths for attribute lookup, which takes place in Boolish.__bool__
66+
67+
def measure(num, objs, l):
68+
r = 0
69+
for i in range(num): # 50000
70+
for j in range(l):
71+
if objs[j]:
72+
r += 1
73+
74+
return r
75+
76+
77+
class Boolish:
78+
def __init__(self, b):
79+
self.b = b
80+
def __bool__(self):
81+
return self.b
82+
83+
84+
data = [Boolish(False), Boolish(True)] * 20
85+
86+
def __benchmark__(num=50000):
87+
measure(num, data, len(data))
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
# This benchmark hits the generic path in PyObjectIsTrueNode, but it is monomorphic in what gets called there
41+
42+
def measure(num, objs, l):
43+
r = 0
44+
for i in range(num): # 50000
45+
for j in range(l):
46+
if objs[j]:
47+
r += 1
48+
49+
return r
50+
51+
52+
data = [frozenset(), frozenset([1,2,3])] * 20
53+
54+
def __benchmark__(num=50000):
55+
measure(num, data, len(data))
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
# This benchmark hits the generic path in PyObjectIsTrueNode with many different types of objects
41+
42+
def measure(num, objs):
43+
l = len(objs)
44+
r = 0
45+
for i in range(num): # 50000
46+
for j in range(l):
47+
if objs[j]:
48+
r += 1
49+
50+
return r
51+
52+
53+
class Dummy:
54+
pass
55+
56+
class Boolish:
57+
def __bool__(self):
58+
return False
59+
60+
data = [frozenset(), frozenset([1,2,3]), Dummy(), 1234567891234567899199, measure, Boolish()] * 10
61+
62+
def __benchmark__(num=50000):
63+
measure(num, data)

graalpython/com.oracle.graal.python.cext/src/typeobject.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9260,6 +9260,9 @@ static void add_slot(PyTypeObject* type, char* name, void* meth, int flags, int
92609260
static int type_ready_graalpy_slot_conv(PyTypeObject* cls) {
92619261
#define ADD_SLOT_CONV(__name__, __meth__, __flags__, __signature__) add_slot(cls, (__name__), (__meth__), (__flags__), (__signature__), NULL)
92629262

9263+
// TODO: once all slots are converted, we can do one upcall to managed implementation
9264+
// of add_operators that will use TpSlots#SLOTDEFS and the same algorithm as CPython
9265+
92639266
/*
92649267
* NOTE: ADD_SLOT_CONV won't overwrite existing attributes, so the order is crucial and must
92659268
* reflect CPython's 'slotdefs' array.
@@ -9273,13 +9276,13 @@ static int type_ready_graalpy_slot_conv(PyTypeObject* cls) {
92739276

92749277
// NOTE: The slots may be called from managed code, i.e., we need to wrap the functions
92759278
// and convert arguments that should be C primitives.
9276-
ADD_SLOT_CONV("__getattribute__", cls->tp_getattr, -2, JWRAPPER_GETATTR);
9279+
// ADD_SLOT_CONV("__getattribute__", cls->tp_getattr, -2, JWRAPPER_GETATTR); tp_getattr does not have wrapper set in slotdefs and hence is ignored in add_operators
92779280
ADD_SLOT_CONV("__setattr__", cls->tp_setattr, -3, JWRAPPER_SETATTR);
92789281
ADD_SLOT_CONV("__repr__", cls->tp_repr, -1, JWRAPPER_REPR);
92799282
ADD_SLOT_CONV("__hash__", cls->tp_hash, -1, JWRAPPER_HASHFUNC);
92809283
ADD_SLOT_CONV("__call__", cls->tp_call, METH_KEYWORDS | METH_VARARGS, JWRAPPER_CALL);
92819284
ADD_SLOT_CONV("__str__", cls->tp_str, -1, JWRAPPER_STR);
9282-
ADD_SLOT_CONV("__getattribute__", cls->tp_getattro, -2, JWRAPPER_DIRECT);
9285+
ADD_SLOT_CONV("__getattribute__", cls->tp_getattro, -2, JWRAPPER_BINARYFUNC);
92839286
ADD_SLOT_CONV("__setattr__", cls->tp_setattro, -3, JWRAPPER_SETATTRO);
92849287
ADD_SLOT_CONV("__delattr__", cls->tp_setattro, -3, JWRAPPER_DELATTRO);
92859288
ADD_SLOT_CONV("__clear__", cls->tp_clear, -1, JWRAPPER_INQUIRY);
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
com.oracle.graal.python.processor.ArgumentClinicProcessor
22
com.oracle.graal.python.processor.GenerateEnumConstantsProcessor
33
com.oracle.graal.python.processor.CApiBuiltinsProcessor
4+
com.oracle.graal.python.processor.SlotsProcessor

graalpython/com.oracle.graal.python.processor/src/com/oracle/graal/python/processor/CodeWriter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -42,7 +42,8 @@
4242

4343
import java.io.IOException;
4444
import java.io.Writer;
45-
import java.util.Arrays;
45+
import java.util.function.Function;
46+
import java.util.stream.Stream;
4647

4748
public class CodeWriter implements AutoCloseable {
4849
private final Writer writer;
@@ -84,8 +85,8 @@ public LineBuilder write(String fmt, Object... args) throws IOException {
8485
return this;
8586
}
8687

87-
public LineBuilder writeEach(int[] items, String delimiter, String fmt) throws IOException {
88-
String[] strs = Arrays.stream(items).mapToObj(x -> String.format(fmt, x)).toArray(String[]::new);
88+
public <T> LineBuilder writeEach(Stream<T> items, String delimiter, Function<T, String> fun) throws IOException {
89+
String[] strs = items.map(fun).toArray(String[]::new);
8990
return write(String.join(delimiter, strs));
9091
}
9192

0 commit comments

Comments
 (0)