-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][VectorOps] Add unrolling for n-D vector.interleave ops (3/4) #80967
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
85 changes: 85 additions & 0 deletions
85
mlir/lib/Dialect/Vector/Transforms/LowerVectorInterleave.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,85 @@ | ||
//===- LowerVectorInterleave.cpp - Lower 'vector.interleave' operation ----===// | ||
// | ||
// 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.interleave' operation. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/Vector/IR/VectorOps.h" | ||
#include "mlir/Dialect/Vector/Transforms/LoweringPatterns.h" | ||
#include "mlir/Dialect/Vector/Utils/VectorUtils.h" | ||
#include "mlir/IR/BuiltinTypes.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
|
||
#define DEBUG_TYPE "vector-interleave-lowering" | ||
|
||
using namespace mlir; | ||
using namespace mlir::vector; | ||
|
||
namespace { | ||
|
||
/// A one-shot unrolling of vector.interleave to the `targetRank`. | ||
/// | ||
/// Example: | ||
/// | ||
/// ```mlir | ||
/// vector.interleave %a, %b : vector<1x2x3x4xi64> | ||
/// ``` | ||
/// Would be unrolled to: | ||
/// ```mlir | ||
/// %result = arith.constant dense<0> : vector<1x2x3x8xi64> | ||
/// %0 = vector.extract %a[0, 0, 0] ─┐ | ||
/// : vector<4xi64> from vector<1x2x3x4xi64> | | ||
/// %1 = vector.extract %b[0, 0, 0] | | ||
/// : vector<4xi64> from vector<1x2x3x4xi64> | - Repeated 6x for | ||
/// %2 = vector.interleave %0, %1 : vector<4xi64> | all leading positions | ||
/// %3 = vector.insert %2, %result [0, 0, 0] | | ||
/// : vector<8xi64> into vector<1x2x3x8xi64> ┘ | ||
/// ``` | ||
/// | ||
/// Note: If any leading dimension before the `targetRank` is scalable the | ||
/// unrolling will stop before the scalable dimension. | ||
class UnrollInterleaveOp : public OpRewritePattern<vector::InterleaveOp> { | ||
public: | ||
UnrollInterleaveOp(int64_t targetRank, MLIRContext *context, | ||
PatternBenefit benefit = 1) | ||
: OpRewritePattern(context, benefit), targetRank(targetRank){}; | ||
|
||
LogicalResult matchAndRewrite(vector::InterleaveOp op, | ||
PatternRewriter &rewriter) const override { | ||
VectorType resultType = op.getResultVectorType(); | ||
auto unrollIterator = vector::createUnrollIterator(resultType, targetRank); | ||
if (!unrollIterator) | ||
return failure(); | ||
|
||
auto loc = op.getLoc(); | ||
Value result = rewriter.create<arith::ConstantOp>( | ||
loc, resultType, rewriter.getZeroAttr(resultType)); | ||
for (auto position : *unrollIterator) { | ||
Value extractLhs = rewriter.create<ExtractOp>(loc, op.getLhs(), position); | ||
Value extractRhs = rewriter.create<ExtractOp>(loc, op.getRhs(), position); | ||
Value interleave = | ||
rewriter.create<InterleaveOp>(loc, extractLhs, extractRhs); | ||
result = rewriter.create<InsertOp>(loc, interleave, result, position); | ||
} | ||
|
||
rewriter.replaceOp(op, result); | ||
return success(); | ||
} | ||
|
||
private: | ||
int64_t targetRank = 1; | ||
}; | ||
|
||
} // namespace | ||
|
||
void mlir::vector::populateVectorInterleaveLoweringPatterns( | ||
RewritePatternSet &patterns, int64_t targetRank, PatternBenefit benefit) { | ||
patterns.add<UnrollInterleaveOp>(targetRank, 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
71 changes: 71 additions & 0 deletions
71
mlir/test/Dialect/Vector/vector-interleave-lowering-transforms.mlir
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,71 @@ | ||
// RUN: mlir-opt %s --transform-interpreter | FileCheck %s | ||
|
||
// CHECK-LABEL: @vector_interleave_2d | ||
// CHECK-SAME: %[[LHS:.*]]: vector<2x3xi8>, %[[RHS:.*]]: vector<2x3xi8>) | ||
func.func @vector_interleave_2d(%a: vector<2x3xi8>, %b: vector<2x3xi8>) -> vector<2x6xi8> | ||
{ | ||
// CHECK-DAG: %[[CST:.*]] = arith.constant dense<0> | ||
// CHECK-DAG: %[[LHS_0:.*]] = vector.extract %[[LHS]][0] | ||
// CHECK-DAG: %[[RHS_0:.*]] = vector.extract %[[RHS]][0] | ||
// CHECK-DAG: %[[LHS_1:.*]] = vector.extract %[[LHS]][1] | ||
// CHECK-DAG: %[[RHS_1:.*]] = vector.extract %[[RHS]][1] | ||
// CHECK-DAG: %[[ZIP_0:.*]] = vector.interleave %[[LHS_0]], %[[RHS_0]] | ||
// CHECK-DAG: %[[ZIP_1:.*]] = vector.interleave %[[LHS_1]], %[[RHS_1]] | ||
// CHECK-DAG: %[[RES_0:.*]] = vector.insert %[[ZIP_0]], %[[CST]] [0] | ||
// CHECK-DAG: %[[RES_1:.*]] = vector.insert %[[ZIP_1]], %[[RES_0]] [1] | ||
// CHECK-NEXT: return %[[RES_1]] : vector<2x6xi8> | ||
%0 = vector.interleave %a, %b : vector<2x3xi8> | ||
return %0 : vector<2x6xi8> | ||
} | ||
|
||
// CHECK-LABEL: @vector_interleave_2d_scalable | ||
// CHECK-SAME: %[[LHS:.*]]: vector<2x[8]xi16>, %[[RHS:.*]]: vector<2x[8]xi16>) | ||
func.func @vector_interleave_2d_scalable(%a: vector<2x[8]xi16>, %b: vector<2x[8]xi16>) -> vector<2x[16]xi16> | ||
{ | ||
// CHECK-DAG: %[[CST:.*]] = arith.constant dense<0> | ||
// CHECK-DAG: %[[LHS_0:.*]] = vector.extract %[[LHS]][0] | ||
// CHECK-DAG: %[[RHS_0:.*]] = vector.extract %[[RHS]][0] | ||
// CHECK-DAG: %[[LHS_1:.*]] = vector.extract %[[LHS]][1] | ||
// CHECK-DAG: %[[RHS_1:.*]] = vector.extract %[[RHS]][1] | ||
// CHECK-DAG: %[[ZIP_0:.*]] = vector.interleave %[[LHS_0]], %[[RHS_0]] | ||
// CHECK-DAG: %[[ZIP_1:.*]] = vector.interleave %[[LHS_1]], %[[RHS_1]] | ||
// CHECK-DAG: %[[RES_0:.*]] = vector.insert %[[ZIP_0]], %[[CST]] [0] | ||
// CHECK-DAG: %[[RES_1:.*]] = vector.insert %[[ZIP_1]], %[[RES_0]] [1] | ||
// CHECK-NEXT: return %[[RES_1]] : vector<2x[16]xi16> | ||
%0 = vector.interleave %a, %b : vector<2x[8]xi16> | ||
return %0 : vector<2x[16]xi16> | ||
} | ||
|
||
// CHECK-LABEL: @vector_interleave_4d | ||
// CHECK-SAME: %[[LHS:.*]]: vector<1x2x3x4xi64>, %[[RHS:.*]]: vector<1x2x3x4xi64>) | ||
func.func @vector_interleave_4d(%a: vector<1x2x3x4xi64>, %b: vector<1x2x3x4xi64>) -> vector<1x2x3x8xi64> | ||
{ | ||
// CHECK: %[[LHS_0:.*]] = vector.extract %[[LHS]][0, 0, 0] : vector<4xi64> from vector<1x2x3x4xi64> | ||
// CHECK: %[[RHS_0:.*]] = vector.extract %[[RHS]][0, 0, 0] : vector<4xi64> from vector<1x2x3x4xi64> | ||
// CHECK: %[[ZIP_0:.*]] = vector.interleave %[[LHS_0]], %[[RHS_0]] : vector<4xi64> | ||
// CHECK: %[[RES_0:.*]] = vector.insert %[[ZIP_0]], %{{.*}} [0, 0, 0] : vector<8xi64> into vector<1x2x3x8xi64> | ||
// CHECK-COUNT-5: vector.interleave %{{.*}}, %{{.*}} : vector<4xi64> | ||
%0 = vector.interleave %a, %b : vector<1x2x3x4xi64> | ||
return %0 : vector<1x2x3x8xi64> | ||
} | ||
|
||
// CHECK-LABEL: @vector_interleave_nd_with_scalable_dim | ||
func.func @vector_interleave_nd_with_scalable_dim(%a: vector<1x3x[2]x2x3x4xf16>, %b: vector<1x3x[2]x2x3x4xf16>) -> vector<1x3x[2]x2x3x8xf16> | ||
{ | ||
// The scalable dim blocks unrolling so only the first two dims are unrolled. | ||
// CHECK-COUNT-3: vector.interleave %{{.*}}, %{{.*}} : vector<[2]x2x3x4xf16> | ||
%0 = vector.interleave %a, %b : vector<1x3x[2]x2x3x4xf16> | ||
return %0 : vector<1x3x[2]x2x3x8xf16> | ||
} | ||
|
||
module attributes {transform.with_named_sequence} { | ||
transform.named_sequence @__transform_main(%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.lower_interleave | ||
} : !transform.any_op | ||
transform.yield | ||
} | ||
} |
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.