Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions mlir/include/mlir/Dialect/Vector/Transforms/LoweringPatterns.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,20 +306,6 @@ void populateVectorRankReducingFMAPattern(RewritePatternSet &patterns);
void populateVectorToFromElementsToShuffleTreePatterns(
RewritePatternSet &patterns, PatternBenefit benefit = 1);

/// Populate the pattern set with the following patterns:
///
/// [UnrollFromElements]
/// Unrolls 2 or more dimensional `vector.from_elements` ops by unrolling the
/// outermost dimension.
void populateVectorFromElementsLoweringPatterns(RewritePatternSet &patterns,
PatternBenefit benefit = 1);

/// Populate the pattern set with the following patterns:
///
/// [UnrollToElements]
void populateVectorToElementsLoweringPatterns(RewritePatternSet &patterns,
PatternBenefit benefit = 1);

/// Populate the pattern set with the following patterns:
///
/// [ContractionOpToMatmulOpLowering]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,20 @@ void populateVectorUnrollPatterns(RewritePatternSet &patterns,
const UnrollVectorOptions &options,
PatternBenefit benefit = 1);

/// Populate the pattern set with the following patterns:
///
/// [UnrollToElements]
void populateVectorToElementsUnrollPatterns(RewritePatternSet &patterns,
PatternBenefit benefit = 1);

/// Populate the pattern set with the following patterns:
///
/// [UnrollFromElements]
/// Unrolls 2 or more dimensional `vector.from_elements` ops by unrolling the
/// outermost dimension.
void populateVectorFromElementsUnrollPatterns(RewritePatternSet &patterns,
PatternBenefit benefit = 1);

/// Collect a set of leading one dimension removal patterns.
///
/// These patterns insert vector.shape_cast to remove leading one dimensions
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Conversion/GPUCommon/GPUToLLVMConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ void GpuToLLVMConversionPass::runOnOperation() {
/*maxTransferRank=*/1);
// Transform N-D vector.from_elements to 1-D vector.from_elements before
// conversion.
vector::populateVectorFromElementsLoweringPatterns(patterns);
vector::populateVectorFromElementsUnrollPatterns(patterns);
if (failed(applyPatternsGreedily(getOperation(), std::move(patterns))))
return signalPassFailure();
}
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Conversion/GPUToNVVM/LowerGpuOpsToNVVMOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ struct LowerGpuOpsToNVVMOpsPass final
populateGpuRewritePatterns(patterns);
// Transform N-D vector.from_elements to 1-D vector.from_elements before
// conversion.
vector::populateVectorFromElementsLoweringPatterns(patterns);
vector::populateVectorFromElementsUnrollPatterns(patterns);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to add just the unroll patterns for vector.from_elements here? It sounds a more generic unrolling for all the ops should have happen before calling this pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept these since we have transform dialect operations that call these functions. I think I can follow up this PR and remove them, but I would rather keep this PR simple.

