This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][Math] Add round operation
ClosedPublic

Authored by chelini on Jun 8 2022, 3:19 AM.

Details

Summary

Introduce RoundOp in the math dialect. The operation rounds the operand to the
nearest integer value in floating-point format. RoundOp lowers to LLVM
intrinsics 'llvm.intr.round' or as a function call to libm (round or roundf).

Diff Detail

Event Timeline

chelini created this revision.Jun 8 2022, 3:19 AM
Herald added a project: Restricted Project. · View Herald Transcript
chelini requested review of this revision.Jun 8 2022, 3:19 AM
ftynse accepted this revision.Jun 8 2022, 3:35 AM
ftynse added inline comments.
mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp
40

Cleanup nit for a separate commit: could we sort these alphabetically?

291

Ditto.

This revision is now accepted and ready to land.Jun 8 2022, 3:35 AM
This revision was automatically updated to reflect the committed changes.
chelini added inline comments.Jun 8 2022, 4:08 AM
mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp
40

Ok I will follow-up.

antiagainst added inline comments.Jun 24 2022, 2:07 PM
mlir/include/mlir/Dialect/Math/IR/MathOps.td
668

The semantics here can be improved. E.g., what's the behavior if the number is x.5? Without knowing that it's hard to properly lower this, e.g., for SPIR-V GLSL round, "The fraction 0.5 will round in a direction chosen by the implementation, presumably the direction that is fastest."

Hi @antiagainst, thanks for the remark. The operation lowers to an llvm intrinsics (llvm.round.*) or directly to libm. The semantics for the intrinsics are the same as those for libm (https://llvm.org/docs/LangRef.html#llvm-round-intrinsic). The libm semantics reads: “ The round functions round their argument to the nearest integer value in floating-point format, rounding halfway cases away from zero, regardless of the current rounding direction. (While the "inexact" floating-point exception behavior is unspecified by the C standard, the round functions are written so that "inexact" is not raised if the result does not equal the argument, which behavior is as recommended by IEEE 754 for its related functions.) .“

I will send out a doc update on Monday.