This is an archive of the discontinued LLVM Phabricator instance.

[mlir][Arith] Remove expansions of integer min and max ops
ClosedPublic

Authored by krzysz00 on Jan 2 2023, 1:40 PM.

Details

Summary

As of several months ago, both ArithToLLVM and ArithToSPIRV have
native support for integer min and max operations. Since these are all
the targets available in MLIR core, the need to "expand" arith.minui,
arith.minsi, arith,maxsi, and arith.manxui to more primitive
operations is to longer present.

Therefore, the expanding of integer min and max operations in Arith,
while correct, is likely to lead to performance loss by way of
misoptimization further down the line, and is no longer needed for
anyone's correctness.

This change may break downstream tests, but will not affect the
semantics of MLIR programs.

arith.minf and arith.maxf have a lot of underlying complexity due to
the many different possible NaN and signed zero semantics available on
various platforms, and so removing their expansion is left to a future
commit.

Diff Detail

Event Timeline

krzysz00 created this revision.Jan 2 2023, 1:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 2 2023, 1:40 PM
krzysz00 requested review of this revision.Jan 2 2023, 1:40 PM

Not directly related to this patch but the lowering of MaxFOp/MinFOp to LLVM doesn't look correct for NaN. MaxFOp defines the semantic for NaN as: If one of the arguments is NaN, then the result is also NaN. (https://github.com/llvm/llvm-project/blob/7df1da08ff9229c12884b408b0f614b8f20d6d5a/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td#L851) while MaxFOp is lowered to llvm intrinsic maxnum (https://github.com/llvm/llvm-project/blob/7df1da08ff9229c12884b408b0f614b8f20d6d5a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp#L56) which has the semantic: If either operand is a NaN, returns the other non-NaN operand. Returns NaN only if both operands are NaN. (https://llvm.org/docs/LangRef.html#llvm-maxnum-intrinsic)

We should probably fix the lowering to LLVM dialect first to avoid regressing functionality. What do you think?

kuhar added a subscriber: gflegar.Jan 3 2023, 7:04 AM

Not directly related to this patch but the lowering of MaxFOp/MinFOp to LLVM doesn't look correct for NaN. MaxFOp defines the semantic for NaN as: If one of the arguments is NaN, then the result is also NaN. (https://github.com/llvm/llvm-project/blob/7df1da08ff9229c12884b408b0f614b8f20d6d5a/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td#L851) while MaxFOp is lowered to llvm intrinsic maxnum (https://github.com/llvm/llvm-project/blob/7df1da08ff9229c12884b408b0f614b8f20d6d5a/mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp#L56) which has the semantic: If either operand is a NaN, returns the other non-NaN operand. Returns NaN only if both operands are NaN. (https://llvm.org/docs/LangRef.html#llvm-maxnum-intrinsic)

We should probably fix the lowering to LLVM dialect first to avoid regressing functionality. What do you think?

+1. I think this is what D137786 by @gflegar was trying to address.

I agree that it'd be a good idea to fix up the LLVM lowering first.

Do y'all think that'l take long enough that I should amend this patch to just remove the integer max/min cases and come back for the float ones later?

Mogball accepted this revision.Jan 3 2023, 8:23 AM

+1 from me, but you might need to add that other patch as a dependency

This revision is now accepted and ready to land.Jan 3 2023, 8:23 AM
krzysz00 updated this revision to Diff 486955.Jan 6 2023, 11:51 AM

Make this only for integers to dodge the minf/maxf question

ThomasRaoux accepted this revision.Jan 6 2023, 11:52 AM
krzysz00 retitled this revision from [mlir][Arith] Remove expansions of min and max ops to [mlir][Arith] Remove expansions of integer min and max ops.Jan 6 2023, 11:53 AM
krzysz00 edited the summary of this revision. (Show Details)