Skip to content

Commit a3b5933

Browse files
authored
[mypyc] Translate more primitive ops to CallC (#9014)
Related issue: mypyc/mypyc#734 Mainly translate primitive ops that use call_emit and with their descriptions used elsewhere. Int ops are intentionally left out because they make a lot of changes to IR tests, therefore making them in a separate PR.
1 parent d3edd60 commit a3b5933

17 files changed

+83
-79
lines changed

mypyc/irbuild/builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ def process_iterator_tuple_assignment(self,
517517
self.activate_block(ok_block)
518518

519519
for litem in reversed(post_star_vals):
520-
ritem = self.primitive_op(list_pop_last, [iter_list], line)
520+
ritem = self.call_c(list_pop_last, [iter_list], line)
521521
self.assign(litem, ritem, line)
522522

523523
# Assign the starred value

mypyc/irbuild/expression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ def transform_tuple_expr(builder: IRBuilder, expr: TupleExpr) -> Value:
463463
def _visit_tuple_display(builder: IRBuilder, expr: TupleExpr) -> Value:
464464
"""Create a list, then turn it into a tuple."""
465465
val_as_list = _visit_list_display(builder, expr.items, expr.line)
466-
return builder.primitive_op(list_tuple_op, [val_as_list], expr.line)
466+
return builder.call_c(list_tuple_op, [val_as_list], expr.line)
467467

468468

469469
def transform_dict_expr(builder: IRBuilder, expr: DictExpr) -> Value:

mypyc/irbuild/ll_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def py_call(self,
270270
pos_args_list = self.primitive_op(new_list_op, pos_arg_values, line)
271271
for star_arg_value in star_arg_values:
272272
self.primitive_op(list_extend_op, [pos_args_list, star_arg_value], line)
273-
pos_args_tuple = self.primitive_op(list_tuple_op, [pos_args_list], line)
273+
pos_args_tuple = self.call_c(list_tuple_op, [pos_args_list], line)
274274

275275
kw_args_dict = self.make_dict(kw_arg_key_value_pairs, line)
276276

mypyc/primitives/dict_ops.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from mypyc.primitives.registry import (
1212
name_ref_op, method_op, binary_op, func_op, custom_op,
1313
simple_emit, negative_int_emit, call_emit, call_negative_bool_emit,
14-
name_emit, c_custom_op, c_method_op
14+
name_emit, c_custom_op, c_method_op, c_function_op
1515
)
1616

1717

@@ -103,25 +103,24 @@
103103
return_type=dict_rprimitive,
104104
c_function_name='CPyDict_Build',
105105
error_kind=ERR_MAGIC,
106-
var_arg_type=object_rprimitive,)
106+
var_arg_type=object_rprimitive)
107107

108108
# Construct a dictionary from another dictionary.
109-
func_op(
109+
c_function_op(
110110
name='builtins.dict',
111111
arg_types=[dict_rprimitive],
112-
result_type=dict_rprimitive,
112+
return_type=dict_rprimitive,
113+
c_function_name='PyDict_Copy',
113114
error_kind=ERR_MAGIC,
114-
emit=call_emit('PyDict_Copy'),
115115
priority=2)
116116

117117
# Generic one-argument dict constructor: dict(obj)
118-
119-
func_op(
118+
c_function_op(
120119
name='builtins.dict',
121120
arg_types=[object_rprimitive],
122-
result_type=dict_rprimitive,
123-
error_kind=ERR_MAGIC,
124-
emit=call_emit('CPyDict_FromAny'))
121+
return_type=dict_rprimitive,
122+
c_function_name='CPyDict_FromAny',
123+
error_kind=ERR_MAGIC)
125124

126125
# dict.keys()
127126
c_method_op(

mypyc/primitives/list_ops.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,20 @@ def emit_new(emitter: EmitterInterface, args: List[str], dest: str) -> None:
4949

5050

5151
# list[index] (for an integer index)
52-
list_get_item_op = method_op(
52+
list_get_item_op = c_method_op(
5353
name='__getitem__',
5454
arg_types=[list_rprimitive, int_rprimitive],
55-
result_type=object_rprimitive,
56-
error_kind=ERR_MAGIC,
57-
emit=call_emit('CPyList_GetItem'))
55+
return_type=object_rprimitive,
56+
c_function_name='CPyList_GetItem',
57+
error_kind=ERR_MAGIC)
5858

5959
# Version with no int bounds check for when it is known to be short
60-
method_op(
60+
c_method_op(
6161
name='__getitem__',
6262
arg_types=[list_rprimitive, short_int_rprimitive],
63-
result_type=object_rprimitive,
63+
return_type=object_rprimitive,
64+
c_function_name='CPyList_GetItemShort',
6465
error_kind=ERR_MAGIC,
65-
emit=call_emit('CPyList_GetItemShort'),
6666
priority=2)
6767

6868
# This is unsafe because it assumes that the index is a non-negative short integer
@@ -76,13 +76,13 @@ def emit_new(emitter: EmitterInterface, args: List[str], dest: str) -> None:
7676
emit=call_emit('CPyList_GetItemUnsafe'))
7777

7878
# list[index] = obj
79-
list_set_item_op = method_op(
79+
list_set_item_op = c_method_op(
8080
name='__setitem__',
8181
arg_types=[list_rprimitive, int_rprimitive, object_rprimitive],
82-
steals=[False, False, True],
83-
result_type=bool_rprimitive,
82+
return_type=bool_rprimitive,
83+
c_function_name='CPyList_SetItem',
8484
error_kind=ERR_FALSE,
85-
emit=call_emit('CPyList_SetItem'))
85+
steals=[False, False, True])
8686

8787
# list.append(obj)
8888
list_append_op = method_op(
@@ -101,20 +101,20 @@ def emit_new(emitter: EmitterInterface, args: List[str], dest: str) -> None:
101101
emit=call_emit('CPyList_Extend'))
102102

103103
# list.pop()
104-
list_pop_last = method_op(
104+
list_pop_last = c_method_op(
105105
name='pop',
106106
arg_types=[list_rprimitive],
107-
result_type=object_rprimitive,
108-
error_kind=ERR_MAGIC,
109-
emit=call_emit('CPyList_PopLast'))
107+
return_type=object_rprimitive,
108+
c_function_name='CPyList_PopLast',
109+
error_kind=ERR_MAGIC)
110110

111111
# list.pop(index)
112-
list_pop = method_op(
112+
list_pop = c_method_op(
113113
name='pop',
114114
arg_types=[list_rprimitive, int_rprimitive],
115-
result_type=object_rprimitive,
116-
error_kind=ERR_MAGIC,
117-
emit=call_emit('CPyList_Pop'))
115+
return_type=object_rprimitive,
116+
c_function_name='CPyList_Pop',
117+
error_kind=ERR_MAGIC)
118118

119119
# list.count(obj)
120120
c_method_op(

mypyc/primitives/tuple_ops.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
1010
EmitterInterface, ERR_NEVER, ERR_MAGIC
1111
)
1212
from mypyc.ir.rtypes import tuple_rprimitive, int_rprimitive, list_rprimitive, object_rprimitive
13-
from mypyc.primitives.registry import func_op, method_op, custom_op, call_emit, simple_emit
13+
from mypyc.primitives.registry import (
14+
func_op, c_method_op, custom_op, simple_emit, c_function_op
15+
)
1416

1517

1618
# tuple[index] (for an int index)
17-
tuple_get_item_op = method_op(
19+
tuple_get_item_op = c_method_op(
1820
name='__getitem__',
1921
arg_types=[tuple_rprimitive, int_rprimitive],
20-
result_type=object_rprimitive,
21-
error_kind=ERR_MAGIC,
22-
emit=call_emit('CPySequenceTuple_GetItem'))
23-
22+
return_type=object_rprimitive,
23+
c_function_name='CPySequenceTuple_GetItem',
24+
error_kind=ERR_MAGIC)
2425

2526
# Construct a boxed tuple from items: (item1, item2, ...)
2627
new_tuple_op = custom_op(
@@ -49,18 +50,18 @@ def emit_len(emitter: EmitterInterface, args: List[str], dest: str) -> None:
4950
emit=emit_len)
5051

5152
# Construct tuple from a list.
52-
list_tuple_op = func_op(
53+
list_tuple_op = c_function_op(
5354
name='builtins.tuple',
5455
arg_types=[list_rprimitive],
55-
result_type=tuple_rprimitive,
56+
return_type=tuple_rprimitive,
57+
c_function_name='PyList_AsTuple',
5658
error_kind=ERR_MAGIC,
57-
emit=call_emit('PyList_AsTuple'),
5859
priority=2)
5960

6061
# Construct tuple from an arbitrary (iterable) object.
61-
func_op(
62+
c_function_op(
6263
name='builtins.tuple',
6364
arg_types=[object_rprimitive],
64-
result_type=tuple_rprimitive,
65-
error_kind=ERR_MAGIC,
66-
emit=call_emit('PySequence_Tuple'))
65+
return_type=tuple_rprimitive,
66+
c_function_name='PySequence_Tuple',
67+
error_kind=ERR_MAGIC)

mypyc/test-data/exceptions.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def f(x):
1414
r2, r3 :: int
1515
L0:
1616
r0 = 0
17-
r1 = x[r0] :: list
17+
r1 = CPyList_GetItemShort(x, r0)
1818
if is_error(r1) goto L3 (error at f:3) else goto L1
1919
L1:
2020
r2 = unbox(int, r1)
@@ -51,7 +51,7 @@ L1:
5151
r2 = None
5252
inc_ref z :: int
5353
r3 = box(int, z)
54-
r4 = x.__setitem__(y, r3) :: list
54+
r4 = CPyList_SetItem(x, y, r3)
5555
if not r4 goto L3 (error at f:4) else goto L2 :: bool
5656
L2:
5757
r5 = None
@@ -144,7 +144,7 @@ L1:
144144
r2 = i < l :: int
145145
if r2 goto L2 else goto L7 :: bool
146146
L2:
147-
r3 = a[i] :: list
147+
r3 = CPyList_GetItem(a, i)
148148
if is_error(r3) goto L8 (error at sum:6) else goto L3
149149
L3:
150150
r4 = unbox(int, r3)

mypyc/test-data/irbuild-any.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ L0:
116116
r5 = a.__setitem__(r3, r4) :: object
117117
r6 = box(int, n)
118118
r7 = l.__setitem__(a, r6) :: object
119-
r8 = l.__setitem__(n, a) :: list
119+
r8 = CPyList_SetItem(l, n, a)
120120
r9 = box(int, n)
121121
r10 = [a, r9]
122122
r11 = None

mypyc/test-data/irbuild-basic.test

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ L0:
754754
r6 = (r4, r5)
755755
r7 = 0
756756
r8 = box(tuple[int, int], r6)
757-
r9 = a.__setitem__(r7, r8) :: list
757+
r9 = CPyList_SetItem(a, r7, r8)
758758
r10 = True
759759
r11 = box(bool, r10)
760760
y = r11
@@ -1633,7 +1633,7 @@ L0:
16331633
r7 = []
16341634
r8 = box(tuple[int, int, int], r3)
16351635
r9 = r7.extend(r8) :: list
1636-
r10 = tuple r7 :: list
1636+
r10 = PyList_AsTuple(r7)
16371637
r11 = PyDict_New()
16381638
r12 = py_call_with_kwargs(r6, r10, r11)
16391639
r13 = unbox(tuple[int, int, int], r12)
@@ -1662,7 +1662,7 @@ L0:
16621662
r8 = [r7]
16631663
r9 = box(tuple[int, int], r3)
16641664
r10 = r8.extend(r9) :: list
1665-
r11 = tuple r8 :: list
1665+
r11 = PyList_AsTuple(r8)
16661666
r12 = PyDict_New()
16671667
r13 = py_call_with_kwargs(r6, r11, r12)
16681668
r14 = unbox(tuple[int, int, int], r13)
@@ -2387,7 +2387,7 @@ def g(a, b, c):
23872387
r2, r3 :: int
23882388
L0:
23892389
r0 = a.__getitem__(c)
2390-
r1 = b[c] :: list
2390+
r1 = CPyList_GetItem(b, c)
23912391
r2 = unbox(int, r1)
23922392
r3 = r0 + r2 :: int
23932393
return r3
@@ -3261,7 +3261,7 @@ L1:
32613261
unreachable
32623262
L2:
32633263
r2 = 0
3264-
r3 = r0[r2] :: list
3264+
r3 = CPyList_GetItemShort(r0, r2)
32653265
r4 = unbox(int, r3)
32663266
return r4
32673267

mypyc/test-data/irbuild-classes.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ L0:
6060
r3 = [c]
6161
a = r3
6262
r4 = 0
63-
r5 = a[r4] :: list
63+
r5 = CPyList_GetItemShort(a, r4)
6464
r6 = cast(__main__.C, r5)
6565
d = r6
6666
r7 = d.x

mypyc/test-data/irbuild-generics.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def g(x):
2020
r2 :: list
2121
L0:
2222
r0 = 0
23-
r1 = x[r0] :: list
23+
r1 = CPyList_GetItemShort(x, r0)
2424
r2 = [r1]
2525
return r2
2626
def h(x, y):

mypyc/test-data/irbuild-lists.test

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def f(x):
1010
r2 :: int
1111
L0:
1212
r0 = 0
13-
r1 = x[r0] :: list
13+
r1 = CPyList_GetItemShort(x, r0)
1414
r2 = unbox(int, r1)
1515
return r2
1616

@@ -26,7 +26,7 @@ def f(x):
2626
r2 :: list
2727
L0:
2828
r0 = 0
29-
r1 = x[r0] :: list
29+
r1 = CPyList_GetItemShort(x, r0)
3030
r2 = cast(list, r1)
3131
return r2
3232

@@ -45,10 +45,10 @@ def f(x):
4545
r5 :: int
4646
L0:
4747
r0 = 0
48-
r1 = x[r0] :: list
48+
r1 = CPyList_GetItemShort(x, r0)
4949
r2 = cast(list, r1)
5050
r3 = 1
51-
r4 = r2[r3] :: list
51+
r4 = CPyList_GetItemShort(r2, r3)
5252
r5 = unbox(int, r4)
5353
return r5
5454

@@ -67,7 +67,7 @@ L0:
6767
r0 = 1
6868
r1 = 0
6969
r2 = box(short_int, r0)
70-
r3 = x.__setitem__(r1, r2) :: list
70+
r3 = CPyList_SetItem(x, r1, r2)
7171
r4 = None
7272
return r4
7373

@@ -188,11 +188,11 @@ L1:
188188
r3 = r2 < r1 :: short_int
189189
if r3 goto L2 else goto L4 :: bool
190190
L2:
191-
r4 = l[i] :: list
191+
r4 = CPyList_GetItem(l, i)
192192
r5 = 1
193193
r6 = box(short_int, r5)
194194
r7 = r4 += r6
195-
r8 = l.__setitem__(i, r7) :: list
195+
r8 = CPyList_SetItem(l, i, r7)
196196
L3:
197197
r9 = 1
198198
r10 = r2 + r9 :: short_int

mypyc/test-data/irbuild-optional.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@ L0:
197197
r0 = 0
198198
r1 = 0
199199
r2 = box(short_int, r0)
200-
r3 = x.__setitem__(r1, r2) :: list
200+
r3 = CPyList_SetItem(x, r1, r2)
201201
r4 = None
202202
r5 = 1
203203
r6 = box(None, r4)
204-
r7 = x.__setitem__(r5, r6) :: list
204+
r7 = CPyList_SetItem(x, r5, r6)
205205
r8 = None
206206
return r8
207207

@@ -334,7 +334,7 @@ def f(x):
334334
r2 :: union[int, str]
335335
L0:
336336
r0 = 0
337-
r1 = x[r0] :: list
337+
r1 = CPyList_GetItemShort(x, r0)
338338
r2 = cast(union[int, str], r1)
339339
return r2
340340

mypyc/test-data/irbuild-statements.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ L0:
559559
a.x = r1; r2 = is_error
560560
r3 = t[1]
561561
r4 = r3[0]
562-
r5 = l.__setitem__(r0, r4) :: list
562+
r5 = CPyList_SetItem(l, r0, r4)
563563
r6 = r3[1]
564564
r7 = unbox(int, r6)
565565
z = r7

0 commit comments

Comments
 (0)