Skip to content

Commit b34f4c6

Browse files
authored
[mypyc] Merge most name_ref_op with LoadAddress (python#9293)
This PR replaces name_ref_op: int, str, list, type, NotImplemented with LoadAddress. The only three remaining name_ref_ops are true, false and none, which should be built directly in irbuild
1 parent d94853d commit b34f4c6

File tree

10 files changed

+52
-74
lines changed

10 files changed

+52
-74
lines changed

mypyc/irbuild/classdef.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
)
99
from mypyc.ir.ops import (
1010
Value, Call, LoadErrorValue, LoadStatic, InitStatic, TupleSet, SetAttr, Return,
11-
BasicBlock, Branch, MethodCall, NAMESPACE_TYPE
11+
BasicBlock, Branch, MethodCall, NAMESPACE_TYPE, LoadAddress
1212
)
1313
from mypyc.ir.rtypes import (
1414
RInstance, object_rprimitive, bool_rprimitive, dict_rprimitive, is_optional_type,
@@ -227,7 +227,8 @@ def find_non_ext_metaclass(builder: IRBuilder, cdef: ClassDef, bases: Value) ->
227227
if cdef.metaclass:
228228
declared_metaclass = builder.accept(cdef.metaclass)
229229
else:
230-
declared_metaclass = builder.primitive_op(type_object_op, [], cdef.line)
230+
declared_metaclass = builder.add(LoadAddress(type_object_op.type,
231+
type_object_op.src, cdef.line))
231232

232233
return builder.primitive_op(py_calc_meta_op, [declared_metaclass, bases], cdef.line)
233234

@@ -279,7 +280,7 @@ def add_non_ext_class_attr(builder: IRBuilder,
279280
# which attributes to compute on.
280281
# TODO: Maybe generate more precise types for annotations
281282
key = builder.load_static_unicode(lvalue.name)
282-
typ = builder.primitive_op(type_object_op, [], stmt.line)
283+
typ = builder.add(LoadAddress(type_object_op.type, type_object_op.src, stmt.line))
283284
builder.call_c(dict_set_item_op, [non_ext.anns, key, typ], stmt.line)
284285

285286
# Only add the attribute to the __dict__ if the assignment is of the form:
@@ -393,7 +394,8 @@ def gen_glue_ne_method(builder: IRBuilder, cls: ClassIR, line: int) -> FuncIR:
393394
# If __eq__ returns NotImplemented, then __ne__ should also
394395
not_implemented_block, regular_block = BasicBlock(), BasicBlock()
395396
eqval = builder.add(MethodCall(args[0], '__eq__', [args[1]], line))
396-
not_implemented = builder.primitive_op(not_implemented_op, [], line)
397+
not_implemented = builder.add(LoadAddress(not_implemented_op.type,
398+
not_implemented_op.src, line))
397399
builder.add(Branch(
398400
builder.binary_op(eqval, not_implemented, 'is', line),
399401
not_implemented_block,
@@ -521,7 +523,7 @@ def dataclass_non_ext_info(builder: IRBuilder, cdef: ClassDef) -> Optional[NonEx
521523
builder.call_c(dict_new_op, [], cdef.line),
522524
builder.add(TupleSet([], cdef.line)),
523525
builder.call_c(dict_new_op, [], cdef.line),
524-
builder.primitive_op(type_object_op, [], cdef.line),
526+
builder.add(LoadAddress(type_object_op.type, type_object_op.src, cdef.line))
525527
)
526528
else:
527529
return None

mypyc/primitives/int_ops.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,18 @@
1313
str_rprimitive, RType
1414
)
1515
from mypyc.primitives.registry import (
16-
name_ref_op, name_emit,
17-
c_unary_op, CFunctionDescription, c_function_op, c_binary_op, c_custom_op
16+
load_address_op, c_unary_op, CFunctionDescription, c_function_op, c_binary_op, c_custom_op
1817
)
1918

2019
# These int constructors produce object_rprimitives that then need to be unboxed
2120
# I guess unboxing ourselves would save a check and branch though?
2221

2322
# Get the type object for 'builtins.int'.
24-
# For ordinary calls to int() we use a name_ref to the type
25-
name_ref_op('builtins.int',
26-
result_type=object_rprimitive,
27-
error_kind=ERR_NEVER,
28-
emit=name_emit('&PyLong_Type', target_type='PyObject *'),
29-
is_borrowed=True)
23+
# For ordinary calls to int() we use a load_address to the type
24+
load_address_op(
25+
name='builtins.int',
26+
type=object_rprimitive,
27+
src='PyLong_Type')
3028

3129
# Convert from a float to int. We could do a bit better directly.
3230
c_function_op(

mypyc/primitives/list_ops.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88
c_int_rprimitive
99
)
1010
from mypyc.primitives.registry import (
11-
name_ref_op, custom_op, name_emit,
12-
call_emit, c_function_op, c_binary_op, c_method_op
11+
custom_op, load_address_op, call_emit, c_function_op, c_binary_op, c_method_op
1312
)
1413

1514

1615
# Get the 'builtins.list' type object.
17-
name_ref_op('builtins.list',
18-
result_type=object_rprimitive,
19-
error_kind=ERR_NEVER,
20-
emit=name_emit('&PyList_Type', target_type='PyObject *'),
21-
is_borrowed=True)
16+
load_address_op(
17+
name='builtins.list',
18+
type=object_rprimitive,
19+
src='PyList_Type')
2220

2321
# list(obj)
2422
to_list = c_function_op(

mypyc/primitives/misc_ops.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
)
88
from mypyc.primitives.registry import (
99
name_ref_op, simple_emit, unary_op, func_op, custom_op, call_emit, name_emit,
10-
call_negative_magic_emit, c_function_op, c_custom_op
10+
call_negative_magic_emit, c_function_op, c_custom_op, load_address_op
1111
)
1212

1313

@@ -46,11 +46,10 @@
4646
is_borrowed=True)
4747

4848
# Get the boxed NotImplemented object
49-
not_implemented_op = name_ref_op(name='builtins.NotImplemented',
50-
result_type=object_rprimitive,
51-
error_kind=ERR_NEVER,
52-
emit=name_emit('Py_NotImplemented'),
53-
is_borrowed=True)
49+
not_implemented_op = load_address_op(
50+
name='builtins.NotImplemented',
51+
type=object_rprimitive,
52+
src='_Py_NotImplementedStruct')
5453

5554
# id(obj)
5655
c_function_op(
@@ -201,12 +200,10 @@
201200
error_kind=ERR_NEVER)
202201

203202
# Get 'builtins.type' (base class of all classes)
204-
type_object_op = name_ref_op(
205-
'builtins.type',
206-
result_type=object_rprimitive,
207-
error_kind=ERR_NEVER,
208-
emit=name_emit('&PyType_Type', target_type='PyObject *'),
209-
is_borrowed=True)
203+
type_object_op = load_address_op(
204+
name='builtins.type',
205+
type=object_rprimitive,
206+
src='PyType_Type')
210207

211208
# Create a heap type based on a template non-heap type.
212209
# See CPyType_FromTemplate for more docs.

mypyc/primitives/registry.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,10 @@
5656
('priority', int)])
5757

