Skip to content

Commit a2e529a

Browse files
authored
[mypyc] Merge two ops and obsolete more old registry (#9368)
This PR merges list_get_item_unsafe_op and get_module_dict_op and also removes several old registry and functions.
1 parent 5c57c5b commit a2e529a

File tree

9 files changed

+23
-102
lines changed

9 files changed

+23
-102
lines changed

mypyc/irbuild/expression.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
from mypy.types import TupleType, get_proper_type
1717

1818
from mypyc.ir.ops import (
19-
Value, TupleGet, TupleSet, PrimitiveOp, BasicBlock, OpDescription, Assign, LoadAddress
19+
Value, TupleGet, TupleSet, BasicBlock, OpDescription, Assign, LoadAddress
2020
)
2121
from mypyc.ir.rtypes import RTuple, object_rprimitive, is_none_rprimitive, is_int_rprimitive
2222
from mypyc.ir.func_ir import FUNC_CLASSMETHOD, FUNC_STATICMETHOD
23-
from mypyc.primitives.registry import name_ref_ops, CFunctionDescription, builtin_names
23+
from mypyc.primitives.registry import CFunctionDescription, builtin_names
2424
from mypyc.primitives.generic_ops import iter_op
2525
from mypyc.primitives.misc_ops import new_slice_op, ellipsis_op, type_op
2626
from mypyc.primitives.list_ops import new_list_op, list_append_op, list_extend_op
@@ -49,11 +49,6 @@ def transform_name_expr(builder: IRBuilder, expr: NameExpr) -> Value:
4949
return builder.true()
5050
if fullname == 'builtins.False':
5151
return builder.false()
52-
if fullname in name_ref_ops:
53-
# Use special access op for this particular name.
54-
desc = name_ref_ops[fullname]
55-
assert desc.result_type is not None
56-
return builder.add(PrimitiveOp([], desc, expr.line))
5752

5853
if isinstance(expr.node, Var) and expr.node.is_final:
5954
value = builder.emit_load_final(

mypyc/irbuild/for_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def unsafe_index(
392392
# since we want to use __getitem__ if we don't have an unsafe version,
393393
# so we just check manually.
394394
if is_list_rprimitive(target.type):
395-
return builder.primitive_op(list_get_item_unsafe_op, [target, index], line)
395+
return builder.call_c(list_get_item_unsafe_op, [target, index], line)
396396
else:
397397
return builder.gen_method_call(target, '__getitem__', [index], None, line)
398398

mypyc/irbuild/ll_builder.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
STATIC_PREFIX
4040
)
4141
from mypyc.primitives.registry import (
42-
method_ops, func_ops,
43-
c_method_call_ops, CFunctionDescription, c_function_ops,
42+
func_ops, c_method_call_ops, CFunctionDescription, c_function_ops,
4443
c_binary_ops, c_unary_ops
4544
)
4645
from mypyc.primitives.list_ops import (
@@ -1025,13 +1024,10 @@ def translate_special_method_call(self,
10251024
10261025
Return None if no translation found; otherwise return the target register.
10271026
"""
1028-
ops = method_ops.get(name, [])
10291027
call_c_ops_candidates = c_method_call_ops.get(name, [])
10301028
call_c_op = self.matching_call_c(call_c_ops_candidates, [base_reg] + args,
10311029
line, result_type)
1032-
if call_c_op is not None:
1033-
return call_c_op
1034-
return self.matching_primitive_op(ops, [base_reg] + args, line, result_type=result_type)
1030+
return call_c_op
10351031

10361032
def translate_eq_cmp(self,
10371033
lreg: Value,

mypyc/irbuild/statement.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def transform_import(builder: IRBuilder, node: Import) -> None:
125125
base = name = node_id.split('.')[0]
126126

127127
# Python 3.7 has a nice 'PyImport_GetModule' function that we can't use :(
128-
mod_dict = builder.primitive_op(get_module_dict_op, [], node.line)
128+
mod_dict = builder.call_c(get_module_dict_op, [], node.line)
129129
obj = builder.call_c(dict_get_item_op,
130130
[mod_dict, builder.load_static_unicode(base)], node.line)
131131
builder.gen_method_call(

mypyc/primitives/list_ops.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
c_int_rprimitive
99
)
1010
from mypyc.primitives.registry import (
11-
custom_op, load_address_op, call_emit, c_function_op, c_binary_op, c_method_op
11+
custom_op, load_address_op, c_function_op, c_binary_op, c_method_op, c_custom_op
1212
)
1313

1414

@@ -66,13 +66,11 @@ def emit_new(emitter: EmitterInterface, args: List[str], dest: str) -> None:
6666

6767
# This is unsafe because it assumes that the index is a non-negative short integer
6868
# that is in-bounds for the list.
69-
list_get_item_unsafe_op = custom_op(
70-
name='__getitem__',
69+
list_get_item_unsafe_op = c_custom_op(
7170
arg_types=[list_rprimitive, short_int_rprimitive],
72-
result_type=object_rprimitive,
73-
error_kind=ERR_NEVER,
74-
format_str='{dest} = {args[0]}[{args[1]}] :: unsafe list',
75-
emit=call_emit('CPyList_GetItemUnsafe'))
71+
return_type=object_rprimitive,
72+
c_function_name='CPyList_GetItemUnsafe',
73+
error_kind=ERR_NEVER)
7674

7775
# list[index] = obj
7876
list_set_item_op = c_method_op(

mypyc/primitives/misc_ops.py

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

@@ -108,12 +108,11 @@
108108
error_kind=ERR_MAGIC)
109109

110110
# Get the sys.modules dictionary
111-
get_module_dict_op = custom_op(
112-
name='get_module_dict',
111+
get_module_dict_op = c_custom_op(
113112
arg_types=[],
114-
result_type=dict_rprimitive,
113+
return_type=dict_rprimitive,
114+
c_function_name='PyImport_GetModuleDict',
115115
error_kind=ERR_NEVER,
116-
emit=call_emit('PyImport_GetModuleDict'),
117116
is_borrowed=True)
118117

119118
# isinstance(obj, cls)

mypyc/primitives/registry.py

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@
6565
# Primitive ops for built-in functions (key is function name such as 'builtins.len')
6666
func_ops = {} # type: Dict[str, List[OpDescription]]
6767

68-
# Primitive ops for built-in methods (key is method name such as 'builtins.list.append')
69-
method_ops = {} # type: Dict[str, List[OpDescription]]
70-
71-
# Primitive ops for reading module attributes (key is name such as 'builtins.None')
72-
name_ref_ops = {} # type: Dict[str, OpDescription]
73-
7468
# CallC op for method call(such as 'str.join')
7569
c_method_call_ops = {} # type: Dict[str, List[CFunctionDescription]]
7670

@@ -109,11 +103,6 @@ def emit(emitter: EmitterInterface, args: List[str], dest: str) -> None:
109103
return emit
110104

111105

112-
def call_emit(func: str) -> EmitCallback:
113-
"""Construct a PrimitiveOp emit callback function that calls a C function."""
114-
return simple_emit('{dest} = %s({comma_args});' % func)
115-
116-
117106
def func_op(name: str,
118107
arg_types: List[RType],
119108
result_type: RType,
@@ -154,62 +143,6 @@ def func_op(name: str,
154143
return desc
155144

156145

157-
def method_op(name: str,
158-
arg_types: List[RType],
159-
result_type: Optional[RType],
160-
error_kind: int,
161-
emit: EmitCallback,
162-
steals: StealsDescription = False,
163-
is_borrowed: bool = False,
164-
priority: int = 1) -> OpDescription:
165-
"""Define a primitive op that replaces a method call.
166-
167-
Most arguments are similar to func_op().
168-
169-
This will be automatically generated by matching against the AST.
170-
171-
Args:
172-
name: short name of the method (for example, 'append')
173-
arg_types: argument types; the receiver is always the first argument
174-
result_type: type of the result, None if void
175-
"""
176-
ops = method_ops.setdefault(name, [])
177-
assert len(arg_types) > 0
178-
args = ', '.join('{args[%d]}' % i
179-
for i in range(1, len(arg_types)))
180-
type_name = short_name(arg_types[0].name)
181-
if name == '__getitem__':
182-
format_str = '{dest} = {args[0]}[{args[1]}] :: %s' % type_name
183-
else:
184-
format_str = '{dest} = {args[0]}.%s(%s) :: %s' % (name, args, type_name)
185-
desc = OpDescription(name, arg_types, result_type, False, error_kind, format_str, emit,
186-
steals, is_borrowed, priority)
187-
ops.append(desc)
188-
return desc
189-
190-
191-
def name_ref_op(name: str,
192-
result_type: RType,
193-
error_kind: int,
194-
emit: EmitCallback,
195-
is_borrowed: bool = False) -> OpDescription:
196-
"""Define an op that is used to implement reading a module attribute.
197-
198-
This will be automatically generated by matching against the AST.
199-
200-
Most arguments are similar to func_op().
201-
202-
Args:
203-
name: fully-qualified name (e.g. 'builtins.None')
204-
"""
205-
assert name not in name_ref_ops, 'already defined: %s' % name
206-
format_str = '{dest} = %s' % short_name(name)
207-
desc = OpDescription(name, [], result_type, False, error_kind, format_str, emit,
208-
False, is_borrowed, 0)
209-
name_ref_ops[name] = desc
210-
return desc
211-
212-
213146
def custom_op(arg_types: List[RType],
214147
result_type: RType,
215148
error_kind: int,

mypyc/test-data/irbuild-basic.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,7 +1975,7 @@ L1:
19751975
r9 = r5 < r8 :: signed
19761976
if r9 goto L2 else goto L14 :: bool
19771977
L2:
1978-
r10 = r4[r5] :: unsafe list
1978+
r10 = CPyList_GetItemUnsafe(r4, r5)
19791979
r11 = unbox(int, r10)
19801980
x = r11
19811981
r13 = x & 1
@@ -2059,7 +2059,7 @@ L1:
20592059
r9 = r5 < r8 :: signed
20602060
if r9 goto L2 else goto L14 :: bool
20612061
L2:
2062-
r10 = r4[r5] :: unsafe list
2062+
r10 = CPyList_GetItemUnsafe(r4, r5)
20632063
r11 = unbox(int, r10)
20642064
x = r11
20652065
r13 = x & 1
@@ -2146,7 +2146,7 @@ L1:
21462146
r4 = r0 < r3 :: signed
21472147
if r4 goto L2 else goto L4 :: bool
21482148
L2:
2149-
r5 = l[r0] :: unsafe list
2149+
r5 = CPyList_GetItemUnsafe(l, r0)
21502150
r6 = unbox(tuple[int, int, int], r5)
21512151
r7 = r6[0]
21522152
x = r7
@@ -2168,7 +2168,7 @@ L5:
21682168
r16 = r12 < r15 :: signed
21692169
if r16 goto L6 else goto L8 :: bool
21702170
L6:
2171-
r17 = l[r12] :: unsafe list
2171+
r17 = CPyList_GetItemUnsafe(l, r12)
21722172
r18 = unbox(tuple[int, int, int], r17)
21732173
r19 = r18[0]
21742174
x0 = r19

mypyc/test-data/irbuild-statements.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ L1:
326326
r4 = r0 < r3 :: signed
327327
if r4 goto L2 else goto L4 :: bool
328328
L2:
329-
r5 = ls[r0] :: unsafe list
329+
r5 = CPyList_GetItemUnsafe(ls, r0)
330330
r6 = unbox(int, r5)
331331
x = r6
332332
r7 = CPyTagged_Add(y, x)
@@ -849,7 +849,7 @@ L1:
849849
r5 = r1 < r4 :: signed
850850
if r5 goto L2 else goto L4 :: bool
851851
L2:
852-
r6 = a[r1] :: unsafe list
852+
r6 = CPyList_GetItemUnsafe(a, r1)
853853
r7 = unbox(int, r6)
854854
x = r7
855855
r8 = CPyTagged_Add(i, x)
@@ -932,7 +932,7 @@ L2:
932932
r6 = PyIter_Next(r1)
933933
if is_error(r6) goto L7 else goto L3
934934
L3:
935-
r7 = a[r0] :: unsafe list
935+
r7 = CPyList_GetItemUnsafe(a, r0)
936936
r8 = unbox(int, r7)
937937
x = r8
938938
r9 = unbox(bool, r6)
@@ -986,7 +986,7 @@ L3:
986986
L4:
987987
r9 = unbox(bool, r3)
988988
x = r9
989-
r10 = b[r1] :: unsafe list
989+
r10 = CPyList_GetItemUnsafe(b, r1)
990990
r11 = unbox(int, r10)
991991
y = r11
992992
x = 0

0 commit comments

Comments
 (0)