Skip to content

spirv-opt: Fix folding of signed division and modulo - #7051

Merged
s-perron merged 1 commit into
KhronosGroup:mainfrom
apollo-2006:fold-signed-div-overflow
Dec 3, 2026
Merged

s-perron merged 1 commit into
KhronosGroup:mainfrom
apollo-2006:fold-signed-div-overflow

Conversation

@apollo-2006

Copy link
Copy Markdown
Contributor

Fixes #7048
Fixes #7049

Folding a signed div/rem/mod of the minimum 64-bit value by -1 did the division on the host, which overflows and traps. Since x / -1 is -x and x % -1 is 0, those are now folded without dividing (the overflowing case is undefined in SPIR-V, so it just wraps).

OpSMod also added the divisor to a zero remainder when the divisor was negative, so 100 mod -10 folded to -10 instead of 0. A zero remainder is now left alone.

I added a test for each case from the issues (three crash and one fails on main) and the opt tests pass. Happy to change anything :)

Folding OpSDiv, OpSRem or OpSMod of the minimum 64-bit value by -1
did the division on the host, which traps. x / -1 and x % -1 are now
folded without dividing.

OpSMod also added the divisor to a zero remainder when the divisor was
negative, so 100 mod -10 folded to -10 instead of 0.
@skiminki-nv

Copy link
Copy Markdown

FWIW, I tested this locally. This fixes the slangc crashes behind #7048, and I see that OpSMod now gives the correct result for things like OpSMod(100, -10). I did not do a detailed source review.

@dneto0
dneto0 requested a review from s-perron November 30, 2026 22:23
@apollo-2006

Copy link
Copy Markdown
Contributor Author

thanks for testing it @skiminki-nv, glad it clears up the slangc crashes :)

@s-perron
s-perron enabled auto-merge (squash) December 3, 2026 15:36
@s-perron
s-perron merged commit 5c32721 into KhronosGroup:main Dec 3, 2026
25 of 26 checks passed
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.

spirv-opt: Incorrect folding of OpSMod when operand 2 (divisor) is negative and remainder is 0 spirv-opt: SIGFPE on OpSDiv(INT64_MIN, -1)

4 participants