5858
# A description for C load operations including LoadGlobal and LoadAddress
59-
CLoadDescription = NamedTuple(
60-
'CLoadDescription', [('name', str),
61-
('return_type', RType),
62-
('identifier', str), # name of the target to load
63-
('cast_str', str), # string represents optional type cast
64-
('load_address', bool)]) # True for LoadAddress otherwise LoadGlobal
59+
LoadAddressDescription = NamedTuple(
60+
'LoadAddressDescription', [('name', str),
61+
('type', RType),
62+
('src', str)]) # name of the target to load
6563

6664
# Primitive binary ops (key is operator such as '+')
6765
binary_ops = {} # type: Dict[str, List[OpDescription]]
@@ -90,9 +88,6 @@
9088
# CallC op for unary ops
9189
c_unary_ops = {} # type: Dict[str, List[CFunctionDescription]]
9290

93-
# LoadGlobal/LoadAddress op for reading global names
94-
c_name_ref_ops = {} # type: Dict[str, CLoadDescription]
95-
9691
builtin_names = {} # type: Dict[str, Tuple[RType, str]]
9792

9893

@@ -489,23 +484,12 @@ def c_unary_op(name: str,
489484
return desc
490485

491486

492-
def c_name_ref_op(name: str,
493-
return_type: RType,
494-
identifier: str,
495-
cast_str: Optional[str] = None,
496-
load_address: bool = False) -> CLoadDescription:
497-
assert name not in c_name_ref_ops, 'already defined: %s' % name
498-
cast_str = cast_str if cast_str else ""
499-
desc = CLoadDescription(name, return_type, identifier, cast_str, load_address)
500-
c_name_ref_ops[name] = desc
501-
return desc
502-
503-
504487
def load_address_op(name: str,
505488
type: RType,
506-
src: str) -> None:
489+
src: str) -> LoadAddressDescription:
507490
assert name not in builtin_names, 'already defined: %s' % name
508491
builtin_names[name] = (type, src)
492+
return LoadAddressDescription(name, type, src)
509493

510494

511495
# Import various modules that set up global state.

mypyc/primitives/str_ops.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22

33
from typing import List, Callable
44

5-
from mypyc.ir.ops import ERR_MAGIC, ERR_NEVER, EmitterInterface, EmitCallback
5+
from mypyc.ir.ops import ERR_MAGIC, EmitterInterface, EmitCallback
66
from mypyc.ir.rtypes import (
77
RType, object_rprimitive, str_rprimitive, bool_rprimitive, int_rprimitive, list_rprimitive
88
)
99
from mypyc.primitives.registry import (
10-
binary_op, simple_emit, name_ref_op, method_op, call_emit, name_emit,
11-
c_method_op, c_binary_op, c_function_op
10+
binary_op, simple_emit, method_op, call_emit, c_method_op, c_binary_op, c_function_op,
11+
load_address_op
1212
)
1313

