Skip to content

[mypyc] Make native int arithmetic wrap around on overflow - #22556

Merged
JukkaL merged 1 commit into
masterfrom
mypyc-native-int-wrap-arithmetic
Nov 26, 2026
Merged

JukkaL merged 1 commit into
masterfrom
mypyc-native-int-wrap-arithmetic

Conversation

@JukkaL

@JukkaL JukkaL commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Signed overflow is undefined behavior in C, so e.g. i64 or i32 addition, subtraction and multiplication, and unary minus could trigger undefined behavior.

Calculate these int operations using an unsigned type and convert the result back, so that they wrap around (two's complement), like left shifts already work. Types narrower than int need this too, since C promotes them to signed int. Ops that already use an unsigned C type, such as short tagged ints and pointer arithmetic, are emitted as before.

Also wrap signed native int constants around during constant folding, as was already done for unsigned types, so that they match runtime semantics. Previously -i8(-128) was folded to 128, which failed to compile. Literals that are out of range are still reported as errors, as are final initializers whose value depends on wraparound.

Add a no_overflow flag to IntOp for addition, subtraction and multiplication that can't overflow. Such ops are emitted as plain C operations, which C compilers may optimize more aggressively. Nothing uses the flag yet.

CPython's CFLAGS include -fwrapv (3.11 and earlier) or -fno-strict-overflow (3.12 and later), and these are used for setuptools builds by default. When using these flags the generated machine code is expected to be unchanged. I checked the arm64 and x86-64 code emitted for various examples, and all of them compiled the same as before. The change matters when building without these flags, such as with MSVC or build systems that don't use sysconfig CFLAGS, or when overriding the default CFLAGS.

Signed overflow is undefined behavior in C, so e.g. i64 or i32 addition,
subtraction and multiplication, and unary minus could trigger undefined
behavior.

Calculate these int operations using an unsigned type and convert the
result back, so that they wrap around (two's complement), like left
shifts already work.  Types narrower than int need this too, since C
promotes them to signed int. Ops that already use an unsigned C type,
such as short tagged ints and pointer arithmetic, are emitted as
before.

Also wrap signed native int constants around during constant folding,
as was already done for unsigned types, so that they match runtime
semantics.  Previously -i8(-128) was folded to 128, which failed to
compile.  Literals that are out of range are still reported as errors,
as are final initializers whose value depends on wraparound.

Add a no_overflow flag to IntOp for addition, subtraction and
multiplication that can't overflow. Such ops are emitted as plain C
operations, which C compilers may optimize more aggressively. Nothing
uses the flag yet.

CPython's CFLAGS include -fwrapv (3.11 and earlier) or
-fno-strict-overflow (3.12 and later), and these are used for
setuptools builds of extensions.  When using these flags the generated
machine code should be unchanged. I checked the arm64 and x86-64 code
emitted for various examples, and all of them compiled the same as
before. The change matters when building without these flags, such as
with MSVC or build systems that don't use sysconfig CFLAGS, or when
overriding the default CFLAGS.
@JukkaL
JukkaL merged commit cf7282f into master Nov 26, 2026
19 checks passed
@JukkaL
JukkaL deleted the mypyc-native-int-wrap-arithmetic branch November 26, 2026 13:37
JukkaL pushed a commit that referenced this pull request Nov 26, 2026
…2558)

Since #22556, constant folding wraps signed native int results around,
to match runtime semantics. Floor division doesn't wrap around at
runtime, though: `MIN // -1` raises `OverflowError`. So if `MIN` is an
`i64` final with value `-2**63`, `MIN // -1` was folded to `-2**63`,
while the same division of non-constant values raises `OverflowError`.
Before #22556, this was a compile-time error (the folded value was out
of range for `i64`).

Don't fold a native int floor division if the result is out of range, so
that it raises `OverflowError` at runtime. Only signed `MIN // -1` can
overflow. `MIN % -1` is 0 and is still folded.
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