-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][vector] Add support for lowering n-D vector.to_elements op. #156992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| //===- LowerVectorToElements.cpp - Lower 'vector.to_elements' op ----------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file implements target-independent rewrites and utilities to lower the | ||
| // 'vector.to_elements' operation. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "mlir/Dialect/Vector/IR/VectorOps.h" | ||
| #include "mlir/Dialect/Vector/Transforms/LoweringPatterns.h" | ||
|
|
||
| #define DEBUG_TYPE "lower-vector-to-elements" | ||
|
|
||
| using namespace mlir; | ||
|
|
||
| namespace { | ||
|
|
||
| /// Flattens 2 or more dimensional `vector.to_elements` ops by | ||
| /// `vector.shape_cast` + `vector.to_elements`. | ||
| struct FlattenToElements : OpRewritePattern<vector::ToElementsOp> { | ||
| using OpRewritePattern::OpRewritePattern; | ||
|
|
||
| LogicalResult matchAndRewrite(vector::ToElementsOp op, | ||
| PatternRewriter &rewriter) const override { | ||
| VectorType vecType = op.getSource().getType(); | ||
| if (vecType.getRank() <= 1) | ||
| return rewriter.notifyMatchFailure( | ||
| op, "the rank is already less than or equal to 1"); | ||
| if (vecType.getNumScalableDims() > 0) | ||
| return rewriter.notifyMatchFailure( | ||
| op, "scalable vector is not yet supported"); | ||
|
Comment on lines
+34
to
+36
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does from_elements support scalable vectors at all? https://mlir.llvm.org/docs/Dialects/Vector/#results-11 I think we can make it an assertion |
||
| auto vec1DType = | ||
| VectorType::get({vecType.getNumElements()}, vecType.getElementType()); | ||
| Value shapeCast = vector::ShapeCastOp::create(rewriter, op.getLoc(), | ||
| vec1DType, op.getSource()); | ||
| rewriter.replaceOpWithNewOp<vector::ToElementsOp>(op, op.getResultTypes(), | ||
| shapeCast); | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| void mlir::vector::populateVectorToElementsLoweringPatterns( | ||
| RewritePatternSet &patterns, PatternBenefit benefit) { | ||
| patterns.add<FlattenToElements>(patterns.getContext(), benefit); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // RUN: mlir-opt %s -test-flatten-vector-to-elements -split-input-file | FileCheck %s | ||
|
|
||
| // CHECK-LABEL: func.func @to_elements_1d( | ||
| // CHECK-SAME: %[[ARG0:.+]]: vector<2xf32> | ||
| // CHECK: %[[RES:.+]]:2 = vector.to_elements %[[ARG0]] : vector<2xf32> | ||
| // CHECK: return %[[RES]]#0, %[[RES]]#1 | ||
| func.func @to_elements_1d(%arg0: vector<2xf32>) -> (f32, f32) { | ||
| %0:2 = vector.to_elements %arg0 : vector<2xf32> | ||
| return %0#0, %0#1 : f32, f32 | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: func.func @to_elements_2d( | ||
| // CHECK-SAME: %[[ARG0:.+]]: vector<2x2xf32> | ||
| // CHECK: %[[CAST:.+]] = vector.shape_cast %[[ARG0]] | ||
| // CHECK: %[[RES:.+]]:4 = vector.to_elements %[[CAST]] : vector<4xf32> | ||
| // CHECK: return %[[RES]]#0, %[[RES]]#1, %[[RES]]#2, %[[RES]]#3 | ||
| func.func @to_elements_2d(%arg0: vector<2x2xf32>) -> (f32, f32, f32, f32) { | ||
| %0:4 = vector.to_elements %arg0 : vector<2x2xf32> | ||
| return %0#0, %0#1, %0#2, %0#3 : f32, f32, f32, f32 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -808,6 +808,28 @@ struct TestUnrollVectorFromElements | |
| } | ||
| }; | ||
|
|
||
| struct TestFlattenVectorToElements | ||
| : public PassWrapper<TestFlattenVectorToElements, | ||
| OperationPass<func::FuncOp>> { | ||
| MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestFlattenVectorToElements) | ||
|
|
||
| StringRef getArgument() const final { | ||
| return "test-flatten-vector-to-elements"; | ||
| } | ||
| StringRef getDescription() const final { | ||
| return "Test flattening patterns for to_elements ops"; | ||
| } | ||
| void getDependentDialects(DialectRegistry ®istry) const override { | ||
| registry.insert<func::FuncDialect, vector::VectorDialect>(); | ||
| } | ||
|
|
||
| void runOnOperation() override { | ||
| RewritePatternSet patterns(&getContext()); | ||
| populateVectorToElementsLoweringPatterns(patterns); | ||
| (void)applyPatternsGreedily(getOperation(), std::move(patterns)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use |
||
| } | ||
| }; | ||
|
|
||
| struct TestFoldArithExtensionIntoVectorContractPatterns | ||
| : public PassWrapper<TestFoldArithExtensionIntoVectorContractPatterns, | ||
| OperationPass<func::FuncOp>> { | ||
|
|
@@ -1083,6 +1105,8 @@ void registerTestVectorLowerings() { | |
|
|
||
| PassRegistration<TestUnrollVectorFromElements>(); | ||
|
|
||
| PassRegistration<TestFlattenVectorToElements>(); | ||
|
|
||
| PassRegistration<TestFoldArithExtensionIntoVectorContractPatterns>(); | ||
|
|
||
| PassRegistration<TestVectorEmulateMaskedLoadStore>(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.