Repository navigation
[mypyc] Make native int arithmetic wrap around on overflow - #22556
Merged
Merged
Conversation
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.
p-sawicki
approved these changes
Nov 26, 2026
This was referenced Nov 26, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.