Missing LLVM lowering for math::Atan2Op


While working with the math::Atan2Op, I encountered the following error:

error: cannot be converted to LLVM IR: missing `LLVMTranslationDialectInterface` registration for dialect for op: math.atan2
note: see current operation: %48 = "math.atan2"(%44, %47) : (f32, f32) -> f32

Is this because the conversion for math::Atan2Op not present in MathToLLVM.cpp? Also, I see there is another file named MathToLibM.cpp, some ops are present in both and some ops in only one of these files. What’s the purpose of this MathToLibM.cpp?

This specific error comes from the translation, there is no translation for any math dialect op to LLVM IR, you must convert the ops to the LLVM dialect first.

There is no direct conversion either. You may convert the op to a function call form libm using math-to-libm conversion pass, approximate it with a series, or do something else.

MathToLibM is intended as an easy way to use system support for these operations. MathToLLVM is, I believe, primarily intended for simple mappings from math operations to LLVM supported operations. There is also mlir/lib/Dialect/Math/Transforms/PolynomialApproximation.cpp which would create a, usually more efficient, approximation.

I would recommend using the approximation first or MathToLibm second for most use cases. Other forms of support, such as putting the approximation in MathToLLVM won’t be as trivial of a discussion as one might expect because these math operations have many edge cases around zero/NaN semantics and might have different accuracies depending on the magnitude of the values and different projects such as ML vs Physics will have different requirements, preventing a single solution that everyone uses.

I am working on the Torch-MLIR project. I am adding the lowering for torch ops like torch.expm1, torch.atan2, etc. When I added the lowering for torch.expm1 by lowering the op to math::expm1, it worked fine. But when I did the same for torch.atan2 by lowering it to math::atan2, it threw the above-mentioned error. I am not able to differentiate between both the cases, since both are lowered to a math dialect op, and one works fine while the other throws the error. Is there something missing for the math::Atan2Op that needs to be added in the llvm/mlir? @ftynse @tpopp

I am not sufficiently familiar with Torch-MLIR to advise here, @_sean_silva probably is. You seem to be running a rather complex pipeline that combines multiple lowering passes followed by a translation. The latter errors out. We need to know which passes are running to understand what happens.

It sounds like atan2 needs to be implemented in math-to-libm or PolynomialApproximation.cpp

It seems that atan2 lowering to libm is already implemented. Doesn’t it work?


I think we need to make sure to explicitly call the math-to-libm conversion beforehand.

Thank you all, the issue has been resolved now by explicitly calling the math-to-libm pass.