[PATCH] D150100: [KnownBits] Improve implementation of `KnownBits::abs`

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 15 14:12:24 PDT 2023


goldstein.w.n added inline comments.


================
Comment at: llvm/lib/Support/KnownBits.cpp:423
+    KnownBits KnownNeg = computeForAddSub(
+        /*Add*/ false, /*NSW*/ false, Zero, *this);
+
----------------
nikic wrote:
> goldstein.w.n wrote:
> > nikic wrote:
> > > goldstein.w.n wrote:
> > > > goldstein.w.n wrote:
> > > > > nikic wrote:
> > > > > > goldstein.w.n wrote:
> > > > > > > nikic wrote:
> > > > > > > > goldstein.w.n wrote:
> > > > > > > > > nikic wrote:
> > > > > > > > > > You can pass IntMinIsPoison to the NSW argument.
> > > > > > > > > > You can pass IntMinIsPoison to the NSW argument.
> > > > > > > > > 
> > > > > > > > > Can we? The `0-Negative_X` is positive unless `Negative_X == INT_MIN` so if `IntMinIsPoison` we can't set `NSW`. Also would do slightly better and just do `KnownAbs.isNonNegative()` because it uses the `IntMinIsPoison` condition (and others) to try and set output sign.
> > > > > > > > > 
> > > > > > > > > But either way, since `KnownAbs` already has logic for setting output sign and we combine our result from negation with `KnownAbs` its kind of a non-issue.
> > > > > > > > I don't understand. The only value for which `0-X` wraps is INT_MIN, which is exactly what the IntMinIsPoison flag controls. The entire purpose of that flag is to allow an `nsw` assumption on the negation.
> > > > > > > Oh I mistakenly though something like `sub nsw i8 0, 129` would be `poison` but guess not.
> > > > > > > Either way though, we 'join' with the original `abs` value to get the signbit so `nsw` is unneeded no?
> > > > > > But can we drop that join if NSW is passed? Just making this `if (isNegative(X)) return neg(X);` would make this logic a lot cleaner.
> > > > > > But can we drop that join if NSW is passed? Just making this `if (isNegative(X)) return neg(X);` would make this logic a lot cleaner.
> > > > > 
> > > > > I doesn't seem to cover all cases:
> > > > > 
> > > > > ```
> > > > >     KnownBits Zero(getBitWidth());
> > > > >     Zero.setAllZero();
> > > > > 
> > > > >     KnownBits KnownNeg = computeForAddSub(
> > > > >         /*Add*/ false, KnownAbs.isNegative(), Zero, *this);
> > > > > 
> > > > >     // Preserve signbit from `KnownAbs` as it has additional logic for figuring
> > > > >     // it out that we don't want to duplicate here.
> > > > >     KnownBits KnownAbsNoSign = KnownAbs;
> > > > >     KnownBits KnownNegNoSign = KnownNeg;
> > > > >     KnownAbsNoSign.One.clearSignBit();
> > > > >     KnownAbsNoSign.Zero.clearSignBit();
> > > > >     KnownNegNoSign.One.clearSignBit();
> > > > >     KnownNegNoSign.Zero.clearSignBit();
> > > > > 
> > > > >     assert(KnownAbsNoSign.One.isSubsetOf(KnownNegNoSign.One));
> > > > >     assert(KnownAbsNoSign.Zero.isSubsetOf(KnownNegNoSign.Zero));
> > > > >     
> > > > >     assert(KnownAbs.One.isSubsetOf(KnownNeg.One));
> > > > >     assert(KnownAbs.Zero.isSubsetOf(KnownNeg.Zero));
> > > > > 
> > > > >     KnownNeg.One.clearSignBit();
> > > > >     KnownNeg.Zero.clearSignBit();
> > > > >     KnownAbs.One |= KnownNeg.One;
> > > > >     KnownAbs.Zero |= KnownNeg.Zero;
> > > > > ```
> > > > > 
> > > > > On the exhaustive tests fails:
> > > > > 
> > > > > ```
> > > > > KnownBits.cpp:440: llvm::KnownBits llvm::KnownBits::abs(bool) const: Assertion `KnownAbs.Zero.isSubsetOf(KnownNeg.Zero)' failed.
> > > > > ```
> > > > > > But can we drop that join if NSW is passed? Just making this `if (isNegative(X)) return neg(X);` would make this logic a lot cleaner.
> > > > > 
> > > > > I doesn't seem to cover all cases:
> > > > > 
> > > > > ```
> > > > >     KnownBits Zero(getBitWidth());
> > > > >     Zero.setAllZero();
> > > > > 
> > > > >     KnownBits KnownNeg = computeForAddSub(
> > > > >         /*Add*/ false, KnownAbs.isNegative(), Zero, *this);
> > > > 
> > > > This should be `isNonNegative()` but still fails the assertion.
> > > > > 
> > > > >     // Preserve signbit from `KnownAbs` as it has additional logic for figuring
> > > > >     // it out that we don't want to duplicate here.
> > > > >     KnownBits KnownAbsNoSign = KnownAbs;
> > > > >     KnownBits KnownNegNoSign = KnownNeg;
> > > > >     KnownAbsNoSign.One.clearSignBit();
> > > > >     KnownAbsNoSign.Zero.clearSignBit();
> > > > >     KnownNegNoSign.One.clearSignBit();
> > > > >     KnownNegNoSign.Zero.clearSignBit();
> > > > > 
> > > > >     assert(KnownAbsNoSign.One.isSubsetOf(KnownNegNoSign.One));
> > > > >     assert(KnownAbsNoSign.Zero.isSubsetOf(KnownNegNoSign.Zero));
> > > > >     
> > > > >     assert(KnownAbs.One.isSubsetOf(KnownNeg.One));
> > > > >     assert(KnownAbs.Zero.isSubsetOf(KnownNeg.Zero));
> > > > > 
> > > > >     KnownNeg.One.clearSignBit();
> > > > >     KnownNeg.Zero.clearSignBit();
> > > > >     KnownAbs.One |= KnownNeg.One;
> > > > >     KnownAbs.Zero |= KnownNeg.Zero;
> > > > > ```
> > > > > 
> > > > > On the exhaustive tests fails:
> > > > > 
> > > > > ```
> > > > > KnownBits.cpp:440: llvm::KnownBits llvm::KnownBits::abs(bool) const: Assertion `KnownAbs.Zero.isSubsetOf(KnownNeg.Zero)' failed.
> > > > > ```
> > > > 
> > > > 
> > > The argument to the NSW flag should be IntMinIsPoison, not KnownAbs.isNegative(). Effectively you're assuming that the flag is always set, so I would expect that to cause exhaustive test failures.
> > Earlier in the code is:
> > ```
> >   if (IntMinIsPoison || (!One.isZero() && !One.isMinSignedValue())) {
> >     KnownAbs.One.clearSignBit();
> >     KnownAbs.Zero.setSignBit();
> >   }
> > ```
> > 
> > So if `KnownAbs.isNonNegative()` is a superset of `IntMinPoison`.
> Okay, I rewrote our test infrastructure to make optimality failures easier to analyze. For a simple implementation using
> ```
>   if (isNegative())
>     return computeForAddSub(/*Add*/ false, /*NSW*/ IntMinIsPoison,
>                             KnownBits::makeConstant(APInt(getBitWidth(), 0)),
>                             *this);
> ```
> I get these failures:
> ```
> /home/npopov/repos/llvm-project/llvm/unittests/Support/KnownBitsTest.cpp:86: Failure
> Value of: isOptimal(Exact, Computed, Known)
>   Actual: false (Inputs = 1?00, Computed = 0?00, Exact = 0100)
> Expected: true
> /home/npopov/repos/llvm-project/llvm/unittests/Support/KnownBitsTest.cpp:86: Failure
> Value of: isOptimal(Exact, Computed, Known)
>   Actual: false (Inputs = 10??, Computed = 0???, Exact = 01??)
> Expected: true
> /home/npopov/repos/llvm-project/llvm/unittests/Support/KnownBitsTest.cpp:86: Failure
> Value of: isOptimal(Exact, Computed, Known)
>   Actual: false (Inputs = 10?0, Computed = 0??0, Exact = 0110)
> Expected: true
> /home/npopov/repos/llvm-project/llvm/unittests/Support/KnownBitsTest.cpp:86: Failure
> Value of: isOptimal(Exact, Computed, Known)
>   Actual: false (Inputs = 100?, Computed = 0???, Exact = 0111)
> Expected: true
> ```
> Basically, the cases where it is non-optimal are cases where we don't make use of the fact that at least one of the unknown bits must be non-zero, otherwise the input would be IntMin. Those are differences in the non-sign bits though. And the optimality failures (for known negative numbers) that I get for your implementation with the KnownAbs fallback are exactly the same.
> Okay, I rewrote our test infrastructure to make optimality failures easier to analyze. For a simple implementation using
> ```
>   if (isNegative())
>     return computeForAddSub(/*Add*/ false, /*NSW*/ IntMinIsPoison,
>                             KnownBits::makeConstant(APInt(getBitWidth(), 0)),
>                             *this);
> ```
> I get these failures:
> ```
> /home/npopov/repos/llvm-project/llvm/unittests/Support/KnownBitsTest.cpp:86: Failure
> Value of: isOptimal(Exact, Computed, Known)
>   Actual: false (Inputs = 1?00, Computed = 0?00, Exact = 0100)
> Expected: true
> /home/npopov/repos/llvm-project/llvm/unittests/Support/KnownBitsTest.cpp:86: Failure
> Value of: isOptimal(Exact, Computed, Known)
>   Actual: false (Inputs = 10??, Computed = 0???, Exact = 01??)
> Expected: true
> /home/npopov/repos/llvm-project/llvm/unittests/Support/KnownBitsTest.cpp:86: Failure
> Value of: isOptimal(Exact, Computed, Known)
>   Actual: false (Inputs = 10?0, Computed = 0??0, Exact = 0110)
> Expected: true
> /home/npopov/repos/llvm-project/llvm/unittests/Support/KnownBitsTest.cpp:86: Failure
> Value of: isOptimal(Exact, Computed, Known)
>   Actual: false (Inputs = 100?, Computed = 0???, Exact = 0111)
> Expected: true
> ```
> Basically, the cases where it is non-optimal are cases where we don't make use of the fact that at least one of the unknown bits must be non-zero, otherwise the input would be IntMin. Those are differences in the non-sign bits though. And the optimality failures (for known negative numbers) that I get for your implementation with the KnownAbs fallback are exactly the same.

You're right. Its only the poison case. Updating. Also think I can cover all these missing cases so will change to full optimal tests.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150100/new/

https://reviews.llvm.org/D150100



More information about the llvm-commits mailing list