Skip to content

Commit 52702e5

Browse files
authored
[mypyc] Merge fast_isinstance_op and py_calc_meta_op (#9662)
relates mypyc/mypyc#753 This PR merges two remaining misc ops.
1 parent 3ed4747 commit 52702e5

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

mypyc/irbuild/classdef.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def find_non_ext_metaclass(builder: IRBuilder, cdef: ClassDef, bases: Value) ->
229229
declared_metaclass = builder.add(LoadAddress(type_object_op.type,
230230
type_object_op.src, cdef.line))
231231

232-
return builder.primitive_op(py_calc_meta_op, [declared_metaclass, bases], cdef.line)
232+
return builder.call_c(py_calc_meta_op, [declared_metaclass, bases], cdef.line)
233233

234234

235235
def setup_non_ext_dict(builder: IRBuilder,

mypyc/irbuild/ll_builder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ def isinstance_native(self, obj: Value, class_ir: ClassIR, line: int) -> Value:
221221
"""
222222
concrete = all_concrete_classes(class_ir)
223223
if concrete is None or len(concrete) > FAST_ISINSTANCE_MAX_SUBCLASSES + 1:
224-
return self.primitive_op(fast_isinstance_op,
225-
[obj, self.get_native_type(class_ir)],
226-
line)
224+
return self.call_c(fast_isinstance_op,
225+
[obj, self.get_native_type(class_ir)],
226+
line)
227227
if not concrete:
228228
# There can't be any concrete instance that matches this.
229229
return self.false()

mypyc/lib-rt/CPy.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,16 @@ static inline bool CPyFloat_Check(PyObject *o) {
456456
return PyFloat_Check(o) || PyLong_Check(o);
457457
}
458458

459+
// TODO: find an unified way to avoid inline functions in non-C back ends that can not
460+
// use inline functions
461+
static inline bool CPy_TypeCheck(PyObject *o, PyObject *type) {
462+
return PyObject_TypeCheck(o, (PyTypeObject *)type);
463+
}
464+
465+
static inline PyObject *CPy_CalculateMetaclass(PyObject *type, PyObject *o) {
466+
return (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)type, o);
467+
}
468+
459469
PyObject *CPy_GetCoro(PyObject *obj);
460470
PyObject *CPyIter_Send(PyObject *iter, PyObject *val);
461471
int CPy_YieldFromErrorHandle(PyObject *iter, PyObject **outp);

mypyc/primitives/misc_ops.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
int_rprimitive, dict_rprimitive, c_int_rprimitive, bit_rprimitive
77
)
88
from mypyc.primitives.registry import (
9-
simple_emit, func_op, custom_op, c_function_op, c_custom_op, load_address_op, ERR_NEG_INT
9+
simple_emit, custom_op, c_function_op, c_custom_op, load_address_op, ERR_NEG_INT
1010
)
1111

1212

@@ -89,13 +89,11 @@
8989

9090
# Determine the most derived metaclass and check for metaclass conflicts.
9191
# Arguments are (metaclass, bases).
92-
py_calc_meta_op = custom_op(
92+
py_calc_meta_op = c_custom_op(
9393
arg_types=[object_rprimitive, object_rprimitive],
94-
result_type=object_rprimitive,
94+
return_type=object_rprimitive,
95+
c_function_name='CPy_CalculateMetaclass',
9596
error_kind=ERR_MAGIC,
96-
format_str='{dest} = py_calc_metaclass({comma_args})',
97-
emit=simple_emit(
98-
'{dest} = (PyObject*) _PyType_CalculateMetaclass((PyTypeObject *){args[0]}, {args[1]});'),
9997
is_borrowed=True
10098
)
10199

@@ -126,12 +124,12 @@
126124

127125
# Faster isinstance(obj, cls) that only works with native classes and doesn't perform
128126
# type checking of the type argument.
129-
fast_isinstance_op = func_op(
127+
fast_isinstance_op = c_function_op(
130128
'builtins.isinstance',
131129
arg_types=[object_rprimitive, object_rprimitive],
132-
result_type=bool_rprimitive,
130+
return_type=bool_rprimitive,
131+
c_function_name='CPy_TypeCheck',
133132
error_kind=ERR_NEVER,
134-
emit=simple_emit('{dest} = PyObject_TypeCheck({args[0]}, (PyTypeObject *){args[1]});'),
135133
priority=0)
136134

137135
# bool(obj) with unboxed result

mypyc/test-data/irbuild-classes.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ def f(x):
663663
r3 :: __main__.B
664664
L0:
665665
r0 = __main__.R :: type
666-
r1 = isinstance x, r0
666+
r1 = CPy_TypeCheck(x, r0)
667667
if r1 goto L1 else goto L2 :: bool
668668
L1:
669669
r2 = cast(__main__.R, x)

0 commit comments

Comments
 (0)