1414

1515
# Get the 'str' type object.
16-
name_ref_op('builtins.str',
17-
result_type=object_rprimitive,
18-
error_kind=ERR_NEVER,
19-
emit=name_emit('&PyUnicode_Type', target_type='PyObject *'),
20-
is_borrowed=True)
16+
load_address_op(
17+
name='builtins.str',
18+
type=object_rprimitive,
19+
src='PyUnicode_Type')
2120

2221
# str(obj)
2322
c_function_op(

mypyc/test-data/irbuild-basic.test

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ def call_python_function(x):
12431243
r0, r1, r2 :: object
12441244
r3 :: int
12451245
L0:
1246-
r0 = int
1246+
r0 = load_address PyLong_Type
12471247
r1 = box(int, x)
12481248
r2 = py_call(r0, r1)
12491249
r3 = unbox(int, r2)
@@ -1294,7 +1294,7 @@ def call_python_function_with_keyword_arg(x):
12941294
r5 :: object
12951295
r6 :: int
12961296
L0:
1297-
r0 = int
1297+
r0 = load_address PyLong_Type
12981298
r1 = unicode_3 :: static ('base')
12991299
r2 = (x) :: tuple
13001300
r3 = box(short_int, 4)
@@ -1696,7 +1696,7 @@ def foo(x):
16961696
r3 :: __main__.B
16971697
r4 :: __main__.A
16981698
L0:
1699-
r0 = int
1699+
r0 = load_address PyLong_Type
17001700
r1 = PyObject_IsInstance(x, r0)
17011701
r2 = truncate r1: int32 to builtins.bool
17021702
if r2 goto L1 else goto L2 :: bool
@@ -2688,11 +2688,11 @@ L4:
26882688
r23 = CPyDict_SetItem(r11, r22, r21)
26892689
r24 = unicode_5 :: static ('Lol')
26902690
r25 = unicode_6 :: static ('a')
2691-
r26 = int
2691+
r26 = load_address PyLong_Type
26922692
r27 = (r25, r26)
26932693
r28 = box(tuple[str, object], r27)
26942694
r29 = unicode_7 :: static ('b')
2695-
r30 = str
2695+
r30 = load_address PyUnicode_Type
26962696
r31 = (r29, r30)
26972697
r32 = box(tuple[str, object], r31)
26982698
r33 = (r28, r32)
@@ -2717,7 +2717,7 @@ L4:
27172717
r52 = __main__.globals :: static
27182718
r53 = unicode_2 :: static ('List')
27192719
r54 = CPyDict_GetItem(r52, r53)
2720-
r55 = int
2720+
r55 = load_address PyLong_Type
27212721
r56 = PyObject_GetItem(r54, r55)
27222722
r57 = __main__.globals :: static
27232723
r58 = unicode_10 :: static ('Foo')
@@ -2820,7 +2820,7 @@ def A.__eq__(self, x):
28202820
self :: __main__.A
28212821
x, r0 :: object
28222822
L0:
2823-
r0 = NotImplemented
2823+
r0 = load_address _Py_NotImplementedStruct
28242824
return r0
28252825
def A.__ne__(self, rhs):
28262826
self :: __main__.A
@@ -2829,7 +2829,7 @@ def A.__ne__(self, rhs):
28292829
r4 :: object
28302830
L0:
28312831
r0 = self.__eq__(rhs)
2832-
r1 = NotImplemented
2832+
r1 = load_address _Py_NotImplementedStruct
28332833
r2 = r0 is r1
28342834
if r2 goto L2 else goto L1 :: bool
28352835
L1:

mypyc/test-data/irbuild-classes.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ def Base.__ne__(self, rhs):
832832
r4 :: object
833833
L0:
834834
r0 = self.__eq__(rhs)
835-
r1 = NotImplemented
835+
r1 = load_address _Py_NotImplementedStruct
836836
r2 = r0 is r1
837837
if r2 goto L2 else goto L1 :: bool
838838
L1:
@@ -953,7 +953,7 @@ def Derived.__ne__(self, rhs):
953953
r4 :: object
954954
L0:
955955
r0 = self.__eq__(rhs)
956-
r1 = NotImplemented
956+
r1 = load_address _Py_NotImplementedStruct
957957
r2 = r0 is r1
958958
if r2 goto L2 else goto L1 :: bool
959959
L1:

mypyc/test-data/irbuild-optional.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def f(x):
295295
r5 :: __main__.A
296296
r6 :: int
297297
L0:
298-
r0 = int
298+
r0 = load_address PyLong_Type
299299
r1 = PyObject_IsInstance(x, r0)
300300
r2 = truncate r1: int32 to builtins.bool
301301
if r2 goto L1 else goto L2 :: bool

mypyc/test-data/refcount.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ def g(x):
734734
r5 :: object
735735
r6 :: int
736736
L0:
737-
r0 = int
737+
r0 = load_address PyLong_Type
738738
r1 = unicode_1 :: static ('base')
739739
r2 = (x) :: tuple
740740
r3 = box(short_int, 4)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy