This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][STD] Add safe scalar constant propagation for FPTruncOp
ClosedPublic

Authored by max-kudr on Aug 4 2021, 5:52 PM.

Details

Summary

Perform scalar constant propagation for FPTruncOp only if the resulting value can be represented without precision loss or rounding.

Example:
%cst = constant 1.000000e+00 : f32
%0 = fptrunc %cst : f32 to bf16
-->
%cst = constant 1.000000e+00 : bf16

Diff Detail

Event Timeline

max-kudr created this revision.Aug 4 2021, 5:52 PM
max-kudr requested review of this revision.Aug 4 2021, 5:52 PM

If there is desire to always perform constant propagation, even if rounding or precision loss happens, let me know. Please also specify if warning message would be appropriate in these cases.

mehdi_amini added inline comments.Aug 6 2021, 1:46 AM
mlir/lib/Dialect/StandardOps/IR/Ops.cpp
1356

I think safe would be more accurate here?

mlir/test/Dialect/Standard/canonicalize.mlir
96

You can simplify the checks:

// CHECK: constant 1.444000e+25 : f32
// CHECK: fptrunc

That's enough to check that fptrunc isn't folded and the constant type unchanged.

max-kudr updated this revision to Diff 364868.Aug 6 2021, 12:23 PM
max-kudr retitled this revision from [MLIR][STD] Add 'cautious' scalar constant propagation for FPTruncOp to [MLIR][STD] Add safe scalar constant propagation for FPTruncOp.

Updated according to comments

max-kudr marked 2 inline comments as done.Aug 6 2021, 12:25 PM
This revision is now accepted and ready to land.Aug 6 2021, 4:09 PM