[mlir] [arith] Combine op for select (#234818)
Fold select(c, F(a0, a1), F(b0, b1)) into F(select(c, a0, b0), select(c,
a1, b1))
Correctness provement:
Let's discuss the cases ignoring poison first.
As F is elementwise, we have
F(a1, a2, ..., a_n)[p] = F(a1[p], a2[p], ..., a_n[p])
then for
L = select(M, F(a0, a1, ..., a_n), F(b0, b1, ..., b_n))
R = F(select(M, a0, b0), select(M, a1, b1), ....)
for any position p, we have:
if M[p] == true, then L[p] == F(a)[p] == F(a[p]) == F(select(true, a[p],
b[p])) == R[p]
if M[p] == false, then L[p] == F(b)[p] == F(b[p]) == F(select(false,
a[p], b[p])) == R[p]
And F is pure, so that we can move and reduce F.
So select(M, F(a0, a1, ..., a_n), F(b0, b1, ..., b_n)) is equal to
F(select(M, a0, b0), select(M, a1, b1), ...) ignoring poison.
Let's consider the poison value now. ArithBase.td says:
Unless otherwise stated, the operations within this dialect
propagate poison values, i.e., if any of its inputs are poison, then the
output is poison.
So most F, F(poison) = poison
Then for any position p, if a[p] is poison
if M[p] == true, L[p] == f(a[p]) == poison == F(select(true, a[p],
b[p])) == R[p]
if M[p] == false, L[p] == f(b[p]) == F(select(false, a[p], b[p])) ==
R[p]
if M[p] == P, L[p] == poison == F(select(P, a, b)) == R[p]
Then the transformation doesn't change poison propagation.
For F which doesn't meet the assumption, this transformation may change
the
poison propagation, but the language allows us to replace poison with a
undef
or any value with the same type. So this is still valid transformation.
Observed significant performance in one of our internal triton kernels.
AI assisted for testing generation.