[Cranelift] (x * y) (==/!=) z --> x (==/!=) (y / z)#11994
Conversation
| (rule (simplify (isub ty (iadd ty x y) x)) y) | ||
| (rule (simplify (isub ty (iadd ty x y) y)) x) | ||
|
|
||
| ;; (x * y) (==/!=) z --> x (==/!=) (y / z) when y is odd and divides z |
There was a problem hiding this comment.
Could you add "and y and z are constant" to the description here? Otherwise this reads as a fairly odd optimization that is replacing a multiply with a divide, which would ordinarily reduce performance.
Also: why is it necessary that y is odd? Is that because we need it to be mutually prime with the ring's modulus (2^n) so the product doesn't collapse to zero?
There was a problem hiding this comment.
I've changed the description so that one can read that the division happens in compile time.
Also, the solution X for X * C = D is not unique when C is even in 2^n module arithmetic.
Therefore, it is not safe to say X = D / C
Subscribe to Label ActionDetailsThis issue or pull request has been labeled: "cranelift", "isle"Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the |
To clarify, this is the counterexample obtained by Crocus, when Furthermore, there is a similar optimization in LLVM, and the optimization requires When When This shows that the optimization violates the semantic equivalence (or refinement) when Reference implementation in LLVM: https://github.com/llvm/llvm-project/blob/a257a063c6fdcbc1db897d360c3791d8c4f4e48d/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp#L2209-L2230 |
cfallin
left a comment
There was a problem hiding this comment.
Yep, that makes sense -- just wanted to get it written down why. Thanks! Happy to merge once the merge conflicts are resolved.
3ae19a2 to
c161c98
Compare
844f8ca to
982d4f9
Compare
This commit fixes a minor regression from bytecodealliance#11994 where a divide-by-zero was happening in a fuzz-generated input.
This commit fixes a minor regression from bytecodealliance#11994 where a divide-by-zero was happening in a fuzz-generated input.
This commit fixes a minor regression from #11994 where a divide-by-zero was happening in a fuzz-generated input.
…dealliance#11994)" This reverts commit 53d3652.
…dealliance#11994)" This reverts commit 53d3652.
…dealliance#11994)" This reverts commit 53d3652.
…dealliance#11994)" This reverts commit 53d3652.
This add the optimization of
(x * y) (==/!=) z --> x (==/!=) (y / z).ymust be odd and divideszfor the rule to be correct.