This is an archive of the discontinued LLVM Phabricator instance.

[mlir][StandardOps] Add folders to AddFOp and SubFOp.
AbandonedPublic

Authored by hanchung on Feb 23 2021, 3:48 AM.

Details

Summary
  • addf(x,0) -> x
  • subf(x,0) -> x
  • subf(x,x) -> 0

Diff Detail

Event Timeline

hanchung created this revision.Feb 23 2021, 3:48 AM
hanchung requested review of this revision.Feb 23 2021, 3:48 AM
rriddle added inline comments.Feb 23 2021, 11:00 AM
mlir/lib/Dialect/StandardOps/IR/Ops.cpp
299

Please avoid hardcoding the constant op(hardcoding limits composability), as well as using getZeroAttr(getZeroAttr creates an attribute, which involves uniquing and thus acquiring a lock). operands contains attributes for operands of this operation that are constant, use that instead:

if (FloatAttr rhs = operands[1].dyn_cast_or_null<FloatAttr>())
  if (rhs.getValue().isZero())
    return op.lhs();
hanchung updated this revision to Diff 325850.Feb 23 2021, 11:33 AM

address comments

hanchung marked an inline comment as done.Feb 23 2021, 11:34 AM
hanchung added inline comments.
mlir/lib/Dialect/StandardOps/IR/Ops.cpp
299

I was trying to address vector cases. If this limits composability, let's go with your suggestion.

mehdi_amini added inline comments.Feb 23 2021, 11:39 AM
mlir/lib/Dialect/StandardOps/IR/Ops.cpp
2766

Is this valid even when x is NaN or Infinity?

hanchung marked an inline comment as done.Feb 24 2021, 4:16 AM
hanchung added inline comments.
mlir/lib/Dialect/StandardOps/IR/Ops.cpp
2766

I'm not so familiar with floating point here. In LLVM, this is true when fast-math flag is on. It is always valid from the perspective of Math, but not LLVM IR floating-point operations.

After revisiting some sections in LLVM doc, I also found that other folders in this patch are might not valid.

For example, ‘x-0’->’x’ is not a valid transformation if the rounding mode is “round.downward” or “round.dynamic” because if the value of ‘x’ is +0 then ‘x-0’ should evaluate to ‘-0’ when rounding downward. However, this transformation is legal for all other rounding modes.

https://llvm.org/docs/LangRef.html#constrained-floating-point-intrinsics

I think the question would be, do we map subf/addf to LLVM intrinsics or math operations?

I also noticed that there is a TODO in the doc of addf

TODO: In the distant future, this will accept optional attributes for fast math, contraction, rounding mode, and other controls.

In this context, it seems that we follow LLVM intrinsics, and this is only valid if we have fast math attribute (which is set to true)?

mehdi_amini added inline comments.Feb 24 2021, 9:28 AM
mlir/lib/Dialect/StandardOps/IR/Ops.cpp
2766

By "LLVM intrinsics" you mean "LLVM instructions" here I think? (intrinsics and instructions are different classes in LLVM)?

Unfortunately floating point isn't just arbitrary math and we're limited here.

hanchung abandoned this revision.Feb 24 2021, 10:52 AM
hanchung added inline comments.
mlir/lib/Dialect/StandardOps/IR/Ops.cpp
2766

Yes, I meant LLVM instructions. Got you, thanks!