Restrict bitwise-op non-negativity to i1 results#2652
Open
kimjune01 wants to merge 2 commits into
Open
Conversation
NonNegativeResultAnalysis marked and/or/xor/not as unconditionally
non-negative, which is unsound for signed integers: not(0) == -1, and
or/xor/and can set the sign bit. Guarantee them only for boolean (i1)
results, where the value is provably in {0, 1}. Add a regression test:
abs(not(%i32)) must keep its abs.
Fixes EnzymeAD#2651
wsmoses
approved these changes
Jul 22, 2026
This was referenced Jul 22, 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 #2651.
NonNegativeResultAnalysismarkedand/or/xor/notas unconditionally non-negative. That is unsound for signed integers:not(0) == -1or/xorcan set the sign bitand(a, b)is negative when both are negative (-1 & -2 == -2)A consumer of
guaranteedNonNegativeResultthen mis-fires:AbsPositiveSimplifydrops theabsfromabs(not(%i32)), returning a value that can be negative.Since StableHLO integers are signless, the only case where these ops are provably in
{0, 1}is a boolean (i1) result, so this guards them oni1and leavesabs/exp/iotaunconditional. Strictly stricter -- it only removes unsound proofs.Added
test_signed_bitwisetoabspositive.mlir:abs(not(%i32))must keep itsabs(fails on the pre-fix analysis, passes after).Note: I could not run a local build/FileCheck (heavy LLVM/MLIR/XLA build), so the lit expectation is reasoned against the pass logic, not locally executed; CI is the verifier. Found via codex triage during #2576.