Skip to content

Narrow Julia's signed-remainder guard to the overflowing pair - #1120

Open
maleadt wants to merge 1 commit into
mainfrom
tb/checked-srem
Open

maleadt wants to merge 1 commit into
mainfrom
tb/checked-srem

Conversation

@maleadt

@maleadt maleadt commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Julia defines typemin(T) % -1 == 0, where LLVM's srem is UB, so codegen branches around the srem for every divisor of -1. A conditional srem can't be CSE'd or hoisted, and doesn't pair with a div of the same operands. Only (typemin(T), -1) overflows, though: x % -1 is 0 for any other x.

This pass narrows the guard to that pair. LLVM then removes it whenever either operand rules the pair out, e.g. a zero-extended index. Julia 1.15 does the same in its own codegen since JuliaLang/julia#65218 (1.15.0-DEV.1007), so the pass only runs on older versions and can be removed once those are no longer supported.

RTX 5080, Julia 1.13, CUDA.jl main:

before after
AcceleratedKernels sort!, 2^20 Float32 350 µs 326 µs (−7%)
CUDA.jl sort, 2d (dims=1) 59–62 µs 47 µs (−22%)
CUDA.jl sort, 1d 176–179 µs 167 µs (−6%)
rem.s64 in CUDA.jl's sort kernels 14 3

@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.15686% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.12%. Comparing base (51de390) to head (d567afe).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
src/irgen.jl 92.15% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1120      +/-   ##
==========================================
- Coverage   93.13%   93.12%   -0.01%     
==========================================
  Files          32       32              
  Lines        8574     8625      +51     
==========================================
+ Hits         7985     8032      +47     
- Misses        589      593       +4     

☔ 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 maleadt added the performance Runtime performance. label Nov 25, 2026
Julia's codegen branches around `srem` for every divisor of -1, so that
`typemin(T) % -1` returns 0. Keeping the `srem` conditional can block CSE,
hoisting, and pairing with an `sdiv` of the same operands. Only
`(typemin(T), -1)` overflows, so rewrite the guard on the unoptimized IR to
test for that pair; LLVM then removes it whenever either operand rules the
pair out, e.g. for a zero-extended index.

Julia 1.15 does this itself since JuliaLang/julia#65218 (1.15.0-DEV.1007),
so the rewrite only runs on older versions.

In AcceleratedKernels' merge sort, merge_tile_kernel! goes from four
`rem.s64` to one (451 -> 332 SASS instructions on sm_120) and
merge_pass_kernel!'s remainder pairs with its division (400 -> 305).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Runtime performance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant