This is an archive of the discontinued LLVM Phabricator instance.

[mlir][VectorOps] Don't drop scalable dims when lowering transfer_reads/writes (in VectorToSCF)
ClosedPublic

Authored by benmxwl-arm on Aug 24 2023, 9:43 AM.

Details

Summary

This allows the lowering of > rank 1 transfer_reads/writes to equivalent
lower-rank ones when the trailing dimension is scalable. The resulting
ops still cannot be completely lowered as they depend on arrays of
scalable vectors being enabled, and a few related fixes (see D158517).

This patch also explicitly disables lowering transfer_reads/writes with
a leading scalable dimension, as more changes would be needed to handle
that correctly and it is unclear if it is required.

Examples of ops that can now be further lowered:

  %vec = vector.transfer_read %arg0[%c0, %c0], %cst, %mask
		 {in_bounds = [true, true]} : memref<3x?xf32>, vector<3x[4]xf32>

  vector.transfer_write %vec, %arg0[%c0, %c0], %mask
		 {in_bounds = [true, true]} : vector<3x[4]xf32>, memref<3x?xf32>

Diff Detail

Event Timeline

benmxwl-arm created this revision.Aug 24 2023, 9:43 AM
Herald added a reviewer: ftynse. · View Herald Transcript
Herald added a reviewer: dcaballe. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
benmxwl-arm requested review of this revision.Aug 24 2023, 9:43 AM

What about vector.transfer_write? Wouldn't that be affected too?

mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
1872 ↗(On Diff #553174)

Is it just me or does [0, 0, 0, 0] feel wrong? That's unrelated to this change, but it should be something "scalable" instead, right?

mlir/test/Conversion/VectorToSCF/vector-to-scf.mlir
647

An example with the trailing dim other than 1 would be more interesting ;-)

benmxwl-arm marked an inline comment as done.Aug 29 2023, 4:33 AM
benmxwl-arm added inline comments.
mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
1872 ↗(On Diff #553174)

It looks wrong, but I think it's okay.
Looking the langref, only zeroinit (for a splat), undef, or poison are allowed there for scalable vectors.

When actually lowered to LLVM this becomes:

shufflevector <vscale x 4 x i32> %4, <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer

So, this is actually a scalable splat.

https://llvm.org/docs/LangRef.html#shufflevector-instruction

benmxwl-arm marked an inline comment as done.
  • Use something more interesting than a 1-dim for vector-to-scf example :)
  • Add transfer_write tests/example (these changes also fix tranfer writes, as the code is shared)
  • Use VectorType::Builder for dropping dims (rather than abandoned helpers)
benmxwl-arm retitled this revision from [mlir][VectorOps] Don't drop scalable dims when lowering transfer_reads to [mlir][VectorOps] Don't drop scalable dims when lowering transfer_reads/writes.Aug 30 2023, 5:21 AM
benmxwl-arm edited the summary of this revision. (Show Details)
benmxwl-arm marked an inline comment as done.
awarzynski accepted this revision.Sep 1 2023, 6:45 AM

LGTM, thanks!

In the spirit of "documenting through testing", it would be good to add some negative tests, i.e. where vector.transfer_{write|read} _is not_ rewritten because a non-trailing dim is scalable.

mlir/lib/Dialect/Vector/Transforms/LowerVectorMask.cpp
63 ↗(On Diff #554671)

[nit]

This revision is now accepted and ready to land.Sep 1 2023, 6:45 AM
  • Split out vector-to-LLVM changes (these depend on D158517)
  • Added negative tests (and updated patch to avoid hitting an assert on types it cannot handle)
benmxwl-arm retitled this revision from [mlir][VectorOps] Don't drop scalable dims when lowering transfer_reads/writes to [mlir][VectorOps] Don't drop scalable dims when lowering transfer_reads/writes (in VectorToSCF).Sep 7 2023, 7:14 AM
benmxwl-arm edited the summary of this revision. (Show Details)
awarzynski accepted this revision.Sep 7 2023, 7:50 AM

Still LGTM :) Two small suggestions added.

mlir/lib/Conversion/VectorToSCF/VectorToSCF.cpp
314–315

[nit] A return value of std::nullopt indicates failure :) And at this point in time it happens when scalable dims are encounter, but keep in mind that this can change in the future. Saying something like "Vectors with the leading scalable dims are not supported (we cannot unroll scalable dims at compile time)." would, IMHO, a bit more future proof :)

1090

[nit] Could this be xferVecType.getScalableDims()[0] instead? Basically to match this bit furhter down:

1099:    int64_t dimSize = xferVecType.getShape()[0];

#self-documenting-code

  • Fixup some nits
benmxwl-arm marked 2 inline comments as done.Sep 7 2023, 10:31 AM
dcaballe accepted this revision.Sep 7 2023, 1:32 PM
dcaballe added inline comments.
mlir/lib/Conversion/VectorToSCF/VectorToSCF.cpp
317

If optional is used to return a failure, we should better use FailureOr

c-rhodes accepted this revision.Sep 8 2023, 12:47 AM

LGTM barring Diego's comment, cheers

benmxwl-arm updated this revision to Diff 556241.EditedSep 8 2023, 2:41 AM
  • Use FailureOr rather than std::optional for failable unpackOneDim()
This revision was landed with ongoing or failed builds.Sep 8 2023, 2:45 AM
This revision was automatically updated to reflect the committed changes.
benmxwl-arm marked an inline comment as done.Sep 8 2023, 2:47 AM