Skip to content
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
11 changes: 11 additions & 0 deletions mypyc/irbuild/specialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
from mypyc.primitives.float_ops import isinstance_float
from mypyc.primitives.generic_ops import generic_setattr, setup_object
from mypyc.primitives.int_ops import (
int_abs_op,
int_to_big_endian_op,
int_to_bytes_op,
int_to_little_endian_op,
Expand Down Expand Up @@ -414,6 +415,16 @@ def translate_builtins_with_unary_dunder(
return None


@specialize_function("builtins.abs")
def translate_abs(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
if len(expr.args) == 1 and expr.arg_kinds == [ARG_POS]:
arg_type = builder.node_type(expr.args[0])
if is_fixed_width_rtype(arg_type):
arg = builder.accept(expr.args[0])
return builder.builder.primitive_op(int_abs_op, [arg], expr.line, type_args=[arg_type])
return None


@specialize_function("builtins.len")
def translate_len(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
if len(expr.args) == 1 and expr.arg_kinds == [ARG_POS]:
Expand Down
23 changes: 21 additions & 2 deletions mypyc/lower/int_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

from typing import NamedTuple

from mypyc.ir.ops import Assign, BasicBlock, Branch, ComparisonOp, Register, Value
from mypyc.ir.rtypes import bool_rprimitive, is_short_int_rprimitive
from mypyc.ir.ops import Assign, BasicBlock, Branch, ComparisonOp, Integer, IntOp, Register, Value
from mypyc.ir.rtypes import bool_rprimitive, is_fixed_width_rtype, is_short_int_rprimitive
from mypyc.irbuild.ll_builder import LowLevelIRBuilder
from mypyc.lower.registry import lower_primitive_op
from mypyc.primitives.int_ops import int_equal_, int_less_than_
Expand Down Expand Up @@ -111,3 +111,22 @@ def lower_int_gt(builder: LowLevelIRBuilder, args: list[Value], line: int) -> Va
@lower_primitive_op("int_ge")
def lower_int_ge(builder: LowLevelIRBuilder, args: list[Value], line: int) -> Value:
return compare_tagged(builder, args[0], args[1], ">=", line)


@lower_primitive_op("int_abs")
def lower_int_abs(builder: LowLevelIRBuilder, args: list[Value], line: int) -> Value:
x = args[0]
typ = x.type
assert is_fixed_width_rtype(typ)
if not typ.is_signed:
return x
result = Register(typ)
builder.add(Assign(result, x, line))
neg, out = BasicBlock(), BasicBlock()
is_neg = builder.comparison_op(x, Integer(0, typ), ComparisonOp.SLT, line)
builder.add(Branch(is_neg, neg, out, Branch.BOOL))
builder.activate_block(neg)
negated = builder.int_op(typ, Integer(0, typ), x, IntOp.SUB, line)
builder.add(Assign(result, negated, line))
builder.goto_and_activate(out)
return result
10 changes: 10 additions & 0 deletions mypyc/primitives/int_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)
from mypyc.ir.rtypes import (
RType,
RTypeVar,
bit_rprimitive,
bool_rprimitive,
bytes_rprimitive,
Expand Down Expand Up @@ -214,6 +215,15 @@ def int_unary_op(name: str, c_function_name: str) -> PrimitiveDescription:
error_kind=ERR_NEVER,
)

# abs(x) for fixed-width x, lowered to inline IR
int_abs_op = custom_primitive_op(
name="int_abs",
arg_types=[RTypeVar(0)],
return_type=RTypeVar(0),
error_kind=ERR_NEVER,
type_params=[RTypeVar(0)],
)


# Primitives related to integer comparison operations:

Expand Down
31 changes: 31 additions & 0 deletions mypyc/test-data/lowering-int.test
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,34 @@ L5:
L6:
r11 = <error> :: None
return r11

[case testLowerI64Abs]
from mypy_extensions import i64

def f(x: i64) -> i64:
return abs(x)
[out]
def f(x):
x, r0 :: i64
r1 :: bit
r2 :: i64
L0:
r0 = x
r1 = x < 0 :: signed
if r1 goto L1 else goto L2 :: bool
L1:
r2 = 0 - x
r0 = r2
L2:
return r0

[case testLowerU8Abs]
from mypy_extensions import u8

def f(x: u8) -> u8:
return abs(x)
[out]
def f(x):
x :: u8
L0:
return x
Loading