Skip to content

[mypyc] Generate lowered IR for abs() on native ints - #22499

Merged
JukkaL merged 1 commit into
python:masterfrom
hnwg:native-int-abs-lowered
Nov 16, 2026
Merged

JukkaL merged 1 commit into
python:masterfrom
hnwg:native-int-abs-lowered

Conversation

@hnwg

@hnwg hnwg commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #22367, as suggested in #22367 (comment).

This generates lowered IR for native integer abs() with a generic primitive op, int_abs, that gets lowered via @lower_primitive_op in mypyc/lower/int_ops.py (kind of like vec_get_item_unsafe_op). Before, mypyc converted the value to a tagged int, called CPyTagged_Absolute and converted the result back.

abs() of the minimum value of a signed type, such as abs(i8(-128)), now overflows like -x instead of raising ValueError. mypyc/doc/int_operations.rst says operations between native integer types don't check for overflow, and there's no run test for -x overflow either, so I didn't add one for abs().

The new lowering tests fail on master.

Generate lowered IR for native integer abs() with a generic primitive
op, int_abs, that gets lowered via @lower_primitive_op in
mypyc/lower/int_ops.py, kind of like vec_get_item_unsafe_op. Before,
mypyc converted the value to a tagged int, called CPyTagged_Absolute and
converted the result back.

abs() of the minimum value of a signed type, such as abs(i8(-128)), now
overflows like -x instead of raising ValueError.
mypyc/doc/int_operations.rst says operations between native integer
types don't check for overflow, and there's no run test for -x overflow
either, so I didn't add one for abs().

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, looks good!

@JukkaL
JukkaL merged commit c2a762e into python:master Nov 16, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants