[mlir][Vector] Add vector bitwidth target to xfer op flattening by dcaballe · Pull Request #81966 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-mlir-vector

@llvm/pr-subscribers-mlir

Author: Diego Caballero (dcaballe)

Changes

This PR adds an optional bitwidth parameter to the vector xfer op flattening transformation so that the flattening doesn't happen if the trailing dimension of the read/writen vector is larger than this bitwidth (i.e., we are already able to fill at least one vector register with that size).


Full diff: https://github.com/llvm/llvm-project/pull/81966.diff

4 Files Affected:

diff --git a/mlir/include/mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h b/mlir/include/mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h index f5941d32e683fc..cb3b3de8051d6f 100644 --- a/mlir/include/mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h +++ b/mlir/include/mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h @@ -328,8 +328,13 @@ void populateDropUnitDimWithShapeCastPatterns(RewritePatternSet &patterns, /// These patterns insert memref.collapse_shape + vector.shape_cast patterns /// to transform multiple small n-D transfers into a larger 1-D transfer where /// the memref contiguity properties allow it. -void populateFlattenVectorTransferPatterns(RewritePatternSet &patterns,

+/// +/// Flattening is only applied if the bitwidth of the trailing vector dimension +/// is smaller or equal to targetVectorBitwidth. +void populateFlattenVectorTransferPatterns(

/// Collect a set of patterns that bubble up/down bitcast ops. /// diff --git a/mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp b/mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp index b761d1ed888973..04e5a816dd91e6 100644 --- a/mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp +++ b/mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp @@ -19,7 +19,6 @@ #include "mlir/Dialect/Vector/Transforms/LoweringPatterns.h" #include "mlir/Dialect/Vector/Transforms/VectorTransforms.h" #include "mlir/Dialect/Vector/Utils/VectorUtils.h" -#include "mlir/IR/BuiltinOps.h" #include "mlir/IR/Dominance.h" #include "mlir/Interfaces/SideEffectInterfaces.h" #include "llvm/ADT/STLExtras.h" @@ -535,9 +534,17 @@ 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. class FlattenContiguousRowMajorTransferReadPattern : public OpRewritePatternvector::TransferReadOp {

@@ -554,6 +561,12 @@ class FlattenContiguousRowMajorTransferReadPattern // If this is already 0D/1D, there's nothing to do. if (vectorType.getRank() <= 1) return failure();

/// Rewrites contiguous row-major vector.transfer_write ops by inserting @@ -650,7 +668,12 @@ class FlattenContiguousRowMajorTransferReadPattern /// already reduced i.e. without unit dims. class FlattenContiguousRowMajorTransferWritePattern : public OpRewritePatternvector::TransferWriteOp {

@@ -665,6 +688,12 @@ class FlattenContiguousRowMajorTransferWritePattern if (vectorType.getRank() <= 1) // Already 0D/1D, nothing to do. return failure();

/// Base class for vector.extract/vector.extract_element(vector.transfer_read) @@ -917,10 +951,11 @@ void mlir::vector::populateVectorTransferDropUnitDimsPatterns( }

void mlir::vector::populateFlattenVectorTransferPatterns(

patterns.add<FlattenContiguousRowMajorTransferReadPattern, FlattenContiguousRowMajorTransferWritePattern>(

+} + +// CHECK-LABEL: func.func @trailing_dim_larger_than_target_vector_bitwidth_read( +// CHECK-NOT: tensor.collapse_shape + +// ----- + +func.func @trailing_dim_larger_than_target_vector_bitwidth_write(

+} + +// CHECK-LABEL: func.func @trailing_dim_larger_than_target_vector_bitwidth_write( +// CHECK-NOT: tensor.collapse_shape + + + + diff --git a/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp b/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp index 126d65b1b8487f..57d104e80d7243 100644 --- a/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp +++ b/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp @@ -480,7 +480,8 @@ struct TestFlattenVectorTransferPatterns } void runOnOperation() override { RewritePatternSet patterns(&getContext());