Skip to content

assert in Reassociate::OptimizeAdd fires when it shouldn't due to miscounted multiplicative factors #6829

Closed

Description

@dneto0

Description
Assert fires on valid code, when asserts are enabled.

Steps to Reproduce
The compiler asserts, when asserts are on, in the following case.
Compile with dxc -HV 2018 a.hlsl -T cs_6_0 -E m

int2 tint_mod(int2 lhs, int rhs) {
  int2 r = int2((rhs).xx);
  int2 rhs_or_one = (((r == (0).xx) | ((lhs == (-2147483648).xx) & (r == (-1).xx))) ? (1).xx : r);
  if (any(((uint2((lhs | rhs_or_one)) & (2147483648u).xx) != (0u).xx))) {
    return (lhs - ((lhs / rhs_or_one) * rhs_or_one));
  } else {
    return (lhs % rhs_or_one);
  }
}

int2 tint_mod_1(int2 lhs, int2 rhs) {
  int2 rhs_or_one = (((rhs == (0).xx) | ((lhs == (-2147483648).xx) & (rhs == (-1).xx))) ? (1).xx : rhs);
  if (any(((uint2((lhs | rhs_or_one)) & (2147483648u).xx) != (0u).xx))) {
    return (lhs - ((lhs / rhs_or_one) * rhs_or_one));
  } else {
    return (lhs % rhs_or_one);
  }
}

[numthreads(1,1,1)]
void m() {
  float x = ddy(0.0f);
  bool2 y = bool2((x>0).xx); // simplified from origial case
  int2 w = ~(~(~(~(~(~(~(-(~(~(~(~(~(int2(y))))))))))))));
  int2 w6 = (w + (((((((~(-(~(~(~(~(~(~(w)))))))) - -(w)) - (-(w) * 2)) - w) - w) - ~(-(w))) - tint_mod(w, 4)) - 1));
  int2 w5 = tint_mod(-(w6), 2); // power of 2 is required here
  int2 w4 = tint_mod(w5, w6.x);
  int2 w3 = tint_mod(w4, 2); // power of 2 is reuquired here
  int2 w2 = tint_mod(w3, 5); // anything more than 1
  int2 w1 = tint_mod(w2, w6.x);
  w = tint_mod(w, w1.x);
  return;
}

Sorry, this is as reduced as I can make it.

The code is in the middle of rewriting expressions like (AB + AC + D) to pull common factor A out, and it finds that there's actually only one A. This can occur when A is a constant, and -A also appears in the terms.

It's a long story, but this has been fixed upstream already, with llvm/llvm-project@95abfa3
That fix ensures that when a negative constant factor is found, don't double count its positive counterpart in case it has already been seen as a factor.

Actual Behavior
The assert is:

opt: /dxc/lib/Transforms/Scalar/Reassociate.cpp:1740: Value *(anonymous namespace)::Reassociate::OptimizeAdd(Instruction *, SmallVectorImpl &): Assertion `NumAddedValues > 1 && "Each occurrence should contribute a value"' failed.
FileCheck error: '-' is empty.

Environment

  • DXC version top of tree.
  • Host Operating System Linux

Activity

  1. added
    bug
    needs-triage
    on Jul 26, 2024
  2. added 2 commits that reference this issue on Jul 26, 2024
    3dc19ef
    8a9ece1
  3. removed this from the Dormant milestone on Jul 30, 2024
  4. moved this from For Google to Triaged in HLSL Triageon Aug 1, 2024
  5. added a commit that references this issue on Aug 1, 2024
    d324261
  6. added a commit that references this issue on Jul 24, 2025
    f6a55c7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions