This is an archive of the discontinued LLVM Phabricator instance.

[mlir][tensor] Add patterns that fold ops into pack and unpack ops.
ClosedPublic

Authored by hanchung on Jan 5 2023, 4:57 PM.

Details

Summary

The tensor.pack ops have pad semantic, so we can fold pad + pack into
pack when

  1. They have the same padding values or the pack op does not have padding values.
  2. The pad op does not have low paddings.

The tensor.unpack ops have extract_slice semantic, so we can fold unpack
+ extract_slice into unpack when

  1. All the offsets are 0s.
  2. All the strides are 1s.

Diff Detail

Event Timeline

hanchung created this revision.Jan 5 2023, 4:57 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 5 2023, 4:57 PM
hanchung requested review of this revision.Jan 5 2023, 4:57 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 5 2023, 4:57 PM
chelini added inline comments.Jan 6 2023, 5:32 AM
mlir/lib/Dialect/Tensor/Transforms/FoldIntoPackAndUnpackPatterns.cpp
62

nit: 1 -> ones or 'zeros' -> 0.

mlir/test/Dialect/Tensor/fold-into-pack-and-unpack.mlir
3

The fact that we are not using %arg1 anymore looks a bit suspicious to me. If the tensor.empty allocates using for example: -empty-tensor-to-alloc-tensor the unpacked tensor will end up in the allocation instead of arg1.

tyb0807 added inline comments.Jan 6 2023, 2:24 PM
mlir/lib/Dialect/Tensor/Transforms/FoldIntoPackAndUnpackPatterns.cpp
23–24
hanchung updated this revision to Diff 488028.Jan 10 2023, 4:28 PM
hanchung marked 2 inline comments as done.

address comments

mlir/test/Dialect/Tensor/fold-into-pack-and-unpack.mlir
3

That's why we have it as a pass, not a canonicalization pattern. If we want to use %arg1, we could swap two ops instead of "folding". I think it's fine in this pass because the tensor.empty ops could be aliased to other operations or block arguments through some analysis. We can revisit it when we have concrete examples.

tyb0807 accepted this revision.Jan 11 2023, 12:16 AM

LGTM, thank you!

This revision is now accepted and ready to land.Jan 11 2023, 12:16 AM