This is an archive of the discontinued LLVM Phabricator instance.

[MLIR][Linalg] Add minimal support for linalg on tensors with one reduction and one result.
AbandonedPublic

Authored by nicolasvasilache on Sep 11 2020, 6:02 AM.

Details

Summary

This revision allows representing a minimal reduction at the level of linalg on tensors.
When a structured op has a reduction and returns tensor(s), the conventions are:

  1. it can only return a single tensor
  2. it cannot have any output buffer operand
  3. as a consequence of points 1. + 2., it must have exactly one output
  4. its last input argument must be a tensor of the same shape and with the same indexing map as its output.

Points 1-3 keep complexity of the representation in check by allowing only 1 result tensor, when reductions are present.

Point 4 is related to the fact that SSA values cannot represent in-place updates.
Instead, linalg adopts a similar convention that exists in e.g. vector.outerproduct: the value that is reduced into is passed as an explicit argument and a new result of the same shape is produced.

It is expected buffer allocation will fold this last input onto the result in a single output buffer, which is why linalg require the same indexing map: the last input operand is "tied" to the result.

An alternative, more complex representation, would allow for multiple results and arbitrary tied input/result pairs as well as relaxing the conditions on the indexing map equalities on the pairs. This is deemed unnecessarily complex for now and is left for a future discussion.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptSep 11 2020, 6:02 AM
nicolasvasilache requested review of this revision.Sep 11 2020, 6:02 AM

Fix bug, add helpers and a roundtrip test.

ftynse accepted this revision.Sep 11 2020, 9:46 AM
ftynse added inline comments.
mlir/docs/Dialects/Linalg.md
467

Does the tag actually work?

477

It's confusing that args_out comprises the number of both actual outputs and operands-used-as-outputs. args is implicitly associated with "arguments". Consider clarifying this point in the text.

479

Can't other ShapedType operands appear after as additional operands?

502

expected that

mlir/include/mlir/Dialect/Linalg/IR/LinalgStructuredOpsInterface.td
27

Documentation does not correspond to what the function does.

66

I wonder if there is ever a non-default implementation? If not, I'd rather put them into extraClassDeclarations that are much easier to read.

mlir/include/mlir/Dialect/Linalg/IR/LinalgTraits.h
96

Are there other intended uses of getReductionDims? Otherwise, it looks like it is constructing AffineDimExpr only to extract the position back from it, and it would have been simpler to just return positions directly.

110

Spurious comment?

This revision is now accepted and ready to land.Sep 11 2020, 9:46 AM
mehdi_amini added inline comments.Sep 12 2020, 7:44 PM
mlir/docs/Dialects/Linalg.md
467

It shouldn't be necessary: the HTML generator is adding a tag with the title name for each section.

479

I'm not following what are the args_out after the n results?

nicolasvasilache abandoned this revision.Sep 16 2020, 1:56 AM

Thanks for your reviews, I have something better planned so I am abandonning this one.