This is an archive of the discontinued LLVM Phabricator instance.

[mlir][Linalg] Add a transform.structured.pack operation
ClosedPublic

Authored by nicolasvasilache on Jan 16 2023, 9:09 AM.

Details

Summary

This revision introduces a transform.structured.pack operation to
transform any Linalg operation to a higher-dimensional Linalg operation on
packed operands.

tensor.pack (resp. tensor.unpack) operations are inserted for the operands
(resp. results) that need to be packed (resp. unpacked) according to the
packed_sizes specification.

At the moment, the packing operation always pads with getZeroAttr which will
need to be adjusted depending on the consumers.

Packing is limited to those dimensions that are indexed only by AffineDimExpr.
Packing more advanced indexings requires modular arithmetic that is outside the
scoped of a linalg.generic at the moment.

Diff Detail

Event Timeline

nicolasvasilache requested review of this revision.Jan 16 2023, 9:09 AM
nicolasvasilache edited the summary of this revision. (Show Details)

Update

chelini added inline comments.Jan 17 2023, 12:33 AM
mlir/include/mlir/Dialect/Linalg/TransformOps/LinalgTransformOps.td
348

apply -> applying

371

(d2, d1) -> (d0, d1)

373

nit: align with ins?

387

nit: align with ins and tensor<?x?x2x4xf32> -> tensor<?x?x2x3xf32>

389

why this is important to mention here?

395

nit: to adding -> to add

403

Is this reflected in the code? I see it returns only the packed linalg op.

mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp
985

nit: packSizes[dim] -> packSizes[i] or let's use dim in the loop.

1033

/*outerDimsPerm=*/{}

1066

Don't we need to use the builder here? based on this discussion: https://reviews.llvm.org/D137922

1099

where does this restriction come from? tensor.pack/unpack accept dynamic tiles sizes.

mlir/test/Dialect/Linalg/transform-op-pack.mlir
319

nit: align please.

nicolasvasilache marked 9 inline comments as done.

Address first round of comments.

mlir/include/mlir/Dialect/Linalg/TransformOps/LinalgTransformOps.td
373

I usually align the ( in this case, this has the effect of often also aligning the arguments when they are of the same number, and similar type / name.

In such cases it makes sense to extra align the arguments like you propose below.

389

Spelled it out a bit more, plz lmk if this is still unclear.

395

it's: compared to (adding additional permutation attributes)

403

Thanks for catching, it was the original intent but the multiple variadic results make it quite annoying to achieve.

Some amount of static information needs to be injected and it seems better to do that in subsequent transforms.

Cleaning up.

nicolasvasilache marked an inline comment as done.Jan 17 2023, 1:02 AM
ftynse requested changes to this revision.Jan 17 2023, 1:45 AM

Something is off with testing here. And a bug in memory effects.

mlir/include/mlir/Dialect/Linalg/TransformOps/LinalgTransformOps.td
367

Nit: do we really need FileCheck regexp syntax in this example?

407–408

Can we start moving from PDL_Operation in this file, at least for the new operations? Something like TransformHandleTypeInterface is more future-proof and the syntax will need the types to be listed.

mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp
937

Wouldn't it be simpler to contain the SmallVector instead of deriving it? It looks like only push_back is used and needs to be forwarded.

961

This looks suspicious. When would the optional dimension index be < 0? Is it some leftover from a previous iteration where negative values meant the absence of value, before optional?

989–990

Do we want to silently ignore the inability to extract the constant integer? There are two cases: (1) the value is an attribute, but not an integer, which is a clear error; (2) the value is an integer but it's value is dynamic, depending on the plans this is either NYI or another error.

996

Expand auto. Here and below, unless the type is clear from RHS or something like an iterator. (Here it's a rather long optional-of-vector-of-optionals, but this is solvable with a using declaration.)

1013

I'd rather make this function accept an OpBuilder so if the caller has one with the listener attached, it would still know what happens here.

1034–1037

Does this work if the elemental type is a vector?

1047

inits is already a ValueRange, no need to construct it again

1066

+1, we probably want a PatternRewriter here so we can call replaceOp.

1080

Nit: this can do just transformResults.set(getPackedOp().cast<OpResult>(), {});, the code below does.

1086

This is no longer necessary, unset results are automatically set to empty lists of the corresponding kind when a silenceable error is returned.

1093

Ditto. Here and below.

1099

Lack of testing, I suppose. I'm fine with a TODO here and a future patch enabling the dynamic size support.

1110

The documentation said this was a definite failure. Fix either here or in the documentation.

Also note that, since this is a new transformation that lives entirely in the transform ops infra, it can use DiagnosedSilenceableFailure to surface a more detailed error message from within the implementation instead of the stupid LogicalResult/FailureOr.

1120

This is wrong. Packed sizes is an operand, an op cannot produce the values it takes as operands.

mlir/test/Dialect/Linalg/transform-op-pack.mlir
2

This seems to be missing -verify-diagnostics.

315–318

If silenceable errors are suppressed, we shouldn't see the error message here because it is currently emitted as silenceable. We will if it becomes definite.

336–339

We shouldn't see the error message here because of silenceable error supression.

354

Ditto. Here and below.

This revision now requires changes to proceed.Jan 17 2023, 1:45 AM
nicolasvasilache marked 20 inline comments as done.Jan 17 2023, 3:56 AM
nicolasvasilache added inline comments.
mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp
961

indeed, thanks for catching

989–990

Adding support for dynamic values, I was under the mistaken impression that the ops did not support that properly.

1034–1037

I don't see anything that would suggest otherwise, do you have a suspicion of something being broken ?

1086

🔥

1086

Rebase but unfortunately, this PR exhibits crashes if I remove those, added a few TODOs, you should be able to to repro by just commenting out.

nicolasvasilache marked 4 inline comments as done.

Address reviews.

ftynse accepted this revision.Jan 17 2023, 4:07 AM
ftynse added inline comments.
mlir/lib/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp
1086

Okay, I'll take a look.

1133

Nit: I think we sizes can be only used, we don't really need to consume them as we don't destroy/rewrite the corresponding ops AFAICS.

This revision is now accepted and ready to land.Jan 17 2023, 4:07 AM
nicolasvasilache marked an inline comment as done.

Add support for dynamic tile sizes.

This revision was landed with ongoing or failed builds.Jan 17 2023, 5:25 AM
This revision was automatically updated to reflect the committed changes.
nicolasvasilache marked 2 inline comments as done.Jan 17 2023, 5:38 AM