-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][vector] Add vector.to_elements unrolling #157142
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
Merged
amd-eochoalo
merged 22 commits into
llvm:main
from
amd-eochoalo:eochoa/2025-09-05/vector-to-elements-lowering
Sep 11, 2025
Merged
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
d661413
[mlir][vector] Add support for lowering n-D vector.to_elements op.
hanhanW edf4019
Add new populate patterns for flattening.
amd-eochoalo 12fd6fc
[mlir][vector] Add function to unroll vectors.
amd-eochoalo 0415e3b
[mlir][vector] Add vector.to_elements unrolling.
amd-eochoalo 1b21117
Split tests for unrolling and flattening to elements
amd-eochoalo 567af4b
Fix test
amd-eochoalo c58b5c4
Address review comments
amd-eochoalo 871f5a5
Remove unnecessary comment
amd-eochoalo 244eed5
Address review comments
amd-eochoalo 9933387
Add comment
amd-eochoalo 351086b
Disable and move vector flattening
amd-eochoalo 9f7d15d
Re-enable and rename flattening to linearize
amd-eochoalo a8f9b9c
Remove LinearizeToElements
amd-eochoalo 0568cba
Improve API of unrollVectorValue.
amd-eochoalo 00cc94b
Documentation
amd-eochoalo 6894af0
Add transform.apply_patterns.vector.unroll_to_elements
amd-eochoalo 82004a2
Minor changes
amd-eochoalo cb6cf99
Remove comment and use inline storage for SmallVector
amd-eochoalo db83d2f
Add test with transform interpreter
amd-eochoalo ccec33c
Use transform dialect library file
amd-eochoalo ecca0d0
Merge branch 'main' into eochoa/2025-09-05/vector-to-elements-lowering
amd-eochoalo 7e52d00
Update mlir/test/Dialect/Vector/lit.local.cfg
amd-eochoalo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
mlir/lib/Dialect/Vector/Transforms/LowerVectorToElements.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| //===- 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 { | ||
|
|
||
| struct UnrollToElements final : public OpRewritePattern<vector::ToElementsOp> { | ||
| using OpRewritePattern::OpRewritePattern; | ||
|
|
||
| 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 (const Value &vector : vectors) { | ||
| auto subElements = | ||
| vector::ToElementsOp::create(rewriter, op.getLoc(), vector); | ||
| llvm::append_range(results, subElements.getResults()); | ||
| } | ||
| rewriter.replaceOp(op, results); | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| void mlir::vector::populateVectorToElementsLoweringPatterns( | ||
| RewritePatternSet &patterns, PatternBenefit benefit) { | ||
| patterns.add<UnrollToElements>(patterns.getContext(), benefit); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Skip the directory with input TD sequences | ||
amd-eochoalo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| config.excludes = ["td"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module attributes {transform.with_named_sequence} { | ||
| transform.named_sequence @unroll_to_elements(%module_op: !transform.any_op {transform.readonly}) { | ||
| %f = transform.structured.match ops{["func.func"]} in %module_op | ||
| : (!transform.any_op) -> !transform.any_op | ||
| transform.apply_patterns to %f { | ||
| transform.apply_patterns.vector.transfer_permutation_patterns | ||
| transform.apply_patterns.vector.unroll_to_elements | ||
| } : !transform.any_op | ||
| transform.yield | ||
| } | ||
| } |
amd-eochoalo marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // RUN: mlir-opt %s -test-unroll-vector-to-elements -split-input-file | FileCheck %s | ||
| // RUN: mlir-opt %s -transform-preload-library='transform-library-paths=%p/td/unroll-elements.mlir' \ | ||
| // RUN: -transform-interpreter=entry-point=unroll_to_elements | 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: %[[VEC0:.+]] = vector.extract %[[ARG0]][0] : vector<2xf32> from vector<2x2xf32> | ||
| // CHECK: %[[VEC1:.+]] = vector.extract %[[ARG0]][1] : vector<2xf32> from vector<2x2xf32> | ||
| // CHECK: %[[RES0:.+]]:2 = vector.to_elements %[[VEC0]] : vector<2xf32> | ||
| // CHECK: %[[RES1:.+]]:2 = vector.to_elements %[[VEC1]] : vector<2xf32> | ||
| // CHECK: return %[[RES0]]#0, %[[RES0]]#1, %[[RES1]]#0, %[[RES1]]#1 | ||
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.