Skip to content

Restrict bitwise-op non-negativity to i1 results#2652

Open
kimjune01 wants to merge 2 commits into
EnzymeAD:mainfrom
kimjune01:fix/nonneg-signed-bitwise-2651
Open

Restrict bitwise-op non-negativity to i1 results#2652
kimjune01 wants to merge 2 commits into
EnzymeAD:mainfrom
kimjune01:fix/nonneg-signed-bitwise-2651

Conversation

@kimjune01

Copy link
Copy Markdown
Contributor

Fixes #2651.

NonNegativeResultAnalysis marked and/or/xor/not as unconditionally non-negative. That is unsound for signed integers:

  • not(0) == -1
  • or/xor can set the sign bit
  • and(a, b) is negative when both are negative (-1 & -2 == -2)

A consumer of guaranteedNonNegativeResult then mis-fires: AbsPositiveSimplify drops the abs from abs(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 on i1 and leaves abs/exp/iota unconditional. Strictly stricter -- it only removes unsound proofs.

Added test_signed_bitwise to abspositive.mlir: abs(not(%i32)) must keep its abs (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.

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

NonNegativeResultAnalysis marks signed bitwise ops (and/or/xor/not) as non-negative

2 participants