Repository navigation
Metal: expand signed addition and subtraction with overflow check - #1086
Merged
Merged
Conversation
Apple's macOS 26 back-end computes the overflow flag of
llvm.s{add,sub}.with.overflow wrongly for 16 to 64-bit integers: `0 + -1`
reports an overflow and `typemin + -1` doesn't, while the sum itself is
right. Julia's checked arithmetic, and thus Rational `+` and `-`, threw
spurious OverflowErrors (seen on Metal.jl#1138). macOS 27 is not
affected, nor are unsigned and 8-bit operations.
Expand them into a plain add or sub and a check of the operands' and the
result's signs, next to the existing expansion of multiplications with
overflow check.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1086 +/- ##
==========================================
+ Coverage 92.87% 92.88% +0.01%
==========================================
Files 31 31
Lines 7788 7803 +15
==========================================
+ Hits 7233 7248 +15
Misses 555 555 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
maleadt
added a commit
to JuliaGPU/Metal.jl
that referenced
this pull request
Nov 10, 2026
Apple's macOS 26 back-end computes the overflow flag of signed 16 to 64-bit additions and subtractions wrongly, so checked arithmetic, and `Rational` `+` and `-` with a negative result, threw spurious OverflowErrors. GPUCompiler 2.22.1 expands them instead (JuliaGPU/GPUCompiler.jl#1086).
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.
Apple's macOS 26 back-end computes the overflow flag of
llvm.sadd.with.overflowandllvm.ssub.with.overflowwrongly for 16 to 64-bit integers. The result value is correct, but0 + -1reports an overflow andtypemin + -1doesn't. On an M3 Pro with macOS 26.7, 60 of 144 signed edge-value pairs got the wrong flag for addition and 69 of 144 for subtraction, at every width from i16 to i64. 8-bit and unsigned operations are correct, and macOS 27 (M1) is unaffected.Julia's checked arithmetic uses these intrinsics, so
Rational+and-threw spuriousOverflowErrors whenever the sum was negative. That is the failure on the GitHub Actions macOS 15 and 26 runners of JuliaGPU/Metal.jl#1138. Buildkite only runs macOS 27, so it didn't catch it.This expands the signed intrinsics into a plain
add/suband a check of the operands' and the result's signs, in the same place as the existing expansion of multiplications with overflow check (#1017), after the cleanup whose InstCombine would form the intrinsic again. The expansion is unconditional: it is a few bitwise operations, and gating on the OS version would be fragile.With this change, the edge-value sweep and Metal.jl#1138's
Rationaltests pass on the macOS 26 device.