Closed dkillebrew-g closed 2 years ago
Divide was annoyingly a coverage hole in the fuzzer -- I'm adding that now, and will confirm that it flags this with the coverage hole plugged, and Mark is making sure there were no instances of this that happened in the wild. I believe it should be rare generally since it was only affecting negative signed values being divided, but this was a great catch, thanks for the thoroughness in implementing the new optimization Dan!
Speaking of this code: https://github.com/google/xls/blob/main/xls/passes/arith_simplification_pass.cc#L391-L410
I believe the rewrite of signed division by any power of 2 that's greater than 1 is wrong.
For example, optimize signed divide by 2. It's rewritten to a SRA by 2, which is further rewritten to:
I tested the optimized IR with the interpreter:
I believe the correct result is 0. That is, RoundTowardZero(-1/2) = 0. The interpretation of the optimized function disagrees:
Clearly, -1/2 is not 1023. So arith_simplification_pass is wrong.
I recommend that you disable this optimization immediately. If you're willing to wait a few days/weeks, I will have a solution for all signed and unsigned division by a constant (not just positive powers of 2).
The input to the pass was: