Skip to content

[mypyc] Build True, False and None op in irbuild #9294

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 6 commits into from
Aug 12, 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
15 changes: 12 additions & 3 deletions mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from mypyc.primitives.list_ops import to_list, list_pop_last
from mypyc.primitives.dict_ops import dict_get_item_op, dict_set_item_op
from mypyc.primitives.generic_ops import py_setattr_op, iter_op, next_op
from mypyc.primitives.misc_ops import true_op, false_op, import_op
from mypyc.primitives.misc_ops import import_op
from mypyc.crash import catch_errors
from mypyc.options import CompilerOptions
from mypyc.errors import Errors
Expand Down Expand Up @@ -200,6 +200,15 @@ def coerce(self, src: Value, target_type: RType, line: int, force: bool = False)
def none_object(self) -> Value:
return self.builder.none_object()

def none(self) -> Value:
return self.builder.none()

def true(self) -> Value:
return self.builder.true()

def false(self) -> Value:
return self.builder.false()

def py_call(self,
function: Value,
arg_values: List[Value],
Expand Down Expand Up @@ -339,9 +348,9 @@ def load_final_literal_value(self, val: Union[int, str, bytes, float, bool],
"""Load value of a final name or class-level attribute."""
if isinstance(val, bool):
if val:
return self.primitive_op(true_op, [], line)
return self.true()
else:
return self.primitive_op(false_op, [], line)
return self.false()
elif isinstance(val, int):
# TODO: take care of negative integer initializers
# (probably easier to fix this in mypy itself).
Expand Down
4 changes: 2 additions & 2 deletions mypyc/irbuild/classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from mypyc.primitives.generic_ops import py_setattr_op, py_hasattr_op
from mypyc.primitives.misc_ops import (
dataclass_sleight_of_hand, pytype_from_template_op, py_calc_meta_op, type_object_op,
not_implemented_op, true_op
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
Expand Down Expand Up @@ -350,7 +350,7 @@ def generate_attr_defaults(builder: IRBuilder, cdef: ClassDef) -> None:
val = builder.coerce(builder.accept(stmt.rvalue), attr_type, stmt.line)
builder.add(SetAttr(self_var, lvalue.name, val, -1))

builder.add(Return(builder.primitive_op(true_op, [], -1)))
builder.add(Return(builder.true()))

blocks, env, ret_type, _ = builder.leave()
ir = FuncIR(
Expand Down
7 changes: 7 additions & 0 deletions mypyc/irbuild/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ def transform_name_expr(builder: IRBuilder, expr: NameExpr) -> Value:
if fullname in builtin_names:
typ, src = builtin_names[fullname]
return builder.add(LoadAddress(typ, src, expr.line))
# special cases
if fullname == 'builtins.None':
return builder.none()
if fullname == 'builtins.True':
return builder.true()
if fullname == 'builtins.False':
return builder.false()
if fullname in name_ref_ops:
# Use special access op for this particular name.
desc = name_ref_ops[fullname]
Expand Down
19 changes: 14 additions & 5 deletions mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
RType, RUnion, RInstance, optional_value_type, int_rprimitive, float_rprimitive,
bool_rprimitive, list_rprimitive, str_rprimitive, is_none_rprimitive, object_rprimitive,
c_pyssize_t_rprimitive, is_short_int_rprimitive, is_tagged, PyVarObject, short_int_rprimitive,
is_list_rprimitive, is_tuple_rprimitive, is_dict_rprimitive, is_set_rprimitive, PySetObject
is_list_rprimitive, is_tuple_rprimitive, is_dict_rprimitive, is_set_rprimitive, PySetObject,
none_rprimitive
)
from mypyc.ir.func_ir import FuncDecl, FuncSignature
from mypyc.ir.class_ir import ClassIR, all_concrete_classes
Expand All @@ -52,7 +53,7 @@
py_getattr_op, py_call_op, py_call_with_kwargs_op, py_method_call_op, generic_len_op
)
from mypyc.primitives.misc_ops import (
none_op, none_object_op, false_op, fast_isinstance_op, bool_op, type_is_op
none_object_op, fast_isinstance_op, bool_op, type_is_op
)
from mypyc.primitives.int_ops import int_logical_op_mapping
from mypyc.rt_subtype import is_runtime_subtype
Expand Down Expand Up @@ -195,7 +196,7 @@ def py_get_attr(self, obj: Value, attr: str, line: int) -> Value:
def isinstance_helper(self, obj: Value, class_irs: List[ClassIR], line: int) -> Value:
"""Fast path for isinstance() that checks against a list of native classes."""
if not class_irs:
return self.primitive_op(false_op, [], line)
return self.false()
ret = self.isinstance_native(obj, class_irs[0], line)
for class_ir in class_irs[1:]:
def other() -> Value:
Expand All @@ -217,7 +218,7 @@ def isinstance_native(self, obj: Value, class_ir: ClassIR, line: int) -> Value:
line)
if not concrete:
# There can't be any concrete instance that matches this.
return self.primitive_op(false_op, [], line)
return self.false()
type_obj = self.get_native_type(concrete[0])
ret = self.primitive_op(type_is_op, [obj, type_obj], line)
for c in concrete[1:]:
Expand Down Expand Up @@ -424,7 +425,15 @@ def call_union_item(value: Value) -> Value:

def none(self) -> Value:
"""Load unboxed None value (type: none_rprimitive)."""
return self.add(PrimitiveOp([], none_op, line=-1))
return self.add(LoadInt(1, -1, none_rprimitive))

def true(self) -> Value:
"""Load unboxed True value (type: bool_rprimitive)."""
return self.add(LoadInt(1, -1, bool_rprimitive))

def false(self) -> Value:
"""Load unboxed False value (type: bool_rprimitive)."""
return self.add(LoadInt(0, -1, bool_rprimitive))

def none_object(self) -> Value:
"""Load Python None value (type: object_rprimitive)."""
Expand Down
17 changes: 8 additions & 9 deletions mypyc/irbuild/specialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
from mypy.types import AnyType, TypeOfAny

from mypyc.ir.ops import (
Value, BasicBlock, LoadInt, RaiseStandardError, Unreachable, OpDescription,
Value, BasicBlock, LoadInt, RaiseStandardError, Unreachable
)
from mypyc.ir.rtypes import (
RType, RTuple, str_rprimitive, list_rprimitive, dict_rprimitive, set_rprimitive,
bool_rprimitive, is_dict_rprimitive
)
from mypyc.primitives.dict_ops import dict_keys_op, dict_values_op, dict_items_op
from mypyc.primitives.misc_ops import true_op, false_op
from mypyc.irbuild.builder import IRBuilder
from mypyc.irbuild.for_helpers import translate_list_comprehension, comprehension_helper

Expand Down Expand Up @@ -147,7 +146,7 @@ def translate_any_call(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> O
if (len(expr.args) == 1
and expr.arg_kinds == [ARG_POS]
and isinstance(expr.args[0], GeneratorExpr)):
return any_all_helper(builder, expr.args[0], false_op, lambda x: x, true_op)
return any_all_helper(builder, expr.args[0], builder.false, lambda x: x, builder.true)
return None


Expand All @@ -158,28 +157,28 @@ def translate_all_call(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> O
and isinstance(expr.args[0], GeneratorExpr)):
return any_all_helper(
builder, expr.args[0],
true_op,
builder.true,
lambda x: builder.unary_op(x, 'not', expr.line),
false_op
builder.false
)
return None


def any_all_helper(builder: IRBuilder,
gen: GeneratorExpr,
initial_value_op: OpDescription,
initial_value: Callable[[], Value],
modify: Callable[[Value], Value],
new_value_op: OpDescription) -> Value:
new_value: Callable[[], Value]) -> Value:
retval = builder.alloc_temp(bool_rprimitive)
builder.assign(retval, builder.primitive_op(initial_value_op, [], -1), -1)
builder.assign(retval, initial_value(), -1)
loop_params = list(zip(gen.indices, gen.sequences, gen.condlists))
true_block, false_block, exit_block = BasicBlock(), BasicBlock(), BasicBlock()

def gen_inner_stmts() -> None:
comparison = modify(builder.accept(gen.left_expr))
builder.add_bool_branch(comparison, true_block, false_block)
builder.activate_block(true_block)
builder.assign(retval, builder.primitive_op(new_value_op, [], -1), -1)
builder.assign(retval, new_value(), -1)
builder.goto(exit_block)
builder.activate_block(false_block)

Expand Down
6 changes: 3 additions & 3 deletions mypyc/irbuild/statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)
from mypyc.ir.rtypes import exc_rtuple
from mypyc.primitives.generic_ops import py_delattr_op
from mypyc.primitives.misc_ops import true_op, false_op, type_op, get_module_dict_op
from mypyc.primitives.misc_ops import type_op, get_module_dict_op
from mypyc.primitives.dict_ops import dict_get_item_op
from mypyc.primitives.exc_ops import (
raise_exception_op, reraise_exception_op, error_catch_op, exc_matches_op, restore_exc_info_op,
Expand Down Expand Up @@ -540,15 +540,15 @@ def transform_with(builder: IRBuilder,
builder.py_get_attr(typ, '__enter__', line), [mgr_v], line
)
mgr = builder.maybe_spill(mgr_v)
exc = builder.maybe_spill_assignable(builder.primitive_op(true_op, [], -1))
exc = builder.maybe_spill_assignable(builder.true())

def try_body() -> None:
if target:
builder.assign(builder.get_assignment_target(target), value, line)
body()

def except_body() -> None:
builder.assign(exc, builder.primitive_op(false_op, [], -1), line)
builder.assign(exc, builder.false(), line)
out_block, reraise_block = BasicBlock(), BasicBlock()
builder.add_bool_branch(
builder.py_call(builder.read(exit_),
Expand Down
21 changes: 2 additions & 19 deletions mypyc/primitives/misc_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

from mypyc.ir.ops import ERR_NEVER, ERR_MAGIC, ERR_FALSE, ERR_NEG_INT
from mypyc.ir.rtypes import (
RTuple, none_rprimitive, bool_rprimitive, object_rprimitive, str_rprimitive,
RTuple, bool_rprimitive, object_rprimitive, str_rprimitive,
int_rprimitive, dict_rprimitive, c_int_rprimitive
)
from mypyc.primitives.registry import (
name_ref_op, simple_emit, unary_op, func_op, custom_op, call_emit, name_emit,
simple_emit, unary_op, func_op, custom_op, call_emit, name_emit,
call_negative_magic_emit, c_function_op, c_custom_op, load_address_op
)

Expand All @@ -19,23 +19,6 @@
emit=name_emit('Py_None'),
is_borrowed=True)

# Get an unboxed None value
none_op = name_ref_op('builtins.None',
result_type=none_rprimitive,
error_kind=ERR_NEVER,
emit=simple_emit('{dest} = 1; /* None */'))

# Get an unboxed True value
true_op = name_ref_op('builtins.True',
result_type=bool_rprimitive,
error_kind=ERR_NEVER,
emit=simple_emit('{dest} = 1;'))

# Get an unboxed False value
false_op = name_ref_op('builtins.False',
result_type=bool_rprimitive,
error_kind=ERR_NEVER,
emit=simple_emit('{dest} = 0;'))

# Get the boxed object '...'
ellipsis_op = custom_op(name='...',
Expand Down
24 changes: 7 additions & 17 deletions mypyc/test-data/analysis.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def f(a):
r2, r3, r4 :: bool
y :: int
z :: int
r5 :: None
L0:
x = 2
r1 = x & 1
Expand All @@ -37,8 +36,7 @@ L4:
L5:
z = 2
L6:
r5 = None
return r5
return 1
(0, 0) {a} {a}
(0, 1) {a} {a, x}
(0, 2) {a, x} {a, x}
Expand Down Expand Up @@ -175,7 +173,6 @@ def f(a):
r2, r3, r4 :: bool
y :: int
x :: int
r5 :: None
L0:
r1 = a & 1
r2 = r1 == 0
Expand All @@ -196,8 +193,7 @@ L4:
L5:
x = 4
L6:
r5 = None
return r5
return 1
(0, 0) {a} {a}
(0, 1) {a} {a}
(0, 2) {a} {a}
Expand Down Expand Up @@ -246,7 +242,6 @@ def f(n):
r3 :: native_int
r4, r5, r6, r7 :: bool
r8, m :: int
r9 :: None
L0:
L1:
r1 = n & 1
Expand All @@ -270,8 +265,7 @@ L5:
m = n
goto L1
L6:
r9 = None
return r9
return 1
(0, 0) {n} {n}
(1, 0) {n} {n}
(1, 1) {n} {n}
Expand Down Expand Up @@ -323,7 +317,6 @@ def f(n):
r10 :: bool
r11 :: native_int
r12, r13, r14, r15 :: bool
r16 :: None
L0:
x = 2
y = 2
Expand Down Expand Up @@ -368,8 +361,7 @@ L10:
L11:
goto L1
L12:
r16 = None
return r16
return 1
(0, 0) {n} {i0, n}
(0, 1) {i0, n} {n, x}
(0, 2) {n, x} {i1, n, x}
Expand Down Expand Up @@ -418,8 +410,8 @@ L12:
(10, 2) {x, y} {n, x, y}
(10, 3) {n, x, y} {n, x, y}
(11, 0) {n, x, y} {n, x, y}
(12, 0) {} {r16}
(12, 1) {r16} {}
(12, 0) {} {i13}
(12, 1) {i13} {}

[case testCall_Liveness]
def f(x: int) -> int:
Expand Down Expand Up @@ -472,7 +464,6 @@ def f(a):
r11 :: native_int
r12, r13, r14, r15 :: bool
y, x :: int
r16 :: None
L0:
L1:
r1 = a & 1
Expand Down Expand Up @@ -514,8 +505,7 @@ L11:
x = a
goto L1
L12:
r16 = None
return r16
return 1
(0, 0) {a} {a}
(1, 0) {a, r0, r8, x, y} {a, r0, r8, x, y}
(1, 1) {a, r0, r8, x, y} {a, r0, r8, x, y}
Expand Down
Loading
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