This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Fold Tensor.extract_slice into a constant splat.
ClosedPublic

Authored by okkwon on Feb 16 2022, 3:20 PM.

Details

Summary

Fold arith.extract_slice into arith.constant when the source is a constant
splat and the result type is statically shaped.

Diff Detail

Event Timeline

okkwon created this revision.Feb 16 2022, 3:20 PM
okkwon requested review of this revision.Feb 16 2022, 3:20 PM
mehdi_amini added inline comments.Feb 16 2022, 3:54 PM
mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
1253

The ArrayRef<Attribute> argument to the folder is providing you with the constant inputs, you're not supposed to match it.
So I would move this as the first folder before the two others (because it seems cheaper) and use the folder argument.

okkwon updated this revision to Diff 409448.Feb 16 2022, 4:29 PM

Use the attribute operand instead of matching.

mehdi_amini added inline comments.Feb 16 2022, 4:37 PM
mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
1246

Can you just pass in splat here instead of converting back to a generic attribute? There is overhead associated to this.
Otherwise we should add a method to be able to do: return splat.reshape(newShape);

okkwon added inline comments.Feb 16 2022, 4:49 PM
mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
1246

Passing 'splat' causes an assertion in the get function because it expects an attribute of the element type, not a tensor type. Adding reshape sounds good to me. I will take a look. Thank you!

okkwon updated this revision to Diff 409456.Feb 16 2022, 5:14 PM

Support a different shape for reshape for a splat type.

okkwon marked 2 inline comments as done.Feb 16 2022, 5:16 PM

There is already reshape(), but it didn't support a shape with a different number of element even for a splat type.
I relaxed the assert condition to allow a type with a different number of elements.

This revision is now accepted and ready to land.Feb 16 2022, 6:08 PM
mravishankar requested changes to this revision.Feb 16 2022, 7:54 PM
mravishankar added inline comments.
mlir/include/mlir/IR/BuiltinAttributes.h
655

I think its clearer to keep reshape semantics to preserving the number of elements. Thats the general use of reshape through out MLIR.
We can add a separate slice method maybe to handle this case.

This revision now requires changes to proceed.Feb 16 2022, 7:54 PM
okkwon updated this revision to Diff 409968.Feb 18 2022, 10:11 AM

Introduce resizeSplat instead of using reshape since reshape is not suppposed
to change the number of elements.

okkwon marked an inline comment as done.Feb 18 2022, 10:13 AM
okkwon added inline comments.
mlir/include/mlir/IR/BuiltinAttributes.h
655

I introduced resizeSplat to create a new attribute instead of reshape.

mravishankar accepted this revision.Feb 18 2022, 11:53 AM
This revision is now accepted and ready to land.Feb 18 2022, 11:53 AM
okkwon closed this revision.Feb 22 2022, 1:42 PM
okkwon marked an inline comment as done.

Merged into the LLVM main.