Skip to content

Metal: expand signed addition and subtraction with overflow check - #1086

Merged
maleadt merged 2 commits into
mainfrom
tb/add-sub-overflow
Nov 10, 2026
Merged

maleadt merged 2 commits into
mainfrom
tb/add-sub-overflow

Conversation

@maleadt

@maleadt maleadt commented Nov 10, 2026

Copy link
Copy Markdown
Member

Apple's macOS 26 back-end computes the overflow flag of llvm.sadd.with.overflow and llvm.ssub.with.overflow wrongly for 16 to 64-bit integers. The result value is correct, but 0 + -1 reports an overflow and typemin + -1 doesn'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 spurious OverflowErrors 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/sub and 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 Rational tests pass on the macOS 26 device.

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.
@maleadt
maleadt merged commit c06879d into main Nov 10, 2026
31 of 36 checks passed
@maleadt
maleadt deleted the tb/add-sub-overflow branch November 10, 2026 14:15
@codecov

codecov Bot commented Nov 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.88%. Comparing base (09ee8bc) to head (4ff7be7).
⚠️ Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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).
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.

1 participant