if (failed(applyPatternsGreedily(m, std::move(patterns))))
return signalPassFailure();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ void ConvertVectorToLLVMPass::runOnOperation() {
populateVectorStepLoweringPatterns(patterns);
populateVectorRankReducingFMAPattern(patterns);
populateVectorGatherLoweringPatterns(patterns);
populateVectorFromElementsLoweringPatterns(patterns);
populateVectorToElementsLoweringPatterns(patterns);
populateVectorFromElementsUnrollPatterns(patterns);
populateVectorToElementsUnrollPatterns(patterns);
if (armI8MM) {
if (armNeon)
arm_neon::populateLowerContractionToNeonI8MMPatterns(patterns);
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/Vector/TransformOps/VectorTransformOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ void transform::ApplyLowerGatherPatternsOp::populatePatterns(

void transform::ApplyUnrollFromElementsPatternsOp::populatePatterns(
RewritePatternSet &patterns) {
vector::populateVectorFromElementsLoweringPatterns(patterns);
vector::populateVectorFromElementsUnrollPatterns(patterns);
}

void transform::ApplyUnrollToElementsPatternsOp::populatePatterns(
RewritePatternSet &patterns) {
vector::populateVectorToElementsLoweringPatterns(patterns);
vector::populateVectorToElementsUnrollPatterns(patterns);
}

void transform::ApplyLowerScanPatternsOp::populatePatterns(
Expand Down
2 changes: 0 additions & 2 deletions mlir/lib/Dialect/Vector/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ add_mlir_dialect_library(MLIRVectorTransforms
LowerVectorBitCast.cpp
LowerVectorBroadcast.cpp
LowerVectorContract.cpp
LowerVectorFromElements.cpp
LowerVectorGather.cpp
LowerVectorInterleave.cpp
LowerVectorMask.cpp
Expand All @@ -12,7 +11,6 @@ add_mlir_dialect_library(MLIRVectorTransforms
LowerVectorShapeCast.cpp
LowerVectorShuffle.cpp
LowerVectorStep.cpp
LowerVectorToElements.cpp
LowerVectorToFromElementsToShuffleTree.cpp
LowerVectorTransfer.cpp
LowerVectorTranspose.cpp
Expand Down
65 changes: 0 additions & 65 deletions mlir/lib/Dialect/Vector/Transforms/LowerVectorFromElements.cpp

This file was deleted.

53 changes: 0 additions & 53 deletions mlir/lib/Dialect/Vector/Transforms/LowerVectorToElements.cpp

This file was deleted.

94 changes: 92 additions & 2 deletions mlir/lib/Dialect/Vector/Transforms/VectorUnroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Utils/IndexingUtils.h"
#include "mlir/Dialect/Vector/Transforms/LoweringPatterns.h"
#include "mlir/Dialect/Vector/Transforms/VectorTransforms.h"
#include "mlir/Interfaces/VectorInterfaces.h"
#include "llvm/ADT/MapVector.h"
Expand Down Expand Up @@ -809,6 +810,38 @@ struct UnrollBroadcastPattern : public OpRewritePattern<vector::BroadcastOp> {
vector::UnrollVectorOptions options;
};

struct UnrollToElements final : public OpRewritePattern<vector::ToElementsOp> {
UnrollToElements(MLIRContext *context,
const vector::UnrollVectorOptions &options,
PatternBenefit benefit = 1)
: OpRewritePattern<vector::ToElementsOp>(context, benefit),
options(options) {}

LogicalResult matchAndRewrite(vector::ToElementsOp op,
PatternRewriter &rewriter) const override {

TypedValue<VectorType> source = op.getSource();
FailureOr<SmallVector<Value>> result =
vector::unrollVectorValue(source, rewriter);
if (failed(result)) {
return failure();
}
SmallVector<Value> vectors = *result;

SmallVector<Value> results;
for (Value vector : vectors) {
auto subElements =
vector::ToElementsOp::create(rewriter, op.getLoc(), vector);
llvm::append_range(results, subElements.getResults());
}
rewriter.replaceOp(op, results);
return success();
}

private:
vector::UnrollVectorOptions options;
};

/// This pattern unrolls `vector.step` operations according to the provided
/// target unroll shape. It decomposes a large step vector into smaller step
/// vectors (segments) and assembles the result by inserting each computed
Expand Down Expand Up @@ -884,6 +917,50 @@ struct UnrollStepPattern : public OpRewritePattern<vector::StepOp> {
vector::UnrollVectorOptions options;
};

/// Unrolls 2 or more dimensional `vector.from_elements` ops by unrolling the
/// outermost dimension. For example:
/// ```
/// %v = vector.from_elements %e0, %e1, %e2, %e3, %e4, %e5 : vector<2x3xf32>
///
/// ==>
///
/// %0 = ub.poison : vector<2x3xf32>
/// %v0 = vector.from_elements %e0, %e1, %e2 : vector<3xf32>
/// %1 = vector.insert %v0, %0 [0] : vector<3xf32> into vector<2x3xf32>
/// %v1 = vector.from_elements %e3, %e4, %e5 : vector<3xf32>
/// %v = vector.insert %v1, %1 [1] : vector<3xf32> into vector<2x3xf32>
/// ```
///
/// When applied exhaustively, this will produce a sequence of 1-d from_elements
/// ops.
struct UnrollFromElements : OpRewritePattern<vector::FromElementsOp> {
UnrollFromElements(MLIRContext *context,
const vector::UnrollVectorOptions &options,
PatternBenefit benefit = 1)
: OpRewritePattern<vector::FromElementsOp>(context, benefit),
options(options) {}

LogicalResult matchAndRewrite(vector::FromElementsOp op,
PatternRewriter &rewriter) const override {
ValueRange allElements = op.getElements();

auto unrollFromElementsFn = [&](PatternRewriter &rewriter, Location loc,
VectorType subTy, int64_t index) {
size_t subTyNumElements = subTy.getNumElements();
assert((index + 1) * subTyNumElements <= allElements.size() &&
"out of bounds");
ValueRange subElements =
allElements.slice(index * subTyNumElements, subTyNumElements);
return vector::FromElementsOp::create(rewriter, loc, subTy, subElements);
};

return unrollVectorOp(op, rewriter, unrollFromElementsFn);
}

private:
vector::UnrollVectorOptions options;
};

} // namespace

void mlir::vector::populateVectorUnrollPatterns(
Expand All @@ -893,6 +970,19 @@ void mlir::vector::populateVectorUnrollPatterns(
UnrollContractionPattern, UnrollElementwisePattern,
UnrollReductionPattern, UnrollMultiReductionPattern,
UnrollTransposePattern, UnrollGatherPattern, UnrollLoadPattern,
UnrollStorePattern, UnrollBroadcastPattern, UnrollStepPattern>(
patterns.getContext(), options, benefit);
UnrollStorePattern, UnrollBroadcastPattern, UnrollFromElements,
UnrollToElements, UnrollStepPattern>(patterns.getContext(),
options, benefit);
}

void mlir::vector::populateVectorToElementsUnrollPatterns(
RewritePatternSet &patterns, PatternBenefit benefit) {
patterns.add<UnrollToElements>(patterns.getContext(), UnrollVectorOptions(),
benefit);
}

void mlir::vector::populateVectorFromElementsUnrollPatterns(
RewritePatternSet &patterns, PatternBenefit benefit) {
patterns.add<UnrollFromElements>(patterns.getContext(), UnrollVectorOptions(),
benefit);
}
44 changes: 44 additions & 0 deletions mlir/test/Conversion/ConvertToSPIRV/vector-unroll.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,47 @@ func.func @transpose(%arg0 : vector<2x3xi32>) -> (vector<3x2xi32>) {
%0 = vector.transpose %arg0, [1, 0] : vector<2x3xi32> to vector<3x2xi32>
return %0 : vector<3x2xi32>
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like these are the only tests that check for unrolling, although it does not check the conversion from vector to spirv, it just checks that the unrolling was applied. We could also start a new file that runs test-convert-to-spirv with run-vector-unrolling=true as an integration test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we need specific vector unrolling test for SPIR-V but we should add independent test to test/Dialect/Vector/.... I think we have vector-unroll-options.mlir for unrolling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm tasked with making sure that vector.to_elements and vector.from_elements are unrolled during the test-convert-to-spirv lowering, so I think that adding tests here makes sense.

// -----

// In order to verify that the pattern is applied,
// we need to make sure that the the 2d vector does not
// come from the parameters. Otherwise, the pattern
// in unrollVectorsInSignatures which splits the 2d vector
// parameter will take precedent. Similarly, let's avoid
// returning a vector as another pattern would take precendence.

// CHECK-LABEL: @unroll_to_elements_2d
func.func @unroll_to_elements_2d() -> (f32, f32, f32, f32) {
%1 = "test.op"() : () -> (vector<2x2xf32>)
// CHECK: %[[VEC2D:.+]] = "test.op"
// CHECK: %[[VEC0:.+]] = vector.extract %[[VEC2D]][0] : vector<2xf32> from vector<2x2xf32>
// CHECK: %[[VEC1:.+]] = vector.extract %[[VEC2D]][1] : vector<2xf32> from vector<2x2xf32>
// CHECK: %[[RES0:.+]]:2 = vector.to_elements %[[VEC0]]
// CHECK: %[[RES1:.+]]:2 = vector.to_elements %[[VEC1]]
%2:4 = vector.to_elements %1 : vector<2x2xf32>
return %2#0, %2#1, %2#2, %2#3 : f32, f32, f32, f32
}

// -----

// In order to verify that the pattern is applied,
// we need to make sure that the the 2d vector is used
// by an operation and that extracts are not folded away.
// In other words we can't use "test.op" nor return the
// value `%0 = vector.from_elements`

// CHECK-LABEL: @unroll_from_elements_2d
// CHECK-SAME: (%[[ARG0:.+]]: f32, %[[ARG1:.+]]: f32, %[[ARG2:.+]]: f32, %[[ARG3:.+]]: f32)
func.func @unroll_from_elements_2d(%arg0: f32, %arg1: f32, %arg2: f32, %arg3: f32) -> (vector<2x2xf32>) {
// CHECK: %[[VEC0:.+]] = vector.from_elements %[[ARG0]], %[[ARG1]] : vector<2xf32>
// CHECK: %[[VEC1:.+]] = vector.from_elements %[[ARG2]], %[[ARG3]] : vector<2xf32>
%0 = vector.from_elements %arg0, %arg1, %arg2, %arg3 : vector<2x2xf32>

// CHECK: %[[RES0:.+]] = arith.addf %[[VEC0]], %[[VEC0]]
// CHECK: %[[RES1:.+]] = arith.addf %[[VEC1]], %[[VEC1]]
%1 = arith.addf %0, %0 : vector<2x2xf32>

// return %[[RES0]], %%[[RES1]] : vector<2xf32>, vector<2xf32>
return %1 : vector<2x2xf32>
}