diff options
| author | Andrzej WarzyĆski <andrzej.warzynski@arm.com> | 2024-06-21 10:55:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-21 10:55:45 +0100 |
| commit | 34de7fd4284ce9f02c2ea902f8a8ce5fd256db3d (patch) | |
| tree | 9d7fe3633edd847dcbedaff19d1a19ceb9d3bc21 /mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp | |
| parent | d4d95ee65159db1ea1a8c4159cfdaf8b81097897 (diff) | |
[mlir][vector] Refactor vector-transfer-flatten.mlir (nfc) (1/n) (#95743)
The main goal of this and subsequent PRs is to unify and categorize
tests in:
* vector-transfer-flatten.mlir
This should make it easier to identify the edge cases being tested (and
how they differ), remove duplicates and to add tests for scalable
vectors.
The main contributions of this PR:
* split tests that covered `xfer_read` + `xfer_write` into separate
tests (majority of the existing tests check _one_ xfer Op at a time),
* organise tests for `xfer_read` and `xfer_write` into separate
groups (separate with a big bold comment).
Note, all tests (i.e. test cases) are preserved and some new tests are
added. Deletions that you will see in `git diff` correspond to
`xfer_write` and `xfer_read` Ops being extracted to separate functions
(so that there's one xfer Op per function). In particular, the number of
test functions has grown from 26 to 30.
In addition, this PR unifies the tests so that:
* input variable names are consistent (e.g. make sure that the input
memref is always `arg`)
* CHECK lines use similar indentations
* 2 x tabs are always used for function arguments, 1 x tab for
function body
Finally, changes in "VectorTransferOpTransforms.cpp" are merely meant to
unify comments and logic between
* `FlattenContiguousRowMajorTransferWritePattern` and
* `FlattenContiguousRowMajorTransferReadPattern`.
Diffstat (limited to 'mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp')
| -rw-r--r-- | mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp b/mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp index c131fde517f8..4c93d3841bf8 100644 --- a/mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp +++ b/mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp @@ -568,6 +568,7 @@ namespace { /// memref.collapse_shape on the source so that the resulting /// vector.transfer_read has a 1D source. Requires the source shape to be /// already reduced i.e. without unit dims. +/// /// If `targetVectorBitwidth` is provided, the flattening will only happen if /// the trailing dimension of the vector read is smaller than the provided /// bitwidth. @@ -617,7 +618,7 @@ public: Value collapsedSource = collapseInnerDims(rewriter, loc, source, firstDimToCollapse); MemRefType collapsedSourceType = - dyn_cast<MemRefType>(collapsedSource.getType()); + cast<MemRefType>(collapsedSource.getType()); int64_t collapsedRank = collapsedSourceType.getRank(); assert(collapsedRank == firstDimToCollapse + 1); @@ -658,6 +659,10 @@ private: /// memref.collapse_shape on the source so that the resulting /// vector.transfer_write has a 1D source. Requires the source shape to be /// already reduced i.e. without unit dims. +/// +/// If `targetVectorBitwidth` is provided, the flattening will only happen if +/// the trailing dimension of the vector read is smaller than the provided +/// bitwidth. class FlattenContiguousRowMajorTransferWritePattern : public OpRewritePattern<vector::TransferWriteOp> { public: @@ -674,9 +679,12 @@ public: VectorType vectorType = cast<VectorType>(vector.getType()); Value source = transferWriteOp.getSource(); MemRefType sourceType = dyn_cast<MemRefType>(source.getType()); + + // 0. Check pre-conditions // Contiguity check is valid on tensors only. if (!sourceType) return failure(); + // If this is already 0D/1D, there's nothing to do. if (vectorType.getRank() <= 1) // Already 0D/1D, nothing to do. return failure(); @@ -688,7 +696,6 @@ public: return failure(); if (!vector::isContiguousSlice(sourceType, vectorType)) return failure(); - int64_t firstDimToCollapse = sourceType.getRank() - vectorType.getRank(); // TODO: generalize this pattern, relax the requirements here. if (transferWriteOp.hasOutOfBoundsDim()) return failure(); @@ -697,10 +704,9 @@ public: if (transferWriteOp.getMask()) return failure(); - SmallVector<Value> collapsedIndices = - getCollapsedIndices(rewriter, loc, sourceType.getShape(), - transferWriteOp.getIndices(), firstDimToCollapse); + int64_t firstDimToCollapse = sourceType.getRank() - vectorType.getRank(); + // 1. Collapse the source memref Value collapsedSource = collapseInnerDims(rewriter, loc, source, firstDimToCollapse); MemRefType collapsedSourceType = @@ -708,11 +714,20 @@ public: int64_t collapsedRank = collapsedSourceType.getRank(); assert(collapsedRank == firstDimToCollapse + 1); + // 2. Generate input args for a new vector.transfer_read that will read + // from the collapsed memref. + // 2.1. New dim exprs + affine map SmallVector<AffineExpr, 1> dimExprs{ getAffineDimExpr(firstDimToCollapse, rewriter.getContext())}; auto collapsedMap = AffineMap::get(collapsedRank, 0, dimExprs, rewriter.getContext()); + // 2.2 New indices + SmallVector<Value> collapsedIndices = + getCollapsedIndices(rewriter, loc, sourceType.getShape(), + transferWriteOp.getIndices(), firstDimToCollapse); + + // 3. Create new vector.transfer_write that writes to the collapsed memref VectorType flatVectorType = VectorType::get({vectorType.getNumElements()}, vectorType.getElementType()); Value flatVector = @@ -721,6 +736,9 @@ public: rewriter.create<vector::TransferWriteOp>( loc, flatVector, collapsedSource, collapsedIndices, collapsedMap); flatWrite.setInBoundsAttr(rewriter.getBoolArrayAttr({true})); + + // 4. Replace the old transfer_write with the new one writing the + // collapsed shape rewriter.eraseOp(transferWriteOp); return success(); } |
