Motivation
During the discussion through supporting some type of canonicalization for complex dialect, we have found it’s necessary (and straight forward) to support FastMath flag beforehand if we want to run the canonicalization which needs to take into consideration nan, infs. Some optimization passes need the clearer semantics of how we treat mathematical operations.
The same discussion happened for arith dialect before. We have discovered this is also the case for complex dialect.
As pointed before, there is some ambiguity how we treat floating-point lowering depending on the path we take (ComplexToLLVM
vs ComplexToStandard
)
In this RFC, we are proposing to support FastMath flag for complex.dialect ops which allows the canonicalization and other optimization passes to keep the intention properly. We basically assume we had already have the similar discussion when supporting the FastMath flag in arith dialect. If there is something we have to take care specially for the complex dialect, let’s talk about it.
Considerations
- It should be feasible to limit the scope of fast-math flag in the complex dialect context because the flag for the non-arithmetic op (e.g. select) are mostly used for optimization purpose. As discussed in the previous RFC for arith dialect, the current MLIR framework can do such optimization on known-function without supporting
fast-math
attribute - We may want to support the FastMath flag capability in more general and broader way (e.g. builtin). That will allow us to reuse the flag capability in multiple levels. To achieve that, it would be better to have the umbrella flag modeling the FFP behavior applicable across MLIR like clang, which should be able to control the dialect specific FastMath flag behavior in a comprehensive manner. Therefore supporting the dialect specific FastMath flag can be done first and progressively supported by the umbrella flag if needed.
Proposed Changes
- Create
FastMathFlags
attribute as EnumAttr as an optional attribute as we did forarith
dialect. - Define
ComplexFastMathInterface
interface which is supposed to be implemented by the operation supportingFastMathFlags
- Add optional
FastMathFlags
attribute to floating-point operations (e.g.complex.MulOp
) and path the flag to loweredarith
operation - Progressive addition of
fast-math
aware transform/conversion/canonicalization for floating point operation from complex to Arith or LLVM.- We can make use of
AttrConvert
prepared in the arith fast-math support. - Conversion mapping for the flags can be idential with the arith dialect
- We can make use of
Discussions
- A complex op is lowered or converted to multiple arith ops (e.g.
complex.MulOp
). Does it make sense to pass the same fastmath flag to all the lowered operations?- For instance,
complex.MulOp
is lowered to the combination ofarith.MulFOp
,arith.OrI
,arith.XOrI
etc. Is it semantically reasonable to pass the same fastmath flag to all the lowered operations?
- For instance,
Initial Diff
https://reviews.llvm.org/D156310