This is an archive of the discontinued LLVM Phabricator instance.

[mlir][Math] Add TruncOp.
ClosedPublic

Authored by jacquesguan on Sep 6 2022, 1:12 AM.

Details

Summary

This patch adds TruncOp for Math, it returns the operand rounded to the nearest integer not larger in magnitude than the operand. And this patch also adds the correspond llvm intrinsic op.

Diff Detail

Event Timeline

jacquesguan created this revision.Sep 6 2022, 1:12 AM
jacquesguan requested review of this revision.Sep 6 2022, 1:12 AM
Herald added a project: Restricted Project. · View Herald Transcript

What's the difference between this and arith.truncf?

What's the difference between this and arith.truncf?

arith.truncf would use the default rounding mode and has the similar semantic with fptrunc in llvm ir; math.trunc should always use strict rounding to the nearest integer not larger in magnitude than the operand and has the similar semantic with llvm.trunc in llvm ir or trunc in libm.

What's the difference between this and arith.truncf?

arith.truncf would use the default rounding mode and has the similar semantic with fptrunc in llvm ir; math.trunc should always use strict rounding to the nearest integer not larger in magnitude than the operand and has the similar semantic with llvm.trunc in llvm ir or trunc in libm.

Actually more fundamentally:

  • arith.truncf is really a "truncate" in the sense of "reducing bit size", for example "convert from f32 to f16 and round". But it does not produce an integer value.
  • llvm.trunc is operating on the same bit-size (f32->f32, f64->f64) and is actually just rounding to the nearest integer. It is coming alongside floor and ceil.
Mogball added inline comments.Sep 7 2022, 8:32 AM
mlir/include/mlir/Dialect/Math/IR/MathOps.td
818–819

This seems like a repeat of the first sentence

820
mlir/lib/Conversion/MathToLibm/MathToLibm.cpp
170

Should this be PromoteOpToF32?

Address comment and rebase main.

mlir/include/mlir/Dialect/Math/IR/MathOps.td
818–819

Done.

820

Done.

mlir/lib/Conversion/MathToLibm/MathToLibm.cpp
170

Done.

Mogball accepted this revision.Sep 8 2022, 8:41 AM
This revision is now accepted and ready to land.Sep 8 2022, 8:41 AM
This revision was automatically updated to reflect the committed changes.