Skip to content

[mypyc] New style new_tuple_op #9298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ def compare_tagged(self, lhs: Value, rhs: Value, op: str, line: int) -> Value:
def builtin_len(self, val: Value, line: int) -> Value:
return self.builder.builtin_len(val, line)

def new_tuple(self, items: List[Value], line: int) -> Value:
return self.builder.new_tuple(items, line)

@property
def environment(self) -> Environment:
return self.builder.environment
Expand Down
10 changes: 4 additions & 6 deletions mypyc/irbuild/classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
not_implemented_op
)
from mypyc.primitives.dict_ops import dict_set_item_op, dict_new_op
from mypyc.primitives.tuple_ops import new_tuple_op
from mypyc.common import SELF_NAME
from mypyc.irbuild.util import (
is_dataclass_decorator, get_func_def, is_dataclass, is_constant, add_self_to_env
Expand Down Expand Up @@ -165,7 +164,7 @@ def allocate_class(builder: IRBuilder, cdef: ClassDef) -> Value:
base_exprs = cdef.base_type_exprs + cdef.removed_base_type_exprs
if base_exprs:
bases = [builder.accept(x) for x in base_exprs]
tp_bases = builder.primitive_op(new_tuple_op, bases, cdef.line)
tp_bases = builder.new_tuple(bases, cdef.line)
else:
tp_bases = builder.add(LoadErrorValue(object_rprimitive, is_borrowed=True))
modname = builder.load_static_unicode(builder.module_name)
Expand Down Expand Up @@ -219,7 +218,7 @@ def populate_non_ext_bases(builder: IRBuilder, cdef: ClassDef) -> Value:

base = builder.load_global_str(cls.name, cdef.line)
bases.append(base)
return builder.primitive_op(new_tuple_op, bases, cdef.line)
return builder.new_tuple(bases, cdef.line)


def find_non_ext_metaclass(builder: IRBuilder, cdef: ClassDef, bases: Value) -> Value:
Expand Down Expand Up @@ -465,9 +464,8 @@ def create_mypyc_attrs_tuple(builder: IRBuilder, ir: ClassIR, line: int) -> Valu
attrs = [name for ancestor in ir.mro for name in ancestor.attributes]
if ir.inherits_python:
attrs.append('__dict__')
return builder.primitive_op(new_tuple_op,
[builder.load_static_unicode(attr) for attr in attrs],
line)
items = [builder.load_static_unicode(attr) for attr in attrs]
return builder.new_tuple(items, line)


def finish_non_ext_dict(builder: IRBuilder, non_ext: NonExtClassInfo, line: int) -> None:
Expand Down
9 changes: 7 additions & 2 deletions mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def py_call(self,

if len(star_arg_values) == 0:
# We can directly construct a tuple if there are no star args.
pos_args_tuple = self.primitive_op(new_tuple_op, pos_arg_values, line)
pos_args_tuple = self.new_tuple(pos_arg_values, line)
else:
# Otherwise we construct a list and call extend it with the star args, since tuples
# don't have an extend method.
Expand Down Expand Up @@ -338,7 +338,8 @@ def native_args_to_positional(self,
for lst, arg in zip(formal_to_actual, sig.args):
output_arg = None
if arg.kind == ARG_STAR:
output_arg = self.primitive_op(new_tuple_op, [args[i] for i in lst], line)
items = [args[i] for i in lst]
output_arg = self.new_tuple(items, line)
elif arg.kind == ARG_STAR2:
dict_entries = [(self.load_static_unicode(cast(str, arg_names[i])), args[i])
for i in lst]
Expand Down Expand Up @@ -844,6 +845,10 @@ def builtin_len(self, val: Value, line: int) -> Value:
else:
return self.call_c(generic_len_op, [val], line)

def new_tuple(self, items: List[Value], line: int) -> Value:
load_size_op = self.add(LoadInt(len(items), -1, c_pyssize_t_rprimitive))
return self.call_c(new_tuple_op, [load_size_op] + items, line)

# Internal helpers

def decompose_union_helper(self,
Expand Down
18 changes: 9 additions & 9 deletions mypyc/primitives/tuple_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"""

from mypyc.ir.ops import ERR_MAGIC
from mypyc.ir.rtypes import tuple_rprimitive, int_rprimitive, list_rprimitive, object_rprimitive
from mypyc.ir.rtypes import (
tuple_rprimitive, int_rprimitive, list_rprimitive, object_rprimitive, c_pyssize_t_rprimitive
)
from mypyc.primitives.registry import (
c_method_op, custom_op, simple_emit, c_function_op
c_method_op, c_function_op, c_custom_op
)


Expand All @@ -20,14 +22,12 @@
error_kind=ERR_MAGIC)

# Construct a boxed tuple from items: (item1, item2, ...)
new_tuple_op = custom_op(
arg_types=[object_rprimitive],
result_type=tuple_rprimitive,
is_var_arg=True,
new_tuple_op = c_custom_op(
arg_types=[c_pyssize_t_rprimitive],
return_type=tuple_rprimitive,
c_function_name='PyTuple_Pack',
error_kind=ERR_MAGIC,
steals=False,
format_str='{dest} = ({comma_args}) :: tuple',
emit=simple_emit('{dest} = PyTuple_Pack({num_args}{comma_if_args}{comma_args});'))
var_arg_type=object_rprimitive)

# Construct tuple from a list.
list_tuple_op = c_function_op(
Expand Down
13 changes: 7 additions & 6 deletions mypyc/test-data/irbuild-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ def call_python_function_with_keyword_arg(x):
L0:
r0 = load_address PyLong_Type
r1 = unicode_3 :: static ('base')
r2 = (x) :: tuple
r2 = PyTuple_Pack(1, x)
r3 = box(short_int, 4)
r4 = CPyDict_Build(1, r1, r3)
r5 = py_call_with_kwargs(r0, r2, r4)
Expand Down Expand Up @@ -1287,15 +1287,15 @@ L0:
r1 = CPyObject_GetAttr(xs, r0)
r2 = unicode_5 :: static ('x')
r3 = box(short_int, 0)
r4 = (r3) :: tuple
r4 = PyTuple_Pack(1, r3)
r5 = box(int, first)
r6 = CPyDict_Build(1, r2, r5)
r7 = py_call_with_kwargs(r1, r4, r6)
r8 = unicode_4 :: static ('insert')
r9 = CPyObject_GetAttr(xs, r8)
r10 = unicode_5 :: static ('x')
r11 = unicode_6 :: static ('i')
r12 = () :: tuple
r12 = PyTuple_Pack(0)
r13 = box(int, second)
r14 = box(short_int, 2)
r15 = CPyDict_Build(2, r10, r13, r11, r14)
Expand Down Expand Up @@ -1828,7 +1828,7 @@ L0:
r7 = __main__.globals :: static
r8 = unicode_6 :: static ('f')
r9 = CPyDict_GetItem(r7, r8)
r10 = () :: tuple
r10 = PyTuple_Pack(0)
r11 = PyDict_New()
r12 = CPyDict_UpdateInDisplay(r11, r6)
r13 = py_call_with_kwargs(r9, r10, r11)
Expand All @@ -1840,7 +1840,8 @@ def h():
r2, r3 :: object
r4, r5 :: dict
r6 :: str
r7, r8 :: object
r7 :: object
r8 :: object
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have located the source cause of this problem. I'll submit a separate PR for that since it's going to change the IR tests in a lot of places(originally when we introduce const_int_regs into pretty IR printing, we didn't find out this happening)

r9 :: tuple
r10 :: dict
r11 :: int32
Expand All @@ -1856,7 +1857,7 @@ L0:
r6 = unicode_6 :: static ('f')
r7 = CPyDict_GetItem(r5, r6)
r8 = box(short_int, 2)
r9 = (r8) :: tuple
r9 = PyTuple_Pack(1, r8)
r10 = PyDict_New()
r11 = CPyDict_UpdateInDisplay(r10, r4)
r12 = py_call_with_kwargs(r7, r9, r10)
Expand Down
8 changes: 4 additions & 4 deletions mypyc/test-data/irbuild-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ L6:
r42 = pytype_from_template(r41, r39, r40)
r43 = C_trait_vtable_setup()
r44 = unicode_8 :: static ('__mypyc_attrs__')
r45 = () :: tuple
r45 = PyTuple_Pack(0)
r46 = PyObject_SetAttr(r42, r44, r45)
__main__.C = r42 :: type
r47 = __main__.globals :: static
Expand All @@ -421,7 +421,7 @@ L6:
r52 = __main__.S_template :: type
r53 = pytype_from_template(r52, r50, r51)
r54 = unicode_8 :: static ('__mypyc_attrs__')
r55 = () :: tuple
r55 = PyTuple_Pack(0)
r56 = PyObject_SetAttr(r53, r54, r55)
__main__.S = r53 :: type
r57 = __main__.globals :: static
Expand All @@ -436,14 +436,14 @@ L6:
r66 = unicode_6 :: static ('T')
r67 = CPyDict_GetItem(r65, r66)
r68 = PyObject_GetItem(r64, r67)
r69 = (r60, r61, r68) :: tuple
r69 = PyTuple_Pack(3, r60, r61, r68)
r70 = unicode_7 :: static ('__main__')
r71 = __main__.D_template :: type
r72 = pytype_from_template(r71, r69, r70)
r73 = D_trait_vtable_setup()
r74 = unicode_8 :: static ('__mypyc_attrs__')
r75 = unicode_11 :: static ('__dict__')
r76 = (r75) :: tuple
r76 = PyTuple_Pack(1, r75)
r77 = PyObject_SetAttr(r72, r74, r76)
__main__.D = r72 :: type
r78 = __main__.globals :: static
Expand Down
2 changes: 1 addition & 1 deletion mypyc/test-data/refcount.test
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ def g(x):
L0:
r0 = load_address PyLong_Type
r1 = unicode_1 :: static ('base')
r2 = (x) :: tuple
r2 = PyTuple_Pack(1, x)
r3 = box(short_int, 4)
r4 = CPyDict_Build(1, r1, r3)
dec_ref r3
Expand Down
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