This is an archive of the discontinued LLVM Phabricator instance.

[mlir][spirv] Add ops and patterns for lowering max/mins to CL SPIRV
ClosedPublic

Authored by raikonenfnu on Aug 29 2022, 1:05 PM.

Diff Detail

Event Timeline

raikonenfnu created this revision.Aug 29 2022, 1:05 PM
raikonenfnu requested review of this revision.Aug 29 2022, 1:05 PM
ThomasRaoux added inline comments.Aug 29 2022, 1:14 PM
mlir/test/Conversion/ArithmeticToSPIRV/arithmetic-to-spirv.mlir
1018–1019

maxf has a different semantic regarding Nan unfortunately so this is not correct. We would need a different version of arith.maxf to be able to do that.

antiagainst requested changes to this revision.Aug 29 2022, 5:07 PM
antiagainst added inline comments.
mlir/test/Conversion/ArithmeticToSPIRV/arithmetic-to-spirv.mlir
1018–1019

Good catch! Yeah, for the purpose of lowering arith.maxf/minf under the current semantics, we'd need emit spv.IsNan checks for both operands. If either is true, returns NaN. For other cases just return the normal maxf/minf result. (Vulkan side this also needs to be fixed. But that's a separate story.)

This revision now requires changes to proceed.Aug 29 2022, 5:07 PM

Rebase on top of maxminfPattern

antiagainst requested changes to this revision.Sep 19 2022, 9:14 AM

Generally LGTM, just one remaining issue about the comment.

mlir/lib/Conversion/ArithmeticToSPIRV/ArithmeticToSPIRV.cpp
889–890

spv.CL.fmax/fmin defines the behavior for NaN operands. It's different from OpenGL side: "If one argument is a NaN, Fmin returns the other argument. If both arguments are NaNs, Fmin returns a NaN."

This revision now requires changes to proceed.Sep 19 2022, 9:14 AM
raikonenfnu added inline comments.Sep 19 2022, 9:50 AM
mlir/lib/Conversion/ArithmeticToSPIRV/ArithmeticToSPIRV.cpp
889–890

Hmm, but doesn't the MinMaxFOpPattern help makes the behavior if one of the argument is NaN then it will result in NaN, which is what we want to mimic pytorch/tensorflow behavior?

Updated comment to reflect cl.fmax/fmin

antiagainst accepted this revision.Sep 19 2022, 10:28 AM
This revision is now accepted and ready to land.Sep 19 2022, 10:28 AM
This revision was landed with ongoing or failed builds.Sep 19 2022, 10:34 AM
This revision was automatically updated to reflect the committed changes.