Skip to content
Open
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
13 changes: 11 additions & 2 deletions mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,17 @@ def py_call(
) -> Value:
return self.builder.py_call(function, arg_values, line, arg_kinds, arg_names)

def add_bool_branch(self, value: Value, true: BasicBlock, false: BasicBlock) -> None:
self.builder.add_bool_branch(value, true, false)
def add_bool_branch(
self,
value: Value,
true: BasicBlock,
false: BasicBlock,
rare: bool = False,
opt_value_none_rare: bool = False,
) -> None:
self.builder.add_bool_branch(
value, true, false, rare=rare, opt_value_none_rare=opt_value_none_rare
)

def load_native_type_object(self, fullname: str) -> Value:
return self.builder.load_native_type_object(fullname)
Expand Down
2 changes: 1 addition & 1 deletion mypyc/irbuild/callable_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def add_get_to_callable_class(builder: IRBuilder, fn_info: FuncInfo) -> None:
comparison = builder.translate_is_op(
builder.read(instance), builder.none_object(), "is", line
)
builder.add_bool_branch(comparison, class_block, instance_block)
builder.add_bool_branch(comparison, class_block, instance_block, rare=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this one might be questionable


builder.activate_block(class_block)
builder.add(Return(builder.self()))
Expand Down
2 changes: 1 addition & 1 deletion mypyc/irbuild/classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ def setup_non_ext_dict(
non_ext_dict = Register(dict_rprimitive)

true_block, false_block, exit_block = BasicBlock(), BasicBlock(), BasicBlock()
builder.add_bool_branch(has_prepare, true_block, false_block)
builder.add_bool_branch(has_prepare, true_block, false_block, rare=True)

builder.activate_block(true_block)
cls_name = builder.load_str(cdef.name)
Expand Down
2 changes: 1 addition & 1 deletion mypyc/irbuild/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def generate_setattr_wrapper(builder: IRBuilder, cdef: ClassDef, setattr: FuncDe
call_delattr, call_setattr = BasicBlock(), BasicBlock()
null = Integer(0, object_rprimitive, line)
is_delattr = builder.add(ComparisonOp(value_arg, null, ComparisonOp.EQ, line))
builder.add_bool_branch(is_delattr, call_delattr, call_setattr)
builder.add_bool_branch(is_delattr, call_delattr, call_setattr, rare=True)

builder.activate_block(call_delattr)
delattr_symbol = cdef.info.get("__delattr__")
Expand Down
2 changes: 1 addition & 1 deletion mypyc/irbuild/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def add_raise_exception_blocks_to_generator_class(builder: IRBuilder, line: int)
error_block = BasicBlock()
ok_block = BasicBlock()
comparison = builder.translate_is_op(exc_type, builder.none_object(), "is not", line)
builder.add_bool_branch(comparison, error_block, ok_block)
builder.add_bool_branch(comparison, error_block, ok_block, rare=True)

builder.activate_block(error_block)
builder.call_c(raise_exception_with_tb_op, [exc_type, exc_val, exc_tb], line)
Expand Down
16 changes: 12 additions & 4 deletions mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1999,15 +1999,23 @@ def bool_value(self, value: Value) -> Value:
result = self.primitive_op(bool_op, [value], value.line)
return result

def add_bool_branch(self, value: Value, true: BasicBlock, false: BasicBlock) -> None:
def add_bool_branch(
self,
value: Value,
true: BasicBlock,
false: BasicBlock,
*,
rare: bool = False,
opt_value_none_rare: bool = False,
) -> None:
opt_value_type = optional_value_type(value.type)
if opt_value_type is None:
bool_value = self.bool_value(value)
self.add(Branch(bool_value, true, false, Branch.BOOL))
self.add(Branch(bool_value, true, false, Branch.BOOL, rare=rare))
else:
# Special-case optional types
is_none = self.translate_is_op(value, self.none_object(), "is not", value.line)
branch = Branch(is_none, true, false, Branch.BOOL)
branch = Branch(is_none, true, false, Branch.BOOL, rare=opt_value_none_rare)
self.add(branch)
always_truthy = False
if isinstance(opt_value_type, RInstance):
Expand All @@ -2024,7 +2032,7 @@ def add_bool_branch(self, value: Value, true: BasicBlock, false: BasicBlock) ->
# unbox_or_cast instead of coerce because we want the
# type to change even if it is a subtype.
remaining = self.unbox_or_cast(value, opt_value_type, value.line)
self.add_bool_branch(remaining, true, false)
self.add_bool_branch(remaining, true, false, rare=rare)

def call_c(
self,
Expand Down