Repository navigation
spirv-opt: Fix folding of signed division and modulo - #7051
Merged
Merged
Conversation
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.
|
FWIW, I tested this locally. This fixes the slangc crashes behind #7048, and I see that |
Contributor
Author
|
thanks for testing it @skiminki-nv, glad it clears up the slangc crashes :) |
s-perron
approved these changes
Dec 3, 2026
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.
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 :)