This is an archive of the discontinued LLVM Phabricator instance.

[mlir][Linalg] Pattern to fuse pad operation with elementwise operations.
ClosedPublic

Authored by mravishankar on Dec 30 2021, 10:07 AM.

Details

Summary

Most convolution operations need explicit padding of the input to
ensure all accesses are inbounds. In such cases, having a pad
operation can be a significant overhead. One way to reduce that
overhead is to try to fuse the pad operation with the producer of its
source.

A sequence

linalg.generic -> linalg.pad_tensor

can be replaced with

linalg.fill -> tensor.extract_slice -> linalg.generic ->
tensor.insert_slice.

if the linalg.generic has all parallel iterator types.

Diff Detail

Event Timeline

mravishankar created this revision.Dec 30 2021, 10:07 AM
mravishankar requested review of this revision.Dec 30 2021, 10:07 AM
antiagainst requested changes to this revision.Jan 10 2022, 11:08 AM

Nice, thanks Mahesh! Generally LGTM. Only blocking issue is the new pass, which seems unnecessary to me.

mlir/include/mlir/Dialect/Linalg/Transforms/Transforms.h
93

..WithProducerGeneric.. to be clear?

mlir/lib/Dialect/Linalg/Transforms/PadOpInterchange.cpp
97

Nit: just use else here.

112

Nit: replaceOpWithNewOp?

mlir/test/Dialect/Linalg/pad_fusion.mlir
94

Add a negative case for non-parallel generic ops?

mlir/test/lib/Dialect/Linalg/TestPadFusion.cpp
2

Do we really need a new pass for this? Can we add another case in https://github.com/llvm/llvm-project/blob/main/mlir/test/lib/Dialect/Linalg/TestLinalgTransforms.cpp? That seems to be the "uber" pass for testing linalg transforms.

10

with its producer ..

This revision now requires changes to proceed.Jan 10 2022, 11:08 AM
mlir/lib/Dialect/Linalg/Transforms/PadOpInterchange.cpp
10

nit: patterns

49

nit: "not constant padding" and save 2 lines

55

this seems unnecessary, I'd just drop the constraint

82

add a TODO that you could only want to fill the boundary region?

87

This seems generally useful as a helper on the op directly: insert/extractInto/FromPaddedSubRegion

mlir/test/lib/Dialect/Linalg/TestPadFusion.cpp
2

We want to graduate linalg.pad_tensor to tensor.pad, having a separate pass for this will reduce future churn.

nicolasvasilache accepted this revision.Jan 11 2022, 4:32 AM
antiagainst accepted this revision.Jan 11 2022, 5:08 AM
antiagainst added inline comments.
mlir/test/lib/Dialect/Linalg/TestPadFusion.cpp
2

Okay that makes sense then.

This revision is now accepted and ready to land.Jan 11 2022, 5:08 AM
mravishankar marked 7 inline comments as done.

Rebase and address comments.

mravishankar marked 5 inline comments as done.

Fix typo.

mlir/lib/Dialect/Linalg/Transforms/PadOpInterchange.cpp
55

Fair enough. I guess then I do need to rename the method.

87

Hmm, not sure how this would work. The pad values are being used to extract the "interior" of a value that is not the source of the pad tensor. So the method would be "use the padding information from the op and extract an equivalent subregion from a different value (which is not the source)". I dont see where that would be useful. In any case, I'll just add a TODO for this here?

mlir/test/Dialect/Linalg/pad_fusion.mlir
94

I dont think that is really needed for now.

This revision was landed with ongoing or failed builds.Jan 11 2022, 1:37 PM
This revision was automatically updated to reflect the committed changes.