I would like to continue the discussion about adding ArithFastMathInterface support for arith.select operation here.
The discussion started in [RFC][mlir] Conditional support for fast-math attributes. by vzakhari · Pull Request #125620 · llvm/llvm-project · GitHub, and there are certain arguments against modifying arith.select
this way.
The above PR, actually, is not directly related to modifying arith.select
, so discussing it there may be confusing.
I would like to reiterate on the use-case that was discussed in LLVM when fast-math
flags support was added to select
instruction. Maybe it was added before that and the flags propagation did not work as expected, but the case seems to be a good starter for the arith.select
discussion.
For this example,
double floatingAbs(double x) {
return (x < 0) ? -x : x;
}
One may want to represent it as math.absf
, but it is not equivalent to the straightforward arith.cmpf/arith.select
representation unless there is an assumption that x
cannot be a signed zero, i.e. in case x
is -0.0
the results are the following:
arith.cmpf/arith.select
=> -0.0
math.absf
=> 0.0
So with regards to this case nsz
attached to arith.select
allows such a transformation, and the absence of this flag disallows it.
I am not sure if the fast-math flags can be deduced from the operands of the arith.select
in general, for example all three operations involved in this pattern may be compiled with different setting of the fast-math, e.g.:
bool cmp(double x) { return x < 0; }
double neg(double x) { return -x; }
double floatingAbs(double x) { return cmp(x) ? neg(x) : x; }
All three functions may be compiled with different fast-math options, and I think for the arith.cmpf/arith.select
conversion into math.abs
to work, after the function inlining, exactly the select has to have nsz
attribute.
@andykaylor please correct me if I am wrong. I think this discussion may also be relevant to ClangIR
-based Clang, which will need to represent fast-math (presumably with arith
dialect) for C/C++.
@kuhar, @benvanik can we please continue arith.select
discussion here?